diff options
author | Benjamin Peterson <benjamin@python.org> | 2014-12-26 16:53:43 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2014-12-26 16:53:43 (GMT) |
commit | 94cb7a24298ee0a4d6a3bb6b8ae80a7729e9f788 (patch) | |
tree | 763dc2b108050e4b7a29db10851260b14e39994b | |
parent | 8c4e112afcf6b2236c43273d007de5b61d4b6519 (diff) | |
download | cpython-94cb7a24298ee0a4d6a3bb6b8ae80a7729e9f788.zip cpython-94cb7a24298ee0a4d6a3bb6b8ae80a7729e9f788.tar.gz cpython-94cb7a24298ee0a4d6a3bb6b8ae80a7729e9f788.tar.bz2 |
fix behavior of trailing slash redirection when a query string is involved (closes #23112)
-rw-r--r-- | Lib/http/server.py | 8 | ||||
-rw-r--r-- | Lib/test/test_httpservers.py | 6 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
3 files changed, 15 insertions, 2 deletions
diff --git a/Lib/http/server.py b/Lib/http/server.py index f916fdd..cfa29f4 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -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": diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py index 97713cd..a31db5b 100644 --- a/Lib/test/test_httpservers.py +++ b/Lib/test/test_httpservers.py @@ -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' + '/') @@ -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. |