Add method for rebuilding proxy configuration.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
This includes auth.
authored by Cory Benfield · committed by Cory Benfield · parent 97cf16e · Browse files · 1 file·+51 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+51 added−1 removed
- other:+51 added−1 removed
Provenance
- Commit
4d8cb32- Tree
2d483b6- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
97cf16e
1 file changed+51 added−1 removed
modified:requests/sessions.py+51 added-1 removed
10 unmodified lines11 import os12 from collections import Mapping13 from datetime import datetime14 15+from .auth import _basic_auth_str16 from .compat import cookielib, OrderedDict, urljoin, urlparse, builtin_str17 from .cookies import (18 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)19 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT3 unmodified lines23 from .structures import CaseInsensitiveDict24 25 from .adapters import HTTPAdapter26 26-from .utils import requote_uri, get_environ_proxies, get_netrc_auth27+from .utils import (28+ requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies,29+ get_auth_from_url30+)31 32 from .status_codes import codes33 34 # formerly defined here, reexposed here for backward compatibility122 unmodified lines157 extract_cookies_to_jar(prepared_request._cookies, prepared_request, resp.raw)158 prepared_request._cookies.update(self.cookies)159 prepared_request.prepare_cookies(prepared_request._cookies)160 161+ # Rebuild auth and proxy information.162+ proxies = self.rebuild_proxies(prepared_request, proxies)163 self.rebuild_auth(prepared_request, resp)164 165 resp = self.send(166 prepared_request,34 unmodified lines201 prepared_request.prepare_auth(new_auth)202 203 return204 205+ def rebuild_proxies(self, prepared_request, proxies):206+ """207+ This method re-evaluates the proxy configuration by considering the208+ environment variables. If we are redirected to a URL covered by209+ NO_PROXY, we strip the proxy configuration. Otherwise, we set missing210+ proxy keys for this URL (in case they were stripped by a previous211+ redirect).212 213+ This method also replaces the Proxy-Authorization header where214+ necessary.215+ """216+ headers = prepared_request.headers217+ url = prepared_request.url218+ new_proxies = {}219+220+ # Consider proxies. First evaluate the new proxy config. If we are221+ # being redirected to a host on the NO_PROXY list then we want to222+ # remove the proxy dictionary entirely. Otherwise, if there's a relevant223+ # environment proxy, set it if we don't already have a proxy to go to.224+ if not should_bypass_proxies(url):225+ environ_proxies = get_environ_proxies(url)226+ scheme = urlparse(url).scheme227+228+ try:229+ new_proxies.setdefault(scheme, environ_proxies[scheme])230+ except KeyError:231+ pass232+233+ # If there's a proxy-authorization header present, remove it, then add234+ # a new one (potentially re-adding the one we just removed).235+ if 'Proxy-Authorization' in headers:236+ del headers['Proxy-Authorization']237+238+ try:239+ username, password = get_auth_from_url(new_proxies[scheme])240+ if username and password:241+ headers['Proxy-Authorization'] = _basic_auth_str(242+ username, password243+ )244+ except KeyError:245+ pass246+247+ return new_proxies248+249+250 class Session(SessionRedirectMixin):251 """A Requests session.252 253 Provides cookie persistence, connection-pooling, and configuration.381 unmodified lines