Skip to content

Commit 22f6a6d

Browse files
author
mateu
committed
WebSocket server behind haproxy. Headers in lowercase.
1 parent bed1714 commit 22f6a6d

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function connect(string $host, int $port, string $path, string $origin =
116116
@fwrite($this->socket, $header);
117117
$response = @fread($this->socket, 1500);
118118

119-
preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches);
119+
preg_match('#Sec-WebSocket-Accept:\s(.*)$#mUi', (string)$response, $matches);
120120

121121
if ($matches) {
122122
$keyAccept = trim($matches[1]);

src/Connection.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ private function handshake(string $data): bool
109109
foreach ($lines as $line) {
110110
$line = chop($line);
111111
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
112-
$headers[$matches[1]] = $matches[2];
112+
$headers[ strtolower( $matches[1] )] = $matches[2];
113113
}
114114
}
115115

116116
// check for supported websocket version:
117-
if (!isset($headers['Sec-WebSocket-Version']) || $headers['Sec-WebSocket-Version'] < 6) {
118-
$this->log('Unsupported websocket version.');
117+
if (!isset($headers['sec-websocket-version']) || $headers['sec-websocket-version'] < 6) {
118+
$this->log('Unsupported websocket version.' );
119119
$this->sendHttpResponse(501);
120120
stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
121121
$this->server->removeClientOnError($this);
@@ -124,8 +124,8 @@ private function handshake(string $data): bool
124124

125125
// check origin:
126126
if ($this->server->getCheckOrigin() === true) {
127-
$origin = (isset($headers['Sec-WebSocket-Origin'])) ? $headers['Sec-WebSocket-Origin'] : '';
128-
$origin = (isset($headers['Origin'])) ? $headers['Origin'] : $origin;
127+
$origin = (isset($headers['sec-websocket-origin'])) ? $headers['sec-websocket-origin'] : '';
128+
$origin = (isset($headers['origin'])) ? $headers['origin'] : $origin;
129129
if (empty($origin)) {
130130
$this->log('No origin provided.');
131131
$this->sendHttpResponse(401);
@@ -144,13 +144,13 @@ private function handshake(string $data): bool
144144
}
145145

146146
// do handyshake: (hybi-10)
147-
$secKey = $headers['Sec-WebSocket-Key'];
147+
$secKey = $headers['sec-websocket-key'];
148148
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
149149
$response = "HTTP/1.1 101 Switching Protocols\r\n";
150150
$response .= "Upgrade: websocket\r\n";
151151
$response .= "Connection: Upgrade\r\n";
152152
$response .= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
153-
if (isset($headers['Sec-WebSocket-Protocol']) && !empty($headers['Sec-WebSocket-Protocol'])) {
153+
if (isset($headers['sec-websocket-protocol']) && !empty($headers['sec-websocket-protocol'])) {
154154
$response .= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n";
155155
}
156156
$response .= "\r\n";

src/Socket.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Socket
2323

2424
public function __construct(string $host = 'localhost', int $port = 8000)
2525
{
26-
ob_implicit_flush(1);
26+
ob_implicit_flush();
2727
$this->createSocket($host, $port);
2828
}
2929

0 commit comments

Comments
 (0)