Skip to content

Commit

Permalink
Lowercase headers
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Oct 5, 2024
1 parent 054f1cc commit f6daa84
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/php/websocket/WebSocket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public function connect($headers= []) {
$headers= [];
while ($line= $this->socket->readLine()) {
sscanf($line, "%[^:]: %[^\r]", $header, $value);
$headers[$header][]= $value;
$headers[strtolower($header)][]= $value;
}

$accept= $headers['Sec-Websocket-Accept'][0] ?? '';
$accept= $headers['sec-websocket-accept'][0] ?? '';
$expect= base64_encode(sha1($key.Handshake::GUID, true));
if ($accept !== $expect) {
$this->socket->close();
Expand Down
8 changes: 4 additions & 4 deletions src/main/php/websocket/protocol/Handshake.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public function next($socket, $i) {
$headers= [];
while ($line= $socket->readLine()) {
sscanf($line, "%[^:]: %[^\r]", $header, $value);
$headers[$header][]= $value;
$headers[strtolower($header)][]= $value;
}

$date= gmdate('D, d M Y H:i:s T');
$host= isset($headers['Host']) ? $headers['Host'][0] : $socket->localEndpoint()->getAddress();
$version= isset($headers['Sec-WebSocket-Version']) ? $headers['Sec-WebSocket-Version'][0] : -1;
$host= $headers['host'][0] ?? $socket->localEndpoint()->getAddress();
$version= $headers['sec-websocket-version'][0] ?? -1;
switch ($version) {
case 13:
if (null === ($listener= $this->listeners->listener($path))) {
Expand All @@ -53,7 +53,7 @@ public function next($socket, $i) {
}

// Hash websocket key and well-known GUID
$key= $headers['Sec-WebSocket-Key'][0];
$key= $headers['sec-websocket-key'][0];
$accept= base64_encode(sha1($key.self::GUID, true));
$socket->write(sprintf(
"HTTP/1.1 101 Switching Protocols\r\n".
Expand Down
17 changes: 15 additions & 2 deletions src/test/php/websocket/unittest/WebSocketTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private function fixture($payload= '') {
"HTTP/1.1 101 Switching Protocols\r\n".
"Connection: Upgrade\r\n".
"Upgrade: websocket\r\n".
"Sec-Websocket-Accept: pT25h6EVFbWDyyinkmTBvzUVxQo=\r\n".
"Sec-WebSocket-Accept: pT25h6EVFbWDyyinkmTBvzUVxQo=\r\n".
"\r\n".
$payload
));
Expand Down Expand Up @@ -69,12 +69,25 @@ public function handshake_mismatch() {
"HTTP/1.1 101 Switching Protocols\r\n".
"Connection: Upgrade\r\n".
"Upgrade: websocket\r\n".
"Sec-Websocket-Accept: EGUNIQA7j7p+kiqxH/TKPdu8A4g=\r\n".
"Sec-WebSocket-Accept: EGUNIQA7j7p+kiqxH/TKPdu8A4g=\r\n".
"\r\n"
));
$fixture->connect();
}

#[Test]
public function lowercase_headers() {
$fixture= new WebSocket(new Channel(
"HTTP/1.1 101 Switching Protocols\r\n".
"connection: Upgrade\r\n".
"upgrade: websocket\r\n".
"sec-websocket-accept: pT25h6EVFbWDyyinkmTBvzUVxQo=\r\n".
"\r\n"
));
$fixture->random(function($bytes) { return str_repeat('*', $bytes); });
$fixture->connect();
}

#[Test]
public function connect() {
$fixture= $this->fixture();
Expand Down

0 comments on commit f6daa84

Please sign in to comment.