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 | |
| parent | f106485d6a757e92949e0c0da6b68385879e4623 (diff) | |
HTTP: Incorporate util ensure valid url with clean path
| -rw-r--r-- | client/client_test.go | 3 | ||||
| -rw-r--r-- | client/server.go | 6 | ||||
| -rw-r--r-- | internal/http/http.go | 35 | ||||
| -rw-r--r-- | internal/util/util.go | 32 | ||||
| -rw-r--r-- | internal/util/util_test.go | 33 |
5 files changed, 39 insertions, 70 deletions
diff --git a/client/client_test.go b/client/client_test.go index 4356736..5a5a39e 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -13,7 +13,6 @@ import ( httpw "github.com/eduvpn/eduvpn-common/internal/http" "github.com/eduvpn/eduvpn-common/internal/oauth" - "github.com/eduvpn/eduvpn-common/internal/util" "github.com/go-errors/errors" ) @@ -22,7 +21,7 @@ func getServerURI(t *testing.T) string { if serverURI == "" { t.Skip("Skipping server test as no SERVER_URI env var has been passed") } - serverURI, parseErr := util.EnsureValidURL(serverURI) + serverURI, parseErr := httpw.EnsureValidURL(serverURI) if parseErr != nil { t.Skip("Skipping server test as the server uri is not valid") } diff --git a/client/server.go b/client/server.go index 4070705..64f1d25 100644 --- a/client/server.go +++ b/client/server.go @@ -2,9 +2,9 @@ package client import ( "github.com/eduvpn/eduvpn-common/internal/failover" + "github.com/eduvpn/eduvpn-common/internal/http" "github.com/eduvpn/eduvpn-common/internal/oauth" "github.com/eduvpn/eduvpn-common/internal/server" - "github.com/eduvpn/eduvpn-common/internal/util" "github.com/eduvpn/eduvpn-common/types" "github.com/go-errors/errors" ) @@ -331,7 +331,7 @@ func (c *Client) AddCustomServer(url string) (srv server.Server, err error) { } }() - if url, err = util.EnsureValidURL(url); err != nil { + if url, err = http.EnsureValidURL(url); err != nil { return nil, err } @@ -457,7 +457,7 @@ func (c *Client) GetConfigCustomServer(url string, preferTCP bool, t oauth.Token } }() - if url, err = util.EnsureValidURL(url); err != nil { + if url, err = http.EnsureValidURL(url); err != nil { return nil, err } 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() diff --git a/internal/util/util.go b/internal/util/util.go index 4370fd1..907f85f 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -6,43 +6,11 @@ import ( "fmt" "net/url" "os" - "path" "strings" "github.com/go-errors/errors" ) -// 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" - } - 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, nil -} - // MakeRandomByteSlice creates a cryptographically random bytes slice of `size` // It returns the byte slice (or nil if error) and an error if it could not be generated. func MakeRandomByteSlice(n int) ([]byte, error) { diff --git a/internal/util/util_test.go b/internal/util/util_test.go index cb82123..c3d1cee 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -5,39 +5,6 @@ import ( "testing" ) -func TestEnsureValidURL(t *testing.T) { - _, validErr := EnsureValidURL("%notvalid%") - - if validErr == nil { - t.Fatal("Got nil error, want: non-nil") - } - - testCases := map[string]string{ - // Make sure we set https - "example.com/": "https://example.com/", - // Make sure we do override the scheme to https - "http://example.com/": "https://example.com/", - // This URL is already valid - "https://example.com/": "https://example.com/", - // Make sure to add a trailing slash (/) - "https://example.com": "https://example.com/", - // Cleanup the path 1 - "https://example.com/////": "https://example.com/", - // Cleanup the path 2 - "https://example.com/..": "https://example.com/", - } - - for k, v := range testCases { - valid, validErr := EnsureValidURL(k) - if validErr != nil { - t.Fatalf("Got: %v, want: nil", validErr) - } - if valid != v { - t.Fatalf("Got: %v, want: %v", valid, v) - } - } -} - func TestMakeRandomByteSlice(t *testing.T) { random, randomErr := MakeRandomByteSlice(32) if randomErr != nil { |
