Commits

Arkaitz Jimenez committed d08ad01

Fixed receiving Close frames

Comments (0)

Files changed (2)

geventwebsocket/handler.py

         # TODO: refactor to run under run_application
         # In case the client doesn't want to initialize a WebSocket connection
         # we will proceed with the default PyWSGI functionality.
-        print "Connection is [%s]"%(self.environ.get("HTTP_CONNECTION"))
         if not self.environ.get("HTTP_CONNECTION").endswith("Upgrade") or \
            self.environ.get("HTTP_UPGRADE").lower() != "websocket" or \
            not self.environ.get("HTTP_SEC_WEBSOCKET_ORIGIN") or \
            not self.accept_upgrade():
-               print "Not!!"
                return super(WebSocketHandler, self).handle_one_response()
 
         self.websocket = WebSocket(self.socket, self.rfile, self.environ)

geventwebsocket/websocket.py

                     self.close()
             elif (frame_type & 0x80) == 0x80: # most significant byte is set, New version
                 opcode = (frame_type & 0xF)
-                if opcode != 1:
-                    self.close()
-                    return None
-                else:
-                    maskandlength = ord(self.rfile.read(1))
-                    (mask,length) = (maskandlength >> 7), (maskandlength & 0x7F)
-                    if length > 125:
-                        length = self._extended_length(length)
+                maskandlength = ord(self.rfile.read(1))
+                (mask,length) = (maskandlength >> 7), (maskandlength & 0x7F)
+                if length > 125:
+                    length = self._extended_length(length)
+                if mask:
+                    mask_value = self.rfile.read(4)
+
+                if opcode == 1:
                     if length == 0:
                         self.close()
                         return None
                     if mask:
-                        mask_value = self.rfile.read(4)
                         data = self.rfile.read(length)
                         return self.apply_mask(data,mask_value)
                     else:
                         return self.rfile.read(length) # discard the bytes
+
+                elif opcode == 8:
+                    data = self.rfile.read(length)
+                    self.close()    
+                else:
+                    print "opcode was " + str(opcode)
+                    self.close()
+                    return None
             else:
                 raise IOError("Reveiced an invalid message")