From: Kenneth Reitz Date: Sat, 14 May 2011 18:02:36 +0000 (-0400) Subject: HTTP Basic recursion. Fixes #31 X-Git-Tag: v0.3.4^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1421ffa32ab4709c9a7634493803bd55a3c6615f;p=services%2Fpython-requests.git HTTP Basic recursion. Fixes #31 --- diff --git a/requests/core.py b/requests/core.py index a90cdd7..7a92c5f 100644 --- a/requests/core.py +++ b/requests/core.py @@ -55,6 +55,28 @@ class _Request(urllib2.Request): return urllib2.Request.get_method(self) +class _HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler): + # from mercurial + + def __init__(self, *args, **kwargs): + urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs) + self.retried_req = None + + def reset_retry_count(self): + # Python 2.6.5 will call this on 401 or 407 errors and thus loop + # forever. We disable reset_retry_count completely and reset in + # http_error_auth_reqed instead. + pass + + def http_error_auth_reqed(self, auth_header, host, req, headers): + # Reset the retry counter once for each request. + if req is not self.retried_req: + self.retried_req = req + self.retried = 0 + return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed( + self, auth_header, host, req, headers) + + class Request(object): """The :class:`Request` object. It carries out all functionality of Requests. Recommended interface is with the Requests functions. @@ -153,10 +175,17 @@ class Request(object): def _build_response(self, resp): """Build internal Response object from given response.""" + if isinstance(resp, HTTPError): + # print resp.__dict__ + pass self.response.status_code = getattr(resp, 'code', None) - self.response.headers = getattr(resp.info(), 'dict', None) - self.response.content = resp.read() + + try: + self.response.headers = getattr(resp.info(), 'dict', None) + self.response.content = resp.read() + except AttributeError, why: + pass if self.response.headers.get('content-encoding', None) == 'gzip': try: @@ -431,7 +460,7 @@ class AuthObject(object): """ _handlers = { - 'basic': urllib2.HTTPBasicAuthHandler, + 'basic': _HTTPBasicAuthHandler, 'digest': urllib2.HTTPDigestAuthHandler, 'proxy_basic': urllib2.ProxyBasicAuthHandler, 'proxy_digest': urllib2.ProxyDigestAuthHandler diff --git a/test_requests.py b/test_requests.py index ecd0177..3bc2e09 100755 --- a/test_requests.py +++ b/test_requests.py @@ -149,6 +149,13 @@ class RequestsTestSuite(unittest.TestCase): requests.get('http://google.com', params={'foo': u'foo'}) requests.get('http://google.com/ø', params={'foo': u'foo'}) + def test_httpauth_recursion(self): + conv_auth = ('requeststest', 'bad_password') + + r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth) + self.assertEquals(r.status_code, 401) + print r.__dict__ + if __name__ == '__main__': unittest.main()