-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathurlx.go
212 lines (178 loc) · 5.32 KB
/
urlx.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Package urlx parses and normalizes URLs. It can also resolve hostname to an IP address.
package urlx
import (
"errors"
"net"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/purell"
"golang.org/x/net/idna"
)
// Parse parses raw URL string into the net/url URL struct.
// It uses the url.Parse() internally, but it slightly changes
// its behavior:
// 1. It forces the default scheme and port to http
// 2. It favors absolute paths over relative ones, thus "example.com"
// is parsed into url.Host instead of url.Path.
// 3. It lowercases the Host (not only the Scheme).
func Parse(rawURL string) (*url.URL, error) {
return ParseWithDefaultScheme(rawURL, "http")
}
func ParseWithDefaultScheme(rawURL string, scheme string) (*url.URL, error) {
rawURL = defaultScheme(rawURL, scheme)
// Use net/url.Parse() now.
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
if err := checkHost(host); err != nil {
return nil, err
}
u.Host = strings.ToLower(u.Host)
u.Scheme = strings.ToLower(u.Scheme)
return u, nil
}
func defaultScheme(rawURL, scheme string) string {
// Force default http scheme, so net/url.Parse() doesn't
// put both host and path into the (relative) path.
if strings.Index(rawURL, "//") == 0 {
// Leading double slashes (any scheme). Force http.
rawURL = scheme + ":" + rawURL
}
if !strings.Contains(rawURL, "://") {
// Missing scheme. Force http.
rawURL = scheme + "://" + rawURL
}
return rawURL
}
var (
domainRegexp = regexp.MustCompile(`^([a-zA-Z0-9-_]{1,63}\.)*([a-zA-Z0-9-]{1,63})$`)
ipv4Regexp = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`)
ipv6Regexp = regexp.MustCompile(`^\[[a-fA-F0-9:]+\]$`)
)
func checkHost(host string) error {
if host == "" {
return &url.Error{Op: "host", URL: host, Err: errors.New("empty host")}
}
host = strings.ToLower(host)
if domainRegexp.MatchString(host) {
return nil
}
if punycode, err := idna.ToASCII(host); err != nil {
return err
} else if domainRegexp.MatchString(punycode) {
return nil
}
// IPv4 and IPv6.
if ipv4Regexp.MatchString(host) || ipv6Regexp.MatchString(host) {
return nil
}
return &url.Error{Op: "host", URL: host, Err: errors.New("invalid host")}
}
// SplitHostPort splits network address of the form "host:port" into
// host and port. Unlike net.SplitHostPort(), it doesn't remove brackets
// from [IPv6] host and it accepts net/url.URL struct instead of a string.
func SplitHostPort(u *url.URL) (host, port string, err error) {
if u == nil {
return "", "", &url.Error{Op: "host", URL: host, Err: errors.New("empty url")}
}
host = u.Host
// Find last colon.
i := strings.LastIndex(host, ":")
if i == -1 {
// No port found.
return host, "", nil
}
// Return if the last colon is inside [IPv6] brackets.
if strings.HasPrefix(host, "[") && strings.Contains(host[i:], "]") {
// No port found.
return host, "", nil
}
if i == len(host)-1 {
return "", "", &url.Error{Op: "port", URL: u.String(), Err: errors.New("empty port")}
}
port = host[i+1:]
host = host[:i]
if _, err := strconv.Atoi(port); err != nil {
return "", "", &url.Error{Op: "port", URL: u.String(), Err: err}
}
return host, port, nil
}
const normalizeFlags purell.NormalizationFlags = purell.FlagRemoveDefaultPort |
purell.FlagDecodeDWORDHost | purell.FlagDecodeOctalHost | purell.FlagDecodeHexHost |
purell.FlagRemoveUnnecessaryHostDots | purell.FlagRemoveDotSegments | purell.FlagRemoveDuplicateSlashes |
purell.FlagUppercaseEscapes | purell.FlagDecodeUnnecessaryEscapes | purell.FlagEncodeNecessaryEscapes |
purell.FlagSortQuery
// Normalize returns normalized URL string.
// Behavior:
// 1. Remove unnecessary host dots.
// 2. Remove default port (http://localhost:80 becomes http://localhost).
// 3. Remove duplicate slashes.
// 4. Remove unnecessary dots from path.
// 5. Sort query parameters.
// 6. Decode host IP into decimal numbers.
// 7. Handle escape values.
// 8. Decode Punycode domains into UTF8 representation.
func Normalize(u *url.URL) (string, error) {
host, port, err := SplitHostPort(u)
if err != nil {
return "", err
}
if err := checkHost(host); err != nil {
return "", err
}
// Decode Punycode.
host, err = idna.ToUnicode(host)
if err != nil {
return "", err
}
u.Host = strings.ToLower(host)
if port != "" {
u.Host += ":" + port
}
u.Scheme = strings.ToLower(u.Scheme)
return purell.NormalizeURL(u, normalizeFlags), nil
}
// NormalizeString returns normalized URL string.
// It's a shortcut for Parse() and Normalize() funcs.
func NormalizeString(rawURL string) (string, error) {
u, err := Parse(rawURL)
if err != nil {
return "", err
}
return Normalize(u)
}
// Resolve resolves the URL host to its IP address.
func Resolve(u *url.URL) (*net.IPAddr, error) {
host, _, err := SplitHostPort(u)
if err != nil {
return nil, err
}
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
return nil, err
}
return addr, nil
}
// Resolve resolves the URL host to its IP address.
// It's a shortcut for Parse() and Resolve() funcs.
func ResolveString(rawURL string) (*net.IPAddr, error) {
u, err := Parse(rawURL)
if err != nil {
return nil, err
}
return Resolve(u)
}
func URIEncode(uri string) (string, error) {
u, err := url.Parse(uri)
if err != nil {
return "", err
}
return u.String(), nil
}