Wrote url normalizer function.
This commit is contained in:
20
url.go
20
url.go
@@ -2,6 +2,7 @@ package honeybee
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.wisehodl.dev/jay/go-honeybee/errors"
|
||||
)
|
||||
@@ -18,3 +19,22 @@ func ParseURL(urlStr string) (*url.URL, error) {
|
||||
|
||||
return parsedURL, nil
|
||||
}
|
||||
|
||||
func NormalizeURL(input string) (string, error) {
|
||||
parsed, err := ParseURL(strings.ToLower(input))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
host := parsed.Hostname()
|
||||
port := parsed.Port()
|
||||
if (parsed.Scheme == "wss" && port == "443") ||
|
||||
(parsed.Scheme == "ws" && port == "80") {
|
||||
parsed.Host = host
|
||||
}
|
||||
|
||||
parsed.Path = strings.TrimRight(parsed.Path, "/")
|
||||
|
||||
return parsed.String(), nil
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user