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
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.
# 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
"""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:
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
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__'):
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__)
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