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: with a Content-Length of 178. I don't have an Apache server handy right now, but I'm pretty sure it exhibits similar behavior, serving an HTML payload with an appropriate content-length. Not including a payload is fine, per the spec. However, it forces a client to close the connection. A client which treats a 301 with no |
|
This piece of code was written in 2006 ( 4281902) and Content-Length was not added. This has survived for so long.
I cannot find anything for 2, and slightly in favor of this change. The history does not suggest that there is a bug here for us to fix. |
|
My analysis is coming to the same as yours. Nothing forbids addition of this change, so +1. But I didn't see a problem with the existing code with the clients I used curl 7.65 |
|
The Azure pipelines failure is unrelated. I see that happened across few jobs from yesterday. The issue not present in the PR's today, I am taking a call to merge this. The backport PRs shouldn't have a problem too. |
|
Thanks @sirosen for the PR, and @orsenthil for merging it |
|
Thanks @sirosen for the PR, and @orsenthil for merging it |
…andler 301s (pythonGH-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@gatech.edu> (cherry picked from commit fb42725) Co-authored-by: Stephen Rosen <sirosen@globus.org>
|
GH-25952 is a backport of this pull request to the 3.9 branch. |
…andler 301s (pythonGH-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@gatech.edu> (cherry picked from commit fb42725) Co-authored-by: Stephen Rosen <sirosen@globus.org>
|
GH-25953 is a backport of this pull request to the 3.10 branch. |
…andler 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@gatech.edu> (cherry picked from commit fb42725) Co-authored-by: Stephen Rosen <sirosen@globus.org>
…andler 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@gatech.edu> (cherry picked from commit fb42725) Co-authored-by: Stephen Rosen <sirosen@globus.org>
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: