forked from celrenheit/lion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
158 lines (132 loc) · 3.43 KB
/
host.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
package lion
import (
"net/http"
"strings"
"github.com/celrenheit/lion/internal/matcher"
)
const (
defaultAnyHostKey = "lionDefaultAnyHostKey"
defaultAnyHostPattern = "*" + defaultAnyHostKey
)
type hostMatcher struct {
matcher matcher.Matcher
defaultRM registerMatcher
multihost bool
}
func newHostMatcher() *hostMatcher {
cfg := &matcher.Config{
ParamChar: '$',
WildcardChar: '*',
Separators: ".:",
New: func() matcher.Store {
return &hostStore{
rm: newPathMatcher(),
}
},
ParamTransformer: newHostParamTransformer(),
}
return &hostMatcher{
matcher: matcher.Custom(cfg),
defaultRM: newPathMatcher(),
}
}
func (hm *hostMatcher) Register(pattern string) registerMatcher {
host := pattern
// Switch to multihost
if !hm.multihost && host != "" {
hm.multihost = true
hm.matcher.Set(reverseHost(defaultAnyHostPattern), hm.defaultRM, nil)
}
if hm.multihost {
if host == "" {
host = defaultAnyHostPattern
}
hs := &hostStore{}
reversedHost := reverseHost(host)
hs = hm.matcher.Set(reversedHost, hs, nil).(*hostStore)
return hs.rm
}
return hm.defaultRM
}
func (hm *hostMatcher) Match(c *ctx, req *http.Request) http.Handler {
if hm.multihost {
reversedHost := reverseHost(req.Host)
value, _ := hm.matcher.GetWithContext(c, reversedHost, nil)
// Delete wildcard param
// TODO: Skip this step for performance reasons
// (Maybe by adding a blacklisted or skiplisted params on host matcher)
if _, ok := c.ParamOk(defaultAnyHostKey); ok {
c.Remove(defaultAnyHostKey)
}
if rm, ok := value.(registerMatcher); ok {
_, h := rm.Match(c, req)
return h
}
} else {
_, h := hm.defaultRM.Match(c, req)
return h
}
return nil
}
type hostStore struct {
rm registerMatcher
}
func (hs *hostStore) Set(value interface{}, tags matcher.Tags) {
// Overwrite RegisterMatcher
if rm, ok := value.(registerMatcher); ok {
hs.rm = rm
}
}
func (hs *hostStore) Get(tags matcher.Tags) interface{} {
return hs.rm
}
type hostParamTransformer struct{}
func newHostParamTransformer() *hostParamTransformer {
return &hostParamTransformer{}
}
func (hpt *hostParamTransformer) Transform(input string) string {
// Split host based on '.' character
reversed := hpt.split(input, ".")
// Split and reverse each host parts if it has a port character ':'
portPart := reversed[len(reversed)-1]
if strings.Contains(portPart, ":") {
splitted := strings.Split(portPart, ":")
splitted[0], splitted[1] = splitted[1], splitted[0]
reversed[len(reversed)-1] = strings.Join(splitted, ":")
}
for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
reversed[i], reversed[j] = reversed[j], reversed[i]
}
output := strings.Join(reversed, ".")
return output
}
// Taken from Go's standard library
// https://github.com/golang/go/blob/master/src/strings/strings.go#L237-L261
func (hpt *hostParamTransformer) split(s, sep string) []string {
slice := []string{}
n := strings.Count(s, sep) + 1
c := sep[0]
start := 0
var a []string
if cap(slice) == n {
a = slice
} else {
a = make([]string, n)
}
na := 0
for i := 0; i+len(sep) <= len(s) && na+1 < n; i++ {
if s[i] == c && (len(sep) == 1 || s[i:i+len(sep)] == sep) && (i == 0 || s[i-1] != '\\') {
a[na] = s[start:i]
na++
start = i + len(sep)
i += len(sep) - 1
}
}
a[na] = s[start:]
slice = a[0 : na+1]
return slice
}
var hostReverser = newHostParamTransformer()
func reverseHost(input string) string {
return hostReverser.Transform(input)
}