Anonymous avatar Anonymous committed 71d3c51

Add remaining reason constants and replace magic numbers

Comments (0)

Files changed (1)

geventwebsocket/websocket.py

     REASON_NORMAL = 1000
     REASON_GOING_AWAY = 1001
     REASON_PROTOCOL_ERROR = 1002
+    REASON_UNSUPPORTED_DATA_TYPE = 1003
+    REASON_TOO_LARGE = 1004
 
     LEN_16 = 126
     LEN_64 = 127
             opcode_octet, length_octet = struct.unpack('!BB', self._read_from_socket(2))
 
             if self.RSV & opcode_octet:
-                self.close(1002, 'Reserved bits cannot be set')
+                self.close(self.REASON_PROTOCOL_ERROR, 'Reserved bits cannot be set')
                 return None
 
             opcode = opcode_octet & self.OPCODE
             is_final_frag = (self.FIN & opcode_octet) != 0
 
             if self._is_opcode_invalid(opcode):
-                self.close(1002, 'Invalid opcode %x' % opcode)
+                self.close(self.REASON_PROTOCOL_ERROR, 'Invalid opcode %x' % opcode)
                 return None
             
             if not is_final_frag and self.OPCODE_CLOSE <= opcode <= self.OPCODE_PONG:
                 return None
 
             if not self.MASK & length_octet:
-                self.close(1002, 'MASK must be set')
+                self.close(self.REASON_PROTOCOL_ERROR, 'MASK must be set')
                 return None
 
             length_code = length_octet & self.PAYLOAD
 
-            if length_code > 125 and (self.OPCODE_CLOSE <= opcode <= self.OPCODE_PONG):
-                self.close(1002, 'Control frame payload cannot be larger than 125 bytes')
+            if length_code >= self.LEN_16 and (self.OPCODE_CLOSE <= opcode <= self.OPCODE_PONG):
+                self.close(self.REASON_PROTOCOL_ERROR,
+                        'Control frame payload cannot be larger than 125 bytes')
                 return None
 
-            if length_code < 126:
+            if length_code < self.LEN_16:
                 length = length_code
-            elif length_code == 126:
+            elif length_code == self.LEN_16:
                 length = struct.unpack('!H', self._read_from_socket(2))[0]
-            elif length_code == 127:
+            elif length_code == self.LEN_64:
                 length = struct.unpack('!Q', self._read_from_socket(8))[0]
             else:
                 raise Exception('Calculated invalid length')
                 else:
                     reason = message = None
 
-                self.close(1000, '')
+                self.close(self.REASON_NORMAL, '')
                 return (reason, message)
 
             if opcode == self.OPCODE_PING:
         if opcode == self.OPCODE_TEXT:
             message = struct.pack('!%ds' % length, message)
 
-        if length < 126:
+        if length < self.LEN_16:
             preamble = struct.pack('!BB', self.FIN | opcode, length)
         elif length < 2 ** 16:
             preamble = struct.pack('!BBH', self.FIN | opcode, self.LEN_16, length)
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.