Skip to content

Commit fbd6051

Browse files
socketpairasvetlov
authored andcommitted
bpo-32323: urllib.parse.urlsplit() must not lowercase() IPv6 scope value (#4867)
1 parent a8d25a1 commit fbd6051

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

Lib/test/test_urlparse.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,15 @@ def _encode(t):
520520
self.assertEqual(result.url, defrag)
521521
self.assertEqual(result.fragment, frag)
522522

523+
def test_urlsplit_scoped_IPv6(self):
524+
p = urllib.parse.urlsplit('http://[FE80::822a:a8ff:fe49:470c%tESt]:1234')
525+
self.assertEqual(p.hostname, "fe80::822a:a8ff:fe49:470c%tESt")
526+
self.assertEqual(p.netloc, '[FE80::822a:a8ff:fe49:470c%tESt]:1234')
527+
528+
p = urllib.parse.urlsplit(b'http://[FE80::822a:a8ff:fe49:470c%tESt]:1234')
529+
self.assertEqual(p.hostname, b"fe80::822a:a8ff:fe49:470c%tESt")
530+
self.assertEqual(p.netloc, b'[FE80::822a:a8ff:fe49:470c%tESt]:1234')
531+
523532
def test_urlsplit_attributes(self):
524533
url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
525534
p = urllib.parse.urlsplit(url)

Lib/urllib/parse.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,12 @@ def password(self):
155155
def hostname(self):
156156
hostname = self._hostinfo[0]
157157
if not hostname:
158-
hostname = None
159-
elif hostname is not None:
160-
hostname = hostname.lower()
161-
return hostname
158+
return None
159+
# Scoped IPv6 address may have zone info, which must not be lowercased
160+
# like http://[fe80::822a:a8ff:fe49:470c%tESt]:1234/keys
161+
separator = '%' if isinstance(hostname, str) else b'%'
162+
hostname, percent, zone = hostname.partition(separator)
163+
return hostname.lower() + percent + zone
162164

163165
@property
164166
def port(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`urllib.parse.urlsplit()` does not convert zone-id (scope) to lower case
2+
for scoped IPv6 addresses in hostnames now.

0 commit comments

Comments
 (0)