use NormalizeURL() on nip11.Fetch()

This commit is contained in:
fiatjaf 2024-03-10 18:44:56 -03:00
parent ec1e86e505
commit c844dc2ccb
2 changed files with 8 additions and 8 deletions

View File

@ -5,8 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/nbd-wtf/go-nostr"
)
// Fetch fetches the NIP-11 RelayInformationDocument.
@ -19,14 +20,9 @@ func Fetch(ctx context.Context, u string) (info *RelayInformationDocument, err e
}
// normalize URL to start with http://, https:// or without protocol
if strings.HasPrefix(u, "wss://") || strings.HasPrefix(u, "ws://") {
u = "http" + u[2:]
}
if !(strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://")) {
u = "http://" + u
}
u = strings.TrimRight(u, "/")
u = "http" + nostr.NormalizeURL(u)[2:]
// make request
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
// add the NIP-11 header

View File

@ -41,4 +41,8 @@ func TestFetch(t *testing.T) {
if err != nil || res.Name == "" {
t.Errorf("failed to fetch from https")
}
res, err = Fetch(context.Background(), "relay.nostr.bg")
if err != nil || res.Name == "" {
t.Errorf("failed to fetch without protocol")
}
}