-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
311 lines (251 loc) · 6.37 KB
/
tree.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
package kid
import (
"bytes"
"errors"
"fmt"
"strings"
)
// Errors.
var (
errNotFound = errors.New("match not found")
errMethodNotAllowed = errors.New("method is not allowed")
)
// Path parameters prefix and suffix.
const (
paramPrefix = "{"
paramSuffix = "}"
starParamPrefix = paramPrefix + "*"
)
type (
// handlerMiddleware zips a handler and its middlewares to each other.
handlerMiddleware struct {
// handler is the route request handler.
handler HandlerFunc
// middlewares is route middlewares.
middlewares []MiddlewareFunc
// name is route name.
name string
}
// Tree is a tree used for routing.
Tree struct {
// size is the number of nodes in the tree.
size uint32
// root of the tree.
root *Node
}
// Node is a tree node.
Node struct {
// id of each node. It separates nodes from each other.
id uint32
// label of the node.
label string
children []*Node
isParam bool
isStar bool
// handlerMap maps HTTP methods to their handlers.
handlerMap map[string]handlerMiddleware
}
// Params is the type of path parameters.
Params map[string]string
)
// newNode returns a new node.
func newNode() Node {
return Node{
children: make([]*Node, 0),
handlerMap: make(map[string]handlerMiddleware),
}
}
// newTree returns a new Tree.
func newTree() Tree {
node := newNode()
node.id = 1
return Tree{
size: 1,
root: &node,
}
}
// insert inserts a new node into the tree.
func (t *Tree) insertNode(path string, methods []string, middlewares []MiddlewareFunc, handler HandlerFunc) {
if len(methods) == 0 {
panic("providing at least one method is required")
}
panicIfNil(handler, "handler cannot be nil")
path = cleanPath(path, false)
segments := strings.Split(path, "/")[1:]
currNode := t.root
for i, segment := range segments {
node := newNode()
node.isParam = isParam(segment)
node.isStar = isStar(segment)
node.setLabel(segment)
node.id = t.size + 1
t.size++
if i != len(segments)-1 {
if node.isStar {
panic("star path parameters can only be the last part of a path")
}
if child := currNode.getChild(node.label, node.isParam, node.isStar); child == nil {
currNode.addChild(&node)
currNode = &node
} else {
currNode = child
}
} else { // Only for the last iteration of the for loop.
hm := handlerMiddleware{handler: handler, middlewares: middlewares, name: path}
if child := currNode.getChild(node.label, node.isParam, node.isStar); child == nil {
node.addHanlder(methods, hm)
currNode.addChild(&node)
} else {
child.addHanlder(methods, hm)
}
}
}
}
// doesMatch deterines if the path matches the node's label.
func (n Node) doesMatch(path []string, pos int) bool {
if n.isStar {
return true
}
if pos >= len(path) {
return false
}
// Param matching.
if n.isParam {
return path[pos] != ""
}
// Exact matching.
return path[pos] == n.label
}
// searchFinished returns true if the search has to be finished.
func (n Node) searchFinished(path []string, pos int) bool {
if pos+1 == len(path) && len(n.handlerMap) > 0 {
return true
}
return n.isStar
}
// getPathParam returns the path parameter.
func (n *Node) getPathParam(path []string, pos int) string {
if n.isStar {
return strings.Join(path[pos:], "/")
}
return path[pos]
}
// getChild returns the specified child of the node.
func (n Node) getChild(label string, isParam, isStar bool) *Node {
for i := 0; i < len(n.children); i++ {
if n.children[i].label == label && n.children[i].isParam == isParam && n.children[i].isStar == isStar {
return n.children[i]
}
}
return nil
}
// addChild adds the given node to the node's children.
func (n *Node) addChild(node *Node) {
n.children = append(n.children, node)
}
// addHanlders add handlers to their methods.
func (n *Node) addHanlder(methods []string, hm handlerMiddleware) {
for _, v := range methods {
if _, ok := n.handlerMap[v]; ok {
panic(fmt.Sprintf("handler is already registered for method %s and node %+v.", v, n))
}
n.handlerMap[v] = hm
}
}
// setLabel sets the node's appropriate label.
func (n *Node) setLabel(label string) {
n.label = label
if n.isParam {
if n.isStar {
n.label = label[2 : len(label)-1]
} else {
n.label = label[1 : len(label)-1]
}
}
}
// isParam determines if a label is a parameter.
func isParam(label string) bool {
if strings.HasPrefix(label, paramPrefix) && strings.HasSuffix(label, paramSuffix) {
return true
}
return false
}
// isStar checks if a parameter is a star parameter.
func isStar(label string) bool {
if isParam(label) && label[1] == '*' {
return true
}
return false
}
// searchDFS searches the tree with the DFS search algorithm.
func (t Tree) searchDFS(path []string) (map[string]handlerMiddleware, Params, bool) {
stack := []*Node{t.root}
visitedMap := map[uint32]bool{}
params := make(Params)
var pos int
// Search while stack is not empty.
SearchLoop:
for len(stack) != 0 {
// Accessing last element.
node := stack[len(stack)-1]
if !visitedMap[node.id] {
visitedMap[node.id] = true
if node.isParam {
params[node.label] = node.getPathParam(path, pos)
}
if node.searchFinished(path, pos) {
return node.handlerMap, params, true
}
}
for _, child := range node.children {
if !visitedMap[child.id] && child.doesMatch(path, pos+1) {
// Push child to the stack.
stack = append(stack, child)
pos++
continue SearchLoop
}
}
if node.isParam {
delete(params, node.label)
}
// Pop from stack.
stack = stack[:len(stack)-1]
pos--
}
return nil, params, false
}
// search searches the Tree and tries to match the path to a handler if possible.
func (t Tree) search(path, method string) (handlerMiddleware, Params, error) {
segments := strings.Split(path, "/")
hmMap, params, found := t.searchDFS(segments)
if !found {
return handlerMiddleware{}, params, errNotFound
}
if hm, ok := hmMap[method]; ok {
return hm, params, nil
}
return handlerMiddleware{}, params, errMethodNotAllowed
}
// cleanPath normalizes the path.
//
// If soft is false it also removes duplicate slashes.
func cleanPath(s string, soft bool) string {
if s == "" {
return "/"
}
if s[0] != '/' {
s = "/" + s
}
if soft {
return s
}
// Removing repeated slashes.
var buff bytes.Buffer
for i := 0; i < len(s); i++ {
if i != 0 && s[i] == '/' && s[i-1] == '/' {
continue
}
buff.WriteByte(s[i])
}
return buff.String()
}