Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Avoid closing connections when HEAD requests have a content length #428

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/waitress/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,18 @@ def start_response(status, headers, exc_info=None):

cl = self.content_length
if cl is not None:
if self.content_bytes_written != cl:
if self.content_bytes_written != cl and self.request.command != "HEAD":
# close the connection so the client isn't sitting around
# waiting for more data when there are too few bytes
# to service content-length
# unless it's a HEAD request in which case we don't expect
# to return any bytes regardless of the content length
self.close_on_finish = True
if self.request.command != "HEAD":
self.logger.warning(
"application returned too few bytes (%s) "
"for specified Content-Length (%s) via app_iter"
% (self.content_bytes_written, cl),
)
self.logger.warning(
"application returned too few bytes (%s) "
"for specified Content-Length (%s) via app_iter"
% (self.content_bytes_written, cl),
)
finally:
if can_close_app_iter and hasattr(app_iter, "close"):
app_iter.close()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def app(environ, start_response):
self.assertEqual(inst.close_on_finish, True)
self.assertEqual(len(inst.logger.logged), 1)

def test_execute_app_do_not_warn_on_head(self):
def test_execute_app_head_with_content_length(self):
def app(environ, start_response):
start_response("200 OK", [("Content-Length", "3")])
return [b""]
Expand All @@ -600,7 +600,7 @@ def app(environ, start_response):
inst.channel.server.application = app
inst.logger = DummyLogger()
inst.execute()
self.assertEqual(inst.close_on_finish, True)
self.assertEqual(inst.close_on_finish, False)
self.assertEqual(len(inst.logger.logged), 0)

def test_execute_app_without_body_204_logged(self):
Expand Down
Loading