Move the Host extractor to axum-extra (#2956)

This commit is contained in:
Jonas Platte 2024-10-14 22:45:37 +00:00 committed by GitHub
parent 24d24f4ccb
commit 0ddc63f77e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 38 additions and 29 deletions

View file

@ -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 {

View file

@ -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;

View file

@ -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,
}
}

View file

@ -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

View file

@ -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,

View file

@ -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]

View file

@ -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"

View file

@ -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;

View file

@ -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"

View file

@ -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};