Proxy urls should have explicit schemes.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Cory Benfield · committed by Cory Benfield · parent 9473f15 · Browse files · 2 files·+13 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+13 added−17 removed
- other:+13 added−17 removed
Provenance
- Commit
840540b- Tree
b2c9a51- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
9473f15
2 files changed+13 added−17 removed
modified:requests/adapters.py+2 added-2 removed
13 unmodified lines14 from .packages.urllib3.poolmanager import PoolManager, ProxyManager15 from .packages.urllib3.response import HTTPResponse16 from .compat import urlparse, basestring, urldefrag, unquote17 from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,18- prepend_scheme_if_needed, get_auth_from_url)18+ except_on_missing_scheme, get_auth_from_url)19 from .structures import CaseInsensitiveDict20 from .packages.urllib3.exceptions import MaxRetryError21 from .packages.urllib3.exceptions import TimeoutError22 from .packages.urllib3.exceptions import SSLError as _SSLError169 unmodified lines192 proxies = proxies or {}193 proxy = proxies.get(urlparse(url.lower()).scheme)194 195 if proxy:196- proxy = prepend_scheme_if_needed(proxy, urlparse(url.lower()).scheme)196+ except_on_missing_scheme(proxy)197 conn = ProxyManager(self.poolmanager.connection_from_url(proxy))198 else:199 conn = self.poolmanager.connection_from_url(url.lower())200 142 unmodified linesmodified:requests/utils.py+11 added-15 removed
23 unmodified lines24 from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse25 from .compat import getproxies, proxy_bypass26 from .cookies import RequestsCookieJar, cookiejar_from_dict27 from .structures import CaseInsensitiveDict28+from .exceptions import MissingSchema29 30 _hush_pyflakes = (RequestsCookieJar,)31 32 NETRC_FILES = ('.netrc', '_netrc')360 unmodified lines393 # First check whether no_proxy is defined. If it is, check that the URL394 # we're getting isn't in the no_proxy list.395 no_proxy = get_proxy('no_proxy')396 netloc = urlparse(url).netloc396- 397+398 if no_proxy:399 # We need to check whether we match here. We need to see if we match400 # the end of the netloc, both with and without the port.401 no_proxy = no_proxy.split(',')401- 402+403 for host in no_proxy:404 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):405 # The URL does match something in no_proxy, so we don't want406 # to apply the proxies on this URL.407 return {}407- 408+409 # If the system proxy settings indicate that this URL should be bypassed,410 # don't proxy.411 if proxy_bypass(netloc):412 return {}1 unmodified line414 # If we get here, we either didn't have no_proxy set or we're not going415 # anywhere that no_proxy applies to, and the system settings don't require416 # bypassing the proxy for the current URL.417 return getproxies()417- 418 419+420 def default_user_agent():421 """Return a string representing the default user agent."""422 _implementation = platform.python_implementation()423 100 unmodified lines524 # Did not detect a valid UTF-32 ascii-range character525 return None526 527 527-def prepend_scheme_if_needed(url, new_scheme):528- '''Given a URL that may or may not have a scheme, prepend the given scheme.529- Does not replace a present scheme with the one provided as an argument.'''530- scheme, netloc, path, params, query, fragment = urlparse(url, new_scheme)528+def except_on_missing_scheme(url):529+ """Given a URL, raise a MissingSchema exception if the scheme is missing.530+ """531+ scheme, netloc, path, params, query, fragment = urlparse(url)532 532- # urlparse is a finicky beast, and sometimes decides that there isn't a533- # netloc present. Assume that it's being over-cautious, and switch netloc534- # and path if urlparse decided there was no netloc.535- if not netloc:536- netloc, path = path, netloc533+ if not scheme:534+ raise MissingSchema('Proxy URLs must have explicit schemes.')535 538- return urlunparse((scheme, netloc, path, params, query, fragment))536 540-537 def get_auth_from_url(url):538 """Given a url with authentication components, extract them into a tuple of539 username,password."""540 if url:4 unmodified lines