forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.go
457 lines (381 loc) · 9.68 KB
/
branch.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
// Copyright (c) 2013 Julien Schmidt, Copyright (c) 2016 Gerasimos Maropoulos,
package iris
import (
"bytes"
"strings"
"github.com/kataras/iris/utils"
)
const (
isStatic BranchCase = iota
isRoot
hasParams
matchEverything
)
type (
// PathParameter is a struct which contains Key and Value, used for named path parameters
PathParameter struct {
Key string
Value string
}
// PathParameters type for a slice of PathParameter
// Tt's a slice of PathParameter type, because it's faster than map
PathParameters []PathParameter
// BranchCase is the type which the type of Branch using in order to determinate what type (parameterized, anything, static...) is the perticular node
BranchCase uint8
// IBranch is the interface which the type Branch must implement
IBranch interface {
AddBranch(string, Middleware)
AddNode(uint8, string, string, Middleware)
GetBranch(string, PathParameters) (Middleware, PathParameters, bool)
GivePrecedenceTo(index int) int
}
// Branch is the node of a tree of the routes,
// in order to learn how this is working, google 'trie' or watch this lecture: https://www.youtube.com/watch?v=uhAUk63tLRM
// this method is used by the BSD's kernel also
Branch struct {
part string
BranchCase BranchCase
hasWildNode bool
tokens string
nodes []*Branch
middleware Middleware
precedence uint64
paramsLen uint8
}
)
var _ IBranch = &Branch{}
// Get returns a value from a key inside this Parameters
// If no parameter with this key given then it returns an empty string
func (params PathParameters) Get(key string) string {
for _, p := range params {
if p.Key == key {
return p.Value
}
}
return ""
}
// String returns a string implementation of all parameters that this PathParameters object keeps
// hasthe form of key1=value1,key2=value2...
func (params PathParameters) String() string {
var buff bytes.Buffer
for i := range params {
buff.WriteString(params[i].Key)
buff.WriteString("=")
buff.WriteString(params[i].Value)
if i < len(params)-1 {
buff.WriteString(",")
}
}
return buff.String()
}
// ParseParams receives a string and returns PathParameters (slice of PathParameter)
// received string must have this form: key1=value1,key2=value2...
func ParseParams(str string) PathParameters {
_paramsstr := strings.Split(str, ",")
if len(_paramsstr) == 0 {
return nil
}
params := make(PathParameters, 0) // PathParameters{}
// for i := 0; i < len(_paramsstr); i++ {
for i := range _paramsstr {
idxOfEq := strings.IndexRune(_paramsstr[i], '=')
if idxOfEq == -1 {
//error
return nil
}
key := _paramsstr[i][:idxOfEq]
val := _paramsstr[i][idxOfEq+1:]
params = append(params, PathParameter{key, val})
}
return params
}
// GetParamsLen returns the parameters length from a given path
func GetParamsLen(path string) uint8 {
var n uint
for i := 0; i < len(path); i++ {
if path[i] != ':' && path[i] != '*' { // ParameterStartByte & MatchEverythingByte
continue
}
n++
}
if n >= 255 {
return 255
}
return uint8(n)
}
// AddBranch adds a branch to the existing branch or to the tree if no branch has the prefix of
func (b *Branch) AddBranch(path string, middleware Middleware) {
fullPath := path
b.precedence++
numParams := GetParamsLen(path)
if len(b.part) > 0 || len(b.nodes) > 0 {
loop:
for {
if numParams > b.paramsLen {
b.paramsLen = numParams
}
i := 0
max := utils.FindLower(len(path), len(b.part))
for i < max && path[i] == b.part[i] {
i++
}
if i < len(b.part) {
node := Branch{
part: b.part[i:],
hasWildNode: b.hasWildNode,
tokens: b.tokens,
nodes: b.nodes,
middleware: b.middleware,
precedence: b.precedence - 1,
}
for i := range node.nodes {
if node.nodes[i].paramsLen > node.paramsLen {
node.paramsLen = node.nodes[i].paramsLen
}
}
b.nodes = []*Branch{&node}
b.tokens = string([]byte{b.part[i]})
b.part = path[:i]
b.middleware = nil
b.hasWildNode = false
}
if i < len(path) {
path = path[i:]
if b.hasWildNode {
b = b.nodes[0]
b.precedence++
if numParams > b.paramsLen {
b.paramsLen = numParams
}
numParams--
if len(path) >= len(b.part) && b.part == path[:len(b.part)] {
if len(b.part) >= len(path) || path[len(b.part)] == '/' {
continue loop
}
}
return
}
c := path[0]
if b.BranchCase == hasParams && c == '/' && len(b.nodes) == 1 {
b = b.nodes[0]
b.precedence++
continue loop
}
//we need the i here to be re-setting, so use the same i variable as we declare it on line 176
for i := range b.tokens {
if c == b.tokens[i] {
i = b.GivePrecedenceTo(i)
b = b.nodes[i]
continue loop
}
}
if c != ParameterStartByte && c != MatchEverythingByte {
b.tokens += string([]byte{c})
node := &Branch{
paramsLen: numParams,
}
b.nodes = append(b.nodes, node)
b.GivePrecedenceTo(len(b.tokens) - 1)
b = node
}
b.AddNode(numParams, path, fullPath, middleware)
return
} else if i == len(path) {
if b.middleware != nil {
return
}
b.middleware = middleware
}
return
}
} else {
b.AddNode(numParams, path, fullPath, middleware)
b.BranchCase = isRoot
}
}
// AddNode adds a branch as children to other Branch
func (b *Branch) AddNode(numParams uint8, path string, fullPath string, middleware Middleware) {
var offset int
for i, max := 0, len(path); numParams > 0; i++ {
c := path[i]
if c != ParameterStartByte && c != MatchEverythingByte {
continue
}
end := i + 1
for end < max && path[end] != '/' {
switch path[end] {
case ParameterStartByte, MatchEverythingByte:
default:
end++
}
}
if len(b.nodes) > 0 {
return
}
if end-i < 2 {
return
}
if c == ParameterStartByte {
if i > 0 {
b.part = path[offset:i]
offset = i
}
child := &Branch{
BranchCase: hasParams,
paramsLen: numParams,
}
b.nodes = []*Branch{child}
b.hasWildNode = true
b = child
b.precedence++
numParams--
if end < max {
b.part = path[offset:end]
offset = end
child := &Branch{
paramsLen: numParams,
precedence: 1,
}
b.nodes = []*Branch{child}
b = child
}
} else {
if end != max || numParams > 1 {
return
}
if len(b.part) > 0 && b.part[len(b.part)-1] == '/' {
return
}
i--
if path[i] != '/' {
return
}
b.part = path[offset:i]
child := &Branch{
hasWildNode: true,
BranchCase: matchEverything,
paramsLen: 1,
}
b.nodes = []*Branch{child}
b.tokens = string(path[i])
b = child
b.precedence++
child = &Branch{
part: path[i:],
BranchCase: matchEverything,
paramsLen: 1,
middleware: middleware,
precedence: 1,
}
b.nodes = []*Branch{child}
return
}
}
b.part = path[offset:]
b.middleware = middleware
}
// GetBranch is used by the Router, it finds and returns the correct branch for a path
func (b *Branch) GetBranch(path string, _params PathParameters) (middleware Middleware, params PathParameters, mustRedirect bool) {
params = _params
loop:
for {
if len(path) > len(b.part) {
if path[:len(b.part)] == b.part {
path = path[len(b.part):]
if !b.hasWildNode {
c := path[0]
for i := range b.tokens {
if c == b.tokens[i] {
b = b.nodes[i]
continue loop
}
}
mustRedirect = (path == Slash && b.middleware != nil)
return
}
b = b.nodes[0]
switch b.BranchCase {
case hasParams:
end := 0
for end < len(path) && path[end] != '/' {
end++
}
if cap(params) < int(b.paramsLen) {
params = make(PathParameters, 0, b.paramsLen)
}
i := len(params)
params = params[:i+1]
params[i].Key = b.part[1:]
params[i].Value = path[:end]
if end < len(path) {
if len(b.nodes) > 0 {
path = path[end:]
b = b.nodes[0]
continue loop
}
mustRedirect = (len(path) == end+1)
return
}
if middleware = b.middleware; middleware != nil {
return
} else if len(b.nodes) == 1 {
b = b.nodes[0]
mustRedirect = (b.part == Slash && b.middleware != nil)
}
return
case matchEverything:
if cap(params) < int(b.paramsLen) {
params = make(PathParameters, 0, b.paramsLen)
}
i := len(params)
params = params[:i+1]
params[i].Key = b.part[2:]
params[i].Value = path
middleware = b.middleware
return
default:
return
}
}
} else if path == b.part {
if middleware = b.middleware; middleware != nil {
return
}
if path == Slash && b.hasWildNode && b.BranchCase != isRoot {
mustRedirect = true
return
}
for i := range b.tokens {
if b.tokens[i] == '/' {
b = b.nodes[i]
mustRedirect = (len(b.part) == 1 && b.middleware != nil) ||
(b.BranchCase == matchEverything && b.nodes[0].middleware != nil)
return
}
}
return
}
mustRedirect = (path == Slash) ||
(len(b.part) == len(path)+1 && b.part[len(path)] == '/' &&
path == b.part[:len(b.part)-1] && b.middleware != nil)
return
}
}
// GivePrecedenceTo just adds the priority of this branch by an index
func (b *Branch) GivePrecedenceTo(index int) int {
b.nodes[index].precedence++
_precedence := b.nodes[index].precedence
newindex := index
for newindex > 0 && b.nodes[newindex-1].precedence < _precedence {
tmpN := b.nodes[newindex-1]
b.nodes[newindex-1] = b.nodes[newindex]
b.nodes[newindex] = tmpN
newindex--
}
if newindex != index {
b.tokens = b.tokens[:newindex] +
b.tokens[index:index+1] +
b.tokens[newindex:index] + b.tokens[index+1:]
}
return newindex
}