Skip to content
Myr

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

1 file+51 added−1 removed

modified:requests/sessions.py+51 added-1 removed
10 unmodified lines
11import os12from collections import Mapping13from datetime import datetime1415from .auth import _basic_auth_str16from .compat import cookielib, OrderedDict, urljoin, urlparse, builtin_str17from .cookies import (18 cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)19from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
3 unmodified lines
23from .structures import CaseInsensitiveDict2425from .adapters import HTTPAdapter2626from .utils import requote_uri, get_environ_proxies, get_netrc_auth27from .utils import (28 requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies,29 get_auth_from_url30)3132from .status_codes import codes3334# formerly defined here, reexposed here for backward compatibility
122 unmodified lines
157 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)160161 # Rebuild auth and proxy information.162 proxies = self.rebuild_proxies(prepared_request, proxies)163 self.rebuild_auth(prepared_request, resp)164165 resp = self.send(166 prepared_request,
34 unmodified lines
201 prepared_request.prepare_auth(new_auth)202203 return204205 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).212213 This method also replaces the Proxy-Authorization header where214 necessary.215 """216 headers = prepared_request.headers217 url = prepared_request.url218 new_proxies = {}219220 # 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).scheme227228 try:229 new_proxies.setdefault(scheme, environ_proxies[scheme])230 except KeyError:231 pass232233 # 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']237238 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 pass246247 return new_proxies248249250class Session(SessionRedirectMixin):251 """A Requests session.252253 Provides cookie persistence, connection-pooling, and configuration.
381 unmodified lines