|
21 | 21 | import zipfile |
22 | 22 | from collections import OrderedDict |
23 | 23 | from urllib3.util import make_headers |
| 24 | +from urllib3.util import parse_url |
24 | 25 |
|
25 | 26 | from .__version__ import __version__ |
26 | 27 | from . import certs |
@@ -963,15 +964,23 @@ def prepend_scheme_if_needed(url, new_scheme): |
963 | 964 |
|
964 | 965 | :rtype: str |
965 | 966 | """ |
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 |
971 | 975 | if not netloc: |
972 | 976 | netloc, path = path, netloc |
973 | 977 |
|
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)) |
975 | 984 |
|
976 | 985 |
|
977 | 986 | def get_auth_from_url(url): |
|
0 commit comments