-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_url_utils.go
More file actions
43 lines (36 loc) · 1.09 KB
/
client_url_utils.go
File metadata and controls
43 lines (36 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package restclient
import (
"net/url"
)
// joinURLPaths joins base and request paths, handling query strings and fragments
// using the standard library's url.JoinPath for proper path joining
func joinURLPaths(base *url.URL, requestURL *url.URL) *url.URL {
// Join the paths using the standard url.JoinPath
targetPath, err := url.JoinPath(base.Path, requestURL.Path)
if err != nil {
// Path join failed, return nil
return nil
}
targetQuery := requestURL.RawQuery
targetFragment := requestURL.Fragment
// Create a URL struct from base parts and new path/query/fragment
tempURL := url.URL{
Scheme: base.Scheme,
Opaque: base.Opaque,
User: base.User,
Host: base.Host,
Path: targetPath,
RawQuery: targetQuery,
Fragment: targetFragment,
}
// Get the string representation of this assembled URL
finalURLStr := tempURL.String()
// URL constructed successfully
// Parse this string to get a fully validated *url.URL object
finalResolvedURL, err := url.Parse(finalURLStr)
if err != nil {
// Failed to parse constructed URL
return nil
}
return finalResolvedURL
}