Skip to main content

agentmux_srv_core/connectivity/
endpoint.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A model API address as the status names it: the same address written two ways is one service,
5//! and a service's id and label never carry a credential or a query.
6
7/// An address reduced to what identifies it: scheme, host, port and path, with no user, password,
8/// query, fragment or trailing slash. `None` for one that doesn't parse as an address with a host
9/// (`user:secret@proxy.example.com` is read as a scheme with no host): it is never shown or kept,
10/// since there is no telling what in it is a credential, and the caller treats it as no address.
11pub(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
20/// What tells a [`normalise`]d address apart in an id and a label: `host`, `host:port` when the port is
21/// not the scheme's default, the path when there is one (two routes of one gateway are two services),
22/// and the scheme when it is not `https`.
23pub(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}