agentmux_srv_core/connectivity/
endpoint.rs1pub(super) fn normalise(url: &str) -> Option<String> {
12 let mut parsed = reqwest::Url::parse(url.trim()).ok().filter(|u| u.has_host())?;
13 let _ = parsed.set_username("");
14 let _ = parsed.set_password(None);
15 parsed.set_query(None);
16 parsed.set_fragment(None);
17 Some(parsed.as_str().trim_end_matches('/').to_string())
18}
19
20pub(super) fn host_of(url: &str) -> String {
24 let Some(parsed) = reqwest::Url::parse(url).ok() else { return "unknown address".to_string() };
25 let Some(host) = parsed.host_str() else { return "unknown address".to_string() };
26 let scheme = if parsed.scheme() == "https" { String::new() } else { format!("{}://", parsed.scheme()) };
27 let port = parsed.port().map(|p| format!(":{p}")).unwrap_or_default();
28 let path = parsed.path().trim_end_matches('/');
29 format!("{scheme}{host}{port}{path}")
30}
31
32#[cfg(test)]
33mod tests {
34 use super::*;
35
36 fn n(url: &str) -> Option<String> {
37 normalise(url)
38 }
39
40 #[test]
41 fn the_same_address_written_two_ways_is_one() {
42 assert_eq!(n("https://api.anthropic.com/"), n(" https://API.anthropic.com "));
43 assert_eq!(n("https://api.anthropic.com").as_deref(), Some("https://api.anthropic.com"));
44 }
45
46 #[test]
47 fn a_credential_a_query_and_a_fragment_are_dropped() {
48 assert_eq!(n("https://user:secret@proxy.example.com:8443/v1/?key=abc#top").as_deref(), Some("https://proxy.example.com:8443/v1"));
49 }
50
51 #[test]
52 fn a_path_is_kept() {
53 assert_eq!(n("https://gateway.example.com/anthropic/").as_deref(), Some("https://gateway.example.com/anthropic"));
54 }
55
56 #[test]
57 fn an_address_without_a_host_is_none_so_nothing_in_it_is_ever_shown() {
58 for raw in ["user:secret@proxy.example.com:8080", "gw.example.com/v1?key=abc", " not a url/ ", "mailto:a@b.c", ""] {
59 assert_eq!(n(raw), None, "{raw}");
60 }
61 }
62
63 #[test]
64 fn the_host_has_its_port_only_when_it_is_not_the_scheme_default() {
65 assert_eq!(host_of("https://proxy.example.com"), "proxy.example.com");
66 assert_eq!(host_of("https://proxy.example.com:443"), "proxy.example.com");
67 assert_eq!(host_of("http://localhost:4000"), "http://localhost:4000", "a scheme other than https is part of the identity");
68 assert_eq!(host_of("user:secret@proxy"), "unknown address", "never echoes what it can't read");
69 }
70
71 #[test]
72 fn two_routes_of_one_gateway_are_two_identities() {
73 let a = host_of("https://gateway.example/team-a");
74 let b = host_of("https://gateway.example/team-b");
75 assert_eq!((a.as_str(), b.as_str()), ("gateway.example/team-a", "gateway.example/team-b"));
76 assert_ne!(host_of("http://gateway.example"), host_of("https://gateway.example"));
77 }
78}