Refactored package structure.
This commit is contained in:
38
transport/url.go
Normal file
38
transport/url.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ParseURL(urlStr string) (*url.URL, error) {
|
||||
parsedURL, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if parsedURL.Scheme != "ws" && parsedURL.Scheme != "wss" {
|
||||
return nil, InvalidProtocol
|
||||
}
|
||||
|
||||
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