KILL THE AUTH HANDLERS
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 68b4830 · Browse files · 2 files·+6 added−117 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+6 added−117 removed
- other:+6 added−117 removed
Provenance
- Commit
b88fbb5- Tree
d75fa07- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
68b4830
2 files changed+6 added−117 removed
modified:requests/models.py+1 added-3 removed
22 unmodified lines23 from .utils import (dict_from_cookiejar, get_unicode_from_response, stream_decode_response_unicode, decode_gzip, stream_decode_gzip)24 from .status_codes import codes25 from .exceptions import Timeout, URLRequired, TooManyRedirects26 from .monkeys import Request as _Request27-from .monkeys import (28- HTTPBasicAuthHandler, HTTPForcedBasicAuthHandler,29- HTTPDigestAuthHandler, HTTPRedirectHandler)27+from .monkeys import HTTPRedirectHandler28 29 from .auth import dispatch as auth_dispatch30 31 REDIRECT_STATI = (codes.moved, codes.found, codes.other, codes.temporary_moved)488 unmodified linesmodified:requests/monkeys.py+5 added-114 removed
27 unmodified lines28 29 30 class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):31 """HTTP Redirect handler."""32- def http_error_301(self, req, fp, code, msg, headers):32+ def _pass(self, req, fp, code, msg, headers):33 pass34 35- http_error_302 = http_error_303 = http_error_307 = http_error_30135+ http_error_302 = _pass36+ http_error_303 = _pass37+ http_error_307 = _pass38+ http_error_301 = _pass39 37-38-39-class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):40- """HTTP Basic Auth Handler with authentication loop fixes."""41-42- def __init__(self, *args, **kwargs):43- urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)44- self.retried_req = None45- self.retried = 046-47-48- def reset_retry_count(self):49- # Python 2.6.5 will call this on 401 or 407 errors and thus loop50- # forever. We disable reset_retry_count completely and reset in51- # http_error_auth_reqed instead.52- pass53-54-55- def http_error_auth_reqed(self, auth_header, host, req, headers):56- # Reset the retry counter once for each request.57- if req is not self.retried_req:58- self.retried_req = req59- self.retried = 060-61- return urllib2.HTTPBasicAuthHandler.http_error_auth_reqed(62- self, auth_header, host, req, headers63- )64-65-66-67-class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):68- """HTTP Basic Auth Handler with forced Authentication."""69-70- auth_header = 'Authorization'71- rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'72- 'realm=(["\'])(.*?)\\2', re.I)73-74- def __init__(self, *args, **kwargs):75- HTTPBasicAuthHandler.__init__(self, *args, **kwargs)76-77-78- def http_error_401(self, req, fp, code, msg, headers):79- url = req.get_full_url()80- response = self._http_error_auth_reqed('www-authenticate', url, req, headers)81- self.reset_retry_count()82- return response83-84- http_error_404 = http_error_40185-86-87- def _http_error_auth_reqed(self, authreq, host, req, headers):88-89- authreq = headers.get(authreq, None)90-91- if self.retried > 5:92- # retry sending the username:password 5 times before failing.93- raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",94- headers, None)95- else:96- self.retried += 197-98- if authreq:99-100- mo = self.rx.search(authreq)101-102- if mo:103- scheme, quote, realm = mo.groups()104-105- if scheme.lower() == 'basic':106- response = self.retry_http_basic_auth(host, req, realm)107-108- if response and response.code not in (401, 404):109- self.retried = 0110- return response111- else:112- response = self.retry_http_basic_auth(host, req, 'Realm')113-114- if response and response.code not in (401, 404):115- self.retried = 0116- return response117-118-119-120-class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):121-122- def __init__(self, *args, **kwargs):123- urllib2.HTTPDigestAuthHandler.__init__(self, *args, **kwargs)124- self.retried_req = None125-126- def reset_retry_count(self):127- # Python 2.6.5 will call this on 401 or 407 errors and thus loop128- # forever. We disable reset_retry_count completely and reset in129- # http_error_auth_reqed instead.130- pass131-132- def http_error_auth_reqed(self, auth_header, host, req, headers):133- # Reset the retry counter once for each request.134- if req is not self.retried_req:135- self.retried_req = req136- self.retried = 0137- # In python < 2.5 AbstractDigestAuthHandler raises a ValueError if138- # it doesn't know about the auth type requested. This can happen if139- # somebody is using BasicAuth and types a bad password.140-141- try:142- return urllib2.HTTPDigestAuthHandler.http_error_auth_reqed(143- self, auth_header, host, req, headers)144- except ValueError, inst:145- arg = inst.args[0]146- if arg.startswith("AbstractDigestAuthHandler doesn't know "):147- return148- raise