From 656b22b8813b369f0c0b6613c1033d930b7d074c Mon Sep 17 00:00:00 2001 From: Instance Date: Sat, 22 Sep 2018 20:19:35 +0200 Subject: Miscellaneous code cleanup --- websocket/_app.py | 10 +++++----- websocket/_http.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/websocket/_app.py b/websocket/_app.py index a281402..1d31f02 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -48,7 +48,7 @@ class Dispatcher: def read(self, sock, read_callback, check_callback): while self.app.sock.connected: r, w, e = select.select( - (self.app.sock.sock, ), (), (), self.ping_timeout) + (self.app.sock.sock, ), (), (), self.ping_timeout) if r: if not read_callback(): break @@ -201,13 +201,13 @@ class WebSocketApp(object): supress_origin: suppress outputting origin header. """ - if ping_timeout is not None and (not ping_timeout or ping_timeout <= 0): + if ping_timeout is not None and ping_timeout <= 0: ping_timeout = None if ping_timeout and ping_interval and ping_interval <= ping_timeout: raise WebSocketException("Ensure ping_interval > ping_timeout") - if sockopt is None: + if not sockopt: sockopt = [] - if sslopt is None: + if not sslopt: sslopt = {} if self.sock: raise WebSocketException("socket is already opened") @@ -232,7 +232,7 @@ class WebSocketApp(object): try: self.sock = WebSocket( self.get_mask_key, sockopt=sockopt, sslopt=sslopt, - fire_cont_frame=self.on_cont_message and True or False, + fire_cont_frame=self.on_cont_message is not None, skip_utf8_validation=skip_utf8_validation, enable_multithread=True if ping_interval else False) self.sock.settimeout(getdefaulttimeout()) diff --git a/websocket/_http.py b/websocket/_http.py index 8d82521..fe97480 100644 --- a/websocket/_http.py +++ b/websocket/_http.py @@ -101,7 +101,7 @@ def _open_proxied_socket(url, options, proxy): def connect(url, options, proxy, socket): - if proxy.host and not socket and not(proxy.type == 'http'): + if proxy.host and not socket and not (proxy.type == 'http'): return _open_proxied_socket(url, options, proxy) hostname, port, resource, is_secure = parse_url(url) -- cgit v1.2.1 From 1d6ffdb1093a58ee8d9caeb468ad0b48dc143c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A4r=20Tojkander?= Date: Thu, 11 Oct 2018 10:15:18 +0200 Subject: Handle redirects in handshake --- websocket/_core.py | 9 +++++++++ websocket/_handshake.py | 11 ++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/websocket/_core.py b/websocket/_core.py index aea16e6..04f1fcb 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -210,6 +210,7 @@ class WebSocket(object): "http_proxy_auth" - http proxy auth information. tuple of username and password. default is None + "redirect_threshold" -> number of redirects to follow. "subprotocols" - array of available sub protocols. default is None. "socket" - pre-initialized stream socket. @@ -220,6 +221,13 @@ class WebSocket(object): try: self.handshake_response = handshake(self.sock, *addrs, **options) + for attempt in range(options.pop('redirect_threshold', 3)): + if self.handshake_response.status in SUPPORTED_REDIRECT_STATUSES: + url = self.handshake_response.headers['location'] + self.sock.close() + self.sock, addrs = connect(url, self.sock_opt, proxy_info(**options), + options.pop('socket', None)) + self.handshake_response = handshake(self.sock, *addrs, **options) self.connected = True except: if self.sock: @@ -482,6 +490,7 @@ def create_connection(url, timeout=None, class_=WebSocket, **options): tuple of username and password. default is None "enable_multithread" -> enable lock for multithread. + "redirect_threshold" -> number of redirects to follow. "sockopt" -> socket options "sslopt" -> ssl option "subprotocols" - array of available sub protocols. diff --git a/websocket/_handshake.py b/websocket/_handshake.py index d9dae47..e28e88d 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -22,6 +22,7 @@ Copyright (C) 2010 Hiroki Ohtani(liris) import hashlib import hmac import os +from http import HTTPStatus import six @@ -36,7 +37,7 @@ if six.PY3: else: from base64 import encodestring as base64encode -__all__ = ["handshake_response", "handshake"] +__all__ = ["handshake_response", "handshake", "SUPPORTED_REDIRECT_STATUSES"] if hasattr(hmac, "compare_digest"): compare_digest = hmac.compare_digest @@ -47,6 +48,8 @@ else: # websocket supported version. VERSION = 13 +SUPPORTED_REDIRECT_STATUSES = [HTTPStatus.MOVED_PERMANENTLY, HTTPStatus.FOUND, HTTPStatus.SEE_OTHER] + CookieJar = SimpleCookieJar() @@ -67,6 +70,8 @@ def handshake(sock, hostname, port, resource, **options): dump("request header", header_str) status, resp = _get_resp_headers(sock) + if status in SUPPORTED_REDIRECT_STATUSES: + return handshake_response(status, resp, None) success, subproto = _validate(resp, key, options.get("subprotocols")) if not success: raise WebSocketException("Invalid WebSocket Header") @@ -134,9 +139,9 @@ def _get_handshake_headers(resource, host, port, options): return headers, key -def _get_resp_headers(sock, success_status=101): +def _get_resp_headers(sock, success_statuses=(101, 301, 302, 303)): status, resp_headers, status_message = read_headers(sock) - if status != success_status: + if status not in success_statuses: raise WebSocketBadStatusException("Handshake status %d %s", status, status_message) return status, resp_headers -- cgit v1.2.1 From b65f8ba4b935528a56c751c2f1de7a7d2d12f17e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A4r=20Tojkander?= Date: Thu, 11 Oct 2018 11:03:22 +0200 Subject: Rename 'redirect_threshold' to 'redirect_limit' --- websocket/_core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/websocket/_core.py b/websocket/_core.py index 04f1fcb..a85fc78 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -210,7 +210,7 @@ class WebSocket(object): "http_proxy_auth" - http proxy auth information. tuple of username and password. default is None - "redirect_threshold" -> number of redirects to follow. + "redirect_limit" -> number of redirects to follow. "subprotocols" - array of available sub protocols. default is None. "socket" - pre-initialized stream socket. @@ -221,7 +221,7 @@ class WebSocket(object): try: self.handshake_response = handshake(self.sock, *addrs, **options) - for attempt in range(options.pop('redirect_threshold', 3)): + for attempt in range(options.pop('redirect_limit', 3)): if self.handshake_response.status in SUPPORTED_REDIRECT_STATUSES: url = self.handshake_response.headers['location'] self.sock.close() @@ -490,7 +490,7 @@ def create_connection(url, timeout=None, class_=WebSocket, **options): tuple of username and password. default is None "enable_multithread" -> enable lock for multithread. - "redirect_threshold" -> number of redirects to follow. + "redirect_limit" -> number of redirects to follow. "sockopt" -> socket options "sslopt" -> ssl option "subprotocols" - array of available sub protocols. -- cgit v1.2.1 From da1fdb16927f01420749d121cc17666a66684587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A4r=20Tojkander?= Date: Thu, 11 Oct 2018 11:04:58 +0200 Subject: Handle PY2/3 --- websocket/_handshake.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/websocket/_handshake.py b/websocket/_handshake.py index e28e88d..8c6f6a6 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -22,7 +22,6 @@ Copyright (C) 2010 Hiroki Ohtani(liris) import hashlib import hmac import os -from http import HTTPStatus import six @@ -37,6 +36,11 @@ if six.PY3: else: from base64 import encodestring as base64encode +if six.PY3: + from http import HTTPStatus +else: + import httplib as HTTPStatus + __all__ = ["handshake_response", "handshake", "SUPPORTED_REDIRECT_STATUSES"] if hasattr(hmac, "compare_digest"): -- cgit v1.2.1 From fc09e7e2610bbf0e7308708a71eab066184aee01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A4r=20Tojkander?= Date: Thu, 11 Oct 2018 12:41:18 +0200 Subject: Handle PY3.4/3.5 --- websocket/_handshake.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/websocket/_handshake.py b/websocket/_handshake.py index 8c6f6a6..3b8ddc0 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -37,7 +37,10 @@ else: from base64 import encodestring as base64encode if six.PY3: - from http import HTTPStatus + if six.PY34: + from http import client as HTTPStatus + else: + from http import HTTPStatus else: import httplib as HTTPStatus -- cgit v1.2.1