Skip to content

Commit

Permalink
Move the Host extractor to axum-extra (tokio-rs#2956)
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte authored Oct 14, 2024
1 parent 24d24f4 commit 0ddc63f
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 29 deletions.
9 changes: 4 additions & 5 deletions axum/src/extract/host.rs → axum-extra/src/extract/host.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion axum-extra/src/extract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Additional extractors.
mod cached;
mod host;
mod optional_path;
pub mod rejection;
mod with_rejection;

#[cfg(feature = "form")]
Expand All @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions axum-extra/src/extract/rejection.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
2 changes: 2 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions axum/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
18 changes: 0 additions & 18 deletions axum/src/extract/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions examples/tls-graceful-shutdown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/tls-graceful-shutdown/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions examples/tls-rustls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion examples/tls-rustls/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 0ddc63f

Please sign in to comment.