From c57897424689df434d7faeb42ed928744eb02644 Mon Sep 17 00:00:00 2001 From: Igor Gnatenko Date: Sun, 2 Sep 2018 09:19:19 +0200 Subject: [PATCH] Switch to websockets 6.0 Signed-off-by: Igor Gnatenko --- environment.yml | 2 +- requirements.txt | 2 +- sanic/websocket.py | 16 +++++----------- setup.py | 2 +- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/environment.yml b/environment.yml index 1c1dd82f..20cd1509 100644 --- a/environment.yml +++ b/environment.yml @@ -15,6 +15,6 @@ dependencies: - httptools>=0.0.9 - ujson>=1.35 - aiofiles>=0.3.0 - - websockets>=3.2 + - websockets>=6.0 - sphinxcontrib-asyncio>=0.2.0 - https://github.com/channelcat/docutils-fork/zipball/master diff --git a/requirements.txt b/requirements.txt index e320e781..4404d5d2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,5 @@ aiofiles httptools ujson; sys_platform != "win32" and implementation_name == "cpython" uvloop; sys_platform != "win32" and implementation_name == "cpython" -websockets>=5.0,<6.0 +websockets>=6.0,<7.0 multidict>=4.0,<5.0 diff --git a/sanic/websocket.py b/sanic/websocket.py index 99408af5..9ccf9fdf 100644 --- a/sanic/websocket.py +++ b/sanic/websocket.py @@ -57,17 +57,11 @@ class WebSocketProtocol(HttpProtocol): async def websocket_handshake(self, request, subprotocols=None): # let the websockets package do the handshake with the client - headers = [] - - def get_header(k): - return request.headers.get(k, '') - - def set_header(k, v): - headers.append((k, v)) + headers = {} try: - key = handshake.check_request(get_header) - handshake.build_response(set_header, key) + key = handshake.check_request(request.headers) + handshake.build_response(headers, key) except InvalidHandshake: raise InvalidUsage('Invalid websocket request') @@ -79,12 +73,12 @@ class WebSocketProtocol(HttpProtocol): for p in client_subprotocols: if p in subprotocols: subprotocol = p - set_header('Sec-Websocket-Protocol', subprotocol) + headers['Sec-Websocket-Protocol'] = subprotocol break # write the 101 response back to the client rv = b'HTTP/1.1 101 Switching Protocols\r\n' - for k, v in headers: + for k, v in headers.items(): rv += k.encode('utf-8') + b': ' + v.encode('utf-8') + b'\r\n' rv += b'\r\n' request.transport.write(rv) diff --git a/setup.py b/setup.py index 34703ab4..7df2964f 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ requirements = [ uvloop, ujson, 'aiofiles>=0.3.0', - 'websockets>=5.0,<6.0', + 'websockets>=6.0,<7.0', 'multidict>=4.0,<5.0', ] if strtobool(os.environ.get("SANIC_NO_UJSON", "no")):