summaryrefslogtreecommitdiffstats
path: root/Lib/urllib
diff options
context:
space:
mode:
authorSenthil Kumaran <senthil@uthcode.com>2014-08-16 17:22:37 (GMT)
committerSenthil Kumaran <senthil@uthcode.com>2014-08-16 17:22:37 (GMT)
commit402df0975c1adacdc3673b9308ec759a557f6b4b (patch)
treed924a828f1a1e96b038f02071de5d2b5c4e48264 /Lib/urllib
parent7869a4e03aa8624bdb7636120819a06c84503e60 (diff)
downloadcpython-402df0975c1adacdc3673b9308ec759a557f6b4b.zip
cpython-402df0975c1adacdc3673b9308ec759a557f6b4b.tar.gz
cpython-402df0975c1adacdc3673b9308ec759a557f6b4b.tar.bz2
backout changeset 3435c5865cfc due to buildbot failures. Ref #8797
Diffstat (limited to 'Lib/urllib')
-rw-r--r--Lib/urllib/request.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index a17c868..0389f5e 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -846,6 +846,10 @@ class AbstractBasicAuthHandler:
password_mgr = HTTPPasswordMgr()
self.passwd = password_mgr
self.add_password = self.passwd.add_password
+ self.retried = 0
+
+ def reset_retry_count(self):
+ self.retried = 0
def http_error_auth_reqed(self, authreq, host, req, headers):
# host may be an authority (without userinfo) or a URL with an
@@ -853,6 +857,13 @@ class AbstractBasicAuthHandler:
# XXX could be multiple headers
authreq = headers.get(authreq, None)
+ if self.retried > 5:
+ # retry sending the username:password 5 times before failing.
+ raise HTTPError(req.get_full_url(), 401, "basic auth failed",
+ headers, None)
+ else:
+ self.retried += 1
+
if authreq:
scheme = authreq.split()[0]
if scheme.lower() != 'basic':
@@ -867,14 +878,17 @@ class AbstractBasicAuthHandler:
warnings.warn("Basic Auth Realm was unquoted",
UserWarning, 2)
if scheme.lower() == 'basic':
- return self.retry_http_basic_auth(host, req, realm)
+ response = self.retry_http_basic_auth(host, req, realm)
+ if response and response.code != 401:
+ self.retried = 0
+ return response
def retry_http_basic_auth(self, host, req, realm):
user, pw = self.passwd.find_user_password(realm, host)
if pw is not None:
raw = "%s:%s" % (user, pw)
auth = "Basic " + base64.b64encode(raw.encode()).decode("ascii")
- if req.get_header(self.auth_header, None) == auth:
+ if req.headers.get(self.auth_header, None) == auth:
return None
req.add_unredirected_header(self.auth_header, auth)
return self.parent.open(req, timeout=req.timeout)
@@ -890,6 +904,7 @@ class HTTPBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
url = req.full_url
response = self.http_error_auth_reqed('www-authenticate',
url, req, headers)
+ self.reset_retry_count()
return response
@@ -905,6 +920,7 @@ class ProxyBasicAuthHandler(AbstractBasicAuthHandler, BaseHandler):
authority = req.host
response = self.http_error_auth_reqed('proxy-authenticate',
authority, req, headers)
+ self.reset_retry_count()
return response