changeset: 101370:789a3f87bde1 branch: 2.7 user: Martin Panter date: Mon May 16 01:07:13 2016 +0000 files: Lib/test/test_urllib.py Lib/test/test_urllib2.py Lib/urllib2.py Misc/NEWS description: Issue #14132: Fix redirect handling when target is just a query string diff -r 19e4e0b7f1bd -r 789a3f87bde1 Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Sat Sep 06 21:41:39 2014 +0300 +++ b/Lib/test/test_urllib.py Mon May 16 01:07:13 2016 +0000 @@ -45,10 +45,11 @@ # buffer to store data for verification in urlopen tests. buf = "" - fakesock = FakeSocket(fakedata) def connect(self): - self.sock = self.fakesock + self.sock = FakeSocket(self.fakedata) + self.__class__.fakesock = self.sock + FakeHTTPConnection.fakedata = fakedata return FakeHTTPConnection diff -r 19e4e0b7f1bd -r 789a3f87bde1 Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Sat Sep 06 21:41:39 2014 +0300 +++ b/Lib/test/test_urllib2.py Mon May 16 01:07:13 2016 +0000 @@ -8,6 +8,7 @@ import urllib2 from urllib2 import Request, OpenerDirector, AbstractDigestAuthHandler +import httplib try: import ssl @@ -419,7 +420,7 @@ self._count = 0 self.requests = [] def http_open(self, req): - import mimetools, httplib, copy + import mimetools, copy from StringIO import StringIO self.requests.append(copy.deepcopy(req)) if self._count == 0: @@ -1037,6 +1038,22 @@ fp = o.open('http://www.example.com') self.assertEqual(fp.geturl(), redirected_url.strip()) + def test_redirect_no_path(self): + # Issue 14132: Relative redirect strips original path + real_class = httplib.HTTPConnection + response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n" + httplib.HTTPConnection = test_urllib.fakehttp(response1) + self.addCleanup(setattr, httplib, "HTTPConnection", real_class) + urls = iter(("/path", "/path?query")) + def request(conn, method, url, *pos, **kw): + self.assertEqual(url, next(urls)) + real_class.request(conn, method, url, *pos, **kw) + # Change response for subsequent connection + conn.__class__.fakedata = b"HTTP/1.1 200 OK\r\n\r\nHello!" + httplib.HTTPConnection.request = request + fp = urllib2.urlopen("http://python.org/path") + self.assertEqual(fp.geturl(), "http://python.org/path?query") + def test_proxy(self): o = OpenerDirector() ph = urllib2.ProxyHandler(dict(http="proxy.example.com:3128")) diff -r 19e4e0b7f1bd -r 789a3f87bde1 Lib/urllib2.py --- a/Lib/urllib2.py Sat Sep 06 21:41:39 2014 +0300 +++ b/Lib/urllib2.py Mon May 16 01:07:13 2016 +0000 @@ -609,7 +609,7 @@ # fix a possible malformed URL urlparts = urlparse.urlparse(newurl) - if not urlparts.path: + if not urlparts.path and urlparts.netloc: urlparts = list(urlparts) urlparts[2] = "/" newurl = urlparse.urlunparse(urlparts) diff -r 19e4e0b7f1bd -r 789a3f87bde1 Misc/NEWS --- a/Misc/NEWS Sat Sep 06 21:41:39 2014 +0300 +++ b/Misc/NEWS Mon May 16 01:07:13 2016 +0000 @@ -77,6 +77,9 @@ Library ------- +- Issue #14132: Fix urllib.request redirect handling when the target only has + a query string. Fix by Ján Janech. + - Removed the requirements for the ctypes and modulefinder modules to be compatible with earlier Python versions.