From 814e0520ad61387e89b22806d20544adf6ce8ed5 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Sat, 17 Dec 2011 13:40:04 -0600 Subject: [PATCH 1/2] Add Proxy Authorization support --- requests/auth.py | 8 ++++++++ requests/models.py | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/requests/auth.py b/requests/auth.py index fad6eb79..042962d8 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -35,6 +35,14 @@ class HTTPBasicAuth(AuthBase): return r +class HTTPProxyAuth(HTTPBasicAuth): + """Attaches HTTP Proxy Authenetication to a given Request object.""" + def __call__(self, r): + auth_s = b64encode('%s:%s' % (self.username, self.password)) + r.headers['Proxy-Authorization'] = ('Basic %s' % auth_s) + return r + + class HTTPDigestAuth(AuthBase): """Attaches HTTP Digest Authentication to the given Request object.""" def __init__(self, username, password): diff --git a/requests/models.py b/requests/models.py index 78af22c6..e043d88c 100644 --- a/requests/models.py +++ b/requests/models.py @@ -17,7 +17,7 @@ from .hooks import dispatch_hook from .structures import CaseInsensitiveDict from .status_codes import codes from .packages import oreos -from .auth import HTTPBasicAuth +from .auth import HTTPBasicAuth, HTTPProxyAuth from .packages.urllib3.exceptions import MaxRetryError from .packages.urllib3.exceptions import SSLError as _SSLError from .packages.urllib3.exceptions import HTTPError as _HTTPError @@ -407,6 +407,12 @@ class Request(object): if proxy: conn = poolmanager.proxy_from_url(proxy) + _proxy = urlparse(proxy) + if '@' in _proxy.netloc: + auth, url = _proxy.netloc.split('@', 1) + self.proxy_auth = HTTPProxyAuth(*auth.split(':', 1)) + r = self.proxy_auth(self) + self.__dict__.update(r.__dict__) else: # Check to see if keep_alive is allowed. if self.config.get('keep_alive'): From 4e2ecd698700698dc1ad0daedf910f3dfa53a4ee Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Sat, 17 Dec 2011 13:48:40 -0600 Subject: [PATCH 2/2] Fixed some tab/spaces mixed issues --- requests/models.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/requests/models.py b/requests/models.py index e043d88c..e84c8f1a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -409,10 +409,10 @@ class Request(object): conn = poolmanager.proxy_from_url(proxy) _proxy = urlparse(proxy) if '@' in _proxy.netloc: - auth, url = _proxy.netloc.split('@', 1) - self.proxy_auth = HTTPProxyAuth(*auth.split(':', 1)) - r = self.proxy_auth(self) - self.__dict__.update(r.__dict__) + auth, url = _proxy.netloc.split('@', 1) + self.proxy_auth = HTTPProxyAuth(*auth.split(':', 1)) + r = self.proxy_auth(self) + self.__dict__.update(r.__dict__) else: # Check to see if keep_alive is allowed. if self.config.get('keep_alive'):