From b37e7e0c7cc6883840cbbeeb96f5af329257d3f2 Mon Sep 17 00:00:00 2001 From: liris Date: Tue, 14 Apr 2015 13:45:11 +0900 Subject: [PATCH] fixed #171 --- ChangeLog | 1 + websocket/_app.py | 8 ++++++-- websocket/_core.py | 3 +++ websocket/_handshake.py | 12 ++++++++---- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2b65219..3885feb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,7 @@ ChangeLog - fixed if client is behind proxy (#169) - support SNI for python 2.7.9+ and 3.2+ (#172) + - update Host HTTP header by user. (#171) - 0.29.0 diff --git a/websocket/_app.py b/websocket/_app.py index 1ee0577..f1869ce 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -122,7 +122,8 @@ class WebSocketApp(object): ping_interval=0, ping_timeout=None, http_proxy_host=None, http_proxy_port=None, http_no_proxy=None, http_proxy_auth=None, - skip_utf8_validation=False): + skip_utf8_validation=False, + host=None, origin=None): """ run event loop for WebSocket framework. This loop is infinite loop and is alive during websocket is available. @@ -138,6 +139,8 @@ class WebSocketApp(object): http_proxy_port: http proxy port. If not set, set to 80. http_no_proxy: host names, which doesn't use proxy. skip_utf8_validation: skip utf8 validation. + host: update host header. + origin: update origin header. """ if not ping_timeout or ping_timeout <= 0: @@ -161,7 +164,8 @@ class WebSocketApp(object): http_proxy_host=http_proxy_host, http_proxy_port=http_proxy_port, http_no_proxy=http_no_proxy, http_proxy_auth=http_proxy_auth, - subprotocols=self.subprotocols) + subprotocols=self.subprotocols, + host=host, origin=origin) self._callback(self.on_open) if ping_interval: diff --git a/websocket/_core.py b/websocket/_core.py index f079194..f98bf33 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -76,6 +76,8 @@ def create_connection(url, timeout=None, **options): options: "header" -> custom http header list. "cookie" -> cookie value. + "origin" -> custom origin url. + "host" -> custom host header string. "http_proxy_host" - http proxy host name. "http_proxy_port" - http proxy port. If not set, set to 80. "http_no_proxy" - host names, which doesn't use proxy. @@ -237,6 +239,7 @@ class WebSocket(object): options: "header" -> custom http header list. "cookie" -> cookie value. "origin" -> custom origin url. + "host" -> custom host header string. "http_proxy_host" - http proxy host name. "http_proxy_port" - http proxy port. If not set, set to 80. "http_no_proxy" - host names, which doesn't use proxy. diff --git a/websocket/_handshake.py b/websocket/_handshake.py index 1fbef04..95e9349 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -47,8 +47,8 @@ class handshake_response(object): self.headers = headers self.subprotocol = subprotocol -def handshake(sock, host, port, resource, **options): - headers, key = _get_handshake_headers(resource, host, port, options) +def handshake(sock, hostname, port, resource, **options): + headers, key = _get_handshake_headers(resource, hostname, port, options) header_str = "\r\n".join(headers) send(sock, header_str) @@ -71,9 +71,13 @@ def _get_handshake_headers(resource, host, port, options): hostport = host else: hostport = "%s:%d" % (host, port) - headers.append("Host: %s" % hostport) - if "origin" in options: + if "host" in options and options["host"]: + headers.append("Host: %s" % options["host"]) + else: + headers.append("Host: %s" % hostport) + + if "origin" in options and options["origin"]: headers.append("Origin: %s" % options["origin"]) else: headers.append("Origin: http://%s" % hostport)