Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the Host extractor to axum-extra #2956

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -10,11 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **breaking:** Require `Sync` for all handlers and services added to `Router`
and `MethodRouter` ([#2473])
- **breaking:** The tuple and tuple_struct `Path` extractor deserializers now check that the number of parameters matches the tuple length exactly ([#2931])
- **breaking:** Move `Host` extractor to `axum-extra` ([#2956])
- **change:** Update minimum rust version to 1.75 ([#2943])

[#2473]: https://github.com/tokio-rs/axum/pull/2473
[#2931]: https://github.com/tokio-rs/axum/pull/2931
[#2943]: https://github.com/tokio-rs/axum/pull/2943
[#2956]: https://github.com/tokio-rs/axum/pull/2956

# 0.7.7

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
Loading