Factor out the proxy selection code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Jason Grout · committed by Jason Grout · parent 2c87215 · Browse files · 2 files·+13 added−13 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−13 removed
- other:+13 added−13 removed
Provenance
- Commit
500f15d- Tree
b5508ea- Author
Jason Grout <jgrout6@bloomberg.net>- Committer
Jason Grout <jgrout6@bloomberg.net>- Parent
2c87215
2 files changed+13 added−13 removed
modified:requests/adapters.py+5 added-13 removed
15 unmodified lines16 from .packages.urllib3.util import Timeout as TimeoutSauce17 from .packages.urllib3.util.retry import Retry18 from .compat import urlparse, basestring19 from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,20- prepend_scheme_if_needed, get_auth_from_url, urldefragauth)20+ prepend_scheme_if_needed, get_auth_from_url, urldefragauth,21+ select_proxy)22 from .structures import CaseInsensitiveDict23 from .packages.urllib3.exceptions import ConnectTimeoutError24 from .packages.urllib3.exceptions import HTTPError as _HTTPError25 from .packages.urllib3.exceptions import MaxRetryError212 unmodified lines238 239 :param url: The URL to connect to.240 :param proxies: (optional) A Requests-style dictionary of proxies used on this request.241 """241- proxies = proxies or {}242- urlparts = urlparse(url.lower())243- proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)244- if proxy is None:245- proxy = proxies.get(urlparts.scheme)242+ proxy = select_proxy(url, proxies)243 244 if proxy:245 proxy = prepend_scheme_if_needed(proxy, 'http')246 proxy_manager = self.proxy_manager_for(proxy)26 unmodified lines273 274 :param request: The :class:`PreparedRequest <PreparedRequest>` being sent.275 :param proxies: A dictionary of schemes to proxy URLs.276 """280- proxies = proxies or {}281- urlparts = urlparse(request.url.lower())282- proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)283- if proxy is None:284- proxy = proxies.get(urlparts.scheme)285-286- if proxy and urlparts.scheme != 'https':277+ proxy = select_proxy(request.url, proxies)278+ if proxy and not request.url.lower().startswith('https'):279 url = urldefragauth(request.url)280 else:281 url = request.path_url282 149 unmodified linesmodified:requests/utils.py+8 added
535 unmodified lines536 return {}537 else:538 return getproxies()539 540+def select_proxy(url, proxies):541+ """Select a proxy, if applicable."""542+ proxies = proxies or {}543+ urlparts = urlparse(url.lower())544+ proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)545+ if proxy is None:546+ proxy = proxies.get(urlparts.scheme)547+ return proxy548 549 def default_user_agent(name="python-requests"):550 """Return a string representing the default user agent."""551 _implementation = platform.python_implementation()165 unmodified lines