Skip to content

Fix passing host to in headers #268

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

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
7 changes: 6 additions & 1 deletion ws4py/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')),
Expand All @@ -266,6 +265,12 @@ 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)))


if not any(x for x in headers if x[0].lower() == 'origin') and \
'origin' not in self.exclude_headers:

Expand Down