Skip to content

wsgi: do not send a body for 1xx/204/304 responses - #3919

Open
BlueX888 wants to merge 4 commits into
tornadoweb:masterfrom
BlueX888:contrib/wsgi-no-content
Open

BlueX888 wants to merge 4 commits into
tornadoweb:masterfrom
BlueX888:contrib/wsgi-no-content

Conversation

@BlueX888

Copy link
Copy Markdown
Contributor

WSGIContainer only special-cased 304. A WSGI app returning 204 with a body or Content-Length, or 304 with a body, made Tornado write a body for a response that must not have one: 204 produced Response with code 204 should not have body and a malformed HTTP message; 304 raised HTTPOutputError: Tried to write more data than Content-Length.

Treat 1xx/204/304 as no-content:

  • 1xx and 204 drop Content-Length/Transfer-Encoding and send headers only.
  • 304 may keep Content-Length but sends no body.
  • HEAD and 200 behavior is unchanged.

Tests:

  • python3 -m tornado.test.runtests tornado.test.wsgi_test — 13 tests OK.
  • Full python3 -m tornado.test.runtests — 1702 tests OK (131 skipped, pre-existing environment skips).
  • New tests: 204 with a bogus Content-Length and body (asserts empty body, no container-synthesized Content-Length/Content-Type), 304 with a body (asserts empty body), 304 with Content-Length, and a 200 control.
  • Raw wire: 204 sends only Server, 304 may send Content-Length with no body, 200 is unchanged, and HEAD sends headers only. Keep-alive reuse after these responses serves the next request normally.
  • black and flake8 pass; mypy reports only pre-existing curl_httpclient.py/optional-module errors, none in the changed files.

@BlueX888
BlueX888 force-pushed the contrib/wsgi-no-content branch from ceb6aea to 03761c5 Compare November 24, 2026 16:11
@BlueX888

Copy link
Copy Markdown
Contributor Author

Rebased onto master (73823ed) after the response-validation change landed. The conflict was only the new try/except and response_written wrapper: the 1xx/204/304 logic is unchanged, and response_written is still set just before write_headers as on master. Full suite passes: python3 -m tornado.test (1723 tests, 131 skipped), and the new wsgi tests also fail as expected against master's wsgi.py. The PR shows as mergeable again.

@bdarnell

Copy link
Copy Markdown
Member

HEAD and 304 are special-cased because of their relationship with GET and 200: it makes sense to implement them by generating a regular response and throwing it away. (the special case for 304 was incomplete, so it's good to throw away the body here). But for 1xx and 204, I think it's appropriate that an error is raised if the wsgi application tries to send a body; we shouldn't silently throw it away.

Review feedback: HEAD and 304 discard a body because they describe a GET
or a cached 200. A body for 1xx or 204 is an application error, so it
should not be silently dropped. Keep discarding for HEAD and 304, and
raise ValueError for a non-empty body on 1xx/204; the container logs it
and sends the fallback 500.

The 204 header-stripping test now uses an empty body. New tests cover a
204 body and a 103 body through a raw container, because the WSGI
validator rejects those apps before the container sees them.
@BlueX888

Copy link
Copy Markdown
Contributor Author

Agreed — changed in cd217d8d: a non-empty body for 1xx or 204 now raises ValueError (the container logs it and sends the fallback 500), while HEAD and 304 still discard a body. The 204 header-stripping test now uses an empty body; new tests cover a 204 body and a 103 body through the container directly, since wsgiref.validate rejects those apps before the container sees them. Full suite: 1725 tests OK (131 skipped); black and flake8 clean.

The validator only rejects the 103 app, and only because a non-204/304
response needs a Content-Type; it accepts a 204 with a body. Keep the 103
case on the raw container and move the 204 body case back through the
validator wrapper, so the comment and the test setup agree.
Comment thread tornado/wsgi.py Outdated
if status_code != 304:
# HEAD and 304 discard a body because they describe a GET or a
# cached 200. A body for 1xx or 204 is an application error, so it
# is not silently dropped.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, a non-zero content-length should also be an error instead of being silently dropped. I think a non-empty transfer-encoding should also be an error (it's never legal to send transfer-encoding: chunked with an empty body so it would never make sense here), but I'm less sure of that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8bce2372: a non-zero Content-Length or a non-empty Transfer-Encoding on 1xx/204 now raises ValueError (fallback 500); Content-Length: 0 is still dropped, and HEAD/304 semantics are unchanged. New tests cover a 204 with a non-zero Content-Length and one with Transfer-Encoding: chunked (raw container), and both fail on the previous head. If you would rather keep the Transfer-Encoding case as a strip, I can drop that branch.

Review feedback: the header rules are the same kind of application error
as a body. A non-zero Content-Length or a non-empty Transfer-Encoding on a
1xx or 204 response now raises ValueError instead of being stripped.
Content-Length: 0 is still dropped, and HEAD/304 semantics are unchanged.

Tests cover a 204 with a non-zero Content-Length and a 204 with
Transfer-Encoding: chunked through the raw container; both fail on the
previous head.

This branch has not been deployed

No deployments
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.

2 participants