]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #14132: Fix redirect handling when target is just a query string
authorMartin Panter <vadmium+py@gmail.com>
Mon, 16 May 2016 01:07:13 +0000 (01:07 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 16 May 2016 01:07:13 +0000 (01:07 +0000)
Lib/test/test_urllib.py
Lib/test/test_urllib2.py
Lib/urllib/request.py
Misc/NEWS

index 49e2a2cd6198dd4513fd7dae70d7c8adc478356b..5d05f8d7d26d66041be7c39070adb165939b476b 100644 (file)
@@ -1,4 +1,4 @@
-"""Regresssion tests for urllib"""
+"""Regresssion tests for what was in Python 2's "urllib" module"""
 
 import urllib.parse
 import urllib.request
@@ -86,10 +86,11 @@ def fakehttp(fakedata):
 
         # buffer to store data for verification in urlopen tests.
         buf = None
-        fakesock = FakeSocket(fakedata)
 
         def connect(self):
-            self.sock = self.fakesock
+            self.sock = FakeSocket(self.fakedata)
+            type(self).fakesock = self.sock
+    FakeHTTPConnection.fakedata = fakedata
 
     return FakeHTTPConnection
 
index b5bba55a97711dea7126a8ae6bc16d5bfbb19e82..58c30712877ff65b543538eaf3f584284808672f 100644 (file)
@@ -462,7 +462,7 @@ class MockHTTPHandler(urllib.request.BaseHandler):
         self.requests = []
 
     def http_open(self, req):
-        import email, http.client, copy
+        import email, copy
         self.requests.append(copy.deepcopy(req))
         if self._count == 0:
             self._count = self._count + 1
@@ -1208,6 +1208,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 = http.client.HTTPConnection
+        response1 = b"HTTP/1.1 302 Found\r\nLocation: ?query\r\n\r\n"
+        http.client.HTTPConnection = test_urllib.fakehttp(response1)
+        self.addCleanup(setattr, http.client, "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!"
+        http.client.HTTPConnection.request = request
+        fp = urllib.request.urlopen("http://python.org/path")
+        self.assertEqual(fp.geturl(), "http://python.org/path?query")
+
     def test_proxy(self):
         o = OpenerDirector()
         ph = urllib.request.ProxyHandler(dict(http="proxy.example.com:3128"))
index 5f40729fca5debae62349f62ca55c6eda9a055fd..bbd2bdf685cb305518b596e758c6621baae6332f 100644 (file)
@@ -652,7 +652,7 @@ class HTTPRedirectHandler(BaseHandler):
                 "%s - Redirection to url '%s' is not allowed" % (msg, newurl),
                 headers, fp)
 
-        if not urlparts.path:
+        if not urlparts.path and urlparts.netloc:
             urlparts = list(urlparts)
             urlparts[2] = "/"
         newurl = urlunparse(urlparts)
index 73c32e911ce24b69b070f838d8e708ceb8785ea5..9a147c407c53d8638cc813f56a0914c8a05033ee 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -118,6 +118,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14132: Fix urllib.request redirect handling when the target only has
+  a query string.  Original fix by Ján Janech.
+
 - Issue #26892: Honor debuglevel flag in urllib.request.HTTPHandler. Patch
   contributed by Chi Hsuan Yen.