Skip to content
Myr

Refactor rebuild_proxies to allow proxy resolution (#5924)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

without stripping Proxy-Authorization header

authored by Nate Prewitt · committed by GitHub · parent 590350f · Browse files · 3 files+45 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+45 added−18 removed

  • tests:+10 added−1 removed
  • other:+35 added−17 removed

3 files+45 added−18 removed

modified:requests/sessions.py+7 added-17 removed
27 unmodified lines
28from .adapters import HTTPAdapter2930from .utils import (31 requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies,32 get_auth_from_url, rewind_body32 get_auth_from_url, rewind_body, resolve_proxies33)3435from .status_codes import codes36
231 unmodified lines
268 new_auth = get_netrc_auth(url) if self.trust_env else None269 if new_auth is not None:270 prepared_request.prepare_auth(new_auth)271272272 def rebuild_proxies(self, prepared_request, proxies):273 """This method re-evaluates the proxy configuration by considering the274 environment variables. If we are redirected to a URL covered by275 NO_PROXY, we strip the proxy configuration. Otherwise, we set missing
4 unmodified lines
280 necessary.281282 :rtype: dict283 """285 proxies = proxies if proxies is not None else {}284 headers = prepared_request.headers287 url = prepared_request.url288 scheme = urlparse(url).scheme289 new_proxies = proxies.copy()290 no_proxy = proxies.get('no_proxy')285 scheme = urlparse(prepared_request.url).scheme286 new_proxies = resolve_proxies(prepared_request, proxies, self.trust_env)287292 bypass_proxy = should_bypass_proxies(url, no_proxy=no_proxy)293 if self.trust_env and not bypass_proxy:294 environ_proxies = get_environ_proxies(url, no_proxy=no_proxy)295296 proxy = environ_proxies.get(scheme, environ_proxies.get('all'))297298 if proxy:299 new_proxies.setdefault(scheme, proxy)300288 if 'Proxy-Authorization' in headers:289 del headers['Proxy-Authorization']290291 try:
327 unmodified lines
619 # the correct parameters to reproduce the previous request.620 kwargs.setdefault('stream', self.stream)621 kwargs.setdefault('verify', self.verify)622 kwargs.setdefault('cert', self.cert)636 kwargs.setdefault('proxies', self.rebuild_proxies(request, self.proxies))623 if 'proxies' not in kwargs:624 kwargs['proxies'] = resolve_proxies(625 request, self.proxies, self.trust_env626 )627628 # It's possible that users might accidentally send a Request object.629 # Guard against that specific failure case.630 if isinstance(request, Request):
141 unmodified lines
modified:requests/utils.py+28 added
828 unmodified lines
829830 return proxy831832833def resolve_proxies(request, proxies, trust_env=True):834 """This method takes proxy information from a request and configuration835 input to resolve a mapping of target proxies. This will consider settings836 such a NO_PROXY to strip proxy configurations.837838 :param request: Request or PreparedRequest839 :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs840 :param trust_env: Boolean declaring whether to trust environment configs841842 :rtype: dict843 """844 proxies = proxies if proxies is not None else {}845 url = request.url846 scheme = urlparse(url).scheme847 no_proxy = proxies.get('no_proxy')848 new_proxies = proxies.copy()849850 bypass_proxy = should_bypass_proxies(url, no_proxy=no_proxy)851 if trust_env and not bypass_proxy:852 environ_proxies = get_environ_proxies(url, no_proxy=no_proxy)853854 proxy = environ_proxies.get(scheme, environ_proxies.get('all'))855856 if proxy:857 new_proxies.setdefault(scheme, proxy)858 return new_proxies859860861def default_user_agent(name="python-requests"):862 """863 Return a string representing the default user agent.864
181 unmodified lines
modified:tests/test_requests.py+10 added-1 removed
588 unmodified lines
589 with pytest.raises(ProxyError):590 session = requests.Session()591 session.request(method='GET', url=httpbin())592593 def test_proxy_authorization_preserved_on_request(self, httpbin):594 proxy_auth_value = "Bearer XXX"595 session = requests.Session()596 session.headers.update({"Proxy-Authorization": proxy_auth_value})597 resp = session.request(method='GET', url=httpbin('get'))598 sent_headers = resp.json().get('headers', {})599600 assert sent_headers.get("Proxy-Authorization") == proxy_auth_value601602 def test_basicauth_with_netrc(self, httpbin):603 auth = ('user', 'pass')604 wrong_auth = ('wronguser', 'wrongpass')605 url = httpbin('basic-auth', 'user', 'pass')
1977 unmodified lines
25832584 def test_json_decode_compatibility(self, httpbin):2585 r = requests.get(httpbin('bytes/20'))2586 with pytest.raises(requests.exceptions.JSONDecodeError):2578 r.json()2587 r.json()