HTTP Basic recursion. Fixes #31
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent 62931b6 · Browse files · 2 files·+39 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+39 added−3 removed
- other:+39 added−3 removed
Provenance
- Commit
1421ffa- Tree
f6c1858- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
62931b6
2 files changed+39 added−3 removed
modified:requests/core.py+32 added-3 removed
53 unmodified lines54 55 return urllib2.Request.get_method(self)56 57 58+class _HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):59+ # from mercurial60+61+ def __init__(self, *args, **kwargs):62+ urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)63+ self.retried_req = None64+65+ def reset_retry_count(self):66+ # Python 2.6.5 will call this on 401 or 407 errors and thus loop67+ # forever. We disable reset_retry_count completely and reset in68+ # http_error_auth_reqed instead.69+ pass70+71+ def http_error_auth_reqed(self, auth_header, host, req, headers):72+ # Reset the retry counter once for each request.73+ if req is not self.retried_req:74+ self.retried_req = req75+ self.retried = 076+ return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(77+ self, auth_header, host, req, headers)78+79+80 class Request(object):81 """The :class:`Request` object. It carries out all functionality of82 Requests. Recommended interface is with the Requests functions.83 """90 unmodified lines174 return opener.open175 176 def _build_response(self, resp):177 """Build internal Response object from given response."""178+ if isinstance(resp, HTTPError):179+ # print resp.__dict__180+ pass181 182 self.response.status_code = getattr(resp, 'code', None)158- self.response.headers = getattr(resp.info(), 'dict', None)159- self.response.content = resp.read()183 184+ try:185+ self.response.headers = getattr(resp.info(), 'dict', None)186+ self.response.content = resp.read()187+ except AttributeError, why:188+ pass189+190 if self.response.headers.get('content-encoding', None) == 'gzip':191 try:192 self.response.content = zlib.decompress(self.response.content, 16+zlib.MAX_WBITS)193 except zlib.error:265 unmodified lines459 :param handler: (optional) basic || digest || proxy_basic || proxy_digest460 """461 462 _handlers = {434- 'basic': urllib2.HTTPBasicAuthHandler,463+ 'basic': _HTTPBasicAuthHandler,464 'digest': urllib2.HTTPDigestAuthHandler,465 'proxy_basic': urllib2.ProxyBasicAuthHandler,466 'proxy_digest': urllib2.ProxyDigestAuthHandler467 }123 unmodified linesmodified:test_requests.py+7 added
147 unmodified lines148 requests.get('http://google.com', params={'føø': 'føø'})149 requests.get('http://google.com', params={'foo': u'foo'})150 requests.get('http://google.com/ø', params={'foo': u'foo'})151 152+ def test_httpauth_recursion(self):153+ conv_auth = ('requeststest', 'bad_password')154 155+ r = requests.get('https://convore.com/api/account/verify.json', auth=conv_auth)156+ self.assertEquals(r.status_code, 401)157+ print r.__dict__158+159+160 if __name__ == '__main__':161 unittest.main()