refine url normalization
Before, the entire url was being lowercased, which mutilates various case-sensitive parts. Now, only the host gets lowercased, and the test verify that the userinfo, path, and query parts preserve their original case.
This commit is contained in:
+6
-1
@@ -22,13 +22,18 @@ func ParseURL(urlStr string) (*url.URL, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NormalizeURL(input string) (string, error) {
|
func NormalizeURL(input string) (string, error) {
|
||||||
parsed, err := ParseURL(strings.ToLower(input))
|
parsed, err := ParseURL(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// lowercase host
|
||||||
|
parsed.Host = strings.ToLower(parsed.Host)
|
||||||
|
|
||||||
host := parsed.Hostname()
|
host := parsed.Hostname()
|
||||||
port := parsed.Port()
|
port := parsed.Port()
|
||||||
|
|
||||||
|
// strip default ports
|
||||||
if (parsed.Scheme == "wss" && port == "443") ||
|
if (parsed.Scheme == "wss" && port == "443") ||
|
||||||
(parsed.Scheme == "ws" && port == "80") {
|
(parsed.Scheme == "ws" && port == "80") {
|
||||||
parsed.Host = host
|
parsed.Host = host
|
||||||
|
|||||||
@@ -142,6 +142,22 @@ func TestNormalizeURL(t *testing.T) {
|
|||||||
input: "wss://relay.example.com/nostr",
|
input: "wss://relay.example.com/nostr",
|
||||||
expected: "wss://relay.example.com/nostr",
|
expected: "wss://relay.example.com/nostr",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "lowercase scheme+host, preserve path case",
|
||||||
|
input: "Wss://Relay.Example.Com/Nostr",
|
||||||
|
expected: "wss://relay.example.com/Nostr",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "preserve user info",
|
||||||
|
input: "wss://user:P@ssWord@relay.example.com",
|
||||||
|
// String() cannot output a raw `@` so `%40` is expected
|
||||||
|
expected: "wss://user:P%40ssWord@relay.example.com",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "preserve query",
|
||||||
|
input: "Wss://relay.example.com?field=Value",
|
||||||
|
expected: "wss://relay.example.com?field=Value",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "no change needed",
|
name: "no change needed",
|
||||||
input: "wss://relay.example.com",
|
input: "wss://relay.example.com",
|
||||||
|
|||||||
Reference in New Issue
Block a user