nip11: url normalization improvement and Fetch test.

This commit is contained in:
fiatjaf
2024-01-13 12:46:59 -03:00
parent fed7e4c3f2
commit 1ff7f826c2
2 changed files with 19 additions and 15 deletions

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
)
@@ -20,21 +19,12 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
}
// normalize URL to start with http:// or https://
if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
u = "wss://" + u
if strings.HasPrefix(u, "ws") {
u = "http" + u[2:]
}
p, err := url.Parse(u)
if err != nil {
return nil, fmt.Errorf("cannot parse url: %s", u)
}
if p.Scheme == "ws" {
p.Scheme = "http"
} else if p.Scheme == "wss" {
p.Scheme = "https"
}
p.Path = strings.TrimRight(p.Path, "/")
u = strings.TrimRight(u, "/")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, p.String(), nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
// add the NIP-11 header
req.Header.Add("Accept", "application/nostr+json")