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
Provenance
- Commit
99b3b49- Tree
35e8097- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
590350f
3 files changed+45 added−18 removed
modified:requests/sessions.py+7 added-17 removed
27 unmodified lines28 from .adapters import HTTPAdapter29 30 from .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 )34 35 from .status_codes import codes36 231 unmodified lines268 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)271 272-272 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 missing4 unmodified lines280 necessary.281 282 :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)287 292- 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)295-296- proxy = environ_proxies.get(scheme, environ_proxies.get('all'))297-298- if proxy:299- new_proxies.setdefault(scheme, proxy)300-288 if 'Proxy-Authorization' in headers:289 del headers['Proxy-Authorization']290 291 try:327 unmodified lines619 # 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+ )627 628 # 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 linesmodified:requests/utils.py+28 added
828 unmodified lines829 830 return proxy831 832 833+def 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.837+838+ :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 configs841+842+ :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()849+850+ 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)853+854+ proxy = environ_proxies.get(scheme, environ_proxies.get('all'))855+856+ if proxy:857+ new_proxies.setdefault(scheme, proxy)858+ return new_proxies859+860+861 def default_user_agent(name="python-requests"):862 """863 Return a string representing the default user agent.864 181 unmodified linesmodified:tests/test_requests.py+10 added-1 removed
588 unmodified lines589 with pytest.raises(ProxyError):590 session = requests.Session()591 session.request(method='GET', url=httpbin())592 593+ 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', {})599+600+ assert sent_headers.get("Proxy-Authorization") == proxy_auth_value601+602 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 lines2583 2584 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()