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

Add Host extractor #827

Merged
merged 7 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions axum/src/extract/host.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
use super::{FromRequest, RequestParts};
use super::{FromRequest, RequestParts, rejection::{HostRejection, FailedToResolveHost}};
use async_trait::async_trait;
use std::{convert::Infallible};

/// Extractor that extracts the host from a request.
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct Host(pub String);

#[async_trait]
impl<B> FromRequest<B> for Host
where
B: Send,
{
type Rejection = Infallible;
type Rejection = HostRejection;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
if let Some(host) = req.uri().host() {
return Ok(Host(host.to_string()));
// todo: extract host from http::header::FORWARDED

if let Some(host) = req.headers().get("X-Forwarded-Host").and_then(|host| host.to_str().ok()) {
return Ok(Host(host.to_owned()));
}

if let Some(Ok(host)) = req.headers().get("host").map(|host| host.to_str()) {
return Ok(Host(host.to_string()));
if let Some(host) = req.headers().get(http::header::HOST).and_then(|host| host.to_str().ok()) {
return Ok(Host(host.to_owned()));
}

if let Some(host) = req.uri().host() {
return Ok(Host(host.to_owned()));
}
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved

Ok(Host("".to_string()))
Err(HostRejection::FailedToResolveHost(FailedToResolveHost))
}
}

Expand Down
4 changes: 2 additions & 2 deletions axum/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod ws;
mod content_length_limit;
mod raw_query;
mod request_parts;
mod host;

#[doc(inline)]
pub use axum_core::extract::{FromRequest, RequestParts};
Expand All @@ -23,13 +24,12 @@ pub use self::{
connect_info::ConnectInfo,
content_length_limit::ContentLengthLimit,
extractor_middleware::extractor_middleware,
host::Host,
path::Path,
raw_query::RawQuery,
request_parts::{BodyStream, RawBody},
};

pub mod host;

#[doc(no_inline)]
#[cfg(feature = "json")]
pub use crate::Json;
Expand Down
18 changes: 18 additions & 0 deletions axum/src/extract/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ define_rejection! {
pub struct InvalidFormContentType;
}

define_rejection! {
#[status = BAD_REQUEST]
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
#[body = "No host found in request"]
/// Rejection type used if the [`Host`](super::Host) extractor is unable to
/// resolve a host from it's supplied `RequestParts`.
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
pub struct FailedToResolveHost;
}

/// Rejection type for extractors that deserialize query strings if the input
/// couldn't be deserialized into the target type.
#[derive(Debug)]
Expand Down Expand Up @@ -160,6 +168,16 @@ 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