Skip to content

Commit

Permalink
Move from urlparse to parse_url for prepending schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Aug 27, 2021
1 parent b351e90 commit bfab037
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 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 @@ -932,15 +933,22 @@ def prepend_scheme_if_needed(url, new_scheme):
:rtype: str
"""
scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)
parsed = parse_url(url)
scheme, auth, host, port, path, query, fragment = parsed

# 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.
# urlparse and parse_url determine that there isn't a netloc present in some
# urls. We've chosen to assume parsing is being over-cautious, and switch
# the netloc and path. This is maintained 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 @@ -588,6 +588,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 bfab037

Please sign in to comment.