New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-43972: Set content-length to 0 for http.server.SimpleHTTPRequestHandler 301s #25705
Conversation
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.
| @@ -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") | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a test case for this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a single check to one of the existing http server tests.
If something else is warranted in this case, I'm more than happy to do it, but I'd need some direction.
The added check seems sufficient to me and in-keeping with the way those existing tests are written.
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.
|
I just added a news file using |
|
@sirosen - Thank you. You are right, this should definitely be documented. I was doing more research on the expected behavior in MOVED_PERMANENTLY situation. What does RFC / WHATWG standards recommend, or apache does so that I could confidently accept the patch. The change (with tests + news) does look good to me. |
|
The specs for HTTP do not require a Going off of HTTP 1.1 RFC 7230, 3.3 states
Nothing 100% unambiguously states that you MUST set
and by RFC7230, 3.3.2:
The spec doesn't require that a client does anything with a payload, but RFC 7231, 6.4.2 suggests that one should be expected
I think it therefore makes sense for a client which reads the headers for a 301 without Content-Length to either
In terms of what webservers do, I have an nginx box which returns an HTML snippet: 301 Moved Permanently |
When
http.server.SimpleHTTPRequestHandlersends a 301 (Moved Permanently) due to a missing file, add a Content-Length of 0. This improves the behavior for certain clients.https://bugs.python.org/issue43972
The text was updated successfully, but these errors were encountered: