-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex_tree.go
More file actions
697 lines (594 loc) · 15.1 KB
/
regex_tree.go
File metadata and controls
697 lines (594 loc) · 15.1 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// Package gin 提供基于 Gin 的增强上下文与相关组件。
package gin
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"sync"
)
// methodTyp 表示 HTTP 方法在正则路由树中的位标记。
type methodTyp uint
const (
mSTUB methodTyp = 1 << iota
mCONNECT
mDELETE
mGET
mHEAD
mOPTIONS
mPATCH
mPOST
mPUT
mTRACE
)
var (
mALL = mCONNECT | mDELETE | mGET | mHEAD |
mOPTIONS | mPATCH | mPOST | mPUT | mTRACE
methodMu = sync.RWMutex{}
methodMap = map[string]methodTyp{
"CONNECT": mCONNECT,
"DELETE": mDELETE,
"GET": mGET,
"HEAD": mHEAD,
"OPTIONS": mOPTIONS,
"PATCH": mPATCH,
"POST": mPOST,
"PUT": mPUT,
"TRACE": mTRACE,
}
reverseMethodMap = map[methodTyp]string{
mCONNECT: "CONNECT",
mDELETE: "DELETE",
mGET: "GET",
mHEAD: "HEAD",
mOPTIONS: "OPTIONS",
mPATCH: "PATCH",
mPOST: "POST",
mPUT: "PUT",
mTRACE: "TRACE",
}
)
// registerMethod 为正则路由树注册自定义 HTTP 方法。
func registerMethod(method string) methodTyp {
method = strings.ToUpper(strings.TrimSpace(method))
if method == "" {
return 0
}
methodMu.Lock()
defer methodMu.Unlock()
if mt, ok := methodMap[method]; ok {
return mt
}
n := len(methodMap)
if n > strconv.IntSize-2 {
panic(fmt.Sprintf("gin: max number of methods reached (%d)", strconv.IntSize))
}
mt := methodTyp(2 << n)
methodMap[method] = mt
reverseMethodMap[mt] = method
mALL |= mt
return mt
}
// lookupMethod 返回方法对应的位标记。
func lookupMethod(method string) (methodTyp, bool) {
methodMu.RLock()
defer methodMu.RUnlock()
mt, ok := methodMap[strings.ToUpper(strings.TrimSpace(method))]
return mt, ok
}
// nodeTyp 表示路由树节点类型。
type nodeTyp uint8
const (
ntStatic nodeTyp = iota // 静态段: /home
ntRegexp // 正则段: /{id:[0-9]+}
ntParam // 参数段: /{user}
ntCatchAll // 通配段: /*
)
// node 是 chi 风格正则路由树节点。
type node struct {
rex *regexp.Regexp
endpoints endpoints
prefix string
children [ntCatchAll + 1]nodes
tail byte
typ nodeTyp
label byte
}
// endpoints 表示一个叶子节点上可用的方法处理器集合。
type endpoints map[methodTyp]*endpoint
// endpoint 表示最终落点处理器与参数元信息。
type endpoint struct {
handler HandlerFunc
pattern string
paramKeys []string
}
func (s endpoints) value(method methodTyp) *endpoint {
mh, ok := s[method]
if !ok {
mh = &endpoint{}
s[method] = mh
}
return mh
}
// routeParams 记录正则路由匹配出的参数。
type routeParams struct {
Keys []string
Values []string
}
// routeContext 是正则路由匹配期间的临时上下文。
type routeContext struct {
URLParams routeParams
routeParams routeParams
routePattern string
methodsAllowed []methodTyp
methodNotAllowed bool
}
// Reset 重置临时路由上下文,复用切片容量。
func (x *routeContext) Reset() {
x.URLParams.Keys = x.URLParams.Keys[:0]
x.URLParams.Values = x.URLParams.Values[:0]
x.routeParams.Keys = x.routeParams.Keys[:0]
x.routeParams.Values = x.routeParams.Values[:0]
x.routePattern = ""
x.methodsAllowed = x.methodsAllowed[:0]
x.methodNotAllowed = false
}
// InsertRoute 将一条 chi 风格 pattern 插入路由树。
func (n *node) InsertRoute(method methodTyp, pattern string, handler HandlerFunc) *node {
var parent *node
search := pattern
for {
if len(search) == 0 {
n.setEndpoint(method, handler, pattern)
return n
}
label := search[0]
var segTail byte
var segEndIdx int
var segTyp nodeTyp
var segRexpat string
if label == '{' || label == '*' {
segTyp, _, segRexpat, segTail, _, segEndIdx = patNextSegment(search)
}
prefix := ""
if segTyp == ntRegexp {
prefix = segRexpat
}
parent = n
n = n.getEdge(segTyp, label, segTail, prefix)
if n == nil {
child := &node{label: label, tail: segTail, prefix: search}
hn := parent.addChild(child, search)
hn.setEndpoint(method, handler, pattern)
return hn
}
if n.typ > ntStatic {
search = search[segEndIdx:]
continue
}
commonPrefix := longestPrefix(search, n.prefix)
if commonPrefix == len(n.prefix) {
search = search[commonPrefix:]
continue
}
child := &node{
typ: ntStatic,
prefix: search[:commonPrefix],
}
parent.replaceChild(search[0], segTail, child)
n.label = n.prefix[commonPrefix]
n.prefix = n.prefix[commonPrefix:]
child.addChild(n, n.prefix)
search = search[commonPrefix:]
if len(search) == 0 {
child.setEndpoint(method, handler, pattern)
return child
}
subchild := &node{
typ: ntStatic,
label: search[0],
prefix: search,
}
hn := child.addChild(subchild, search)
hn.setEndpoint(method, handler, pattern)
return hn
}
}
func (n *node) addChild(child *node, prefix string) *node {
search := prefix
hn := child
segTyp, _, segRexpat, segTail, segStartIdx, segEndIdx := patNextSegment(search)
switch segTyp {
case ntStatic:
// noop
default:
if segTyp == ntRegexp {
rex, err := regexp.Compile(segRexpat)
if err != nil {
panic(fmt.Sprintf("gin: invalid regexp pattern '%s' in route param", segRexpat))
}
child.prefix = segRexpat
child.rex = rex
}
if segStartIdx == 0 {
child.typ = segTyp
if segTyp == ntCatchAll {
segStartIdx = -1
} else {
segStartIdx = segEndIdx
}
if segStartIdx < 0 {
segStartIdx = len(search)
}
child.tail = segTail
if segStartIdx != len(search) {
search = search[segStartIdx:]
nn := &node{
typ: ntStatic,
label: search[0],
prefix: search,
}
hn = child.addChild(nn, search)
}
} else if segStartIdx > 0 {
child.typ = ntStatic
child.prefix = search[:segStartIdx]
child.rex = nil
search = search[segStartIdx:]
nn := &node{
typ: segTyp,
label: search[0],
tail: segTail,
}
hn = child.addChild(nn, search)
}
}
n.children[child.typ] = append(n.children[child.typ], child)
n.children[child.typ].Sort()
return hn
}
func (n *node) replaceChild(label, tail byte, child *node) {
for i := 0; i < len(n.children[child.typ]); i++ {
if n.children[child.typ][i].label == label && n.children[child.typ][i].tail == tail {
n.children[child.typ][i] = child
n.children[child.typ][i].label = label
n.children[child.typ][i].tail = tail
return
}
}
panic("gin: replacing missing child")
}
func (n *node) getEdge(ntyp nodeTyp, label, tail byte, prefix string) *node {
nds := n.children[ntyp]
for i := range nds {
if nds[i].label == label && nds[i].tail == tail {
if ntyp == ntRegexp && nds[i].prefix != prefix {
continue
}
return nds[i]
}
}
return nil
}
func (n *node) setEndpoint(method methodTyp, handler HandlerFunc, pattern string) {
if n.endpoints == nil {
n.endpoints = make(endpoints)
}
paramKeys := patParamKeys(pattern)
if method&mSTUB == mSTUB {
n.endpoints.value(mSTUB).handler = handler
}
if method&mALL == mALL {
h := n.endpoints.value(mALL)
h.handler = handler
h.pattern = pattern
h.paramKeys = paramKeys
methodMu.RLock()
defer methodMu.RUnlock()
for _, m := range methodMap {
h := n.endpoints.value(m)
h.handler = handler
h.pattern = pattern
h.paramKeys = paramKeys
}
return
}
h := n.endpoints.value(method)
h.handler = handler
h.pattern = pattern
h.paramKeys = paramKeys
}
// FindRoute 在路由树中查找请求方法与路径对应的处理器。
func (n *node) FindRoute(rctx *routeContext, method methodTyp, path string) (*node, endpoints, HandlerFunc) {
rctx.routePattern = ""
rctx.routeParams.Keys = rctx.routeParams.Keys[:0]
rctx.routeParams.Values = rctx.routeParams.Values[:0]
rn := n.findRoute(rctx, method, path)
if rn == nil {
return nil, nil, nil
}
rctx.URLParams.Keys = append(rctx.URLParams.Keys, rctx.routeParams.Keys...)
rctx.URLParams.Values = append(rctx.URLParams.Values, rctx.routeParams.Values...)
if h := pickEndpoint(rn.endpoints, method); h != nil && h.pattern != "" {
rctx.routePattern = h.pattern
return rn, rn.endpoints, h.handler
}
return rn, rn.endpoints, nil
}
func (n *node) findRoute(rctx *routeContext, method methodTyp, path string) *node {
nn := n
search := path
for t, nds := range nn.children {
ntyp := nodeTyp(t)
if len(nds) == 0 {
continue
}
var xn *node
xsearch := search
var label byte
if search != "" {
label = search[0]
}
switch ntyp {
case ntStatic:
xn = nds.findEdge(label)
if xn == nil || !strings.HasPrefix(xsearch, xn.prefix) {
continue
}
xsearch = xsearch[len(xn.prefix):]
case ntParam, ntRegexp:
if xsearch == "" {
if ntyp == ntRegexp {
for _, xn = range nds {
if xn.rex == nil || xn.hasChildren() || !xn.rex.MatchString("") {
continue
}
prevKeysLen := len(rctx.routeParams.Keys)
prevValsLen := len(rctx.routeParams.Values)
rctx.routeParams.Values = append(rctx.routeParams.Values, "")
if xn.isLeaf() {
if h := pickEndpoint(xn.endpoints, method); h != nil && h.handler != nil {
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
return xn
}
for endpointMethod := range xn.endpoints {
if endpointMethod == mALL || endpointMethod == mSTUB {
continue
}
rctx.methodsAllowed = append(rctx.methodsAllowed, endpointMethod)
}
rctx.methodNotAllowed = true
}
rctx.routeParams.Keys = rctx.routeParams.Keys[:prevKeysLen]
rctx.routeParams.Values = rctx.routeParams.Values[:prevValsLen]
}
}
continue
}
for _, xn = range nds {
p := strings.IndexByte(xsearch, xn.tail)
if p < 0 {
if xn.tail == '/' {
p = len(xsearch)
} else {
continue
}
} else if ntyp == ntRegexp && p == 0 {
continue
}
candidate := xsearch[:p]
if ntyp == ntRegexp && xn.rex != nil {
if xn.tail == '/' && !xn.hasChildren() && xn.rex.MatchString(xsearch) {
p = len(xsearch)
candidate = xsearch
}
if !xn.rex.MatchString(candidate) {
continue
}
} else if strings.IndexByte(candidate, '/') != -1 {
continue
}
prevlen := len(rctx.routeParams.Values)
rctx.routeParams.Values = append(rctx.routeParams.Values, candidate)
xsearch = xsearch[p:]
if len(xsearch) == 0 {
if xn.isLeaf() {
if h := pickEndpoint(xn.endpoints, method); h != nil && h.handler != nil {
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
return xn
}
for endpointMethod := range xn.endpoints {
if endpointMethod == mALL || endpointMethod == mSTUB {
continue
}
rctx.methodsAllowed = append(rctx.methodsAllowed, endpointMethod)
}
rctx.methodNotAllowed = true
}
}
fin := xn.findRoute(rctx, method, xsearch)
if fin != nil {
return fin
}
rctx.routeParams.Values = rctx.routeParams.Values[:prevlen]
xsearch = search
}
rctx.routeParams.Values = append(rctx.routeParams.Values, "")
default:
catchAllValue := search
if catchAllValue == "" {
catchAllValue = "/"
} else if catchAllValue[0] != '/' {
catchAllValue = "/" + catchAllValue
}
rctx.routeParams.Values = append(rctx.routeParams.Values, catchAllValue)
xn = nds[0]
xsearch = ""
}
if xn == nil {
continue
}
if len(xsearch) == 0 {
if xn.isLeaf() {
if h := pickEndpoint(xn.endpoints, method); h != nil && h.handler != nil {
rctx.routeParams.Keys = append(rctx.routeParams.Keys, h.paramKeys...)
return xn
}
for endpointMethod := range xn.endpoints {
if endpointMethod == mALL || endpointMethod == mSTUB {
continue
}
rctx.methodsAllowed = append(rctx.methodsAllowed, endpointMethod)
}
rctx.methodNotAllowed = true
}
}
fin := xn.findRoute(rctx, method, xsearch)
if fin != nil {
return fin
}
if xn.typ > ntStatic && len(rctx.routeParams.Values) > 0 {
rctx.routeParams.Values = rctx.routeParams.Values[:len(rctx.routeParams.Values)-1]
}
}
return nil
}
func pickEndpoint(eps endpoints, method methodTyp) *endpoint {
if eps == nil {
return nil
}
if h := eps[method]; h != nil && h.handler != nil {
return h
}
if h := eps[mALL]; h != nil && h.handler != nil {
return h
}
return nil
}
func (n *node) isLeaf() bool {
return n.endpoints != nil
}
func (n *node) hasChildren() bool {
for _, children := range n.children {
if len(children) > 0 {
return true
}
}
return false
}
// patNextSegment 返回下一个 pattern 段的信息。
func patNextSegment(pattern string) (nodeTyp, string, string, byte, int, int) {
ps := strings.Index(pattern, "{")
ws := strings.Index(pattern, "*")
if ps < 0 && ws < 0 {
return ntStatic, "", "", 0, 0, len(pattern)
}
if ps >= 0 && ws >= 0 && ws < ps {
panic("gin: wildcard '*' must be the last pattern in a route, otherwise use a '{param}'")
}
tail := byte('/')
if ps >= 0 {
nt := ntParam
cc := 0
pe := ps
for i, c := range pattern[ps:] {
if c == '{' {
cc++
} else if c == '}' {
cc--
if cc == 0 {
pe = ps + i
break
}
}
}
if pe == ps {
panic("gin: route param closing delimiter '}' is missing")
}
key := pattern[ps+1 : pe]
pe++
if pe < len(pattern) {
tail = pattern[pe]
}
key, rexpat, isRegexp := strings.Cut(key, ":")
if isRegexp {
nt = ntRegexp
}
if len(rexpat) > 0 {
if rexpat[0] != '^' {
rexpat = "^" + rexpat
}
if rexpat[len(rexpat)-1] != '$' {
rexpat += "$"
}
}
return nt, key, rexpat, tail, ps, pe
}
if ws < len(pattern)-1 {
key := pattern[ws+1:]
if strings.IndexByte(key, '/') >= 0 {
panic("gin: wildcard '*' must be the last value in a route. trim trailing text or use a '{param}' instead")
}
return ntCatchAll, key, "", 0, ws, len(pattern)
}
return ntCatchAll, "*", "", 0, ws, len(pattern)
}
// patParamKeys 提取 pattern 中所有参数名,保持顺序。
func patParamKeys(pattern string) []string {
pat := pattern
paramKeys := []string{}
for {
ptyp, paramKey, _, _, _, e := patNextSegment(pat)
if ptyp == ntStatic {
return paramKeys
}
for i := 0; i < len(paramKeys); i++ {
if paramKeys[i] == paramKey {
panic(fmt.Sprintf("gin: routing pattern '%s' contains duplicate param key, '%s'", pattern, paramKey))
}
}
paramKeys = append(paramKeys, paramKey)
pat = pat[e:]
}
}
func longestPrefix(k1, k2 string) (i int) {
for i = 0; i < min(len(k1), len(k2)); i++ {
if k1[i] != k2[i] {
break
}
}
return
}
type nodes []*node
func (ns nodes) Sort() { sort.Sort(ns); ns.tailSort() }
func (ns nodes) Len() int { return len(ns) }
func (ns nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }
func (ns nodes) Less(i, j int) bool { return ns[i].label < ns[j].label }
func (ns nodes) tailSort() {
for i := len(ns) - 1; i >= 0; i-- {
if ns[i].typ > ntStatic && ns[i].tail == '/' {
ns.Swap(i, len(ns)-1)
return
}
}
}
func (ns nodes) findEdge(label byte) *node {
num := len(ns)
idx := 0
i, j := 0, num-1
for i <= j {
idx = i + (j-i)/2
if label > ns[idx].label {
i = idx + 1
} else if label < ns[idx].label {
j = idx - 1
} else {
i = num
}
}
if num == 0 || ns[idx].label != label {
return nil
}
return ns[idx]
}