Skip to content

Commit 82b5f6b

Browse files
bpo-27657: Fix urlparse() with numeric paths (GH-661)
* bpo-27657: Fix urlparse() with numeric paths Revert parsing decision from bpo-754016 in favor of the documented consensus in bpo-16932 of how to treat strings without a // to designate the netloc. * bpo-22891: Remove urlsplit() optimization for 'http' prefixed inputs. (cherry picked from commit 5a88d50) Co-authored-by: Tim Graham <[email protected]>
1 parent 1bceb0e commit 82b5f6b

File tree

3 files changed

+9
-25
lines changed

3 files changed

+9
-25
lines changed

Lib/test/test_urlparse.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,17 @@ def test_withoutscheme(self):
709709

710710
def test_portseparator(self):
711711
# Issue 754016 makes changes for port separator ':' from scheme separator
712-
self.assertEqual(urllib.parse.urlparse("path:80"),
713-
('','','path:80','','',''))
712+
self.assertEqual(urllib.parse.urlparse("http:80"), ('http','','80','','',''))
713+
self.assertEqual(urllib.parse.urlparse("https:80"), ('https','','80','','',''))
714+
self.assertEqual(urllib.parse.urlparse("path:80"), ('path','','80','','',''))
714715
self.assertEqual(urllib.parse.urlparse("http:"),('http','','','','',''))
715716
self.assertEqual(urllib.parse.urlparse("https:"),('https','','','','',''))
716717
self.assertEqual(urllib.parse.urlparse("http://www.python.org:80"),
717718
('http','www.python.org:80','','','',''))
718719
# As usual, need to check bytes input as well
719-
self.assertEqual(urllib.parse.urlparse(b"path:80"),
720-
(b'',b'',b'path:80',b'',b'',b''))
720+
self.assertEqual(urllib.parse.urlparse(b"http:80"), (b'http',b'',b'80',b'',b'',b''))
721+
self.assertEqual(urllib.parse.urlparse(b"https:80"), (b'https',b'',b'80',b'',b'',b''))
722+
self.assertEqual(urllib.parse.urlparse(b"path:80"), (b'path',b'',b'80',b'',b'',b''))
721723
self.assertEqual(urllib.parse.urlparse(b"http:"),(b'http',b'',b'',b'',b'',b''))
722724
self.assertEqual(urllib.parse.urlparse(b"https:"),(b'https',b'',b'',b'',b'',b''))
723725
self.assertEqual(urllib.parse.urlparse(b"http://www.python.org:80"),

Lib/urllib/parse.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -426,31 +426,11 @@ def urlsplit(url, scheme='', allow_fragments=True):
426426
netloc = query = fragment = ''
427427
i = url.find(':')
428428
if i > 0:
429-
if url[:i] == 'http': # optimize the common case
430-
url = url[i+1:]
431-
if url[:2] == '//':
432-
netloc, url = _splitnetloc(url, 2)
433-
if (('[' in netloc and ']' not in netloc) or
434-
(']' in netloc and '[' not in netloc)):
435-
raise ValueError("Invalid IPv6 URL")
436-
if allow_fragments and '#' in url:
437-
url, fragment = url.split('#', 1)
438-
if '?' in url:
439-
url, query = url.split('?', 1)
440-
_checknetloc(netloc)
441-
v = SplitResult('http', netloc, url, query, fragment)
442-
_parse_cache[key] = v
443-
return _coerce_result(v)
444429
for c in url[:i]:
445430
if c not in scheme_chars:
446431
break
447432
else:
448-
# make sure "url" is not actually a port number (in which case
449-
# "scheme" is really part of the path)
450-
rest = url[i+1:]
451-
if not rest or any(c not in '0123456789' for c in rest):
452-
# not a port number
453-
scheme, url = url[:i].lower(), rest
433+
scheme, url = url[:i].lower(), url[i+1:]
454434

455435
if url[:2] == '//':
456436
netloc, url = _splitnetloc(url, 2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix urllib.parse.urlparse() with numeric paths. A string like "path:80" is
2+
no longer parsed as a path but as a scheme ("path") and a path ("80").

0 commit comments

Comments
 (0)