Skip to content

Commit

Permalink
Implement Scheme extractor within axum-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Sparks committed Sep 9, 2024
1 parent cb29660 commit 887b53f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions axum-extra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ json-lines = [
]
multipart = ["dep:multer"]
protobuf = ["dep:prost"]
scheme = []
query = ["dep:serde_html_form"]
tracing = ["dep:tracing", "axum-core/tracing", "axum/tracing"]
typed-header = ["dep:headers"]
Expand Down
7 changes: 7 additions & 0 deletions axum-extra/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ pub use crate::json_lines::JsonLines;
#[cfg(feature = "typed-header")]
#[doc(no_inline)]
pub use crate::typed_header::TypedHeader;

#[cfg(feature = "scheme")]
pub mod scheme;

#[cfg(feature = "scheme")]
#[doc(no_inline)]
pub use self::scheme::{SchemeMissing, Scheme};
36 changes: 24 additions & 12 deletions axum/src/extract/scheme.rs → axum-extra/src/extract/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
use super::{
rejection::{FailedToResolveScheme, SchemeRejection},
FromRequestParts,
//! Extractor that parses the scheme of a request.
//! See [`Scheme`] for more details.

use axum::{
extract::FromRequestParts,
response::{IntoResponse, Response},
};
use async_trait::async_trait;
use http::{
header::{HeaderMap, FORWARDED},
request::Parts,
};
const X_FORWARDED_PROTO_HEADER_KEY: &str = "X-Forwarded-Proto";

/// Extractor that resolves the scheme / protocol of the request.
/// Extractor that resolves the scheme / protocol of a request.
///
/// The scheme is resolved through the following, in order:
/// - `Forwarded` header
/// - `X-Forwarded-Proto` header
/// - request URI (If the request is an HTTP/2 request! e.g. use `--http2(-prior-knowledge)` with cURL)
/// - Request URI (If the request is an HTTP/2 request! e.g. use `--http2(-prior-knowledge)` with cURL)
///
/// Note that user agents can set the `X-Forwarded-Proto` header to arbitrary values so make
/// sure to validate them to avoid security issues.
#[derive(Debug, Clone)]
pub struct Scheme(pub String);

#[async_trait]
/// Rejection type used if the [`Scheme`] extractor is unable to
/// resolve a scheme.
#[derive(Debug)]
pub struct SchemeMissing;

impl IntoResponse for SchemeMissing {
fn into_response(self) -> Response {
(http::StatusCode::BAD_REQUEST, "No scheme found in request").into_response()
}
}

#[axum::async_trait]
impl<S> FromRequestParts<S> for Scheme
where
S: Send + Sync,
{
type Rejection = SchemeRejection;
type Rejection = SchemeMissing;

async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
// Within Forwarded header
Expand All @@ -48,9 +61,7 @@ where
return Ok(Scheme(scheme.to_owned()));
}

Err(SchemeRejection::FailedToResolveScheme(
FailedToResolveScheme,
))
Err(SchemeMissing)
}
}

Expand All @@ -73,7 +84,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
2 changes: 0 additions & 2 deletions axum/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub(crate) mod nested_path;
mod raw_form;
mod raw_query;
mod request_parts;
mod scheme;
mod state;

#[doc(inline)]
Expand All @@ -32,7 +31,6 @@ pub use self::{
path::{Path, RawPathParams},
raw_form::RawForm,
raw_query::RawQuery,
scheme::Scheme,
state::State,
};

Expand Down

0 comments on commit 887b53f

Please sign in to comment.