use chunked encoding for streaming responses with HTTP/1.1#2091
Conversation
|
I'm not sure if there are any docs that needs to change as an effect of this. If so, I need a pointer to the right place. |
9b059f7 to
2f252e2
Compare
|
While playing around with this and making HTTP/1.1 the default, I've noticed that the debugger page can be inconsistent about loading all the resources when in HTTP/1.1 keep-alive mode, every 3 reloads or so it gets stuck loading the font file. As soon as I go back to HTTP/1.0 mode, where every connection is always closed after a single request, everything loads. Or if I enable the threaded server, everything loads. This did lead me to notice that the debugger could use In my own projects I've been leaning towards using Gunicorn or another WSGI server in development since it just generally supports more features than the Werkzeug dev server ever will. You can even enable Werkzeug's debugger, here's an example with Flask: from flask import Flask
from werkzeug.debug import DebuggedApplication
app = Flask(__name__)
if app.env == "development":
app.wsgi_app = DebuggedApplication(app, evalex=True) |
|
OK, after a bit more experimenting, I think it's safe to enable HTTP/1.1 if |
For HTTP/1.0, the only way to stream content is to rely on the closing of connection to convey end of response. This can typically lead to silently truncated content in HTTP client implementations. For HTTP/1.1, the better way is to use `Transfer-Encoding: chunked` which has a builtin mechanism to always terminate response with a 0-sized chunk, marking end of stream.
2f252e2 to
50509cf
Compare
|
I changed if self.server.passthrough_errors or (status_sent and chunk_response):
raiseto if self.server.passthrough_errors:
raise
if status_sent is not None and chunk_response:
self.close_connection = TrueSo that the dev server's traceback logging is used instead of raising the error up to the base server. |
This approach differs a bit from the approach in PR#1328, in that it is completely contained in the server without any change in the application. (As per RFC, no reliance on hop-by-hop headers). It is triggered if HTTP/1.1 is configured (such as by specifying a subclass of request_handler), at which point
Connection: closebehavior is always replaced withTransfer-Encoding: chunked. The rationale is that any spec-compliant HTTP client must support this, so it should not cause any unjustifiable regressions for clients. From what I've seen, this seems to also match behavior in other WSGI-servers to ensure the apps behave consistently across test/prod.Checklist:
CHANGES.rstsummarizing the change and linking to the issue... versionchanged::entries in any relevant code docs.pre-commithooks and fix any issues.pytestandtox, no tests failed.