fix non-port ipv6

This commit is contained in:
Robin Appelman 2026-03-06 13:20:56 +01:00
commit 20d09d186d
3 changed files with 23 additions and 3 deletions

View file

@ -348,7 +348,15 @@ fn split_host(host: &str) -> (&str, Option<u16>, Option<&str>) {
if host.starts_with('/') {
return ("localhost", None, Some(host));
}
let (host, port_or_socket) = host.rsplit_once(':').unwrap_or((host, ""));
let (host, port_or_socket) = if host.starts_with('[') {
if let Some(pos) = host.rfind("]:") {
(&host[0..pos + 1], &host[pos + 2..])
} else {
(host, "")
}
} else {
host.rsplit_once(':').unwrap_or((host, ""))
};
if port_or_socket.is_empty() {
return (host, None, None);
}
@ -357,3 +365,15 @@ fn split_host(host: &str) -> (&str, Option<u16>, Option<&str>) {
Err(_) => (host, None, Some(port_or_socket)),
}
}
#[test]
fn test_spit_host() {
assert_eq!(("localhost", None, None), split_host("localhost"));
assert_eq!(("localhost", Some(123), None), split_host("localhost:123"));
assert_eq!(
("localhost", None, Some("foo")),
split_host("localhost:foo")
);
assert_eq!(("[::1]", None, None), split_host("[::1]"));
assert_eq!(("[::1]", Some(123), None), split_host("[::1]:123"));
}