Skip to content

Commit

Permalink
[3.8] gh-67693: Fix urlunparse() and urlunsplit() for URIs with path …
Browse files Browse the repository at this point in the history
…starting with multiple slashes and no authority (GH-113563) (#119028)

(cherry picked from commit e237b25)

Co-authored-by: Łukasz Langa <[email protected]>
  • Loading branch information
serhiy-storchaka and ambv authored Sep 4, 2024
1 parent 7d1f50c commit 0a87812
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
70 changes: 67 additions & 3 deletions Lib/test/test_urlparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,17 @@

class UrlParseTestCase(unittest.TestCase):

def checkRoundtrips(self, url, parsed, split):
def checkRoundtrips(self, url, parsed, split, url2=None):
if url2 is None:
url2 = url
result = urllib.parse.urlparse(url)
self.assertEqual(result, parsed)
t = (result.scheme, result.netloc, result.path,
result.params, result.query, result.fragment)
self.assertEqual(t, parsed)
# put it back together and it should be the same
result2 = urllib.parse.urlunparse(result)
self.assertEqual(result2, url)
self.assertEqual(result2, url2)
self.assertEqual(result2, result.geturl())

# the result of geturl() is a fixpoint; we can always parse it
Expand All @@ -104,7 +106,7 @@ def checkRoundtrips(self, url, parsed, split):
result.query, result.fragment)
self.assertEqual(t, split)
result2 = urllib.parse.urlunsplit(result)
self.assertEqual(result2, url)
self.assertEqual(result2, url2)
self.assertEqual(result2, result.geturl())

# check the fixpoint property of re-parsing the result of geturl()
Expand Down Expand Up @@ -142,9 +144,39 @@ def test_qs(self):

def test_roundtrips(self):
str_cases = [
('path/to/file',
('', '', 'path/to/file', '', '', ''),
('', '', 'path/to/file', '', '')),
('/path/to/file',
('', '', '/path/to/file', '', '', ''),
('', '', '/path/to/file', '', '')),
('//path/to/file',
('', 'path', '/to/file', '', '', ''),
('', 'path', '/to/file', '', '')),
('////path/to/file',
('', '', '//path/to/file', '', '', ''),
('', '', '//path/to/file', '', '')),
('scheme:path/to/file',
('scheme', '', 'path/to/file', '', '', ''),
('scheme', '', 'path/to/file', '', '')),
('scheme:/path/to/file',
('scheme', '', '/path/to/file', '', '', ''),
('scheme', '', '/path/to/file', '', '')),
('scheme://path/to/file',
('scheme', 'path', '/to/file', '', '', ''),
('scheme', 'path', '/to/file', '', '')),
('scheme:////path/to/file',
('scheme', '', '//path/to/file', '', '', ''),
('scheme', '', '//path/to/file', '', '')),
('file:///tmp/junk.txt',
('file', '', '/tmp/junk.txt', '', '', ''),
('file', '', '/tmp/junk.txt', '', '')),
('file:////tmp/junk.txt',
('file', '', '//tmp/junk.txt', '', '', ''),
('file', '', '//tmp/junk.txt', '', '')),
('file://///tmp/junk.txt',
('file', '', '///tmp/junk.txt', '', '', ''),
('file', '', '///tmp/junk.txt', '', '')),
('imap://mail.python.org/mbox1',
('imap', 'mail.python.org', '/mbox1', '', '', ''),
('imap', 'mail.python.org', '/mbox1', '', '')),
Expand Down Expand Up @@ -175,6 +207,38 @@ def _encode(t):
for url, parsed, split in str_cases + bytes_cases:
self.checkRoundtrips(url, parsed, split)

def test_roundtrips_normalization(self):
str_cases = [
('///path/to/file',
'/path/to/file',
('', '', '/path/to/file', '', '', ''),
('', '', '/path/to/file', '', '')),
('scheme:///path/to/file',
'scheme:/path/to/file',
('scheme', '', '/path/to/file', '', '', ''),
('scheme', '', '/path/to/file', '', '')),
('file:/tmp/junk.txt',
'file:///tmp/junk.txt',
('file', '', '/tmp/junk.txt', '', '', ''),
('file', '', '/tmp/junk.txt', '', '')),
('http:/tmp/junk.txt',
'http:///tmp/junk.txt',
('http', '', '/tmp/junk.txt', '', '', ''),
('http', '', '/tmp/junk.txt', '', '')),
('https:/tmp/junk.txt',
'https:///tmp/junk.txt',
('https', '', '/tmp/junk.txt', '', '', ''),
('https', '', '/tmp/junk.txt', '', '')),
]
def _encode(t):
return (t[0].encode('ascii'),
t[1].encode('ascii'),
tuple(x.encode('ascii') for x in t[2]),
tuple(x.encode('ascii') for x in t[3]))
bytes_cases = [_encode(x) for x in str_cases]
for url, url2, parsed, split in str_cases + bytes_cases:
self.checkRoundtrips(url, parsed, split, url2)

def test_http_roundtrips(self):
# urllib.parse.urlsplit treats 'http:' as an optimized special case,
# so we test both 'http:' and 'https:' in all the following.
Expand Down
2 changes: 1 addition & 1 deletion Lib/urllib/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def urlunsplit(components):
empty query; the RFC states that these are equivalent)."""
scheme, netloc, url, query, fragment, _coerce_result = (
_coerce_args(*components))
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
if netloc or (scheme and scheme in uses_netloc) or url[:2] == '//':
if url and url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url
if scheme:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`urllib.parse.urlunparse` and :func:`urllib.parse.urlunsplit` for URIs with path starting with multiple slashes and no authority.
Based on patch by Ashwin Ramaswami.

0 comments on commit 0a87812

Please sign in to comment.