Skip to content

wsgi: Discard the response body for HEAD requests - #3830

Merged
bdarnell merged 1 commit into
tornadoweb:masterfrom
b84330808:wsgi-head-body
Nov 6, 2026
Merged

bdarnell merged 1 commit into
tornadoweb:masterfrom
b84330808:wsgi-head-body

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Fixes #2190.

WSGIContainer passes whatever body the WSGI app returned straight to write_headers(). For a HEAD request HTTP1Connection expects no content, so an app that returns a body for HEAD and leaves it to the server to drop it (Django in the original report, or even the simple_app example from the docs) fails with HTTPOutputError: Tried to write more data than Content-Length, and the client just sees the stream close.

Following the suggestion in the issue, this accepts and discards the body for HEAD, the same way RequestHandler.flush() already does. The body is dropped after Content-Length has been filled in, so the HEAD response still carries the length a GET would.

I added test_head to wsgi_test.py; it fails on master and passes with this change. I also checked by hand that HEAD and GET requests alternate correctly on a single keep-alive connection.

I used Claude Code to write this change.

WSGIContainer passed whatever body the application returned to
write_headers, but HTTP1Connection expects no content in a HEAD
response. Any WSGI app that returns a body for HEAD and leaves it to
the server to drop it (the issue was reported with Django) failed with
"Tried to write more data than Content-Length", and the client saw a
closed stream.

Drop the body for HEAD the same way RequestHandler does, after the
Content-Length has been computed from it.

Fixes tornadoweb#2190

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bdarnell

bdarnell commented Nov 6, 2026

Copy link
Copy Markdown
Member

Thanks!

@bdarnell
bdarnell merged commit d64098f into tornadoweb:master Nov 6, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wsgi: HEAD raises "Tried to write more data than Content-Length"

2 participants