From 3079bbebac4e455bec8e3f2e983daf63503af196 Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Mon, 16 May 2016 01:07:13 +0000 Subject: Issue #14132: Fix redirect handling when target is just a query string --- Lib/test/test_urllib.py | 5 +++-- Lib/test/test_urllib2.py | 19 ++++++++++++++++++- Lib/urllib2.py | 2 +- Misc/NEWS | 3 +++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index c18e738..434d533 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -45,10 +45,11 @@ def fakehttp(fakedata): # 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 --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py index 30c3d2b..6d24d5d 100644 --- a/Lib/test/test_urllib2.py +++ b/Lib/test/test_urllib2.py @@ -8,6 +8,7 @@ import StringIO import urllib2 from urllib2 import Request, OpenerDirector, AbstractDigestAuthHandler +import httplib try: import ssl @@ -419,7 +420,7 @@ class MockHTTPHandler(urllib2.BaseHandler): 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 @@ class HandlerTests(unittest.TestCase): 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 --git a/Lib/urllib2.py b/Lib/urllib2.py index 93a3350..8b634ad 100644 --- a/Lib/urllib2.py +++ b/Lib/urllib2.py @@ -609,7 +609,7 @@ class HTTPRedirectHandler(BaseHandler): # 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 --git a/Misc/NEWS b/Misc/NEWS index b1a1d28..3425403 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -77,6 +77,9 @@ Core and Builtins 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. -- cgit v0.12