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

Skip to content

Commit 6fbc610

Browse files
[3.11] gh-100985: Consistently wrap IPv6 IP address during CONNECT (GH-100986) (GH-115606)
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 a6776cd commit 6fbc610

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
@@ -907,17 +907,23 @@ def _get_hostport(self, host, port):
907907
host = host[:i]
908908
else:
909909
port = self.default_port
910-
if host and host[0] == '[' and host[-1] == ']':
911-
host = host[1:-1]
910+
if host and host[0] == '[' and host[-1] == ']':
911+
host = host[1:-1]
912912

913913
return (host, port)
914914

915915
def set_debuglevel(self, level):
916916
self.debuglevel = level
917917

918+
def _wrap_ipv6(self, ip):
919+
if b':' in ip and ip[0] != b'['[0]:
920+
return b"[" + ip + b"]"
921+
return ip
922+
918923
def _tunnel(self):
919924
connect = b"CONNECT %s:%d HTTP/1.0\r\n" % (
920-
self._tunnel_host.encode("ascii"), self._tunnel_port)
925+
self._wrap_ipv6(self._tunnel_host.encode("ascii")),
926+
self._tunnel_port)
921927
headers = [connect]
922928
for header, value in self._tunnel_headers.items():
923929
headers.append(f"{header}: {value}\r\n".encode("latin-1"))
@@ -1188,9 +1194,8 @@ def putrequest(self, method, url, skip_host=False,
11881194

11891195
# As per RFC 273, IPv6 address should be wrapped with []
11901196
# when used as Host header
1191-
1197+
host_enc = self._wrap_ipv6(host_enc)
11921198
if ":" in host:
1193-
host_enc = b'[' + host_enc + b']'
11941199
host_enc = _strip_ipv6_iface(host_enc)
11951200

11961201
if port == self.default_port:

Lib/test/test_httplib.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2267,6 +2267,22 @@ def test_connect_put_request(self):
22672267
self.assertIn(b'CONNECT destination.com', self.conn.sock.data)
22682268
self.assertIn(b'Host: destination.com', self.conn.sock.data)
22692269

2270+
def test_connect_put_request_ipv6(self):
2271+
self.conn.set_tunnel('[1:2:3::4]', 1234)
2272+
self.conn.request('PUT', '/', '')
2273+
self.assertEqual(self.conn.sock.host, self.host)
2274+
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
2275+
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
2276+
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)
2277+
2278+
def test_connect_put_request_ipv6_port(self):
2279+
self.conn.set_tunnel('[1:2:3::4]:1234')
2280+
self.conn.request('PUT', '/', '')
2281+
self.assertEqual(self.conn.sock.host, self.host)
2282+
self.assertEqual(self.conn.sock.port, client.HTTP_PORT)
2283+
self.assertIn(b'CONNECT [1:2:3::4]:1234', self.conn.sock.data)
2284+
self.assertIn(b'Host: [1:2:3::4]:1234', self.conn.sock.data)
2285+
22702286
def test_tunnel_debuglog(self):
22712287
expected_header = 'X-Dummy: 1'
22722288
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
@@ -740,6 +740,7 @@ Raymond Hettinger
740740
Lisa Hewus Fresh
741741
Kevan Heydon
742742
Wouter van Heyst
743+
Derek Higgins
743744
Kelsey Hightower
744745
Jason Hildebrand
745746
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)