Commits

Jeffrey Gelens committed 7d0bfc3

Now also supports protocol version 75 (thanks Nicholas Piël)

Comments (0)

Files changed (2)

geventwebsocket/handler.py

 
         self.websocket = WebSocket(self.rfile, self.wfile, self.socket, self.environ)
         self.environ['wsgi.websocket'] = self.websocket
-        challenge = self._get_challenge()
 
-        headers = [
-            ("Upgrade", "WebSocket"),
-            ("Connection", "Upgrade"),
-            ("Sec-WebSocket-Origin", self.websocket.origin),
-            ("Sec-WebSocket-Protocol", self.websocket.protocol),
-            ("Sec-WebSocket-Location", "ws://" + self.environ.get('HTTP_HOST') + self.websocket.path),
-        ]
+        # Detect the Websocket protocol
+        if "HTTP_SEC_WEBSOCKET_KEY1" in self.environ:
+            version = 76
+        else:
+            version = 75
 
-        self.start_response("101 Web Socket Protocol Handshake", headers)
-        self.write([challenge])
+        if version == 75:
+            headers = [
+                ("Upgrade", "WebSocket"),
+                ("Connection", "Upgrade"),
+                ("WebSocket-Origin", self.websocket.origin),
+                ("WebSocket-Protocol", self.websocket.protocol),
+                ("WebSocket-Location", "ws://" + self.environ.get('HTTP_HOST') + self.websocket.path),
+            ]
+            self.start_response("101 Web Socket Protocol Handshake", headers)
+        elif version == 76:
+            challenge = self._get_challenge()
+            headers = [
+                ("Upgrade", "WebSocket"),
+                ("Connection", "Upgrade"),
+                ("Sec-WebSocket-Origin", self.websocket.origin),
+                ("Sec-WebSocket-Protocol", self.websocket.protocol),
+                ("Sec-WebSocket-Location", "ws://" + self.environ.get('HTTP_HOST') + self.websocket.path),
+            ]
+
+            self.start_response("101 Web Socket Protocol Handshake", headers)
+            self.write([challenge])
+        else:
+            raise Exception("Version not supported")
 
         return self.application(self.environ, self.start_response)
 
     def _get_challenge(self):
         key1 = self.environ.get('HTTP_SEC_WEBSOCKET_KEY1')
         key2 = self.environ.get('HTTP_SEC_WEBSOCKET_KEY2')
+
         if not (key1 and key2):
-            message = "Client using old protocol implementation"
+            message = "Client using old/invalid protocol implementation"
             headers = [("Content-Length", str(len(message))),]
             self.start_response("400 Bad Request", headers)
             self.write([message])

tests/test__websocket.py

         "GET /echo HTTP/1.1\r\n" \
         "Host: localhost\r\n" \
         "Connection: Upgrade\r\n" \
-        "WebSocket-Protocol: sample\r\n" \
+        "Sec-WebSocket-Protocol: test\r\n" \
         "Upgrade: WebSocket\r\n" \
-        "Origin: http://example.com\r\n\r\n" \
+        "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5\r\n" \
+        "Origin: http://localhost\r\n\r\n" \
         "^n:ds[4U"
 
         fd.write(headers)
-        read_http(fd, code=400, reason='Bad Request', body='Client using old protocol implementation')
+        read_http(fd, code=400, reason='Bad Request',
+            body='Client using old/invalid protocol implementation')
+
+        fd.close()
+
+    def test_protocol_version75(self):
+        fd = self.connect().makefile(bufsize=1)
+        headers = "" \
+        "GET /echo HTTP/1.1\r\n" \
+        "Host: localhost\r\n" \
+        "Connection: Upgrade\r\n" \
+        "WebSocket-Protocol: sample\r\n" \
+        "Upgrade: WebSocket\r\n" \
+        "Origin: http://example.com\r\n\r\n"
+
+        fd.write(headers)
+        fd.write(self.message)
+        response = read_http(fd, code=101, body=self.message, reason="Web Socket Protocol Handshake")
 
         fd.close()