From 2a46b5771d15ea55e20a5b52bddb6c04b55326e7 Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Thu, 16 Feb 2023 15:44:38 +0100 Subject: HTTP: Implement join URL path --- internal/http/http.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'internal/http') diff --git a/internal/http/http.go b/internal/http/http.go index 73e7b13..c5939b5 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -59,6 +59,23 @@ func EnsureValidURL(s string) (string, error) { } return cleanPath(u), nil } + +// JoinURLPath joins url's path, in go 1.19 we can use url.JoinPath +func JoinURLPath(u string, p string) (string, error) { + pu, err := url.Parse(u) + if err != nil { + return "", errors.WrapPrefix(err, "failed to parse url for joining paths", 0) + } + pp, err := url.Parse(p) + if err != nil { + return "", errors.WrapPrefix(err, "failed to parse path for joining paths", 0) + } + fp := pu.ResolveReference(pp) + + // We also clean the path for consistency + return cleanPath(fp), nil +} + // ConstructURL creates a URL with the included parameters. func ConstructURL(u *url.URL, params URLParameters) (string, error) { q := u.Query() -- cgit v1.2.3