From 29cf973a2b86ecd1156fcf0185148f40a77261b5 Mon Sep 17 00:00:00 2001 From: liuchong Date: Wed, 7 Apr 2021 16:02:06 +0800 Subject: [PATCH 1/3] Fix passing host to in headers --- ws4py/client/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ws4py/client/__init__.py b/ws4py/client/__init__.py index 411638f..f56727d 100644 --- a/ws4py/client/__init__.py +++ b/ws4py/client/__init__.py @@ -253,7 +253,6 @@ def handshake_headers(self): handshake. """ headers = [ - ('Host', '%s:%s' % (self.host, self.port)), ('Connection', 'Upgrade'), ('Upgrade', 'websocket'), ('Sec-WebSocket-Key', self.key.decode('utf-8')), @@ -266,6 +265,10 @@ def handshake_headers(self): if self.extra_headers: headers.extend(self.extra_headers) + if not any(x for x in headers if x[0].lower() == 'host') and \ + 'host' not in self.exclude_headers: + headers.append(('Host', '%s:%s' % (self.host, self.port))) + if not any(x for x in headers if x[0].lower() == 'origin') and \ 'origin' not in self.exclude_headers: From 37c8ec5052d1e143beed807f84d20e4e6eff034a Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Tue, 24 Dec 2024 15:04:27 +0600 Subject: [PATCH 2/3] Update ws4py/client/__init__.py Signed-off-by: Asif Saif Uddin --- ws4py/client/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ws4py/client/__init__.py b/ws4py/client/__init__.py index f56727d..ec7db8a 100644 --- a/ws4py/client/__init__.py +++ b/ws4py/client/__init__.py @@ -269,6 +269,7 @@ def handshake_headers(self): 'host' not in self.exclude_headers: headers.append(('Host', '%s:%s' % (self.host, self.port))) + if not any(x for x in headers if x[0].lower() == 'origin') and \ 'origin' not in self.exclude_headers: From 1b0e6b377d3d4000ebff40fde3e32bcb5d0b680b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=86=B2?= Date: Fri, 24 Jan 2025 17:45:01 +0800 Subject: [PATCH 3/3] Add unit test for passing host to in headers --- test/test_client.py | 9 +++++++++ ws4py/client/__init__.py | 1 + 2 files changed, 10 insertions(+) diff --git a/test/test_client.py b/test/test_client.py index 7ad6a4b..e4c62fb 100644 --- a/test/test_client.py +++ b/test/test_client.py @@ -104,6 +104,15 @@ def test_parse_wss_scheme_with_query_string(self): self.assertEqual(c.resource, "/?token=value") self.assertEqual(c.bind_addr, ("127.0.0.1", 443)) + def test_overriding_host_from_headers(self): + c = WebSocketBaseClient(url="wss://127.0.0.1", headers=[("Host", "example123.com")]) + self.assertEqual(c.host, "127.0.0.1") + self.assertEqual(c.port, 443) + self.assertEqual(c.bind_addr, ("127.0.0.1", 443)) + for h in c.handshake_headers: + if h[0].lower() == "host": + self.assertEqual(h[1], "example123.com") + @patch('ws4py.client.socket') def test_connect_and_close(self, sock): diff --git a/ws4py/client/__init__.py b/ws4py/client/__init__.py index ec7db8a..32d28a8 100644 --- a/ws4py/client/__init__.py +++ b/ws4py/client/__init__.py @@ -265,6 +265,7 @@ def handshake_headers(self): if self.extra_headers: headers.extend(self.extra_headers) + # keep old logic if no overriding Host in headers if not any(x for x in headers if x[0].lower() == 'host') and \ 'host' not in self.exclude_headers: headers.append(('Host', '%s:%s' % (self.host, self.port)))