From 0ddc63f77ecf75e2b602886a6986c1e33d768da4 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 14 Oct 2024 22:45:37 +0000 Subject: [PATCH] Move the Host extractor to axum-extra (#2956) --- {axum => axum-extra}/src/extract/host.rs | 9 ++++----- axum-extra/src/extract/mod.rs | 6 +++++- axum-extra/src/extract/rejection.rs | 23 ++++++++++++++++++++++ axum/CHANGELOG.md | 2 ++ axum/src/extract/mod.rs | 3 --- axum/src/extract/rejection.rs | 18 ----------------- examples/tls-graceful-shutdown/Cargo.toml | 1 + examples/tls-graceful-shutdown/src/main.rs | 2 +- examples/tls-rustls/Cargo.toml | 1 + examples/tls-rustls/src/main.rs | 2 +- 10 files changed, 38 insertions(+), 29 deletions(-) rename {axum => axum-extra}/src/extract/host.rs (96%) create mode 100644 axum-extra/src/extract/rejection.rs diff --git a/axum/src/extract/host.rs b/axum-extra/src/extract/host.rs similarity index 96% rename from axum/src/extract/host.rs rename to axum-extra/src/extract/host.rs index 62f0dc78..477bc4fa 100644 --- a/axum/src/extract/host.rs +++ b/axum-extra/src/extract/host.rs @@ -1,7 +1,5 @@ -use super::{ - rejection::{FailedToResolveHost, HostRejection}, - FromRequestParts, -}; +use super::rejection::{FailedToResolveHost, HostRejection}; +use axum::extract::FromRequestParts; use http::{ header::{HeaderMap, FORWARDED}, request::Parts, @@ -77,7 +75,8 @@ fn parse_forwarded(headers: &HeaderMap) -> Option<&str> { #[cfg(test)] mod tests { use super::*; - use crate::{routing::get, test_helpers::TestClient, Router}; + use crate::test_helpers::TestClient; + use axum::{routing::get, Router}; use http::header::HeaderName; fn test_client() -> TestClient { diff --git a/axum-extra/src/extract/mod.rs b/axum-extra/src/extract/mod.rs index 1f9974de..a0e710d1 100644 --- a/axum-extra/src/extract/mod.rs +++ b/axum-extra/src/extract/mod.rs @@ -1,7 +1,9 @@ //! Additional extractors. mod cached; +mod host; mod optional_path; +pub mod rejection; mod with_rejection; #[cfg(feature = "form")] @@ -19,7 +21,9 @@ mod query; #[cfg(feature = "multipart")] pub mod multipart; -pub use self::{cached::Cached, optional_path::OptionalPath, with_rejection::WithRejection}; +pub use self::{ + cached::Cached, host::Host, optional_path::OptionalPath, with_rejection::WithRejection, +}; #[cfg(feature = "cookie")] pub use self::cookie::CookieJar; diff --git a/axum-extra/src/extract/rejection.rs b/axum-extra/src/extract/rejection.rs new file mode 100644 index 00000000..f4847329 --- /dev/null +++ b/axum-extra/src/extract/rejection.rs @@ -0,0 +1,23 @@ +//! Rejection response types. + +use axum_core::{ + __composite_rejection as composite_rejection, __define_rejection as define_rejection, +}; + +define_rejection! { + #[status = BAD_REQUEST] + #[body = "No host found in request"] + /// Rejection type used if the [`Host`](super::Host) extractor is unable to + /// resolve a host. + pub struct FailedToResolveHost; +} + +composite_rejection! { + /// Rejection used for [`Host`](super::Host). + /// + /// Contains one variant for each way the [`Host`](super::Host) extractor + /// can fail. + pub enum HostRejection { + FailedToResolveHost, + } +} diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 5b058c85..9eec367f 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **breaking:** Move `Host` extractor to `axum-extra` ([#2956]) - **added:** Add `method_not_allowed_fallback` to set a fallback when a path matches but there is no handler for the given HTTP method ([#2903]) - **added:** Add `NoContent` as a self-described shortcut for `StatusCode::NO_CONTENT` ([#2978]) - **added:** Add support for WebSockets over HTTP/2. @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#2897]: https://github.com/tokio-rs/axum/pull/2897 [#2903]: https://github.com/tokio-rs/axum/pull/2903 [#2894]: https://github.com/tokio-rs/axum/pull/2894 +[#2956]: https://github.com/tokio-rs/axum/pull/2956 [#2961]: https://github.com/tokio-rs/axum/pull/2961 [#2974]: https://github.com/tokio-rs/axum/pull/2974 [#2978]: https://github.com/tokio-rs/axum/pull/2978 diff --git a/axum/src/extract/mod.rs b/axum/src/extract/mod.rs index c02bc6f0..6d5e8de8 100644 --- a/axum/src/extract/mod.rs +++ b/axum/src/extract/mod.rs @@ -10,7 +10,6 @@ pub mod rejection; #[cfg(feature = "ws")] pub mod ws; -mod host; pub(crate) mod nested_path; mod raw_form; mod raw_query; @@ -24,9 +23,7 @@ pub use axum_core::extract::{DefaultBodyLimit, FromRef, FromRequest, FromRequest pub use axum_macros::{FromRef, FromRequest, FromRequestParts}; #[doc(inline)] -#[allow(deprecated)] pub use self::{ - host::Host, nested_path::NestedPath, path::{Path, RawPathParams}, raw_form::RawForm, diff --git a/axum/src/extract/rejection.rs b/axum/src/extract/rejection.rs index cba76af0..cd49cc78 100644 --- a/axum/src/extract/rejection.rs +++ b/axum/src/extract/rejection.rs @@ -65,14 +65,6 @@ define_rejection! { pub struct InvalidFormContentType; } -define_rejection! { - #[status = BAD_REQUEST] - #[body = "No host found in request"] - /// Rejection type used if the [`Host`](super::Host) extractor is unable to - /// resolve a host. - pub struct FailedToResolveHost; -} - define_rejection! { #[status = BAD_REQUEST] #[body = "Failed to deserialize form"] @@ -178,16 +170,6 @@ composite_rejection! { } } -composite_rejection! { - /// Rejection used for [`Host`](super::Host). - /// - /// Contains one variant for each way the [`Host`](super::Host) extractor - /// can fail. - pub enum HostRejection { - FailedToResolveHost, - } -} - #[cfg(feature = "matched-path")] define_rejection! { #[status = INTERNAL_SERVER_ERROR] diff --git a/examples/tls-graceful-shutdown/Cargo.toml b/examples/tls-graceful-shutdown/Cargo.toml index 15b3b73b..517e5a38 100644 --- a/examples/tls-graceful-shutdown/Cargo.toml +++ b/examples/tls-graceful-shutdown/Cargo.toml @@ -6,6 +6,7 @@ publish = false [dependencies] axum = { path = "../../axum" } +axum-extra = { path = "../../axum-extra" } axum-server = { version = "0.7", features = ["tls-rustls"] } tokio = { version = "1", features = ["full"] } tracing = "0.1" diff --git a/examples/tls-graceful-shutdown/src/main.rs b/examples/tls-graceful-shutdown/src/main.rs index f42ac435..344ecf9d 100644 --- a/examples/tls-graceful-shutdown/src/main.rs +++ b/examples/tls-graceful-shutdown/src/main.rs @@ -5,13 +5,13 @@ //! ``` use axum::{ - extract::Host, handler::HandlerWithoutStateExt, http::{StatusCode, Uri}, response::Redirect, routing::get, BoxError, Router, }; +use axum_extra::extract::Host; use axum_server::tls_rustls::RustlsConfig; use std::{future::Future, net::SocketAddr, path::PathBuf, time::Duration}; use tokio::signal; diff --git a/examples/tls-rustls/Cargo.toml b/examples/tls-rustls/Cargo.toml index 9b976f16..c5cd65ad 100644 --- a/examples/tls-rustls/Cargo.toml +++ b/examples/tls-rustls/Cargo.toml @@ -6,6 +6,7 @@ publish = false [dependencies] axum = { path = "../../axum" } +axum-extra = { path = "../../axum-extra" } axum-server = { version = "0.7", features = ["tls-rustls"] } tokio = { version = "1", features = ["full"] } tracing = "0.1" diff --git a/examples/tls-rustls/src/main.rs b/examples/tls-rustls/src/main.rs index 88da6e53..bd8b07fc 100644 --- a/examples/tls-rustls/src/main.rs +++ b/examples/tls-rustls/src/main.rs @@ -7,13 +7,13 @@ #![allow(unused_imports)] use axum::{ - extract::Host, handler::HandlerWithoutStateExt, http::{StatusCode, Uri}, response::Redirect, routing::get, BoxError, Router, }; +use axum_extra::extract::Host; use axum_server::tls_rustls::RustlsConfig; use std::{net::SocketAddr, path::PathBuf}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};