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

Implement the OptionalFromRequestParts trait for the Host extractor #3177

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
61 changes: 53 additions & 8 deletions axum-extra/src/extract/host.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use super::rejection::{FailedToResolveHost, HostRejection};
use axum::extract::FromRequestParts;
use axum::{
extract::{FromRequestParts, OptionalFromRequestParts},
RequestPartsExt,
};
use http::{
header::{HeaderMap, FORWARDED},
request::Parts,
uri::Authority,
};
use std::convert::Infallible;

const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host";

Expand All @@ -31,31 +35,50 @@ where
type Rejection = HostRejection;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
parts
.extract::<Option<Host>>()
.await
.ok()
.flatten()
.ok_or(HostRejection::FailedToResolveHost(FailedToResolveHost))
Turbo87 marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<S> OptionalFromRequestParts<S> for Host
where
S: Send + Sync,
{
type Rejection = Infallible;

async fn from_request_parts(
parts: &mut Parts,
_state: &S,
) -> Result<Option<Self>, Self::Rejection> {
if let Some(host) = parse_forwarded(&parts.headers) {
return Ok(Host(host.to_owned()));
return Ok(Some(Host(host.to_owned())));
}

if let Some(host) = parts
.headers
.get(X_FORWARDED_HOST_HEADER_KEY)
.and_then(|host| host.to_str().ok())
{
return Ok(Host(host.to_owned()));
return Ok(Some(Host(host.to_owned())));
}

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

if let Some(authority) = parts.uri.authority() {
return Ok(Host(parse_authority(authority).to_owned()));
return Ok(Some(Host(parse_authority(authority).to_owned())));
}

Err(HostRejection::FailedToResolveHost(FailedToResolveHost))
Ok(None)
}
}

Expand Down Expand Up @@ -148,18 +171,40 @@ mod tests {
async fn ip4_uri_host() {
let mut parts = Request::new(()).into_parts().0;
parts.uri = "https://127.0.0.1:1234/image.jpg".parse().unwrap();
let host = Host::from_request_parts(&mut parts, &()).await.unwrap();
let host = parts.extract::<Host>().await.unwrap();
assert_eq!(host.0, "127.0.0.1:1234");
}

#[crate::test]
async fn ip6_uri_host() {
let mut parts = Request::new(()).into_parts().0;
parts.uri = "http://cool:user@[::1]:456/file.txt".parse().unwrap();
let host = Host::from_request_parts(&mut parts, &()).await.unwrap();
let host = parts.extract::<Host>().await.unwrap();
assert_eq!(host.0, "[::1]:456");
}

#[crate::test]
async fn missing_host() {
let mut parts = Request::new(()).into_parts().0;
let host = parts.extract::<Host>().await.unwrap_err();
assert!(matches!(host, HostRejection::FailedToResolveHost(_)));
}

#[crate::test]
async fn optional_extractor() {
let mut parts = Request::new(()).into_parts().0;
parts.uri = "https://127.0.0.1:1234/image.jpg".parse().unwrap();
let host = parts.extract::<Option<Host>>().await.unwrap();
assert!(host.is_some());
}

#[crate::test]
async fn optional_extractor_none() {
let mut parts = Request::new(()).into_parts().0;
let host = parts.extract::<Option<Host>>().await.unwrap();
assert!(host.is_none());
}

#[test]
fn forwarded_parsing() {
// the basic case
Expand Down