Skip to content

Strip body and Content-Length from 204/304 responses (RFC 9112)#3493

Open
daniel7an wants to merge 1 commit intobenoitc:masterfrom
daniel7an:fix/issue-3413-rfc9112-no-body
Open

Strip body and Content-Length from 204/304 responses (RFC 9112)#3493
daniel7an wants to merge 1 commit intobenoitc:masterfrom
daniel7an:fix/issue-3413-rfc9112-no-body

Conversation

@daniel7an
Copy link

Summary

Per RFC 9112 Section 6.3, responses with status codes 204 and 304 must not contain a message body, and Content-Length should be absent.

Currently, Gunicorn forwards 204/304 responses as-is from the WSGI app, including any body and Content-Length header. This can cause issues with downstream proxies (e.g., AWS ALB returning 502).

Changes

  • gunicorn/http/wsgi.py: Strip Content-Length header during process_headers() for 204/304 responses. Silently discard body data in write() for these status codes.
  • tests/test_http.py: Added tests for 204, 304 (Content-Length stripped, body discarded) and 200 (normal behavior preserved).

Behavior

Status Before After
204 Body + Content-Length sent as-is Body discarded, Content-Length stripped
304 Body + Content-Length sent as-is Body discarded, Content-Length stripped
200 Normal Normal (unchanged)

Other frameworks (Flask, FastAPI, Express, Spring) already enforce this behavior.

Fixes #3413

Per RFC 9112 Section 6.3, responses with status codes 204 and 304
must not contain a message body. This commit:

- Strips Content-Length header during response header processing for
  204 and 304 status codes
- Silently discards any body data written for these status codes
- Adds tests verifying the behavior for 204, 304, and normal 200

Fixes benoitc#3413
@benoitc benoitc self-assigned this Feb 11, 2026
if lname == "content-length":
# RFC 9112 6.3: 1xx, 204, and 304 responses must not
# contain a message body, so Content-Length is stripped.
if self.status_code in (204, 304):
Copy link
Contributor

@pajod pajod Feb 11, 2026

Choose a reason for hiding this comment

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

Excessive. Content-Length for 304 responses is perfectly reasonable.. it expresses the (potentially non-zero) size as usual, it is merely not to be used for determining message framing.

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.

204/304 responses should not send body or Content-Length (RFC 9112 compliance)

3 participants