]> 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/urllib2.py
Misc/NEWS

index c18e738f8987371e88baf72b7c01825a64fc4926..434d533e0561494e35f6e0d2697115b98a3e4925 100644 (file)
@@ -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
 
index 30c3d2b46422d0771232ce6da51e12c8746a2c21..6d24d5ddf83c611db21dfc0eebe2afe927d86bc9 100644 (file)
@@ -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"))
index 93a3350a867b4fd642beb1ea7df9f02228c85638..8b634ada3723b6094fbc6cc1e4178084fbde9582 100644 (file)
@@ -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)
index b1a1d28ce539a6e65b64d388eaed94828d7dc383..342540336937d1b0cfe01acb2614e709d6051791 100644 (file)
--- 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.