Skip to content

Commit a6f8af2

Browse files
committed
feedback
1 parent 499a154 commit a6f8af2

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

internal/util/url.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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"
2133
func 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
}

internal/util/url_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ func TestResolveURLPath(t *testing.T) {
111111
pathOrURL: "/models",
112112
expected: "http://localhost:8080/api/models#section",
113113
},
114+
{
115+
name: "invalid base URL returns path",
116+
baseURL: "://invalid-url",
117+
pathOrURL: "/v1/models",
118+
expected: "/v1/models",
119+
},
114120
}
115121

116122
for _, tc := range tests {

0 commit comments

Comments
 (0)