[Python-checkins] bpo-43972: Set content-length to 0 for http.server.SimpleHTTPRequestHandler 301s (GH-25705)

miss-islington webhook-mailer at python.org
Thu May 6 15:51:03 EDT 2021


https://github.com/python/cpython/commit/b391b9b9255697ce6028bb4e7a99c18080aa991c
commit: b391b9b9255697ce6028bb4e7a99c18080aa991c
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-05-06T12:50:54-07:00
summary:

bpo-43972: Set content-length to 0 for http.server.SimpleHTTPRequestHandler 301s (GH-25705)


* Set content-length for simple http server 301s

When http.server.SimpleHTTPRequestHandler sends a 301 (Moved
Permanently) due to a missing file, it does not set a Content-Length
of 0. Unfortunately, certain clients can be left waiting for the
connection to be closed in this circumstance, even though no body
will be sent. At time of writing, both curl and Firefox demonstrate
this behavior.

* Test Content-Length on simple http server redirect

When serving a redirect, the SimpleHTTPRequestHandler will now send
`Content-Length: 0`. Several tests for http.server already cover
various behaviors and checks including redirection. This change only
adds one check for the expected Content-Length on the simplest case
for a redirect.

* Add news entry for SimpleHTTPRequestHandler fix

* Clarify the specific kind of 301

Co-authored-by: Senthil Kumaran <skumaran at gatech.edu>
(cherry picked from commit fb427255614fc1f740e7785554c1da8ca39116c2)

Co-authored-by: Stephen Rosen <sirosen at globus.org>

files:
A Misc/NEWS.d/next/Library/2021-04-30-16-58-24.bpo-43972.Y2r9lg.rst
M Lib/http/server.py
M Lib/test/test_httpservers.py

diff --git a/Lib/http/server.py b/Lib/http/server.py
index def05f46be4f91..d7cce20432ead4 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -689,6 +689,7 @@ def send_head(self):
                              parts[3], parts[4])
                 new_url = urllib.parse.urlunsplit(new_parts)
                 self.send_header("Location", new_url)
+                self.send_header("Content-Length", "0")
                 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 8df0b5251f1ae3..b33fedaafc5a00 100644
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -426,6 +426,7 @@ def test_get(self):
         self.check_status_and_reason(response, HTTPStatus.OK)
         response = self.request(self.base_url)
         self.check_status_and_reason(response, HTTPStatus.MOVED_PERMANENTLY)
+        self.assertEqual(response.getheader("Content-Length"), "0")
         response = self.request(self.base_url + '/?hi=2')
         self.check_status_and_reason(response, HTTPStatus.OK)
         response = self.request(self.base_url + '?hi=1')
diff --git a/Misc/NEWS.d/next/Library/2021-04-30-16-58-24.bpo-43972.Y2r9lg.rst b/Misc/NEWS.d/next/Library/2021-04-30-16-58-24.bpo-43972.Y2r9lg.rst
new file mode 100644
index 00000000000000..3d67b885bab105
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-04-30-16-58-24.bpo-43972.Y2r9lg.rst
@@ -0,0 +1,3 @@
+When :class:`http.server.SimpleHTTPRequestHandler` sends a
+``301 (Moved Permanently)`` for a directory path not ending with `/`, add a
+``Content-Length: 0`` header. This improves the behavior for certain clients.



More information about the Python-checkins mailing list