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 [...] diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a5d35e10ff..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) @@ -298,12 +296,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 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 diff --git a/CHANGELOG.md b/CHANGELOG.md index b58e22bbf7..30fbb11685 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,34 @@ +## 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) + + +#### 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 22826d9900..51db37736f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hyper" -version = "1.11.0" +version = "1.12.0" description = "A protective and efficient HTTP library for all." readme = "README.md" homepage = "https://hyper.rs" @@ -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" @@ -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 @@ -96,6 +96,7 @@ capi = [] tracing = ["dep:tracing"] # internal features used in CI +__deny_warnings = [] nightly = [] [lints.rust.unexpected_cfgs] @@ -115,42 +116,26 @@ 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" -collapsible_match = "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" -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 -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" -needless_continue = "allow" needless_pass_by_value = "allow" -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" -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" +rest_pattern_accessible_field = "allow" +needless_bool = "allow" # explicitly allowed absolute_paths = "allow" # sometimes its cleaner @@ -359,3 +344,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/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 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/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..45d7a515c9 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: @@ -116,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/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 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 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..fea679c90a --- /dev/null +++ b/docs/hips/0001-hyper-improvement-proposals.md @@ -0,0 +1,134 @@ +# HIP-0001: hyper Improvement Proposals + +- Authors: seanmonstar +- Created: 2026-09-01 +- PR: [#4131](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 + +#### 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. + +### 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 + +- +- +- +- +- +- diff --git a/src/body/chan.rs b/src/body/chan.rs new file mode 100644 index 0000000000..6e5f9f6f0d --- /dev/null +++ b/src/body/chan.rs @@ -0,0 +1,316 @@ +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)] + #[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()) + } + + 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; + let receiver_open = state.receiver_open; + drop(state); + if receiver_open { + 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; + let sender_open = state.sender_open; + drop(state); + if sender_open { + 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 std::{ + sync::atomic::{AtomicUsize, Ordering}, + task::{Wake, Waker}, + }; + + 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"); + } + + #[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); + } +} diff --git a/src/body/incoming.rs b/src/body/incoming.rs index a0677e7e58..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,119 +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)] - 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; @@ -473,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; @@ -491,11 +332,9 @@ mod tests { body_expected_size, ); - //assert_eq!(body_size, mem::size_of::>(), "Option"); - assert_eq!( mem::size_of::(), - mem::size_of::() * 5, + mem::size_of::() * 2, "Sender" ); @@ -506,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/length.rs b/src/body/length.rs index 1ed5e0d031..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; } @@ -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/body/mod.rs b/src/body/mod.rs index 2df3319dd5..42678b63d1 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; @@ -34,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/client/conn/http1.rs b/src/client/conn/http1.rs index ec64543c23..43c2564280 100644 --- a/src/client/conn/http1.rs +++ b/src/client/conn/http1.rs @@ -97,6 +97,11 @@ 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. + #[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>> { @@ -127,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, @@ -137,6 +143,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 +169,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 +222,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, @@ -222,7 +239,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"); @@ -236,10 +253,11 @@ 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. + #[allow(clippy::result_large_err)] pub fn try_send_request( &mut self, req: Request, @@ -251,7 +269,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"); @@ -336,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, @@ -484,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 @@ -533,6 +567,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, @@ -567,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/client/conn/http2.rs b/src/client/conn/http2.rs index ec046e0a39..9e19d26949 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, @@ -159,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"); @@ -174,10 +185,11 @@ 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. + #[allow(clippy::result_large_err)] pub fn try_send_request( &mut self, req: Request, @@ -189,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"); @@ -302,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 @@ -547,6 +564,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/client/dispatch.rs b/src/client/dispatch.rs index 69c9b8cdaa..c3140cf6d7 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,41 +85,27 @@ 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(); 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) } #[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(); 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 +132,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 +140,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) } } @@ -176,27 +158,41 @@ 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) => { + 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, } } - #[cfg(feature = "http1")] pub(crate) fn close(&mut self) { self.taker.cancel(); self.inner.close(); } + 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()) { @@ -408,7 +404,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..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] @@ -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/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/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/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/time.rs b/src/common/time.rs index b3534f1580..c238b4b3d4 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,7 +80,8 @@ impl Time { Time::Timer(..) => Some(dur), }, Dur::Configured(Some(dur)) => match self { - Time::Empty => panic!("timeout `{name}` set, but no timer set",), + #[allow(clippy::panic)] + 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/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) - } -} diff --git a/src/error.rs b/src/error.rs index 1ccd09b7a9..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) ) } @@ -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/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/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..ba6886b835 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) } @@ -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() } 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/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/lib.rs b/src/lib.rs index 7be9926076..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))] @@ -33,10 +35,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 //! diff --git a/src/proto/h1/conn.rs b/src/proto/h1/conn.rs index a2d2670e6b..6a5bed9fc8 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::{Entry, HeaderValue, CONNECTION}; use http::{HeaderMap, Method, Version}; use http_body::Frame; use httparse::ParserConfig; @@ -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, @@ -325,11 +331,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)))) } @@ -668,10 +670,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 { @@ -680,12 +683,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")); + } + }, _ => (), } } @@ -702,10 +707,20 @@ 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() { - 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")); + } + } } } _ => (), @@ -765,24 +780,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<()> { @@ -939,6 +955,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")] @@ -1009,7 +1026,7 @@ impl fmt::Debug for State { // Purposefully leaving off other fields.. - builder.finish() + builder.finish_non_exhaustive() } } @@ -1054,8 +1071,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 bdfdf79abf..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"))); @@ -286,12 +285,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, } @@ -301,7 +300,7 @@ impl ChunkedState { ChunkedState::Start } fn step( - &self, + self, cx: &mut Context<'_>, body: &mut R, StepArgs { @@ -314,8 +313,11 @@ impl ChunkedState { max_headers_bytes, }: StepArgs<'_>, ) -> Poll> { - use self::ChunkedState::*; - match *self { + 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), SizeLws => ChunkedState::read_size_lws(cx, body), @@ -649,7 +651,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:?}"), )); } }; @@ -659,7 +661,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:?}"), )); } }; @@ -718,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/dispatch.rs b/src/proto/h1/dispatch.rs index 03aaba1a9e..26c62391ec 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( @@ -414,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 { @@ -430,7 +449,6 @@ where ); } else { trace!("discarding unknown frame"); - continue; } } else { *clear_body = true; @@ -510,10 +528,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) } @@ -765,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) + } } } @@ -775,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(); @@ -842,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 a1eebafa12..b4771f2d2f 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")] @@ -216,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, { @@ -230,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; @@ -239,15 +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)) } } } @@ -255,7 +265,7 @@ impl Encoder { Kind::CloseDelimited => { trace!("close delimited write {}B", len); dst.buffer(msg); - false + Ok(false) } } } @@ -317,7 +327,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), @@ -328,11 +338,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 +375,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 +392,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/h1/io.rs b/src/proto/h1/io.rs index efec6fef4a..f5829a2786 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() } } @@ -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())); @@ -240,7 +247,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 @@ -344,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 @@ -360,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)] @@ -534,12 +556,7 @@ impl WriteBuf { strategy, } } -} -impl WriteBuf -where - B: Buf, -{ fn set_strategy(&mut self, strategy: WriteStrategy) { self.strategy = strategy; } @@ -601,7 +618,7 @@ impl fmt::Debug for WriteBuf { f.debug_struct("WriteBuf") .field("remaining", &self.remaining()) .field("strategy", &self.strategy) - .finish() + .finish_non_exhaustive() } } @@ -637,7 +654,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 } @@ -657,19 +674,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() { @@ -715,6 +719,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 0a442c4c7e..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() } @@ -68,26 +72,27 @@ 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, + h1_max_header_size: Option, preserve_header_case: bool, #[cfg(feature = "ffi")] 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, @@ -107,7 +112,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 } } diff --git a/src/proto/h1/role.rs b/src/proto/h1/role.rs index f5c8db5ed0..d27c429cab 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; } } @@ -177,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 { @@ -253,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); @@ -305,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); } } @@ -430,6 +441,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:?}"), } @@ -481,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, @@ -498,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 } @@ -1049,6 +1064,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 = { @@ -1098,6 +1118,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()) @@ -1120,12 +1141,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); } } @@ -1142,6 +1165,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 { @@ -1218,6 +1248,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"); @@ -1401,6 +1432,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()) } } @@ -1434,7 +1468,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; @@ -1649,7 +1683,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<'_> { @@ -1689,6 +1723,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, @@ -1717,6 +1752,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, @@ -1741,6 +1777,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, @@ -1762,6 +1799,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, @@ -1785,6 +1823,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, @@ -1812,6 +1851,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, @@ -1836,6 +1876,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, @@ -1864,6 +1905,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, @@ -1891,6 +1933,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, @@ -1911,6 +1954,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, @@ -1950,6 +1994,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, @@ -1971,6 +2016,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, @@ -2211,6 +2257,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, @@ -2232,6 +2279,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, @@ -2253,6 +2301,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, @@ -2368,19 +2417,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!( @@ -2524,6 +2584,166 @@ 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, + h1_max_header_size: 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, + h1_max_header_size: 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() { @@ -2542,6 +2762,118 @@ 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_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; @@ -2823,6 +3155,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, @@ -2867,6 +3200,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, @@ -2891,6 +3225,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, @@ -2967,6 +3302,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"; @@ -2977,6 +3384,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 +3398,37 @@ 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, + h1_max_header_size: 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] @@ -3060,6 +3502,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, @@ -3105,6 +3548,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/h2/client.rs b/src/proto/h2/client.rs index 4803dba0f1..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,12 +678,50 @@ 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(()) => (), 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 { @@ -772,7 +812,6 @@ where } } self.poll_pipe(f, cx); - continue; } Poll::Ready(None) => { diff --git a/src/proto/h2/mod.rs b/src/proto/h2/mod.rs index f0db9b64eb..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); + } } } } @@ -319,7 +323,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), @@ -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")); + } +} diff --git a/src/proto/h2/ping.rs b/src/proto/h2/ping.rs index 198bff465c..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; } @@ -395,8 +395,11 @@ 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; + 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; diff --git a/src/proto/h2/server.rs b/src/proto/h2/server.rs index 92026276aa..03b6180385 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(); @@ -287,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/src/proto/h2/upgrade.rs b/src/proto/h2/upgrade.rs index f4e65466a2..50addbc998 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 @@ -236,16 +172,14 @@ 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; } 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)) } @@ -330,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/src/rt/io.rs b/src/rt/io.rs index 447774c6c0..31e699d73a 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> { @@ -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..12ab61cc4f 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, @@ -178,6 +179,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| { @@ -247,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, @@ -340,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. /// @@ -475,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") @@ -534,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) } } 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). 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 + } } } } diff --git a/src/upgrade.rs b/src/upgrade.rs index 470f4ca435..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() { @@ -405,7 +407,6 @@ mod tests { _: &mut Context<'_>, buf: &[u8], ) -> Poll> { - // panic!("poll_write shouldn't be called"); Poll::Ready(Ok(buf.len())) } diff --git a/tests/client.rs b/tests/client.rs index b512260cc5..d422338a6a 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}; @@ -2247,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}; @@ -2370,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; @@ -2877,6 +3035,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(); @@ -3315,6 +3535,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 { diff --git a/tests/h1_ready_in_flight.rs b/tests/h1_ready_in_flight.rs new file mode 100644 index 0000000000..19edc104cc --- /dev/null +++ b/tests/h1_ready_in_flight.rs @@ -0,0 +1,100 @@ +// Test: an HTTP/1 client connection must not report ready while a request is +// in flight on it. +// +// 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 +// +// Deterministically trigger coop exhaustion and verify readiness state both +// before and after dequeuing the buffered request + +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()); + + // 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(); + } + 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; + + // 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" + ); +} diff --git a/tests/server.rs b/tests/server.rs index 855840b388..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(), ) @@ -2047,6 +2053,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; @@ -2784,6 +2859,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() { @@ -3335,6 +3497,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(); @@ -3469,8 +3704,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<'_> {