Commit a6952f5b authored by Taddeüs Kroes's avatar Taddeüs Kroes

Changed InvalidRequest to HandshakeError

parent 26c511bd
...@@ -2,7 +2,7 @@ class SocketClosed(Exception): ...@@ -2,7 +2,7 @@ class SocketClosed(Exception):
pass pass
class InvalidRequest(ValueError): class HandshakeError(Exception):
pass pass
......
...@@ -6,7 +6,7 @@ from threading import Thread ...@@ -6,7 +6,7 @@ from threading import Thread
from websocket import websocket from websocket import websocket
from connection import Connection from connection import Connection
from frame import CLOSE_NORMAL from frame import CLOSE_NORMAL
from exceptions import InvalidRequest from exceptions import HandshakeError
class Server(object): class Server(object):
...@@ -36,7 +36,7 @@ class Server(object): ...@@ -36,7 +36,7 @@ class Server(object):
thread = Thread(target=client.receive_forever) thread = Thread(target=client.receive_forever)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
except InvalidRequest as e: except HandshakeError as e:
logging.error('Invalid request: %s', e.message) logging.error('Invalid request: %s', e.message)
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info('Received interrupt, stopping server...') logging.info('Received interrupt, stopping server...')
......
...@@ -3,7 +3,7 @@ import socket ...@@ -3,7 +3,7 @@ import socket
from hashlib import sha1 from hashlib import sha1
from frame import receive_frame from frame import receive_frame
from exceptions import InvalidRequest from exceptions import HandshakeError
WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' WS_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
...@@ -57,7 +57,7 @@ class websocket(object): ...@@ -57,7 +57,7 @@ class websocket(object):
""" """
Equivalent to socket.accept(), but transforms the socket into a Equivalent to socket.accept(), but transforms the socket into a
websocket instance and sends a server handshake (after receiving 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. exception.
""" """
client, address = self.sock.accept() client, address = self.sock.accept()
...@@ -102,8 +102,8 @@ class websocket(object): ...@@ -102,8 +102,8 @@ class websocket(object):
def server_handshake(self): def server_handshake(self):
""" """
Execute a handshake as the server end point of the socket. If the HTTP Execute a handshake as the server end point of the socket. If the HTTP
request headers sent by the client are invalid, an InvalidRequest request headers sent by the client are invalid, a HandshakeError
exception is raised. is raised.
""" """
raw_headers = self.sock.recv(512).decode('utf-8', 'ignore') raw_headers = self.sock.recv(512).decode('utf-8', 'ignore')
...@@ -119,13 +119,13 @@ class websocket(object): ...@@ -119,13 +119,13 @@ class websocket(object):
for name in ('Host', 'Upgrade', 'Connection', 'Sec-WebSocket-Key', for name in ('Host', 'Upgrade', 'Connection', 'Sec-WebSocket-Key',
'Origin', 'Sec-WebSocket-Version'): 'Origin', 'Sec-WebSocket-Version'):
if name not in header_names: if name not in header_names:
raise InvalidRequest('missing "%s" header' % name) raise HandshakeError('missing "%s" header' % name)
# Check WebSocket version used by client # Check WebSocket version used by client
version = header('Sec-WebSocket-Version') version = header('Sec-WebSocket-Version')
if version != WS_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)) 'is supported)' % (version, WS_VERSION))
# Only supported protocols are returned # Only supported protocols are returned
...@@ -160,5 +160,9 @@ class websocket(object): ...@@ -160,5 +160,9 @@ class websocket(object):
self.sock.send(shake + '\r\n') self.sock.send(shake + '\r\n')
def client_handshake(self): 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 # TODO: implement HTTP request headers for client handshake
raise NotImplementedError() raise NotImplementedError()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment