Skip to content
Myr

Merge pull request #1162 from Lukasa/proxy_auth

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

Proxy Authorization Headers

authored by Kenneth Reitz · committed by Kenneth Reitz · parents c68f928, d437c33 · Browse files · 3 files+33 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

3 files+33 added−1 removed

  • other:+33 added−1 removed

3 files+33 added−1 removed

modified:requests/adapters.py+18 added-1 removed
13 unmodified lines
14from .packages.urllib3.poolmanager import PoolManager, ProxyManager15from .packages.urllib3.response import HTTPResponse16from .compat import urlparse, basestring, urldefrag17from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,18 prepend_scheme_if_needed)18 prepend_scheme_if_needed, get_auth_from_url)19from .structures import CaseInsensitiveDict20from .packages.urllib3.exceptions import MaxRetryError21from .packages.urllib3.exceptions import TimeoutError22from .packages.urllib3.exceptions import SSLError as _SSLError23from .packages.urllib3.exceptions import HTTPError as _HTTPError24from .cookies import extract_cookies_to_jar25from .exceptions import ConnectionError, Timeout, SSLError26from .auth import _basic_auth_str2728DEFAULT_POOLSIZE = 1029DEFAULT_RETRIES = 030
115 unmodified lines
146 url = request.path_url147148 return url149150 def add_headers(self, request, **kwargs):151 """Add any headers needed by the connection. Currently this only adds a152 Host: header if a proxy is being used."""153 proxies = kwargs.get('proxies', {})154155 if proxies is None:156 proxies = {}157158 proxy = proxies.get(urlparse(request.url).scheme)159 username, password = get_auth_from_url(proxy)160161 if username and password:162 request.headers['Proxy-Authorization'] = _basic_auth_str(username,163 password)164165 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):166 """Sends PreparedRequest object. Returns Response object."""167168 conn = self.get_connection(request.url, proxies)169170 self.cert_verify(conn, request.url, verify, cert)171 url = self.request_url(request, proxies)172 self.add_headers(request, proxies=proxies)173174 chunked = not (request.body is None or 'Content-Length' in request.headers)175176 try:
62 unmodified lines
modified:requests/utils.py+10 added
578 unmodified lines
579 if not netloc:580 netloc, path = path, netloc581582 return urlunparse((scheme, netloc, path, params, query, fragment))583584585def get_auth_from_url(url):586 """Given a url with authentication components, extract them into a tuple of587 username,password."""588 if url:589 parsed = urlparse(url)590 return (parsed.username, parsed.password)591 else:592 return ('', '')
modified:test_requests.py+5 added
334 unmodified lines
335 r.raw = io336 self.assertTrue(next(iter(r)))337 io.close()338339 def test_get_auth_from_url(self):340 url = 'http://user:pass@complex.url.com/path?query=yes'341 self.assertEqual(('user', 'pass'),342 requests.utils.get_auth_from_url(url))343344if __name__ == '__main__':345 unittest.main()