/
url.go
383 lines (336 loc) · 8.39 KB
/
url.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package url
import (
"crypto/sha256"
"encoding/binary"
"encoding/json"
"net/url"
"path"
"strings"
)
// URL is an Accumulate URL.
type URL struct {
UserInfo string
Authority string
Path string
Query string
Fragment string
}
type Values = url.Values
// Parse parses the string as an Accumulate URL. The scheme may be omitted, in
// which case `acc://` will be added, but if present it must be `acc`. The
// hostname must be non-empty. RawPath, ForceQuery, and RawFragment are not
// preserved.
func Parse(s string) (*URL, error) {
u, err := url.Parse(s)
if err == nil && u.Scheme == "" {
u, err = url.Parse("acc://" + s)
}
if err != nil {
return nil, err
}
if u.Scheme != "acc" {
return nil, wrongScheme(s)
}
if u.Host == "" || u.Host[0] == ':' {
return nil, missingHost(s)
}
v := new(URL)
v.Authority = u.Host
v.Path = u.Path
v.Query = u.RawQuery
v.Fragment = u.Fragment
if u.User != nil {
v.UserInfo = u.User.Username()
if pw, _ := u.User.Password(); pw != "" {
v.UserInfo += ":" + pw
}
}
return v, nil
}
// MustParse calls Parse and panics if it returns an error.
func MustParse(s string) *URL {
u, err := Parse(s)
if err != nil {
panic(err)
}
return u
}
func splitColon(s string) (string, string) {
t := strings.SplitN(s, ":", 2)
if len(t) == 1 {
return t[0], ""
}
return t[0], t[1]
}
// URL returns a net/url.URL.
func (u *URL) URL() *url.URL {
v := new(url.URL)
v.Scheme = "acc"
v.Host = u.Authority
v.Path = u.Path
v.RawQuery = u.Query
v.Fragment = u.Fragment
if u.UserInfo != "" {
user, pass := splitColon(u.UserInfo)
if pass != "" {
v.User = url.UserPassword(user, pass)
} else {
v.User = url.User(user)
}
}
return v
}
// Compare returns an integer comparing two URLs as lower case strings. The
// result will be 0 if u == v, -1 if u < v, and +1 if u > v.
func (u *URL) Compare(v *URL) int {
uStr := strings.ToLower(u.String())
vStr := strings.ToLower(v.String())
switch {
case uStr < vStr:
return -1
case uStr > vStr:
return +1
default:
return 0
}
}
// Copy returns a copy of the url.
func (u *URL) Copy() *URL {
v := *u
return &v
}
// String reassembles the URL into a valid URL string. See net/url.URL.String().
func (u *URL) String() string {
return u.URL().String()
}
// ShortString returns String without the scheme prefix.
func (u *URL) ShortString() string {
return u.String()[6:]
}
// RawString concatenates all of the URL parts. Does not percent-encode
// anything. Primarily used for validation.
func (u *URL) RawString() string {
s := "acc://"
if u.UserInfo != "" {
s += u.UserInfo + "@"
}
s += u.Authority + u.Path
if u.Query != "" {
s += "?" + u.Query
}
if u.Fragment != "" {
s += "#" + u.Fragment
}
return s
}
// Hostname returns the hostname from the authority component.
func (u *URL) Hostname() string {
s, _ := splitColon(u.Authority)
return s
}
// Port returns the port from the authority component.
func (u *URL) Port() string {
_, s := splitColon(u.Authority)
return s
}
// Username returns the username from the user info component.
func (u *URL) Username() string {
s, _ := splitColon(u.UserInfo)
return s
}
// Password returns the password from the user info component.
func (u *URL) Password() string {
_, s := splitColon(u.UserInfo)
return s
}
// QueryValues parses Query and returns the corresponding values. It silently
// discards malformed value pairs. To check errors use net/url.ParseQuery.
func (u *URL) QueryValues() Values {
v, _ := url.ParseQuery(u.Query)
return v
}
func id(s string) [32]byte {
s = strings.ToLower(s)
h := sha256.Sum256([]byte(s))
return h
}
func concatId(ids ...[32]byte) [32]byte {
digest := sha256.New()
for _, id := range ids {
digest.Write(id[:]) //nolint:rangevarref
}
var result [32]byte
copy(result[:], digest.Sum(nil))
return result
}
func ensurePath(s string) string {
if s == "" || s[0] == '/' {
return s
}
return "/" + s
}
// RootIdentity returns a copy of the URL with an empty path.
func (u *URL) RootIdentity() *URL {
return &URL{Authority: u.Authority, UserInfo: u.UserInfo}
}
// Identity returns a copy of the URL with the last section cut off the path.
func (u *URL) Identity() *URL {
v, _ := u.Parent()
return v
}
// IsRootIdentity returns true if the URL is a root identity.
func (u *URL) IsRootIdentity() bool {
return u.Path == "" || u.Path == "/"
}
// Parent gets the URL's parent path, or returns the original URL and false.
func (u *URL) Parent() (*URL, bool) {
v := new(URL)
v.UserInfo = u.UserInfo
v.Authority = u.Authority
v.Path = u.Path
// Canonicalize the path
v.Path = strings.TrimSuffix(v.Path, "/")
if len(v.Path) == 0 {
return v, false
}
slashIdx := strings.LastIndex(v.Path, "/")
if slashIdx == -1 {
v.Path = ""
} else {
v.Path = v.Path[:slashIdx]
}
return v, true
}
// ParentOf returns true if U is the parent of V
func (u *URL) ParentOf(v *URL) bool {
v, ok := v.Parent()
return ok && u.Equal(v)
}
// PrefixOf returns true if U is a prefix of V.
func (u *URL) PrefixOf(v *URL) bool {
if !strings.EqualFold(u.Authority, v.Authority) {
return false
}
if len(u.Path) > len(v.Path) {
return false
}
for {
var ok bool
v, ok = v.Parent()
if !ok {
return false
}
if u.Equal(v) {
return true
}
}
}
// LocalTo returns true if U is local to V, that is if they have the same root
// identity.
func (u *URL) LocalTo(v *URL) bool {
return u.RootIdentity().Equal(v.RootIdentity())
}
// IdentityAccountID constructs an account identifier from the lower case
// hostname. The port is not included.
//
// ID = Hash(LowerCase(u.Host()))
func (u *URL) IdentityAccountID() []byte {
c := u.IdentityAccountID32()
return c[:]
}
// IdentityAccountID32 returns IdentityAccountID as a [32]byte.
func (u *URL) IdentityAccountID32() [32]byte {
return id(u.Hostname())
}
// AccountID constructs an account identifier from the lower case hostname and
// path. The port is not included. If the path does not begin with `/`, `/` is
// added between the hostname and the path.
//
// ID = Hash(LowerCase(Sprintf("%s/%s", u.Host(), u.Path)))
func (u *URL) AccountID() []byte {
c := u.AccountID32()
return c[:]
}
// AccountID32 returns AccountID as a [32]byte.
func (u *URL) AccountID32() [32]byte {
return id(u.Hostname() + ensurePath(u.Path))
}
// Routing returns the first 8 bytes of the identity account ID as an integer.
//
// Routing = uint64(u.IdentityAccountID()[:8])
func (u *URL) Routing() uint64 {
return binary.BigEndian.Uint64(u.IdentityAccountID())
}
// Hash calculates a hash of the URL starting with AccountID, hashing and
// concatenating Query unless it is empty, and hashing and concatenating
// Fragment unless it is empty. If Query and Fragment are both non-empty, with H
// defined as the SHA-256 hash of the lower case of the operand, the result is
// H(H(AccountID + H(Query)) + H(Fragment)).
func (u *URL) Hash() []byte {
hash := u.Hash32()
return hash[:]
}
// Hash32 returns Hash as a [32]byte.
func (u *URL) Hash32() [32]byte {
hash := u.AccountID32()
if u.Query != "" {
hash = concatId(hash, id(u.Query))
}
if u.Fragment != "" {
hash = concatId(hash, id(u.Fragment))
}
return hash
}
// Equal reports whether u and v, converted to strings and interpreted as UTF-8,
// are equal under Unicode case-folding.
func (u *URL) Equal(v *URL) bool {
if u == v {
return true
}
if u == nil || v == nil {
return false
}
return strings.EqualFold(u.String(), v.String())
}
// PathEqual reports whether u.Path and v, normalized and interpreted as UTF-8,
// are equal under Unicode case-folding.
func (u *URL) PathEqual(v string) bool {
up, v := strings.Trim(u.Path, "/"), strings.Trim(v, "/")
return strings.EqualFold(up, v)
}
// JoinPath returns a copy of U with additional path elements.
func (u *URL) JoinPath(s ...string) *URL {
if len(s) == 0 {
return u
}
v := u.Copy()
if len(v.Path) == 0 {
v.Path = "/"
}
v.Path = path.Join(append([]string{v.Path}, s...)...)
return v
}
// WithFragment returns a copy of U with the fragment set.
func (u *URL) WithFragment(s string) *URL {
v := u.Copy()
v.Fragment = s
return v
}
// MarshalJSON marshals the URL to JSON as a string.
func (u *URL) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}
// UnmarshalJSON unmarshals the URL from JSON as a string.
func (u *URL) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
v, err := Parse(s)
if err != nil {
return err
}
*u = *v
return nil
}