-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_storage.go
168 lines (136 loc) · 3.78 KB
/
route_storage.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
package chain
import (
"sort"
"strings"
)
type RouteStorage struct {
routes map[int][]*Route // by num of segments
}
func (s *RouteStorage) add(route *Route) {
details := route.Info
numSegments := len(details.segments)
if s.routes == nil {
s.routes = map[int][]*Route{}
}
if s.routes[numSegments] == nil {
s.routes[numSegments] = []*Route{}
// inserts wildcards from lower levels into this list
for oNumSegments, routes := range s.routes {
if oNumSegments < numSegments {
for _, other := range routes {
if other.Info.hasWildcard {
s.routes[numSegments] = append(s.routes[numSegments], other)
}
}
}
}
}
s.routes[numSegments] = append(s.routes[numSegments], route)
sort.Slice(s.routes[numSegments], func(i, j int) bool {
// high priority at the beginning'
return s.routes[numSegments][i].Info.priority > s.routes[numSegments][j].Info.priority
})
if details.hasWildcard {
// inserts this new path in the upper segments and does the reordering
for oNumSegments, _ := range s.routes {
if oNumSegments > numSegments {
s.routes[oNumSegments] = append(s.routes[oNumSegments], route)
sort.Slice(s.routes[oNumSegments], func(i, j int) bool {
return s.routes[oNumSegments][i].Info.priority > s.routes[oNumSegments][j].Info.priority
})
}
}
}
}
func (s *RouteStorage) lookup(ctx *Context) *Route {
if s.routes == nil {
return nil
}
var (
path = ctx.path
segments = ctx.pathSegments
segmentsCount = ctx.pathSegmentsCount
)
for i := segmentsCount; i > 0; i-- {
routes := s.routes[i]
if routes == nil {
continue
}
nextRoute:
for _, route := range routes {
details := route.Info
if !details.hasWildcard && i < segmentsCount {
// at this point it's just looking for the wildcard that satisfies this route
continue
}
// same effect as ` !details.FastMatch(ctx)`, but faster
for j, segment := range details.segments {
if strings.IndexByte(segment, wildcard) == 0 {
break
}
if strings.IndexByte(segment, parameter) == 0 && ctx.path[ctx.pathSegments[j]+1:ctx.pathSegments[j+1]] != "" {
continue
}
if segment != ctx.path[ctx.pathSegments[j]+1:ctx.pathSegments[j+1]] {
continue nextRoute
}
}
// found, populate parameters
if details.hasWildcard {
for j, index := range details.paramsIndex {
if j == len(details.paramsIndex)-1 {
ctx.addParameter(details.params[j], path[segments[index]:])
break
}
ctx.addParameter(details.params[j], path[segments[index]+1:segments[index+1]])
}
} else {
for j, index := range details.paramsIndex {
ctx.addParameter(details.params[j], path[segments[index]+1:segments[index+1]])
}
}
return route
}
// it only does the search in a single height
break
}
return nil
}
func (s *RouteStorage) lookupCaseInsensitive(ctx *Context) *Route {
if s.routes == nil {
return nil
}
var (
segmentsCount = ctx.pathSegmentsCount
)
for i := segmentsCount; i > 0; i-- {
routes := s.routes[i]
if routes == nil {
continue
}
nextRoute:
for _, route := range routes {
details := route.Info
if !details.hasWildcard && i < segmentsCount {
// at this point it's just looking for the wildcard that satisfies this route
continue
}
// same effect as ` !details.FastMatch(ctx)`, but faster
for j, segment := range details.segments {
if strings.IndexByte(segment, wildcard) == 0 {
break
}
if strings.IndexByte(segment, parameter) == 0 && ctx.path[ctx.pathSegments[j]+1:ctx.pathSegments[j+1]] != "" {
continue
}
if !strings.EqualFold(segment, ctx.path[ctx.pathSegments[j]+1:ctx.pathSegments[j+1]]) {
continue nextRoute
}
}
return route
}
// it only does the search in a single height
break
}
return nil
}