mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-11-19 18:47:37 +01:00
nip61 and nip60 improvements and fixes.
This commit is contained in:
44
normalize.go
44
normalize.go
@@ -1,6 +1,7 @@
|
||||
package nostr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
@@ -36,6 +37,49 @@ func NormalizeURL(u string) string {
|
||||
return p.String()
|
||||
}
|
||||
|
||||
// NormalizeHTTPURL does normalization of http(s):// URLs according to rfc3986. Don't use for relay URLs.
|
||||
func NormalizeHTTPURL(s string) (string, error) {
|
||||
s = strings.TrimSpace(s)
|
||||
|
||||
if !strings.HasPrefix(s, "http") {
|
||||
s = "https://" + s
|
||||
}
|
||||
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
|
||||
if u.Scheme == "" {
|
||||
u, err = url.Parse("http://" + s)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
}
|
||||
|
||||
if strings.HasPrefix(s, "//") {
|
||||
s = "http:" + s
|
||||
}
|
||||
|
||||
var p int
|
||||
switch u.Scheme {
|
||||
case "http":
|
||||
p = 80
|
||||
case "https":
|
||||
p = 443
|
||||
}
|
||||
u.Host = strings.TrimSuffix(u.Host, fmt.Sprintf(":%d", p))
|
||||
|
||||
v := u.Query()
|
||||
u.RawQuery = v.Encode()
|
||||
u.RawQuery, _ = url.QueryUnescape(u.RawQuery)
|
||||
|
||||
h := u.String()
|
||||
h = strings.TrimSuffix(h, "/")
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// NormalizeOKMessage takes a string message that is to be sent in an `OK` or `CLOSED` command
|
||||
// and prefixes it with "<prefix>: " if it doesn't already have an acceptable prefix.
|
||||
func NormalizeOKMessage(reason string, prefix string) string {
|
||||
|
||||
Reference in New Issue
Block a user