@@ -172,6 +172,12 @@ def _encode(data, name='data'):
172
172
"if you want to send it encoded in UTF-8." %
173
173
(name .title (), data [err .start :err .end ], name )) from None
174
174
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
175
181
176
182
class HTTPMessage (email .message .Message ):
177
183
# XXX The only usage of this method is in
@@ -1195,13 +1201,7 @@ def putrequest(self, method, url, skip_host=False,
1195
1201
netloc_enc = netloc .encode ("ascii" )
1196
1202
except UnicodeEncodeError :
1197
1203
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 ))
1205
1205
else :
1206
1206
if self ._tunnel_host :
1207
1207
host = self ._tunnel_host
@@ -1219,11 +1219,8 @@ def putrequest(self, method, url, skip_host=False,
1219
1219
# when used as Host header
1220
1220
1221
1221
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 ('%' )]
1226
1222
host_enc = b'[' + host_enc + b']'
1223
+ host_enc = _strip_ipv6_iface (host_enc )
1227
1224
1228
1225
if port == self .default_port :
1229
1226
self .putheader ('Host' , host_enc )
0 commit comments