fix and use a supposedly much faster library on NormalizeURL(), it's slightly less tolerant with stupidly-formed URLs.
This commit is contained in:
48
normalize.go
48
normalize.go
@@ -4,6 +4,8 @@ import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/ImVexed/fasturl"
|
||||
)
|
||||
|
||||
// NormalizeURL normalizes the url and replaces http://, https:// schemes with ws://, wss://
|
||||
@@ -14,27 +16,49 @@ func NormalizeURL(u string) string {
|
||||
}
|
||||
|
||||
u = strings.TrimSpace(u)
|
||||
if fqn := strings.Split(u, ":")[0]; fqn == "localhost" || fqn == "127.0.0.1" {
|
||||
u = "ws://" + u
|
||||
} else if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
|
||||
u = "wss://" + u
|
||||
}
|
||||
|
||||
p, err := url.Parse(u)
|
||||
p, err := fasturl.ParseURL(u)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if p.Scheme == "http" {
|
||||
p.Scheme = "ws"
|
||||
} else if p.Scheme == "https" {
|
||||
p.Scheme = "wss"
|
||||
// the fabulous case of localhost:1234 that considers "localhost" the protocol and "123" the host
|
||||
if p.Port == "" && len(p.Protocol) > 5 {
|
||||
p.Protocol, p.Host, p.Port = "", p.Protocol, p.Host
|
||||
}
|
||||
|
||||
if p.Protocol == "" {
|
||||
if p.Host == "localhost" || p.Host == "127.0.0.1" {
|
||||
p.Protocol = "ws"
|
||||
} else {
|
||||
p.Protocol = "wss"
|
||||
}
|
||||
} else if p.Protocol == "https" {
|
||||
p.Protocol = "wss"
|
||||
} else if p.Protocol == "http" {
|
||||
p.Protocol = "ws"
|
||||
}
|
||||
|
||||
p.Host = strings.ToLower(p.Host)
|
||||
p.Path = strings.TrimRight(p.Path, "/")
|
||||
|
||||
return p.String()
|
||||
var buf strings.Builder
|
||||
buf.Grow(
|
||||
len(p.Protocol) + 3 + len(p.Host) + 1 + len(p.Port) + len(p.Path) + 1 + len(p.Query),
|
||||
)
|
||||
|
||||
buf.WriteString(p.Protocol)
|
||||
buf.WriteString("://")
|
||||
buf.WriteString(p.Host)
|
||||
if p.Port != "" {
|
||||
buf.WriteByte(':')
|
||||
buf.WriteString(p.Port)
|
||||
}
|
||||
buf.WriteString(p.Path)
|
||||
if p.Query != "" {
|
||||
buf.WriteByte('?')
|
||||
buf.WriteString(p.Query)
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// NormalizeHTTPURL does normalization of http(s):// URLs according to rfc3986. Don't use for relay URLs.
|
||||
|
||||
Reference in New Issue
Block a user