-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathurl.go
More file actions
271 lines (232 loc) · 6 KB
/
url.go
File metadata and controls
271 lines (232 loc) · 6 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package libcore
import (
"errors"
"net"
"net/url"
"strconv"
"strings"
_ "unsafe"
)
type URL interface {
GetScheme() string
SetScheme(scheme string)
GetOpaque() string
SetOpaque(opaque string)
GetUsername() string
SetUsername(username string)
GetPassword() string
SetPassword(password string) error
GetHost() string
SetHost(host string)
GetPort() int32
SetPort(port int32)
GetPath() string
SetPath(path string)
GetRawPath() string
SetRawPath(rawPath string) error
QueryParameterNotBlank(key string) string
AddQueryParameter(key, value string)
GetFragment() string
SetRawFragment(rawFragment string) error
GetString() string
}
var _ URL = (*netURL)(nil)
type netURL struct {
url.URL
url.Values
}
func NewURL(scheme string) URL {
u := new(netURL)
u.Scheme = scheme
u.Values = make(url.Values)
return u
}
//go:linkname getScheme net/url.getScheme
func getScheme(rawURL string) (scheme, path string, err error)
//go:linkname parseAuthority net/url.parseAuthority
func parseAuthority(authority string) (user *url.Userinfo, host string, err error)
//go:linkname setFragment net/url.(*URL).setFragment
func setFragment(u *url.URL, fragment string) error
//go:linkname setPath net/url.(*URL).setPath
func setPath(u *url.URL, fragment string) error
// parse parses a URL from a string in one of two contexts. If
// viaRequest is true, the URL is assumed to have arrived via an HTTP request,
// in which case only absolute URLs or path-absolute relative URLs are allowed.
// If viaRequest is false, all forms of relative URLs are allowed.
func parse(rawURL string) (*url.URL, error) {
var rest string
var err error
url := new(url.URL)
if rawURL == "*" {
url.Path = "*"
return url, nil
}
// Split off possible leading "http:", "mailto:", etc.
// Cannot contain escaped characters.
if url.Scheme, rest, err = getScheme(rawURL); err != nil {
return nil, err
}
url.Scheme = strings.ToLower(url.Scheme)
if strings.HasSuffix(rest, "?") && strings.Count(rest, "?") == 1 {
url.ForceQuery = true
rest = rest[:len(rest)-1]
} else {
rest, url.RawQuery, _ = strings.Cut(rest, "?")
}
if !strings.HasPrefix(rest, "/") {
if url.Scheme != "" {
// We consider rootless paths per RFC 3986 as opaque.
url.Opaque = rest
return url, nil
}
// Avoid confusion with malformed schemes, like cache_object:foo/bar.
// See golang.org/issue/16822.
//
// RFC 3986, §3.3:
// In addition, a URI reference (Section 4.1) may be a relative-path reference,
// in which case the first path segment cannot contain a colon (":") character.
if segment, _, _ := strings.Cut(rest, "/"); strings.Contains(segment, ":") {
// First path segment has colon. Not allowed in relative URL.
return nil, errors.New("first path segment in URL cannot contain colon")
}
}
if (url.Scheme != "" || !strings.HasPrefix(rest, "///")) && strings.HasPrefix(rest, "//") {
var authority string
authority, rest = rest[2:], ""
if i := strings.Index(authority, "/"); i >= 0 {
authority, rest = authority[:i], authority[i:]
}
url.User, url.Host, err = parseAuthority(authority)
if err != nil {
return nil, err
}
}
// Set Path and, optionally, RawPath.
// RawPath is a hint of the encoding of Path. We don't want to set it if
// the default escaping of Path is equivalent, to help make sure that people
// don't rely on it in general.
if err := setPath(url, rest); err != nil {
return nil, err
}
return url, nil
}
func ParseURL(rawURL string) (URL, error) {
u := &netURL{}
ru, frag, _ := strings.Cut(rawURL, "#")
uu, err := parse(ru)
if err != nil {
return nil, newError("failed to parse url: ", rawURL).Base(err)
}
u.URL = *uu
u.Values = u.Query()
if u.Values == nil {
u.Values = make(url.Values)
}
if frag == "" {
return u, nil
}
if err = u.SetRawFragment(frag); err != nil {
return nil, err
}
return u, nil
}
func (u *netURL) GetScheme() string {
return u.Scheme
}
func (u *netURL) SetScheme(scheme string) {
u.Scheme = scheme
}
func (u *netURL) GetOpaque() string {
return u.Opaque
}
func (u *netURL) SetOpaque(opaque string) {
u.Opaque = opaque
}
func (u *netURL) GetUsername() string {
if u.User != nil {
return u.User.Username()
}
return ""
}
func (u *netURL) SetUsername(username string) {
if u.User != nil {
if password, ok := u.User.Password(); !ok {
u.User = url.User(username)
} else {
u.User = url.UserPassword(username, password)
}
} else {
u.User = url.User(username)
}
}
func (u *netURL) GetPassword() string {
if u.User != nil {
if password, ok := u.User.Password(); ok {
return password
}
}
return ""
}
func (u *netURL) SetPassword(password string) error {
if u.User == nil {
return newError("set username first")
}
u.User = url.UserPassword(u.User.Username(), password)
return nil
}
func (u *netURL) GetHost() string {
return u.Hostname()
}
func (u *netURL) SetHost(host string) {
_, port, err := net.SplitHostPort(u.Host)
if err == nil {
u.Host = net.JoinHostPort(host, port)
} else {
u.Host = host
}
}
func (u *netURL) GetPort() int32 {
portStr := u.Port()
if portStr == "" {
return 0
}
port, _ := strconv.Atoi(portStr)
return int32(port)
}
func (u *netURL) SetPort(port int32) {
host, _, err := net.SplitHostPort(u.Host)
if err == nil {
u.Host = net.JoinHostPort(host, strconv.Itoa(int(port)))
} else {
u.Host = net.JoinHostPort(u.Host, strconv.Itoa(int(port)))
}
}
func (u *netURL) GetPath() string {
return u.Path
}
func (u *netURL) SetPath(path string) {
u.Path = path
u.RawPath = ""
}
func (u *netURL) GetRawPath() string {
return u.RawPath
}
func (u *netURL) SetRawPath(rawPath string) error {
return setPath(&u.URL, rawPath)
}
func (u *netURL) QueryParameterNotBlank(key string) string {
return u.Get(key)
}
func (u *netURL) AddQueryParameter(key, value string) {
u.Add(key, value)
}
func (u *netURL) GetFragment() string {
return u.Fragment
}
func (u *netURL) SetRawFragment(rawFragment string) error {
return setFragment(&u.URL, rawFragment)
}
func (u *netURL) GetString() string {
u.RawQuery = u.Encode()
return u.String()
}