Jeffrey Gelens avatar Jeffrey Gelens committed 66b3e7e

First Version 8 test is working in FF and Chrome

Comments (0)

Files changed (3)

examples/websocket.html

 var iets = "";
 window.onload = function() {
     var data = {};
-    var s = new MozWebSocket("ws://localhost:8000/data");
+    //var s = new MozWebSocket("ws://localhost:8000/data");
+    var s = new WebSocket("ws://localhost:8000/data");
     s.onopen = function() {
         //alert('open');
         s.send('hi');
     };
     s.onmessage = function(e) {
 
-      alert('got ' + e.data);
+      //alert('got ' + e.data);
       var lines = e.data.split('\n');
       for (var i = 0; i < lines.length - 1; i++) {
         var parts = lines[i].split(' ');

geventwebsocket/handler.py

 import re
 import struct
 from hashlib import md5, sha1
-from base64 import b64encode, b64decode
 
 from gevent.pywsgi import WSGIHandler
 from geventwebsocket import WebSocketVersion7, WebSocketLegacy
 
 
-
 class HandShakeError(ValueError):
     """ Hand shake challenge can't be parsed """
     pass
 
 
+
 class WebSocketHandler(WSGIHandler):
     """ Automatically upgrades the connection to websockets. """
 
             self._handle_one_legacy_response()
         elif self.environ.get("HTTP_SEC_WEBSOCKET_VERSION"):
             version = int(self.environ.get("HTTP_SEC_WEBSOCKET_VERSION"))
-            if version is 7:
+            if version in self.SUPPORTED_VERSIONS:
                 if not self._handle_one_version7_response():
                     return
             else:
         protocol, version = self.request_version.split("/")
         key = environ.get("HTTP_SEC_WEBSOCKET_KEY")
 
+        print key
+
         # check client handshake for validity
         if not environ.get("REQUEST_METHOD") == "GET":
             # 5.2.1 (1)
         if "upgrade" in self.environ.get("HTTP_CONNECTION", "").lower(). \
              replace(" ", "").split(",") and \
              "websocket" in self.environ.get("HTTP_UPGRADE").lower() and \
-             self.upgrade_allowed():
+             self.accept_upgrade():
             self.websocket_connection = True
         else:
             print "NORMAL"
             pprint(self.environ)
             return super(WebSocketHandler, self).handle_one_response()
 
-        self.init_websocket()
+        self.websocket = WebSocketLegacy(self.socket, self.rfile, self.environ)
         self.environ['wsgi.websocket'] = self.websocket
 
         # Detect the Websocket protocol
 
         return key_number / spaces
 
-    def _get_challenge_hybi00(self):
+    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/invalid protocol implementation"
-            headers = [("Content-Length", str(len(message))),]
-            self.start_response("400 Bad Request", headers)
-            self.write(message)
-            self.close_connection = True
-            return
+        if not key1:
+            raise BadRequest("SEC-WEBSOCKET-KEY1 header is missing")
+        if not key2:
+            raise BadRequest("SEC-WEBSOCKET-KEY2 header is missing")
 
         part1 = self._get_key_value(self.environ['HTTP_SEC_WEBSOCKET_KEY1'])
         part2 = self._get_key_value(self.environ['HTTP_SEC_WEBSOCKET_KEY2'])
         # This request should have 8 bytes of data in the body
         key3 = self.rfile.read(8)
 
-        challenge = ""
-        challenge += struct.pack("!I", part1)
-        challenge += struct.pack("!I", part2)
-        challenge += key3
+        return md5(struct.pack("!II", part1, part2) + key3).digest()
 
-        return md5(challenge).digest()
-
-    def _get_challenge_hybi06(self):
-        key = self.environ.get("HTTP_SEC_WEBSOCKET_KEY")
-        return b64encode(sha1(key + MAGIC_STRING).digest())
 
     def wait(self):
         return self.websocket.wait()

geventwebsocket/websocket.py

-import struct
-
-
 import struct
 
 class WebSocket(object):
     pass
 
 
+
+class ProtocolException(Exception):
+    pass
+
+
+class FrameTooLargeException(Exception):
+    pass
+
+
 class WebSocketLegacy(object):
     def __init__(self, sock, rfile, environ):
         self.rfile = rfile
                 if len(self._fragments) != 0:
                     raise ProtocolException("Cannot continue a non started message")
 
-            if opcode == self.OPCODE_TEXT:
+                if opcode == self.OPCODE_TEXT:
                     self._fragments.append(payload.decode("utf-8", "replace"))
                 else:
                     self._fragments.append(payload)
             raise Exception('Invalid encoding')
 
     def _is_opcode_invalid(self, opcode):
-        return opcode < self.OPCODE_FRAG or (opcode > self.OPCODE_BINARY and
+        return opcode < self.OPCODE_CONTINUATION or (opcode > self.OPCODE_BINARY and
                 opcode < self.OPCODE_CLOSE) or opcode > self.OPCODE_PONG
 
     def send(self, message, opcode=OPCODE_TEXT):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.