mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
[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:
parent
11b91be55b
commit
f6e11eab49
3 changed files with 27 additions and 2 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue