-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add initial support for PerMessage Deflate #2273
Changes from 20 commits
00bae99
405be9a
5017f01
70ab0e9
e0a459c
52de28c
048a8f2
72127c5
4c55414
3c6029e
79714a7
e51f48d
f8f0f21
63bd563
895ea75
adf31e0
39a15ef
b45c0ba
a04c1c9
c3c640e
99b50d5
268da36
2e961b3
b91d1c7
c95a7f4
c911749
01ea9b1
97a2c1e
c98c777
fff88de
26e7bac
e5c651d
8da8460
22fb3f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
from .helpers import (PY_35, CeilTimeout, ProxyInfo, TimeoutHandle, | ||
_BaseCoroMixin, deprecated_noop, sentinel) | ||
from .http import WS_KEY, WebSocketReader, WebSocketWriter | ||
from .http_websocket import ws_ext_gen, ws_ext_parse | ||
from .streams import FlowControlDataQueue | ||
|
||
|
||
|
@@ -370,7 +371,8 @@ def ws_connect(self, url, *, | |
origin=None, | ||
headers=None, | ||
proxy=None, | ||
proxy_auth=None): | ||
proxy_auth=None, | ||
compress=15): | ||
"""Initiate websocket connection.""" | ||
return _WSRequestContextManager( | ||
self._ws_connect(url, | ||
|
@@ -384,7 +386,8 @@ def ws_connect(self, url, *, | |
origin=origin, | ||
headers=headers, | ||
proxy=proxy, | ||
proxy_auth=proxy_auth)) | ||
proxy_auth=proxy_auth, | ||
compress=compress)) | ||
|
||
@asyncio.coroutine | ||
def _ws_connect(self, url, *, | ||
|
@@ -398,7 +401,8 @@ def _ws_connect(self, url, *, | |
origin=None, | ||
headers=None, | ||
proxy=None, | ||
proxy_auth=None): | ||
proxy_auth=None, | ||
compress=15): | ||
|
||
if headers is None: | ||
headers = CIMultiDict() | ||
|
@@ -420,6 +424,11 @@ def _ws_connect(self, url, *, | |
headers[hdrs.SEC_WEBSOCKET_PROTOCOL] = ','.join(protocols) | ||
if origin is not None: | ||
headers[hdrs.ORIGIN] = origin | ||
if compress: | ||
if compress is True: | ||
compress = 15 | ||
extstr = ws_ext_gen(compress=compress) | ||
headers[hdrs.SEC_WEBSOCKET_EXTENSIONS] = extstr | ||
|
||
# send request | ||
resp = yield from self.get(url, headers=headers, | ||
|
@@ -478,12 +487,41 @@ def _ws_connect(self, url, *, | |
protocol = proto | ||
break | ||
|
||
# websocket compress | ||
notakeover = False | ||
if compress: | ||
if hdrs.SEC_WEBSOCKET_EXTENSIONS in resp.headers: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
compress, notakeover = ws_ext_parse( | ||
resp.headers[hdrs.SEC_WEBSOCKET_EXTENSIONS] | ||
) | ||
if compress == 0: | ||
pass | ||
elif compress == -1: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could live with this code style but it looks too C-ish.
|
||
raise WSServerHandshakeError( | ||
resp.request_info, | ||
resp.history, | ||
message='Invalid window size', | ||
code=resp.status, | ||
headers=resp.headers) | ||
elif compress == -2: | ||
raise WSServerHandshakeError( | ||
resp.request_info, | ||
resp.history, | ||
message='Invalid deflate extension', | ||
code=resp.status, | ||
headers=resp.headers) | ||
else: | ||
compress = 0 | ||
notakeover = False | ||
|
||
proto = resp.connection.protocol | ||
reader = FlowControlDataQueue( | ||
proto, limit=2 ** 16, loop=self._loop) | ||
proto.set_parser(WebSocketReader(reader), reader) | ||
resp.connection.writer.set_tcp_nodelay(True) | ||
writer = WebSocketWriter(resp.connection.writer, use_mask=True) | ||
writer = WebSocketWriter( | ||
resp.connection.writer, use_mask=True, | ||
compress=compress, notakeover=notakeover) | ||
except Exception: | ||
resp.close() | ||
raise | ||
|
@@ -497,7 +535,9 @@ def _ws_connect(self, url, *, | |
autoping, | ||
self._loop, | ||
receive_timeout=receive_timeout, | ||
heartbeat=heartbeat) | ||
heartbeat=heartbeat, | ||
compress=compress, | ||
client_notakeover=notakeover) | ||
|
||
def _prepare_headers(self, headers): | ||
""" Add default headers and transform it to CIMultiDict | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check is not needed: let's assume
compress
is always integer.