diff --git a/Cargo.lock b/Cargo.lock index 5e5a3c6..b32115c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -797,7 +797,7 @@ dependencies = [ [[package]] name = "nextcloud-config-parser" -version = "0.15.1" +version = "0.15.2" dependencies = [ "form_urlencoded", "indexmap", diff --git a/Cargo.toml b/Cargo.toml index 491d062..183f325 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "nextcloud-config-parser" description = "Rust parser for nextcloud config files" -version = "0.15.1" +version = "0.15.2" authors = ["Robin Appelman "] edition = "2021" license = "MIT OR Apache-2.0" diff --git a/src/lib.rs b/src/lib.rs index ae6c966..14743f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -348,7 +348,15 @@ fn split_host(host: &str) -> (&str, Option, 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, 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")); +}