[3.11] gh-73561: Omit interface scope from IPv6 when used as Host header (GH-93324) (#112273)

gh-73561: Omit interface scope from IPv6 when used as Host header (GH-93324)

Omit the `@interface_scope` from an IPv6 address when used as Host header by `http.client`.

---------

(cherry picked from commit ce1096f974)


 [Google LLC]

Co-authored-by: Michael <35783820+mib1185@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2023-11-20 00:25:40 +01:00 committed by GitHub
parent 11b91be55b
commit f6e11eab49
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View file

@ -172,6 +172,13 @@ def _encode(data, name='data'):
"if you want to send it encoded in UTF-8." %
(name.title(), data[err.start:err.end], name)) from None
def _strip_ipv6_iface(enc_name: bytes) -> bytes:
"""Remove interface scope from IPv6 address."""
enc_name, percent, _ = enc_name.partition(b"%")
if percent:
assert enc_name.startswith(b'['), enc_name
enc_name += b']'
return enc_name
class HTTPMessage(email.message.Message):
# XXX The only usage of this method is in
@ -1161,7 +1168,7 @@ class HTTPConnection:
netloc_enc = netloc.encode("ascii")
except UnicodeEncodeError:
netloc_enc = netloc.encode("idna")
self.putheader('Host', netloc_enc)
self.putheader('Host', _strip_ipv6_iface(netloc_enc))
else:
if self._tunnel_host:
host = self._tunnel_host
@ -1178,8 +1185,9 @@ class HTTPConnection:
# As per RFC 273, IPv6 address should be wrapped with []
# when used as Host header
if host.find(':') >= 0:
if ":" in host:
host_enc = b'[' + host_enc + b']'
host_enc = _strip_ipv6_iface(host_enc)
if port == self.default_port:
self.putheader('Host', host_enc)