Files
reverse-proxy/src/utils.rs
glm-5.1 42c721e954 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().
2026-06-12 04:40:43 +00:00

49 lines
1.1 KiB
Rust

pub fn strip_port_from_host(host: &str) -> &str {
if host.starts_with('[') {
if let Some(bracket_end) = host.find(']') {
&host[..bracket_end + 1]
} else {
host
}
} else if let Some(colon_pos) = host.rfind(':') {
&host[..colon_pos]
} else {
host
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn strip_port_plain() {
assert_eq!(strip_port_from_host("example.com"), "example.com");
}
#[test]
fn strip_port_with_port() {
assert_eq!(strip_port_from_host("example.com:8080"), "example.com");
}
#[test]
fn strip_port_ipv6_bare() {
assert_eq!(strip_port_from_host("[::1]"), "[::1]");
}
#[test]
fn strip_port_ipv6_with_port() {
assert_eq!(strip_port_from_host("[::1]:8080"), "[::1]");
}
#[test]
fn strip_port_ipv6_with_long_address() {
assert_eq!(strip_port_from_host("[2001:db8::1]:8080"), "[2001:db8::1]");
}
#[test]
fn strip_port_ipv4_with_port() {
assert_eq!(strip_port_from_host("192.168.1.1:8080"), "192.168.1.1");
}
}