-
Notifications
You must be signed in to change notification settings - Fork 7
/
node.go
492 lines (386 loc) · 9.78 KB
/
node.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
package radix
import (
"sort"
"strings"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)
func newNode(path string) *node {
return &node{
nType: static,
path: path,
}
}
// conflict raises a panic with some details
func (n *nodeWildcard) conflict(path, fullPath string) error {
prefix := fullPath[:strings.LastIndex(fullPath, path)] + n.path
return newRadixError(errWildcardConflict, path, fullPath, n.path, prefix)
}
// wildPathConflict raises a panic with some details
func (n *node) wildPathConflict(path, fullPath string) error {
pathSeg := strings.SplitN(path, "/", 2)[0]
prefix := fullPath[:strings.LastIndex(fullPath, path)] + n.path
return newRadixError(errWildPathConflict, pathSeg, fullPath, n.path, prefix)
}
// clone clones the current node in a new pointer
func (n node) clone() *node {
cloneNode := new(node)
cloneNode.nType = n.nType
cloneNode.path = n.path
cloneNode.tsr = n.tsr
cloneNode.handler = n.handler
if len(n.children) > 0 {
cloneNode.children = make([]*node, len(n.children))
for i, child := range n.children {
cloneNode.children[i] = child.clone()
}
}
if n.wildcard != nil {
cloneNode.wildcard = &nodeWildcard{
path: n.wildcard.path,
paramKey: n.wildcard.paramKey,
handler: n.wildcard.handler,
}
}
if len(n.paramKeys) > 0 {
cloneNode.paramKeys = make([]string, len(n.paramKeys))
copy(cloneNode.paramKeys, n.paramKeys)
}
cloneNode.paramRegex = n.paramRegex
return cloneNode
}
func (n *node) split(i int) {
cloneChild := n.clone()
cloneChild.nType = static
cloneChild.path = cloneChild.path[i:]
cloneChild.paramKeys = nil
cloneChild.paramRegex = nil
n.path = n.path[:i]
n.handler = nil
n.tsr = false
n.wildcard = nil
n.children = append(n.children[:0], cloneChild)
}
func (n *node) findEndIndexAndValues(path string) (int, []string) {
index := n.paramRegex.FindStringSubmatchIndex(path)
if len(index) == 0 {
return -1, nil
}
end := index[1]
index = index[2:]
values := make([]string, len(index)/2)
i := 0
for j := range index {
if (j+1)%2 != 0 {
continue
}
values[i] = copyString(path[index[j-1]:index[j]])
i++
}
return end, values
}
func (n *node) setHandler(handler fasthttp.RequestHandler, fullPath string) (*node, error) {
if n.handler != nil || n.tsr {
return n, newRadixError(errSetHandler, fullPath)
}
n.handler = handler
foundTSR := false
// Set TSR in method
for i := range n.children {
child := n.children[i]
if child.path != "/" {
continue
}
child.tsr = true
foundTSR = true
break
}
if n.path != "/" && !foundTSR {
childTSR := newNode("/")
childTSR.tsr = true
n.children = append(n.children, childTSR)
}
return n, nil
}
func (n *node) insert(path, fullPath string, handler fasthttp.RequestHandler) (*node, error) {
end := segmentEndIndex(path, true)
child := newNode(path)
wp := findWildPath(path, fullPath)
if wp != nil {
j := end
if wp.start > 0 {
j = wp.start
}
child.path = path[:j]
if wp.start > 0 {
n.children = append(n.children, child)
return child.insert(path[j:], fullPath, handler)
}
switch wp.pType {
case param:
n.hasWildChild = true
child.nType = wp.pType
child.paramKeys = wp.keys
child.paramRegex = wp.regex
case wildcard:
if len(path) == end && n.path[len(n.path)-1] != '/' {
return nil, newRadixError(errWildcardSlash, fullPath)
} else if len(path) != end {
return nil, newRadixError(errWildcardNotAtEnd, fullPath)
}
if n.path != "/" && n.path[len(n.path)-1] == '/' {
n.split(len(n.path) - 1)
n.tsr = true
n = n.children[0]
}
if n.wildcard != nil {
if n.wildcard.path == path {
return n, newRadixError(errSetWildcardHandler, fullPath)
}
return nil, n.wildcard.conflict(path, fullPath)
}
n.wildcard = &nodeWildcard{
path: wp.path,
paramKey: wp.keys[0],
handler: handler,
}
return n, nil
}
path = path[wp.end:]
if len(path) > 0 {
n.children = append(n.children, child)
return child.insert(path, fullPath, handler)
}
}
child.handler = handler
n.children = append(n.children, child)
if child.path == "/" {
// Add TSR when split a edge and the remain path to insert is "/"
n.tsr = true
} else if strings.HasSuffix(child.path, "/") {
child.split(len(child.path) - 1)
child.tsr = true
} else {
childTSR := newNode("/")
childTSR.tsr = true
child.children = append(child.children, childTSR)
}
return child, nil
}
// add adds the handler to node for the given path
func (n *node) add(path, fullPath string, handler fasthttp.RequestHandler) (*node, error) {
if len(path) == 0 {
return n.setHandler(handler, fullPath)
}
for _, child := range n.children {
i := longestCommonPrefix(path, child.path)
if i == 0 {
continue
}
switch child.nType {
case static:
if len(child.path) > i {
child.split(i)
}
if len(path) > i {
return child.add(path[i:], fullPath, handler)
}
case param:
wp := findWildPath(path, fullPath)
isParam := wp.start == 0 && wp.pType == param
hasHandler := child.handler != nil || handler == nil
if len(path) == wp.end && isParam && hasHandler {
// The current segment is a param and it's duplicated
if child.path == path {
return child, newRadixError(errSetHandler, fullPath)
}
return nil, child.wildPathConflict(path, fullPath)
}
if len(path) > i {
if child.path == wp.path {
return child.add(path[i:], fullPath, handler)
}
return n.insert(path, fullPath, handler)
}
}
if path == "/" {
n.tsr = true
}
return child.setHandler(handler, fullPath)
}
return n.insert(path, fullPath, handler)
}
func (n *node) getFromChild(path string, ctx *fasthttp.RequestCtx) (fasthttp.RequestHandler, bool) {
for _, child := range n.children {
switch child.nType {
case static:
// Checks if the first byte is equal
// It's faster than compare strings
if path[0] != child.path[0] {
continue
}
if len(path) > len(child.path) {
if path[:len(child.path)] != child.path {
continue
}
h, tsr := child.getFromChild(path[len(child.path):], ctx)
if h != nil || tsr {
return h, tsr
}
} else if path == child.path {
switch {
case child.tsr:
return nil, true
case child.handler != nil:
return child.handler, false
case child.wildcard != nil:
if ctx != nil {
ctx.SetUserValue(child.wildcard.paramKey, "")
}
return child.wildcard.handler, false
}
return nil, false
}
case param:
end := segmentEndIndex(path, false)
values := []string{copyString(path[:end])}
if child.paramRegex != nil {
end, values = child.findEndIndexAndValues(path[:end])
if end == -1 {
continue
}
}
if len(path) > end {
h, tsr := child.getFromChild(path[end:], ctx)
if tsr {
return nil, tsr
} else if h != nil {
if ctx != nil {
for i, key := range child.paramKeys {
ctx.SetUserValue(key, values[i])
}
}
return h, false
}
} else if len(path) == end {
switch {
case child.tsr:
return nil, true
case child.handler == nil:
// try another child
continue
case ctx != nil:
for i, key := range child.paramKeys {
ctx.SetUserValue(key, values[i])
}
}
return child.handler, false
}
default:
panic("invalid node type")
}
}
if n.wildcard != nil {
if ctx != nil {
ctx.SetUserValue(n.wildcard.paramKey, copyString(path))
}
return n.wildcard.handler, false
}
return nil, false
}
func (n *node) find(path string, buf *bytebufferpool.ByteBuffer) (bool, bool) {
if len(path) > len(n.path) {
if !strings.EqualFold(path[:len(n.path)], n.path) {
return false, false
}
path = path[len(n.path):]
_, _ = buf.WriteString(n.path)
found, tsr := n.findFromChild(path, buf)
if found {
return found, tsr
}
bufferRemoveString(buf, n.path)
} else if strings.EqualFold(path, n.path) {
_, _ = buf.WriteString(n.path)
if n.tsr {
if n.path == "/" {
bufferRemoveString(buf, n.path)
} else {
_ = buf.WriteByte('/')
}
return true, true
}
if n.handler != nil {
return true, false
} else {
bufferRemoveString(buf, n.path)
}
}
return false, false
}
func (n *node) findFromChild(path string, buf *bytebufferpool.ByteBuffer) (bool, bool) {
for _, child := range n.children {
switch child.nType {
case static:
found, tsr := child.find(path, buf)
if found {
return found, tsr
}
case param:
end := segmentEndIndex(path, false)
if child.paramRegex != nil {
end, _ = child.findEndIndexAndValues(path[:end])
if end == -1 {
continue
}
}
_, _ = buf.WriteString(path[:end])
if len(path) > end {
found, tsr := child.findFromChild(path[end:], buf)
if found {
return found, tsr
}
} else if len(path) == end {
if child.tsr {
_ = buf.WriteByte('/')
return true, true
}
if child.handler != nil {
return true, false
}
}
bufferRemoveString(buf, path[:end])
default:
panic("invalid node type")
}
}
if n.wildcard != nil {
_, _ = buf.WriteString(path)
return true, false
}
return false, false
}
// sort sorts the current node and their children
func (n *node) sort() {
for _, child := range n.children {
child.sort()
}
sort.Sort(n)
}
// Len returns the total number of children the node has
func (n *node) Len() int {
return len(n.children)
}
// Swap swaps the order of children nodes
func (n *node) Swap(i, j int) {
n.children[i], n.children[j] = n.children[j], n.children[i]
}
// Less checks if the node 'i' has less priority than the node 'j'
func (n *node) Less(i, j int) bool {
if n.children[i].nType < n.children[j].nType {
return true
} else if n.children[i].nType > n.children[j].nType {
return false
}
return len(n.children[i].children) > len(n.children[j].children)
}