Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94cb7a2

Browse files
committed
fix behavior of trailing slash redirection when a query string is involved (closes #23112)
1 parent 8c4e112 commit 94cb7a2

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/http/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,14 @@ def send_head(self):
701701
path = self.translate_path(self.path)
702702
f = None
703703
if os.path.isdir(path):
704-
if not self.path.endswith('/'):
704+
parts = urllib.parse.urlsplit(self.path)
705+
if not parts.path.endswith('/'):
705706
# redirect browser - doing basically what apache does
706707
self.send_response(301)
707-
self.send_header("Location", self.path + "/")
708+
new_parts = (parts[0], parts[1], parts[2] + '/',
709+
parts[3], parts[4])
710+
new_url = urllib.parse.urlunsplit(new_parts)
711+
self.send_header("Location", new_url)
708712
self.end_headers()
709713
return None
710714
for index in "index.html", "index.htm":

Lib/test/test_httpservers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ def test_get(self):
305305
self.check_status_and_reason(response, 200)
306306
response = self.request(self.tempdir_name)
307307
self.check_status_and_reason(response, 301)
308+
response = self.request(self.tempdir_name + '/?hi=2')
309+
self.check_status_and_reason(response, 200)
310+
response = self.request(self.tempdir_name + '?hi=1')
311+
self.check_status_and_reason(response, 301)
312+
self.assertEqual(response.getheader("Location"),
313+
self.tempdir_name + "/?hi=1")
308314
response = self.request('/ThisDoesNotExist')
309315
self.check_status_and_reason(response, 404)
310316
response = self.request('/' + 'ThisDoesNotExist' + '/')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Core and Builtins
4141
Library
4242
-------
4343

44+
- Issue #23112: Fix SimpleHTTPServer to correctly carry the query string and
45+
fragment when it redirects to add a trailing slash.
46+
4447
- Issue #23093: In the io, module allow more operations to work on detached
4548
streams.
4649

0 commit comments

Comments
 (0)