27
27
return urllib2.Request.get_method(self)
29
30
class HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
31
"""HTTP Redirect handler."""
31
32
def http_error_301(self, req, fp, code, msg, headers):
38
39
class HTTPBasicAuthHandler(urllib2.HTTPBasicAuthHandler):
40
"""HTTP Basic Auth Handler with authentication loop fixes."""
40
42
def __init__(self, *args, **kwargs):
41
43
urllib2.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
42
44
self.retried_req = None
44
48
def reset_retry_count(self):
45
49
# Python 2.6.5 will call this on 401 or 407 errors and thus loop
47
51
# http_error_auth_reqed instead.
50
55
def http_error_auth_reqed(self, auth_header, host, req, headers):
51
56
# Reset the retry counter once for each request.
52
57
if req is not self.retried_req:
67
class HTTPForcedBasicAuthHandler(HTTPBasicAuthHandler):
68
"""HTTP Basic Auth Handler with forced Authentication."""
70
auth_header = 'Authorization'
71
rx = re.compile('(?:.*,)*[ \t]*([^ \t]+)[ \t]+'
72
'realm=(["\'])(.*?)\\2', re.I)
74
def __init__(self, *args, **kwargs):
75
HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
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()
84
http_error_404 = http_error_401
87
def _http_error_auth_reqed(self, authreq, host, req, headers):
89
authreq = headers.get(authreq, None)
92
# retry sending the username:password 5 times before failing.
93
raise urllib2.HTTPError(req.get_full_url(), 401, "basic auth failed",
100
mo = self.rx.search(authreq)
103
scheme, quote, realm = mo.groups()
105
if scheme.lower() == 'basic':
106
response = self.retry_http_basic_auth(host, req, realm)
108
if response and response.code not in (401, 404):
112
response = self.retry_http_basic_auth(host, req, 'Realm')
114
if response and response.code not in (401, 404):
62
120
class HTTPDigestAuthHandler(urllib2.HTTPDigestAuthHandler):
64
122
def __init__(self, *args, **kwargs):