From 483790083238d327c5969007a9677ea57cb11880 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 17 Dec 2012 04:55:58 -0500 Subject: [PATCH] digest works! added some nice backwards compatible stuff too --- requests/adapters.py | 11 +++++++++-- requests/auth.py | 4 +--- requests/models.py | 8 +------- requests/sessions.py | 2 +- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/requests/adapters.py b/requests/adapters.py index d6e3da6..a7cc514 100644 --- a/requests/adapters.py +++ b/requests/adapters.py @@ -13,6 +13,7 @@ import socket from .models import Response from .packages.urllib3.poolmanager import PoolManager +from .hooks import dispatch_hook from .utils import DEFAULT_CA_BUNDLE_PATH, get_encoding_from_headers from .structures import CaseInsensitiveDict from .packages.urllib3.exceptions import MaxRetryError @@ -88,8 +89,8 @@ class HTTPAdapter(BaseAdapter): else: conn.cert_file = cert - @staticmethod - def build_response(req, resp): + # @staticmethod + def build_response(self, req, resp): response = Response() # Fallback to None if there's no status_code, for whatever reason. @@ -110,6 +111,12 @@ class HTTPAdapter(BaseAdapter): # Add new cookies from the server. extract_cookies_to_jar(response.cookies, req, resp) + # Give the Response some context. + response.request = req + response.connection = self + + # Run the Response hook. + response = dispatch_hook('response', req.hooks, response) return response diff --git a/requests/auth.py b/requests/auth.py index 45a4ef1..9d33e5f 100644 --- a/requests/auth.py +++ b/requests/auth.py @@ -141,7 +141,6 @@ class HTTPDigestAuth(AuthBase): """Takes the given response and tries digest-auth, if needed.""" num_401_calls = r.request.hooks['response'].count(self.handle_401) - s_auth = r.headers.get('www-authenticate', '') if 'digest' in s_auth.lower() and num_401_calls < 2: @@ -154,8 +153,7 @@ class HTTPDigestAuth(AuthBase): r.raw.release_conn() r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url) - r.request.send(anyway=True) - _r = r.request.response + _r = r.connection.send(r.request) _r.history.append(r) return _r diff --git a/requests/models.py b/requests/models.py index 49c8820..53fb247 100644 --- a/requests/models.py +++ b/requests/models.py @@ -146,9 +146,7 @@ class RequestEncodingMixin(object): class RequestHooksMixin(object): def register_hook(self, event, hook): """Properly register a hook.""" - print self - print event - print hook + if isinstance(hook, collections.Callable): self.hooks[event].append(hook) elif hasattr(hook, '__iter__'): @@ -342,16 +340,12 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): def prepare_auth(self, auth): """Prepares the given HTTP auth data.""" if auth: - # print auth if isinstance(auth, tuple) and len(auth) == 2: # special-case basic HTTP auth auth = HTTPBasicAuth(*auth) # Allow auth to make its changes. r = auth(self) - # print r - # print r.__dict__ - # print self.__dict__ # Update self to reflect the auth changes. self.__dict__.update(r.__dict__) diff --git a/requests/sessions.py b/requests/sessions.py index c2ed48a..5fec1eb 100644 --- a/requests/sessions.py +++ b/requests/sessions.py @@ -12,7 +12,7 @@ requests (cookies, auth, proxies). from .compat import cookielib from .cookies import cookiejar_from_dict, remove_cookie_by_name from .models import Request -from .hooks import dispatch_hook +from .hooks import dispatch_hook, HOOKS from .utils import header_expand, from_key_val_list, default_headers from .exceptions import TooManyRedirects -- 2.7.4