Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocketServer behind haproxy #24

Closed
mateusan opened this issue Dec 15, 2020 · 3 comments
Closed

WebSocketServer behind haproxy #24

mateusan opened this issue Dec 15, 2020 · 3 comments

Comments

@mateusan
Copy link
Contributor

Hi.

When the server is behind a HaProxy, the HTTP headers come in "lowercase"

diff --git a/src/Connection.php b/src/Connection.php
index 98ea406..e9269c2 100644
--- a/src/Connection.php
+++ b/src/Connection.php
@@ -109,13 +109,13 @@ class Connection
         foreach ($lines as $line) {
             $line = chop($line);
             if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
-                $headers[$matches[1]] = $matches[2];
+                $headers[ strtolower( $matches[1] )] = $matches[2];
             }
         }
 
         // check for supported websocket version:
-        if (!isset($headers['Sec-WebSocket-Version']) || $headers['Sec-WebSocket-Version'] < 6) {
-            $this->log('Unsupported websocket version.');
+        if (!isset($headers['sec-websocket-version']) || $headers['sec-websocket-version'] < 6) {
+            $this->log('Unsupported websocket version.'.print_r( $headers, true ) );
             $this->sendHttpResponse(501);
             stream_socket_shutdown($this->socket, STREAM_SHUT_RDWR);
             $this->server->removeClientOnError($this);
@@ -124,8 +124,8 @@ class Connection
 
         // check origin:
         if ($this->server->getCheckOrigin() === true) {
-            $origin = (isset($headers['Sec-WebSocket-Origin'])) ? $headers['Sec-WebSocket-Origin'] : '';
-            $origin = (isset($headers['Origin'])) ? $headers['Origin'] : $origin;
+            $origin = (isset($headers['sec-websocket-origin'])) ? $headers['sec-websocket-origin'] : '';
+            $origin = (isset($headers['origin'])) ? $headers['origin'] : $origin;
             if (empty($origin)) {
                 $this->log('No origin provided.');
                 $this->sendHttpResponse(401);
@@ -144,13 +144,13 @@ class Connection
         }
 
         // do handyshake: (hybi-10)
-        $secKey = $headers['Sec-WebSocket-Key'];
+        $secKey = $headers['sec-websocket-key'];
         $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
         $response = "HTTP/1.1 101 Switching Protocols\r\n";
         $response .= "Upgrade: websocket\r\n";
         $response .= "Connection: Upgrade\r\n";
         $response .= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
-        if (isset($headers['Sec-WebSocket-Protocol']) && !empty($headers['Sec-WebSocket-Protocol'])) {
+        if (isset($headers['sec-websocket-protocol']) && !empty($headers['sec-websocket-protocol'])) {
             $response .= "Sec-WebSocket-Protocol: " . substr($path, 1) . "\r\n";
         }
         $response .= "\r\n";

@nekudo
Copy link
Collaborator

nekudo commented Dec 16, 2020

Could you make a Pull-Request out of this so I can merge it into the repository?

@mateusan
Copy link
Contributor Author

#25

@nekudo
Copy link
Collaborator

nekudo commented Dec 16, 2020

Thanks. I merged the changes into Master.

@nekudo nekudo closed this as completed Dec 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants