Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/request/basicauthhandler.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
from thirdparty.six.moves import urllib as _urllib
9
10
class SmartHTTPBasicAuthHandler(_urllib.request.HTTPBasicAuthHandler):
11
"""
12
Reference: http://selenic.com/hg/rev/6c51a5056020
13
Fix for a: http://bugs.python.org/issue8797
14
"""
15
16
def __init__(self, *args, **kwargs):
17
_urllib.request.HTTPBasicAuthHandler.__init__(self, *args, **kwargs)
18
self.retried_req = set()
19
self.retried_count = 0
20
21
def reset_retry_count(self):
22
# Python 2.6.5 will call this on 401 or 407 errors and thus loop
23
# forever. We disable reset_retry_count completely and reset in
24
# http_error_auth_reqed instead.
25
pass
26
27
def http_error_auth_reqed(self, auth_header, host, req, headers):
28
# Reset the retry counter once for each request.
29
if hash(req) not in self.retried_req:
30
self.retried_req.add(hash(req))
31
self.retried_count = 0
32
else:
33
if self.retried_count > 5:
34
raise _urllib.error.HTTPError(req.get_full_url(), 401, "basic auth failed", headers, None)
35
else:
36
self.retried_count += 1
37
38
return _urllib.request.HTTPBasicAuthHandler.http_error_auth_reqed(self, auth_header, host, req, headers)
39
40