[New-bugs-announce] [issue43972] Simple HTTP Request Handler in http.server does not set a content-length and does not close connections on 301s

Stephen Rosen report at bugs.python.org
Wed Apr 28 21:06:37 EDT 2021


New submission from Stephen Rosen <sirosen at globus.org>:

If you use the `http.server` simple server and handler to serve a directory, navigating to a directory name without a trailing slash will trigger a 301 to add the trailing slash.

For example, if "foo/" is a directory under the file server, a GET for "/foo" will receive a 301 Moved Permanently response with a Location header pointing at "/foo/".

However, the response is sent without a "Content-Length: 0" and the connection is not closed. Unfortunately, certain clients will hang indefinitely and wait under these conditions, without processing the redirect. In my testing, curl 7.68 and Firefox 87 both exhibted this behavior.

If a Content-Length header is set, these clients behave correctly.
For example, subclass the handler and add

    def send_response(self, code):
        super().send_response(code)
        if code == HTTPStatus.MOVED_PERMANENTLY:
            self.send_header("Content-Length", "0")

----------
components: Library (Lib)
messages: 392272
nosy: sirosen
priority: normal
severity: normal
status: open
title: Simple HTTP Request Handler in http.server does not set a content-length and does not close connections on 301s
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43972>
_______________________________________


More information about the New-bugs-announce mailing list