merge from 3.4 · pythoncapi/cpython@2b7ccbd · GitHub
Skip to content

Commit 2b7ccbd

Browse files
committed
merge from 3.4
Fix Issue python#8797: Raise HTTPError on failed Basic Authentication immediately. Initial patch by Sam Bull.
2 parents f2e500c + 7837376 commit 2b7ccbd

3 files changed

Lines changed: 88 additions & 18 deletions

File tree

Lib/test/test_urllib2_localnet.py

Lines changed: 83 additions & 0 deletions

Lib/urllib/request.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -846,24 +846,13 @@ def __init__(self, password_mgr=None):
846846
password_mgr = HTTPPasswordMgr()
847847
self.passwd = password_mgr
848848
self.add_password = self.passwd.add_password
849-
self.retried = 0
850-
851-
def reset_retry_count(self):
852-
self.retried = 0
853849

854850
def http_error_auth_reqed(self, authreq, host, req, headers):
855851
# host may be an authority (without userinfo) or a URL with an
856852
# authority
857853
# XXX could be multiple headers
858854
authreq = headers.get(authreq, None)
859855

860-
if self.retried > 5:
861-
# retry sending the username:password 5 times before failing.
862-
raise HTTPError(req.get_full_url(), 401, "basic auth failed",
863-
headers, None)
864-
else:
865-
self.retried += 1
866-
867856
if authreq:
868857
scheme = authreq.split()[0]
869858
if scheme.lower() != 'basic':
@@ -878,17 +867,14 @@ def http_error_auth_reqed(self, authreq, host, req, headers):
878867
warnings.warn("Basic Auth Realm was unquoted",
879868
UserWarning, 2)
880869
if scheme.lower() == 'basic':
881-
response = self.retry_http_basic_auth(host, req, realm)
882-
if response and response.code != 401:
883-
self.retried = 0
884-
return response
870+
return self.retry_http_basic_auth(host, req, realm)
885871

886872
def retry_http_basic_auth(self, host, req, realm):
887873
user, pw = self.passwd.find_user_password(realm, host)
888874
if pw is not None:
889875
raw = "%s:%s" % (user, pw)
890876
auth = "Basic " + base64.b64encode(raw.encode()).decode("ascii")
891-
if req.headers.get(self.auth_header, None) == auth:
877+
if req.get_header(self.auth_header, None) == auth:
892878
return None
893879
req.add_unredirected_header(self.auth_header, auth)
894880
return self.parent.open(req, timeout=req.timeout)
@@ -904,7 +890,6 @@ def http_error_401(self, req, fp, code, msg, headers):
904890
url = req.full_url
905891
response = self.http_error_auth_reqed('www-authenticate',
906892
url, req, headers)
907-
self.reset_retry_count()
908893
return response
909894

910895

@@ -920,7 +905,6 @@ def http_error_407(self, req, fp, code, msg, headers):
920905
authority = req.host
921906
response = self.http_error_auth_reqed('proxy-authenticate',
922907
authority, req, headers)
923-
self.reset_retry_count()
924908
return response
925909

926910

Misc/NEWS

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)