Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce6e068

Browse files
committed
Issue #14132: Fix redirect handling when target is just a query string
1 parent f95455d commit ce6e068

4 files changed

Lines changed: 25 additions & 5 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Regresssion tests for urllib"""
1+
"""Regresssion tests for what was in Python 2's "urllib" module"""
22

33
import urllib.parse
44
import urllib.request
@@ -86,10 +86,11 @@ class FakeHTTPConnection(http.client.HTTPConnection):
8686

8787
# buffer to store data for verification in urlopen tests.
8888
buf = None
89-
fakesock = FakeSocket(fakedata)
9089

9190
def connect(self):
92-
self.sock = self.fakesock
91+
self.sock = FakeSocket(self.fakedata)
92+
type(self).fakesock = self.sock
93+
FakeHTTPConnection.fakedata = fakedata
9394

9495
return FakeHTTPConnection
9596

Lib/test/test_urllib2.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def reset(self):
462462
self.requests = []
463463

464464
def http_open(self, req):
465-
import email, http.client, copy
465+
import email, copy
466466
self.requests.append(copy.deepcopy(req))
467467
if self._count == 0:
468468
self._count = self._count + 1
@@ -1208,6 +1208,22 @@ def test_redirect_fragment(self):
12081208
fp = o.open('http://www.example.com')
12091209
self.assertEqual(fp.geturl(), redirected_url.strip())
12101210

1211+
def test_redirect_no_path(self):
1212+
# Issue 14132: Relative redirect strips original path
1213+
real_class = http.client.HTTPConnection
1214+
response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n"
1215+
http.client.HTTPConnection = test_urllib.fakehttp(response1)
1216+
self.addCleanup(setattr, http.client, "HTTPConnection", real_class)
1217+
urls = iter(("/path", "/path?query"))
1218+
def request(conn, method, url, *pos, **kw):
1219+
self.assertEqual(url, next(urls))
1220+
real_class.request(conn, method, url, *pos, **kw)
1221+
# Change response for subsequent connection
1222+
conn.__class__.fakedata = b"HTTP/1.1 200 OK\r\n\r\nHello!"
1223+
http.client.HTTPConnection.request = request
1224+
fp = urllib.request.urlopen("http://python.org/path")
1225+
self.assertEqual(fp.geturl(), "http://python.org/path?query")
1226+
12111227
def test_proxy(self):
12121228
o = OpenerDirector()
12131229
ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))

Lib/urllib/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def http_error_302(self, req, fp, code, msg, headers):
652652
"%s - Redirection to url '%s' is not allowed" % (msg, newurl),
653653
headers, fp)
654654

655-
if not urlparts.path:
655+
if not urlparts.path and urlparts.netloc:
656656
urlparts = list(urlparts)
657657
urlparts[2] = "/"
658658
newurl = urlunparse(urlparts)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ Core and Builtins
118118
Library
119119
-------
120120

121+
- Issue #14132: Fix urllib.request redirect handling when the target only has
122+
a query string. Original fix by Ján Janech.
123+
121124
- Issue #26892: Honor debuglevel flag in urllib.request.HTTPHandler. Patch
122125
contributed by Chi Hsuan Yen.
123126

0 commit comments

Comments
 (0)