From fa3a4b2c04c12fd849dc50abaceba32c4d436069 Mon Sep 17 00:00:00 2001 From: dswij Date: Mon, 10 Aug 2026 23:19:24 +0800 Subject: [PATCH 01/57] chore(lib): old commented code cleanup (#4151) --- src/body/incoming.rs | 2 -- src/common/io/rewind.rs | 4 ---- src/proto/h1/io.rs | 13 ------------- src/upgrade.rs | 1 - 4 files changed, 20 deletions(-) diff --git a/src/body/incoming.rs b/src/body/incoming.rs index a0677e7e58..93c99a01a6 100644 --- a/src/body/incoming.rs +++ b/src/body/incoming.rs @@ -491,8 +491,6 @@ mod tests { body_expected_size, ); - //assert_eq!(body_size, mem::size_of::>(), "Option"); - assert_eq!( mem::size_of::(), mem::size_of::() * 5, diff --git a/src/common/io/rewind.rs b/src/common/io/rewind.rs index b692f438ee..7acd809685 100644 --- a/src/common/io/rewind.rs +++ b/src/common/io/rewind.rs @@ -46,10 +46,6 @@ impl Rewind { pub(crate) fn into_inner(self) -> (T, Bytes) { (self.inner, self.pre.unwrap_or_default()) } - - // pub(crate) fn get_mut(&mut self) -> &mut T { - // &mut self.inner - // } } impl Read for Rewind diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index efec6fef4a..6b3f10fbae 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -657,19 +657,6 @@ mod tests { use tokio_test::io::Builder as Mock; - // #[cfg(feature = "nightly")] - // use test::Bencher; - - /* - impl MemRead for AsyncIo { - fn read_mem(&mut self, len: usize) -> Poll { - let mut v = vec![0; len]; - let n = try_nb!(self.read(v.as_mut_slice())); - Ok(Async::Ready(BytesMut::from(&v[..n]).freeze())) - } - } - */ - #[tokio::test] #[ignore] async fn iobuf_write_empty_slice() { diff --git a/src/upgrade.rs b/src/upgrade.rs index 470f4ca435..df1b9e6847 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -405,7 +405,6 @@ mod tests { _: &mut Context<'_>, buf: &[u8], ) -> Poll> { - // panic!("poll_write shouldn't be called"); Poll::Ready(Ok(buf.len())) } From 3534d75c898da17b023e6bff9b4cf71afed123ad Mon Sep 17 00:00:00 2001 From: Nam2ee <81401376+nam2ee@users.noreply.github.com> Date: Tue, 11 Aug 2026 01:08:24 +0900 Subject: [PATCH 02/57] fix(http1): recognize `\n\r\n` as a head terminator in the partial-read fast path (#4147) `is_complete_fast` recognizes `\r\n\r\n` and `\n\n` as head terminators but not `\n\r\n`, while the full parser (httparse) accepts all three. So a request whose head ends with `\n\r\n` parses fine when it arrives in a single read, but stalls when it arrives split across reads: the fast path never reports the head complete and the connection keeps waiting for more bytes. This extends the `\n` branch to also accept a following `\r\n`, using the same panic-safe slicing idiom as the `\r` branch. Added the `\n\r\n` witness and the `\n\r` negative to `test_is_complete_fast`, plus a parse-level test documenting that the full parser accepts this terminator. Closes #4145 --- src/proto/h1/role.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index f5c8db5ed0..29bcd44b0d 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -106,7 +106,10 @@ fn is_complete_fast(bytes: &[u8], prev_len: usize) -> bool { if bytes[i + 1..].chunks(3).next() == Some(&b"\n\r\n"[..]) { return true; } - } else if b == b'\n' && bytes.get(i + 1) == Some(&b'\n') { + } else if b == b'\n' + && (bytes.get(i + 1) == Some(&b'\n') + || bytes[i + 1..].chunks(2).next() == Some(&b"\r\n"[..])) + { return true; } } @@ -2977,6 +2980,10 @@ mod tests { for n in 0..s.len() { assert!(is_complete_fast(s, n)); } + let s = b"GET / HTTP/1.1\r\na: b\n\r\n"; + for n in 0..s.len() { + assert!(is_complete_fast(s, n), "{:?}; {}", s, n); + } // Not let s = b"GET / HTTP/1.1\r\na: b\r\n\r"; @@ -2987,6 +2994,36 @@ mod tests { for n in 0..s.len() { assert!(!is_complete_fast(s, n)); } + let s = b"GET / HTTP/1.1\r\na: b\n\r"; + for n in 0..s.len() { + assert!(!is_complete_fast(s, n)); + } + } + + #[cfg(feature = "server")] + #[test] + fn test_parse_accepts_lf_crlf_terminator() { + // The full parser (httparse) accepts a bare-LF line ending followed + // by a CRLF blank line as the end of the head, so the partial-read + // fast path must recognize it too. + let mut bytes = BytesMut::from("GET / HTTP/1.1\r\na: b\n\r\n"); + Server::parse( + &mut bytes, + ParseContext { + cached_headers: &mut None, + req_method: &mut None, + h1_parser_config: Default::default(), + h1_max_headers: None, + preserve_header_case: false, + #[cfg(feature = "ffi")] + preserve_header_order: false, + h09_responses: false, + #[cfg(feature = "client")] + on_informational: &mut None, + }, + ) + .expect("parse ok") + .expect("parse complete"); } #[test] From af432e993c92ea65e76fc590738334e74b5ccf34 Mon Sep 17 00:00:00 2001 From: Morax <100508620+fzlzjerry@users.noreply.github.com> Date: Tue, 11 Aug 2026 00:09:05 +0800 Subject: [PATCH 03/57] docs(body): add streaming read examples (#4148) Show how to process data and trailer frames while preserving back-pressure, and contrast that with intentionally collecting a bounded body in memory. Closes #2201 --- src/body/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/body/mod.rs b/src/body/mod.rs index 2df3319dd5..c34d019d21 100644 --- a/src/body/mod.rs +++ b/src/body/mod.rs @@ -17,7 +17,52 @@ //! There are additional implementations available in [`http-body-util`][], //! such as a `Full` or `Empty` body. //! +//! ## Reading a body +//! +//! The [`BodyExt`][] extension trait provides an asynchronous way to read the +//! frames of a body. A frame can contain either data or trailers: +//! +//! ``` +//! use http_body_util::BodyExt as _; +//! use hyper::body::Incoming; +//! +//! async fn read_body(mut body: Incoming) -> Result<(), hyper::Error> { +//! while let Some(frame) = body.frame().await { +//! let frame = frame?; +//! +//! if let Some(data) = frame.data_ref() { +//! println!("received {} bytes", data.len()); +//! } +//! +//! if let Some(trailers) = frame.trailers_ref() { +//! println!("received trailers: {trailers:?}"); +//! } +//! } +//! +//! Ok(()) +//! } +//! ``` +//! +//! A body only advances when it is polled. Processing each frame before +//! polling for the next one preserves back-pressure on the connection. +//! +//! If a body is known to be small, it can be collected into memory instead: +//! +//! ``` +//! use http_body_util::BodyExt as _; +//! use hyper::body::{Bytes, Incoming}; +//! +//! /// Consider using `Limited` if the body is untrusted. +//! async fn read_entire_body(body: Incoming) -> Result { +//! Ok(body.collect().await?.to_bytes()) +//! } +//! ``` +//! +//! Collecting buffers the whole body, so it should be avoided for large or +//! untrusted bodies unless their size is limited. +//! //! [`http-body-util`]: https://docs.rs/http-body-util +//! [`BodyExt`]: https://docs.rs/http-body-util/latest/http_body_util/trait.BodyExt.html pub use bytes::{Buf, Bytes}; pub use http_body::Body; From 72fcf545c9b4a261f31d89bafd3019a2d4e9e1d7 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 11 Aug 2026 03:16:47 -0400 Subject: [PATCH 04/57] fix(http1): detect TE: trailers caselessly and with other values (#4152) --- src/headers.rs | 52 +++++++++++++++++++++++++++++++ src/proto/h1/conn.rs | 8 ++--- tests/server.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 6 deletions(-) diff --git a/src/headers.rs b/src/headers.rs index 89b70fd45e..caace71f2a 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -42,6 +42,26 @@ fn connection_has(value: &HeaderValue, needle: &str) -> bool { false } +#[cfg(feature = "http1")] +pub(super) fn te_is_trailers(headers: &http::HeaderMap) -> bool { + header_value_list_has(headers.get_all(http::header::TE).into_iter(), "trailers") +} + +#[cfg(feature = "http1")] +fn header_value_list_has(values: http::header::ValueIter<'_, HeaderValue>, needle: &str) -> bool { + for value in values { + if let Ok(line) = value.to_str() { + for token in line.split(',') { + if token.trim().eq_ignore_ascii_case(needle) { + return true; + } + } + } + } + + false +} + #[cfg(all(feature = "http1", feature = "server"))] pub(super) fn content_length_parse(value: &HeaderValue) -> Option { from_digits(value.as_bytes()) @@ -166,3 +186,35 @@ pub(super) fn add_chunked(mut entry: http::header::OccupiedEntry<'_, HeaderValue entry.insert(HeaderValue::from_static(CHUNKED)); } + +#[cfg(all(test, feature = "http1"))] +mod tests { + use super::te_is_trailers; + use http::header::{HeaderValue, TE}; + use http::HeaderMap; + + #[test] + fn te_is_trailers_accepts_comma_separated_values() { + let mut headers = HeaderMap::new(); + headers.insert(TE, HeaderValue::from_static("gzip, Trailers")); + + assert!(te_is_trailers(&headers)); + } + + #[test] + fn te_is_trailers_accepts_multiple_header_lines() { + let mut headers = HeaderMap::new(); + headers.append(TE, HeaderValue::from_static("gzip")); + headers.append(TE, HeaderValue::from_static("trailers")); + + assert!(te_is_trailers(&headers)); + } + + #[test] + fn te_is_trailers_rejects_missing_trailers_token() { + let mut headers = HeaderMap::new(); + headers.insert(TE, HeaderValue::from_static("gzip")); + + assert!(!te_is_trailers(&headers)); + } +} diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index a2d2670e6b..19c1283df7 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -11,7 +11,7 @@ use std::time::Duration; use crate::rt::{Read, Write}; use bytes::{Buf, Bytes}; use futures_core::ready; -use http::header::{HeaderValue, CONNECTION, TE}; +use http::header::{HeaderValue, CONNECTION}; use http::{HeaderMap, Method, Version}; use http_body::Frame; use httparse::ParserConfig; @@ -325,11 +325,7 @@ where )); } - self.state.allow_trailer_fields = msg - .head - .headers - .get(TE) - .map_or(false, |te_header| te_header == "trailers"); + self.state.allow_trailer_fields = headers::te_is_trailers(&msg.head.headers); Poll::Ready(Some(Ok((msg.head, msg.decode, wants)))) } diff --git a/tests/server.rs b/tests/server.rs index 855840b388..1843610e88 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -3335,6 +3335,79 @@ fn http1_trailer_fields_not_allowed() { assert_eq!(body, expected_body); } +#[test] +fn http1_trailer_fields_allowed_with_comma_separated_te() { + let body = futures_util::stream::once(async move { Ok("hello".into()) }); + let mut headers = HeaderMap::new(); + headers.insert("chunky-trailer", "header data".parse().unwrap()); + + let server = serve(); + server + .reply() + .header("transfer-encoding", "chunked") + .header("trailer", "chunky-trailer") + .body_stream_with_trailers(body, headers); + let mut req = connect(server.addr()); + req.write_all( + b"\ + GET / HTTP/1.1\r\n\ + Host: example.domain\r\n\ + Connection: keep-alive\r\n\ + TE: gzip, Trailers\r\n\ + \r\n\ + ", + ) + .expect("writing"); + + let chunky_trailer_chunk = b"\r\nchunky-trailer: header data\r\n\r\n"; + let res = read_until(&mut req, |buf| buf.ends_with(chunky_trailer_chunk)).expect("reading"); + let sres = s(&res); + + let date_fragment = "GMT\r\n\r\n"; + let pos = sres.find(date_fragment).expect("find GMT"); + let body = &sres[pos + date_fragment.len()..]; + + let expected_body = "5\r\nhello\r\n0\r\nchunky-trailer: header data\r\n\r\n"; + assert_eq!(body, expected_body); +} + +#[test] +fn http1_trailer_fields_allowed_with_multiple_te_headers() { + let body = futures_util::stream::once(async move { Ok("hello".into()) }); + let mut headers = HeaderMap::new(); + headers.insert("chunky-trailer", "header data".parse().unwrap()); + + let server = serve(); + server + .reply() + .header("transfer-encoding", "chunked") + .header("trailer", "chunky-trailer") + .body_stream_with_trailers(body, headers); + let mut req = connect(server.addr()); + req.write_all( + b"\ + GET / HTTP/1.1\r\n\ + Host: example.domain\r\n\ + Connection: keep-alive\r\n\ + TE: gzip\r\n\ + TE: trailers\r\n\ + \r\n\ + ", + ) + .expect("writing"); + + let chunky_trailer_chunk = b"\r\nchunky-trailer: header data\r\n\r\n"; + let res = read_until(&mut req, |buf| buf.ends_with(chunky_trailer_chunk)).expect("reading"); + let sres = s(&res); + + let date_fragment = "GMT\r\n\r\n"; + let pos = sres.find(date_fragment).expect("find GMT"); + let body = &sres[pos + date_fragment.len()..]; + + let expected_body = "5\r\nhello\r\n0\r\nchunky-trailer: header data\r\n\r\n"; + assert_eq!(body, expected_body); +} + #[test] fn http1_trailer_recv_fields() { let server = serve(); From fca07bd3937d4a3a4822372acdf766d0640796af Mon Sep 17 00:00:00 2001 From: Murilo Silva Date: Wed, 12 Aug 2026 12:24:55 -0300 Subject: [PATCH 05/57] style(lib): fix missing_errors_doc lint (#4154) Refs: #4071 --- Cargo.toml | 1 - src/client/conn/http1.rs | 21 ++++++++++++++++++++- src/client/conn/http2.rs | 17 ++++++++++++++++- src/upgrade.rs | 6 ++++-- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 22826d9900..150b030a75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -131,7 +131,6 @@ map_err_ignore = "allow" map_unwrap_or = "allow" match_wild_err_arm = "allow" missing_fields_in_debug = "allow" # TODO: use finish_non_exhaustive -missing_errors_doc = "allow" # TODO: good to fix missing_panics_doc = "allow" # TODO: might be false multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" diff --git a/src/client/conn/http1.rs b/src/client/conn/http1.rs index ec64543c23..7468507623 100644 --- a/src/client/conn/http1.rs +++ b/src/client/conn/http1.rs @@ -97,6 +97,10 @@ where /// Prevent shutdown of the underlying IO object at the end of service the request, /// instead run `into_parts`. This is a convenience wrapper over `poll_without_shutdown`. + /// + /// # Errors + /// + /// Returns an error if the connection encounters an error while being polled to completion. pub async fn without_shutdown(self) -> crate::Result> { let mut conn = Some(self); crate::common::future::poll_fn(move |cx| -> Poll>> { @@ -137,6 +141,10 @@ pub struct Builder { /// /// This is a shortcut for `Builder::new().handshake(io)`. /// See [`client::conn`](crate::client::conn) for more. +/// +/// # Errors +/// +/// Returns an error if the HTTP/1 connection handshake fails. pub async fn handshake(io: T) -> crate::Result<(SendRequest, Connection)> where T: Read + Write + Unpin, @@ -159,6 +167,8 @@ impl SendRequest { /// Waits until the dispatcher is ready. /// + /// # Errors + /// /// If the associated connection is closed, this returns an Error. pub async fn ready(&mut self) -> crate::Result<()> { crate::common::future::poll_fn(|cx| self.poll_ready(cx)).await @@ -210,6 +220,11 @@ where /// hyper closes the underlying connection when a request future is /// dropped before completion. Any subsequent calls on the same /// [`SendRequest`] will return a `canceled` error. + /// + /// # Errors + /// + /// Returns an error if the connection is not ready or if an error occurs while + /// processing the request. pub fn send_request( &mut self, req: Request, @@ -236,7 +251,7 @@ where /// /// Returns a future that if successful, yields the `Response`. /// - /// # Error + /// # Errors /// /// If there was an error before trying to serialize the request to the /// connection, the message will be returned as part of this error. @@ -533,6 +548,10 @@ impl Builder { /// /// Note, if [`Connection`] is not `await`-ed, [`SendRequest`] will /// do nothing. + /// + /// # Errors + /// + /// Returns an error if the HTTP/1connection handshake fails. pub fn handshake( &self, io: T, diff --git a/src/client/conn/http2.rs b/src/client/conn/http2.rs index ec046e0a39..d1dfcfd416 100644 --- a/src/client/conn/http2.rs +++ b/src/client/conn/http2.rs @@ -74,6 +74,10 @@ pub struct Builder { /// /// This is a shortcut for `Builder::new(exec).handshake(io)`. /// See [`client::conn`](crate::client::conn) for more. +/// +/// # Errors +/// +/// Returns an error if the HTTP/2 connection handshake fails. pub async fn handshake( exec: E, io: T, @@ -104,6 +108,8 @@ impl SendRequest { /// Waits until the dispatcher is ready. /// + /// # Errors + /// /// If the associated connection is closed, this returns an Error. pub async fn ready(&mut self) -> crate::Result<()> { crate::common::future::poll_fn(|cx| self.poll_ready(cx)).await @@ -147,6 +153,11 @@ where /// other in-flight and future requests. The peer is notified /// immediately rather than continuing to send a response body that /// would be discarded. + /// + /// # Errors + /// + /// Returns an error if the connection is not ready or if an error occurs while + /// processing the request. pub fn send_request( &mut self, req: Request, @@ -174,7 +185,7 @@ where /// /// Returns a future that if successful, yields the `Response`. /// - /// # Error + /// # Errors /// /// If there was an error before trying to serialize the request to the /// connection, the message will be returned as part of this error. @@ -547,6 +558,10 @@ where /// /// Note, if [`Connection`] is not `await`-ed, [`SendRequest`] will /// do nothing. + /// + /// # Errors + /// + /// Returns an error if the HTTP/2 connection handshake fails. pub fn handshake( &self, io: T, diff --git a/src/upgrade.rs b/src/upgrade.rs index df1b9e6847..bb350e78ed 100644 --- a/src/upgrade.rs +++ b/src/upgrade.rs @@ -147,8 +147,10 @@ impl Upgraded { /// Tries to downcast the internal trait object to the type passed. /// - /// On success, returns the downcasted parts. On error, returns the - /// `Upgraded` back. + /// On success, returns the downcasted parts. + /// + /// # Errors + /// On error, returns the `Upgraded` back. pub fn downcast(self) -> Result, Self> { let (io, buf) = self.io.into_inner(); match io.__hyper_downcast() { From d32b294f0bd0320df96c37d39a3047219ab0d7ae Mon Sep 17 00:00:00 2001 From: MsfPablo <129399053+MsfPablo@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:53:09 +0200 Subject: [PATCH 06/57] style(lib): fix needless_continue lint (#4157) --- Cargo.toml | 1 - src/proto/h1/dispatch.rs | 1 - src/proto/h2/client.rs | 1 - src/proto/h2/upgrade.rs | 4 +--- 4 files changed, 1 insertion(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 150b030a75..5a02a7215b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -134,7 +134,6 @@ missing_fields_in_debug = "allow" # TODO: use finish_non_exhaustive missing_panics_doc = "allow" # TODO: might be false multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" -needless_continue = "allow" needless_pass_by_value = "allow" panic = "allow" pattern_type_mismatch = "allow" diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index 03aaba1a9e..f250f55863 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -430,7 +430,6 @@ where ); } else { trace!("discarding unknown frame"); - continue; } } else { *clear_body = true; diff --git a/src/proto/h2/client.rs b/src/proto/h2/client.rs index 4803dba0f1..eb768baa76 100644 --- a/src/proto/h2/client.rs +++ b/src/proto/h2/client.rs @@ -772,7 +772,6 @@ where } } self.poll_pipe(f, cx); - continue; } Poll::Ready(None) => { diff --git a/src/proto/h2/upgrade.rs b/src/proto/h2/upgrade.rs index f4e65466a2..217aa27e88 100644 --- a/src/proto/h2/upgrade.rs +++ b/src/proto/h2/upgrade.rs @@ -236,9 +236,7 @@ impl Read for H2Upgraded { self.buf = loop { match ready!(self.recv_stream.poll_data(cx)) { None => return Poll::Ready(Ok(())), - Some(Ok(buf)) if buf.is_empty() && !self.recv_stream.is_end_stream() => { - continue - } + Some(Ok(buf)) if buf.is_empty() && !self.recv_stream.is_end_stream() => {} Some(Ok(buf)) => { self.ping.record_data(buf.len()); break buf; From 8fcaf1a62787b824fdd7b95a3e7c9ea81acd3449 Mon Sep 17 00:00:00 2001 From: MsfPablo <129399053+MsfPablo@users.noreply.github.com> Date: Mon, 17 Aug 2026 21:46:48 +0200 Subject: [PATCH 07/57] docs(lib): expand crate-level cancel safety section with HTTP/1 vs HTTP/2 (#4156) Closes #4054 --- src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7be9926076..8d6e2d2d5a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,10 +33,23 @@ //! # Cancel safety //! //! Futures returned by hyper are cancel safe: dropping a future before it -//! completes is the supported way to cancel the operation. See the -//! documentation on individual futures — for example `SendRequest::send_request` -//! in `client::conn::http1` and `client::conn::http2` — for the protocol- -//! specific behavior on cancellation. +//! completes is the supported way to cancel the operation. The protocol in +//! use changes what that cancellation actually does on the wire: +//! +//! - **HTTP/1** has no in-protocol way to abort a single request without +//! affecting the shared connection, so dropping an in-flight request future +//! closes the underlying TCP connection. Any subsequent call on the same +//! `SendRequest` returns a `canceled` error; the connection cannot be +//! reused. +//! - **HTTP/2** resets the single stream with `RST_STREAM` (`CANCEL` error +//! code) and notifies the peer immediately rather than continuing to +//! deliver a response body that would be discarded. The shared connection +//! stays usable for other in-flight and future requests. +//! +//! See the documentation on individual futures — for example +//! `SendRequest::send_request` in `client::conn::http1` and the equivalent +//! in `client::conn::http2` — for the protocol-specific behavior on +//! cancellation. //! //! # Optional Features //! From 8b2e533253cf3acaadd85d3c05c93b3be2205d43 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 24 Aug 2026 08:09:52 -0400 Subject: [PATCH 08/57] style(lib): temporarily allow some new clippy lints (#4165) --- Cargo.toml | 3 +++ src/body/incoming.rs | 1 + src/client/conn/http1.rs | 1 + src/client/conn/http2.rs | 1 + 4 files changed, 6 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 5a02a7215b..01fff25297 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -139,9 +139,11 @@ panic = "allow" pattern_type_mismatch = "allow" redundant_closure_for_method_calls = "allow" redundant_else = "allow" +rest_pattern_accessible_field = "allow" # TODO: consider, might catch bugs single_char_lifetime_names = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? trivially_copy_pass_by_ref = "allow" +unnecessary_rest_pattern = "allow" # TODO: simple fix unnecessary_trailing_comma = "allow" unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? @@ -158,6 +160,7 @@ clone_on_ref_ptr = "allow" # Arc::clone(blah) is needlessly noisy cognitive_complexity = "allow" # is this ever useful? decimal_literal_representation = "allow" # arguably more human-readable default_numeric_fallback = "allow" # too many false positives +definition_in_module_root = "allow" # this is normal expect_used = "allow" # expect is self-documenting error_impl_error = "allow" # mod::Error is a fine name field_scoped_visibility_modifiers = "allow" # possibly good idea, noisy for now diff --git a/src/body/incoming.rs b/src/body/incoming.rs index 93c99a01a6..6ef6c0963a 100644 --- a/src/body/incoming.rs +++ b/src/body/incoming.rs @@ -390,6 +390,7 @@ impl Sender { /// Send trailers on trailers channel. #[allow(unused)] + #[allow(clippy::unused_async_trait_impl)] pub(crate) async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> { let tx = match self.trailers_tx.take() { Some(tx) => tx, diff --git a/src/client/conn/http1.rs b/src/client/conn/http1.rs index 7468507623..bdb03171d4 100644 --- a/src/client/conn/http1.rs +++ b/src/client/conn/http1.rs @@ -255,6 +255,7 @@ where /// /// If there was an error before trying to serialize the request to the /// connection, the message will be returned as part of this error. + #[allow(clippy::result_large_err)] pub fn try_send_request( &mut self, req: Request, diff --git a/src/client/conn/http2.rs b/src/client/conn/http2.rs index d1dfcfd416..10613b354a 100644 --- a/src/client/conn/http2.rs +++ b/src/client/conn/http2.rs @@ -189,6 +189,7 @@ where /// /// If there was an error before trying to serialize the request to the /// connection, the message will be returned as part of this error. + #[allow(clippy::result_large_err)] pub fn try_send_request( &mut self, req: Request, From 08a288135fb66321940b07ac15f3b9b8e46bc861 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 25 Aug 2026 14:35:05 -0400 Subject: [PATCH 09/57] docs(contrib): add initial COLLABORATORS guide (#4158) --- docs/COLLABORATORS.md | 68 +++++++++++++++++++++++++++++++++++++++++++ docs/GOVERNANCE.md | 1 + 2 files changed, 69 insertions(+) create mode 100644 docs/COLLABORATORS.md diff --git a/docs/COLLABORATORS.md b/docs/COLLABORATORS.md new file mode 100644 index 0000000000..37c590a7ec --- /dev/null +++ b/docs/COLLABORATORS.md @@ -0,0 +1,68 @@ +# hyper Collaborator Guide + +This document describes how collaborators manage hyper. + +- First off, a collaborator doesn't need to do all of the following perfectly. + - It takes time to learn. + - Instead, focus on growth. + - Get in the habit of checking this guide with some frequency, and improve rather than stagnate. + +## Exemplify the Code of Conduct + +- Our [code of conduct](./CODE_OF_CONDUCT.md) is not elaborate. It doesn't require much study, nor explication. +- To be a good collaborator, it does require internalizing it. +- The code of conduct is proactive, positive. +- A collaborator must be a good example of being kind. + - It should not be possible to find significant fault in a collaborator against the simple principles in the code of conduct. + +## Internalize the Vision + +- Understand and use the project's [vision](./VISION.md) when helping guide decisions. + +## Review Pull Requests + +- Review pull requests promptly and kindly. Approve CI for new contributors. + - Consider telling a maintainer to add you to the automated review queue. +- Collaborators should feel comfortable approving and merging straightforward changes. Such as documentation, chores, styling, simple refactors, and basic bug fixes. +- When reviewing larger changes, keep in mind the points on [being API caretakers](#api-caretakers). +- When approving a PR, allow time for other collaborators to look before merging. + - Especially if the PR is newer, or has a more significant change. +- When merging, prefer squashing (it adds in the PR number). Touch up the commit message to match our [COMMITS](./COMMITS.md) style. + +## Provide Feedback on Features and Designs + +Collaborators try to participate early and often when new features are proposed. They also actively consider and recommend new features that users may need. + +### API Caretakers + +Collaborators are caretakers of hyper's API. When proposing, discussing, or reviewing changes to hyper, keep the following in mind: + +- Conservative additions + - Conservative APIs reduce breaking changes +- Don't expose internal implementation details + - [Accessors can reveal internal repr](https://seanmonstar.com/micro/20260721-accessors-reveal-internal-repr/) +- hyper-util is a place to explore first + - [Vision: Not quite stable, but utile](./VISION.md#not-quite-stable-but-utile-useful) + - [Roadmap: hyper-util](./ROADMAP.md#hyper-util) +- Unstable features + - Unstable features are not bound by our stability promise. + - We don't just name a crate feature with unstable. + - This has the problem of an intermediary crate enabling it, and someone depending on _that_ crate and not realizing they are accessing unstable hyper features. + - All unstable features require a conditional config flag passed to the Rust compiler. + - For example, `hyper_unstable_ffi`. +- Breaking changes + - These would usually be reserved for a new SemVer major release, such as v2.0, and thus would usually be very rare. + - An exception is fixing a mistake quickly after release of a new feature. + - All breaking changes require approval by a maintainer. + +## Welcome New Contributors + +- Help new contributors find their way around the project and its processes. Follow the guidance on [acknowledging people when triaging](./ISSUES.md#acknowledge). + +## Answer Questions + +- Answer questions in issues or chat when you can, or help find someone who can. See the project's [help channels](../CONTRIBUTING.md#help). + +## Mentor Contributors + +- Help contributors grow toward becoming project members. Issue triaging often provides opportunities for [mentoring](./ISSUES.md#mentoring), but the ideas can be applied to reviews, or when answering questions. diff --git a/docs/GOVERNANCE.md b/docs/GOVERNANCE.md index f4366dba8a..7a1becf07c 100644 --- a/docs/GOVERNANCE.md +++ b/docs/GOVERNANCE.md @@ -71,6 +71,7 @@ How to become: ### Collaborator Collaborators are contributors who have been helping out in a consistent basis. +See the [collaborator guide](./COLLABORATORS.md) for practical guidance on the role. Responsibilities: From 37c9d0c7e018d3c218f1fc0e472207ff05ef3772 Mon Sep 17 00:00:00 2001 From: tangaac Date: Wed, 26 Aug 2026 20:22:52 +0800 Subject: [PATCH 10/57] style(lib): remove lints that have been removed in newer clippy (#4167) The following lints no longer exist in recent clippy versions and cause `unknown lint` errors (promoted to errors by `-D warnings` / `-W unknown-lints`): - clippy::unnecessary_rest_pattern - clippy::rest_pattern_accessible_field - clippy::definition_in_module_root Remove them from the allowed lints list in Cargo.toml. --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 01fff25297..5a02a7215b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -139,11 +139,9 @@ panic = "allow" pattern_type_mismatch = "allow" redundant_closure_for_method_calls = "allow" redundant_else = "allow" -rest_pattern_accessible_field = "allow" # TODO: consider, might catch bugs single_char_lifetime_names = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? trivially_copy_pass_by_ref = "allow" -unnecessary_rest_pattern = "allow" # TODO: simple fix unnecessary_trailing_comma = "allow" unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? @@ -160,7 +158,6 @@ clone_on_ref_ptr = "allow" # Arc::clone(blah) is needlessly noisy cognitive_complexity = "allow" # is this ever useful? decimal_literal_representation = "allow" # arguably more human-readable default_numeric_fallback = "allow" # too many false positives -definition_in_module_root = "allow" # this is normal expect_used = "allow" # expect is self-documenting error_impl_error = "allow" # mod::Error is a fine name field_scoped_visibility_modifiers = "allow" # possibly good idea, noisy for now From 6371cd425017155f7fbecef0e57b218edbe6a93a Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Thu, 27 Aug 2026 12:07:16 -0400 Subject: [PATCH 11/57] v1.11.1 --- CHANGELOG.md | 12 ++++++++++++ Cargo.toml | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b58e22bbf7..309188a22f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ +### v1.11.1 (2026-08-27) + + +#### Bug Fixes + +* **http1:** + * detect TE: trailers caselessly and with other values (#4152) ([72fcf545](https://github.com/hyperium/hyper/commit/72fcf545c9b4a261f31d89bafd3019a2d4e9e1d7)) + * recognize `\n\r\n` as a head terminator in the partial-read fast path (#4147) ([3534d75c](https://github.com/hyperium/hyper/commit/3534d75c898da17b023e6bff9b4cf71afed123ad), closes [#4145](https://github.com/hyperium/hyper/issues/4145)) + * flush bytes buffered by the write re-check before yielding (#4143) ([f660f5bf](https://github.com/hyperium/hyper/commit/f660f5bf6eed3fe793f899507ff5bb9e266d4b0d)) + * evict pooled conn on request-side Connection: close (#4110) ([da19be08](https://github.com/hyperium/hyper/commit/da19be08f34d3da01c4c0f9cfe2837e7d377f73d)) + + ## v1.11.0 (2026-07-20) diff --git a/Cargo.toml b/Cargo.toml index 5a02a7215b..d9e081d506 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hyper" -version = "1.11.0" +version = "1.11.1" description = "A protective and efficient HTTP library for all." readme = "README.md" homepage = "https://hyper.rs" From 3b6b7006bb907d18c0760dce2b508588b9bf191d Mon Sep 17 00:00:00 2001 From: Vaibhav Srivastava Date: Sat, 29 Aug 2026 19:57:19 +0530 Subject: [PATCH 12/57] docs(security): fix typo (#4177) --- SECURITY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SECURITY.md b/SECURITY.md index aa5ac773bf..056e82a20d 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -1,6 +1,6 @@ # Security Policy -hyper (and related projects in hyperium) take security seriously, and greatly appreciate responsibile disclosure. +hyper (and related projects in hyperium) take security seriously, and greatly appreciate responsible disclosure. ## Report a security issue From 7611e61209123a558abac2923c8aa6372fa04935 Mon Sep 17 00:00:00 2001 From: dox4 <61588545+dox4@users.noreply.github.com> Date: Sun, 30 Aug 2026 00:12:04 +0800 Subject: [PATCH 13/57] style(lib): replace single-character lifetime names with descriptive names (#4173) --- Cargo.toml | 1 - src/client/dispatch.rs | 2 +- src/common/buf.rs | 2 +- src/ext/informational.rs | 2 +- src/ext/mod.rs | 10 +++++----- src/ffi/task.rs | 6 ++++-- src/proto/h1/decode.rs | 12 ++++++------ src/proto/h1/dispatch.rs | 6 +++--- src/proto/h1/encode.rs | 2 +- src/proto/h1/io.rs | 2 +- src/proto/h1/mod.rs | 14 +++++++------- src/proto/h1/role.rs | 2 +- src/proto/h2/mod.rs | 2 +- src/rt/io.rs | 8 ++++---- tests/server.rs | 4 ++-- 15 files changed, 38 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d9e081d506..b50b8d6c13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -139,7 +139,6 @@ panic = "allow" pattern_type_mismatch = "allow" redundant_closure_for_method_calls = "allow" redundant_else = "allow" -single_char_lifetime_names = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? trivially_copy_pass_by_ref = "allow" unnecessary_trailing_comma = "allow" diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 69c9b8cdaa..a9375f627a 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -408,7 +408,7 @@ mod tests { } /// Helper to check if the future is ready after polling once. - struct PollOnce<'a, F>(&'a mut F); + struct PollOnce<'fut, F>(&'fut mut F); impl Future for PollOnce<'_, F> where diff --git a/src/common/buf.rs b/src/common/buf.rs index d00071551b..0bf02497f4 100644 --- a/src/common/buf.rs +++ b/src/common/buf.rs @@ -56,7 +56,7 @@ impl Buf for BufList { } #[inline] - fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize { + fn chunks_vectored<'data>(&'data self, dst: &mut [IoSlice<'data>]) -> usize { if dst.is_empty() { return 0; } diff --git a/src/ext/informational.rs b/src/ext/informational.rs index e728580fa5..6c86b4974e 100644 --- a/src/ext/informational.rs +++ b/src/ext/informational.rs @@ -66,7 +66,7 @@ where // being either a real reference, or moving the http::Response into the closure, // in a backwards-compatible change in the future. #[derive(Debug)] -pub struct Response<'a>(&'a http::Response<()>); +pub struct Response<'resp>(&'resp http::Response<()>); impl Response<'_> { #[inline] diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 0fccb5ffec..82f7cd7d07 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -113,8 +113,8 @@ impl Protocol { } #[cfg(feature = "http2")] -impl<'a> From<&'a str> for Protocol { - fn from(value: &'a str) -> Self { +impl<'proto> From<&'proto str> for Protocol { + fn from(value: &'proto str) -> Self { Self { inner: h2::ext::Protocol::from(value), } @@ -165,10 +165,10 @@ impl HeaderCaseMap { /// Returns a view of all spellings associated with that header name, /// in the order they were found. #[cfg(feature = "client")] - pub(crate) fn get_all<'a>( - &'a self, + pub(crate) fn get_all<'hdr>( + &'hdr self, name: &HeaderName, - ) -> impl Iterator + 'a> + 'a { + ) -> impl Iterator + 'hdr> + 'hdr { self.get_all_internal(name) } diff --git a/src/ffi/task.rs b/src/ffi/task.rs index 9c7fba6a26..b02de525ac 100644 --- a/src/ffi/task.rs +++ b/src/ffi/task.rs @@ -128,7 +128,7 @@ struct TaskFuture { /// its only purpose is to provide access to the waker. See `hyper_waker`. /// /// Corresponding Rust type: -pub struct hyper_context<'a>(Context<'a>); +pub struct hyper_context<'ctx>(Context<'ctx>); /// A waker that is saved and used to waken a pending task. /// @@ -511,7 +511,9 @@ where // ===== impl hyper_context ===== impl hyper_context<'_> { - pub(crate) fn wrap<'a, 'b>(cx: &'a mut Context<'b>) -> &'a mut hyper_context<'b> { + pub(crate) fn wrap<'borrow, 'ctx>( + cx: &'borrow mut Context<'ctx>, + ) -> &'borrow mut hyper_context<'ctx> { // A struct with only one field has the same layout as that field. unsafe { std::mem::transmute::<&mut Context<'_>, &mut hyper_context<'_>>(cx) } } diff --git a/src/proto/h1/decode.rs b/src/proto/h1/decode.rs index bdfdf79abf..65adaa784b 100644 --- a/src/proto/h1/decode.rs +++ b/src/proto/h1/decode.rs @@ -286,12 +286,12 @@ macro_rules! put_u8 { }; } -struct StepArgs<'a> { - chunk_size: &'a mut u64, - chunk_buf: &'a mut Option, - extensions_cnt: &'a mut u64, - trailers_buf: &'a mut Option, - trailers_cnt: &'a mut usize, +struct StepArgs<'args> { + chunk_size: &'args mut u64, + chunk_buf: &'args mut Option, + extensions_cnt: &'args mut u64, + trailers_buf: &'args mut Option, + trailers_cnt: &'args mut usize, max_headers_cnt: usize, max_headers_bytes: usize, } diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index f250f55863..d98aebcf1f 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -509,10 +509,10 @@ where /// A drop guard to allow a mutable borrow of an Option while being able to /// set whether the `Option` should be cleared on drop. -struct OptGuard<'a, T>(Pin<&'a mut Option>, bool); +struct OptGuard<'opt, T>(Pin<&'opt mut Option>, bool); -impl<'a, T> OptGuard<'a, T> { - fn new(pin: Pin<&'a mut Option>) -> Self { +impl<'opt, T> OptGuard<'opt, T> { + fn new(pin: Pin<&'opt mut Option>) -> Self { OptGuard(pin, false) } diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index a1eebafa12..266b4dd6dd 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -317,7 +317,7 @@ where } #[inline] - fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize { + fn chunks_vectored<'data>(&'data self, dst: &mut [IoSlice<'data>]) -> usize { match &self.kind { BufKind::Exact(b) => b.chunks_vectored(dst), BufKind::Limited(b) => b.chunks_vectored(dst), diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index 6b3f10fbae..b49e48e5c8 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -637,7 +637,7 @@ impl Buf for WriteBuf { } #[inline] - fn chunks_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize { + fn chunks_vectored<'data>(&'data self, dst: &mut [IoSlice<'data>]) -> usize { let n = self.headers.chunks_vectored(dst); self.queue.chunks_vectored(&mut dst[n..]) + n } diff --git a/src/proto/h1/mod.rs b/src/proto/h1/mod.rs index 0a442c4c7e..a17dbae83c 100644 --- a/src/proto/h1/mod.rs +++ b/src/proto/h1/mod.rs @@ -68,9 +68,9 @@ pub(crate) struct ParsedMessage { wants_upgrade: bool, } -pub(crate) struct ParseContext<'a> { - cached_headers: &'a mut Option, - req_method: &'a mut Option, +pub(crate) struct ParseContext<'ctx> { + cached_headers: &'ctx mut Option, + req_method: &'ctx mut Option, h1_parser_config: ParserConfig, h1_max_headers: Option, preserve_header_case: bool, @@ -78,16 +78,16 @@ pub(crate) struct ParseContext<'a> { preserve_header_order: bool, h09_responses: bool, #[cfg(feature = "client")] - on_informational: &'a mut Option, + on_informational: &'ctx mut Option, } /// Passed to `Http1Transaction::encode`. -pub(crate) struct Encode<'a, T> { - head: &'a mut MessageHead, +pub(crate) struct Encode<'encode, T> { + head: &'encode mut MessageHead, body: Option, #[cfg(feature = "server")] keep_alive: bool, - req_method: &'a mut Option, + req_method: &'encode mut Option, title_case_headers: bool, #[cfg(feature = "server")] date_header: bool, diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 29bcd44b0d..d083d2a912 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1652,7 +1652,7 @@ fn write_headers_original_case( } #[cfg(feature = "client")] -struct FastWrite<'a>(&'a mut Vec); +struct FastWrite<'data>(&'data mut Vec); #[cfg(feature = "client")] impl fmt::Write for FastWrite<'_> { diff --git a/src/proto/h2/mod.rs b/src/proto/h2/mod.rs index f0db9b64eb..393d4179ab 100644 --- a/src/proto/h2/mod.rs +++ b/src/proto/h2/mod.rs @@ -319,7 +319,7 @@ impl Buf for SendBuf { } } - fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { + fn chunks_vectored<'data>(&'data self, dst: &mut [IoSlice<'data>]) -> usize { match self { Self::Buf(b) => b.chunks_vectored(dst), Self::Cursor(c) => c.chunks_vectored(dst), diff --git a/src/rt/io.rs b/src/rt/io.rs index 447774c6c0..9e633e69e8 100644 --- a/src/rt/io.rs +++ b/src/rt/io.rs @@ -164,8 +164,8 @@ pub trait Write { /// It is undefined behavior to de-initialize any bytes from the uninitialized /// region, since it is merely unknown whether this region is uninitialized or /// not, and if part of it turns out to be initialized, it must stay initialized. -pub struct ReadBuf<'a> { - raw: &'a mut [MaybeUninit], +pub struct ReadBuf<'data> { + raw: &'data mut [MaybeUninit], filled: usize, init: usize, } @@ -227,8 +227,8 @@ pub struct ReadBuf<'a> { /// assert_eq!(read_buf.filled(), b"hello"); /// ``` #[derive(Debug)] -pub struct ReadBufCursor<'a> { - buf: &'a mut ReadBuf<'a>, +pub struct ReadBufCursor<'buf> { + buf: &'buf mut ReadBuf<'buf>, } impl<'data> ReadBuf<'data> { diff --git a/tests/server.rs b/tests/server.rs index 1843610e88..098ed7a6ee 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -3542,8 +3542,8 @@ impl Serve { type BoxError = Box; type BoxFuture = Pin, BoxError>> + Send>>; -struct ReplyBuilder<'a> { - tx: &'a Mutex>, +struct ReplyBuilder<'mtx> { + tx: &'mtx Mutex>, } impl ReplyBuilder<'_> { From 203e4c7f5d64ecfed41a97bba2836c6e32511c2b Mon Sep 17 00:00:00 2001 From: Nakaryo <128471892+nakaryo716@users.noreply.github.com> Date: Mon, 31 Aug 2026 18:29:17 +0900 Subject: [PATCH 14/57] style(lib): resolve collapsible_match lint (#4178) --- Cargo.toml | 1 - src/proto/h1/conn.rs | 8 +++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b50b8d6c13..67594976d8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -118,7 +118,6 @@ as_conversions = "allow" # TODO: tricky cast_possible_truncation = "allow" # TODO: consider cast_precision_loss = "allow" # TODO: consider checked_conversions = "allow" -collapsible_match = "allow" else_if_without_else = "allow" enum_glob_use = "allow" float_arithmetic = "allow" diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 19c1283df7..593d3d9eda 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -676,11 +676,9 @@ where Version::HTTP_10 => self.state.disable_keep_alive(), // If response is version 1.1 and keep-alive is wanted, add // Connection: keep-alive header when not present - Version::HTTP_11 => { - if self.state.wants_keep_alive() { - head.headers - .insert(CONNECTION, HeaderValue::from_static("keep-alive")); - } + Version::HTTP_11 if self.state.wants_keep_alive() => { + head.headers + .insert(CONNECTION, HeaderValue::from_static("keep-alive")); } _ => (), } From 4f36f33a28709f93d693bc9f942b93b54d7032a4 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 1 Sep 2026 16:47:00 -0400 Subject: [PATCH 15/57] docs(hips): hyper improvement proposals (#4131) --- docs/hips/0000-template.md | 41 ++++++ docs/hips/0001-hyper-improvement-proposals.md | 130 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 docs/hips/0000-template.md create mode 100644 docs/hips/0001-hyper-improvement-proposals.md diff --git a/docs/hips/0000-template.md b/docs/hips/0000-template.md new file mode 100644 index 0000000000..88c469754d --- /dev/null +++ b/docs/hips/0000-template.md @@ -0,0 +1,41 @@ +# HIP-0000: Template + +- Authors: +- Created: (date) +- PR: (link to pull request) + +## Summary + +(The tl;dr. Concise statement of the reason for the document and recommendation.) + +## Tenets + +(List of tenets, values, guiding principles that are used to evaluate the proposal.) + +## Motivation + +(Clearly explain the problem.) + +## Recommendation + +### User Experience + +### Implementation + +### Testing Plan + +(Optional: are there any special considerations for testing required?) + +### Security Considerations + +(What security risks might exist for the recommendation? Can we reduce them? What should users consider when using it?) + +## Alternatives + +(List alternatives considered, and especially why they were not recommended. This portion of "documenting the why" helps Future Us to see if anything has changed since last considered.) + +## Unresolved Questions + + +## References + diff --git a/docs/hips/0001-hyper-improvement-proposals.md b/docs/hips/0001-hyper-improvement-proposals.md new file mode 100644 index 0000000000..03a4bfc42f --- /dev/null +++ b/docs/hips/0001-hyper-improvement-proposals.md @@ -0,0 +1,130 @@ +# HIP-0001: hyper Improvement Proposals + +- Authors: seanmonstar +- Created: 2026-09-01 +- PR: https://github.com/hyperium/hyper/pull/4131 + +## Summary + +HIPs establish a predictable, written‑first process for proposing and evaluating significant changes to hyper. They provide an authoritative place to articulate the problem, the design rationale, and the tradeoffs so contributors can make informed, durable decisions. + +## Tenets + +- Open: the decision-making process should be out in the open, for all to see and any to participate. +- Understandable: Code and changelogs are not the complete theory. Others should be able to understand what we meant. +- Writing is thinking: while the written artifact is extremely valuable, the process of creating is thinking itself. It refines our thought, sharpens it, makes it better. +- Document the why: just as important is understanding why we thought that way. This allows evaluating when the decision should be reconsidered. + +## Motivation + +hyper's existing contribution process does not adequately support proposals for large features and design changes. + +Much of the shape of hyper can be seen in the [VISION](../VISION.md) and [1.0 ROADMAP](../ROADMAP-1.0.md). Adding new small features is very easy. However, designing large new systems in hyper must be handled with care. hyper is deployed at massive scale. The stability promise requires we be deliberate with what API to expose. + +The current process is to ask people to write up larger designs in an issue. GitHub issues are terrible places to discuss multiple points at once. Or people submit massive pull requests, and the discussion is sprinkled through the code reviews. + +It's extremely difficult for reviewers to be sure they understand the design, and to ensure that all critical aspects have been considered. If an implementation is denied for a desirable feature, new pull requests tend to be submitted which don't address the previous concerns. The barrier for someone to contribute goes up due to the lack of structure. And the review process slows down as reviewers struggle to continuously rebuild mental context. + +## Recommendation + +Introduce a structured, written process for proposing, discussing, and deciding large design changes. We call these **hyper Improvement Proposals** (HIPs). They are similar in spirit to [RFCs](https://en.wikipedia.org/wiki/Request_for_Comments). + +### Scope + +When should HIPs be used? + +HIPs are intended for changes that significantly affect hyper's architecture, public API, performance characteristics, or long‑term maintenance burden. They are useful when a decision is difficult to reverse, when there appear to be several strong options, or as a foundation for future proposals. + +When are they not needed? + +Small, localized changes that do not alter core design or guarantees generally do not require a HIP. + +### Content + +What should be included in a HIP? + +This defines at a high level the purpose of what content is included. The exact sections might change over time, and so are defined in a separate `0000-template.md` file. + +But in general, HIPs should provide the following information: + +- Objective - the problem, the motivation, the what are we even doing here. +- Tenets - how to measure solutions + - A meta point here: spend more time on the objective, tenets, and alternatives. The recommendation will fall into place. +- A single recommendation + - The job of a proposal is to propose a solution, not to propose a problem. + - There will often be several ways to solve anything. + - The proposal should research and measure the options using the facts and tenets, and decide which is the best. + - The discussion during the proposal period may change what is the recommended solution. + - The essence of those discussions should be recaptured into the proposal itself. Both in the recommendation section, and also potentially in an FAQ appendix. + - Future readers should not need to reconstruct the design from the review comments. +- The whys (and why nots) of the solution + - A proposal should always include why the recommendation is the best option, and why the other options were not chosen +- References + - Links to all the relevant research that was done + - Links to previous discussions +- Appendices + - FAQ + +#### What not to include + +- Minor details that do not need to be discussed to accept the proposal. + +### Lifecycle + +- Start with an issue. + - (A HIP should almost never be started without a previous discussion) +- If the feature request seems large enough, a collaborator may ask for a HIP. +- In a new branch, copy the template file to `0000-your-feature-name.md`. +- Fill out the sections based on the instructions in the template. + - You don't need to have fully polished prose in every section to submit for discussion. + - It can even be beneficial to submit initially with bullet points. + - Balance giving proper thought to the sections, with moving quickly to get appropriate feedback. +- Consider sharing an early draft with someone else first. +- Submit a pull request, and include a big reminder to prefer line comments over top-level PR comments. +- As discussion occurs, revise the sections. + - Consider adding to an FAQ appendix. +- Eventually, a [maintainer](../GOVERNANCE.md#maintainer) will decide whether the proposal is accepted or rejected. + +Once a proposal is accepted, a number will be assigned. The file in the PR can be renamed with the assigned number, and then merged into trunk. + +#### Rejection + +If a proposal is rejected or withdrawn, its pull request is closed without assigning a HIP number or merging the document. The pull request remains the record of that discussion. If the discussion reveals a generally applicable constraint or principle, that information should instead be incorporated into the appropriate project documentation. + +#### Mutability and History + +HIPs are mutable, even after being accepted. + +Significant changes can be added to a `## History` section of the document, inserted before any `## References` or appendices. Besides explaining what changed, the reasoning for _why_ should be included. + +## Alternatives + +- Ad-hoc design docs + - While design documents are an ideal artifact, without defining a process behind them, it's hard for contributors to know when to use one or how. +- Module documentation + - A reader of module documentation is not the right audience. + - The purpose of module documentation is to explain how to use the library. + - It is not a great place to document the why. +- Issues and Pull Requests + - This is the status quo. + - Discussion in issues is a horrible experience. + - Spreads out the full design across disconnected areas. + - Silos the knowledge directly in GitHub. +- A chat application, such as Zulip + - Chat-style messages are extremely poor for having thoughtful discussion and decisions. + - They become littered with messages that have no longer-term value, and it makes it hard to piece back together what a decision is. + - No contributor should ever _have_ to participate in chat-style messaging. + +## Unresolved Questions + +- Is there a point when significant changes to an accepted HIP should itself be a new HIP? + - Possibly, but I'm not sure it's worth deciding yet. + +## References + +- https://rust-lang.github.io/rfcs/0002-rfc-process.html +- https://rfd.shared.oxide.computer/rfd/0001 +- https://peps.python.org/pep-0001/ +- https://docs.jj-vcs.dev/latest/design_docs/ +- https://blog.ceejbot.com/posts/design-docs/ +- https://adr.github.io/ From a53292651b2ab7d8766e201cceaba63bab8b9f04 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 8 Sep 2026 08:57:15 -0400 Subject: [PATCH 16/57] perf(body): simpler custom Incoming channel (#4168) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous channel variant for `Incoming` used several individual channels to handle the various mechanisms: sending data, signaling error/abort, sending trailers, waiting for optional expect-101 polling. Each individual channel would allocate its own internal shared state in an Arc. Now the `Incoming` channel variant uses a single combined implementation, putting all the required shared pieces in a single Arc. This resulted in less allocations, and less places to potentially store wakers. The following performance improvements were noticed with microbenchmarks: | Benchmark | Before | After | Improvement | | --------------- | ------ | ---------- | ----------- | | Create/drop | 400 ns | 130–139 ns | ~66% | | Data handoff | 229 ns | 147–155 ns | ~34% | | Expect-101 wait | 492 ns | 267–298 ns | ~40–46% | --- src/body/chan.rs | 264 +++++++++++++++++++++++++++++++++++++++++++ src/body/incoming.rs | 237 ++++++++++---------------------------- src/body/mod.rs | 2 + src/common/mod.rs | 2 - src/common/watch.rs | 73 ------------ 5 files changed, 327 insertions(+), 251 deletions(-) create mode 100644 src/body/chan.rs delete mode 100644 src/common/watch.rs diff --git a/src/body/chan.rs b/src/body/chan.rs new file mode 100644 index 0000000000..b45bea866b --- /dev/null +++ b/src/body/chan.rs @@ -0,0 +1,264 @@ +use std::fmt; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll}; + +use atomic_waker::AtomicWaker; +use bytes::Bytes; +use http::HeaderMap; + +use crate::common::lock::LockResultExt; + +pub(crate) fn channel(wanter: bool) -> (Sender, Receiver) { + let shared = Arc::new(Shared { + state: Mutex::new(State { + item: None, + pending_error: None, + trailers: None, + sender_open: true, + receiver_open: true, + want: !wanter, + }), + sender_waker: AtomicWaker::new(), + receiver_waker: AtomicWaker::new(), + }); + + ( + Sender { + shared: Arc::clone(&shared), + trailers_sent: false, + }, + Receiver { + shared, + terminated: false, + }, + ) +} + +#[must_use = "Sender does nothing unless sent on"] +pub(crate) struct Sender { + shared: Arc, + trailers_sent: bool, +} + +pub(crate) struct Receiver { + shared: Arc, + terminated: bool, +} + +struct Shared { + state: Mutex, + sender_waker: AtomicWaker, + receiver_waker: AtomicWaker, +} + +struct State { + item: Option>, + // An error must not displace data which was already accepted. The old + // mpsc channel achieved this by sending the error from a cloned sender. + pending_error: Option, + trailers: Option, + sender_open: bool, + receiver_open: bool, + want: bool, +} + +impl Sender { + pub(crate) fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.shared.sender_waker.register(cx.waker()); + let state = self.shared.state.lock().panic_if_poisoned(); + if !state.receiver_open { + Poll::Ready(Err(crate::Error::new_closed())) + } else if state.want && state.item.is_none() && state.pending_error.is_none() { + Poll::Ready(Ok(())) + } else { + Poll::Pending + } + } + + #[cfg(test)] + pub(crate) async fn ready(&mut self) -> crate::Result<()> { + futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await + } + + #[cfg(test)] + #[allow(dead_code)] + pub(crate) async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> { + self.ready().await?; + self.try_send_data(chunk) + .map_err(|_| crate::Error::new_closed()) + } + + pub(crate) fn try_send_data(&mut self, chunk: Bytes) -> Result<(), Bytes> { + let mut state = self.shared.state.lock().panic_if_poisoned(); + if !state.receiver_open + || !state.want + || state.item.is_some() + || state.pending_error.is_some() + { + return Err(chunk); + } + state.item = Some(Ok(chunk)); + drop(state); + self.shared.receiver_waker.wake(); + Ok(()) + } + + pub(crate) fn try_send_trailers( + &mut self, + trailers: HeaderMap, + ) -> Result<(), Option> { + if self.trailers_sent { + return Err(None); + } + self.trailers_sent = true; + + let mut state = self.shared.state.lock().panic_if_poisoned(); + if !state.receiver_open { + return Err(Some(trailers)); + } + state.trailers = Some(trailers); + drop(state); + self.shared.receiver_waker.wake(); + Ok(()) + } + + #[allow(dead_code)] + #[allow(clippy::unused_async_trait_impl)] + pub(crate) async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> { + self.try_send_trailers(trailers) + .map_err(|_| crate::Error::new_closed()) + } + + pub(crate) fn send_error(&mut self, err: crate::Error) { + let mut state = self.shared.state.lock().panic_if_poisoned(); + if !state.receiver_open { + return; + } + if state.item.is_none() { + state.item = Some(Err(err)); + } else if state.pending_error.is_none() { + state.pending_error = Some(err); + } + drop(state); + self.shared.receiver_waker.wake(); + } + + #[cfg(test)] + pub(crate) fn abort(mut self) { + self.send_error(crate::Error::new_body_write_aborted()); + } + + fn is_closed(&self) -> bool { + !self.shared.state.lock().panic_if_poisoned().receiver_open + } +} + +impl Receiver { + pub(crate) fn poll_next( + &mut self, + cx: &mut Context<'_>, + ) -> Poll>> { + if self.terminated { + return Poll::Ready(None); + } + + self.shared.receiver_waker.register(cx.waker()); + let mut state = self.shared.state.lock().panic_if_poisoned(); + let wake_sender = if !state.want { + state.want = true; + true + } else { + false + }; + if let Some(item) = state.item.take() { + drop(state); + self.shared.sender_waker.wake(); + return Poll::Ready(Some(item)); + } + if let Some(err) = state.pending_error.take() { + drop(state); + if wake_sender { + self.shared.sender_waker.wake(); + } + return Poll::Ready(Some(Err(err))); + } + let sender_open = state.sender_open; + drop(state); + if wake_sender { + self.shared.sender_waker.wake(); + } + if sender_open { + Poll::Pending + } else { + self.terminated = true; + Poll::Ready(None) + } + } + + pub(crate) fn take_trailers(&mut self) -> Option { + debug_assert!(self.terminated, "data channel still open before trailers"); + let mut state = self.shared.state.lock().panic_if_poisoned(); + state.trailers.take() + } +} + +impl Drop for Sender { + fn drop(&mut self) { + let mut state = self.shared.state.lock().panic_if_poisoned(); + state.sender_open = false; + drop(state); + self.shared.receiver_waker.wake(); + } +} + +impl Drop for Receiver { + fn drop(&mut self) { + let mut state = self.shared.state.lock().panic_if_poisoned(); + state.receiver_open = false; + drop(state); + self.shared.sender_waker.wake(); + } +} + +impl fmt::Debug for Sender { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.is_closed() { + f.debug_tuple("Sender").field(&"Closed").finish() + } else { + f.debug_tuple("Sender").field(&"Open").finish() + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + async fn recv(rx: &mut Receiver) -> Option> { + futures_util::future::poll_fn(|cx| rx.poll_next(cx)).await + } + + #[tokio::test] + async fn error_queued_behind_accepted_data() { + let (mut tx, mut rx) = channel(false); + tx.try_send_data(Bytes::from_static(b"data")).unwrap(); + tx.send_error(crate::Error::new_incomplete()); + drop(tx); + + assert_eq!(recv(&mut rx).await.unwrap().unwrap(), "data"); + assert!(recv(&mut rx).await.unwrap().is_err()); + assert!(recv(&mut rx).await.is_none()); + } + + #[tokio::test] + async fn trailers_follow_data_close() { + let (mut tx, mut rx) = channel(false); + let mut trailers = HeaderMap::new(); + trailers.insert("x-trailer", "value".parse().unwrap()); + tx.try_send_trailers(trailers).unwrap(); + drop(tx); + + assert!(recv(&mut rx).await.is_none()); + assert_eq!(rx.take_trailers().unwrap()["x-trailer"], "value"); + } +} diff --git a/src/body/incoming.rs b/src/body/incoming.rs index 6ef6c0963a..b026a24d6b 100644 --- a/src/body/incoming.rs +++ b/src/body/incoming.rs @@ -1,38 +1,25 @@ use std::fmt; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -use std::future::Future; use std::pin::Pin; use std::task::{Context, Poll}; use bytes::Bytes; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -use futures_channel::{mpsc, oneshot}; #[cfg(all( any(feature = "http1", feature = "http2"), any(feature = "client", feature = "server") ))] use futures_core::ready; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -use futures_core::{stream::FusedStream, Stream}; // for mpsc::Receiver -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -use http::HeaderMap; use http_body::{Body, Frame, SizeHint}; +#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] +use super::chan; #[cfg(all( any(feature = "http1", feature = "http2"), any(feature = "client", feature = "server") ))] use super::DecodedLength; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -use crate::common::watch; #[cfg(all(feature = "http2", any(feature = "client", feature = "server")))] use crate::proto::h2::ping; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -type BodySender = mpsc::Sender>; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -type TrailersSender = oneshot::Sender; - /// A stream of `Bytes`, used when receiving bodies from the network. /// /// Note that Users should not instantiate this struct directly. When working with the hyper client, @@ -58,9 +45,7 @@ enum Kind { #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] Chan { content_length: DecodedLength, - want_tx: watch::Sender, - data_rx: mpsc::Receiver>, - trailers_rx: oneshot::Receiver, + rx: chan::Receiver, }, #[cfg(all(feature = "http2", any(feature = "client", feature = "server")))] H2 { @@ -86,18 +71,8 @@ enum Kind { /// /// [`Body::channel()`]: struct.Body.html#method.channel /// [`Sender::abort()`]: struct.Sender.html#method.abort -#[must_use = "Sender does nothing unless sent on"] #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -pub(crate) struct Sender { - want_rx: watch::Receiver, - data_tx: BodySender, - trailers_tx: Option, -} - -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -const WANT_PENDING: usize = 1; -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -const WANT_READY: usize = 2; +pub(crate) use super::chan::Sender; impl Incoming { /// Create a `Body` stream with an associated sender half. @@ -112,26 +87,8 @@ impl Incoming { #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] pub(crate) fn new_channel(content_length: DecodedLength, wanter: bool) -> (Sender, Incoming) { - let (data_tx, data_rx) = mpsc::channel(0); - let (trailers_tx, trailers_rx) = oneshot::channel(); - - // If wanter is true, `Sender::poll_ready()` won't becoming ready - // until the `Body` has been polled for data once. - let want = if wanter { WANT_PENDING } else { WANT_READY }; - - let (want_tx, want_rx) = watch::channel(want); - - let tx = Sender { - want_rx, - data_tx, - trailers_tx: Some(trailers_tx), - }; - let rx = Incoming::new(Kind::Chan { - content_length, - want_tx, - data_rx, - trailers_rx, - }); + let (tx, rx) = chan::channel(wanter); + let rx = Incoming::new(Kind::Chan { content_length, rx }); (tx, rx) } @@ -210,24 +167,13 @@ impl Body for Incoming { #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] Kind::Chan { content_length: len, - data_rx, - want_tx, - trailers_rx, + rx, } => { - want_tx.send(WANT_READY); - - if !data_rx.is_terminated() { - if let Some(chunk) = ready!(Pin::new(data_rx).poll_next(cx)?) { - len.sub_if(chunk.len() as u64); - return Poll::Ready(Some(Ok(Frame::data(chunk)))); - } - } - - // check trailers after data is terminated - match ready!(Pin::new(trailers_rx).poll(cx)) { - Ok(t) => Poll::Ready(Some(Ok(Frame::trailers(t)))), - Err(_) => Poll::Ready(None), + if let Some(chunk) = ready!(rx.poll_next(cx)?) { + len.sub_if(chunk.len() as u64); + return Poll::Ready(Some(Ok(Frame::data(chunk)))); } + Poll::Ready(rx.take_trailers().map(Frame::trailers).map(Ok)) } #[cfg(all(feature = "http2", any(feature = "client", feature = "server")))] Kind::H2 { @@ -353,120 +299,12 @@ impl fmt::Debug for Incoming { } } -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -impl Sender { - /// Check to see if this `Sender` can send more data. - pub(crate) fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { - // Check if the receiver end has tried polling for the body yet - ready!(self.poll_want(cx)?); - self.data_tx - .poll_ready(cx) - .map_err(|_| crate::Error::new_closed()) - } - - fn poll_want(&mut self, cx: &mut Context<'_>) -> Poll> { - match self.want_rx.load(cx) { - WANT_READY => Poll::Ready(Ok(())), - WANT_PENDING => Poll::Pending, - watch::CLOSED => Poll::Ready(Err(crate::Error::new_closed())), - unexpected => unreachable!("want_rx value: {}", unexpected), - } - } - - #[cfg(test)] - async fn ready(&mut self) -> crate::Result<()> { - futures_util::future::poll_fn(|cx| self.poll_ready(cx)).await - } - - /// Send data on data channel when it is ready. - #[cfg(test)] - #[allow(unused)] - pub(crate) async fn send_data(&mut self, chunk: Bytes) -> crate::Result<()> { - self.ready().await?; - self.data_tx - .try_send(Ok(chunk)) - .map_err(|_| crate::Error::new_closed()) - } - - /// Send trailers on trailers channel. - #[allow(unused)] - #[allow(clippy::unused_async_trait_impl)] - pub(crate) async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> { - let tx = match self.trailers_tx.take() { - Some(tx) => tx, - None => return Err(crate::Error::new_closed()), - }; - tx.send(trailers).map_err(|_| crate::Error::new_closed()) - } - - /// Try to send data on this channel. - /// - /// # Errors - /// - /// Returns `Err(Bytes)` if the channel could not (currently) accept - /// another `Bytes`. - /// - /// # Note - /// - /// This is mostly useful for when trying to send from some other thread - /// that doesn't have an async context. If in an async context, prefer - /// `send_data()` instead. - #[cfg(feature = "http1")] - pub(crate) fn try_send_data(&mut self, chunk: Bytes) -> Result<(), Bytes> { - self.data_tx - .try_send(Ok(chunk)) - .map_err(|err| err.into_inner().expect("just sent Ok")) - } - - #[cfg(feature = "http1")] - pub(crate) fn try_send_trailers( - &mut self, - trailers: HeaderMap, - ) -> Result<(), Option> { - let tx = match self.trailers_tx.take() { - Some(tx) => tx, - None => return Err(None), - }; - - tx.send(trailers).map_err(Some) - } - - #[cfg(test)] - pub(crate) fn abort(mut self) { - self.send_error(crate::Error::new_body_write_aborted()); - } - - pub(crate) fn send_error(&mut self, err: crate::Error) { - let _ = self - .data_tx - // clone so the send works even if buffer is full - .clone() - .try_send(Err(err)); - } -} - -#[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] -impl fmt::Debug for Sender { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - #[derive(Debug)] - struct Open; - #[derive(Debug)] - struct Closed; - - let mut builder = f.debug_tuple("Sender"); - match self.want_rx.peek() { - watch::CLOSED => builder.field(&Closed), - _ => builder.field(&Open), - }; - - builder.finish() - } -} - #[cfg(test)] mod tests { #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] use std::mem; + #[cfg(all(feature = "nightly", not(miri)))] + use std::pin::Pin; #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] use std::task::Poll; @@ -474,6 +312,8 @@ mod tests { use super::{Body, Incoming, SizeHint}; #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] use super::{DecodedLength, Sender}; + #[cfg(all(feature = "nightly", not(miri)))] + use bytes::Bytes; #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] use http_body_util::BodyExt; @@ -494,7 +334,7 @@ mod tests { assert_eq!( mem::size_of::(), - mem::size_of::() * 5, + mem::size_of::() * 2, "Sender" ); @@ -505,6 +345,51 @@ mod tests { ); } + #[cfg(all(feature = "nightly", not(miri)))] + #[bench] + fn bench_channel_create_and_drop(b: &mut test::Bencher) { + b.iter(|| { + let _ = test::black_box(Incoming::new_channel( + DecodedLength::CHUNKED, + /* wanter = */ false, + )); + }); + } + + #[cfg(all(feature = "nightly", not(miri)))] + #[bench] + fn bench_channel_data_handoff(b: &mut test::Bencher) { + let (mut tx, mut body) = + Incoming::new_channel(DecodedLength::CHUNKED, /* wanter = */ false); + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + + b.iter(|| { + assert!(tx.poll_ready(&mut cx).is_ready()); + tx.try_send_data(Bytes::from_static(b"hello world")) + .unwrap(); + let frame = match Pin::new(&mut body).poll_frame(&mut cx) { + Poll::Ready(Some(Ok(frame))) => frame, + unexpected => panic!("unexpected body poll: {unexpected:?}"), + }; + test::black_box(frame); + }); + } + + #[cfg(all(feature = "nightly", not(miri)))] + #[bench] + fn bench_channel_want_transition(b: &mut test::Bencher) { + let mut cx = std::task::Context::from_waker(std::task::Waker::noop()); + + b.iter(|| { + let (mut tx, mut body) = + Incoming::new_channel(DecodedLength::CHUNKED, /* wanter = */ true); + assert!(tx.poll_ready(&mut cx).is_pending()); + assert!(Pin::new(&mut body).poll_frame(&mut cx).is_pending()); + assert!(tx.poll_ready(&mut cx).is_ready()); + let _ = test::black_box((tx, body)); + }); + } + #[cfg(all(feature = "http1", any(feature = "client", feature = "server")))] #[test] fn size_hint() { diff --git a/src/body/mod.rs b/src/body/mod.rs index c34d019d21..42678b63d1 100644 --- a/src/body/mod.rs +++ b/src/body/mod.rs @@ -79,6 +79,8 @@ pub(crate) use self::incoming::Sender; ))] pub(crate) use self::length::DecodedLength; +#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] +mod chan; mod incoming; #[cfg(all( any(feature = "http1", feature = "http2"), diff --git a/src/common/mod.rs b/src/common/mod.rs index 5be740b000..da1ad9eafb 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -18,5 +18,3 @@ pub(crate) mod task; all(any(feature = "client", feature = "server"), feature = "http2"), ))] pub(crate) mod time; -#[cfg(all(any(feature = "client", feature = "server"), feature = "http1"))] -pub(crate) mod watch; diff --git a/src/common/watch.rs b/src/common/watch.rs deleted file mode 100644 index 81acd5df70..0000000000 --- a/src/common/watch.rs +++ /dev/null @@ -1,73 +0,0 @@ -//! An SPSC broadcast channel. -//! -//! - The value can only be a `usize`. -//! - The consumer is only notified if the value is different. -//! - The value `0` is reserved for closed. - -use atomic_waker::AtomicWaker; -use std::sync::{ - atomic::{AtomicUsize, Ordering}, - Arc, -}; -use std::task; - -type Value = usize; - -pub(crate) const CLOSED: usize = 0; - -pub(crate) fn channel(initial: Value) -> (Sender, Receiver) { - debug_assert!( - initial != CLOSED, - "watch::channel initial state of 0 is reserved" - ); - - let shared = Arc::new(Shared { - value: AtomicUsize::new(initial), - waker: AtomicWaker::new(), - }); - - ( - Sender { - shared: shared.clone(), - }, - Receiver { shared }, - ) -} - -pub(crate) struct Sender { - shared: Arc, -} - -pub(crate) struct Receiver { - shared: Arc, -} - -struct Shared { - value: AtomicUsize, - waker: AtomicWaker, -} - -impl Sender { - pub(crate) fn send(&mut self, value: Value) { - if self.shared.value.swap(value, Ordering::SeqCst) != value { - self.shared.waker.wake(); - } - } -} - -impl Drop for Sender { - fn drop(&mut self) { - self.send(CLOSED); - } -} - -impl Receiver { - pub(crate) fn load(&mut self, cx: &mut task::Context<'_>) -> Value { - self.shared.waker.register(cx.waker()); - self.shared.value.load(Ordering::SeqCst) - } - - pub(crate) fn peek(&self) -> Value { - self.shared.value.load(Ordering::Relaxed) - } -} From 6612d993a6bcfb6a21a8739b51779a30170402cc Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 9 Sep 2026 08:06:03 -0400 Subject: [PATCH 17/57] docs(maintainers): add 0x676e67 as a collaborator (#4184) --- docs/MAINTAINERS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/MAINTAINERS.md b/docs/MAINTAINERS.md index ab9fe5e3d5..3bd87307a0 100644 --- a/docs/MAINTAINERS.md +++ b/docs/MAINTAINERS.md @@ -14,6 +14,7 @@ To see what these roles do, and how to become one, look at [GOVERNANCE](./GOVERN - dswij (@dswij) - tottoto (@tottoto) - katelyn martin (@cratelyn) +- 0x676e67 (@0x676e67) ### Collaborator emeriti From 270feeb71138f817c47e4c0d54a8cacea92d64f2 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Thu, 10 Sep 2026 14:26:46 -0400 Subject: [PATCH 18/57] docs(ext): remove broken `OriginalHeaderOrder` doctest (#4188) i stumbled across this while investigating a solution to #3914. in #2798, a documentation test was introduced that appears to have been broken since its inception. after spending some time figuring out how to run this documentation test, i found the following errors: ``` ; RUSTDOCFLAGS='--cfg hyper_unstable_ffi' RUSTFLAGS='--cfg hyper_unstable_ffi' cargo test --doc --features client,server,http1,http2,ffi get_in_order Compiling hyper v1.11.1 (/hyper) Finished `test` profile [unoptimized + debuginfo] target(s) in 1.39s Doc-tests hyper running 1 test test src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) - compile ... FAILED failures: ---- src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) stdout ---- error: expected item, found keyword `let` --> src/ext/mod.rs:266:1 | 266 | let mut h_order = OriginalHeaderOrder::default(); | ^^^ `let` cannot be used for global variables | = help: consider using `static` and a `Mutex` instead of `let mut` = note: for a full list of items that can appear in modules, see error: aborting due to 1 previous error Couldn't compile the test. failures: src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) ``` after wrapping the code in the example in a `fn main() {}` block: ``` ; RUSTDOCFLAGS='--cfg hyper_unstable_ffi' RUSTFLAGS='--cfg hyper_unstable_ffi' cargo test --doc --features client,server,http1,http2,ffi get_in_order Compiling hyper v1.11.1 (/hyper) Finished `test` profile [unoptimized + debuginfo] target(s) in 0.37s Doc-tests hyper running 1 test test src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) - compile ... FAILED failures: ---- src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) stdout ---- error: expected `;`, found keyword `let` --> src/ext/mod.rs:284:22 | 284 | h_order.append(name3) | ^ help: add `;` here 285 | 286 | let mut iter = h_order.get_in_order() | --- unexpected token error: expected `;`, found keyword `let` --> src/ext/mod.rs:286:38 | 286 | let mut iter = h_order.get_in_order() | ^ help: add `;` here 287 | 288 | let (name, idx) = iter.next(); | --- unexpected token error[E0603]: struct `OriginalHeaderOrder` is private --> src/ext/mod.rs:264:17 | 264 | use hyper::ext::OriginalHeaderOrder; | ^^^^^^^^^^^^^^^^^^^ private struct | note: the struct `OriginalHeaderOrder` is defined here --> src/ext/mod.rs:204:1 | 204 | pub(crate) struct OriginalHeaderOrder { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0433]: cannot find type `Headermap` in this scope --> src/ext/mod.rs:269:17 | 269 | let mut h_map = Headermap::new(); | ^^^^^^^^^ use of undeclared type `Headermap` | help: a struct with a similar name exists | 269 - let mut h_map = Headermap::new(); 269 + let mut h_map = HeaderMap::new(); | error: aborting due to 4 previous errors Some errors have detailed explanations: E0433, E0603. For more information about an error, try `rustc --explain E0433`. Couldn't compile the test. failures: src/ext/mod.rs - ext::OriginalHeaderOrder::get_in_order (line 262) ``` after addressing these errors, i observed: ``` error[E0277]: the trait bound `&[u8; 10]: IntoHeaderName` is not satisfied --> src/ext/mod.rs:273:14 | 273 | h_map.append(name1); | ------ ^^^^^ the trait `IntoHeaderName` is not implemented for `&[u8; 10]` | | | required by a bound introduced by this call | help: the following other types implement trait `IntoHeaderName` --> /.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/http-1.3.1/src/header/map.rs:3690:5 | 3690 | impl IntoHeaderName for HeaderName {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `HeaderName` ... 3712 | impl<'a> IntoHeaderName for &'a HeaderName {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&'a HeaderName` ... 3734 | impl IntoHeaderName for &'static str {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&'static str` note: required by a bound in `HeaderMap::::append` --> /.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/http-1.3.1/src/header/map.rs:1373:12 | 1371 | pub fn append(&mut self, key: K, value: T) -> bool | ------ required by a bound in this associated function 1372 | where 1373 | K: IntoHeaderName, | ^^^^^^^^^^^^^^ required by this bound in `HeaderMap::::append` error[E0061]: this method takes 2 arguments but 1 argument was supplied --> src/ext/mod.rs:273:7 | 273 | h_map.append(name1); | ^^^^^^------- argument #2 of type `HeaderValue` is missing | note: method defined here --> /.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/http-1.3.1/src/header/map.rs:1371:12 | 1371 | pub fn append(&mut self, key: K, value: T) -> bool | ^^^^^^ help: provide the argument | 273 | h_map.append(name1, /* HeaderValue */); | +++++++++++++++++++ ``` because these errors point to missing arguments and incorrect types, this commit removes this example altogether. i'm not convinced it was ever run. Signed-off-by: katelyn martin --- src/ext/mod.rs | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/src/ext/mod.rs b/src/ext/mod.rs index 82f7cd7d07..ba6886b835 100644 --- a/src/ext/mod.rs +++ b/src/ext/mod.rs @@ -257,41 +257,6 @@ impl OriginalHeaderOrder { // here. /// This returns an iterator that provides header names and indexes /// in the original order received. - /// - /// # Examples - /// ```no_run - /// use hyper::ext::OriginalHeaderOrder; - /// use hyper::header::{HeaderName, HeaderValue, HeaderMap}; - /// - /// let mut h_order = OriginalHeaderOrder::default(); - /// let mut h_map = Headermap::new(); - /// - /// let name1 = b"Set-CookiE"; - /// let value1 = b"a=b"; - /// h_map.append(name1); - /// h_order.append(name1); - /// - /// let name2 = b"Content-Encoding"; - /// let value2 = b"gzip"; - /// h_map.append(name2, value2); - /// h_order.append(name2); - /// - /// let name3 = b"SET-COOKIE"; - /// let value3 = b"c=d"; - /// h_map.append(name3, value3); - /// h_order.append(name3) - /// - /// let mut iter = h_order.get_in_order() - /// - /// let (name, idx) = iter.next(); - /// assert_eq!(b"a=b", h_map.get_all(name).nth(idx).unwrap()); - /// - /// let (name, idx) = iter.next(); - /// assert_eq!(b"gzip", h_map.get_all(name).nth(idx).unwrap()); - /// - /// let (name, idx) = iter.next(); - /// assert_eq!(b"c=d", h_map.get_all(name).nth(idx).unwrap()); - /// ``` pub(crate) fn get_in_order(&self) -> impl Iterator { self.entry_order.iter() } From c9f0165fec729c5b7af862f8960a3fdfc3066572 Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Fri, 11 Sep 2026 05:34:11 +0800 Subject: [PATCH 19/57] chore(dependencies): remove unused futures-channel from http1 feature (#4185) --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 67594976d8..f5446c6629 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,7 +81,7 @@ full = [ ] # HTTP versions -http1 = ["dep:atomic-waker", "dep:futures-channel", "dep:futures-core", "dep:httparse", "dep:itoa"] +http1 = ["dep:atomic-waker", "dep:futures-core", "dep:httparse", "dep:itoa"] http2 = ["dep:atomic-waker", "dep:futures-channel", "dep:futures-core", "dep:h2"] # Client/Server From 83da75a6bdea754643422243c044cba790eb8019 Mon Sep 17 00:00:00 2001 From: tottoto Date: Fri, 11 Sep 2026 17:27:39 +0900 Subject: [PATCH 20/57] chore(ci): update to cargo-check-external-types 0.5.0 (#4189) --- .github/workflows/CI.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a5d35e10ff..5d625ae140 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -298,12 +298,12 @@ jobs: - name: Install Rust uses: dtolnay/rust-toolchain@master with: - toolchain: nightly-2025-10-18 # Compatible version for cargo-check-external-types + toolchain: nightly-2026-03-20 # Compatible version for cargo-check-external-types - name: Install cargo-check-external-types uses: taiki-e/cache-cargo-install-action@v2 with: - tool: cargo-check-external-types@0.4.0 + tool: cargo-check-external-types@0.5.0 - uses: Swatinem/rust-cache@v2 From c6dca2078ce223050dc0832be7c9ab07baa6c4bf Mon Sep 17 00:00:00 2001 From: Nakaryo <128471892+nakaryo716@users.noreply.github.com> Date: Mon, 14 Sep 2026 01:02:20 +0900 Subject: [PATCH 21/57] style(lib): address ignored_unit_patterns lint (#4193) --- Cargo.toml | 1 - src/client/dispatch.rs | 8 ++++---- src/proto/h1/io.rs | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f5446c6629..d6bd75c244 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -121,7 +121,6 @@ checked_conversions = "allow" else_if_without_else = "allow" enum_glob_use = "allow" float_arithmetic = "allow" -ignored_unit_patterns = "allow" indexing_slicing = "allow" integer_division = "allow" integer_division_remainder_used = "allow" diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index a9375f627a..0d91a1d92e 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -111,7 +111,7 @@ impl Sender { let (tx, rx) = oneshot::channel(); self.inner .send(Envelope(Some((val, Callback::Retry(Some(tx)))))) - .map(move |_| rx) + .map(move |()| rx) .map_err(|mut e| (e.0).0.take().expect("envelope not dropped").0) } @@ -123,7 +123,7 @@ impl Sender { let (tx, rx) = oneshot::channel(); self.inner .send(Envelope(Some((val, Callback::NoRetry(Some(tx)))))) - .map(move |_| rx) + .map(move |()| rx) .map_err(|mut e| (e.0).0.take().expect("envelope not dropped").0) } @@ -150,7 +150,7 @@ impl UnboundedSender { let (tx, rx) = oneshot::channel(); self.inner .send(Envelope(Some((val, Callback::Retry(Some(tx)))))) - .map(move |_| rx) + .map(move |()| rx) .map_err(|mut e| (e.0).0.take().expect("envelope not dropped").0) } @@ -158,7 +158,7 @@ impl UnboundedSender { let (tx, rx) = oneshot::channel(); self.inner .send(Envelope(Some((val, Callback::NoRetry(Some(tx)))))) - .map(move |_| rx) + .map(move |()| rx) .map_err(|mut e| (e.0).0.take().expect("envelope not dropped").0) } } diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index b49e48e5c8..386f1be35b 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -240,7 +240,7 @@ where let dst = unsafe { self.read_buf.chunk_mut().as_uninit_slice_mut() }; let mut buf = ReadBuf::uninit(dst); match Pin::new(&mut self.io).poll_read(cx, buf.unfilled()) { - Poll::Ready(Ok(_)) => { + Poll::Ready(Ok(())) => { let n = buf.filled().len(); trace!("received {} bytes", n); // Safety: we just read that many bytes into the From 74a39081ff1496970fb75da75c5282b23a9b69e5 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 15 Sep 2026 12:14:50 -0700 Subject: [PATCH 22/57] fix(http1): preserve hop-by-hop when setting close or keep-alive (#4196) This adjusts https://github.com/hyperium/hyper/pull/4110 to use `append` instead of `insert` when a nonzero number of "connection" headers are already present, and only if none of the existing headers already include the value being added. Fixes https://github.com/hyperium/hyper/issues/4195 --- src/proto/h1/conn.rs | 35 ++++++++++++++++++++++++----------- tests/client.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 593d3d9eda..69ca50b1a4 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -11,7 +11,7 @@ use std::time::Duration; use crate::rt::{Read, Write}; use bytes::{Buf, Bytes}; use futures_core::ready; -use http::header::{HeaderValue, CONNECTION}; +use http::header::{Entry, HeaderValue, CONNECTION}; use http::{HeaderMap, Method, Version}; use http_body::Frame; use httparse::ParserConfig; @@ -664,10 +664,11 @@ where // Fix keep-alive when Connection: keep-alive header is not present fn fix_keep_alive(&mut self, head: &mut MessageHead) { - let outgoing_is_keep_alive = head - .headers - .get(CONNECTION) - .map_or(false, headers::connection_keep_alive); + let connection_entry = head.headers.entry(CONNECTION); + let outgoing_is_keep_alive = match &connection_entry { + Entry::Occupied(entry) => entry.iter().any(headers::connection_keep_alive), + Entry::Vacant(_) => false, + }; if !outgoing_is_keep_alive { match head.version { @@ -676,10 +677,14 @@ where Version::HTTP_10 => self.state.disable_keep_alive(), // If response is version 1.1 and keep-alive is wanted, add // Connection: keep-alive header when not present - Version::HTTP_11 if self.state.wants_keep_alive() => { - head.headers - .insert(CONNECTION, HeaderValue::from_static("keep-alive")); - } + Version::HTTP_11 if self.state.wants_keep_alive() => match connection_entry { + Entry::Occupied(mut entry) => { + entry.append(HeaderValue::from_static("keep-alive")); + } + Entry::Vacant(entry) => { + entry.insert(HeaderValue::from_static("keep-alive")); + } + }, _ => (), } } @@ -698,8 +703,16 @@ where } Version::HTTP_11 => { if let KA::Disabled = self.state.keep_alive.status() { - head.headers - .insert(CONNECTION, HeaderValue::from_static("close")); + match head.headers.entry(CONNECTION) { + Entry::Occupied(mut entry) => { + if !entry.iter().any(headers::connection_close) { + entry.append(HeaderValue::from_static("close")); + } + } + Entry::Vacant(entry) => { + entry.insert(HeaderValue::from_static("close")); + } + } } } _ => (), diff --git a/tests/client.rs b/tests/client.rs index b512260cc5..e138f09efe 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -1558,6 +1558,35 @@ test! { body: None, } +// https://github.com/hyperium/hyper/issues/4195 +test! { + name: client_hop_by_hop_headers, + + server: + expected: "\ + GET / HTTP/1.1\r\n\ + connection: close, x-hop\r\n\ + x-hop: ...\r\n\ + host: {addr}\r\n\ + \r\n\ + ", + reply: REPLY_OK, + + client: + request: { + method: GET, + url: "http://{addr}/", + headers: { + "connection" => "close, x-hop", + "x-hop" => "...", + }, + }, + response: + status: OK, + headers: {}, + body: None, +} + mod conn { use std::error::Error; use std::io::{self, Read, Write}; From e89d0d93d9566210ee95aea234a00952ebc4a202 Mon Sep 17 00:00:00 2001 From: Leo Camus Date: Fri, 18 Sep 2026 21:05:44 +0200 Subject: [PATCH 23/57] fix(http2): do not reserve capacity for idle Upgraded streams (#4198) The send task of an HTTP/2 `Upgraded` stream reserved one byte of capacity at the top of every loop, before any write was queued. As with `PipeToSendStream` before #4061, an idle tunnel then pins the last byte of the connection-level window, and a second stream can deadlock against peers that only send WINDOW_UPDATE once their window is exhausted. Pull the next write from the channel first, hold it until h2 has capacity, and only then reserve. The next write is still not pulled before the held one is handed to h2, so the writer keeps seeing backpressure. The task now always polls the channel when it holds nothing, which makes the separate close notification unnecessary, so remove it. Refs #4003 --- src/proto/h2/upgrade.rs | 133 ++++++++++------------------------------ tests/client.rs | 112 +++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 99 deletions(-) diff --git a/src/proto/h2/upgrade.rs b/src/proto/h2/upgrade.rs index 217aa27e88..def1fcfc96 100644 --- a/src/proto/h2/upgrade.rs +++ b/src/proto/h2/upgrade.rs @@ -1,11 +1,8 @@ use std::future::Future; use std::io::Cursor; use std::pin::Pin; -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::Arc; use std::task::{Context, Poll}; -use atomic_waker::AtomicWaker; use bytes::{Buf, Bytes}; use futures_channel::{mpsc, oneshot}; use futures_core::{ready, Stream}; @@ -23,15 +20,10 @@ pub(super) fn pair( ) -> (H2Upgraded, UpgradedSendStreamTask) { let (tx, rx) = mpsc::channel(1); let (error_tx, error_rx) = oneshot::channel(); - let close_notify = Arc::new(UpgradedCloseNotify::new()); ( H2Upgraded { - send_stream: UpgradedSendStreamBridge { - tx, - error_rx, - close_notify: close_notify.clone(), - }, + send_stream: UpgradedSendStreamBridge { tx, error_rx }, recv_stream, ping, buf: Bytes::new(), @@ -39,7 +31,7 @@ pub(super) fn pair( UpgradedSendStreamTask { h2_tx: send_stream, rx, - close_notify, + buffered: None, error_tx: Some(error_tx), }, ) @@ -55,46 +47,6 @@ pub(super) struct H2Upgraded { struct UpgradedSendStreamBridge { tx: mpsc::Sender>>, error_rx: oneshot::Receiver, - close_notify: Arc, -} - -impl Drop for UpgradedSendStreamBridge { - fn drop(&mut self) { - self.close_notify.close(); - } -} - -struct UpgradedCloseNotify { - closed: AtomicBool, - task: AtomicWaker, -} - -impl UpgradedCloseNotify { - fn new() -> Self { - Self { - closed: AtomicBool::new(false), - task: AtomicWaker::new(), - } - } - - fn close(&self) { - self.closed.store(true, Ordering::Release); - self.task.wake(); - } - - fn poll_closed(&self, cx: &mut Context<'_>) -> Poll<()> { - if self.closed.load(Ordering::Acquire) { - return Poll::Ready(()); - } - - self.task.register(cx.waker()); - - if self.closed.load(Ordering::Acquire) { - Poll::Ready(()) - } else { - Poll::Pending - } - } } pin_project! { @@ -104,7 +56,7 @@ pin_project! { h2_tx: SendStream>, #[pin] rx: mpsc::Receiver>>, - close_notify: Arc, + buffered: Option>>, error_tx: Option>, } } @@ -123,35 +75,6 @@ where // one of the sides hanging up, so the task doesn't live around // longer than it's meant to. loop { - // we don't have the next chunk of data yet, so just reserve 1 byte to make - // sure there's some capacity available. h2 will handle the capacity management - // for the actual body chunk. - me.h2_tx.reserve_capacity(1); - - let h2_has_capacity = if me.h2_tx.capacity() == 0 { - // poll_capacity oddly needs a loop - loop { - match me.h2_tx.poll_capacity(cx) { - Poll::Ready(Some(Ok(0))) => {} - Poll::Ready(Some(Ok(_))) => break true, - Poll::Ready(Some(Err(e))) => { - return Poll::Ready(Err(crate::Error::new_body_write(e))) - } - Poll::Ready(None) => { - // None means the stream is no longer in a - // streaming state, we either finished it - // somehow, or the remote reset us. - return Poll::Ready(Err(crate::Error::new_body_write( - "send stream capacity unexpectedly closed", - ))); - } - Poll::Pending => break false, - } - } - } else { - true - }; - match me.h2_tx.poll_reset(cx) { Poll::Ready(Ok(reason)) => { trace!("stream received RST_STREAM: {:?}", reason); @@ -165,30 +88,43 @@ where Poll::Pending => (), } - // If h2 has no capacity, don't pull another item from the mpsc - // receiver. That would free a channel slot and let the writer - // enqueue more data without h2 backpressure. - // - // Still allow the task to finish once the upgraded write side is - // gone and the mpsc queue is empty. - if !h2_has_capacity { - // `size_hint` reads the queued message count without popping, - // so an accepted write stays queued until h2 capacity returns. - if me.rx.size_hint().0 == 0 && me.close_notify.poll_closed(cx).is_ready() { - me.h2_tx - .send_data(SendBuf::None, true) - .map_err(crate::Error::new_body_write)?; - return Poll::Ready(Ok(())); + // A write taken from the mpsc receiver waits here for h2 capacity, + // and the next one isn't pulled until it has been handed to h2, so + // the writer still sees h2 backpressure. + if me.buffered.is_some() { + // poll_capacity oddly needs a loop + while me.h2_tx.capacity() == 0 { + match ready!(me.h2_tx.poll_capacity(cx)) { + Some(Ok(0)) => {} + Some(Ok(_)) => break, + Some(Err(e)) => return Poll::Ready(Err(crate::Error::new_body_write(e))), + None => { + // None means the stream is no longer in a + // streaming state, we either finished it + // somehow, or the remote reset us. + return Poll::Ready(Err(crate::Error::new_body_write( + "send stream capacity unexpectedly closed", + ))); + } + } } - return Poll::Pending; + let cursor = me.buffered.take().expect("checked is_some above"); + me.h2_tx + .send_data(SendBuf::Cursor(cursor), false) + .map_err(crate::Error::new_body_write)?; + continue; } match me.rx.as_mut().poll_next(cx) { Poll::Ready(Some(cursor)) => { - me.h2_tx - .send_data(SendBuf::Cursor(cursor), false) - .map_err(crate::Error::new_body_write)?; + // Only reserve capacity once there is something to send. + // Reserving while idle, even a single byte, pins that + // capacity on the connection-level window (#4003). As in + // `PipeToSendStream`, h2 raises the request to the + // buffered length inside `send_data`. + me.h2_tx.reserve_capacity(1); + *me.buffered = Some(cursor); } Poll::Ready(None) => { me.h2_tx @@ -328,7 +264,6 @@ impl Write for H2Upgraded { cx: &mut Context<'_>, ) -> Poll> { self.send_stream.tx.close_channel(); - self.send_stream.close_notify.close(); match Pin::new(&mut self.send_stream.error_rx).poll(cx) { Poll::Ready(Ok(reason)) => Poll::Ready(Err(io_error(reason))), Poll::Ready(Err(_task_dropped)) => Poll::Ready(Ok(())), diff --git a/tests/client.rs b/tests/client.rs index e138f09efe..e4c8d12cf6 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -3344,6 +3344,118 @@ mod conn { drop(tx_a); let _ = tokio::time::timeout(Duration::from_secs(5), a_handle).await; } + + // https://github.com/hyperium/hyper/issues/4003, for HTTP/2 CONNECT + // + // Like `h2_idle_stream_does_not_pin_connection_window`, but the idle + // stream is the send side of an `Upgraded` tunnel. It must not reserve + // connection-level flow control capacity while it has nothing to write. + #[tokio::test] + async fn h2_idle_upgraded_does_not_pin_connection_window() { + // One byte short of the initial connection-level window. + const STREAM_A_LEN: usize = 65534; + + let (client_io, server_io, _) = setup_duplex_test_server(); + let (stream_a_full_tx, stream_a_full_rx) = oneshot::channel::<()>(); + let (stream_a_done_tx, stream_a_done_rx) = oneshot::channel::<()>(); + let (stream_b_got_tx, stream_b_got_rx) = oneshot::channel::(); + + // Raw h2 server that never calls `release_capacity`, so it never + // sends a connection-level WINDOW_UPDATE. + tokio::spawn(async move { + let mut h2 = h2::server::handshake(server_io).await.unwrap(); + let mut stream_a_full_tx = Some(stream_a_full_tx); + let mut stream_a_done_rx = Some(stream_a_done_rx); + let mut stream_b_got_tx = Some(stream_b_got_tx); + while let Some(result) = h2.accept().await { + let (req, mut respond) = result.unwrap(); + if req.method() == Method::CONNECT { + let full_tx = stream_a_full_tx.take().unwrap(); + let done_rx = stream_a_done_rx.take().unwrap(); + tokio::spawn(async move { + let _send = respond.send_response(Response::new(()), false).unwrap(); + let mut body = req.into_body(); + let mut received = 0usize; + while received < STREAM_A_LEN { + match body.data().await { + Some(Ok(frame)) => received += frame.len(), + _ => return, + } + } + let _ = full_tx.send(()); + // Hold on to the recv stream, dropping it would release + // its capacity and send a WINDOW_UPDATE. + let _ = done_rx.await; + drop(body); + }); + } else { + let got_tx = stream_b_got_tx.take().unwrap(); + tokio::spawn(async move { + let mut body = req.into_body(); + let mut received = 0usize; + if let Some(Ok(frame)) = body.data().await { + received += frame.len(); + } + let _ = got_tx.send(received); + let mut send = respond.send_response(Response::new(()), false).unwrap(); + let _ = send.send_data(Bytes::from_static(b"ok"), true); + }); + } + } + }); + + let io = TokioIo::new(client_io); + let (mut client, conn) = conn::http2::Builder::new(TokioExecutor) + .handshake::<_, Full>(io) + .await + .expect("http handshake"); + tokio::spawn(async move { + let _ = conn.await; + }); + + // Stream A: a tunnel that writes STREAM_A_LEN bytes and then stays + // open, leaving one byte of connection window. + let req_a = Request::connect("localhost") + .body(Full::new(Bytes::new())) + .unwrap(); + let res_a = client.send_request(req_a).await.expect("send_request A"); + assert_eq!(res_a.status(), StatusCode::OK); + let mut upgraded = TokioIo::new(hyper::upgrade::on(res_a).await.unwrap()); + upgraded + .write_all(&vec![b'A'; STREAM_A_LEN]) + .await + .expect("write to tunnel"); + + tokio::time::timeout(Duration::from_secs(5), stream_a_full_rx) + .await + .expect("server should receive all of stream A in time") + .expect("stream_a_full_rx"); + + // Let the tunnel's send task park waiting for more writes. + for _ in 0..16 { + tokio::task::yield_now().await; + } + + // Stream B: one byte of body, which needs the last byte of window. + let req_b = Request::post("http://localhost/b") + .body(Full::new(Bytes::from_static(b"b"))) + .unwrap(); + let b_fut = client.send_request(req_b); + + let received_b = tokio::time::timeout(Duration::from_secs(5), stream_b_got_rx) + .await + .expect("stream B must reach the server even while the tunnel is idle") + .expect("stream_b_got_rx"); + assert_eq!( + received_b, 1, + "stream B should deliver its single body byte" + ); + + let _ = tokio::time::timeout(Duration::from_secs(5), b_fut).await; + + let _ = stream_a_done_tx.send(()); + drop(upgraded); + } } trait FutureHyperExt: TryFuture { From fd4ce420a781480229faa89a2fc824812c9dbcc3 Mon Sep 17 00:00:00 2001 From: "Gavin D." Date: Thu, 24 Sep 2026 16:52:24 -0400 Subject: [PATCH 24/57] style(lib): address clippy panics and missing_panics_doc lints (#4191) cc #4071 --- Cargo.toml | 2 -- src/client/conn/http1.rs | 5 +++-- src/client/conn/http2.rs | 4 ++-- src/common/lock.rs | 1 + src/common/time.rs | 4 ++++ src/ext/h1_reason_phrase.rs | 6 +++++- src/proto/h1/role.rs | 2 ++ src/rt/io.rs | 5 +++++ src/server/conn/http1.rs | 1 + 9 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6bd75c244..2c98449797 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,11 +129,9 @@ map_err_ignore = "allow" map_unwrap_or = "allow" match_wild_err_arm = "allow" missing_fields_in_debug = "allow" # TODO: use finish_non_exhaustive -missing_panics_doc = "allow" # TODO: might be false multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" needless_pass_by_value = "allow" -panic = "allow" pattern_type_mismatch = "allow" redundant_closure_for_method_calls = "allow" redundant_else = "allow" diff --git a/src/client/conn/http1.rs b/src/client/conn/http1.rs index bdb03171d4..8e59c1f185 100644 --- a/src/client/conn/http1.rs +++ b/src/client/conn/http1.rs @@ -101,6 +101,7 @@ where /// # Errors /// /// Returns an error if the connection encounters an error while being polled to completion. + #[allow(clippy::missing_panics_doc)] pub async fn without_shutdown(self) -> crate::Result> { let mut conn = Some(self); crate::common::future::poll_fn(move |cx| -> Poll>> { @@ -237,7 +238,7 @@ where Ok(Ok(resp)) => Ok(resp), Ok(Err(err)) => Err(err), // this is definite bug if it happens, but it shouldn't happen! - Err(_canceled) => panic!("dispatch dropped without returning error"), + Err(_canceled) => unreachable!("dispatch dropped without returning error"), }, Err(_req) => { debug!("connection was not ready"); @@ -267,7 +268,7 @@ where Ok(Ok(res)) => Ok(res), Ok(Err(err)) => Err(err), // this is definite bug if it happens, but it shouldn't happen! - Err(_) => panic!("dispatch dropped without returning error"), + Err(_) => unreachable!("dispatch dropped without returning error"), }, Err(req) => { debug!("connection was not ready"); diff --git a/src/client/conn/http2.rs b/src/client/conn/http2.rs index 10613b354a..4df19a10cc 100644 --- a/src/client/conn/http2.rs +++ b/src/client/conn/http2.rs @@ -170,7 +170,7 @@ where Ok(Ok(resp)) => Ok(resp), Ok(Err(err)) => Err(err), // this is definite bug if it happens, but it shouldn't happen! - Err(_canceled) => panic!("dispatch dropped without returning error"), + Err(_canceled) => unreachable!("dispatch dropped without returning error"), }, Err(_req) => { debug!("connection was not ready"); @@ -201,7 +201,7 @@ where Ok(Ok(res)) => Ok(res), Ok(Err(err)) => Err(err), // this is definite bug if it happens, but it shouldn't happen! - Err(_) => panic!("dispatch dropped without returning error"), + Err(_) => unreachable!("dispatch dropped without returning error"), }, Err(req) => { debug!("connection was not ready"); diff --git a/src/common/lock.rs b/src/common/lock.rs index 4e2b70bff2..24b5d104f1 100644 --- a/src/common/lock.rs +++ b/src/common/lock.rs @@ -4,6 +4,7 @@ pub(crate) trait LockResultExt { fn panic_if_poisoned(self) -> T; } +#[allow(clippy::panic)] impl LockResultExt for LockResult { #[track_caller] fn panic_if_poisoned(self) -> T { diff --git a/src/common/time.rs b/src/common/time.rs index b3534f1580..2115c0e239 100644 --- a/src/common/time.rs +++ b/src/common/time.rs @@ -33,6 +33,7 @@ impl Time { #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] pub(crate) fn sleep(&self, duration: Duration) -> Pin> { match &self { + #[allow(clippy::panic)] Time::Empty => { panic!("You must supply a timer.") } @@ -43,6 +44,7 @@ impl Time { #[cfg(all(feature = "server", feature = "http1"))] pub(crate) fn sleep_until(&self, deadline: Instant) -> Pin> { match &self { + #[allow(clippy::panic)] Time::Empty => { panic!("You must supply a timer.") } @@ -59,6 +61,7 @@ impl Time { pub(crate) fn reset(&self, sleep: &mut Pin>, new_deadline: Instant) { match &self { + #[allow(clippy::panic)] Time::Empty => { panic!("You must supply a timer.") } @@ -77,6 +80,7 @@ impl Time { Time::Timer(..) => Some(dur), }, Dur::Configured(Some(dur)) => match self { + #[allow(clippy::panic)] Time::Empty => panic!("timeout `{name}` set, but no timer set",), Time::Timer(..) => Some(dur), }, diff --git a/src/ext/h1_reason_phrase.rs b/src/ext/h1_reason_phrase.rs index c4c615bc20..3ebdfcf9a9 100644 --- a/src/ext/h1_reason_phrase.rs +++ b/src/ext/h1_reason_phrase.rs @@ -40,8 +40,12 @@ impl ReasonPhrase { } /// Converts a static byte slice to a reason phrase. + /// + /// # Panics + /// + /// This method will panic if 'reason' contains an invalid byte. In a const context, + /// this is a compile-time error instead. pub const fn from_static(reason: &'static [u8]) -> Self { - // TODO: this can be made const once MSRV is >= 1.57.0 assert!( find_invalid_byte(reason).is_none(), "invalid byte in static reason phrase" diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index d083d2a912..b83c8ab12b 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -433,6 +433,7 @@ impl Http1Transaction for Server { debug!("response with HTTP2 version coerced to HTTP/1.1"); extend(dst, b"HTTP/1.1 "); } + #[allow(clippy::panic)] other => panic!("unexpected response version: {other:?}"), } @@ -1221,6 +1222,7 @@ impl Http1Transaction for Client { debug!("request with HTTP2 version coerced to HTTP/1.1"); extend(dst, b"HTTP/1.1"); } + #[allow(clippy::panic)] other => panic!("unexpected request version: {other:?}"), } extend(dst, b"\r\n"); diff --git a/src/rt/io.rs b/src/rt/io.rs index 9e633e69e8..31e699d73a 100644 --- a/src/rt/io.rs +++ b/src/rt/io.rs @@ -337,6 +337,11 @@ impl ReadBufCursor<'_> { /// # Safety /// /// The caller must take care that `n` more bytes have been initialized. + /// + /// # Panics + /// + /// This method will panic if advancing would cause the filled cursor to + /// overflow `usize`. #[inline] pub unsafe fn advance(&mut self, n: usize) { self.buf.filled = self.buf.filled.checked_add(n).expect("overflow"); diff --git a/src/server/conn/http1.rs b/src/server/conn/http1.rs index 7bd3d72e2f..180d507e90 100644 --- a/src/server/conn/http1.rs +++ b/src/server/conn/http1.rs @@ -178,6 +178,7 @@ where /// # Error /// /// This errors if the underlying connection protocol is not HTTP/1. + #[allow(clippy::missing_panics_doc)] pub fn without_shutdown(self) -> impl Future>> { let mut zelf = Some(self); crate::common::future::poll_fn(move |cx| { From e60932da8fb37c0060ff929bfc9dc234e2e44e8b Mon Sep 17 00:00:00 2001 From: Toyeshh Medikonda Date: Fri, 25 Sep 2026 21:25:48 -0500 Subject: [PATCH 25/57] docs: fix three typos (#4204) - capi/README.md: "staring with" -> "starting with" in the build instructions - docs/MSRV.md: "Minimum Support Rust Version" -> "Minimum Supported Rust Version" - docs/GOVERNANCE.md: "elsehwere" -> "elsewhere" --- capi/README.md | 2 +- docs/GOVERNANCE.md | 2 +- docs/MSRV.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/capi/README.md b/capi/README.md index becd57862f..490bb5dae7 100644 --- a/capi/README.md +++ b/capi/README.md @@ -10,7 +10,7 @@ Because of that, it's only accessible if `--cfg hyper_unstable_ffi` is passed to ## Building -The C API is part of the Rust library, but isn't compiled by default. Using `cargo`, staring with `1.64.0`, it can be compiled with the following command: +The C API is part of the Rust library, but isn't compiled by default. Using `cargo`, starting with `1.64.0`, it can be compiled with the following command: ``` RUSTFLAGS="--cfg hyper_unstable_ffi" cargo rustc --features client,http1,http2,ffi --crate-type cdylib diff --git a/docs/GOVERNANCE.md b/docs/GOVERNANCE.md index 7a1becf07c..45d7a515c9 100644 --- a/docs/GOVERNANCE.md +++ b/docs/GOVERNANCE.md @@ -117,7 +117,7 @@ How to become: - Advisors are invited by a maintainer. - Sometimes they are experienced Collaborators who must step back from active - contributing. Sometimes they are experts placed elsehwere who are friendly to + contributing. Sometimes they are experts placed elsewhere who are friendly to the project. ### Maintainer diff --git a/docs/MSRV.md b/docs/MSRV.md index 70752c9138..b8b68287eb 100644 --- a/docs/MSRV.md +++ b/docs/MSRV.md @@ -1,4 +1,4 @@ -# Minimum Support Rust Version (MSRV) +# Minimum Supported Rust Version (MSRV) hyper's current policy is to always support a Rust version at least 6 months old. That is, a compiler version released within the last 6 months can compile From c954d80cdcb91ae8faa0aa743639a13498f74b7c Mon Sep 17 00:00:00 2001 From: "Gavin D." Date: Tue, 29 Sep 2026 16:05:40 -0400 Subject: [PATCH 26/57] style(lib): address trivial, low quantity clippy lints (#4190) --- Cargo.toml | 6 ------ src/body/chan.rs | 1 + src/common/time.rs | 2 +- src/proto/h1/decode.rs | 5 ++++- src/proto/h1/io.rs | 5 ----- src/proto/h2/ping.rs | 2 ++ src/proto/h2/server.rs | 3 +-- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2c98449797..edf0af719d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -119,14 +119,9 @@ cast_possible_truncation = "allow" # TODO: consider cast_precision_loss = "allow" # TODO: consider checked_conversions = "allow" else_if_without_else = "allow" -enum_glob_use = "allow" float_arithmetic = "allow" indexing_slicing = "allow" -integer_division = "allow" -integer_division_remainder_used = "allow" let_unit_value = "allow" -map_err_ignore = "allow" -map_unwrap_or = "allow" match_wild_err_arm = "allow" missing_fields_in_debug = "allow" # TODO: use finish_non_exhaustive multiple_inherent_impl = "allow" @@ -137,7 +132,6 @@ redundant_closure_for_method_calls = "allow" redundant_else = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? trivially_copy_pass_by_ref = "allow" -unnecessary_trailing_comma = "allow" unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? unused_trait_names = "allow" # TODO: kinda annoying, but might be good to deny diff --git a/src/body/chan.rs b/src/body/chan.rs index b45bea866b..314a7d38e2 100644 --- a/src/body/chan.rs +++ b/src/body/chan.rs @@ -124,6 +124,7 @@ impl Sender { #[allow(dead_code)] #[allow(clippy::unused_async_trait_impl)] + #[allow(clippy::map_err_ignore)] pub(crate) async fn send_trailers(&mut self, trailers: HeaderMap) -> crate::Result<()> { self.try_send_trailers(trailers) .map_err(|_| crate::Error::new_closed()) diff --git a/src/common/time.rs b/src/common/time.rs index 2115c0e239..c238b4b3d4 100644 --- a/src/common/time.rs +++ b/src/common/time.rs @@ -81,7 +81,7 @@ impl Time { }, Dur::Configured(Some(dur)) => match self { #[allow(clippy::panic)] - Time::Empty => panic!("timeout `{name}` set, but no timer set",), + Time::Empty => panic!("timeout `{name}` set, but no timer set"), Time::Timer(..) => Some(dur), }, Dur::Default(None) | Dur::Configured(None) => None, diff --git a/src/proto/h1/decode.rs b/src/proto/h1/decode.rs index 65adaa784b..fe2c8d9cdf 100644 --- a/src/proto/h1/decode.rs +++ b/src/proto/h1/decode.rs @@ -314,7 +314,10 @@ impl ChunkedState { max_headers_bytes, }: StepArgs<'_>, ) -> Poll> { - use self::ChunkedState::*; + use self::ChunkedState::{ + Body, BodyCr, BodyLf, End, EndCr, EndLf, Extension, Size, SizeLf, SizeLws, Start, + Trailer, TrailerLf, + }; match *self { Start => ChunkedState::read_start(cx, body, chunk_size), Size => ChunkedState::read_size(cx, body, chunk_size), diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index 386f1be35b..5833004768 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -534,12 +534,7 @@ impl WriteBuf { strategy, } } -} -impl WriteBuf -where - B: Buf, -{ fn set_strategy(&mut self, strategy: WriteStrategy) { self.strategy = strategy; } diff --git a/src/proto/h2/ping.rs b/src/proto/h2/ping.rs index 198bff465c..ab3af13b83 100644 --- a/src/proto/h2/ping.rs +++ b/src/proto/h2/ping.rs @@ -395,6 +395,8 @@ impl Bdp { // if the current `bytes` sample is at least 2/3 the previous // bdp, increase to double the current sample. + #[allow(clippy::integer_division_remainder_used)] + #[allow(clippy::integer_division)] if bytes >= self.bdp as usize * 2 / 3 { self.bdp = (bytes * 2).min(BDP_LIMIT) as WindowSize; trace!("BDP increased to {}", self.bdp); diff --git a/src/proto/h2/server.rs b/src/proto/h2/server.rs index 92026276aa..2098758d9e 100644 --- a/src/proto/h2/server.rs +++ b/src/proto/h2/server.rs @@ -267,8 +267,7 @@ where let ping = self .ping .as_ref() - .map(|ping| ping.0.clone()) - .unwrap_or_else(ping::disabled); + .map_or_else(ping::disabled, |ping| ping.0.clone()); // Record the headers received ping.record_non_data(); From e1f6d9b6804e2c90e16456e6d364e2535396939e Mon Sep 17 00:00:00 2001 From: dswij Date: Fri, 2 Oct 2026 22:24:27 +0800 Subject: [PATCH 27/57] chore(clippy): allow clippy lints following 1.99.0 release (#4219) --- Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index edf0af719d..e9ca2c890c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,6 +138,10 @@ unused_trait_names = "allow" # TODO: kinda annoying, but might be good to deny unwrap_in_result = "allow" useless_borrows_in_formatting = "allow" wildcard_enum_match_arm = "allow" +definition_in_module_root = "allow" +unnecessary_rest_pattern = "allow" +rest_pattern_accessible_field = "allow" +needless_bool = "allow" # explicitly allowed absolute_paths = "allow" # sometimes its cleaner From da3b1b197c954ac5454a92562fe3d729a87cba56 Mon Sep 17 00:00:00 2001 From: ump45nose <52391318+ump45nose@users.noreply.github.com> Date: Sat, 3 Oct 2026 06:03:46 +0800 Subject: [PATCH 28/57] style(lib): remove useless_borrows_in_formatting from the clippy allowlist (#4220) Fix the two remaining violations in src/proto/h1/decode.rs by inlining the header variable into the format string, which also satisfies the uninlined_format_args lint that the old &header borrow was dodging. Refs: #4071 --- Cargo.toml | 1 - src/proto/h1/decode.rs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e9ca2c890c..12cfa31f4d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -136,7 +136,6 @@ unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? unused_trait_names = "allow" # TODO: kinda annoying, but might be good to deny unwrap_in_result = "allow" -useless_borrows_in_formatting = "allow" wildcard_enum_match_arm = "allow" definition_in_module_root = "allow" unnecessary_rest_pattern = "allow" diff --git a/src/proto/h1/decode.rs b/src/proto/h1/decode.rs index fe2c8d9cdf..9d8ed7f408 100644 --- a/src/proto/h1/decode.rs +++ b/src/proto/h1/decode.rs @@ -652,7 +652,7 @@ fn decode_trailers(buf: &mut BytesMut, count: usize) -> Result { return Err(io::Error::new( io::ErrorKind::InvalidInput, - format!("Invalid header name: {:?}", &header), + format!("Invalid header name: {header:?}"), )); } }; @@ -662,7 +662,7 @@ fn decode_trailers(buf: &mut BytesMut, count: usize) -> Result { return Err(io::Error::new( io::ErrorKind::InvalidInput, - format!("Invalid header value: {:?}", &header), + format!("Invalid header value: {header:?}"), )); } }; From 0d474402c20c17939c30aaecde56d1a3e21b2b0a Mon Sep 17 00:00:00 2001 From: "Dmitry B." <4666600+badimalex@users.noreply.github.com> Date: Sat, 3 Oct 2026 18:39:34 +0300 Subject: [PATCH 29/57] style(lib): mark partial Debug impls as non-exhaustive (#4218) --- Cargo.toml | 1 - src/proto/h1/conn.rs | 2 +- src/proto/h1/io.rs | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 12cfa31f4d..33a3776e39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -123,7 +123,6 @@ float_arithmetic = "allow" indexing_slicing = "allow" let_unit_value = "allow" match_wild_err_arm = "allow" -missing_fields_in_debug = "allow" # TODO: use finish_non_exhaustive multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" needless_pass_by_value = "allow" diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 69ca50b1a4..11059cd4e3 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -1016,7 +1016,7 @@ impl fmt::Debug for State { // Purposefully leaving off other fields.. - builder.finish() + builder.finish_non_exhaustive() } } diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index 5833004768..ab7438a28b 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -47,7 +47,7 @@ where f.debug_struct("Buffered") .field("read_buf", &self.read_buf) .field("write_buf", &self.write_buf) - .finish() + .finish_non_exhaustive() } } @@ -596,7 +596,7 @@ impl fmt::Debug for WriteBuf { f.debug_struct("WriteBuf") .field("remaining", &self.remaining()) .field("strategy", &self.strategy) - .finish() + .finish_non_exhaustive() } } From 714c7cf46fd405c88eb34c2f243ae5b146ba8f07 Mon Sep 17 00:00:00 2001 From: "Gavin D." Date: Mon, 5 Oct 2026 09:14:18 -0400 Subject: [PATCH 30/57] style(lib): address cast-possible-truncation clippy findings (#4192) --- Cargo.toml | 1 - src/proto/h1/encode.rs | 31 ++++++++++++++++++------------- src/proto/h2/ping.rs | 7 ++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33a3776e39..347695fa0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -115,7 +115,6 @@ restriction = { level = "warn", priority = -2 } arithmetic_side_effects = "allow" # TODO: consider as_conversions = "allow" # TODO: tricky -cast_possible_truncation = "allow" # TODO: consider cast_precision_loss = "allow" # TODO: consider checked_conversions = "allow" else_if_without_else = "allow" diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index 266b4dd6dd..4a9fd4348b 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -142,13 +142,18 @@ impl Encoder { } Kind::Length(remaining) => { trace!("sized write, len = {}", len); - if len as u64 > *remaining { - let limit = *remaining as usize; - *remaining = 0; - BufKind::Limited(msg.take(limit)) - } else { - *remaining -= len as u64; - BufKind::Exact(msg) + match usize::try_from(*remaining) { + // Holding more than is owed, so write only what is left. + Ok(limit) if limit < len => { + *remaining = 0; + BufKind::Limited(msg.take(limit)) + } + // Ok(_) => Owed at least what we hold, write all of it. + // Err(_) => Owed more than `usize` can represent, write all of it. + Ok(_) | Err(_) => { + *remaining -= len as u64; + BufKind::Exact(msg) + } } } #[cfg(feature = "server")] @@ -241,6 +246,7 @@ impl Encoder { dst.buffer(msg); !self.is_last } + #[allow(clippy::cast_possible_truncation)] Ordering::Greater => { dst.buffer(msg.take(remaining as usize)); !self.is_last @@ -328,11 +334,7 @@ where } } -#[cfg(target_pointer_width = "32")] -const USIZE_BYTES: usize = 4; - -#[cfg(target_pointer_width = "64")] -const USIZE_BYTES: usize = 8; +const USIZE_BYTES: usize = std::mem::size_of::(); // each byte will become 2 hex const CHUNK_SIZE_MAX_BYTES: usize = USIZE_BYTES * 2; @@ -369,6 +371,7 @@ impl Buf for ChunkSize { } #[inline] + #[allow(clippy::cast_possible_truncation)] fn advance(&mut self, cnt: usize) { assert!(cnt <= self.remaining()); self.pos += cnt as u8; // just asserted cnt fits in u8 @@ -385,12 +388,14 @@ impl fmt::Debug for ChunkSize { } impl fmt::Write for ChunkSize { + #[allow(clippy::cast_possible_truncation)] fn write_str(&mut self, num: &str) -> fmt::Result { use std::io::Write; (&mut self.bytes[self.len.into()..]) .write_all(num.as_bytes()) .expect("&mut [u8].write() cannot error"); - self.len += num.len() as u8; // safe because bytes is never bigger than 256 + debug_assert!(u8::try_from(num.len()).is_ok()); + self.len += num.len() as u8; // safe because bytes is never bigger than 255 Ok(()) } } diff --git a/src/proto/h2/ping.rs b/src/proto/h2/ping.rs index ab3af13b83..33016a28d4 100644 --- a/src/proto/h2/ping.rs +++ b/src/proto/h2/ping.rs @@ -361,12 +361,12 @@ impl Shared { // ===== impl Bdp ===== /// Any higher than this likely will be hitting the TCP flow control. -const BDP_LIMIT: usize = 1024 * 1024 * 16; +const BDP_LIMIT: WindowSize = 1024 * 1024 * 16; impl Bdp { fn calculate(&mut self, bytes: usize, rtt: Duration) -> Option { // No need to do any math if we're at the limit. - if self.bdp as usize == BDP_LIMIT { + if self.bdp == BDP_LIMIT { self.stabilize_delay(); return None; } @@ -398,7 +398,8 @@ impl Bdp { #[allow(clippy::integer_division_remainder_used)] #[allow(clippy::integer_division)] if bytes >= self.bdp as usize * 2 / 3 { - self.bdp = (bytes * 2).min(BDP_LIMIT) as WindowSize; + self.bdp = + WindowSize::try_from((bytes * 2).min(BDP_LIMIT as usize)).unwrap_or(BDP_LIMIT); trace!("BDP increased to {}", self.bdp); self.stable_count = 0; From d1cd14eff392be63a2318d690cc9380723ec61a1 Mon Sep 17 00:00:00 2001 From: Dahale Aditya Dnyaneshwar Date: Mon, 5 Oct 2026 21:17:22 +0530 Subject: [PATCH 31/57] feat(http1): add max_header_size limit for server and client (#4183) Adds max_header_size(val: usize) on both server and client HTTP/1 builders. This allows configuring an explicit byte limit for request/response headers (including the start line and chunked trailers). When the limit is exceeded: - On server: responds with 431 Request Header Fields Too Large and closes the connection. - On client: returns a Parse::TooLarge error (is_parse_too_large). - On chunked decoder: replaces existing TODO in proto/h1/conn.rs with the configured h1_max_header_size. Closes #3832. --- src/client/conn/http1.rs | 20 ++++++++ src/proto/h1/conn.rs | 11 ++++- src/proto/h1/io.rs | 10 +++- src/proto/h1/mod.rs | 1 + src/proto/h1/role.rs | 103 +++++++++++++++++++++++++++++++++++++++ src/server/conn/http1.rs | 20 ++++++++ tests/client.rs | 65 ++++++++++++++++++++++++ tests/server.rs | 87 +++++++++++++++++++++++++++++++++ 8 files changed, 314 insertions(+), 3 deletions(-) diff --git a/src/client/conn/http1.rs b/src/client/conn/http1.rs index 8e59c1f185..43c2564280 100644 --- a/src/client/conn/http1.rs +++ b/src/client/conn/http1.rs @@ -132,6 +132,7 @@ pub struct Builder { h1_title_case_headers: bool, h1_preserve_header_case: bool, h1_max_headers: Option, + h1_max_header_size: Option, #[cfg(feature = "ffi")] h1_preserve_header_order: bool, h1_read_buf_exact_size: Option, @@ -353,6 +354,7 @@ impl Builder { h1_title_case_headers: false, h1_preserve_header_case: false, h1_max_headers: None, + h1_max_header_size: None, #[cfg(feature = "ffi")] h1_preserve_header_order: false, h1_max_buf_size: None, @@ -501,6 +503,21 @@ impl Builder { self } + /// Set the maximum size of response headers (including the status line) in bytes. + /// + /// If the server sends headers exceeding this limit, the error "message head is too large" + /// is returned. + /// + /// If not configured, then the [`max_buf_size`](Builder::max_buf_size) will naturally be reached and applied. + /// + /// This value is also used as the maximum size limit for chunked trailers. + /// + /// Default is `None`. + pub fn max_header_size(&mut self, val: usize) -> &mut Self { + self.h1_max_header_size = Some(val); + self + } + /// Set whether to support preserving original header order. /// /// Currently, this will record the order in which headers are received, and store this @@ -588,6 +605,9 @@ impl Builder { if let Some(max_headers) = opts.h1_max_headers { conn.set_http1_max_headers(max_headers); } + if let Some(max_header_size) = opts.h1_max_header_size { + conn.set_http1_max_header_size(max_header_size); + } #[cfg(feature = "ffi")] if opts.h1_preserve_header_order { conn.set_preserve_header_order(); diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 11059cd4e3..8a5a734940 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -58,6 +58,7 @@ where method: None, h1_parser_config: ParserConfig::default(), h1_max_headers: None, + h1_max_header_size: None, #[cfg(feature = "server")] h1_header_read_timeout: None, #[cfg(feature = "server")] @@ -141,6 +142,10 @@ where self.state.h1_max_headers = Some(val); } + pub(crate) fn set_http1_max_header_size(&mut self, val: usize) { + self.state.h1_max_header_size = Some(val); + } + #[cfg(feature = "server")] pub(crate) fn set_http1_header_read_timeout(&mut self, val: Duration) { self.state.h1_header_read_timeout = Some(val); @@ -241,6 +246,7 @@ where req_method: &mut self.state.method, h1_parser_config: self.state.h1_parser_config.clone(), h1_max_headers: self.state.h1_max_headers, + h1_max_header_size: self.state.h1_max_header_size, preserve_header_case: self.state.preserve_header_case, #[cfg(feature = "ffi")] preserve_header_order: self.state.preserve_header_order, @@ -309,7 +315,7 @@ where self.try_keep_alive(cx); } } else if msg.expect_continue && msg.head.version.gt(&Version::HTTP_10) { - let h1_max_header_size = None; // TODO: remove this when we land h1_max_header_size support + let h1_max_header_size = self.state.h1_max_header_size; self.state.reading = Reading::Continue(Decoder::new( msg.decode, self.state.h1_max_headers, @@ -317,7 +323,7 @@ where )); wants = wants.add(Wants::EXPECT); } else { - let h1_max_header_size = None; // TODO: remove this when we land h1_max_header_size support + let h1_max_header_size = self.state.h1_max_header_size; self.state.reading = Reading::Body(Decoder::new( msg.decode, self.state.h1_max_headers, @@ -946,6 +952,7 @@ struct State { method: Option, h1_parser_config: ParserConfig, h1_max_headers: Option, + h1_max_header_size: Option, #[cfg(feature = "server")] h1_header_read_timeout: Option, #[cfg(feature = "server")] diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index ab7438a28b..bcd0204d2f 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -188,6 +188,7 @@ where req_method: parse_ctx.req_method, h1_parser_config: parse_ctx.h1_parser_config.clone(), h1_max_headers: parse_ctx.h1_max_headers, + h1_max_header_size: parse_ctx.h1_max_header_size, preserve_header_case: parse_ctx.preserve_header_case, #[cfg(feature = "ffi")] preserve_header_order: parse_ctx.preserve_header_order, @@ -200,8 +201,14 @@ where self.partial_len = None; return Poll::Ready(Ok(msg)); } else { - let max = self.read_buf_strategy.max(); let curr_len = self.read_buf.len(); + if let Some(max_header_size) = parse_ctx.h1_max_header_size { + if curr_len >= max_header_size { + debug!("max_header_size ({}) reached, closing", max_header_size); + return Poll::Ready(Err(crate::Error::new_too_large())); + } + } + let max = self.read_buf_strategy.max(); if curr_len >= max { debug!("max_buf_size ({}) reached, closing", max); return Poll::Ready(Err(crate::Error::new_too_large())); @@ -697,6 +704,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, diff --git a/src/proto/h1/mod.rs b/src/proto/h1/mod.rs index a17dbae83c..344d5a8a4f 100644 --- a/src/proto/h1/mod.rs +++ b/src/proto/h1/mod.rs @@ -73,6 +73,7 @@ pub(crate) struct ParseContext<'ctx> { req_method: &'ctx mut Option, h1_parser_config: ParserConfig, h1_max_headers: Option, + h1_max_header_size: Option, preserve_header_case: bool, #[cfg(feature = "ffi")] preserve_header_order: bool, diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index b83c8ab12b..262de2480a 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -180,6 +180,11 @@ impl Http1Transaction for Server { ) { Ok(httparse::Status::Complete(parsed_len)) => { trace!("Request.parse Complete({})", parsed_len); + if let Some(max_header_size) = ctx.h1_max_header_size { + if parsed_len > max_header_size { + return Err(Parse::TooLarge); + } + } len = parsed_len; let uri = req.path.expect("httparse completed"); if uri.len() > MAX_URI_LEN { @@ -1053,6 +1058,11 @@ impl Http1Transaction for Client { ) { Ok(httparse::Status::Complete(len)) => { trace!("Response.parse Complete({})", len); + if let Some(max_header_size) = ctx.h1_max_header_size { + if len > max_header_size { + return Err(Parse::TooLarge); + } + } let status = StatusCode::from_u16(res.code.expect("httparse completed"))?; let reason = { @@ -1694,6 +1704,7 @@ mod tests { req_method: &mut method, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1722,6 +1733,7 @@ mod tests { req_method: &mut Some(crate::Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1746,6 +1758,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1767,6 +1780,7 @@ mod tests { req_method: &mut Some(crate::Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1790,6 +1804,7 @@ mod tests { req_method: &mut Some(crate::Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1817,6 +1832,7 @@ mod tests { req_method: &mut Some(crate::Method::GET), h1_parser_config, h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1841,6 +1857,7 @@ mod tests { req_method: &mut Some(crate::Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1869,6 +1886,7 @@ mod tests { req_method: &mut method, h1_parser_config, h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1896,6 +1914,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1916,6 +1935,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: true, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1955,6 +1975,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -1976,6 +1997,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2216,6 +2238,7 @@ mod tests { req_method: &mut Some(Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2237,6 +2260,7 @@ mod tests { req_method: &mut Some(m), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2258,6 +2282,7 @@ mod tests { req_method: &mut Some(Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2828,6 +2853,7 @@ mod tests { req_method: &mut Some(Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2872,6 +2898,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: max_headers, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2896,6 +2923,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: max_headers, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2972,6 +3000,78 @@ mod tests { parse(Some(200), 210, false); } + #[test] + #[cfg(feature = "server")] + fn test_h1_server_max_header_size() { + let _ = pretty_env_logger::try_init(); + + let req_str = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"; + assert_eq!(req_str.len(), 37); + + let parse_req = |max_header_size: Option| { + let mut bytes = BytesMut::from(req_str); + Server::parse( + &mut bytes, + ParseContext { + cached_headers: &mut None, + req_method: &mut None, + h1_parser_config: Default::default(), + h1_max_headers: None, + h1_max_header_size: max_header_size, + preserve_header_case: false, + #[cfg(feature = "ffi")] + preserve_header_order: false, + h09_responses: false, + #[cfg(feature = "client")] + on_informational: &mut None, + }, + ) + }; + + // Server checks + parse_req(None).unwrap().unwrap(); + parse_req(Some(37)).unwrap().unwrap(); + parse_req(Some(50)).unwrap().unwrap(); + assert!(matches!(parse_req(Some(36)), Err(Parse::TooLarge))); + assert!(matches!(parse_req(Some(10)), Err(Parse::TooLarge))); + } + + #[test] + #[cfg(feature = "client")] + fn test_h1_client_max_header_size() { + let _ = pretty_env_logger::try_init(); + + let resp_str = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"; + assert_eq!(resp_str.len(), 38); + + let parse_resp = |max_header_size: Option| { + let mut bytes = BytesMut::from(resp_str); + Client::parse( + &mut bytes, + ParseContext { + cached_headers: &mut None, + req_method: &mut None, + h1_parser_config: Default::default(), + h1_max_headers: None, + h1_max_header_size: max_header_size, + preserve_header_case: false, + #[cfg(feature = "ffi")] + preserve_header_order: false, + h09_responses: false, + #[cfg(feature = "client")] + on_informational: &mut None, + }, + ) + }; + + // Client checks + parse_resp(None).unwrap().unwrap(); + parse_resp(Some(38)).unwrap().unwrap(); + parse_resp(Some(50)).unwrap().unwrap(); + assert!(matches!(parse_resp(Some(37)), Err(Parse::TooLarge))); + assert!(matches!(parse_resp(Some(10)), Err(Parse::TooLarge))); + } + #[test] fn test_is_complete_fast() { let s = b"GET / HTTP/1.1\r\na: b\r\n\r\n"; @@ -3016,6 +3116,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -3099,6 +3200,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -3144,6 +3246,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, diff --git a/src/server/conn/http1.rs b/src/server/conn/http1.rs index 180d507e90..8dc83ab586 100644 --- a/src/server/conn/http1.rs +++ b/src/server/conn/http1.rs @@ -77,6 +77,7 @@ pub struct Builder { h1_title_case_headers: bool, h1_preserve_header_case: bool, h1_max_headers: Option, + h1_max_header_size: Option, h1_header_read_timeout: Dur, h1_writev: Option, max_buf_size: Option, @@ -248,6 +249,7 @@ impl Builder { h1_title_case_headers: false, h1_preserve_header_case: false, h1_max_headers: None, + h1_max_header_size: None, h1_header_read_timeout: Dur::Default(Some(Duration::from_secs(30))), h1_writev: None, max_buf_size: None, @@ -341,6 +343,21 @@ impl Builder { self } + /// Set the maximum size of request headers (including the start line) in bytes. + /// + /// If the client sends headers exceeding this limit, the server responds to the + /// client with "431 Request Header Fields Too Large" and closes the connection. + /// + /// If not configured, then the [`max_buf_size`](Builder::max_buf_size) will naturally be reached and applied. + /// + /// This value is also used as the maximum size limit for chunked trailers. + /// + /// Default is `None`. + pub fn max_header_size(&mut self, val: usize) -> &mut Self { + self.h1_max_header_size = Some(val); + self + } + /// Set a timeout for reading client request headers. If a client does not /// transmit the entire header within this time, the connection is closed. /// @@ -476,6 +493,9 @@ impl Builder { if let Some(max_headers) = self.h1_max_headers { conn.set_http1_max_headers(max_headers); } + if let Some(max_header_size) = self.h1_max_header_size { + conn.set_http1_max_header_size(max_header_size); + } if let Some(dur) = self .timer .check(self.h1_header_read_timeout, "header_read_timeout") diff --git a/tests/client.rs b/tests/client.rs index e4c8d12cf6..4cb6615657 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -2276,6 +2276,71 @@ mod conn { let _res = client.send_request(req).await.expect("send_request"); } + #[tokio::test] + async fn client_max_header_size_exceeded() { + let (server, addr) = setup_std_test_server(); + + thread::spawn(move || { + let mut sock = server.accept().unwrap().0; + let mut buf = [0; 1024]; + sock.read(&mut buf).unwrap(); + sock.write_all(b"HTTP/1.1 200 OK\r\nX-Long: ").unwrap(); + sock.write_all(&[b'a'; 1000]).unwrap(); + sock.write_all(b"\r\n\r\n").unwrap(); + }); + + let tcp = tcp_connect(&addr).await.unwrap(); + + let (mut client, conn) = conn::http1::Builder::new() + .max_header_size(512) + .handshake(tcp) + .await + .unwrap(); + + tokio::spawn(async move { + let _ = conn.await; + }); + + let req = Request::builder() + .uri("/a") + .body(Empty::::new()) + .unwrap(); + let err = client.send_request(req).await.unwrap_err(); + assert!(err.is_parse_too_large()); + } + + #[tokio::test] + async fn client_max_header_size_accepted() { + let (server, addr) = setup_std_test_server(); + + thread::spawn(move || { + let mut sock = server.accept().unwrap().0; + let mut buf = [0; 1024]; + sock.read(&mut buf).unwrap(); + sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") + .unwrap(); + }); + + let tcp = tcp_connect(&addr).await.unwrap(); + + let (mut client, conn) = conn::http1::Builder::new() + .max_header_size(512) + .handshake(tcp) + .await + .unwrap(); + + tokio::spawn(async move { + let _ = conn.await; + }); + + let req = Request::builder() + .uri("/a") + .body(Empty::::new()) + .unwrap(); + let res = client.send_request(req).await.expect("send_request"); + assert_eq!(res.status(), StatusCode::OK); + } + #[tokio::test] async fn client_on_informational_ext() { use std::sync::atomic::{AtomicUsize, Ordering}; diff --git a/tests/server.rs b/tests/server.rs index 098ed7a6ee..094558cb61 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -2784,6 +2784,93 @@ async fn max_buf_size_split_header_boundary() { .expect_err("should TooLarge error"); } +#[cfg(feature = "http1")] +#[tokio::test] +async fn max_header_size_exceeded() { + let (listener, addr) = setup_tcp_listener(); + + const MAX_HEADER: usize = 512; + + thread::spawn(move || { + let mut tcp = connect(&addr); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: x\r\nX-Long: ") + .expect("write 1"); + tcp.write_all(&[b'a'; 1000]).expect("write 2"); + tcp.write_all(b"\r\n\r\n").expect("write 3"); + let mut buf = [0; 256]; + tcp.read(&mut buf).expect("read 1"); + + let expected = "HTTP/1.1 431 "; + assert_eq!(s(&buf[..expected.len()]), expected); + }); + + let (socket, _) = listener.accept().await.unwrap(); + let socket = TokioIo::new(socket); + http1::Builder::new() + .max_header_size(MAX_HEADER) + .serve_connection(socket, HelloWorld) + .await + .expect_err("should TooLarge error"); +} + +#[cfg(feature = "http1")] +#[tokio::test] +async fn max_header_size_accepted() { + let (listener, addr) = setup_tcp_listener(); + + const MAX_HEADER: usize = 512; + + thread::spawn(move || { + let mut tcp = connect(&addr); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n") + .expect("write"); + let mut buf = String::new(); + tcp.read_to_string(&mut buf).expect("read"); + + let expected = "HTTP/1.1 200 "; + assert_eq!(&buf[..expected.len()], expected); + }); + + let (socket, _) = listener.accept().await.unwrap(); + let socket = TokioIo::new(socket); + http1::Builder::new() + .max_header_size(MAX_HEADER) + .serve_connection(socket, HelloWorld) + .await + .expect("should succeed"); +} + +#[cfg(feature = "http1")] +#[tokio::test] +async fn max_header_size_with_large_max_buf_size() { + let (listener, addr) = setup_tcp_listener(); + + const MAX_HEADER: usize = 512; + const MAX_BUF: usize = 64 * 1024; + + thread::spawn(move || { + let mut tcp = connect(&addr); + tcp.write_all(b"GET / HTTP/1.1\r\nHost: x\r\nX-Long: ") + .expect("write 1"); + tcp.write_all(&[b'a'; 1000]).expect("write 2"); + tcp.write_all(b"\r\n\r\n").expect("write 3"); + let mut buf = [0; 256]; + tcp.read(&mut buf).expect("read 1"); + + let expected = "HTTP/1.1 431 "; + assert_eq!(s(&buf[..expected.len()]), expected); + }); + + let (socket, _) = listener.accept().await.unwrap(); + let socket = TokioIo::new(socket); + http1::Builder::new() + .max_buf_size(MAX_BUF) + .max_header_size(MAX_HEADER) + .serve_connection(socket, HelloWorld) + .await + .expect_err("should TooLarge error even with large max_buf_size"); +} + #[cfg(feature = "http1")] #[tokio::test] async fn graceful_shutdown_before_first_request_no_block() { From 5dadff76d1cb5d21046218df3568569fbba8ed21 Mon Sep 17 00:00:00 2001 From: Jeremy Schoemaker Date: Mon, 5 Oct 2026 10:48:45 -0500 Subject: [PATCH 32/57] fix(http1): let a Connection close token win over a later keep-alive (#4201) When parsing a message with more than one `Connection` header line, the per-line logic keyed the branch off the current value of `keep_alive`, so a line that set `keep_alive` to false made the next line take the HTTP/1.0 arm and set it back to true. `Connection: close` followed by `Connection: keep-alive` therefore parsed as keep-alive, while the reverse order parsed correctly. Track whether any `Connection` line carried a `close` token and let that win for the rest of the loop, in both `Server::parse` and `Client::parse`. HTTP/1.0 messages that opt in with `Connection: keep-alive` are unchanged. --- src/proto/h1/role.rs | 188 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 176 insertions(+), 12 deletions(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 262de2480a..f8556db637 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -261,6 +261,7 @@ impl Http1Transaction for Server { }; let mut headers = ctx.cached_headers.take().unwrap_or_default(); + let mut saw_connection_close = false; headers.reserve(headers_len); @@ -313,12 +314,14 @@ impl Http1Transaction for Server { con_len = Some(len); } header::CONNECTION => { - // keep_alive was previously set to default for Version - if keep_alive { - // HTTP/1.1 - keep_alive = !headers::connection_close(&value); - } else { - // HTTP/1.0 + // A message may carry more than one `Connection` line. Once + // any of them asks to close, a later `keep-alive` must not + // undo it. + if saw_connection_close || headers::connection_close(&value) { + saw_connection_close = true; + keep_alive = false; + } else if !keep_alive { + // HTTP/1.0, which defaults to close keep_alive = headers::connection_keep_alive(&value); } } @@ -1112,6 +1115,7 @@ impl Http1Transaction for Client { let mut headers = ctx.cached_headers.take().unwrap_or_default(); let mut keep_alive = version == Version::HTTP_11; + let mut saw_connection_close = false; let mut header_case_map = if ctx.preserve_header_case { Some(HeaderCaseMap::default()) @@ -1134,12 +1138,14 @@ impl Http1Transaction for Client { let value = header_value!(slice.slice(header.value.0..header.value.1)); if let header::CONNECTION = name { - // keep_alive was previously set to default for Version - if keep_alive { - // HTTP/1.1 - keep_alive = !headers::connection_close(&value); - } else { - // HTTP/1.0 + // A message may carry more than one `Connection` line. Once + // any of them asks to close, a later `keep-alive` must not + // undo it. + if saw_connection_close || headers::connection_close(&value) { + saw_connection_close = true; + keep_alive = false; + } else if !keep_alive { + // HTTP/1.0, which defaults to close keep_alive = headers::connection_keep_alive(&value); } } @@ -2554,6 +2560,164 @@ mod tests { ); } + #[cfg(feature = "server")] + #[test] + fn test_parse_request_multiple_connection_headers() { + fn parse(s: &str) -> ParsedMessage { + let mut bytes = BytesMut::from(s); + Server::parse( + &mut bytes, + ParseContext { + cached_headers: &mut None, + req_method: &mut None, + h1_parser_config: Default::default(), + h1_max_headers: None, + preserve_header_case: false, + #[cfg(feature = "ffi")] + preserve_header_order: false, + h09_responses: false, + #[cfg(feature = "client")] + on_informational: &mut None, + }, + ) + .unwrap() + .unwrap() + } + + assert!( + !parse( + "\ + GET / HTTP/1.1\r\n\ + connection: close\r\n\ + connection: keep-alive\r\n\ + \r\n\ + " + ) + .keep_alive, + "close before keep-alive is still close" + ); + + assert!( + !parse( + "\ + GET / HTTP/1.1\r\n\ + connection: keep-alive\r\n\ + connection: close\r\n\ + \r\n\ + " + ) + .keep_alive, + "keep-alive before close is still close" + ); + + assert!( + parse( + "\ + GET / HTTP/1.0\r\n\ + connection: keep-alive\r\n\ + connection: foo\r\n\ + \r\n\ + " + ) + .keep_alive, + "HTTP/1.0 keep-alive is not undone by an unrelated token" + ); + + assert!( + !parse( + "\ + GET / HTTP/1.0\r\n\ + connection: keep-alive\r\n\ + connection: close\r\n\ + \r\n\ + " + ) + .keep_alive, + "HTTP/1.0 close wins over an earlier keep-alive" + ); + } + + #[cfg(feature = "client")] + #[test] + fn test_parse_response_multiple_connection_headers() { + fn parse(s: &str) -> ParsedMessage { + let mut bytes = BytesMut::from(s); + Client::parse( + &mut bytes, + ParseContext { + cached_headers: &mut None, + req_method: &mut Some(Method::GET), + h1_parser_config: Default::default(), + h1_max_headers: None, + preserve_header_case: false, + #[cfg(feature = "ffi")] + preserve_header_order: false, + h09_responses: false, + #[cfg(feature = "client")] + on_informational: &mut None, + }, + ) + .unwrap() + .unwrap() + } + + assert!( + !parse( + "\ + HTTP/1.1 200 OK\r\n\ + content-length: 0\r\n\ + connection: close\r\n\ + connection: keep-alive\r\n\ + \r\n\ + " + ) + .keep_alive, + "close before keep-alive is still close" + ); + + assert!( + !parse( + "\ + HTTP/1.1 200 OK\r\n\ + content-length: 0\r\n\ + connection: keep-alive\r\n\ + connection: close\r\n\ + \r\n\ + " + ) + .keep_alive, + "keep-alive before close is still close" + ); + + assert!( + parse( + "\ + HTTP/1.0 200 OK\r\n\ + content-length: 0\r\n\ + connection: keep-alive\r\n\ + connection: foo\r\n\ + \r\n\ + " + ) + .keep_alive, + "HTTP/1.0 keep-alive is not undone by an unrelated token" + ); + + assert!( + !parse( + "\ + HTTP/1.0 200 OK\r\n\ + content-length: 0\r\n\ + connection: keep-alive\r\n\ + connection: close\r\n\ + \r\n\ + " + ) + .keep_alive, + "HTTP/1.0 close wins over an earlier keep-alive" + ); + } + #[cfg(feature = "client")] #[test] fn test_client_obs_fold_line() { From a5d7fd7861598c0b8c857cb75240a75bcfbd96a3 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 5 Oct 2026 17:47:40 -0400 Subject: [PATCH 33/57] fix(http1): always remove content-length if transfer-encoding is sent (#4223) --- src/proto/h1/role.rs | 64 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index f8556db637..4c91319bc7 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1455,7 +1455,7 @@ impl Client { // This is because we need a second mutable borrow to remove // content-length header. if let Some(encoder) = encoder { - if should_remove_con_len && existing_con_len.is_some() { + if should_remove_con_len { headers.remove(header::CONTENT_LENGTH); } return encoder; @@ -2572,6 +2572,7 @@ mod tests { req_method: &mut None, h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2649,6 +2650,7 @@ mod tests { req_method: &mut Some(Method::GET), h1_parser_config: Default::default(), h1_max_headers: None, + h1_max_header_size: None, preserve_header_case: false, #[cfg(feature = "ffi")] preserve_header_order: false, @@ -2736,6 +2738,66 @@ mod tests { assert_eq!(unfold("obs\r\n fold\r\n\t line"), "obs fold line",); } + #[test] + fn test_client_request_encode_explicit_transfer_encoding_removes_content_length() { + use crate::proto::BodyLength; + use http::header::HeaderValue; + + for values in [ + vec!["10"], + vec![""], + vec!["abc"], + vec!["+10"], + vec!["18446744073709551616"], + vec!["10", "11"], + vec!["10, 11"], + vec!["10", "abc"], + ] { + for transfer_encoding in ["chunked", "gzip"] { + for body in [BodyLength::Known(10), BodyLength::Unknown] { + let mut head = MessageHead::default(); + for value in &values { + head.headers + .append("content-length", HeaderValue::from_static(value)); + } + head.headers.insert( + "transfer-encoding", + HeaderValue::from_static(transfer_encoding), + ); + + let mut vec = Vec::new(); + let encoder = Client::encode( + Encode { + head: &mut head, + body: Some(body), + #[cfg(feature = "server")] + keep_alive: true, + req_method: &mut None, + title_case_headers: false, + #[cfg(feature = "server")] + date_header: true, + }, + &mut vec, + ) + .unwrap(); + + assert!(encoder.is_chunked()); + let expected_te = if transfer_encoding == "chunked" { + "chunked" + } else { + "gzip, chunked" + }; + assert_eq!( + vec, + format!("GET / HTTP/1.1\r\ntransfer-encoding: {expected_te}\r\n\r\n") + .as_bytes(), + "content-length: {values:?}, transfer-encoding: {transfer_encoding}" + ); + } + } + } + } + #[test] fn test_client_request_encode_title_case() { use crate::proto::BodyLength; From 69396fdd960629d69b09cc6c9b367ef8650c001d Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Mon, 5 Oct 2026 18:40:22 -0400 Subject: [PATCH 34/57] fix(http1): remove content-length if transfer-encoding is auto-added (#4224) --- src/proto/h1/role.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 4c91319bc7..fce1df78af 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1422,6 +1422,9 @@ impl Client { Method::GET | Method::HEAD | Method::CONNECT => Some(Encoder::length(0)), _ => { te.insert(HeaderValue::from_static("chunked")); + // A valid Content-Length would have taken precedence above. + // Remove any invalid values now that we're using chunked. + should_remove_con_len = true; Some(Encoder::chunked()) } } @@ -2798,6 +2801,58 @@ mod tests { } } + #[test] + fn test_client_request_encode_automatic_transfer_encoding_removes_content_length() { + use crate::proto::BodyLength; + use http::header::HeaderValue; + + for values in [ + vec![], + vec![""], + vec!["abc"], + vec!["+10"], + vec!["18446744073709551616"], + vec!["10", "11"], + vec!["10, 11"], + vec!["10", "abc"], + vec!["10"], + ] { + let mut head = RequestHead::default(); + head.subject.0 = Method::POST; + for value in &values { + head.headers + .append("content-length", HeaderValue::from_static(value)); + } + + let mut vec = Vec::new(); + let encoder = Client::encode( + Encode { + head: &mut head, + body: Some(BodyLength::Unknown), + #[cfg(feature = "server")] + keep_alive: true, + req_method: &mut None, + title_case_headers: false, + #[cfg(feature = "server")] + date_header: true, + }, + &mut vec, + ) + .unwrap(); + + if values == ["10"] { + assert_eq!(encoder, Encoder::length(10)); + assert_eq!(vec, b"POST / HTTP/1.1\r\ncontent-length: 10\r\n\r\n"); + } else { + assert!(encoder.is_chunked()); + assert_eq!( + vec, b"POST / HTTP/1.1\r\ntransfer-encoding: chunked\r\n\r\n", + "content-length: {values:?}" + ); + } + } + } + #[test] fn test_client_request_encode_title_case() { use crate::proto::BodyLength; From c44f1cc51c2bc45b502031a646d3d54315bd7a16 Mon Sep 17 00:00:00 2001 From: shodoco Date: Tue, 6 Oct 2026 04:56:04 -0700 Subject: [PATCH 35/57] fix(http1): withdraw a stale want when the client dispatcher takes a request (#4208) The Receiver signals want when its queue reports Pending, and SendRequest::is_ready reports that want. A want signaled after finding the queue empty can land after the Sender's give() for the request that follows, and tokio's coop budget can make the queue report Pending with a request already queued. Either way the request is taken with want set, and the connection reports ready until the response completes. hyper-util's legacy pool returns connections on is_ready, so it hands a busy connection to the next request, which then waits behind the whole response. Withdraw the want with want::Taker::unwant when a request is taken. Want is signaled again once the dispatcher is idle. Requires want with Taker::unwant (seanmonstar/want#6). --- Cargo.toml | 7 ++- src/client/dispatch.rs | 6 +++ tests/h1_ready_in_flight.rs | 95 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 tests/h1_ready_in_flight.rs diff --git a/Cargo.toml b/Cargo.toml index 347695fa0f..61f2afa874 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,7 +39,7 @@ itoa = { version = "1", optional = true } pin-project-lite = { version = "0.2.4", optional = true } smallvec = { version = "1.12", features = ["const_generics", "const_new"], optional = true } tracing = { version = "0.1", default-features = false, features = ["std"], optional = true } -want = { version = "0.3", optional = true } +want = { version = "0.3.2", optional = true } [dev-dependencies] form_urlencoded = "1" @@ -347,3 +347,8 @@ required-features = ["full"] name = "h1_shutdown_while_buffered" path = "tests/h1_shutdown_while_buffered.rs" required-features = ["full"] + +[[test]] +name = "h1_ready_in_flight" +path = "tests/h1_ready_in_flight.rs" +required-features = ["full"] diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 0d91a1d92e..37afa9f78d 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -182,6 +182,12 @@ impl Receiver { pub(crate) fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll)>> { match self.inner.poll_recv(cx) { Poll::Ready(item) => { + // A want signaled after finding the queue empty can land after + // the Sender's `give()` for the message taken here, as can one + // signaled on a coop-budget Pending with a message queued. + // Withdraw it, or the Sender reports this connection as ready + // while it is still serving this message. + self.taker.unwant(); Poll::Ready(item.map(|mut env| env.0.take().expect("envelope not dropped"))) } Poll::Pending => { diff --git a/tests/h1_ready_in_flight.rs b/tests/h1_ready_in_flight.rs new file mode 100644 index 0000000000..f2317ba308 --- /dev/null +++ b/tests/h1_ready_in_flight.rs @@ -0,0 +1,95 @@ +// Test: an HTTP/1 client connection must not report ready while a request is +// in flight on it. +// +// The dispatch `Receiver` signals want when its queue reports `Pending`, and +// `SendRequest::is_ready` reports that want. The signal can go stale: the +// connection task can find its queue empty and a request land before it +// signals, or tokio's coop budget can make the queue report `Pending` with a +// request already in it. The request is then taken with want still set, so +// `is_ready` stays true until the response is complete. Pools that return a +// connection on `is_ready`, such as hyper-util's legacy client, then give that +// connection to the next request, which waits behind the whole response. +// +// The coop path is deterministic, so the test drives that one. + +use std::future::Future; +use std::io; +use std::pin::Pin; +use std::task::{Context, Poll}; + +use bytes::Bytes; +use futures_util::future::poll_fn; +use http_body_util::Empty; +use hyper::client::conn::http1::{self, Connection}; +use hyper::rt::{Read, ReadBufCursor, Write}; +use hyper::Request; + +/// Accepts every write and never answers, so a sent request stays in flight. +struct SilentIo; + +impl Read for SilentIo { + fn poll_read( + self: Pin<&mut Self>, + _: &mut Context<'_>, + _: ReadBufCursor<'_>, + ) -> Poll> { + Poll::Pending + } +} + +impl Write for SilentIo { + fn poll_write( + self: Pin<&mut Self>, + _: &mut Context<'_>, + buf: &[u8], + ) -> Poll> { + Poll::Ready(Ok(buf.len())) + } + + fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } +} + +/// Runs the connection task once; it never finishes, as the peer never answers. +fn poll_conn(conn: &mut Connection>, cx: &mut Context<'_>) -> Poll<()> { + assert!(Pin::new(conn).poll(cx).is_pending()); + Poll::Ready(()) +} + +#[tokio::test] +async fn h1_connection_is_not_ready_while_a_request_is_in_flight() { + let (mut sender, mut conn) = http1::handshake(SilentIo).await.unwrap(); + + // Idle, the connection finds its queue empty and signals it is ready. + poll_fn(|cx| poll_conn(&mut conn, cx)).await; + assert!(sender.is_ready()); + + // Kept alive: dropping the response future cancels the request. + let _response = sender.send_request(Request::new(Empty::new())); + assert!(!sender.is_ready()); + + // Out of coop budget, the queue reports Pending with the request in it, so + // the connection signals ready again. A connection task preempted between + // finding its queue empty and signaling leaves the same stale signal. + poll_fn(|cx| { + while let Poll::Ready(restore) = tokio::task::coop::poll_proceed(cx) { + restore.made_progress(); + } + poll_conn(&mut conn, cx) + }) + .await; + tokio::task::yield_now().await; + + // With a fresh budget it takes and writes the request, which then waits + // for a response that never comes. + poll_fn(|cx| poll_conn(&mut conn, cx)).await; + assert!( + !sender.is_ready(), + "connection reports ready with a request in flight" + ); +} From 254bf1271bf4385edd743316ca7c508b3c77d6cb Mon Sep 17 00:00:00 2001 From: 0x676e67 Date: Tue, 6 Oct 2026 20:47:01 +0800 Subject: [PATCH 36/57] perf(body): skip waking a closed peer when a channel end drops (#4225) --- src/body/chan.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/body/chan.rs b/src/body/chan.rs index 314a7d38e2..6e5f9f6f0d 100644 --- a/src/body/chan.rs +++ b/src/body/chan.rs @@ -207,8 +207,11 @@ impl Drop for Sender { fn drop(&mut self) { let mut state = self.shared.state.lock().panic_if_poisoned(); state.sender_open = false; + let receiver_open = state.receiver_open; drop(state); - self.shared.receiver_waker.wake(); + if receiver_open { + self.shared.receiver_waker.wake(); + } } } @@ -216,8 +219,11 @@ impl Drop for Receiver { fn drop(&mut self) { let mut state = self.shared.state.lock().panic_if_poisoned(); state.receiver_open = false; + let sender_open = state.sender_open; drop(state); - self.shared.sender_waker.wake(); + if sender_open { + self.shared.sender_waker.wake(); + } } } @@ -233,6 +239,11 @@ impl fmt::Debug for Sender { #[cfg(test)] mod tests { + use std::{ + sync::atomic::{AtomicUsize, Ordering}, + task::{Wake, Waker}, + }; + use super::*; async fn recv(rx: &mut Receiver) -> Option> { @@ -262,4 +273,44 @@ mod tests { assert!(recv(&mut rx).await.is_none()); assert_eq!(rx.take_trailers().unwrap()["x-trailer"], "value"); } + + #[test] + fn drop_wakes_only_an_open_peer() { + struct Count(AtomicUsize); + + impl Wake for Count { + fn wake(self: Arc) { + self.0.fetch_add(1, Ordering::Relaxed); + } + } + + let count = Arc::new(Count(AtomicUsize::new(0))); + let waker = Waker::from(count.clone()); + let mut cx = Context::from_waker(&waker); + let wakes = || count.0.load(Ordering::Relaxed); + + // A receiver dropped after the sender does not wake the sender's stale waker. + let (mut tx, rx) = channel(true); + assert!(tx.poll_ready(&mut cx).is_pending()); + drop(tx); + drop(rx); + assert_eq!(wakes(), 0); + + // Nor does a sender dropped after the receiver. + let (tx, mut rx) = channel(false); + assert!(rx.poll_next(&mut cx).is_pending()); + drop(rx); + drop(tx); + assert_eq!(wakes(), 0); + + // An open peer is still woken to observe the close. + let (mut tx, rx) = channel(true); + assert!(tx.poll_ready(&mut cx).is_pending()); + drop(rx); + assert_eq!(wakes(), 1); + let (tx, mut rx) = channel(false); + assert!(rx.poll_next(&mut cx).is_pending()); + drop(tx); + assert_eq!(wakes(), 2); + } } From 7f67699ce32d717081deb7018b4da7d679277ac0 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 6 Oct 2026 11:38:03 -0400 Subject: [PATCH 37/57] v1.12.0 --- CHANGELOG.md | 19 +++++++++++++++++++ Cargo.toml | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 309188a22f..30fbb11685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,22 @@ +## v1.12.0 (2026-10-06) + + +#### Bug Fixes + +* **http1:** + * withdraw a stale want when the client dispatcher takes a request (#4208) ([c44f1cc5](https://github.com/hyperium/hyper/commit/c44f1cc51c2bc45b502031a646d3d54315bd7a16)) + * remove content-length if transfer-encoding is auto-added (#4224) ([69396fdd](https://github.com/hyperium/hyper/commit/69396fdd960629d69b09cc6c9b367ef8650c001d)) + * always remove content-length if transfer-encoding is sent (#4223) ([a5d7fd78](https://github.com/hyperium/hyper/commit/a5d7fd7861598c0b8c857cb75240a75bcfbd96a3)) + * let a Connection close token win over a later keep-alive (#4201) ([5dadff76](https://github.com/hyperium/hyper/commit/5dadff76d1cb5d21046218df3568569fbba8ed21)) + * preserve hop-by-hop when setting close or keep-alive (#4196) ([74a39081](https://github.com/hyperium/hyper/commit/74a39081ff1496970fb75da75c5282b23a9b69e5)) +* **http2:** do not reserve capacity for idle Upgraded streams (#4198) ([e89d0d93](https://github.com/hyperium/hyper/commit/e89d0d93d9566210ee95aea234a00952ebc4a202)) + + +#### Features + +* **http1:** add max_header_size limit for server and client (#4183) ([d1cd14ef](https://github.com/hyperium/hyper/commit/d1cd14eff392be63a2318d690cc9380723ec61a1), closes [#3832](https://github.com/hyperium/hyper/issues/3832)) + + ### v1.11.1 (2026-08-27) diff --git a/Cargo.toml b/Cargo.toml index 61f2afa874..f7f7f1e6c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hyper" -version = "1.11.1" +version = "1.12.0" description = "A protective and efficient HTTP library for all." readme = "README.md" homepage = "https://hyper.rs" From f1b0876495cb042d228e8db00740056152ac131c Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 7 Oct 2026 10:57:19 -0400 Subject: [PATCH 38/57] docs(contrib): minor improvements to HIP-0001 (#4227) - Make sure the bare links render as clickable. - Add a note to refrain from using AI/LLMs when writing a HIP. --- docs/hips/0001-hyper-improvement-proposals.md | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/docs/hips/0001-hyper-improvement-proposals.md b/docs/hips/0001-hyper-improvement-proposals.md index 03a4bfc42f..fea679c90a 100644 --- a/docs/hips/0001-hyper-improvement-proposals.md +++ b/docs/hips/0001-hyper-improvement-proposals.md @@ -2,7 +2,7 @@ - Authors: seanmonstar - Created: 2026-09-01 -- PR: https://github.com/hyperium/hyper/pull/4131 +- PR: [#4131](https://github.com/hyperium/hyper/pull/4131) ## Summary @@ -65,6 +65,10 @@ But in general, HIPs should provide the following information: - Appendices - FAQ +#### Do not use AI to write + +AI (LLMs) have their benefits, but since a major reason for writing this is the thinking process, do not use AI to write the actual English in a HIP. + #### What not to include - Minor details that do not need to be discussed to accept the proposal. @@ -122,9 +126,9 @@ Significant changes can be added to a `## History` section of the document, inse ## References -- https://rust-lang.github.io/rfcs/0002-rfc-process.html -- https://rfd.shared.oxide.computer/rfd/0001 -- https://peps.python.org/pep-0001/ -- https://docs.jj-vcs.dev/latest/design_docs/ -- https://blog.ceejbot.com/posts/design-docs/ -- https://adr.github.io/ +- +- +- +- +- +- From 97fff93e53f9e6a993f2d6a25d50883b7b8752a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=A4=E5=9D=82=E7=90=86=E5=AD=90?= Date: Sat, 10 Oct 2026 04:58:30 +0800 Subject: [PATCH 39/57] fix(http1): drain in-flight requests before dispatcher shutdown (#4222) Keep the closed dispatch receiver alive until all in-progress sends have published and canceled their callbacks Preserve the connection result across drain polls and keep upgrade handoffs unchanged Closes #4202 --- src/client/dispatch.rs | 9 ++ src/proto/h1/dispatch.rs | 188 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 185 insertions(+), 12 deletions(-) diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 37afa9f78d..76a9c3e20c 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -203,6 +203,15 @@ impl Receiver { self.inner.close(); } + #[cfg(feature = "http1")] + pub(crate) fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<()> { + self.close(); + // A send that started before close may still be publishing its envelope. + // Keep the receiver alive until all such envelopes have been canceled. + while futures_core::ready!(self.inner.poll_recv(cx)).is_some() {} + Poll::Ready(()) + } + #[cfg(feature = "http1")] pub(crate) fn try_recv(&mut self) -> Option<(T, Callback)> { match crate::common::task::now_or_never(self.inner.recv()) { diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index d98aebcf1f..6ccbecba94 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -25,6 +25,7 @@ pub(crate) struct Dispatcher { body_tx: SenderDropGuard, body_rx: Pin>>, is_closing: bool, + pending_result: Option>, } pub(crate) trait Dispatch { @@ -40,6 +41,9 @@ pub(crate) trait Dispatch { -> crate::Result<()>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll>; fn should_poll(&self) -> bool; + fn poll_close(&mut self, _cx: &mut Context<'_>) -> Poll<()> { + Poll::Ready(()) + } } cfg_server! { @@ -84,6 +88,7 @@ where body_tx: SenderDropGuard::none(), body_rx: Box::pin(None), is_closing: false, + pending_result: None, } } @@ -125,19 +130,33 @@ where cx: &mut Context<'_>, should_shutdown: bool, ) -> Poll> { - Poll::Ready(ready!(self.poll_inner(cx, should_shutdown)).or_else(|e| { - // Be sure to alert a streaming body of the failure with a - // more specific error than the drop guard would provide. - if let Some(mut body) = self.body_tx.take() { - body.send_error(crate::Error::new_body("connection error")); + if self.pending_result.is_none() { + let result = ready!(self.poll_inner(cx, should_shutdown)).or_else(|e| { + // Be sure to alert a streaming body of the failure with a + // more specific error than the drop guard would provide. + if let Some(mut body) = self.body_tx.take() { + body.send_error(crate::Error::new_body("connection error")); + } + // An error means we're shutting down either way. + // We just try to give the error to the user, + // and close the connection with an Ok. If we + // cannot give it to the user, then return the Err. + self.dispatch.recv_msg(Err(e))?; + Ok(Dispatched::Shutdown) + }); + // An upgrade hands off the connection rather than shutting it down. + // Preserve the sender's readiness until the dispatcher is dropped. + if matches!(result, Ok(Dispatched::Upgrade(_))) { + return Poll::Ready(result); } - // An error means we're shutting down either way. - // We just try to give the error to the user, - // and close the connection with an Ok. If we - // cannot give it to the user, then return the Err. - self.dispatch.recv_msg(Err(e))?; - Ok(Dispatched::Shutdown) - })) + self.pending_result = Some(result); + } + + // Closing a channel does not prevent an already-started send from + // publishing later. Drain it before completing so a caller holding + // the sender while awaiting its response cannot strand a callback. + ready!(self.dispatch.poll_close(cx)); + Poll::Ready(self.pending_result.take().expect("closing result missing")) } fn poll_inner( @@ -764,6 +783,10 @@ cfg_client! { fn should_poll(&self) -> bool { self.callback.is_none() } + + fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<()> { + self.rx.poll_close(cx) + } } } @@ -774,6 +797,147 @@ mod tests { use crate::proto::h1::ClientTransaction; use std::time::Duration; + #[cfg(not(miri))] + #[tokio::test] + async fn client_close_drains_pending_requests_on_reset() { + client_close_drains_pending_requests(true, true).await; + } + + #[cfg(not(miri))] + #[tokio::test] + async fn client_close_drains_pending_requests_on_eof() { + client_close_drains_pending_requests(false, true).await; + } + + #[cfg(not(miri))] + #[tokio::test] + async fn client_close_drains_pending_requests_without_shutdown() { + client_close_drains_pending_requests(false, false).await; + } + + #[cfg(not(miri))] + async fn client_close_drains_pending_requests(reset: bool, should_shutdown: bool) { + use std::{cell::Cell, io, rc::Rc}; + + #[derive(Clone, Copy)] + enum ReadState { + Pending, + Response, + Close, + Done, + } + + // Unlike Tokio IO, this reader does not consume cooperative budget, so + // the close is observed while the dispatch channel is forced to yield. + struct ClosingReader { + state: Rc>, + reset: bool, + } + + impl tokio::io::AsyncRead for ClosingReader { + fn poll_read( + self: Pin<&mut Self>, + _cx: &mut Context<'_>, + buf: &mut tokio::io::ReadBuf<'_>, + ) -> Poll> { + match self.state.get() { + ReadState::Pending => Poll::Pending, + ReadState::Response => { + buf.put_slice(b"HTTP/1.1 200 OK\r\ncontent-length: 0\r\n\r\n"); + self.state.set(ReadState::Pending); + Poll::Ready(Ok(())) + } + ReadState::Close => { + self.state.set(ReadState::Done); + if self.reset { + Poll::Ready(Err(io::Error::new( + io::ErrorKind::ConnectionReset, + "reset", + ))) + } else { + Poll::Ready(Ok(())) + } + } + ReadState::Done => panic!("closed IO must not be polled again"), + } + } + } + + let state = Rc::new(Cell::new(ReadState::Pending)); + let io = Compat::new(tokio::io::join( + ClosingReader { + state: state.clone(), + reset, + }, + Vec::::new(), + )); + let (mut tx, rx) = crate::client::dispatch::channel(); + let conn = Conn::<_, bytes::Bytes, ClientTransaction>::new(io); + let mut dispatcher = Dispatcher::new(Client::new(rx), conn); + + // Complete an exchange to put the connection into idle keep-alive. + let mut first = tokio_test::task::spawn( + tx.try_send(crate::Request::new(IncomingBody::empty())) + .unwrap(), + ); + tokio_test::task::spawn(()).enter(|cx, _| { + assert!(Pin::new(&mut dispatcher).poll(cx).is_pending()); + state.set(ReadState::Response); + assert!(Pin::new(&mut dispatcher).poll(cx).is_pending()); + }); + tokio_test::assert_ready_ok!(first.poll()).unwrap(); + assert!(tx.is_ready()); + state.set(ReadState::Close); + let req = crate::Request::builder() + .uri("/not-sent") + .body(IncomingBody::empty()) + .unwrap(); + let promise = tx.try_send(req).unwrap(); + let mut dispatcher = tokio_test::task::spawn(futures_util::future::poll_fn(|cx| { + dispatcher.poll_catch(cx, should_shutdown) + })); + + // Exhaust Tokio's cooperative budget so even a closed channel returns + // Pending when polled. This deterministically exercises the same drain + // path as a send that has reserved a slot but not yet published it. + let (budget_tx, mut budget_rx) = tokio::sync::mpsc::unbounded_channel(); + loop { + budget_tx.send(()).unwrap(); + if task::now_or_never(budget_rx.recv()).is_none() { + break; + } + } + + assert!( + dispatcher.poll().is_pending(), + "must finish draining before exiting" + ); + assert!(tx.is_closed(), "must reject new requests while draining"); + + // The dispatcher must retain its original result while the drain is + // pending, rather than polling the closed connection again. + tokio::task::yield_now().await; + assert!(dispatcher.is_woken()); + let result = tokio_test::assert_ready!(dispatcher.poll()); + if reset { + let err = result.err().expect("connection reset error"); + assert_eq!( + err.find_source::().unwrap().kind(), + std::io::ErrorKind::ConnectionReset, + ); + } else { + assert!(matches!(result, Ok(Dispatched::Shutdown))); + } + + // Keep both the sender and dispatcher alive: cancellation must not + // depend on either one's destructor running. + let mut promise = tokio_test::task::spawn(promise); + let mut err = tokio_test::assert_ready_ok!(promise.poll()).unwrap_err(); + assert!(err.error().is_canceled()); + assert_eq!(err.take_message().unwrap().uri(), "/not-sent"); + assert!(tx.is_closed()); + } + #[test] fn client_read_bytes_before_writing_request() { let _ = pretty_env_logger::try_init(); From 2b454af2127561a447214a11ac3a498ee868062c Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 9 Oct 2026 17:55:03 -0400 Subject: [PATCH 40/57] chore(ci): check that PR descriptions assert that the words are human (#4229) This should be able to run automatically even without our approving of new contributors PRs, so they will hopefully be alerted before we have to read some generated PR description that is overly long. Organization members are skipped, since there is already trust established. --- .github/workflows/pr-template-check.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/pr-template-check.yml diff --git a/.github/workflows/pr-template-check.yml b/.github/workflows/pr-template-check.yml new file mode 100644 index 0000000000..3d73e536b4 --- /dev/null +++ b/.github/workflows/pr-template-check.yml @@ -0,0 +1,11 @@ +name: PR Template Verification + +on: + pull_request_target: + types: [opened, edited, reopened, synchronize] + +permissions: {} + +jobs: + verify-template: + uses: hyperium/.github/.github/workflows/pr-template-check.yml@master From b07337878fe57b34b2065335ae8cae9ce5a780a0 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Tue, 13 Oct 2026 13:47:23 -0400 Subject: [PATCH 41/57] style(proto): enforce clippy:let_unit_value (#4231) see https://github.com/hyperium/hyper/issues/4071. this commit addresses the `let_unit_value` clippy lint. only two sites are affected, relating to tracing spans. this commit introduces a `hyper::trace::Disabled` type to act as a ZST marker in places that can sometimes conditionally compile to a `tracing::span::Entered` guard. this is based upon experimenting in . to restate some of those learnings here: ### background hyper contains a `trace` submodule, introduced in hyperium/hyper#3326, that contains macros like the following: ```rust macro_rules! trace { ($($arg:tt)*) => { #[cfg(feature = "tracing")] { tracing::trace!($($arg)+); } } } macro_rules! trace_span { ($($arg:tt)*) => { { #[cfg(feature = "tracing")] { let _span = tracing::trace_span!($($arg)+); _span.entered() } } } } ``` these thinly wrap `tracing`'s macros, to account for the fact that tracing support in hyper is opt-in. it is gated behind a feature flag, and additionally mandates that the `hyper_unstable_tracing` flag be enabled. however, this leads to violations of clippy's `let_unit_value` rule when compiled without the `tracing` feature flag is enabled. this is because `trace_span!` only returns a `tracing::span::Entered` when that flag is active, otherwise it returns `()`. thus, we *must* bind these calls to a variable, so that we properly hold on to the span guard when the tracing flag is active. however, we must contend with the fact that when this flag is not active, strict clippy linting yields us warnings like this: ```text cargo clippy --no-default-features Checking hyper-span-macro-example v0.1.0 error: this let-binding has unit value --> src/main.rs:52:5 | 52 | let _entered = trace_span!("instrumented"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.99.0/index.html#let_unit_value ``` ### why not use `#[allow(..)]` one might try to selectively apply `#[allow(clippy::let_unit_value)]` attributes to stifle this. however, applying attributes to expressions is still a nightly feature we cannot rely on yet, so call-sites for our macros must be updated rather than modifying the code generated by the macro. see rust-lang/rust#15701. this would require us to do something like this throughout the codebase, wherever we create tracing spans: ```rust #[cfg_attr(not(feature = "tracing"), allow(clippy::let_unit_value))] let _entered = trace_span!("instrumented"); ``` `#[cfg_attr]` is a bit esoteric, and if we require that instrumenting code in hyper require this song and dance every time we create a span, it will generally discourage instrumentation. ### changes this commit introduce a `Disabled` type to replace the empty `()` when tracing instrumentation is disabled. this satisfies clippy's `let_unit_value` rule because macros like `trace_span!` will never return `()` no matter which feature flags are active. this _also_ prevents accidentally writing code that would drop span guards once the tracing flag is enabled, because an inert `#[must_use]` guard acts as a stand-in when the flag isn't present. Signed-off-by: katelyn martin --- Cargo.toml | 1 - src/trace.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f7f7f1e6c4..33fa8fee91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -120,7 +120,6 @@ checked_conversions = "allow" else_if_without_else = "allow" float_arithmetic = "allow" indexing_slicing = "allow" -let_unit_value = "allow" match_wild_err_arm = "allow" multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" diff --git a/src/trace.rs b/src/trace.rs index 88f9a243a0..54f931f1c7 100644 --- a/src/trace.rs +++ b/src/trace.rs @@ -10,6 +10,27 @@ compile_error!( " ); +/// A type representing a RAII guard from a disabled tracing span. +/// +/// When the `tracing` feature flag is not active we do not enter tracing +/// spans. This type does nothing when dropped, and does not guard any +/// resources. +/// +/// This helps avoid expressions that conditionally compile to either `()` or +/// `tracing::span::Entered` depending on which feature flags are enabled. +// +// This ZST (zero sized type) marker only exists when the tracing flag is not +// active. +#[cfg(not(feature = "tracing"))] +// This module allows `unused_macros`. Similarly, we allow dead code in case +// none of the macros exported by this module are used elsewhere. +#[allow(dead_code)] +// This type is marked as a `#[must_use]` type not for its own semantic +// constraints, but to enforce consistency with code paths associated with +// the `tracing` feature flag. +#[must_use = "once a span has been entered, it should be exited"] +pub(crate) struct Disabled; + macro_rules! debug { ($($arg:tt)+) => { #[cfg(feature = "tracing")] @@ -27,6 +48,10 @@ macro_rules! debug_span { let _span = tracing::debug_span!($($arg)+); _span.entered() } + #[cfg(not(feature = "tracing"))] + { + crate::trace::Disabled + } } } } @@ -48,6 +73,10 @@ macro_rules! error_span { let _span = tracing::error_span!($($arg)+); _span.entered() } + #[cfg(not(feature = "tracing"))] + { + crate::trace::Disabled + } } } } @@ -69,6 +98,10 @@ macro_rules! info_span { let _span = tracing::info_span!($($arg)+); _span.entered() } + #[cfg(not(feature = "tracing"))] + { + crate::trace::Disabled + } } } } @@ -90,6 +123,10 @@ macro_rules! trace_span { let _span = tracing::trace_span!($($arg)+); _span.entered() } + #[cfg(not(feature = "tracing"))] + { + crate::trace::Disabled + } } } } @@ -123,6 +160,10 @@ macro_rules! warn_span { let _span = tracing::warn_span!($($arg)+); _span.entered() } + #[cfg(not(feature = "tracing"))] + { + crate::trace::Disabled + } } } } From a8ab2c7fa4583a37e39ed89db6b22bcb30107d40 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 29 Oct 2026 12:48:27 -0700 Subject: [PATCH 42/57] style(lib): enforce clippy::redundant_closure_for_method_calls (#4251) See https://github.com/hyperium/hyper/issues/4071. Clippy lint documentation is at https://clippy.rust-lang.org/master/index.html#redundant_closure_for_method_calls. The rule checks for closures which only invoke a method on the closure argument and can be replaced by referencing the method directly. Only three closures matched this rule, which called a single method without arguments. They were replaced by referencing the method directly. --- Cargo.toml | 1 - src/common/buf.rs | 2 +- src/error.rs | 2 +- src/server/conn/http1.rs | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33fa8fee91..d1738cc1ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -125,7 +125,6 @@ multiple_inherent_impl = "allow" multiple_unsafe_ops_per_block = "allow" needless_pass_by_value = "allow" pattern_type_mismatch = "allow" -redundant_closure_for_method_calls = "allow" redundant_else = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? trivially_copy_pass_by_ref = "allow" diff --git a/src/common/buf.rs b/src/common/buf.rs index 0bf02497f4..fba2aaaebe 100644 --- a/src/common/buf.rs +++ b/src/common/buf.rs @@ -29,7 +29,7 @@ impl BufList { impl Buf for BufList { #[inline] fn remaining(&self) -> usize { - self.bufs.iter().map(|buf| buf.remaining()).sum() + self.bufs.iter().map(Buf::remaining).sum() } #[inline] diff --git a/src/error.rs b/src/error.rs index 1ccd09b7a9..bfbcf90cc5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -354,7 +354,7 @@ impl Error { // Find an h2::Reason somewhere in the cause stack, if it exists, // otherwise assume an INTERNAL_ERROR. self.find_source::() - .and_then(|h2_err| h2_err.reason()) + .and_then(h2::Error::reason) .unwrap_or(h2::Reason::INTERNAL_ERROR) } diff --git a/src/server/conn/http1.rs b/src/server/conn/http1.rs index 8dc83ab586..12ab61cc4f 100644 --- a/src/server/conn/http1.rs +++ b/src/server/conn/http1.rs @@ -555,7 +555,7 @@ where /// Return the inner IO object, and additional information provided the connection /// has not yet been upgraded. pub fn into_parts(self) -> Option> { - self.inner.map(|conn| conn.into_parts()) + self.inner.map(Connection::into_parts) } } From 8abd0b4873f5a5fc3a9d16e72c0fda90dc70dd66 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Fri, 30 Oct 2026 11:31:18 -0700 Subject: [PATCH 43/57] style(lib): enforce clippy::trivially_copy_pass_by_ref (#4252) See https://github.com/hyperium/hyper/issues/4071. Lint doc: https://clippy.rust-lang.org/master/index.html#trivially_copy_pass_by_ref The rule checks for functions taking arguments by reference, where the argument type is Copy and small enough to be more efficient to always pass by value. Only four functions were flagged by this rule, where `&self` was changed to `self`. --- Cargo.toml | 1 - src/body/length.rs | 2 +- src/proto/h1/conn.rs | 4 ++-- src/proto/h1/decode.rs | 4 ++-- src/proto/h1/mod.rs | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d1738cc1ac..7f372a27ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,7 +127,6 @@ needless_pass_by_value = "allow" pattern_type_mismatch = "allow" redundant_else = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? -trivially_copy_pass_by_ref = "allow" unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? unused_trait_names = "allow" # TODO: kinda annoying, but might be good to deny diff --git a/src/body/length.rs b/src/body/length.rs index 1ed5e0d031..ba121bde11 100644 --- a/src/body/length.rs +++ b/src/body/length.rs @@ -81,7 +81,7 @@ impl DecodedLength { /// /// It would return false if "chunked" or otherwise size-unknown. #[cfg(all(any(feature = "client", feature = "server"), feature = "http2"))] - pub(crate) fn is_exact(&self) -> bool { + pub(crate) fn is_exact(self) -> bool { self.0 <= MAX_LEN } } diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 8a5a734940..1c00f6f774 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -1068,8 +1068,8 @@ impl KA { *self = KA::Disabled; } - fn status(&self) -> KA { - *self + fn status(self) -> KA { + self } } diff --git a/src/proto/h1/decode.rs b/src/proto/h1/decode.rs index 9d8ed7f408..c46a5dba80 100644 --- a/src/proto/h1/decode.rs +++ b/src/proto/h1/decode.rs @@ -301,7 +301,7 @@ impl ChunkedState { ChunkedState::Start } fn step( - &self, + self, cx: &mut Context<'_>, body: &mut R, StepArgs { @@ -318,7 +318,7 @@ impl ChunkedState { Body, BodyCr, BodyLf, End, EndCr, EndLf, Extension, Size, SizeLf, SizeLws, Start, Trailer, TrailerLf, }; - match *self { + match self { Start => ChunkedState::read_start(cx, body, chunk_size), Size => ChunkedState::read_size(cx, body, chunk_size), SizeLws => ChunkedState::read_size_lws(cx, body), diff --git a/src/proto/h1/mod.rs b/src/proto/h1/mod.rs index 344d5a8a4f..37045d6856 100644 --- a/src/proto/h1/mod.rs +++ b/src/proto/h1/mod.rs @@ -108,7 +108,7 @@ impl Wants { Wants(self.0 | other.0) } - fn contains(&self, other: Wants) -> bool { + fn contains(self, other: Wants) -> bool { (self.0 & other.0) == other.0 } } From cf563e8a97cc389fd8800a9f178e847dc3203d1f Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Mon, 2 Nov 2026 08:37:23 -0800 Subject: [PATCH 44/57] style(lib): enforce clippy::unnested_or_patterns (#4256) see https://github.com/hyperium/hyper/issues/4071. Lint doc: https://clippy.rust-lang.org/master/index.html#unnested_or_patterns The rule rewrites patterns in disjunctive normal form (DNF) into conjunctive normal form (CNF). Four patterns were flagged by this rule, which were replaced with equivalent nested ones. --- Cargo.toml | 1 - src/body/length.rs | 2 +- src/error.rs | 2 +- src/proto/h1/role.rs | 7 +++---- src/proto/h2/upgrade.rs | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f372a27ed..ed5e00c770 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,7 +127,6 @@ needless_pass_by_value = "allow" pattern_type_mismatch = "allow" redundant_else = "allow" struct_excessive_bools = "allow" # TODO: bogus lint? -unnested_or_patterns = "allow" unused_async = "allow" # TODO: is it for API? unused_trait_names = "allow" # TODO: kinda annoying, but might be good to deny unwrap_in_result = "allow" diff --git a/src/body/length.rs b/src/body/length.rs index ba121bde11..385a8b3c3b 100644 --- a/src/body/length.rs +++ b/src/body/length.rs @@ -68,7 +68,7 @@ impl DecodedLength { ))] pub(crate) fn sub_if(&mut self, amt: u64) { match self { - &mut DecodedLength::CHUNKED | &mut DecodedLength::CLOSE_DELIMITED => (), + &mut (DecodedLength::CHUNKED | DecodedLength::CLOSE_DELIMITED) => (), DecodedLength(known) => { *known -= amt; } diff --git a/src/error.rs b/src/error.rs index bfbcf90cc5..48609ffcb3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -193,7 +193,7 @@ impl Error { pub fn is_parse_too_large(&self) -> bool { matches!( self.inner.kind, - Kind::Parse(Parse::TooLarge) | Kind::Parse(Parse::UriTooLong) + Kind::Parse(Parse::TooLarge | Parse::UriTooLong) ) } diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index fce1df78af..e976931c76 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -493,10 +493,9 @@ impl Http1Transaction for Server { fn on_error(err: &crate::Error) -> Option> { use crate::error::Kind; let status = match *err.kind() { - Kind::Parse(Parse::Method) - | Kind::Parse(Parse::Header(_)) - | Kind::Parse(Parse::Uri) - | Kind::Parse(Parse::Version) => StatusCode::BAD_REQUEST, + Kind::Parse(Parse::Method | Parse::Header(_) | Parse::Uri | Parse::Version) => { + StatusCode::BAD_REQUEST + } Kind::Parse(Parse::TooLarge) => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE, Kind::Parse(Parse::UriTooLong) => StatusCode::URI_TOO_LONG, _ => return None, diff --git a/src/proto/h2/upgrade.rs b/src/proto/h2/upgrade.rs index def1fcfc96..50addbc998 100644 --- a/src/proto/h2/upgrade.rs +++ b/src/proto/h2/upgrade.rs @@ -179,7 +179,7 @@ impl Read for H2Upgraded { } Some(Err(e)) => { return Poll::Ready(match e.reason() { - Some(Reason::NO_ERROR) | Some(Reason::CANCEL) => Ok(()), + Some(Reason::NO_ERROR | Reason::CANCEL) => Ok(()), Some(Reason::STREAM_CLOSED) => { Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, e)) } From c5e100ca0d5497a021894215bad9222099c32203 Mon Sep 17 00:00:00 2001 From: katelyn martin Date: Mon, 9 Nov 2026 10:12:25 -0500 Subject: [PATCH 45/57] chore(ci): deny warnings in 'features' step (#4253) fixes https://github.com/hyperium/hyper/issues/4232. in #4231, we found that we don't deny warnings when running cargo hack, because of the predicate that conditionally gates a crate-level `#![deny(warnings)]` attribute in `lib.rs`: ```rust // src/lib.rs #![cfg_attr(all(test, feature = "full"), deny(unreachable_pub))] #![cfg_attr(all(test, feature = "full"), deny(warnings))] #![cfg_attr(all(test, feature = "nightly"), feature(test))] ``` while this prevents warnings from existing when running `cargo test --features full`, it does not affect other builds. so, this would not be active in the commands run by the `features` step in CI, which runs the following two `cargo-hack` commands: ```sh RUSTFLAGS="-D dead_code -D unused_imports" \ cargo hack --no-dev-deps check \ --feature-powerset --depth 2 --skip ffi,tracing RUSTFLAGS="--cfg hyper_unstable_tracing -D dead_code -D unused_imports" \ cargo hack --no-dev-deps check \ --feature-powerset --depth 2 --features tracing --skip ffi ``` this commit introduces a new feature, intended for internal use in CI, called `__deny_warnings`. this provides another mechanism to deny warnings when building hyper. `cargo hack` invocations run in CI are updated to enable this flag. because `-D dead_code -D unused_imports` are now redundant, the `RUSTFLAGS` values passed to these steps are also accordingly simplified. Signed-off-by: katelyn martin --- .github/workflows/CI.yml | 8 +++----- Cargo.toml | 1 + src/lib.rs | 6 ++++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 5d625ae140..1f8568549f 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -181,14 +181,12 @@ jobs: - uses: Swatinem/rust-cache@v2 - name: check --feature-powerset - run: cargo hack --no-dev-deps check --feature-powerset --depth 2 --skip ffi,tracing - env: - RUSTFLAGS: "-D dead_code -D unused_imports" + run: cargo hack --no-dev-deps check --feature-powerset --depth 2 --features __deny_warnings --skip ffi,tracing - name: check --feature-powerset with tracing feature - run: cargo hack --no-dev-deps check --feature-powerset --depth 2 --features tracing --skip ffi + run: cargo hack --no-dev-deps check --feature-powerset --depth 2 --features tracing,__deny_warnings --skip ffi env: - RUSTFLAGS: "--cfg hyper_unstable_tracing -D dead_code -D unused_imports" + RUSTFLAGS: "--cfg hyper_unstable_tracing" ffi: name: Test C API (FFI) diff --git a/Cargo.toml b/Cargo.toml index ed5e00c770..51db37736f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,7 @@ capi = [] tracing = ["dep:tracing"] # internal features used in CI +__deny_warnings = [] nightly = [] [lints.rust.unexpected_cfgs] diff --git a/src/lib.rs b/src/lib.rs index 8d6e2d2d5a..c7894a608a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,10 @@ #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![cfg_attr(test, deny(rust_2018_idioms))] -#![cfg_attr(all(test, feature = "full"), deny(unreachable_pub))] -#![cfg_attr(all(test, feature = "full"), deny(warnings))] +#![cfg_attr( + any(all(test, feature = "full"), feature = "__deny_warnings"), + deny(warnings, unreachable_pub) +)] #![cfg_attr(all(test, feature = "nightly"), feature(test))] #![cfg_attr(docsrs, feature(doc_cfg))] From b638fc0de351987f3eaffa2bdabd393c8679a9e8 Mon Sep 17 00:00:00 2001 From: Robert Ream Date: Mon, 9 Nov 2026 12:28:32 -0800 Subject: [PATCH 46/57] feat(http2): add an executor getter for server and client (#4250) `Builder::new(exec)` is the only constructor on either HTTP/2 builder and the executor cannot be read back, so a caller that needs it for work alongside a connection keeps a second copy with nothing relating the two. `impl Builder` is unconstrained, so the accessor needs no bound, and returning `&E` leaves cloning to the caller. Signed-off-by: katelyn martin --- src/client/conn/http2.rs | 5 +++++ src/server/conn/http2.rs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/client/conn/http2.rs b/src/client/conn/http2.rs index 4df19a10cc..9e19d26949 100644 --- a/src/client/conn/http2.rs +++ b/src/client/conn/http2.rs @@ -314,6 +314,11 @@ where } } + /// Returns the executor this builder was created with. + pub fn executor(&self) -> &Ex { + &self.exec + } + /// Provide a timer to execute background HTTP2 tasks. pub fn timer(&mut self, timer: M) -> &mut Builder where diff --git a/src/server/conn/http2.rs b/src/server/conn/http2.rs index e4dac78bea..72f28ecbbd 100644 --- a/src/server/conn/http2.rs +++ b/src/server/conn/http2.rs @@ -120,6 +120,11 @@ impl Builder { } } + /// Returns the executor this builder was created with. + pub fn executor(&self) -> &E { + &self.exec + } + /// Configures the maximum number of pending reset streams allowed before a GOAWAY will be sent. /// /// This will default to the default value set by the [`h2` crate](https://crates.io/crates/h2). From 378f8479b8b703d098b46de1cec9ea648f12ea69 Mon Sep 17 00:00:00 2001 From: kevin <5299031+kevin9327@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:01:56 +0900 Subject: [PATCH 47/57] fix(http1): drop Content-Length when a response also has Transfer-Encoding (#4264) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The HTTP/1 server decoder already removes Content-Length when Transfer-Encoding is present. The client decoder preferred Transfer-Encoding for the body length, but still exposed both headers. RFC 7230 §3.3.3 says Transfer-Encoding overrides Content-Length. Remove the Content-Length header from parsed responses so callers cannot treat it as authoritative. Closes #4263 --- src/proto/h1/role.rs | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index e976931c76..18d587610a 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -1161,6 +1161,13 @@ impl Http1Transaction for Client { headers.append(name, value); } + // https://tools.ietf.org/html/rfc7230#section-3.3.3 + // Transfer-Encoding overrides Content-Length; drop the latter so + // the exposed headers match the server-side decoder. + if headers.contains_key(header::TRANSFER_ENCODING) { + headers.remove(header::CONTENT_LENGTH); + } + let mut extensions = http::Extensions::default(); if let Some(header_case_map) = header_case_map { @@ -2406,19 +2413,30 @@ mod tests { DecodedLength::CLOSE_DELIMITED ); - // transfer-encoding and content-length = chunked - assert_eq!( - parse( - "\ - HTTP/1.1 200 OK\r\n\ - content-length: 10\r\n\ - transfer-encoding: chunked\r\n\ - \r\n\ - " - ) - .decode, - DecodedLength::CHUNKED + // transfer-encoding and content-length = chunked; Content-Length is dropped + let msg = parse( + "\ + HTTP/1.1 200 OK\r\n\ + content-length: 100000\r\n\ + transfer-encoding: chunked\r\n\ + \r\n\ + ", ); + assert_eq!(msg.decode, DecodedLength::CHUNKED); + assert!(!msg.head.headers.contains_key(header::CONTENT_LENGTH)); + assert!(msg.head.headers.contains_key(header::TRANSFER_ENCODING)); + + let msg = parse( + "\ + HTTP/1.1 200 OK\r\n\ + transfer-encoding: chunked\r\n\ + content-length: 100000\r\n\ + \r\n\ + ", + ); + assert_eq!(msg.decode, DecodedLength::CHUNKED); + assert!(!msg.head.headers.contains_key(header::CONTENT_LENGTH)); + assert!(msg.head.headers.contains_key(header::TRANSFER_ENCODING)); // HEAD can have content-length, but not body assert_eq!( From 88520c3eaea391f8ba49591a84f8439998b76191 Mon Sep 17 00:00:00 2001 From: Shuvro Roy Date: Sat, 14 Nov 2026 00:51:19 +0600 Subject: [PATCH 48/57] fix(http2): keep connection open after rejecting CONNECT (#4235) Continue accepting streams after resetting a CONNECT request with a nonzero Content-Length. Returning from the server loop closes the entire HTTP/2 connection and interrupts unrelated requests. Add a regression test that keeps a GET in flight during the reset and then sends another GET over the same connection. --- src/proto/h2/server.rs | 2 +- tests/server.rs | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/proto/h2/server.rs b/src/proto/h2/server.rs index 2098758d9e..03b6180385 100644 --- a/src/proto/h2/server.rs +++ b/src/proto/h2/server.rs @@ -286,7 +286,7 @@ where if content_length.map_or(false, |len| len != 0) { warn!("h2 connect request with non-zero body not supported"); respond.send_reset(h2::Reason::INTERNAL_ERROR); - return Poll::Ready(Ok(())); + continue; } let (pending, upgrade) = crate::upgrade::pending(); debug_assert!(parts.extensions.get::().is_none()); diff --git a/tests/server.rs b/tests/server.rs index 094558cb61..b335433afb 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -2047,6 +2047,75 @@ async fn h2_connect() { .unwrap(); } +#[tokio::test] +async fn h2_connect_with_content_length_only_resets_stream() { + tokio::time::timeout(Duration::from_secs(5), async { + let (client_io, server_io) = tokio::io::duplex(4096); + let (started_tx, started_rx) = oneshot::channel(); + let (release_tx, release_rx) = oneshot::channel(); + let pending = Mutex::new(Some((started_tx, release_rx))); + + let svc = service_fn(move |req: Request| { + assert_eq!(req.method(), Method::GET); + let pending = if req.uri().path() == "/pending" { + pending.lock().unwrap().take() + } else { + None + }; + async move { + if let Some((started_tx, release_rx)) = pending { + started_tx.send(()).unwrap(); + release_rx.await.unwrap(); + } + Ok::<_, hyper::Error>(Response::new(Full::new(Bytes::from_static(b"hello")))) + } + }); + let server = tokio::spawn( + http2::Builder::new(TokioExecutor).serve_connection(TokioIo::new(server_io), svc), + ); + + let (mut client, connection) = h2::client::handshake(client_io).await.unwrap(); + let connection = tokio::spawn(connection); + + let request = Request::get("https://localhost/pending").body(()).unwrap(); + let (pending_response, _) = client.send_request(request, true).unwrap(); + started_rx.await.unwrap(); + + let request = Request::connect("localhost:443") + .header("content-length", "1") + .body(()) + .unwrap(); + let (response, request_body) = client.send_request(request, false).unwrap(); + let err = response.await.expect_err("CONNECT should be reset"); + assert_eq!(err.reason(), Some(h2::Reason::INTERNAL_ERROR)); + drop(request_body); + + release_tx.send(()).unwrap(); + let response = pending_response.await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let mut body = response.into_body(); + assert_eq!(body.data().await.unwrap().unwrap(), "hello"); + assert!(body.data().await.is_none()); + drop(body); + + let mut client = client.ready().await.unwrap(); + let request = Request::get("https://localhost/after").body(()).unwrap(); + let (response, _) = client.send_request(request, true).unwrap(); + let response = response.await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + let mut body = response.into_body(); + assert_eq!(body.data().await.unwrap().unwrap(), "hello"); + assert!(body.data().await.is_none()); + drop(body); + + drop(client); + connection.await.unwrap().unwrap(); + server.await.unwrap().unwrap(); + }) + .await + .expect("connection should remain usable after resetting CONNECT"); +} + #[tokio::test] async fn h2_connect_multiplex() { use futures_util::stream::FuturesUnordered; From b1e7eabff4683a4fa6a9f97479736a325437bae6 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 13 Nov 2026 13:51:49 -0500 Subject: [PATCH 49/57] chore(github): add human authorship checkbox to issue templates (#4267) --- .github/ISSUE_TEMPLATE/bug_report.yml | 6 ++++++ .github/ISSUE_TEMPLATE/feature_request.md | 3 +++ 2 files changed, 9 insertions(+) diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 23abc6275d..6d09fd1181 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -2,6 +2,12 @@ name: "Bug report 🐛" description: Create a report to help us improve labels: ["C-bug"] body: + - type: checkboxes + attributes: + label: Human words + description: Communication between humans should be written by a human, not an LLM. + options: + - label: The words are written by a [human](https://seanmonstar.com/micro/20260729-i-want-your-own-words/). - type: input id: version attributes: diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index fa9eaf989e..bb878ca782 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -7,6 +7,9 @@ assignees: '' --- +**Are the words provided written by a human, not an LLM?** +- [ ] The words have been written by a [human](https://seanmonstar.com/micro/20260729-i-want-your-own-words/). + **Is your feature request related to a problem? Please describe.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] From a44e32a7f85a1f98e34307ae17985c0bc1349a03 Mon Sep 17 00:00:00 2001 From: Patrick Hintermayer Date: Mon, 16 Nov 2026 14:48:34 +0100 Subject: [PATCH 50/57] fix(http2): strip all headers named in every Connection header (#4271) HeaderMap::remove returns only the first value, so headers listed in a second Connection line were still sent over HTTP/2. --- src/proto/h2/mod.rs | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/proto/h2/mod.rs b/src/proto/h2/mod.rs index 393d4179ab..0330afe7cf 100644 --- a/src/proto/h2/mod.rs +++ b/src/proto/h2/mod.rs @@ -69,7 +69,7 @@ fn strip_connection_headers(headers: &mut HeaderMap, kind: MessageKind) { warn!("TE headers illegal in HTTP/2 responses"); } - if let Some(header) = headers.remove(CONNECTION) { + if let http::header::Entry::Occupied(entry) = headers.entry(CONNECTION) { warn!( "Connection header illegal in HTTP/2: {}", CONNECTION.as_str() @@ -80,10 +80,14 @@ fn strip_connection_headers(headers: &mut HeaderMap, kind: MessageKind) { // Iterate these names and remove them as headers. Connection-specific headers are // forbidden in HTTP2, as that information has been moved into frame types of the h2 // protocol. - if let Ok(header_contents) = header.to_str() { - for name in header_contents.split(',') { - let name = name.trim(); - headers.remove(name); + let (_, values) = entry.remove_entry_mult(); + let values: Vec<_> = values.collect(); + for header in values { + if let Ok(header_contents) = header.to_str() { + for name in header_contents.split(',') { + let name = name.trim(); + headers.remove(name); + } } } } @@ -327,3 +331,24 @@ impl Buf for SendBuf { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn strip_connection_headers_multiple_values() { + let mut headers = HeaderMap::new(); + headers.append(CONNECTION, "x-a".parse().unwrap()); + headers.append(CONNECTION, "x-b, x-c".parse().unwrap()); + headers.insert("x-a", "1".parse().unwrap()); + headers.insert("x-b", "2".parse().unwrap()); + headers.insert("x-c", "3".parse().unwrap()); + headers.insert("x-d", "4".parse().unwrap()); + + strip_connection_headers(&mut headers, MessageKind::Request); + + assert_eq!(headers.len(), 1); + assert!(headers.contains_key("x-d")); + } +} From 00a85adb71e8429a615d0f98ca7620aafc2f0baf Mon Sep 17 00:00:00 2001 From: Patrick Hintermayer Date: Mon, 16 Nov 2026 17:13:43 +0100 Subject: [PATCH 51/57] perf(http1): read chunk framing bytes without split_to (#4272) The chunked decoder read every size, CRLF and trailer byte through read_mem(1), paying a split_to().freeze() per byte. Add read_byte to MemRead and use it in the byte! macro. About 3x faster decoding for small chunks. --- src/proto/h1/decode.rs | 59 +++++++++++++++++++++++++++++++++++++++--- src/proto/h1/io.rs | 15 +++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/proto/h1/decode.rs b/src/proto/h1/decode.rs index c46a5dba80..78dd2ce754 100644 --- a/src/proto/h1/decode.rs +++ b/src/proto/h1/decode.rs @@ -251,9 +251,8 @@ impl fmt::Debug for Decoder { macro_rules! byte ( ($rdr:ident, $cx:expr) => ({ - let buf = ready!($rdr.read_mem($cx, 1))?; - if !buf.is_empty() { - buf[0] + if let Some(b) = ready!($rdr.read_byte($cx))? { + b } else { return Poll::Ready(Err(io::Error::new(io::ErrorKind::UnexpectedEof, "unexpected EOF during chunk size line"))); @@ -721,6 +720,60 @@ mod tests { } } + impl MemRead for BytesMut { + fn read_mem(&mut self, _: &mut Context<'_>, len: usize) -> Poll> { + let n = std::cmp::min(len, self.len()); + Poll::Ready(Ok(self.split_to(n).freeze())) + } + fn read_byte(&mut self, _: &mut Context<'_>) -> Poll>> { + if self.is_empty() { + return Poll::Ready(Ok(None)); + } + let b = self[0]; + bytes::Buf::advance(self, 1); + Poll::Ready(Ok(Some(b))) + } + } + + #[cfg(all(feature = "nightly", not(miri)))] + fn bench_chunked_body(b: &mut test::Bencher, n_chunks: usize, chunk: usize) { + let rt = new_runtime(); + let mut vec = Vec::new(); + for _ in 0..n_chunks { + vec.extend(format!("{:x}\r\n", chunk).as_bytes()); + vec.extend(std::iter::repeat(b'a').take(chunk)); + vec.extend(b"\r\n"); + } + vec.extend(b"0\r\n\r\n"); + b.bytes = (n_chunks * chunk) as u64; + b.iter(|| { + let mut raw = BytesMut::from(&vec[..]); + let mut decoder = Decoder::chunked(None, None); + rt.block_on(async { + let mut total = 0; + loop { + let d = decoder + .decode_fut(&mut raw) + .await + .unwrap() + .into_data() + .unwrap(); + if d.is_empty() { + break; + } + total += d.len(); + } + assert_eq!(total, n_chunks * chunk); + }); + }); + } + + #[cfg(all(feature = "nightly", not(miri)))] + #[bench] + fn bench_decode_chunked_small_chunks(b: &mut test::Bencher) { + bench_chunked_body(b, 100, 16) + } + impl MemRead for Bytes { fn read_mem(&mut self, _: &mut Context<'_>, len: usize) -> Poll> { let n = std::cmp::min(len, self.len()); diff --git a/src/proto/h1/io.rs b/src/proto/h1/io.rs index bcd0204d2f..f5829a2786 100644 --- a/src/proto/h1/io.rs +++ b/src/proto/h1/io.rs @@ -351,6 +351,11 @@ impl Unpin for Buffered {} // TODO: This trait is old... at least rename to PollBytes or something... pub(crate) trait MemRead { fn read_mem(&mut self, cx: &mut Context<'_>, len: usize) -> Poll>; + + /// Reads one byte, `None` on EOF. + fn read_byte(&mut self, cx: &mut Context<'_>) -> Poll>> { + self.read_mem(cx, 1).map_ok(|b| b.first().copied()) + } } impl MemRead for Buffered @@ -367,6 +372,16 @@ where Poll::Ready(Ok(self.read_buf.split_to(::std::cmp::min(len, n)).freeze())) } } + + // Skips the split/freeze (and its refcount traffic) of `read_mem` for chunk framing bytes. + fn read_byte(&mut self, cx: &mut Context<'_>) -> Poll>> { + if self.read_buf.is_empty() && ready!(self.poll_read_from_io(cx))? == 0 { + return Poll::Ready(Ok(None)); + } + let b = self.read_buf[0]; + self.read_buf.advance(1); + Poll::Ready(Ok(Some(b))) + } } #[derive(Clone, Copy, Debug)] From 750620646ba0777daa45be3d82bd1704dcf470b8 Mon Sep 17 00:00:00 2001 From: Patrick Hintermayer Date: Mon, 16 Nov 2026 18:13:47 +0100 Subject: [PATCH 52/57] fix(http1): error when final body chunk is shorter than content-length (#4270) A body whose only frame ends the stream goes through write_body_and_end, which ignored a short write and just closed the writer. The client then waited forever for a response. Return the same BodyWriteAborted error that end_body already returns for streamed bodies. --- src/proto/h1/conn.rs | 17 +++++++++-------- src/proto/h1/dispatch.rs | 37 ++++++++++++++++++++++++++++++++++++- src/proto/h1/encode.rs | 16 ++++++++++------ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 1c00f6f774..108c1d9e5d 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -778,24 +778,25 @@ where } } - pub(crate) fn write_body_and_end(&mut self, chunk: B) { + pub(crate) fn write_body_and_end(&mut self, chunk: B) -> crate::Result<()> { debug_assert!(self.can_write_body() && self.can_buffer_body()); // empty chunks should be discarded at Dispatcher level debug_assert!(chunk.remaining() != 0); let state = match &mut self.state.writing { - Writing::Body(encoder) => { - let can_keep_alive = encoder.encode_and_end(chunk, self.io.write_buf()); - if can_keep_alive { - Writing::KeepAlive - } else { - Writing::Closed + Writing::Body(encoder) => match encoder.encode_and_end(chunk, self.io.write_buf()) { + Ok(true) => Writing::KeepAlive, + Ok(false) => Writing::Closed, + Err(not_eof) => { + self.state.writing = Writing::Closed; + return Err(crate::Error::new_body_write_aborted().with(not_eof)); } - } + }, _ => unreachable!("write_body invalid state: {:?}", self.state.writing), }; self.state.writing = state; + Ok(()) } pub(crate) fn end_body(&mut self) -> crate::Result<()> { diff --git a/src/proto/h1/dispatch.rs b/src/proto/h1/dispatch.rs index 6ccbecba94..26c62391ec 100644 --- a/src/proto/h1/dispatch.rs +++ b/src/proto/h1/dispatch.rs @@ -433,7 +433,7 @@ where trace!("discarding empty chunk"); self.conn.end_body()?; } else { - self.conn.write_body_and_end(chunk); + self.conn.write_body_and_end(chunk)?; } } else { if chunk.remaining() == 0 { @@ -1005,6 +1005,41 @@ mod tests { assert!(!tx.is_ready()); } + #[tokio::test] + async fn client_body_shorter_than_content_length_errors() { + let _ = pretty_env_logger::try_init(); + + let (io, _handle) = tokio_test::io::Builder::new() + .write(b"POST / HTTP/1.1\r\ncontent-length: 10\r\n\r\nhello") + .wait(std::time::Duration::from_secs(2)) + .build_with_handle(); + + let (mut tx, rx) = crate::client::dispatch::channel(); + let conn = Conn::<_, bytes::Bytes, ClientTransaction>::new(Compat::new(io)); + let dispatcher = Dispatcher::new(Client::new(rx), conn); + let _dispatcher = tokio::spawn(async move { dispatcher.await }); + + // The body ends with its only chunk, so it takes the write_body_and_end path. + let body = { + let (mut tx, body) = IncomingBody::new_channel(DecodedLength::new(5), false); + tx.try_send_data("hello".into()).unwrap(); + body + }; + + let req = crate::Request::builder() + .method("POST") + .header("content-length", "10") + .body(body) + .unwrap(); + + let err = tokio::time::timeout(Duration::from_secs(1), tx.try_send(req).unwrap()) + .await + .expect("request should not hang") + .expect("callback dropped") + .expect_err("short body should error"); + assert!(err.error().is_body_write_aborted(), "{:?}", err.error()); + } + #[cfg(not(miri))] #[tokio::test] async fn body_empty_chunks_ignored() { diff --git a/src/proto/h1/encode.rs b/src/proto/h1/encode.rs index 4a9fd4348b..b4771f2d2f 100644 --- a/src/proto/h1/encode.rs +++ b/src/proto/h1/encode.rs @@ -221,7 +221,11 @@ impl Encoder { } } - pub(super) fn encode_and_end(&self, msg: B, dst: &mut WriteBuf>) -> bool + pub(super) fn encode_and_end( + &self, + msg: B, + dst: &mut WriteBuf>, + ) -> Result where B: Buf, { @@ -235,7 +239,7 @@ impl Encoder { .chain(msg) .chain(b"\r\n0\r\n\r\n" as &'static [u8]); dst.buffer(buf); - !self.is_last + Ok(!self.is_last) } Kind::Length(remaining) => { use std::cmp::Ordering; @@ -244,16 +248,16 @@ impl Encoder { match (len as u64).cmp(&remaining) { Ordering::Equal => { dst.buffer(msg); - !self.is_last + Ok(!self.is_last) } #[allow(clippy::cast_possible_truncation)] Ordering::Greater => { dst.buffer(msg.take(remaining as usize)); - !self.is_last + Ok(!self.is_last) } Ordering::Less => { dst.buffer(msg); - false + Err(NotEof(remaining - len as u64)) } } } @@ -261,7 +265,7 @@ impl Encoder { Kind::CloseDelimited => { trace!("close delimited write {}B", len); dst.buffer(msg); - false + Ok(false) } } } From dea9acb5344e0e34bb9196f5493464db68edd24a Mon Sep 17 00:00:00 2001 From: Azhar Date: Tue, 17 Nov 2026 03:58:44 +0530 Subject: [PATCH 53/57] fix(client): fail parked h2 request with connection error on close (#4275) When the h2 connection drops while a request is still waiting for a stream slot, the parked callback was left to drop, and the caller got a misleading DispatchGone user error. Fail it with the connection error instead: canceled on a clean GOAWAY, the h2 reason otherwise. Adds a regression test with a server that only allows one concurrent stream and closes while the second request is parked. Fixes #4269 --- src/proto/h2/client.rs | 22 ++++++++++++++- tests/client.rs | 62 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/proto/h2/client.rs b/src/proto/h2/client.rs index eb768baa76..6fa1c4b424 100644 --- a/src/proto/h2/client.rs +++ b/src/proto/h2/client.rs @@ -681,7 +681,27 @@ where Ok(()) => (), Err(err) => { self.ping.ensure_not_timed_out()?; - return if err.reason() == Some(::h2::Reason::NO_ERROR) { + let graceful = err.reason() == Some(::h2::Reason::NO_ERROR); + // A request may be parked in fut_ctx waiting for its + // stream to open. Fail it here with the connection + // error instead of letting it fall through to + // Callback::drop, which reports a misleading + // DispatchGone user error. + if let Some(f) = self.fut_ctx.take() { + let cb_err = if graceful { + crate::Error::new_canceled().with("connection closed") + } else { + match err.reason() { + Some(reason) => crate::Error::new_h2(reason.into()), + None => crate::Error::new_canceled().with("connection closed"), + } + }; + f.cb.send(Err(TrySendError { + error: cb_err, + message: None, + })); + } + return if graceful { trace!("connection gracefully shutdown"); Poll::Ready(Ok(Dispatched::Shutdown)) } else { diff --git a/tests/client.rs b/tests/client.rs index 4cb6615657..19cadda79f 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -2971,6 +2971,68 @@ mod conn { upgraded.shutdown().await.unwrap(); } + #[tokio::test] + async fn h2_pending_open_fails_with_connection_error_on_close() { + // A request parked waiting for a stream slot (the server only + // allows one concurrent stream and the first is still open) must + // fail with a connection error when the peer goes away, not with + // a misleading DispatchGone user error. + let (client_io, server_io, _) = setup_duplex_test_server(); + + tokio::spawn(async move { + let sock = server_io; + let mut h2 = h2::server::Builder::new() + .max_concurrent_streams(1) + .handshake::<_, bytes::Bytes>(sock) + .await + .unwrap(); + + // Accept the first request and hold its stream open, so the + // second request parks waiting for a stream slot. + let (_req, respond) = h2.accept().await.unwrap().unwrap(); + tokio::time::sleep(Duration::from_millis(200)).await; + // Close the connection while the second request is parked. + drop(respond); + drop(h2); + }); + + let io = TokioIo::new(client_io); + let (mut client, conn) = conn::http2::Builder::new(TokioExecutor) + .handshake(io) + .await + .expect("http handshake"); + + tokio::spawn(async move { + let _ = conn.await; + }); + + let mut client2 = client.clone(); + let req1 = Request::builder() + .uri("http://localhost/") + .body(Empty::::new()) + .unwrap(); + let res1 = tokio::spawn(async move { client.send_request(req1).await }); + let req2 = Request::builder() + .uri("http://localhost/") + .body(Empty::::new()) + .unwrap(); + let res2 = tokio::spawn(async move { client2.send_request(req2).await }); + + // The first request stays open on the server, the second parks. + // Both fail once the connection closes; the parked one must not + // come back as a user (DispatchGone) error. + let err = res2.await.unwrap().expect_err("parked request must fail"); + assert!( + !err.is_user(), + "parked request failed with a user error instead of a connection error: {err:?}" + ); + assert!( + err.is_canceled(), + "parked request should fail as canceled when the connection closes: {err:?}" + ); + let _ = res1.await; + } + #[tokio::test] async fn h2_connect_rejected() { let (client_io, server_io, _) = setup_duplex_test_server(); From ac3f9501de373388fb9914e285134cfec2283bd8 Mon Sep 17 00:00:00 2001 From: Asuka Date: Tue, 17 Nov 2026 22:25:00 +0900 Subject: [PATCH 54/57] fix(http1): avoid adding connection-close to 101 responses (#4248) Do not append Connection: close to an HTTP/1.1 101 response when HTTP keep-alive is disabled The connection is handed off to the upgraded protocol rather than closed Extend the existing upgrade test to cover a request with Connection: close, Upgrade Verify the response headers while retaining the buffered-byte and upgraded-I/O checks Fixes #4244 --- src/proto/h1/conn.rs | 4 +++- src/proto/h1/mod.rs | 4 ++++ src/proto/h1/role.rs | 4 ++++ tests/server.rs | 12 +++++++++--- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index 108c1d9e5d..6a5bed9fc8 100644 --- a/src/proto/h1/conn.rs +++ b/src/proto/h1/conn.rs @@ -707,7 +707,9 @@ where // to do only speak HTTP/1.0 as well. head.version = Version::HTTP_10; } - Version::HTTP_11 => { + // A 101 hands off the connection to another protocol, so disabled + // HTTP keep-alive must not add a close option to the upgrade response + Version::HTTP_11 if !T::is_switching_protocols(head) => { if let KA::Disabled = self.state.keep_alive.status() { match head.headers.entry(CONNECTION) { Entry::Occupied(mut entry) => { diff --git a/src/proto/h1/mod.rs b/src/proto/h1/mod.rs index 37045d6856..a20e942032 100644 --- a/src/proto/h1/mod.rs +++ b/src/proto/h1/mod.rs @@ -37,6 +37,10 @@ pub(crate) trait Http1Transaction { fn on_error(err: &crate::Error) -> Option>; + fn is_switching_protocols(_head: &MessageHead) -> bool { + false + } + fn is_client() -> bool { !Self::is_server() } diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index 18d587610a..d27c429cab 100644 --- a/src/proto/h1/role.rs +++ b/src/proto/h1/role.rs @@ -509,6 +509,10 @@ impl Http1Transaction for Server { Some(msg) } + fn is_switching_protocols(head: &MessageHead) -> bool { + head.subject == StatusCode::SWITCHING_PROTOCOLS + } + fn is_server() -> bool { true } diff --git a/tests/server.rs b/tests/server.rs index b335433afb..f62e1228a5 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -1819,18 +1819,23 @@ async fn upgrades_new() { b"\ GET / HTTP/1.1\r\n\ Upgrade: foobar\r\n\ - Connection: upgrade\r\n\ + Connection: close, Upgrade\r\n\ \r\n\ eagerly optimistic\ ", ) .expect("write 1"); let mut buf = [0; 256]; - tcp.read(&mut buf).expect("read 1"); + let response = read_until(&mut tcp, |buf| buf.ends_with(b"\r\n\r\n")).expect("read 1"); - let response = s(&buf); + let response = s(&response); assert!(response.starts_with("HTTP/1.1 101 Switching Protocols\r\n")); assert!(!has_header(response, "content-length")); + assert!( + response.contains("\r\nconnection: upgrade\r\n"), + "{response}" + ); + assert!(!response.contains("close"), "{response}"); let _ = read_101_tx.send(()); let n = tcp.read(&mut buf).expect("read 2"); @@ -1846,6 +1851,7 @@ async fn upgrades_new() { Response::builder() .status(101) .header("upgrade", "foobar") + .header("connection", "upgrade") .body(Empty::::new()) .unwrap(), ) From 47c725c459ede66e68ad705f7da9a901abbbdc17 Mon Sep 17 00:00:00 2001 From: Asuka Date: Tue, 17 Nov 2026 22:40:11 +0900 Subject: [PATCH 55/57] fix(http1): prevent `SendRequest` from reporting ready while a request is pending (#4247) Represent the initial buffer slot with a want and signal each subsequent want only once per receive cycle Do not restore readiness on Pending or withdraw a grant when dequeuing a request Extend the existing regression to check readiness while the request is still queued Fixes #4246 --- src/client/dispatch.rs | 69 ++++++++++++++----------------------- tests/h1_ready_in_flight.rs | 31 ++++++++++------- 2 files changed, 44 insertions(+), 56 deletions(-) diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 76a9c3e20c..143fc5a9cb 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -30,14 +30,16 @@ pub struct TrySendError { pub(crate) fn channel() -> (Sender, Receiver) { let (tx, rx) = mpsc::unbounded_channel(); - let (giver, taker) = want::new(); - let tx = Sender { - #[cfg(feature = "http1")] - buffered_once: false, - giver, - inner: tx, + let (giver, mut taker) = want::new(); + // Seed with one want so the first request can be sent before the + // receiver is polled; every request must consume a want + taker.want(); + let tx = Sender { giver, inner: tx }; + let rx = Receiver { + inner: rx, + taker, + wanting: true, }; - let rx = Receiver { inner: rx, taker }; (tx, rx) } @@ -46,17 +48,11 @@ pub(crate) fn channel() -> (Sender, Receiver) { /// While the inner sender is unbounded, the Giver is used to determine /// if the Receiver is ready for another request. pub(crate) struct Sender { - /// One message is always allowed, even if the Receiver hasn't asked - /// for it yet. This boolean keeps track of whether we've sent one - /// without notice. - #[cfg(feature = "http1")] - buffered_once: bool, - /// The Giver helps watch that the Receiver side has been polled - /// when the queue is empty. This helps us know when a request and - /// response have been fully processed, and a connection is ready - /// for more. + // Controls whether the sender is permitted to dispatch a request + // Beyond the initial request, the Receiver only grants readiness once + // the preceding request/response cycle has fully completed giver: want::Giver, - /// Actually bounded by the Giver, plus `buffered_once`. + // Capacity is enforced via the Giver inner: mpsc::UnboundedSender>, } @@ -89,23 +85,9 @@ impl Sender { self.giver.is_canceled() } - #[cfg(feature = "http1")] - fn can_send(&mut self) -> bool { - if self.giver.give() || !self.buffered_once { - // If the receiver is ready *now*, then of course we can send. - // - // If the receiver isn't ready yet, but we don't have anything - // in the channel yet, then allow one message. - self.buffered_once = true; - true - } else { - false - } - } - #[cfg(feature = "http1")] pub(crate) fn try_send(&mut self, val: T) -> Result, T> { - if !self.can_send() { + if !self.giver.give() { return Err(val); } let (tx, rx) = oneshot::channel(); @@ -117,7 +99,7 @@ impl Sender { #[cfg(feature = "http1")] pub(crate) fn send(&mut self, val: T) -> Result, T> { - if !self.can_send() { + if !self.giver.give() { return Err(val); } let (tx, rx) = oneshot::channel(); @@ -176,24 +158,25 @@ impl Clone for UnboundedSender { pub(crate) struct Receiver { inner: mpsc::UnboundedReceiver>, taker: want::Taker, + // Tracks whether readiness (`want`) has already been granted for the next request + wanting: bool, } impl Receiver { pub(crate) fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll)>> { + if !self.wanting { + self.taker.want(); + self.wanting = true; + } match self.inner.poll_recv(cx) { Poll::Ready(item) => { - // A want signaled after finding the queue empty can land after - // the Sender's `give()` for the message taken here, as can one - // signaled on a coop-budget Pending with a message queued. - // Withdraw it, or the Sender reports this connection as ready - // while it is still serving this message. - self.taker.unwant(); + self.wanting = false; Poll::Ready(item.map(|mut env| env.0.take().expect("envelope not dropped"))) } - Poll::Pending => { - self.taker.want(); - Poll::Pending - } + // Avoid re-signaling `want` here: the sender might have already claimed it + // and enqueued a request, even if polling the channel returns `Pending` + // due to Tokio's coop budget exhaustion or a concurrent send + Poll::Pending => Poll::Pending, } } diff --git a/tests/h1_ready_in_flight.rs b/tests/h1_ready_in_flight.rs index f2317ba308..19edc104cc 100644 --- a/tests/h1_ready_in_flight.rs +++ b/tests/h1_ready_in_flight.rs @@ -1,16 +1,15 @@ // Test: an HTTP/1 client connection must not report ready while a request is // in flight on it. // -// The dispatch `Receiver` signals want when its queue reports `Pending`, and -// `SendRequest::is_ready` reports that want. The signal can go stale: the -// connection task can find its queue empty and a request land before it -// signals, or tokio's coop budget can make the queue report `Pending` with a -// request already in it. The request is then taken with want still set, so -// `is_ready` stays true until the response is complete. Pools that return a -// connection on `is_ready`, such as hyper-util's legacy client, then give that -// connection to the next request, which waits behind the whole response. +// Re-granting `want` on every `Pending` poll from the dispatch channel can +// falsely mark the connection as ready while a request is already in flight +// (eg, during a concurrent send or when Tokio's coop budget runs out with +// a buffered request) +// Revoking readiness when the request is dequeued is too late, as the caller +// may have already seen `Ready` and will fail on send // -// The coop path is deterministic, so the test drives that one. +// Deterministically trigger coop exhaustion and verify readiness state both +// before and after dequeuing the buffered request use std::future::Future; use std::io; @@ -73,14 +72,20 @@ async fn h1_connection_is_not_ready_while_a_request_is_in_flight() { let _response = sender.send_request(Request::new(Empty::new())); assert!(!sender.is_ready()); - // Out of coop budget, the queue reports Pending with the request in it, so - // the connection signals ready again. A connection task preempted between - // finding its queue empty and signaling leaves the same stale signal. + // With the coop budget exhausted, polling the queue yields `Pending` even + // though a request is buffered + // Readiness must not be re-signaled here poll_fn(|cx| { while let Poll::Ready(restore) = tokio::task::coop::poll_proceed(cx) { restore.made_progress(); } - poll_conn(&mut conn, cx) + let _ = poll_conn(&mut conn, cx); + assert!( + sender.poll_ready(cx).is_pending(), + "dispatcher should not report ready while a request is still buffered" + ); + assert!(!sender.is_ready()); + Poll::Ready(()) }) .await; tokio::task::yield_now().await; From 5634b91d607ba1b781699765b22f16fd6ec1c8b7 Mon Sep 17 00:00:00 2001 From: mingley <46031485+mingley@users.noreply.github.com> Date: Tue, 17 Nov 2026 07:59:12 -0600 Subject: [PATCH 56/57] fix(http2): prevent requests from hanging during concurrent connection shutdown race (#4242) --- src/client/dispatch.rs | 2 -- src/proto/h2/client.rs | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 143fc5a9cb..c3140cf6d7 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -180,13 +180,11 @@ impl Receiver { } } - #[cfg(feature = "http1")] pub(crate) fn close(&mut self) { self.taker.cancel(); self.inner.close(); } - #[cfg(feature = "http1")] pub(crate) fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<()> { self.close(); // A send that started before close may still be publishing its envelope. diff --git a/src/proto/h2/client.rs b/src/proto/h2/client.rs index 6fa1c4b424..c7dcd1e1e2 100644 --- a/src/proto/h2/client.rs +++ b/src/proto/h2/client.rs @@ -201,6 +201,7 @@ where h2_tx, req_rx, fut_ctx: None, + closing: None, marker: PhantomData, }) } @@ -434,6 +435,7 @@ where h2_tx: SendRequest>, req_rx: ClientRx, fut_ctx: Option>, + closing: Option>, marker: PhantomData, } @@ -676,6 +678,24 @@ where type Output = crate::Result; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + if self.closing.is_none() { + let res = ready!(self.poll_dispatch(cx)); + self.closing = Some(res); + } + ready!(self.req_rx.poll_close(cx)); + Poll::Ready(self.closing.take().expect("Future polled twice")) + } +} + +impl ClientTask +where + B: Body + 'static + Unpin, + B::Data: Send, + B::Error: Into>, + E: Http2ClientConnExec + Clone + Unpin, + T: Read + Write + Unpin, +{ + fn poll_dispatch(&mut self, cx: &mut Context<'_>) -> Poll> { loop { match ready!(self.h2_tx.poll_ready(cx)) { Ok(()) => (), From 6b7673f9f04f8938fef1158cf87d78c6bfeabafe Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Tue, 17 Nov 2026 09:16:22 -0500 Subject: [PATCH 57/57] test(http2): test for http2 connection shutdown race of pending reqs (#4276) --- tests/client.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/client.rs b/tests/client.rs index 19cadda79f..d422338a6a 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -2464,6 +2464,70 @@ mod conn { .unwrap(); } + #[tokio::test] + async fn http2_shutdown_drains_queued_request_before_resolving() { + use std::future::Future; + + #[derive(Clone)] + struct CaptureExecutor(std::sync::mpsc::Sender + Send>>>); + + impl hyper::rt::Executor for CaptureExecutor + where + F: Future + Send + 'static, + { + fn execute(&self, future: F) { + self.0.send(Box::pin(future)).unwrap(); + } + } + + let (io, peer) = tokio::io::duplex(4096); + let (tasks_tx, tasks_rx) = std::sync::mpsc::channel(); + let (mut client, mut conn) = conn::http2::Builder::new(CaptureExecutor(tasks_tx)) + .handshake::<_, Empty>(TokioIo::new(io)) + .await + .unwrap(); + + drop(peer); + // Finish the transport task without polling the request dispatcher. + let connection_task = tasks_rx.try_recv().unwrap(); + tokio::time::timeout(Duration::from_secs(5), connection_task) + .await + .expect("transport task should finish"); + + let response = client.try_send_request(Request::new(Empty::::new())); + + let first_poll = future::poll_fn(|cx| { + // Force the dispatch receiver to yield even though a request is queued. + loop { + let budget = tokio::task::consume_budget(); + tokio::pin!(budget); + if budget.poll(cx).is_pending() { + break; + } + } + Poll::Ready(Pin::new(&mut conn).poll(cx)) + }) + .await; + assert!( + first_poll.is_pending(), + "shutdown must wait for dispatch to drain" + ); + + let _ = tokio::time::timeout(Duration::from_secs(5), &mut conn) + .await + .expect("connection should finish draining"); + let err = tokio::time::timeout(Duration::from_secs(5), response) + .await + .expect("queued request should be canceled while connection is retained") + .expect_err("request should fail"); + assert!(err.error().is_canceled()); + assert!( + err.message().is_some(), + "unsent request should be recoverable" + ); + drop(conn); + } + #[tokio::test] async fn http2_detect_conn_eof() { use futures_util::future;