This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.go
432 lines (369 loc) · 10.5 KB
/
tree.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package treemux
import (
"fmt"
"net/http"
"net/url"
"strings"
)
type handlerMap struct {
get HandlerFunc
post HandlerFunc
put HandlerFunc
delete HandlerFunc
head HandlerFunc
options HandlerFunc
patch HandlerFunc
// If true, the head handler was set implicitly, so let it also be set explicitly.
implicitHead bool
}
func newHandlerMap() *handlerMap {
return new(handlerMap)
}
func (h *handlerMap) String() string {
var ss []string
if h.get != nil {
ss = append(ss, http.MethodGet)
}
if h.post != nil {
ss = append(ss, http.MethodPost)
}
if h.put != nil {
ss = append(ss, http.MethodPut)
}
if h.delete != nil {
ss = append(ss, http.MethodDelete)
}
if h.head != nil {
ss = append(ss, http.MethodHead)
}
if h.options != nil {
ss = append(ss, http.MethodOptions)
}
if h.patch != nil {
ss = append(ss, http.MethodPatch)
}
return "[" + strings.Join(ss, " ") + "]"
}
func (h *handlerMap) Get(meth string) HandlerFunc {
switch meth {
case http.MethodGet:
return h.get
case http.MethodPost:
return h.post
case http.MethodPut:
return h.put
case http.MethodDelete:
return h.delete
case http.MethodHead:
return h.head
case http.MethodOptions:
return h.options
case http.MethodPatch:
return h.patch
default:
return nil
}
}
func (h *handlerMap) Set(meth string, handler HandlerFunc) {
switch meth {
case http.MethodGet:
h.get = handler
case http.MethodPost:
h.post = handler
case http.MethodPut:
h.put = handler
case http.MethodDelete:
h.delete = handler
case http.MethodHead:
h.head = handler
case http.MethodOptions:
h.options = handler
case http.MethodPatch:
h.patch = handler
default:
panic(fmt.Errorf("unknown HTTP method: %s", meth))
}
}
type staticNode struct {
*node
firstChar byte
}
type node struct {
route string
path string
priority int
// The list of static children to check.
staticChildren []staticNode
// If none of the above match, check the wildcard children
wildcardChild *node
// If none of the above match, then we use the catch-all, if applicable.
catchAllChild *node
// Data for the node is below.
// If this node is the end of the URL, then call the handler, if applicable.
handlerMap *handlerMap
// The names of the parameters to apply.
leafWildcardNames []string
addSlash bool
isCatchAll bool
}
func (n *node) paramName(i int) string {
return n.leafWildcardNames[len(n.leafWildcardNames)-1-i]
}
func (n *node) sortStaticChildren(i int) {
for i > 0 && n.staticChildren[i].priority > n.staticChildren[i-1].priority {
n.staticChildren[i], n.staticChildren[i-1] = n.staticChildren[i-1], n.staticChildren[i]
i -= 1
}
}
func (n *node) setHandler(verb string, handler HandlerFunc, implicitHead bool) {
if n.handlerMap == nil {
n.handlerMap = newHandlerMap()
}
if h := n.handlerMap.Get(verb); h != nil &&
(verb != http.MethodHead || !n.handlerMap.implicitHead) {
panic(fmt.Sprintf("%s already handles %s", n.path, verb))
}
n.handlerMap.Set(verb, handler)
if verb == http.MethodHead {
n.handlerMap.implicitHead = implicitHead
}
}
func (n *node) addPath(path string, wildcards []string, inStaticToken bool) *node {
if path == "" { // leaf
if wildcards == nil {
return n
}
if n.leafWildcardNames == nil {
// No wildcards yet, so just add the existing set.
n.leafWildcardNames = wildcards
return n
}
// Make sure the current wildcards are the same as the old ones.
// If not then we have an ambiguous path.
if len(n.leafWildcardNames) != len(wildcards) {
// This should never happen.
panic("Reached leaf node with differing wildcard array length. Please report this as a bug.")
}
for i := 0; i < len(wildcards); i++ {
if n.leafWildcardNames[i] != wildcards[i] {
panic(fmt.Sprintf("Wildcards %v are ambiguous with wildcards %v",
n.leafWildcardNames, wildcards))
}
}
return n
}
c := path[0]
nextSlash := strings.IndexByte(path, '/')
var thisToken string
var tokenEnd int
if c == '/' {
// Done processing the previous token, so reset inStaticToken to false.
thisToken = "/"
tokenEnd = 1
} else if nextSlash == -1 {
thisToken = path
tokenEnd = len(path)
} else {
thisToken = path[0:nextSlash]
tokenEnd = nextSlash
}
remainingPath := path[tokenEnd:]
if c == '*' && !inStaticToken {
// Token starts with a *, so it's a catch-all
thisToken = thisToken[1:]
if n.catchAllChild == nil {
n.catchAllChild = &node{path: thisToken, isCatchAll: true}
}
if path[1:] != n.catchAllChild.path {
panic(fmt.Sprintf("Catch-all name in %s doesn't match %s. You probably tried to define overlapping catchalls",
path, n.catchAllChild.path))
}
if nextSlash != -1 {
panic("/ after catch-all found in " + path)
}
if wildcards == nil {
wildcards = []string{thisToken}
} else {
wildcards = append(wildcards, thisToken)
}
n.catchAllChild.leafWildcardNames = wildcards
return n.catchAllChild
}
if c == ':' && !inStaticToken {
// Token starts with a :
thisToken = thisToken[1:]
if wildcards == nil {
wildcards = []string{thisToken}
} else {
wildcards = append(wildcards, thisToken)
}
if n.wildcardChild == nil {
n.wildcardChild = &node{path: "wildcard"}
}
return n.wildcardChild.addPath(remainingPath, wildcards, false)
}
// if strings.ContainsAny(thisToken, ":*") {
// panic("* or : in middle of path component " + path)
// }
var unescaped bool
if len(thisToken) >= 2 && !inStaticToken {
if thisToken[0] == '\\' && (thisToken[1] == '*' || thisToken[1] == ':' || thisToken[1] == '\\') {
// The token starts with a character escaped by a backslash. Drop the backslash.
c = thisToken[1]
thisToken = thisToken[1:]
unescaped = true
}
}
// Set inStaticToken to ensure that the rest of this token is not mistaken
// for a wildcard if a prefix split occurs at a '*' or ':'.
inStaticToken = (c != '/')
// Do we have an existing node that starts with the same letter?
for i, staticChildren := range n.staticChildren {
if staticChildren.firstChar == c {
// Yes. Split it based on the common prefix of the existing
// node and the new one.
child, prefixSplit := n.splitCommonPrefix(staticChildren.node, thisToken)
n.staticChildren[i].node = child
child.priority++
n.sortStaticChildren(i)
if unescaped {
// Account for the removed backslash.
prefixSplit++
}
return child.addPath(path[prefixSplit:], wildcards, inStaticToken)
}
}
// No existing node starting with this letter, so create it.
child := &node{path: thisToken}
if n.staticChildren == nil {
n.staticChildren = []staticNode{{firstChar: c, node: child}}
} else {
n.staticChildren = append(n.staticChildren, staticNode{firstChar: c, node: child})
}
return child.addPath(remainingPath, wildcards, inStaticToken)
}
func (n *node) splitCommonPrefix(childNode *node, path string) (*node, int) {
if strings.HasPrefix(path, childNode.path) {
// No split needs to be done. Rather, the new path shares the entire
// prefix with the existing node, so the new node is just a child of
// the existing one. Or the new path is the same as the existing path,
// which means that we just move on to the next token. Either way,
// this return accomplishes that
return childNode, len(childNode.path)
}
var i int
// Find the length of the common prefix of the child node and the new path.
for i = range childNode.path {
if i == len(path) {
break
}
if path[i] != childNode.path[i] {
break
}
}
commonPrefix := path[:i]
childNode.path = childNode.path[i:]
// Create a new intermediary node in the place of the existing node, with
// the existing node as a child.
newNode := &node{
path: commonPrefix,
priority: childNode.priority,
staticChildren: []staticNode{{
firstChar: childNode.path[0],
node: childNode,
}},
}
return newNode, i
}
func (n *node) search(method, path string) (*node, HandlerFunc, []Param) {
// if test != nil {
// test.Logf("Searching for %s in %s", path, n.dumpTree("", ""))
// }
if path == "" {
if n.handlerMap == nil {
return nil, nil, nil
}
return n, n.handlerMap.Get(method), nil
}
var lastNode *node
// First see if this matches a static token.
firstChar := path[0]
for _, child := range n.staticChildren {
if child.firstChar == firstChar {
if strings.HasPrefix(path, child.path) {
nextPath := path[len(child.path):]
node, handler, params := child.search(method, nextPath)
if handler != nil {
return node, handler, params
}
lastNode = node
}
break
}
}
if n.wildcardChild != nil {
// Didn't find a static token, so check for a wildcard.
nextSlash := strings.IndexByte(path, '/')
if nextSlash < 0 {
nextSlash = len(path)
}
thisToken := path[:nextSlash]
nextToken := path[nextSlash:]
if len(thisToken) > 0 { // Don't match on empty tokens.
node, handler, params := n.wildcardChild.search(method, nextToken)
if handler != nil || (lastNode == nil && node != nil) {
unescaped, err := url.PathUnescape(thisToken)
if err != nil {
unescaped = thisToken
}
if params == nil {
params = make([]Param, 0, len(node.leafWildcardNames))
}
params = append(params, Param{
Name: node.paramName(len(params)),
Value: unescaped,
})
if handler != nil {
return node, handler, params
}
// Didn't actually find a handler here, so remember that we
// found a node but also see if we can fall through to the
// catchall.
lastNode = node
}
}
}
if n.catchAllChild != nil {
// Hit the catchall, so just assign the whole remaining path if it
// has a matching handler.
handler := n.catchAllChild.handlerMap.Get(method)
// Found a handler, or we found a catchall node without a handler.
// Either way, return it since there's nothing left to check after this.
if handler != nil || lastNode == nil {
unescaped, err := url.PathUnescape(path)
if err != nil {
unescaped = path
}
return n.catchAllChild, handler, []Param{{
Name: n.catchAllChild.paramName(0),
Value: unescaped,
}}
}
}
return lastNode, nil, nil
}
func (n *node) dumpTree(prefix, nodeType string) string {
line := fmt.Sprintf("%s %02d %s%s [%d] %v wildcards %v\n", prefix, n.priority, nodeType, n.path,
len(n.staticChildren), n.handlerMap, n.leafWildcardNames)
prefix += " "
for _, node := range n.staticChildren {
line += node.dumpTree(prefix, "")
}
if n.wildcardChild != nil {
line += n.wildcardChild.dumpTree(prefix, ":")
}
if n.catchAllChild != nil {
line += n.catchAllChild.dumpTree(prefix, "*")
}
return line
}