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

Skip to content

Commit 4ac7ed9

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.
1 parent 1e40f10 commit 4ac7ed9

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
@@ -771,8 +771,7 @@ def set_tunnel(self, host, port=None, headers=None):
771771
if self.sock:
772772
raise RuntimeError("Can't set up tunnel for established connection")
773773

774-
self._tunnel_host = host
775-
self._tunnel_port = port
774+
self._tunnel_host, self._tunnel_port = self._get_hostport(host, port)
776775
if headers:
777776
self._tunnel_headers = headers
778777
else:
@@ -802,9 +801,8 @@ def set_debuglevel(self, level):
802801
self.debuglevel = level
803802

804803
def _tunnel(self):
805-
(host, port) = self._get_hostport(self._tunnel_host,
806-
self._tunnel_port)
807-
connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
804+
connect_str = "CONNECT %s:%d HTTP/1.0\r\n" % (self._tunnel_host,
805+
self._tunnel_port)
808806
connect_bytes = connect_str.encode("ascii")
809807
self.send(connect_bytes)
810808
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
@@ -1068,11 +1068,13 @@ def create_connection(address, timeout=None, source_address=None):
10681068

10691069
self.assertEqual(conn.sock.host, 'proxy.com')
10701070
self.assertEqual(conn.sock.port, 80)
1071-
self.assertTrue(b'CONNECT destination.com' in conn.sock.data)
1072-
self.assertTrue(b'Host: destination.com' in conn.sock.data)
1071+
self.assertIn(b'CONNECT destination.com', conn.sock.data)
1072+
# issue22095
1073+
self.assertNotIn(b'Host: destination.com:None', conn.sock.data)
1074+
self.assertIn(b'Host: destination.com', conn.sock.data)
10731075

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

10771079
conn.close()
10781080
conn.request('PUT', '/', '')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ Core and Builtins
3939
Library
4040
-------
4141

42+
- Issue #22095: Fixed HTTPConnection.set_tunnel with default port. The port
43+
value in the host header was set to "None". Patch by Demian Brecht.
44+
4245
- Issue #23016: A warning no longer produces an AttributeError when the program
4346
is run with pythonw.exe.
4447

0 commit comments

Comments
 (0)