Skip to content

Commit 43fc859

Browse files
committed
use partition method to split bytes
1 parent a69e99a commit 43fc859

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Lib/http/client.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ def _encode(data, name='data'):
172172
"if you want to send it encoded in UTF-8." %
173173
(name.title(), data[err.start:err.end], name)) from None
174174

175+
def _strip_ipv6_iface(enc_name: bytes) -> bytes:
176+
"""Remove interface scope from IPv6 address."""
177+
enc_name, percent, _ = enc_name.partition(b"%")
178+
if percent:
179+
enc_name += b']'
180+
return enc_name
175181

176182
class HTTPMessage(email.message.Message):
177183
# XXX The only usage of this method is in
@@ -1195,13 +1201,7 @@ def putrequest(self, method, url, skip_host=False,
11951201
netloc_enc = netloc.encode("ascii")
11961202
except UnicodeEncodeError:
11971203
netloc_enc = netloc.encode("idna")
1198-
1199-
# remove interface scope from IPv6 address
1200-
# when used as Host header
1201-
if "%" in netloc:
1202-
netloc_enc = netloc_enc[:netloc.find('%')] + b']'
1203-
1204-
self.putheader('Host', netloc_enc)
1204+
self.putheader('Host', _strip_ipv6_iface(netloc_enc))
12051205
else:
12061206
if self._tunnel_host:
12071207
host = self._tunnel_host
@@ -1219,11 +1219,8 @@ def putrequest(self, method, url, skip_host=False,
12191219
# when used as Host header
12201220

12211221
if ":" in host:
1222-
# remove interface scope from IPv6 address
1223-
# when used as Host header
1224-
if "%" in host:
1225-
host_enc = host_enc[:host.find('%')]
12261222
host_enc = b'[' + host_enc + b']'
1223+
host_enc = _strip_ipv6_iface(host_enc)
12271224

12281225
if port == self.default_port:
12291226
self.putheader('Host', host_enc)

0 commit comments

Comments
 (0)