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

Skip to content

Commit 23aef57

Browse files
[3.12] gh-100985: Consistently wrap IPv6 IP address during CONNECT (GH-100986) (GH-115591)
Update _get_hostport to always remove square brackets from IPv6 addresses. Then add them if needed in "CONNECT .." and "Host: ". (cherry picked from commit 465db27) Co-authored-by: Derek Higgins <[email protected]>
1 parent 9148b77 commit 23aef57

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

Lib/http/client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -936,17 +936,23 @@ def _get_hostport(self, host, port):
936936
host = host[:i]
937937
else:
938938
port = self.default_port
939-
if host and host[0] == '[' and host[-1] == ']':
940-
host = host[1:-1]
939+
if host and host[0] == '[' and host[-1] == ']':
940+
host = host[1:-1]
941941

942942
return (host, port)
943943

944944
def set_debuglevel(self, level):
945945
self.debuglevel = level
946946

947+
def _wrap_ipv6(self, ip):
948+
if b':' in ip and ip[0] != b'['[0]:
949+
return b"[" + ip + b"]"
950+
return ip
951+
947952
def _tunnel(self):
948953
connect = b"CONNECT %s:%d %s\r\n" % (
949-
self._tunnel_host.encode("idna"), self._tunnel_port,
954+
self._wrap_ipv6(self._tunnel_host.encode("idna")),
955+
self._tunnel_port,
950956
self._http_vsn_str.encode("ascii"))
951957
headers = [connect]
952958
for header, value in self._tunnel_headers.items():
@@ -1221,9 +1227,8 @@ def putrequest(self, method, url, skip_host=False,
12211227

12221228
# As per RFC 273, IPv6 address should be wrapped with []
12231229
# when used as Host header
1224-
1230+
host_enc = self._wrap_ipv6(host_enc)
12251231
if ":" in host:
1226-
host_enc = b'[' + host_enc + b']'
12271232
host_enc = _strip_ipv6_iface(host_enc)
12281233

12291234
if port == self.default_port:

Lib/test/test_httplib.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,22 @@ def test_connect_put_request(self):
24082408
self.assertIn(b'PUT / HTTP/1.1\r\nHost: %(host)s\r\n' % d,
24092409
self.conn.sock.data)
24102410

2411+
def test_connect_put_request_ipv6(self):
2412+
self.conn.set_tunnel('[1:2:3::4]', 1234)
2413+
self.conn.request('PUT', '/', '')
2414+
self.assertEqual(self.conn.sock.host, self.host)
2415+
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
2416+
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
2417+
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)
2418+
2419+
def test_connect_put_request_ipv6_port(self):
2420+
self.conn.set_tunnel('[1:2:3::4]:1234')
2421+
self.conn.request('PUT', '/', '')
2422+
self.assertEqual(self.conn.sock.host, self.host)
2423+
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
2424+
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
2425+
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)
2426+
24112427
def test_tunnel_debuglog(self):
24122428
expected_header = 'X-Dummy: 1'
24132429
response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header)

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ Raymond Hettinger
752752
Lisa Hewus Fresh
753753
Kevan Heydon
754754
Wouter van Heyst
755+
Derek Higgins
755756
Kelsey Hightower
756757
Jason Hildebrand
757758
Ryan Hileman
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update HTTPSConnection to consistently wrap IPv6 Addresses when using a
2+
proxy.

0 commit comments

Comments
 (0)