]> granicus.if.org Git - python/commitdiff
fix behavior of trailing slash redirection when a query string is involved (closes...
authorBenjamin Peterson <benjamin@python.org>
Fri, 26 Dec 2014 16:53:43 +0000 (10:53 -0600)
committerBenjamin Peterson <benjamin@python.org>
Fri, 26 Dec 2014 16:53:43 +0000 (10:53 -0600)
Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS

index f916fdd95cbb71011be1b4f7b717295b5649821b..cfa29f44d351cfd3ac6e8ea05d18b6d43c148e2b 100644 (file)
@@ -701,10 +701,14 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
         path = self.translate_path(self.path)
         f = None
         if os.path.isdir(path):
-            if not self.path.endswith('/'):
+            parts = urllib.parse.urlsplit(self.path)
+            if not parts.path.endswith('/'):
                 # redirect browser - doing basically what apache does
                 self.send_response(301)
-                self.send_header("Location", self.path + "/")
+                new_parts = (parts[0], parts[1], parts[2] + '/',
+                             parts[3], parts[4])
+                new_url = urllib.parse.urlunsplit(new_parts)
+                self.send_header("Location", new_url)
                 self.end_headers()
                 return None
             for index in "index.html", "index.htm":
index 97713cd0431cb9a07bf8ae58f484fa52ea40f88d..a31db5b57b8eb8ef5aea4b3907b696caf437b1dc 100644 (file)
@@ -305,6 +305,12 @@ class SimpleHTTPServerTestCase(BaseTestCase):
         self.check_status_and_reason(response, 200)
         response = self.request(self.tempdir_name)
         self.check_status_and_reason(response, 301)
+        response = self.request(self.tempdir_name + '/?hi=2')
+        self.check_status_and_reason(response, 200)
+        response = self.request(self.tempdir_name + '?hi=1')
+        self.check_status_and_reason(response, 301)
+        self.assertEqual(response.getheader("Location"),
+                         self.tempdir_name + "/?hi=1")
         response = self.request('/ThisDoesNotExist')
         self.check_status_and_reason(response, 404)
         response = self.request('/' + 'ThisDoesNotExist' + '/')
index 958f28579dc14cffee5201d18e2f2743f9661540..82cdbfd5a7225d94291414bf02efceb37a55c969 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -41,6 +41,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #23112: Fix SimpleHTTPServer to correctly carry the query string and
+  fragment when it redirects to add a trailing slash.
+
 - Issue #23093: In the io, module allow more operations to work on detached
   streams.