From 1e2567c39fb9964a19574eed3465ded05eba5265 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Mon, 20 Feb 2023 22:54:44 +0100 Subject: [PATCH] Fix missing `Allow` when middleware are applied to `MethodRouter` (#1773) --- axum/CHANGELOG.md | 2 +- axum/Cargo.toml | 8 ++++---- axum/src/routing/method_routing.rs | 11 +++++++++++ axum/src/routing/route.rs | 13 ------------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index edb664e0..4fa14a1e 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased -- None. +- **fixed:** Fix `Allow` missing from routers with middleware # 0.6.7 (17. February, 2023) diff --git a/axum/Cargo.toml b/axum/Cargo.toml index 7e4778b0..f63543ce 100644 --- a/axum/Cargo.toml +++ b/axum/Cargo.toml @@ -35,10 +35,10 @@ axum-core = { path = "../axum-core", version = "0.3.2" } bitflags = "1.0" bytes = "1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } -http = "0.2.5" +http = "0.2.9" http-body = "0.4.4" -hyper = { version = "0.14.14", features = ["stream"] } -itoa = "1.0.1" +hyper = { version = "0.14.24", features = ["stream"] } +itoa = "=1.0.5" matchit = "0.7" memchr = "2.4.1" mime = "0.3.16" @@ -72,7 +72,7 @@ axum-macros = { path = "../axum-macros", version = "0.3.4", features = ["__priva futures = "0.3" quickcheck = "1.0" quickcheck_macros = "1.0" -reqwest = { version = "0.11.11", default-features = false, features = ["json", "stream", "multipart"] } +reqwest = { version = "0.11.14", default-features = false, features = ["json", "stream", "multipart"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" time = { version = "0.3", features = ["serde-human-readable"] } diff --git a/axum/src/routing/method_routing.rs b/axum/src/routing/method_routing.rs index dee78c77..fa7d3f12 100644 --- a/axum/src/routing/method_routing.rs +++ b/axum/src/routing/method_routing.rs @@ -1487,6 +1487,17 @@ mod tests { assert_eq!(headers[ALLOW], "GET,POST"); } + #[crate::test] + async fn allow_header_noop_middleware() { + let mut svc = MethodRouter::new() + .get(ok) + .layer(tower::layer::util::Identity::new()); + + let (status, headers, _) = call(Method::DELETE, &mut svc).await; + assert_eq!(status, StatusCode::METHOD_NOT_ALLOWED); + assert_eq!(headers[ALLOW], "GET,HEAD"); + } + #[crate::test] #[should_panic( expected = "Overlapping method route. Cannot add two method routes that both handle `GET`" diff --git a/axum/src/routing/route.rs b/axum/src/routing/route.rs index 89957255..1667db16 100644 --- a/axum/src/routing/route.rs +++ b/axum/src/routing/route.rs @@ -155,9 +155,6 @@ where #[inline] fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - #[derive(Clone, Copy)] - struct AlreadyPassedThroughRouteFuture; - let this = self.project(); let mut res = match this.kind.project() { @@ -171,16 +168,6 @@ where } }; - if res - .extensions() - .get::() - .is_some() - { - return Poll::Ready(Ok(res)); - } else { - res.extensions_mut().insert(AlreadyPassedThroughRouteFuture); - } - set_allow_header(res.headers_mut(), this.allow_header); // make sure to set content-length before removing the body