Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4a001b

Browse files
Issue #22095: Fixed HTTPConnection.set_tunnel with default port. The port
value in the host header was set to "None". Patch by Demian Brecht.
2 parents d625a18 + 4ac7ed9 commit d4a001b

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

Lib/http/client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,7 @@ def set_tunnel(self, host, port=None, headers=None):
835835
if self.sock:
836836
raise RuntimeError("Can't set up tunnel for established connection")
837837

838-
self._tunnel_host = host
839-
self._tunnel_port = port
838+
self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)
840839
if headers:
841840
self._tunnel_headers = headers
842841
else:
@@ -866,9 +865,8 @@ def set_debuglevel(self, level):
866865
self.debuglevel = level
867866

868867
def _tunnel(self):
869-
(host, port) = self._get_hostport(self._tunnel_host,
870-
self._tunnel_port)
871-
connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
868+
connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,
869+
self._tunnel_port)
872870
connect_bytes = connect_str.encode("ascii")
873871
self.send(connect_bytes)
874872
for header, value in self._tunnel_headers.items():

Lib/test/test_httplib.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,11 +1299,13 @@ def create_connection(address, timeout=None, source_address=None):
12991299

13001300
self.assertEqual(conn.sock.host, 'proxy.com')
13011301
self.assertEqual(conn.sock.port, 80)
1302-
self.assertTrue(b'CONNECT destination.com' in conn.sock.data)
1303-
self.assertTrue(b'Host: destination.com' in conn.sock.data)
1302+
self.assertIn(b'CONNECT destination.com', conn.sock.data)
1303+
# issue22095
1304+
self.assertNotIn(b'Host: destination.com:None', conn.sock.data)
1305+
self.assertIn(b'Host: destination.com', conn.sock.data)
13041306

13051307
# This test should be removed when CONNECT gets the HTTP/1.1 blessing
1306-
self.assertTrue(b'Host: proxy.com' not in conn.sock.data)
1308+
self.assertNotIn(b'Host: proxy.com', conn.sock.data)
13071309

13081310
conn.close()
13091311
conn.request('PUT', '/', '')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Core and Builtins
194194
Library
195195
-------
196196

197+
- Issue #22095: Fixed HTTPConnection.set_tunnel with default port. The port
198+
value in the host header was set to "None". Patch by Demian Brecht.
199+
197200
- Issue #23016: A warning no longer produces an AttributeError when the program
198201
is run with pythonw.exe.
199202

0 commit comments

Comments
 (0)