@@ -18,6 +18,18 @@ import (
1818// Examples:
1919// - ResolveURLPath("http://localhost:12434/api/", "/v1/models") -> "http://localhost:12434/api/v1/models"
2020// - ResolveURLPath("http://localhost:12434/api/", "http://other:9000/models") -> "http://other:9000/models"
21+ // ResolveURLPath resolves a path or absolute URL against a base URL.
22+ // This function exists because url.ResolveReference() follows RFC 3986 strictly,
23+ // treating paths with leading "/" as absolute references that replace the entire base path.
24+ // In our context, we want to preserve the base path prefix when joining with relative paths.
25+ //
26+ // We use url.Parse() and path.Join() to achieve the desired behaviour where:
27+ // - Absolute URLs (with scheme) are returned unchanged
28+ // - Relative paths are appended to the base URL's path, preserving the base path prefix
29+ //
30+ // Examples:
31+ // - ResolveURLPath("http://localhost:12434/api/", "/v1/models") -> "http://localhost:12434/api/v1/models"
32+ // - ResolveURLPath("http://localhost:12434/api/", "http://other:9000/models") -> "http://other:9000/models"
2133func ResolveURLPath (baseURL , pathOrURL string ) string {
2234 if baseURL == "" {
2335 return pathOrURL
@@ -38,7 +50,8 @@ func ResolveURLPath(baseURL, pathOrURL string) string {
3850 return pathOrURL
3951 }
4052
41- // Join paths using path.Join which handles slashes correctly
53+ // Use path.Join to preserve the base path prefix when joining with relative paths
54+ // and to normalise redundant slashes
4255 base .Path = path .Join (base .Path , pathOrURL )
4356 return base .String ()
4457}
0 commit comments