diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-02-16 15:44:08 +0100 |
|---|---|---|
| committer | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2023-02-16 15:44:08 +0100 |
| commit | 75ac9ee246e7d3be5890b972a241855da875f4b0 (patch) | |
| tree | 59b480e4f20ec81809ec9f69cfbd7eb9f38f6ac5 /internal/http | |
| parent | f106485d6a757e92949e0c0da6b68385879e4623 (diff) | |
HTTP: Incorporate util ensure valid url with clean path
Diffstat (limited to 'internal/http')
| -rw-r--r-- | internal/http/http.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/http/http.go b/internal/http/http.go index 58d69bf..73e7b13 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "path" "strings" "time" @@ -24,6 +25,40 @@ type OptionalParams struct { Timeout time.Duration } +func cleanPath(u *url.URL) string { + if u.Path != "" { + // Clean the path + // https://pkg.go.dev/path#Clean + u.Path = path.Clean(u.Path) + } + + str := u.String() + + // Make sure the URL ends with a / + if str[len(str)-1:] != "/" { + str += "/" + } + return str +} + +// EnsureValidURL ensures that the input URL is valid to be used internally +// It does the following +// - Sets the scheme to https if none is given +// - It 'cleans' up the path using path.Clean +// - It makes sure that the URL ends with a / +// It returns an error if the URL cannot be parsed. +func EnsureValidURL(s string) (string, error) { + u, err := url.Parse(s) + if err != nil { + return "", errors.WrapPrefix(err, "failed parsing url", 0) + } + + // Make sure the scheme is always https + if u.Scheme != "https" { + u.Scheme = "https" + } + return cleanPath(u), nil +} // ConstructURL creates a URL with the included parameters. func ConstructURL(u *url.URL, params URLParameters) (string, error) { q := u.Query() |
