From 893bb75e3bab9697e78a8b3529edab7bac8b135b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ml=C3=A1dek?= Date: Thu, 7 Nov 2024 17:27:41 +0100 Subject: [PATCH 1/4] CI: allow `pin-project-lite` in public dependencies (#3020) --- axum-extra/Cargo.toml | 1 + axum/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index 266ed3b8..6fd81f92 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -98,6 +98,7 @@ allowed = [ "headers_core", "http", "http_body", + "pin_project_lite", "prost", "serde", "tokio", diff --git a/axum/Cargo.toml b/axum/Cargo.toml index c47a1ead..4b94caef 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -199,6 +199,7 @@ allowed = [ "futures_core", "futures_sink", "futures_util", + "pin_project_lite", "tower_layer", "tower_service", From ce3d42947eff69b5e244efc4436512ce54733120 Mon Sep 17 00:00:00 2001 From: Sabrina Jewson Date: Sat, 16 Nov 2024 15:50:25 +0000 Subject: [PATCH 2/4] fix: Avoid setting content-length before middleware (#3031) --- axum/src/routing/route.rs | 28 +++++++++++++++++----------- axum/src/routing/tests/mod.rs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/axum/src/routing/route.rs b/axum/src/routing/route.rs index 8fc9a1d7..6b896114 100644 --- a/axum/src/routing/route.rs +++ b/axum/src/routing/route.rs @@ -18,7 +18,7 @@ use std::{ task::{Context, Poll}, }; use tower::{ - util::{BoxCloneService, MapErrLayer, MapRequestLayer, MapResponseLayer, Oneshot}, + util::{BoxCloneService, MapErrLayer, MapResponseLayer, Oneshot}, ServiceExt, }; use tower_layer::Layer; @@ -73,7 +73,6 @@ impl Route { NewError: 'static, { let layer = ( - MapRequestLayer::new(|req: Request<_>| req.map(Body::new)), MapErrLayer::new(Into::into), MapResponseLayer::new(IntoResponse::into_response), layer, @@ -113,7 +112,7 @@ where #[inline] fn call(&mut self, req: Request) -> Self::Future { let req = req.map(Body::new); - RouteFuture::from_future(self.oneshot_inner(req)) + RouteFuture::from_future(self.oneshot_inner(req)).not_top_level() } } @@ -124,6 +123,7 @@ pin_project! { kind: RouteFutureKind, strip_body: bool, allow_header: Option, + top_level: bool, } } @@ -151,6 +151,7 @@ impl RouteFuture { kind: RouteFutureKind::Future { future }, strip_body: false, allow_header: None, + top_level: true, } } @@ -163,6 +164,11 @@ impl RouteFuture { self.allow_header = Some(allow_header); self } + + pub(crate) fn not_top_level(mut self) -> Self { + self.top_level = false; + self + } } impl Future for RouteFuture { @@ -183,16 +189,16 @@ impl Future for RouteFuture { } }; - set_allow_header(res.headers_mut(), this.allow_header); + if *this.top_level { + set_allow_header(res.headers_mut(), this.allow_header); - // make sure to set content-length before removing the body - set_content_length(res.size_hint(), res.headers_mut()); + // make sure to set content-length before removing the body + set_content_length(res.size_hint(), res.headers_mut()); - let res = if *this.strip_body { - res.map(|_| Body::empty()) - } else { - res - }; + if *this.strip_body { + *res.body_mut() = Body::empty(); + } + } Poll::Ready(Ok(res)) } diff --git a/axum/src/routing/tests/mod.rs b/axum/src/routing/tests/mod.rs index c7ae1f70..7fbffbb8 100644 --- a/axum/src/routing/tests/mod.rs +++ b/axum/src/routing/tests/mod.rs @@ -1087,3 +1087,22 @@ async fn locks_mutex_very_little() { assert_eq!(num, 1); } } + +#[crate::test] +async fn middleware_adding_body() { + let app = Router::new() + .route("/", get(())) + .layer(MapResponseLayer::new(|mut res: Response| -> Response { + // If there is a content-length header, its value will be zero and axum will avoid + // overwriting it. But this means our content-length doesn't match the length of the + // body, which leads to panics in Hyper. Thus we have to ensure that axum doesn't add + // on content-length headers until after middleware has been run. + assert!(!res.headers().contains_key("content-length")); + *res.body_mut() = "…".into(); + res + })); + + let client = TestClient::new(app); + let res = client.get("/").await; + assert_eq!(res.text().await, "…"); +} From a8ce6fa0302385c366dbb44cc6a3d64560475124 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sat, 16 Nov 2024 11:14:51 +0100 Subject: [PATCH 3/4] axum-extra: Add links to features table (#3030) --- axum-extra/src/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/axum-extra/src/lib.rs b/axum-extra/src/lib.rs index 9aa3ddf7..f4e2e76e 100644 --- a/axum-extra/src/lib.rs +++ b/axum-extra/src/lib.rs @@ -9,21 +9,21 @@ //! //! Name | Description | Default? //! ---|---|--- -//! `async-read-body` | Enables the `AsyncReadBody` body | No -//! `cookie` | Enables the `CookieJar` extractor | No -//! `cookie-private` | Enables the `PrivateCookieJar` extractor | No -//! `cookie-signed` | Enables the `SignedCookieJar` extractor | No -//! `cookie-key-expansion` | Enables the `Key::derive_from` method | No -//! `erased-json` | Enables the `ErasedJson` response | No -//! `form` | Enables the `Form` extractor | No -//! `json-deserializer` | Enables the `JsonDeserializer` extractor | No -//! `json-lines` | Enables the `JsonLines` extractor and response | No -//! `multipart` | Enables the `Multipart` extractor | No -//! `protobuf` | Enables the `Protobuf` extractor and response | No -//! `query` | Enables the `Query` extractor | No +//! `async-read-body` | Enables the [`AsyncReadBody`](crate::body::AsyncReadBody) body | No +//! `cookie` | Enables the [`CookieJar`](crate::extract::CookieJar) extractor | No +//! `cookie-private` | Enables the [`PrivateCookieJar`](crate::extract::PrivateCookieJar) extractor | No +//! `cookie-signed` | Enables the [`SignedCookieJar`](crate::extract::SignedCookieJar) extractor | No +//! `cookie-key-expansion` | Enables the [`Key::derive_from`](crate::extract::cookie::Key::derive_from) method | No +//! `erased-json` | Enables the [`ErasedJson`](crate::response::ErasedJson) response | No +//! `form` | Enables the [`Form`](crate::extract::Form) extractor | No +//! `json-deserializer` | Enables the [`JsonDeserializer`](crate::extract::JsonDeserializer) extractor | No +//! `json-lines` | Enables the [`JsonLines`](crate::extract::JsonLines) extractor and response | No +//! `multipart` | Enables the [`Multipart`](crate::extract::Multipart) extractor | No +//! `protobuf` | Enables the [`Protobuf`](crate::protobuf::Protobuf) extractor and response | No +//! `query` | Enables the [`Query`](crate::extract::Query) extractor | No //! `tracing` | Log rejections from built-in extractors | Yes -//! `typed-routing` | Enables the `TypedPath` routing utilities | No -//! `typed-header` | Enables the `TypedHeader` extractor and response | No +//! `typed-routing` | Enables the [`TypedPath`](crate::routing::TypedPath) routing utilities | No +//! `typed-header` | Enables the [`TypedHeader`] extractor and response | No //! //! [`axum`]: https://crates.io/crates/axum From 9983bc1da460becd3a0f08c513d610411e84dd43 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 16 Nov 2024 17:10:03 +0100 Subject: [PATCH 4/4] Bump versions --- axum-extra/CHANGELOG.md | 6 ++++++ axum-extra/Cargo.toml | 2 +- axum/CHANGELOG.md | 6 ++++++ axum/Cargo.toml | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index 34abb208..6ea82f31 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog], and this project adheres to [Semantic Versioning]. +# 0.9.6 + +- **docs:** Add links to features table ([#3030]) + +[#3030]: https://github.com/tokio-rs/axum/pull/3030 + # 0.9.5 - **added:** Add `RouterExt::typed_connect` ([#2961]) diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index 6fd81f92..21d34517 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" name = "axum-extra" readme = "README.md" repository = "https://github.com/tokio-rs/axum" -version = "0.9.5" +version = "0.9.6" [features] default = ["tracing", "multipart"] diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 6eb80d6d..ce0ec3da 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# 0.7.9 + +- **fixed:** Avoid setting content-length before middleware ([#3031]) + +[#3031]:https://github.com/tokio-rs/axum/pull/3031 + # 0.7.8 - **fixed:** Skip SSE incompatible chars of `serde_json::RawValue` in `Event::json_data` ([#2992]) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 4b94caef..bc8e109f 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum" -version = "0.7.8" +version = "0.7.9" categories = ["asynchronous", "network-programming", "web-programming::http-server"] description = "Web framework that focuses on ergonomics and modularity" edition = "2021"