forked from kataras/iris
/
trie.go
281 lines (231 loc) · 6.48 KB
/
trie.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
package router
import (
"strings"
"github.com/kataras/iris/context"
)
const (
// ParamStart the character in string representation where the underline router starts its dynamic named parameter.
ParamStart = ":"
// WildcardParamStart the character in string representation where the underline router starts its dynamic wildcard
// path parameter.
WildcardParamStart = "*"
)
// An iris-specific identical version of the https://github.com/kataras/muxie version 1.0.0 released at 15 Oct 2018
type trieNode struct {
parent *trieNode
children map[string]*trieNode
hasDynamicChild bool // does one of the children contains a parameter or wildcard?
childNamedParameter bool // is the child a named parameter (single segmnet)
childWildcardParameter bool // or it is a wildcard (can be more than one path segments) ?
paramKeys []string // the param keys without : or *.
end bool // it is a complete node, here we stop and we can say that the node is valid.
key string // if end == true then key is filled with the original value of the insertion's key.
// if key != "" && its parent has childWildcardParameter == true,
// we need it to track the static part for the closest-wildcard's parameter storage.
staticKey string
// insert data.
Handlers context.Handlers
RouteName string
}
func newTrieNode() *trieNode {
n := new(trieNode)
return n
}
func (tn *trieNode) hasChild(s string) bool {
return tn.getChild(s) != nil
}
func (tn *trieNode) getChild(s string) *trieNode {
if tn.children == nil {
return nil
}
return tn.children[s]
}
func (tn *trieNode) addChild(s string, n *trieNode) {
if tn.children == nil {
tn.children = make(map[string]*trieNode)
}
if _, exists := tn.children[s]; exists {
return
}
n.parent = tn
tn.children[s] = n
}
func (tn *trieNode) findClosestParentWildcardNode() *trieNode {
tn = tn.parent
for tn != nil {
if tn.childWildcardParameter {
return tn.getChild(WildcardParamStart)
}
tn = tn.parent
}
return nil
}
func (tn *trieNode) String() string {
return tn.key
}
type trie struct {
root *trieNode
// if true then it will handle any path if not other parent wildcard exists,
// so even 404 (on http services) is up to it, see trie#insert.
hasRootWildcard bool
hasRootSlash bool
method string
// subdomain is empty for default-hostname routes,
// ex: mysubdomain.
subdomain string
}
func newTrie() *trie {
return &trie{
root: newTrieNode(),
}
}
const (
pathSep = "/"
pathSepB = '/'
)
func slowPathSplit(path string) []string {
if path == "/" {
return []string{"/"}
}
return strings.Split(path, pathSep)[1:]
}
func (tr *trie) insert(path, routeName string, handlers context.Handlers) {
input := slowPathSplit(path)
n := tr.root
if path == pathSep {
tr.hasRootSlash = true
}
var paramKeys []string
for _, s := range input {
c := s[0]
if isParam, isWildcard := c == ParamStart[0], c == WildcardParamStart[0]; isParam || isWildcard {
n.hasDynamicChild = true
paramKeys = append(paramKeys, s[1:]) // without : or *.
// if node has already a wildcard, don't force a value, check for true only.
if isParam {
n.childNamedParameter = true
s = ParamStart
}
if isWildcard {
n.childWildcardParameter = true
s = WildcardParamStart
if tr.root == n {
tr.hasRootWildcard = true
}
}
}
if !n.hasChild(s) {
child := newTrieNode()
n.addChild(s, child)
}
n = n.getChild(s)
}
n.RouteName = routeName
n.Handlers = handlers
n.paramKeys = paramKeys
n.key = path
n.end = true
i := strings.Index(path, ParamStart)
if i == -1 {
i = strings.Index(path, WildcardParamStart)
}
if i == -1 {
i = len(n.key)
}
n.staticKey = path[:i]
}
func (tr *trie) search(q string, params *context.RequestParams) *trieNode {
end := len(q)
if end == 0 || (end == 1 && q[0] == pathSepB) {
// fixes only root wildcard but no / registered at.
if tr.hasRootSlash {
return tr.root.getChild(pathSep)
} else if tr.hasRootWildcard {
// no need to going through setting parameters, this one has not but it is wildcard.
return tr.root.getChild(WildcardParamStart)
}
return nil
}
n := tr.root
start := 1
i := 1
var paramValues []string
for {
if i == end || q[i] == pathSepB {
if child := n.getChild(q[start:i]); child != nil {
n = child
} else if n.childNamedParameter {
n = n.getChild(ParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:i]
} else {
paramValues = append(paramValues, q[start:i])
}
} else if n.childWildcardParameter {
n = n.getChild(WildcardParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:]
} else {
paramValues = append(paramValues, q[start:])
}
break
} else {
n = n.findClosestParentWildcardNode()
if n != nil {
// means that it has :param/static and *wildcard, we go trhough the :param
// but the next path segment is not the /static, so go back to *wildcard
// instead of not found.
//
// Fixes:
// /hello/*p
// /hello/:p1/static/:p2
// req: http://localhost:8080/hello/dsadsa/static/dsadsa => found
// req: http://localhost:8080/hello/dsadsa => but not found!
// and
// /second/wild/*p
// /second/wild/static/otherstatic/
// req: /second/wild/static/otherstatic/random => but not found!
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
return nil
}
if i == end {
break
}
i++
start = i
continue
}
i++
}
if n == nil || !n.end {
if n != nil { // we need it on both places, on last segment (below) or on the first unnknown (above).
if n = n.findClosestParentWildcardNode(); n != nil {
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
}
if tr.hasRootWildcard {
// that's the case for root wildcard, tests are passing
// even without it but stick with it for reference.
// Note ote that something like:
// Routes: /other2/*myparam and /other2/static
// Reqs: /other2/staticed will be handled
// the /other2/*myparam and not the root wildcard, which is what we want.
//
n = tr.root.getChild(WildcardParamStart)
params.Set(n.paramKeys[0], q[1:])
return n
}
return nil
}
for i, paramValue := range paramValues {
if len(n.paramKeys) > i {
params.Set(n.paramKeys[i], paramValue)
}
}
return n
}