normalize.go raw

   1  package nostr
   2  
   3  import (
   4  	"fmt"
   5  	"net/url"
   6  	"strings"
   7  
   8  	"github.com/ImVexed/fasturl"
   9  )
  10  
  11  // NormalizeURL normalizes the url and replaces http://, https:// schemes with ws://, wss://
  12  // and normalizes the path.
  13  func NormalizeURL(u string) string {
  14  	if u == "" {
  15  		return ""
  16  	}
  17  
  18  	u = strings.TrimSpace(u)
  19  	p, err := fasturl.ParseURL(u)
  20  	if err != nil {
  21  		return ""
  22  	}
  23  
  24  	// the fabulous case of localhost:1234 that considers "localhost" the protocol and "123" the host
  25  	if p.Port == "" && len(p.Protocol) > 5 {
  26  		p.Protocol, p.Host, p.Port = "", p.Protocol, p.Host
  27  	}
  28  
  29  	if p.Protocol == "" {
  30  		if p.Host == "localhost" || p.Host == "127.0.0.1" {
  31  			p.Protocol = "ws"
  32  		} else {
  33  			p.Protocol = "wss"
  34  		}
  35  	} else if p.Protocol == "https" {
  36  		p.Protocol = "wss"
  37  	} else if p.Protocol == "http" {
  38  		p.Protocol = "ws"
  39  	}
  40  
  41  	p.Host = strings.ToLower(p.Host)
  42  	p.Path = strings.TrimRight(p.Path, "/")
  43  
  44  	var buf strings.Builder
  45  	buf.Grow(
  46  		len(p.Protocol) + 3 + len(p.Host) + 1 + len(p.Port) + len(p.Path) + 1 + len(p.Query),
  47  	)
  48  
  49  	buf.WriteString(p.Protocol)
  50  	buf.WriteString("://")
  51  	buf.WriteString(p.Host)
  52  	if p.Port != "" {
  53  		buf.WriteByte(':')
  54  		buf.WriteString(p.Port)
  55  	}
  56  	buf.WriteString(p.Path)
  57  	if p.Query != "" {
  58  		buf.WriteByte('?')
  59  		buf.WriteString(p.Query)
  60  	}
  61  	return buf.String()
  62  }
  63  
  64  // NormalizeHTTPURL does normalization of http(s):// URLs according to rfc3986. Don't use for relay URLs.
  65  func NormalizeHTTPURL(s string) (string, error) {
  66  	s = strings.TrimSpace(s)
  67  
  68  	if !strings.HasPrefix(s, "http") {
  69  		s = "https://" + s
  70  	}
  71  
  72  	u, err := url.Parse(s)
  73  	if err != nil {
  74  		return s, err
  75  	}
  76  
  77  	if u.Scheme == "" {
  78  		u, err = url.Parse("http://" + s)
  79  		if err != nil {
  80  			return s, err
  81  		}
  82  	}
  83  
  84  	if strings.HasPrefix(s, "//") {
  85  		s = "http:" + s
  86  	}
  87  
  88  	var p int
  89  	switch u.Scheme {
  90  	case "http":
  91  		p = 80
  92  	case "https":
  93  		p = 443
  94  	}
  95  	u.Host = strings.TrimSuffix(u.Host, fmt.Sprintf(":%d", p))
  96  
  97  	v := u.Query()
  98  	u.RawQuery = v.Encode()
  99  	u.RawQuery, _ = url.QueryUnescape(u.RawQuery)
 100  
 101  	h := u.String()
 102  	h = strings.TrimSuffix(h, "/")
 103  
 104  	return h, nil
 105  }
 106  
 107  // NormalizeOKMessage takes a string message that is to be sent in an `OK` or `CLOSED` command
 108  // and prefixes it with "<prefix>: " if it doesn't already have an acceptable prefix.
 109  func NormalizeOKMessage(reason string, prefix string) string {
 110  	if idx := strings.Index(reason, ": "); idx == -1 || strings.IndexByte(reason[0:idx], ' ') != -1 {
 111  		return prefix + ": " + reason
 112  	}
 113  	return reason
 114  }
 115