2022-01-02 08:44:18 -03:00
|
|
|
package nostr
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
import (
|
2025-02-04 13:43:18 -03:00
|
|
|
"fmt"
|
2021-01-31 11:05:09 -03:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-11-28 22:23:51 -03:00
|
|
|
// NormalizeURL normalizes the url and replaces http://, https:// schemes with ws://, wss://
|
|
|
|
// and normalizes the path.
|
2021-01-31 11:05:09 -03:00
|
|
|
func NormalizeURL(u string) string {
|
2022-11-19 07:19:10 -03:00
|
|
|
if u == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-03-14 21:54:25 -03:00
|
|
|
u = strings.TrimSpace(u)
|
2024-08-19 11:53:18 -03:00
|
|
|
if fqn := strings.Split(u, ":")[0]; fqn == "localhost" || fqn == "127.0.0.1" {
|
2024-07-04 12:06:24 -03:00
|
|
|
u = "ws://" + u
|
2024-07-04 16:29:04 -03:00
|
|
|
} else if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
|
2021-02-20 17:44:05 -03:00
|
|
|
u = "wss://" + u
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
2024-07-04 16:29:04 -03:00
|
|
|
|
2021-01-31 11:05:09 -03:00
|
|
|
p, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-02-20 17:44:05 -03:00
|
|
|
if p.Scheme == "http" {
|
|
|
|
p.Scheme = "ws"
|
|
|
|
} else if p.Scheme == "https" {
|
|
|
|
p.Scheme = "wss"
|
2021-01-31 11:05:09 -03:00
|
|
|
}
|
|
|
|
|
2025-01-16 20:08:49 -03:00
|
|
|
p.Host = strings.ToLower(p.Host)
|
2022-11-18 14:15:41 -03:00
|
|
|
p.Path = strings.TrimRight(p.Path, "/")
|
2021-01-31 11:05:09 -03:00
|
|
|
|
|
|
|
return p.String()
|
|
|
|
}
|
2023-11-28 22:23:51 -03:00
|
|
|
|
2025-02-04 13:43:18 -03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2023-11-28 22:23:51 -03:00
|
|
|
// 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 {
|
|
|
|
if idx := strings.Index(reason, ": "); idx == -1 || strings.IndexByte(reason[0:idx], ' ') != -1 {
|
|
|
|
return prefix + ": " + reason
|
|
|
|
}
|
|
|
|
return reason
|
|
|
|
}
|