Repository navigation
Expand file tree
/
Copy pathutils.go
More file actions
83 lines (68 loc) · 1.96 KB
/
Copy pathutils.go
File metadata and controls
83 lines (68 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package corsproxy
import (
"net/netip"
"net/url"
"strings"
)
// wildcard represents a pattern for wildcard matching in allowed targets.
type wildcard struct {
prefix string
suffix string
}
// match checks if the target URL matches the wildcard pattern.
func (w wildcard) match(s string) bool {
if len(s) >= len(w.prefix)+len(w.suffix) {
if after, found := strings.CutPrefix(s, w.prefix); found {
return strings.HasSuffix(after, w.suffix) || strings.Contains(after, w.suffix+"/")
}
}
return false
}
// isPrivateAddr checks if the given host is a private or loopback IP address.
func isPrivateAddr(host string) (private bool, parsed bool) {
var (
addr netip.Addr
addrPort netip.AddrPort
err error
)
if strings.Contains(host, ":") {
// Parse as address:port
addrPort, err = netip.ParseAddrPort(host)
if err == nil {
addr = addrPort.Addr()
}
} else {
// Parse as address only
addr, err = netip.ParseAddr(host)
}
if err != nil {
return false, false
}
// Check if the address is private or loopback
return addr.IsPrivate() || addr.IsLoopback(), true
}
// StripURLQuery takes a raw URL string, normalizes it, and returns the URL without the query string.
func StripURLQuery(rawURL string) (string, error) {
normalizedURL, err := NormalizeURL(rawURL)
if err != nil {
return "", err
}
return strings.SplitN(normalizedURL, "?", 2)[0], nil
}
// NormalizeURL takes a raw URL string and normalizes it by converting the host to lowercase.
func NormalizeURL(rawURL string) (string, error) {
parsedURL, err := NormalizeParseURL(rawURL)
if err != nil {
return "", err
}
return parsedURL.String(), nil
}
// NormalizeParseURL takes a raw URL string, parses it into a *url.URL, and normalizes it by converting the host to lowercase.
func NormalizeParseURL(rawURL string) (*url.URL, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
parsedURL.Host = strings.ToLower(parsedURL.Host)
return parsedURL, nil
}