Skip to content

Commit

Permalink
fix: fix get host from request header, #10
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed May 27, 2024
1 parent 45bb853 commit 59ea377
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,15 @@ fn run() -> Result<(), Box<dyn Error>> {
for (name, plugin) in prox_plugins {
info!(
"Proxy plugin {name}, category:{}, step:{}",
plugin.category(),
plugin.step(),
plugin.category()
);
}
for (name, plugin) in response_plugins {
info!(
"Response plugin {name}, category:{}, step:{}",
plugin.category(),
plugin.step(),
plugin.category()
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl ProxyHttp for Server {
.locations
.iter()
.enumerate()
.find(|(_, item)| item.matched(&host, path));
.find(|(_, item)| item.matched(host, path));

if location_result.is_none() {
HttpResponse::unknown_error(Bytes::from(format!(
Expand Down
14 changes: 11 additions & 3 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,19 @@ pub fn local_ip_list() -> Vec<String> {
ip_list
}

pub fn get_host(header: &RequestHeader) -> Option<String> {
/// Get request host in this order of precedence:
/// host name from the request line,
/// or host name from the "Host" request header field
pub fn get_host(header: &RequestHeader) -> Option<&str> {
if let Some(host) = header.uri.host() {
return Some(host);
}
if let Some(host) = header.headers.get("Host") {
return Some(host.to_str().unwrap_or_default().to_string());
if let Ok(value) = host.to_str().map(|host| host.split(':').next()) {
return value;
}
}
header.uri.host().map(|host| host.to_string())
None
}

#[cfg(test)]
Expand Down

0 comments on commit 59ea377

Please sign in to comment.