Repository navigation
fix(h1): forward HTTP/1.0 backend responses as HTTP/1.1 - #1683
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 29, 2026 13:00
A backend's HTTP/1.0 status line reached the client verbatim: kawa's H1 converter serialises Version::V10 as "HTTP/1.0". Sozu now answers in its own version (RFC 9110 section 6.2): HttpContext::on_response_headers rewrites an HTTP/1.0 response to Version::V11. The H2 converter ignores the version, so H2 clients are unaffected. HTTP/1.1 is persistent by default where HTTP/1.0 is not (RFC 9112 section 9.3). An HTTP/1.0 final response without a keep-alive connection option, with a close-delimited body, or with Transfer-Encoding (faulty framing in HTTP/1.0, RFC 9112 section 6.1) now clears keep_alive_backend and has its Connection options merged into one line ending in close (RFC 9112 section 9.6). Options nominating hop-by-hop fields are kept; only the contradicting keep-alive is dropped. The backend EOF then ends a close-delimited body through ConnectionH1::terminate_close_delimited, and the H1 client connection closes after the response. Before, the backend connection was taken for persistent: a close-delimited HTTP/1.0 body reached an H2 client followed by RST_STREAM instead of END_STREAM. The H2 converter drops the merged line, and the H2 connection stays open. A 1xx keeps its headers apart from the version: its persistence belongs to the final response, and a 101 keeps Connection: Upgrade. The close option is now matched as a list token for every version, so "Connection: keep-alive, close" closes the backend connection. Unit tests in editor.rs and e2e rows in mux_tests.rs and h2_correctness_tests.rs cover these cases; LIFECYCLE.md and CHANGELOG.md are updated. Closes #16 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h1-response-version
branch
from
September 29, 2026 13:29
7495f9d to
190feef
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 29, 2026
## Summary Follow-up to #1683. The request side still compared the whole `Connection` value with `close`, so a client sending `Connection: keep-alive, close` or `Connection: close, TE` kept its connection open after the response. `Connection` is a comma-separated list of case-insensitive options (RFC 9110 §7.6.1), and a `close` among them means the connection closes after the response (RFC 9112 §9.6). #1683 already matches it this way on the response side, so the two sides disagreed. ## Change - `lib/src/protocol/kawa_h1/editor.rs:1105` (`HttpContext::on_request_headers`): `compare_no_case(val, b"close")` becomes `has_connection_option(val, b"close")`. This is the list-token matcher at `editor.rs:104`, the one the response side uses at `editor.rs:1491`. It runs on every `Connection` line. - Nothing else changes: a single `close` still closes, a request without the option still keeps its connection, and the forwarded `Connection` lines are sent as the client wrote them. A token like `closed` does not match. - H2 clients are not affected: pkawa rejects `Connection` as a connection-specific header before the editor sees it. ## Other `Connection` compares checked - **Request path:** there are no other whole-value compares. Upgrades are detected from the 101 status (`ConnectionH1::writable`, `lib/src/protocol/mux/h1.rs`), not from the request header. The `pkawa.rs` compares check header *names*. The request side never reads `keep-alive`. - **Not changed:** `lib/src/protocol/kawa_h1/answers.rs:238` matches `Connection: close` as a whole value too. It reads operator-written answer templates at configuration time, not client input, and is outside the request path. It can be fixed separately if wanted. ## Not fixed here: `closing` overwrites the `Connection` value During shutdown (`closing = true`), both `on_request_headers` (`editor.rs:1102`) and `on_response_headers` (`editor.rs:1487`) replace each `Connection` value with `close`. On responses this happens before the HTTP/1.0 merge runs. The replacement drops any hop-by-hop fields the line nominated (RFC 9110 §7.6.1), for HTTP/1.1 as well. This predates #1683. Fixing it correctly means recording the close first and merging the options afterwards, on both sides and for every version, with the HTTP/1.0 merge loop as the model. That changes shutdown behaviour, including for requests with `Connection: Upgrade`, so it is not in this PR. ## Tests (red, then green) - Unit: `a_close_option_in_a_request_connection_list_closes_the_client` covers `close`, `keep-alive, close`, `close, TE`, `close` listed on a second `Connection` line (`keep-alive` / `TE, Close `), `closed` (must not match), `keep-alive` alone, and no `Connection` header. It also checks that `keep_alive_backend` is not touched and that the `Connection` lines are forwarded unchanged. Before the fix the three list cases fail. The single-token cases passed before and after, and pin the unchanged behaviour. - e2e: `test_h1_client_connection_close_option_in_a_list_closes_client` (`e2e/src/tests/mux_tests.rs`). The client sends `keep-alive, close`, then `close, TE`, to a keep-alive backend that answers with a `Content-Length`. Sōzu must close the client connection after the response and release the `http.active_requests` charge. RED, before the fix: ``` test protocol::kawa_h1::editor::tests::a_close_option_in_a_request_connection_list_closes_the_client ... FAILED assertion `left == right` failed: keep-alive then close: keep_alive_frontend left: true right: false ``` ``` H1-CLIENT-CLOSE-LIST: the client connection was not closed: still open after 5s, read so far: "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: 01M3PSFTX4EFKY1G2M8KTVYTWK\r\n\r\npong" test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 503 filtered out; finished in 5.07s ``` GREEN, after the fix: ``` test protocol::kawa_h1::editor::tests::a_close_option_in_a_request_connection_list_closes_the_client ... ok test tests::mux_tests::test_h1_client_connection_close_option_in_a_list_closes_client ... ok ``` ## Validation - `cargo build --all-features` exits 0 - `cargo clippy --all-features --all-targets -- -D warnings` exits 0 - `cargo +nightly fmt --all -- --check` exits 0 - `cargo test -p sozu-lib`: `1349 passed; 0 failed`, and every other target passes - `cargo test -p sozu-e2e -- keep_alive close connection`: `52 passed; 0 failed` - `python3 .github/scripts/check_doc_citations.py`: `--self-test`, `--root .`, `--base origin/main` and `--audit --root .` all exit 0. The audit's findings are all in `proxy_protocol/LIFECYCLE.md`, which this PR does not touch. ## Docs - `lib/src/protocol/kawa_h1/LIFECYCLE.md` §2: the `on_request_headers` entry now states the request-side `close` rule and names both tests. - `CHANGELOG.md`: new entry under Unreleased / Fixed. Signed-off-by: Florentin Dubois <[email protected]>
This was referenced Sep 29, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Sep 29, 2026
## Problem While Sōzu shuts down (`HttpContext::closing`, set by `Mux::shutting_down_inner`), `HttpContext::on_request_headers` and `HttpContext::on_response_headers` (`lib/src/protocol/kawa_h1/editor.rs`) overwrote every `Connection` value with `close`. That dropped the other options: a field the peer nominated as hop-by-hop (`Connection: x-custom`) was forwarded as end-to-end (RFC 9110 §7.6.1), a request's `Upgrade` field reached the backend without the `upgrade` option that must accompany it (§7.8), and, on the response side, the backend's own `close` was no longer recorded in `keep_alive_backend`. An HTTP/1.1 final response without a `Connection` line got no `close` at all. ## Fix - Both header loops now always record the `close` decision from the original value (`keep_alive_frontend` / `keep_alive_backend`); nothing is rewritten inside the loop. - The HTTP/1.0 merge from #1683 moves into `merge_connection_options_into_close`: every `Connection` line is elided and one line is pushed with the other options followed by a single `close`; `keep-alive` (and an already listed `close`) is dropped. A lone `close` stays a static value (no allocation). - Request side: under `closing`, the merge runs (it replaces the old push-if-absent), so `Connection: close` is still added when the client sent none. - Response side: the merge runs for every final response under `closing` (HTTP/1.1 as well as HTTP/1.0), and as before for a non-persistent HTTP/1.0 response. A 1xx is left alone, as the HTTP/1.0 branch already did. - Docs: `lib/src/protocol/kawa_h1/LIFECYCLE.md` (both callbacks), `CHANGELOG.md` (Unreleased / Fixed). ## Decision: a request asking for `Upgrade` during shutdown Options: (a) keep `Connection: Upgrade, close` and let the backend decide; (b) refuse the upgrade. This PR takes **(b)**: the `upgrade` option is dropped along with every `Upgrade` field (a nominated field must not go end-to-end, §7.6.1). The backend then answers in HTTP/1.1, which a server may always do (§7.8: accepting an upgrade is optional), and the client gets a normal response with `Connection: close` and can retry the upgrade on a new connection. Why not (a): `ConnectionH1::writable` (`lib/src/protocol/mux/h1.rs`) checks `stream.context.closing` **before** the `101` arm, so while draining Sōzu never completes an upgrade. Under (a), a 101 would reach the client and the connection would close right after it. Refusing is the only choice consistent with the existing drain semantics (finish in-flight work, start no long-lived work). Before this PR the request already went out as `Connection: close` + `Upgrade: websocket` (see RED below), which was broken in its own way. If you would rather keep the upgrade, `writable` has to change too; please say so on this PR. ## `lib/src/protocol/kawa_h1/answers.rs` whole-value `Connection: close` match: left as is That match (in the template parser) sets the template's own `keep_alive` flag. It reads operator-authored answer templates, not peer traffic, and every shipped default template carries a lone `Connection: close` line (checked: all 15 `Connection:` lines of the built-in default templates in `answers.rs`, outside its test module, are exactly `Connection: close`), so no default answer is affected. It is the same kind of defect as #1687 (a list such as `x, close` would read as keep-alive), but it is outside this shutdown fix. It deserves its own small follow-up that exposes `has_connection_option` to `answers.rs`. ## Tests New unit tests in `editor.rs`, run with `closing = true`: - `a_shutting_down_request_keeps_its_connection_options`: no Connection → `close`; `x-custom` → `x-custom, close`; `keep-alive, x-custom` → `x-custom, close`; `close` stays a single `close` and clears `keep_alive_frontend`; two lines merged; HTTP/1.0. - `a_shutting_down_request_does_not_upgrade`: `Upgrade` → `close` with no `Upgrade:` field; `keep-alive, Upgrade, x-custom` → `x-custom, close`. - `a_shutting_down_response_keeps_its_connection_options`: the same matrix on HTTP/1.1 responses plus a persistent HTTP/1.0 one; the backend `close` clears `keep_alive_backend`. - `a_shutting_down_interim_response_is_left_alone`: a 101 keeps `Connection: Upgrade`; a 100 gets no line. **RED** (tests added on `origin/main` 3ed6e8a before the fix, `cargo test -p sozu-lib --lib a_shutting_down`): ``` test protocol::kawa_h1::editor::tests::a_shutting_down_request_does_not_upgrade ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_request_keeps_its_connection_options ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_interim_response_is_left_alone ... FAILED test protocol::kawa_h1::editor::tests::a_shutting_down_response_keeps_its_connection_options ... FAILED websocket: the Upgrade field is not forwarded, got "GET /chat HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\nUpgrade: websocket\r\n..." nominated field: ... left: ["Connection: close"] right: ["Connection: x-custom, close"] 101: ... left: ["Connection: close"] right: ["Connection: Upgrade"] no Connection: ... got "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nSozu-Id: ...\r\n\r\nabcd" left: [] right: ["Connection: close"] test result: FAILED. 0 passed; 4 failed; 0 ignored; 0 measured; 1349 filtered out ``` **GREEN** (`cargo test -p sozu-lib --lib kawa_h1`): ``` test protocol::kawa_h1::editor::tests::a_shutting_down_request_does_not_upgrade ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_interim_response_is_left_alone ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_response_keeps_its_connection_options ... ok test protocol::kawa_h1::editor::tests::a_shutting_down_request_keeps_its_connection_options ... ok test result: ok. 71 passed; 0 failed ``` ### Why there is no e2e test The request-side shutdown branch is unreachable today, and no known sequence reaches the response-side one: the fix is defensive. The e2e harness cannot reach either. `Mux::shutting_down_inner` sets `closing` only on an `Unlinked` stream, whose headers were already edited, and an idle keep-alive session is closed rather than marked (see `try_msg_close`). Measured: with an `eprintln!` probe in both merge branches, the whole `sozu-e2e` suite (fuzz skipped) got **0 probe hits** (493 passed / 7 failed; the 7 are load flakes, see below). The probe was then removed. This shows only that the harness does not reach the branches; whether production traffic can reach them (for example via a retried or H2 stream) is not established here. The unit tests are the coverage, and LIFECYCLE.md says so. ## Validation (each by its own exit code) - `cargo build --all-features` → 0 - `cargo clippy --all-features --all-targets -- -D warnings` → 0 - `cargo +nightly fmt --all -- --check` → 0 - `RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked` → 0 - `cargo test -p sozu-lib` → 0 (1363 passed); `-p sozu` → 0 (238); `-p sozu-command-lib` → 0 (195) - e2e: `http10` 5/5, `close_delimited` 12/12, `keep_alive` 4/4, `connection` 20/20, `upgrade` 14/14, `websocket` 4/4, `soft_stop` 2/2, all exit 0 - `check_doc_citations.py` `--self-test`, `--root .`, `--base origin/main`, `--audit --root .` → all 0 Flake triage for the 7 failures in the full probe run (host load average 43–63 from concurrent builds): `test_h1_smuggling_te_cl_trailing_tab`, `e2e_h2_flow_per_stream_idle_timeout`, `test_upgrade` and `test_upgrade_multiple_in_flight` passed on their first isolated rerun. `test_h2_graceful_shutdown_completes_large_transfer` and `test_issue_806` failed once in isolation, then passed 3 more isolated runs. `test_h1_to_h2_basic_request` (H1 client → H2 backend, `closing` never set) then went 8/8 on this branch and 8/8 on an `origin/main` worktree in interleaved runs. None of these paths enter the changed branches: the probe fired 0 times. ## Revision 2 (after review) - Rebased on `origin/main` d9ce2a0 (#1686). - MSRV clippy (1.93.1, `clippy::nonminimal_bool`) flagged the option filter in `merge_connection_options_into_close`: it is now a named `dropped` disjunction, with no `allow`. `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` → 0 (0 warning/error lines); stable clippy → 0. - Dropped the tautological shutdown `debug_assert!` in `on_response_headers`. - LIFECYCLE.md now states that the request-side shutdown branch is unreachable today (`Mux::shutting_down_inner` sets `closing` only on an `Unlinked`, non-quiesced stream whose heads were both edited) and that both branches are defensive. - Rerun: build, nightly fmt, doc `-D warnings`, `cargo test -p sozu-lib` (1372 passed), `kawa_h1` 71 passed, e2e `http10` 5/5, `connection` 20/20, `upgrade` 15/15, `websocket` 4/4, four citation checks → all 0. ## Residual risks - Textual conflict with #1685 (`feat/forwarded-headers`) in the `on_request_headers` header loop: adjacent lines only, trivial to resolve. The first PR to merge leaves a one-hunk rebase for the other. - The shutdown branches are covered by unit tests only (see above). Closes #1690 Signed-off-by: Florentin Dubois <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Sōzu forwarded a backend's
HTTP/1.0status line to the client unchanged. It now answers with its own version (RFC 9110 §6.2), which is what remains of #16 after the maintainer narrowed it. The PR also applies the persistence rules that change with the version. This description matches head190feef3, which includes the fixes from an adversarial review of the first revision (7495f9d5).Design
All line references are to
lib/src/protocol/kawa_h1/editor.rsat190feef3, insideHttpContext::on_response_headers.:1464,:1528,:1568): an HTTP/1.0 response is rewritten toVersion::V11, including a 1xx.HTTP/1.0only forV10;V11andV20both giveHTTP/1.1(kawa 0.7.2src/protocol/h1/converter.rs).:1529,:1533): HTTP/1.0 is not persistent by default (RFC 9112 §9.3). A final (non-1xx) HTTP/1.0 response clearskeep_alive_backendwhen any of these holds:keep-aliveconnection option;Transfer-Encoding, which is faulty framing in HTTP/1.0 (§6.1, detected at:1496).:1535–:1564): the response'sConnectionlines are merged into one line ending inclose(§9.6).keep-alive, which would contradictclose, is dropped, along with any duplicateclose.closing), the result is a singleConnection: close.ConnectionH1::terminate_close_delimited,lib/src/protocol/mux/h1.rs).ConnectionH1::writablecloses the H1 client connection after the response.Review fixes
101lostConnection: Upgrade: a 1xx is now excluded from the persistence rewrite (:1529), and only its version changes. A 101 keepsConnection: Upgrade.Connection: keep-alive, closewas treated as persistent:closeis now matched as a case-insensitive, trimmed option in a comma-separated list, on everyConnectionline (has_connection_option,:104, used at:1490).Connectiondropped hop-by-hop nominations: the options are now merged and extended withcloseinstead of the header being replaced (:1535–:1564). Only the contradictingkeep-aliveis dropped.Transfer-Encoding: the response is now non-persistent (:1496,:1533).lib/src/protocol/kawa_h1/LIFECYCLE.md§2 describes all of the above.Behaviour change for HTTP/1.1
Item 2 changes how
closeis detected for every version, not only HTTP/1.0.Connection: keep-alive, closeorConnection: foo, closecompared the whole value withclose. The response was therefore treated as persistent, and both connections stayed open.Connectionheaders are still forwarded unchanged.Performance-visible
A backend that answers HTTP/1.0 without
Connection: keep-alivenow makes Sōzu close the H1 client connection after every response. Those clients open a new connection per request, plus a new handshake over TLS. Enabling keep-alive on such backends avoids it. This is noted inCHANGELOG.md.Tests
Unit tests, all in
editor.rs:an_http10_keep_alive_response_is_forwarded_as_http11:3437keep-aliveplusContent-Lengthpersists;HTTP/1.1; no close.an_http10_response_without_keep_alive_is_forwarded_as_http11_with_close:3479Content-Lengthwithoutkeep-alive, andkeep-alivebut close-delimited. Each gives exactlyConnection: close.a_close_option_in_a_connection_list_closes_the_backend(new):3540keep-alive, close; HTTP/1.1keep-alive, Close; HTTP/1.1closeon a second line.a_non_persistent_http10_response_keeps_its_connection_options(new):3580x-custom→x-custom, close;keep-alive, x-custom→x-custom, close; two lines merged.a_chunked_http10_response_closes_the_backend(new):3618Transfer-Encoding: chunkedandkeep-aliveis non-persistent.an_http10_101_keeps_its_connection_upgrade(new):3644HTTP/1.1 101,Connection: Upgradepreserved, keep-alive untouched.an_http10_100_leaves_persistence_to_the_final_response(new):3674End-to-end tests, with an HTTP/1.1 or H2 client in front of an HTTP/1.0 backend:
e2e/src/tests/mux_tests.rs:test_h1_http10_close_delimited_response_is_forwarded_as_http11(:3056)test_h1_http10_close_delimited_response_read_with_the_eof_is_forwarded_as_http11(:3074)test_h1_http10_content_length_response_is_forwarded_as_http11(:3092)HTTP/1.1 200status line,Connection: close, the full body, the client connection reaching EOF, andhttp.active_requestsreturning to 0.e2e/src/tests/h2_correctness_tests.rs:test_h2_http10_close_delimited_body_ends_stream_at_backend_close(:4499)test_h2_http10_close_delimited_body_keeps_the_h2_connection(:4600)Red, first revision (the tests run against
origin/main)Red, review fixes (the five new tests run against
7495f9d5)Green at
190feef3Validation at
190feef3cargo build --all-features: exit 0.aws-lc-fips-sysnative artifact (could not find native static library aws_lc_fips_0_14_2_crypto).cargo clean -p aws-lc-fips-sys. That crate is a dependency outside this change.cargo clippy --all-features --all-targets -- -D warnings: exit 0.cargo +nightly fmt --all -- --check: exit 0.cargo test -p sozu-lib: 1348 passed; 0 failed.cargo test -p sozu-e2e -- --skip tests::fuzz_tests::: 499 passed; 0 failed.python3 .github/scripts/check_doc_citations.pywith--self-test,--root .,--base origin/mainand--audit --root .: all exit 0.Residual risks and out of scope
101: it is covered byan_http10_101_keeps_its_connection_upgradeonly. The existing upgrade and WebSocket e2e tests, which use HTTP/1.1 backends, pass.closedetection is now token-based: see the behaviour-change section above.keep_alive_frontendis still cleared only by aConnection: closerequest header.closeoption is still not treated as ending at the close for backend reuse. This PR does not change that.Closes #16