Skip to content

Commit

Permalink
HTTP/2 requests contain the scheme in the URI
Browse files Browse the repository at this point in the history
I cannot see a way to test this right now, but cURL shows it works
  • Loading branch information
Benjamin Sparks committed Sep 5, 2024
1 parent 48a718d commit 7957018
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions axum/src/extract/scheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const X_FORWARDED_PROTO_HEADER_KEY: &str = "X-Forwarded-Proto";
/// The scheme is resolved through the following, in order:
/// - `Forwarded` header
/// - `X-Forwarded-Proto` header
/// - request target / URI
/// - 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.
Expand Down Expand Up @@ -43,7 +43,7 @@ where
return Ok(Scheme(scheme.to_owned()));
}

// From parts
// From parts of an HTTP/2 request
if let Some(scheme) = parts.uri.scheme_str() {
return Ok(Scheme(scheme.to_owned()));
}
Expand Down Expand Up @@ -74,9 +74,7 @@ fn parse_forwarded(headers: &HeaderMap) -> Option<&str> {
mod tests {
use super::*;
use crate::{routing::get, test_helpers::TestClient, Router};
use http::{header::HeaderName, Request};

use axum_core::{body::Body, extract::FromRequest};
use http::header::HeaderName;

fn test_client() -> TestClient {
async fn scheme_as_body(Scheme(scheme): Scheme) -> String {
Expand Down Expand Up @@ -121,19 +119,6 @@ mod tests {
assert_eq!(scheme, original_scheme);
}

#[crate::test]
async fn from_parts() {
let original_scheme = "http";
let req = Request::builder()
.uri(format!("{original_scheme}://localhost/"))
.body(Body::empty())
.unwrap();
assert_eq!(
Scheme::from_request(req, &()).await.unwrap().0,
original_scheme
);
}

#[crate::test]
async fn precedence_forwarded_over_x_forwarded() {
let scheme = test_client()
Expand Down

0 comments on commit 7957018

Please sign in to comment.