Skip to content

Commit

Permalink
Merge pull request #2399 from pallets/no-keep-alive
Browse files Browse the repository at this point in the history
disable keep-alive connections in dev server
  • Loading branch information
davidism authored Apr 25, 2022
2 parents 19323ef + 600a2b9 commit 53ba5f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Unreleased
``<!doctype html>`` and ``<html lang=en>``. :issue:`2390`
- Fix ability to set some ``cache_control`` attributes to ``False``.
:issue:`2379`
- Disable ``keep-alive`` connections in the development server, which
are not supported sufficiently by Python's ``http.server``.
:issue:`2397`


Version 2.1.1
Expand Down
27 changes: 16 additions & 11 deletions src/werkzeug/serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,23 @@ def write(data: bytes) -> None:
# is the more conservative behavior and matches other
# parts of the code.
# https://httpwg.org/specs/rfc7230.html#rfc.section.3.3.1
if not (
"content-length" in header_keys
or environ["REQUEST_METHOD"] == "HEAD"
or (100 <= code < 200)
or code in {204, 304}
if (
not (
"content-length" in header_keys
or environ["REQUEST_METHOD"] == "HEAD"
or (100 <= code < 200)
or code in {204, 304}
)
and self.protocol_version >= "HTTP/1.1"
):
if self.protocol_version >= "HTTP/1.1":
chunk_response = True
self.send_header("Transfer-Encoding", "chunked")
else:
self.send_header("Connection", "close")

chunk_response = True
self.send_header("Transfer-Encoding", "chunked")

# Always close the connection. This disables HTTP/1.1
# keep-alive connections. They aren't handled well by
# Python's http.server because it doesn't know how to
# drain the stream before the next request line.
self.send_header("Connection", "close")
self.end_headers()

assert isinstance(data, bytes), "applications must write bytes"
Expand Down

0 comments on commit 53ba5f8

Please sign in to comment.