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
Provenance
- Commit
03f6908- Tree
6fc03aa- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
c68f928- Parent 2
d437c33
3 files changed+33 added−1 removed
modified:requests/adapters.py+18 added-1 removed
13 unmodified lines14 from .packages.urllib3.poolmanager import PoolManager, ProxyManager15 from .packages.urllib3.response import HTTPResponse16 from .compat import urlparse, basestring, urldefrag17 from .utils import (DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers,18- prepend_scheme_if_needed)18+ prepend_scheme_if_needed, get_auth_from_url)19 from .structures import CaseInsensitiveDict20 from .packages.urllib3.exceptions import MaxRetryError21 from .packages.urllib3.exceptions import TimeoutError22 from .packages.urllib3.exceptions import SSLError as _SSLError23 from .packages.urllib3.exceptions import HTTPError as _HTTPError24 from .cookies import extract_cookies_to_jar25 from .exceptions import ConnectionError, Timeout, SSLError26+from .auth import _basic_auth_str27 28 DEFAULT_POOLSIZE = 1029 DEFAULT_RETRIES = 030 115 unmodified lines146 url = request.path_url147 148 return url149 150+ 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', {})154+155+ if proxies is None:156+ proxies = {}157+158+ proxy = proxies.get(urlparse(request.url).scheme)159+ username, password = get_auth_from_url(proxy)160+161+ if username and password:162+ request.headers['Proxy-Authorization'] = _basic_auth_str(username,163+ password)164+165 def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):166 """Sends PreparedRequest object. Returns Response object."""167 168 conn = self.get_connection(request.url, proxies)169 170 self.cert_verify(conn, request.url, verify, cert)171 url = self.request_url(request, proxies)172+ self.add_headers(request, proxies=proxies)173 174 chunked = not (request.body is None or 'Content-Length' in request.headers)175 176 try:62 unmodified linesmodified:requests/utils.py+10 added
578 unmodified lines579 if not netloc:580 netloc, path = path, netloc581 582 return urlunparse((scheme, netloc, path, params, query, fragment))583+584+585+def 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 lines335 r.raw = io336 self.assertTrue(next(iter(r)))337 io.close()338 339+ 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))343+344 if __name__ == '__main__':345 unittest.main()