-
Notifications
You must be signed in to change notification settings - Fork 56
/
alias.go
88 lines (72 loc) · 1.8 KB
/
alias.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
package serverHandler
import (
"sort"
"strings"
)
type alias interface {
urlPath() string
fsPath() string
caseSensitive() bool
isMatch(rawReqPath string) bool
isSuccessorOf(rawReqPath string) bool
isPredecessorOf(rawReqPath string) bool
namesEqual(a, b string) bool
}
type aliases []alias
func newAliases(entriesAccurate, entriesNoCase map[string]string) aliases {
aliases := make(aliases, 0, len(entriesAccurate)+len(entriesNoCase))
for urlPath, fsPath := range entriesAccurate {
aliases = append(aliases, createAliasAccurate(urlPath, fsPath))
}
for urlPath, fsPath := range entriesNoCase {
aliases = append(aliases, createAliasNoCase(urlPath, fsPath))
}
sort.Sort(aliases)
return aliases
}
func (aliases aliases) byUrlPath(urlPath string) (alias alias, ok bool) {
for _, alias := range aliases {
if alias.isMatch(urlPath) {
return alias, true
}
}
return nil, false
}
func getAliasSubPart(alias alias, rawReqPath string) (subName string, isLastPart, ok bool) {
if !alias.isSuccessorOf(rawReqPath) {
return
}
subName = alias.urlPath()[len(rawReqPath):]
if len(subName) > 0 && subName[0] == '/' {
subName = subName[1:]
}
slashIndex := strings.Index(subName, "/")
if slashIndex > 0 {
subName = subName[:slashIndex]
} else {
isLastPart = true
}
ok = true
return
}
func (aliases aliases) Len() int {
return len(aliases)
}
func (aliases aliases) Less(i, j int) bool {
iLen := len(aliases[i].urlPath())
jLen := len(aliases[j].urlPath())
if iLen != jLen {
// longer is prior
return iLen > jLen
}
_, isIAccurate := aliases[i].(aliasAccurate)
_, isJAccurate := aliases[j].(aliasAccurate)
if isIAccurate != isJAccurate {
// accurate is prior
return isIAccurate
}
return i < j
}
func (aliases aliases) Swap(i, j int) {
aliases[i], aliases[j] = aliases[j], aliases[i]
}