Skip to content

Commit 083d1ae

Browse files
committed
handle Host header that includes port
1 parent 207ca64 commit 083d1ae

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

requests_toolbelt/adapters/host_header_ssl.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def send(self, request, **kwargs):
3535
connection_pool_kwargs = self.poolmanager.connection_pool_kw
3636

3737
if host_header:
38+
# host header can include port, but we should not include it in the assert hostname
39+
host_header = host_header.split(':')[0]
40+
3841
connection_pool_kwargs["assert_hostname"] = host_header
3942
elif "assert_hostname" in connection_pool_kwargs:
4043
# an assert_hostname from a previous request may have been left

tests/test_host_header_ssl_adapter.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
@pytest.fixture
88
def session():
99
"""Create a session with our adapter mounted."""
10-
session = requests.Session()
11-
session.mount('https://', hhssl.HostHeaderSSLAdapter())
10+
s = requests.Session()
11+
s.mount('https://', hhssl.HostHeaderSSLAdapter())
12+
return s
1213

1314

15+
# Let's not spam example.org:
1416
@pytest.mark.skip
1517
class TestHostHeaderSSLAdapter(object):
1618
"""Tests for our HostHeaderSNIAdapter."""
@@ -30,14 +32,19 @@ def test_ssladapter(self, session):
3032
headers={'Host': 'example.com'})
3133
assert r.status_code == 200
3234

33-
def test_stream(self):
34-
self.session.get('https://54.175.219.8/stream/20',
35-
headers={'Host': 'httpbin.org'},
36-
stream=True)
35+
def test_stream(self, session):
36+
session.get('https://54.175.219.8/stream/20',
37+
headers={'Host': 'httpbin.org'},
38+
stream=True)
3739

38-
def test_case_insensitive_header(self):
39-
r = self.session.get('https://93.184.216.34',
40-
headers={'hOSt': 'example.org'})
40+
def test_case_insensitive_header(self, session):
41+
r = session.get('https://93.184.216.34',
42+
headers={'hOSt': 'example.org'})
43+
assert r.status_code == 200
44+
45+
def test_case_header_with_port(self, session):
46+
r = session.get('https://93.184.216.34',
47+
headers={'Host': 'example.org:443'})
4148
assert r.status_code == 200
4249

4350
def test_plain_requests(self):

0 commit comments

Comments
 (0)