This repository was archived by the owner on Jun 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.go
More file actions
364 lines (340 loc) · 8.06 KB
/
tree.go
File metadata and controls
364 lines (340 loc) · 8.06 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
package egret
import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
)
const (
pathNodeTypeStatic = iota
pathNodeTypeRegex
pathNodeTypeRoot
)
type (
pathNodeType int
pathNode struct {
kind pathNodeType
path string
regex *regexp.Regexp
pnames []string
constraint ConstraintFunc
strictSlash bool
handlers map[string][]HandlerFunc
children []*pathNode
}
pathTree struct {
*pathNode
}
)
func newPathTree() *pathTree {
return &pathTree{
&pathNode{
kind: pathNodeTypeRoot,
handlers: make(map[string][]HandlerFunc, 0),
children: make([]*pathNode, 0),
},
}
}
func newPathNode() *pathNode {
return &pathNode{
handlers: make(map[string][]HandlerFunc, 0),
children: make([]*pathNode, 0),
}
}
func (n *pathNode) setConstraint(constraint ConstraintFunc) *pathNode {
n.constraint = constraint
return n
}
func (n *pathNode) setStrictSlash(strictSlash bool) *pathNode {
n.strictSlash = strictSlash
return n
}
func normalizePath(path string) string {
npath := ""
for i, pn := 0, len(path); i < pn; i++ {
ch := path[i]
if ch == '<' {
pname, pattern, pnameParsed := "", "", false
for i++; i < pn; i++ {
ch = path[i]
if ch == '>' {
break
} else if ch == ':' {
pnameParsed = true
continue
}
if pnameParsed {
pattern += string(ch)
} else {
pname += string(ch)
}
}
if pattern == "" || pattern == ".*" || pattern == "[^/]*" || pattern == "[^/]+" {
pattern = ".+"
}
if pattern == "" {
npath += "<" + pname + ">"
} else {
npath += "<" + pname + ":" + pattern + ">"
}
} else {
npath += string(ch)
}
}
if npath[0] != '/' {
return "/" + npath
}
return npath
}
func (t *pathTree) add(path string) (*pathNode, bool) {
path = normalizePath(path)
for _, child := range t.children {
node, ok := child.add(path)
if ok {
return node, ok
}
}
return t.pathNode.addChild(path), true
}
func (n *pathNode) add(path string) (*pathNode, bool) {
matched := 0
//find the common prefix
for ; matched < len(path) && matched < len(n.path); matched++ {
if path[matched] != n.path[matched] {
break
}
}
if matched == len(n.path) {
if matched == len(path) {
// the pathNode key is the same as the key: make the current pathNode as data pathNode
// if the pathNode is already a data pathNode, ignore the new data since we only care the first matched pathNode
return n, true
}
// the pathNode key is a prefix of the path: create a child pathNode
newPath := path[matched:]
for _, child := range n.children {
n, ok := child.add(newPath)
if ok {
return n, true
}
}
//this new path can not be added to child pathNode, need to create new child pathNode in current pathNode
return n.addChild(newPath), true
}
// no common prefix, or partial common prefix with a non-static pathNode: should skip this pathNode
if matched == 0 || n.regex != nil {
return nil, false
}
// the pathNode key shares a partial prefix with the key: split the pathNode key
n1 := &pathNode{
kind: pathNodeTypeStatic,
path: n.path[matched:],
regex: n.regex,
handlers: n.handlers,
children: n.children,
}
n.path = path[0:matched]
n.handlers = make(map[string][]HandlerFunc, 0)
n.regex = nil //n must be static pathNode, regex is already nil, no need to set it again.
n.children = []*pathNode{n1}
return n.add(path)
}
func (n *pathNode) addChild(path string) *pathNode {
// find the first occurrence of a param token
p0, p1, level := -1, -1, 0
for i := 0; i < len(path); i++ {
if p0 < 0 && path[i] == '<' {
p0 = i
level++
}
if p0 >= 0 && path[i] == '>' {
p1 = i
level--
if level == 0 {
break
}
}
}
if p1 < 0 {
//no param token: create a static pathNode
child := &pathNode{
kind: pathNodeTypeStatic,
path: path,
handlers: make(map[string][]HandlerFunc, 0),
children: make([]*pathNode, 0),
}
n.children = append(n.children, child)
return child
}
if p0 > 0 && p1 > 0 {
// param token occurs after a static string
child := &pathNode{
kind: pathNodeTypeStatic,
path: path[0:p0],
handlers: make(map[string][]HandlerFunc, 0),
children: make([]*pathNode, 0),
}
n.children = append(n.children, child)
n = child
path = path[p0:]
p1 -= p0
p0 = 0
}
child := &pathNode{
kind: pathNodeTypeRegex,
path: path[p0 : p1+1],
pnames: make([]string, 1),
handlers: make(map[string][]HandlerFunc, 0),
children: make([]*pathNode, 0),
}
n.children = append(n.children, child)
pnames, pattern, end := getPnames(path, 0)
child.pnames = pnames
if pattern != "" && pattern != "(.+)" {
child.regex = regexp.MustCompile("^" + pattern)
}
if end == len(path)-1 {
return child
}
return child.addChild(path[end+1:])
}
func (n *pathNode) get(method string, url *url.URL) ([]HandlerFunc, map[string]string) {
params := make(map[string]string, 0)
handlers := n.innerGet(method, url.Path, url.Path, url.RawQuery, params)
return handlers, params
}
func (n *pathNode) innerGet(method, fullPath, reset, rawQuery string, params map[string]string) []HandlerFunc {
if n.kind == pathNodeTypeRoot { //only set root pathNode's path to nil{
if len(n.children) > 0 {
for _, child := range n.children {
handlers := child.innerGet(method, fullPath, reset, rawQuery, params)
if handlers != nil {
return handlers
}
}
}
return nil
}
if n.kind == pathNodeTypeStatic {
npl, rtl := len(n.path), len(reset)
if npl > rtl {
if npl == rtl+1 {
if n.path == reset+"/" {
reset = ""
} else {
return nil
}
} else {
return nil
}
} else {
if reset[0:npl] == n.path {
reset = reset[npl:]
} else {
return nil
}
}
} else if n.kind == pathNodeTypeRegex {
if n.regex != nil {
ln := strings.IndexByte(reset, '/')
if ln == -1 {
ln = len(reset)
}
subp := reset[0:ln]
results := n.regex.FindStringSubmatch(subp)
if len(results) == len(n.pnames)+1 {
for i, il := 0, len(n.pnames); i < il; i++ {
params[n.pnames[i]] = results[i+1]
}
} else {
return nil
}
reset = reset[len(results[0]):]
} else {
if n.pnames[0][0] == '*' {
params[n.pnames[0]] = reset[0:len(reset)]
return n.handlers[method]
}
ln := strings.IndexByte(reset, '/')
if ln == -1 {
ln = len(reset)
}
params[n.pnames[0]] = reset[0:ln]
reset = reset[ln:]
}
}
if reset != "" {
if len(n.children) > 0 {
for _, child := range n.children {
handlers := child.innerGet(method, fullPath, reset, rawQuery, params)
if handlers != nil {
return handlers
}
}
return nil
} else if reset == "/" {
if n.strictSlash {
rurl := fullPath[0 : len(fullPath)-1]
if rawQuery != "" {
rurl += "?" + rawQuery
}
return []HandlerFunc{
func(ctx *Context) {
ctx.Redirect(rurl, http.StatusMovedPermanently)
},
}
}
return n.handlers[method]
}
return nil
}
return n.handlers[method]
}
func (n *pathNode) print(level int) string {
r := fmt.Sprintf("%v{kind: %v, path: %v, regex: %v, handlers: %v, children: %v, pnames: %v}\n", strings.Repeat(" ", level<<2), n.kind, n.path, n.regex, len(n.handlers), len(n.children), n.pnames)
for _, child := range n.children {
if child != nil {
r += child.print(level + 1)
}
}
return r
}
var regchs = "{}[]()^$.|*+?\\"
func getPnames(path string, startIndex int) (pnames []string, parttern string, endIndex int) {
pnames, pattern := []string{}, ""
pn := len(path)
for i := startIndex; i < pn; i++ {
ch := path[i]
if ch == '/' {
return pnames, pattern, i - 1
} else if ch == '<' {
pname, pnameParsed := "", false
for i++; i < pn; i++ {
ch := path[i]
if ch == '>' {
pnames = append(pnames, pname)
pattern += ")"
break
}
if ch == ':' {
pnameParsed = true
pattern += "("
continue
}
if pnameParsed {
pattern += string(ch)
} else {
pname += string(ch)
}
}
} else {
if strings.Contains(regchs, string(ch)) {
pattern += "\\" + string(ch)
} else {
pattern += string(ch)
}
}
}
return pnames, pattern, pn - 1
}