-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthcache.go
82 lines (73 loc) · 1.42 KB
/
authcache.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
package httpclient
import (
"net/url"
"sort"
"strings"
)
type AuthCache struct {
Domain map[string]AuthPaths
}
func NewAuthCache() *AuthCache {
return &AuthCache{
Domain: make(map[string]AuthPaths),
}
}
func (c *AuthCache) Get(uri *url.URL) (auth string) {
paths, ok := c.Domain[uri.Host]
if !ok {
return
}
for _, v := range paths {
if v.Matches(uri.Path) {
return v.Auth
}
}
return
}
func (c *AuthCache) Set(uri *url.URL, auth string) {
pairs := c.Domain[uri.Host]
if pairs != nil {
for _, v := range pairs {
if v.Path == uri.Path {
v.Auth = auth
return
}
}
}
pairs = append(pairs, AuthPath{Path: uri.Path, Auth: auth})
sort.Sort(pairs)
c.Domain[uri.Host] = pairs
}
type AuthPaths []AuthPath
type AuthPath struct {
Path string
Auth string
}
func (ap AuthPath) Matches(path string) bool {
if ap.Path == path {
return true
}
if strings.HasPrefix(path, ap.Path) {
n := len(ap.Path)
return (ap.Path[n-1] == '/' || path[n] == '/')
}
return false
}
func (p AuthPaths) Len() int { return len(p) }
func (p AuthPaths) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p AuthPaths) Less(i, j int) bool {
if p[i].Path == "/" && p[j].Path != "/" {
return false
}
if p[i].Path != "/" && p[j].Path == "/" {
return true
}
a, b := strings.Count(p[i].Path, "/"), strings.Count(p[j].Path, "/")
if a > b {
return true
}
if a < b {
return false
}
return p[i].Path < p[j].Path
}