Skip to content

Commit

Permalink
Merge pull request #5917 from nateprewitt/proxy_scheme_unknown_fix
Browse files Browse the repository at this point in the history
Move from urlparse to parse_url for prepending schemes
  • Loading branch information
sigmavirus24 committed Dec 29, 2021
2 parents d096599 + ef59aa0 commit 28d537d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
21 changes: 15 additions & 6 deletions requests/utils.py
Expand Up @@ -21,6 +21,7 @@
import zipfile
from collections import OrderedDict
from urllib3.util import make_headers
from urllib3.util import parse_url

from .__version__ import __version__
from . import certs
Expand Down Expand Up @@ -963,15 +964,23 @@ def prepend_scheme_if_needed(url, new_scheme):
:rtype: str
"""
scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)

# urlparse is a finicky beast, and sometimes decides that there isn't a
# netloc present. Assume that it's being over-cautious, and switch netloc
# and path if urlparse decided there was no netloc.
parsed = parse_url(url)
scheme, auth, host, port, path, query, fragment = parsed

# A defect in urlparse determines that there isn't a netloc present in some
# urls. We previously assumed parsing was overly cautious, and swapped the
# netloc and path. Due to a lack of tests on the original defect, this is
# maintained with parse_url for backwards compatibility.
netloc = parsed.netloc
if not netloc:
netloc, path = path, netloc

return urlunparse((scheme, netloc, path, params, query, fragment))
if scheme is None:
scheme = new_scheme
if path is None:
path = ''

return urlunparse((scheme, netloc, path, '', query, fragment))


def get_auth_from_url(url):
Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Expand Up @@ -601,6 +601,7 @@ def test_parse_header_links(value, expected):
'value, expected', (
('example.com/path', 'http://example.com/path'),
('//example.com/path', 'http://example.com/path'),
('example.com:80', 'http://example.com:80'),
))
def test_prepend_scheme_if_needed(value, expected):
assert prepend_scheme_if_needed(value, 'http') == expected
Expand Down

0 comments on commit 28d537d

Please sign in to comment.