fix: normalize_host handles IPv6 bracket notation

Extract strip_port_from_host into shared utils module and update normalize_host to properly strip brackets from IPv6 addresses like [::1]:443 -> ::1 instead of incorrectly using split(':').next().
This commit is contained in:
2026-06-12 04:40:43 +00:00
parent d7f811ffb5
commit 42c721e954
4 changed files with 78 additions and 40 deletions

View File

@@ -50,8 +50,14 @@ pub fn build_routing_table(sites: &[SiteConfig]) -> HashMap<String, SiteConfig>
}
pub fn normalize_host(host: &str) -> String {
let lower = host.to_lowercase();
lower.split(':').next().unwrap_or(&lower).to_string()
let stripped = crate::utils::strip_port_from_host(host);
let lower = stripped.to_lowercase();
lower
.strip_prefix('[')
.unwrap_or(&lower)
.strip_suffix(']')
.unwrap_or(&lower)
.to_string()
}
#[derive(Debug, Deserialize, Clone, PartialEq)]
@@ -304,6 +310,26 @@ mod tests {
assert_eq!(normalize_host(""), "");
}
#[test]
fn normalize_host_ipv6_with_port() {
assert_eq!(normalize_host("[::1]:443"), "::1");
}
#[test]
fn normalize_host_ipv6_long_with_port() {
assert_eq!(normalize_host("[2001:db8::1]:8080"), "2001:db8::1");
}
#[test]
fn normalize_host_ipv6_bare() {
assert_eq!(normalize_host("[::1]"), "::1");
}
#[test]
fn normalize_host_ipv6_uppercase() {
assert_eq!(normalize_host("[2001:DB8::1]:443"), "2001:db8::1");
}
#[test]
fn routing_table_lookup_finds_site() {
let config = test_fixtures::test_dynamic_config();