-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor host extraction to common logic + fix authority rama-fp
- Loading branch information
glendc
committed
Apr 19, 2024
1 parent
61c474e
commit 30c869d
Showing
6 changed files
with
112 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Http header Extract utilities. | ||
use crate::http::{header::FORWARDED, HeaderMap}; | ||
|
||
const X_FORWARDED_HOST_HEADER_KEY: &str = "X-Forwarded-Host"; | ||
|
||
/// Extract the host from the headers ([`HeaderMap`]). | ||
pub fn extract_host_from_headers(headers: &HeaderMap) -> Option<String> { | ||
if let Some(host) = parse_forwarded(headers) { | ||
return Some(host.to_owned()); | ||
} | ||
|
||
if let Some(host) = headers | ||
.get(X_FORWARDED_HOST_HEADER_KEY) | ||
.and_then(|host| host.to_str().ok()) | ||
{ | ||
return Some(host.to_owned()); | ||
} | ||
|
||
if let Some(host) = headers | ||
.get(http::header::HOST) | ||
.and_then(|host| host.to_str().ok()) | ||
{ | ||
return Some(host.to_owned()); | ||
} | ||
|
||
None | ||
} | ||
|
||
fn parse_forwarded(headers: &HeaderMap) -> Option<&str> { | ||
// if there are multiple `Forwarded` `HeaderMap::get` will return the first one | ||
let forwarded_values = headers.get(FORWARDED)?.to_str().ok()?; | ||
|
||
// get the first set of values | ||
let first_value = forwarded_values.split(',').next()?; | ||
|
||
// find the value of the `host` field | ||
first_value.split(';').find_map(|pair| { | ||
let (key, value) = pair.split_once('=')?; | ||
key.trim() | ||
.eq_ignore_ascii_case("host") | ||
.then(|| value.trim().trim_matches('"')) | ||
}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::http::HeaderName; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn forwarded_parsing() { | ||
// the basic case | ||
let headers = header_map(&[(FORWARDED, "host=192.0.2.60;proto=http;by=203.0.113.43")]); | ||
let value = parse_forwarded(&headers).unwrap(); | ||
assert_eq!(value, "192.0.2.60"); | ||
|
||
// is case insensitive | ||
let headers = header_map(&[(FORWARDED, "host=192.0.2.60;proto=http;by=203.0.113.43")]); | ||
let value = parse_forwarded(&headers).unwrap(); | ||
assert_eq!(value, "192.0.2.60"); | ||
|
||
// ipv6 | ||
let headers = header_map(&[(FORWARDED, "host=\"[2001:db8:cafe::17]:4711\"")]); | ||
let value = parse_forwarded(&headers).unwrap(); | ||
assert_eq!(value, "[2001:db8:cafe::17]:4711"); | ||
|
||
// multiple values in one header | ||
let headers = header_map(&[(FORWARDED, "host=192.0.2.60, host=127.0.0.1")]); | ||
let value = parse_forwarded(&headers).unwrap(); | ||
assert_eq!(value, "192.0.2.60"); | ||
|
||
// multiple header values | ||
let headers = header_map(&[ | ||
(FORWARDED, "host=192.0.2.60"), | ||
(FORWARDED, "host=127.0.0.1"), | ||
]); | ||
let value = parse_forwarded(&headers).unwrap(); | ||
assert_eq!(value, "192.0.2.60"); | ||
} | ||
|
||
fn header_map(values: &[(HeaderName, &str)]) -> HeaderMap { | ||
let mut headers = HeaderMap::new(); | ||
for (key, value) in values { | ||
headers.append(key, value.parse().unwrap()); | ||
} | ||
headers | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters