Skip to content

Commit 28d537d

Browse files
authored
Merge pull request #5917 from nateprewitt/proxy_scheme_unknown_fix
Move from urlparse to parse_url for prepending schemes
2 parents d096599 + ef59aa0 commit 28d537d

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

requests/utils.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import zipfile
2222
from collections import OrderedDict
2323
from urllib3.util import make_headers
24+
from urllib3.util import parse_url
2425

2526
from .__version__ import __version__
2627
from . import certs
@@ -963,15 +964,23 @@ def prepend_scheme_if_needed(url, new_scheme):
963964
964965
:rtype: str
965966
"""
966-
scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)
967-
968-
# urlparse is a finicky beast, and sometimes decides that there isn't a
969-
# netloc present. Assume that it's being over-cautious, and switch netloc
970-
# and path if urlparse decided there was no netloc.
967+
parsed = parse_url(url)
968+
scheme, auth, host, port, path, query, fragment = parsed
969+
970+
# A defect in urlparse determines that there isn't a netloc present in some
971+
# urls. We previously assumed parsing was overly cautious, and swapped the
972+
# netloc and path. Due to a lack of tests on the original defect, this is
973+
# maintained with parse_url for backwards compatibility.
974+
netloc = parsed.netloc
971975
if not netloc:
972976
netloc, path = path, netloc
973977

974-
return urlunparse((scheme, netloc, path, params, query, fragment))
978+
if scheme is None:
979+
scheme = new_scheme
980+
if path is None:
981+
path = ''
982+
983+
return urlunparse((scheme, netloc, path, '', query, fragment))
975984

976985

977986
def get_auth_from_url(url):

tests/test_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ def test_parse_header_links(value, expected):
601601
'value, expected', (
602602
('example.com/path', 'http://example.com/path'),
603603
('//example.com/path', 'http://example.com/path'),
604+
('example.com:80', 'http://example.com:80'),
604605
))
605606
def test_prepend_scheme_if_needed(value, expected):
606607
assert prepend_scheme_if_needed(value, 'http') == expected

0 commit comments

Comments
 (0)