-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtree.go
541 lines (488 loc) · 11.6 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package baa
import (
"fmt"
"sync"
)
const (
leafKindStatic uint = iota
leafKindParam
leafKindWide
)
// Tree provlider router for baa with radix tree
type Tree struct {
autoHead bool
autoTrailingSlash bool
mu sync.RWMutex
groups []*group
nodes [RouteLength]*leaf
baa *Baa
nameNodes map[string]*Node
}
// Node is struct for named route
type Node struct {
paramNum int
pattern string
format string
name string
root *Tree
}
// Leaf is a tree node
type leaf struct {
kind uint
pattern string
param string
handlers []HandlerFunc
children []*leaf
childrenNum uint
paramChild *leaf
wideChild *leaf
root *Tree
nameNode *Node
}
// group route
type group struct {
pattern string
handlers []HandlerFunc
}
// NewTree create a router instance
func NewTree(b *Baa) Router {
t := new(Tree)
for i := 0; i < len(t.nodes); i++ {
t.nodes[i] = newLeaf("/", nil, t)
}
t.nameNodes = make(map[string]*Node)
t.groups = make([]*group, 0)
t.baa = b
return t
}
// NewNode create a route node
func NewNode(pattern string, root *Tree) *Node {
return &Node{
pattern: pattern,
root: root,
}
}
// newLeaf create a tree leaf
func newLeaf(pattern string, handlers []HandlerFunc, root *Tree) *leaf {
l := new(leaf)
l.pattern = pattern
l.handlers = handlers
l.root = root
l.kind = leafKindStatic
l.children = make([]*leaf, 128)
return l
}
// newGroup create a group router
func newGroup() *group {
g := new(group)
g.handlers = make([]HandlerFunc, 0)
return g
}
// SetAutoHead sets the value who determines whether add HEAD method automatically
// when GET method is added. Combo router will not be affected by this value.
func (t *Tree) SetAutoHead(v bool) {
t.autoHead = v
}
// SetAutoTrailingSlash optional trailing slash.
func (t *Tree) SetAutoTrailingSlash(v bool) {
t.autoTrailingSlash = v
}
// Match find matched route then returns handlers and name
func (t *Tree) Match(method, pattern string, c *Context) ([]HandlerFunc, string) {
var i, l int
var root, nl *leaf
root = t.nodes[RouterMethods[method]]
current := root
for {
switch current.kind {
case leafKindStatic:
// static route
l = len(current.pattern)
if l > len(pattern) {
break
}
i := l - 1
for ; i >= 0; i-- {
if current.pattern[i] != pattern[i] {
break
}
}
if i >= 0 {
break
}
if len(pattern) == l || current.children[pattern[l]] != nil ||
current.paramChild != nil ||
current.wideChild != nil {
pattern = pattern[l:]
root = current
}
case leafKindParam:
// params route
l = len(pattern)
for i = 0; i < l && pattern[i] != '/'; i++ {
}
c.SetParam(current.param, pattern[:i])
pattern = pattern[i:]
root = current
case leafKindWide:
// wide route
c.SetParam(current.param, pattern)
pattern = pattern[:0]
default:
}
if len(pattern) == 0 {
if current.handlers != nil {
if current.nameNode != nil {
return current.handlers, current.nameNode.name
}
return current.handlers, ""
}
if root.paramChild == nil && root.wideChild == nil {
return nil, ""
}
} else {
// children static route
if current == root {
if nl = root.children[pattern[0]]; nl != nil {
current = nl
continue
}
}
}
// param route
if root.paramChild != nil {
current = root.paramChild
continue
}
// wide route
if root.wideChild != nil {
current = root.wideChild
continue
}
break
}
return nil, ""
}
// URLFor use named route return format url
func (t *Tree) URLFor(name string, args ...interface{}) string {
if name == "" {
return ""
}
node := t.nameNodes[name]
if node == nil || len(node.format) == 0 {
return ""
}
format := make([]byte, len(node.format))
copy(format, node.format)
for i := node.paramNum + 1; i <= len(args); i++ {
format = append(format, "%v"...)
}
return fmt.Sprintf(string(format), args...)
}
// Routes returns registered route uri in a string slice
func (t *Tree) Routes() map[string][]string {
routes := make(map[string][]string)
for _, method := range RouterMethodName {
routes[method] = make([]string, 0)
}
for k := range t.nodes {
routes[RouterMethodName[k]] = t.routes(t.nodes[k])
}
return routes
}
// routes print the route table
func (t *Tree) routes(l *leaf) []string {
if l == nil {
return nil
}
var data []string
if l.handlers != nil {
data = append(data, l.String())
}
l.children = append(l.children, l.paramChild)
l.children = append(l.children, l.wideChild)
for i := range l.children {
if l.children[i] != nil {
cdata := t.routes(l.children[i])
for i := range cdata {
data = append(data, l.String()+cdata[i])
}
}
}
return data
}
// NamedRoutes returns named route uri in a string slice
func (t *Tree) NamedRoutes() map[string]string {
routes := make(map[string]string)
for k, v := range t.nameNodes {
routes[k] = v.pattern
}
return routes
}
// Add registers a new handle with the given method, pattern and handlers.
// add check training slash option.
func (t *Tree) Add(method, pattern string, handlers []HandlerFunc) RouteNode {
if method == "GET" && t.autoHead {
t.add("HEAD", pattern, handlers)
}
if t.autoTrailingSlash && (len(pattern) > 1 || len(t.groups) > 0) {
var index byte
if len(pattern) > 0 {
index = pattern[len(pattern)-1]
}
if index == '/' {
t.add(method, pattern[:len(pattern)-1], handlers)
} else if index == '*' {
// wideChild not need trail slash
} else {
t.add(method, pattern+"/", handlers)
}
}
return t.add(method, pattern, handlers)
}
// GroupAdd add a group route has same prefix and handle chain
func (t *Tree) GroupAdd(pattern string, f func(), handlers []HandlerFunc) {
g := newGroup()
g.pattern = pattern
g.handlers = handlers
t.groups = append(t.groups, g)
f()
t.groups = t.groups[:len(t.groups)-1]
}
// add registers a new request handle with the given method, pattern and handlers.
func (t *Tree) add(method, pattern string, handlers []HandlerFunc) RouteNode {
if _, ok := RouterMethods[method]; !ok {
panic("unsupport http method [" + method + "]")
}
t.mu.Lock()
defer t.mu.Unlock()
// check group set
if len(t.groups) > 0 {
var gpattern string
var ghandlers []HandlerFunc
for i := range t.groups {
gpattern += t.groups[i].pattern
if len(t.groups[i].handlers) > 0 {
ghandlers = append(ghandlers, t.groups[i].handlers...)
}
}
pattern = gpattern + pattern
ghandlers = append(ghandlers, handlers...)
handlers = ghandlers
}
// check pattern (for training slash move behind group check)
if pattern == "" {
panic("route pattern can not be emtpy!")
}
if pattern[0] != '/' {
panic("route pattern must begin /")
}
for i := 0; i < len(handlers); i++ {
handlers[i] = WrapHandlerFunc(handlers[i])
}
root := t.nodes[RouterMethods[method]]
origPattern := pattern
nameNode := NewNode(origPattern, t)
// specialy route = /
if len(pattern) == 1 {
root.handlers = handlers
root.nameNode = nameNode
return nameNode
}
// left trim slash, because root is slash /
pattern = pattern[1:]
var radix []byte
var param []byte
var i, k int
var tl *leaf
for i = 0; i < len(pattern); i++ {
// wide route
if pattern[i] == '*' {
// clear static route
if len(radix) > 0 {
root = root.insertChild(newLeaf(string(radix), nil, t))
radix = radix[:0]
}
tl = newLeaf("*", handlers, t)
tl.kind = leafKindWide
tl.nameNode = nameNode
root.insertChild(tl)
break
}
// param route
if pattern[i] == ':' {
// clear static route
if len(radix) > 0 {
root = root.insertChild(newLeaf(string(radix), nil, t))
radix = radix[:0]
}
// set param route
param = param[:0]
k = 0
for i = i + 1; i < len(pattern); i++ {
if pattern[i] == '/' {
i--
break
}
param = append(param, pattern[i])
k++
}
if k == 0 {
panic("route pattern param is empty")
}
// check last character
if i == len(pattern) {
tl = newLeaf(":", handlers, t)
tl.nameNode = nameNode
} else {
tl = newLeaf(":", nil, t)
}
tl.param = string(param[:k])
tl.kind = leafKindParam
root = root.insertChild(tl)
continue
}
radix = append(radix, pattern[i])
}
// static route
if len(radix) > 0 {
tl = newLeaf(string(radix), handlers, t)
tl.nameNode = nameNode
root.insertChild(tl)
}
return nameNode
}
// insertChild insert child into root route, and returns the child route
func (l *leaf) insertChild(node *leaf) *leaf {
// wide route
if node.kind == leafKindWide {
if l.wideChild != nil {
panic("Router Tree.insert error: cannot set two wide route with same prefix!")
}
l.wideChild = node
return node
}
// param route
if node.kind == leafKindParam {
if l.paramChild == nil {
l.paramChild = node
return l.paramChild
}
if l.paramChild.param != node.param {
panic("Router Tree.insert error cannot use two param [:" + l.paramChild.param + ", :" + node.param + "] with same prefix!")
}
if node.handlers != nil {
if l.paramChild.handlers != nil {
panic("Router Tree.insert error: cannot twice set handler for same route")
}
l.paramChild.handlers = node.handlers
l.paramChild.nameNode = node.nameNode
}
return l.paramChild
}
// static route
child := l.children[node.pattern[0]]
if child == nil {
// new child
l.children[node.pattern[0]] = node
l.childrenNum++
return node
}
pos := child.hasPrefixString(node.pattern)
pre := node.pattern[:pos]
if pos == len(child.pattern) {
// same route
if pos == len(node.pattern) {
if node.handlers != nil {
if child.handlers != nil {
panic("Router Tree.insert error: cannot twice set handler for same route")
}
child.handlers = node.handlers
child.nameNode = node.nameNode
}
return child
}
// child is prefix or node
node.pattern = node.pattern[pos:]
return child.insertChild(node)
}
newChild := newLeaf(child.pattern[pos:], child.handlers, child.root)
newChild.nameNode = child.nameNode
newChild.children = child.children
newChild.childrenNum = child.childrenNum
newChild.paramChild = child.paramChild
newChild.wideChild = child.wideChild
// node is prefix of child
if pos == len(node.pattern) {
child.reset(node.pattern, node.handlers)
child.nameNode = node.nameNode
child.children[newChild.pattern[0]] = newChild
child.childrenNum++
return child
}
// child and node has same prefix
child.reset(pre, nil)
child.children[newChild.pattern[0]] = newChild
child.childrenNum++
node.pattern = node.pattern[pos:]
child.children[node.pattern[0]] = node
child.childrenNum++
return node
}
// resetPattern reset route pattern and alpha
func (l *leaf) reset(pattern string, handlers []HandlerFunc) {
l.pattern = pattern
l.children = make([]*leaf, 128)
l.childrenNum = 0
l.paramChild = nil
l.wideChild = nil
l.nameNode = nil
l.param = ""
l.handlers = handlers
}
// hasPrefixString returns the same prefix position, if none return 0
func (l *leaf) hasPrefixString(s string) int {
var i, j int
j = len(l.pattern)
if len(s) < j {
j = len(s)
}
for i = 0; i < j && s[i] == l.pattern[i]; i++ {
}
return i
}
// String returns pattern of leaf
func (l *leaf) String() string {
s := l.pattern
if l.kind == leafKindParam {
s += l.param
}
return s
}
// Name set name of route
func (n *Node) Name(name string) {
if name == "" {
return
}
p := 0
f := make([]byte, 0, len(n.pattern))
for i := 0; i < len(n.pattern); i++ {
if n.pattern[i] != ':' {
f = append(f, n.pattern[i])
continue
}
f = append(f, '%')
f = append(f, 'v')
p++
for i = i + 1; i < len(n.pattern); i++ {
if n.pattern[i] == '/' {
i--
break
}
}
}
n.format = string(f)
n.paramNum = p
n.name = name
n.root.nameNodes[name] = n
}