Ver Fonte

Changed InvalidRequest to HandshakeError

Taddeus Kroes há 13 anos atrás
pai
commit
a6952f5b27
3 ficheiros alterados com 13 adições e 9 exclusões
  1. 1 1
      exceptions.py
  2. 2 2
      server.py
  3. 10 6
      websocket.py

+ 1 - 1
exceptions.py

@@ -2,7 +2,7 @@ class SocketClosed(Exception):
     pass
 
 
-class InvalidRequest(ValueError):
+class HandshakeError(Exception):
     pass
 
 

+ 2 - 2
server.py

@@ -6,7 +6,7 @@ from threading import Thread
 from websocket import websocket
 from connection import Connection
 from frame import CLOSE_NORMAL
-from exceptions import InvalidRequest
+from exceptions import HandshakeError
 
 
 class Server(object):
@@ -36,7 +36,7 @@ class Server(object):
                 thread = Thread(target=client.receive_forever)
                 thread.daemon = True
                 thread.start()
-            except InvalidRequest as e:
+            except HandshakeError as e:
                 logging.error('Invalid request: %s', e.message)
             except KeyboardInterrupt:
                 logging.info('Received interrupt, stopping server...')

+ 10 - 6
websocket.py

@@ -3,7 +3,7 @@ import socket
 from hashlib import sha1
 
 from frame import receive_frame
-from exceptions import InvalidRequest
+from exceptions import HandshakeError
 
 
 WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
@@ -57,7 +57,7 @@ class websocket(object):
         """
         Equivalent to socket.accept(), but transforms the socket into a
         websocket instance and sends a server handshake (after receiving a
-        client handshake). Note that the handshake may raise an InvalidRequest
+        client handshake). Note that the handshake may raise an HandshakeError
         exception.
         """
         client, address = self.sock.accept()
@@ -102,8 +102,8 @@ class websocket(object):
     def server_handshake(self):
         """
         Execute a handshake as the server end point of the socket. If the HTTP
-        request headers sent by the client are invalid, an InvalidRequest
-        exception is raised.
+        request headers sent by the client are invalid, a HandshakeError
+        is raised.
         """
         raw_headers = self.sock.recv(512).decode('utf-8', 'ignore')
 
@@ -119,13 +119,13 @@ class websocket(object):
         for name in ('Host', 'Upgrade', 'Connection', 'Sec-WebSocket-Key',
                      'Origin', 'Sec-WebSocket-Version'):
             if name not in header_names:
-                raise InvalidRequest('missing "%s" header' % name)
+                raise HandshakeError('missing "%s" header' % name)
 
         # Check WebSocket version used by client
         version = header('Sec-WebSocket-Version')
 
         if version != WS_VERSION:
-            raise InvalidRequest('WebSocket version %s requested (only %s '
+            raise HandshakeError('WebSocket version %s requested (only %s '
                                  'is supported)' % (version, WS_VERSION))
 
         # Only supported protocols are returned
@@ -160,5 +160,9 @@ class websocket(object):
         self.sock.send(shake + '\r\n')
 
     def client_handshake(self):
+        """
+        Execute a handshake as the client end point of the socket. May raise a
+        HandshakeError if the server response is invalid.
+        """
         # TODO: implement HTTP request headers for client handshake
         raise NotImplementedError()