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:
48
src/utils.rs
Normal file
48
src/utils.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user