Index: Lib/http/server.py =================================================================== --- Lib/http/server.py (revision 86565) +++ Lib/http/server.py (working copy) @@ -661,12 +661,17 @@ """ path = self.translate_path(self.path) + # We want to use an untranslated path here, but stripped of + # query parameters and fragments + stripped_path = self.path.split('?', 1)[0] + stripped_path = stripped_path.split('#', 1)[0] + suffix = self.path[len(stripped_path):] f = None if os.path.isdir(path): - if not self.path.endswith('/'): + if not stripped_path.endswith('/'): # redirect browser - doing basically what apache does self.send_response(301) - self.send_header("Location", self.path + "/") + self.send_header("Location", stripped_path + "/" + suffix) self.end_headers() return None for index in "index.html", "index.htm": Index: Lib/test/test_httpservers.py =================================================================== --- Lib/test/test_httpservers.py (revision 86565) +++ Lib/test/test_httpservers.py (working copy) @@ -209,6 +209,8 @@ self.tempdir_name = os.path.basename(self.tempdir) with open(os.path.join(self.tempdir, 'test'), 'wb') as temp: temp.write(self.data) + with open(os.path.join(self.tempdir, 'test?foo'), 'wb') as temp: + temp.write(self.data) def tearDown(self): try: @@ -249,6 +251,23 @@ response = self.request(self.tempdir_name + '/') self.check_status_and_reason(response, 404) os.chmod(self.tempdir, 0o755) + response = self.request(self.tempdir_name + '?foo') + self.check_status_and_reason(response, 301) + self.assertEqual(response.getheader('Location'), self.tempdir_name + '/?foo') + response = self.request(self.tempdir_name + '/?foo') + self.check_status_and_reason(response, 200) + response = self.request(self.tempdir_name + '/?foo/') + self.check_status_and_reason(response, 200) + response = self.request(self.tempdir_name + '?foo/') + self.check_status_and_reason(response, 301) + self.assertEqual(response.getheader('Location'), self.tempdir_name + '/?foo/') + response = self.request(self.tempdir_name + '#foo') + self.check_status_and_reason(response, 301) + self.assertEqual(response.getheader('Location'), self.tempdir_name + '/#foo') + response = self.request(self.tempdir_name + '/test%3Ffoo') + self.check_status_and_reason(response, 200, data=self.data) + response = self.request(self.tempdir_name + '/test%3Ffoo?foo') + self.check_status_and_reason(response, 200, data=self.data) def test_head(self): response = self.request(