[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
Sat May 1 21:13:20 EDT 2021


Stephen Rosen <sirosen at globus.org> added the comment:

Ach! Sorry! I didn't even realize this but the issue only arises when you are modifying the handler to set the protocol to HTTP/1.1 .

In HTTP/1.0 , there's no notion of persistent connections, so the issue does not arise.

But when the protocol version changes to 1.1 , persistent connections are the norm, and curl will wait indefinitely.

The following short script is sufficient to reproduce:
```
import http.server


class CustomRequestHandler(http.server.SimpleHTTPRequestHandler):
    protocol_version = "HTTP/1.1"


with http.server.HTTPServer(("", 8000), CustomRequestHandler) as httpd:
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\nKeyboard interrupt received, exiting.")
```

After double-checking the docs, the current doc for `protocol_version` [1] is quite clear about this:
"your server must then include an accurate Content-Length header (using send_header()) in all of its responses to clients"

I still think the fix I proposed is an improvement. Setting a Content-Length isn't forbidden in HTTP/1.0 , and it guarantees good behavior when HTTP/1.1 is used.

[1] https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler.protocol_version

----------

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


More information about the Python-bugs-list mailing list