use crate::config::HttpBlock;
use crate::security::upstream_filter::split_host_port;
pub fn validate_host(host: Option<&str>, block: &HttpBlock) -> bool {
match block.host_header_policy.as_str() {
"any" => true,
"list" => {
let Some(host) = host else { return false };
let (host_only, _) = split_host_port(host);
let host_only = host_only.to_lowercase();
let in_allowed = block
.allowed_hosts
.as_ref()
.map(|hosts| hosts.iter().any(|h| h.eq_ignore_ascii_case(&host_only)))
.unwrap_or(false);
in_allowed
|| block
.server_name
.iter()
.any(|p| matches_name(&host_only, p))
}
_ => {
let Some(host) = host else { return false };
let (host_only, _) = split_host_port(host);
let host_only = host_only.to_lowercase();
block
.server_name
.iter()
.any(|p| matches_name(&host_only, p))
}
}
}
fn matches_name(host: &str, pattern: &str) -> bool {
let pattern = pattern.to_lowercase();
if host == pattern {
return true;
}
if let Some(rest) = pattern.strip_prefix("*.") {
let suffix = &pattern[1..]; return host.ends_with(suffix) || host == rest;
}
false
}