changeset: 93966:b058a904c630 branch: 2.7 parent: 93957:55462210ba2d user: Benjamin Peterson date: Fri Dec 26 10:53:43 2014 -0600 files: Lib/SimpleHTTPServer.py Lib/test/test_httpservers.py Misc/NEWS description: fix behavior of trailing slash redirection when a query string is involved (closes #23112) diff -r 55462210ba2d -r b058a904c630 Lib/SimpleHTTPServer.py --- a/Lib/SimpleHTTPServer.py Wed Dec 24 13:58:05 2014 -0600 +++ b/Lib/SimpleHTTPServer.py Fri Dec 26 10:53:43 2014 -0600 @@ -14,6 +14,7 @@ import posixpath import BaseHTTPServer import urllib +import urlparse import cgi import sys import shutil @@ -68,10 +69,14 @@ path = self.translate_path(self.path) f = None if os.path.isdir(path): - if not self.path.endswith('/'): + parts = urlparse.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 = urlparse.urlunsplit(new_parts) + self.send_header("Location", new_url) self.end_headers() return None for index in "index.html", "index.htm": diff -r 55462210ba2d -r b058a904c630 Lib/test/test_httpservers.py --- a/Lib/test/test_httpservers.py Wed Dec 24 13:58:05 2014 -0600 +++ b/Lib/test/test_httpservers.py Fri Dec 26 10:53:43 2014 -0600 @@ -321,6 +321,12 @@ 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' + '/') diff -r 55462210ba2d -r b058a904c630 Misc/NEWS --- a/Misc/NEWS Wed Dec 24 13:58:05 2014 -0600 +++ b/Misc/NEWS Fri Dec 26 10:53:43 2014 -0600 @@ -15,6 +15,9 @@ 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.