Skip to content
Myr

Forced Basic Auth for 404'd resources

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 7d91d3f · Browse files · 1 file+59 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+59 added−2 removed

  • other:+59 added−2 removed

1 file+59 added−2 removed

modified:requests/monkeys.py+59 added-2 removed
7 unmodified lines
89"""1011import urllib212import re131314class Request(urllib2.Request):15 """Hidden wrapper around the urllib2.Request object. Allows for manual16 setting of HTTP methods.17 """
7 unmodified lines
25 return self.method2627 return urllib2.Request.get_method(self)2829class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):2930class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):31 """HTTP Redirect handler."""32 def http_error_301(self, req, fp, code, msg, headers):33 pass3435 http_error_302 = http_error_303 = http_error_307 = http_error_30136373839class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40 """HTTP Basic Auth Handler with authentication loop fixes."""4142 def __init__(self, *args, **kwargs):43 urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44 self.retried_req = None454647 def reset_retry_count(self):48 # Python 2.6.5 will call this on 401 or 407 errors and thus loop49 # forever. We disable reset_retry_count completely and reset in50 # http_error_auth_reqed instead.51 pass525354 def http_error_auth_reqed(self, auth_header, host, req, headers):55 # Reset the retry counter once for each request.56 if req is not self.retried_req:57 self.retried_req = req
4 unmodified lines
62 )63646566class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):67 """HTTP Basic Auth Handler with forced Authentication."""6869 auth_header = 'Authorization'70 rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'71 'realm=(["\'])(.*?)\\2', re.I)7273 def __init__(self, *args, **kwargs):74 HTTPBasicAuthHandler.__init__(self, *args, **kwargs)757677 def http_error_401(self, req, fp, code, msg, headers):78 url = req.get_full_url()79 response = self._http_error_auth_reqed('www-authenticate', url, req, headers)80 self.reset_retry_count()81 return response8283 http_error_404 = http_error_401848586 def _http_error_auth_reqed(self, authreq, host, req, headers):8788 authreq = headers.get(authreq, None)8990 if self.retried > 5:91 # retry sending the username:password 5 times before failing.92 raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",93 headers, None)94 else:95 self.retried += 19697 if authreq:9899 mo = self.rx.search(authreq)100101 if mo:102 scheme, quote, realm = mo.groups()103104 if scheme.lower() == 'basic':105 response = self.retry_http_basic_auth(host, req, realm)106107 if response and response.code not in (401, 404):108 self.retried = 0109 return response110 else:111 response = self.retry_http_basic_auth(host, req, 'Realm')112113 if response and response.code not in (401, 404):114 self.retried = 0115 return response116117118119class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):120121 def __init__(self, *args, **kwargs):122 urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)
25 unmodified lines