Skip to content

Commit fb8cc2f

Browse files
committed
Fix for issue5102, timeout value propages between redirects, proxy, digest and
auth handlers. Fixed tests to reflect the same.
1 parent 046f723 commit fb8cc2f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Lib/test/test_urllib2.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ def test_password_manager_default_port(self):
222222

223223
class MockOpener:
224224
addheaders = []
225-
def open(self, req, data=None):
226-
self.req, self.data = req, data
225+
def open(self, req, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
226+
self.req, self.data, self.timeout = req, data, timeout
227227
def error(self, proto, *args):
228228
self.proto, self.args = proto, args
229229

@@ -854,6 +854,7 @@ def test_redirect(self):
854854
for data in None, "blah\nblah\n":
855855
method = getattr(h, "http_error_%s" % code)
856856
req = Request(from_url, data)
857+
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
857858
req.add_header("Nonsense", "viking=withhold")
858859
if data is not None:
859860
req.add_header("Content-Length", str(len(data)))
@@ -883,6 +884,7 @@ def test_redirect(self):
883884

884885
# loop detection
885886
req = Request(from_url)
887+
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
886888
def redirect(h, req, url=to_url):
887889
h.http_error_302(req, MockFile(), 302, "Blah",
888890
MockHeaders({"location": url}))
@@ -892,6 +894,7 @@ def redirect(h, req, url=to_url):
892894
# detect infinite loop redirect of a URL to itself
893895
req = Request(from_url, origin_req_host="example.com")
894896
count = 0
897+
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
895898
try:
896899
while 1:
897900
redirect(h, req, "http://example.com/")
@@ -903,6 +906,7 @@ def redirect(h, req, url=to_url):
903906
# detect endless non-repeating chain of redirects
904907
req = Request(from_url, origin_req_host="example.com")
905908
count = 0
909+
req.timeout = socket._GLOBAL_DEFAULT_TIMEOUT
906910
try:
907911
while 1:
908912
redirect(h, req, "http://example.com/%d" % count)

Lib/urllib/request.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ def http_error_302(self, req, fp, code, msg, headers):
552552
fp.read()
553553
fp.close()
554554

555-
return self.parent.open(new)
555+
return self.parent.open(new, timeout=req.timeout)
556556

557557
http_error_301 = http_error_303 = http_error_307 = http_error_302
558558

@@ -669,7 +669,7 @@ def proxy_open(self, req, proxy, type):
669669
# {'http': 'ftp://proxy.example.com'}, we may end up turning
670670
# a request for http://acme.example.com/a into one for
671671
# ftp://proxy.example.com/a
672-
return self.parent.open(req)
672+
return self.parent.open(req, timeout=req.timeout)
673673

674674
class HTTPPasswordMgr:
675675

@@ -785,7 +785,7 @@ def retry_http_basic_auth(self, host, req, realm):
785785
if req.headers.get(self.auth_header, None) == auth:
786786
return None
787787
req.add_header(self.auth_header, auth)
788-
return self.parent.open(req)
788+
return self.parent.open(req, timeout=req.timeout)
789789
else:
790790
return None
791791

@@ -866,7 +866,7 @@ def retry_http_digest_auth(self, req, auth):
866866
if req.headers.get(self.auth_header, None) == auth_val:
867867
return None
868868
req.add_unredirected_header(self.auth_header, auth_val)
869-
resp = self.parent.open(req)
869+
resp = self.parent.open(req, timeout=req.timeout)
870870
return resp
871871

872872
def get_cnonce(self, nonce):

0 commit comments

Comments
 (0)