digest works!
authorKenneth Reitz <me@kennethreitz.com>
Mon, 17 Dec 2012 09:55:58 +0000 (04:55 -0500)
committerKenneth Reitz <me@kennethreitz.com>
Mon, 17 Dec 2012 09:55:58 +0000 (04:55 -0500)
added some nice backwards compatible stuff too

requests/adapters.py
requests/auth.py
requests/models.py
requests/sessions.py

index d6e3da6..a7cc514 100644 (file)
@@ -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
 
 
index 45a4ef1..9d33e5f 100644 (file)
@@ -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
index 49c8820..53fb247 100644 (file)
@@ -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__)
index c2ed48a..5fec1eb 100644 (file)
@@ -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