Skip to content

fix(mux-h2): add no Transfer-Encoding to a response without a body - #1778

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-to-h1-bodiless-no-transfer-encoding
Oct 2, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/h2-to-h1-bodiless-no-transfer-encoding

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

When an H2 backend sent a header section without END_STREAM and without content-length, pkawa::handle_header added Transfer-Encoding: chunked and framed the response chunked for an H1 client, including for a 1xx, a 204, a 304 and a response to HEAD. These have no content by definition (RFC 9110 §6.4.1), and RFC 9112 §6.1 forbids the field in a 1xx or 204. A 1xx never completed either, so an H1 client read a chunked 103 and never the final response.

Such a response now gains no framing:

  • A 1xx is complete at its head, like kawa's H1 parser does it, and the final response follows on the same stream.
  • A 204, a 304 or a response to HEAD stays open until the backend ends its stream with an empty DATA frame or a trailer HEADERS frame (RFC 9113 §5.1, §8.1). That END_STREAM ends the response for every frontend: an H1 client reads the head alone, and an H2 client receives END_STREAM. Marked complete at its head, as a response to HEAD already was on main, the H2 client stream was retired without END_STREAM, and the backend stream was reset while its trailer HEADERS was in flight. The closed-stream check then answered with GOAWAY(STREAM_CLOSED) for the whole backend connection. Keeping the stream open until END_STREAM needs no reset and no memory of reset streams. One trade-off: a backend that never ends such a stream is bounded by the stream timeout, as for any other response.
  • A content-length is removed from a 1xx or a 204, where a server MUST NOT send it, and kept on a 304 or a response to HEAD (RFC 9110 §8.6).
  • A :status 101, which HTTP/2 does not support (RFC 9113 §8.6), is now a stream error (PROTOCOL_ERROR) answered 502. It used to reach the H1 upgrade path, which closed the session.

DATA carrying a payload on a 204, a 304 or a response to HEAD is still forwarded. That is #1772, handled by #1775.

Tests:

  • a_bodiless_h2_response_gains_no_transfer_encoding_towards_an_h1_client (h1.rs): head bytes and completion for 103, 204, 304 and HEAD, with and without content-length, plus a GET 200 control.
  • a_101_response_is_a_stream_protocol_error (pkawa.rs).
  • e2e in e2e/src/tests/h2_security_header_injection.rs:
    • test_h2_bodiless_response_head_has_no_transfer_encoding
    • test_h2_bodiless_response_ends_the_h2_client_stream: 204, 304 and HEAD, each ended by trailers or by an empty DATA frame.
    • test_h2_bodiless_response_end_keeps_the_backend_connection: a second stream on the same backend connection, no GOAWAY or RST_STREAM towards the backend.
    • test_h2_backend_interim_response_reaches_the_client: 103 then 200, to an H1 and an H2 client.
    • test_h2_backend_101_is_a_bad_gateway

Documented in lib/src/protocol/mux/LIFECYCLE.md §8.4 and CHANGELOG.md.

Fixes #1776

When an H2 backend sent a header section without END_STREAM and
without content-length, pkawa::handle_header added Transfer-Encoding:
chunked and framed the response chunked for an H1 client, including for
a 1xx, a 204, a 304 and a response to HEAD, which have no content by
definition (RFC 9110 section 6.4.1). RFC 9112 section 6.1 forbids the
field in a 1xx or 204, and a 1xx never completed, so an H1 client read
a chunked 103 and never the final response.

Such a response now gains no framing. A 1xx is complete at its head and
the final response follows on the same stream. A 204, a 304 or a
response to HEAD stays open until the backend ends its stream (RFC 9113
sections 5.1, 8.1), and that END_STREAM reaches an H2 client. Marked
complete at its head, as a response to HEAD already was, the client
stream was retired without END_STREAM and the backend stream reset with
its trailer HEADERS in flight, which the closed-stream check answered
with GOAWAY(STREAM_CLOSED) for the whole backend connection.

A content-length is removed from a 1xx or a 204, where a server MUST
NOT send it, and kept on a 304 or a response to HEAD (RFC 9110 section
8.6). A :status 101, which HTTP/2 does not support (RFC 9113 section
8.6), is a stream error (PROTOCOL_ERROR) answered 502.

Closes #1776

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h2-to-h1-bodiless-no-transfer-encoding branch from 75c899c to cafeec5 Compare October 2, 2026 02:07
@FlorentinDUBOIS
FlorentinDUBOIS merged commit df7bade into main Oct 2, 2026
28 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
…HEAD response's DATA (#1775)

A response to HEAD, a 204 or a 304 has no content (RFC 9110 §6.4.1), but `ConnectionH2::handle_data_frame` forwarded a DATA payload on it. An H1 client read it after the head as the start of the next response, and an H2 client received it as DATA.

- 204 and 304 cannot carry content (RFC 9110 §15.3.5, §15.4.5; RFC 9113 §8.1.1). The backend stream is reset with PROTOCOL_ERROR before the payload is queued, counted in the new `h2.bodiless_response_data_reset`. An H1 client that was already sent the head sees the connection close; one that was not gets a 502.
- A response to HEAD only SHOULD NOT carry content (RFC 9110 §9.3.2). Its windows are credited, and its payload is never queued and is dropped from the end of the stream buffer. If the payload were kept in the buffer, it would only be freed when the frontend consumes a queued block, and nothing refers to it. A payload larger than the buffer would then stop the backend connection from being read before END_STREAM, stalling every stream on that connection.

With #1778, such a backend stream stays linked until its END_STREAM, so its DATA reaches these branches whether it arrives with the head or later.

Known limitation: after the PROTOCOL_ERROR reset of a 204 or a 304, a trailer HEADERS frame the backend had already sent on that stream still triggers a GOAWAY that ends every stream of the backend connection. This lasts until #1784 lands.

Tests:
- `a_backend_response_content_is_forbidden_for_204_and_304_and_discarded_for_head` (`h2.rs`) pins the classifier.
- `a_204_response_carrying_data_resets_its_backend_stream_and_a_head_response_discards_it` (`h2.rs`) drives `handle_data_frame` through three cases:
  - the reset: RST_STREAM(PROTOCOL_ERROR) alone, no GOAWAY, payload not queued;
  - the HEAD discard: no frame, payload not queued, no byte left in the buffer;
  - a GET 200 that forwards its payload.
- e2e in `e2e/src/tests/h2_security_header_injection.rs`:
  - `test_h2_bodiless_response_data_never_reaches_h1_client`: 204, 304 and HEAD, with HEADERS and DATA in one write or 200 ms apart.
  - `test_h2_head_response_with_large_data_completes`: HEAD with 20, 60 and 200 KB of DATA, to an H1 client with a keep-alive follow-up and to an H2 client.
  - `test_h2_head_response_with_large_data_keeps_the_backend_connection`: HEAD with 20 KB on stream 1 and a GET on stream 3 over one backend connection.
- The raw H2 mock now honours sozu's flow-control windows.

Documented in `lib/src/protocol/mux/LIFECYCLE.md` §8.4, `doc/configure.md` and `CHANGELOG.md`.

Fixes #1772



Signed-off-by: Florentin Dubois <[email protected]>
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.

fix(mux-h2): Transfer-Encoding added to an H2 response without a body towards an H1 client

1 participant