forked from husobee/vestigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
486 lines (418 loc) · 11.7 KB
/
router.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
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
// Portions Copyright 2015 Husobee Associates, LLC. All rights reserved.
// Use of this source code is governed by The MIT License, which
// can be found in the LICENSE file included.
// Portions Copyright 2015 Labstack. All rights reserved.
package vestigo
import (
"net/http"
"strings"
)
const (
stype ntype = iota
ptype
mtype
)
type (
ntype uint8
children []*node
)
// middleware takes in HandlerFunc (which can be another middleware or handler)
// and wraps it within another one
type Middleware func(http.HandlerFunc) http.HandlerFunc
// Router - The main vestigo router data structure
type Router struct {
root *node
globalCors *CorsAccessControl
}
// NewRouter - Create a new vestigo router
func NewRouter() *Router {
return &Router{
root: &node{
resource: newResource(),
},
}
}
// GetMatchedPathTemplate - get the path template from the url in the request
func (r *Router) GetMatchedPathTemplate(req *http.Request) string {
p, _ := r.find(req)
return p
}
// SetGlobalCors - Settings for Global Cors Options. This takes a *CorsAccessControl
// policy, and will apply said policy to every resource. If this is not set on the
// router, CORS functionality is turned off.
func (r *Router) SetGlobalCors(c *CorsAccessControl) {
r.globalCors = c
}
// SetCors - Set per resource Cors Policy. The CorsAccessControl policy passed in
// will map to the policy that is validated against the "path" resource. This policy
// will be merged with the global policy, and values will be deduplicated if there are
// overlaps.
func (r *Router) SetCors(path string, c *CorsAccessControl) {
r.addWithCors("CORS", path, nil, c)
}
// ServeHTTP - implementation of a http.Handler, making Router a http.Handler
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h := r.Find(req)
h(w, req)
}
// Get - Helper method to add HTTP GET Method to router
func (r *Router) Get(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodGet, path, handler, middleware...)
}
// Post - Helper method to add HTTP POST Method to router
func (r *Router) Post(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodPost, path, handler, middleware...)
}
// Connect - Helper method to add HTTP CONNECT Method to router
func (r *Router) Connect(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodConnect, path, handler, middleware...)
}
// Delete - Helper method to add HTTP DELETE Method to router
func (r *Router) Delete(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodDelete, path, handler, middleware...)
}
// Patch - Helper method to add HTTP PATCH Method to router
func (r *Router) Patch(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodPatch, path, handler, middleware...)
}
// Put - Helper method to add HTTP PUT Method to router
func (r *Router) Put(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodPut, path, handler, middleware...)
}
// Trace - Helper method to add HTTP TRACE Method to router
func (r *Router) Trace(path string, handler http.HandlerFunc, middleware ...Middleware) {
r.Add(http.MethodTrace, path, handler, middleware...)
}
// Handle - Helper method to add all HTTP Methods to router
func (r *Router) Handle(path string, handler http.Handler, middleware ...Middleware) {
for k := range methods {
if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace {
continue
}
r.Add(k, path, handler.ServeHTTP, middleware...)
}
}
// HandleFunc - Helper method to add all HTTP Methods to router
func (r *Router) HandleFunc(path string, handler http.HandlerFunc, middleware ...Middleware) {
for k := range methods {
if k == http.MethodHead || k == http.MethodOptions || k == http.MethodTrace {
continue
}
r.Add(k, path, handler.ServeHTTP, middleware...)
}
}
// Add - Add a method/handler combination to the router
func (r *Router) addWithCors(method, path string, h http.HandlerFunc, cors *CorsAccessControl) {
r.add(method, path, h, cors)
}
// Add - Add a method/handler combination to the router
func (r *Router) Add(method, path string, h http.HandlerFunc, middleware ...Middleware) {
r.add(method, path, h, nil, middleware...)
}
// Add - Add a method/handler combination to the router
func (r *Router) add(method, path string, h http.HandlerFunc, cors *CorsAccessControl, middleware ...Middleware) {
h = buildChain(h, middleware...)
pnames := make(pNames)
pnames[method] = []string{}
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' {
j := i + 1
r.insert(method, path[:i], nil, stype, nil, cors)
for ; i < l && path[i] != '/'; i++ {
}
pnames[method] = append(pnames[method], path[j:i])
path = path[:j] + path[i:]
i, l = j, len(path)
if i == l {
r.insert(method, path[:i], h, ptype, pnames, cors)
return
}
r.insert(method, path[:i], nil, ptype, pnames, cors)
} else if path[i] == '*' {
r.insert(method, path[:i], nil, stype, nil, cors)
pnames[method] = append(pnames[method], "_name")
r.insert(method, path[:i+1], h, mtype, pnames, cors)
return
}
}
r.insert(method, path, h, stype, pnames, cors)
}
// Find - Find A route within the router tree
func (r *Router) Find(req *http.Request) (h http.HandlerFunc) {
_, h = r.find(req)
return
}
func (r *Router) find(req *http.Request) (prefix string, h http.HandlerFunc) {
// get tree base node from the router
cn := r.root
h = notFoundHandler
if !validMethod(req.Method) {
// if the method is completely invalid
h = methodNotAllowedHandler(cn.resource.allowedMethods)
return
}
var (
search = req.URL.Path
c *node // Child node
n int // Param counter
collectedPnames = []string{}
)
// Search order static > param > match-any
for {
if search == "" {
if cn.resource != nil {
// Found route, check if method is applicable
theHandler, allowedMethods := cn.resource.GetMethodHandler(req.Method)
if theHandler == nil {
if uint16(req.Method[0])<<8|uint16(req.Method[1]) == 0x4f50 {
h = optionsHandler(r.globalCors, cn.resource.Cors, allowedMethods)
return
}
if allowedMethods != "" {
// route is valid, but method is not allowed, 405
h = methodNotAllowedHandler(allowedMethods)
}
return
}
h = corsFlightWrapper(r.globalCors, cn.resource.Cors, allowedMethods, theHandler)
for i, v := range collectedPnames {
if len(cn.pnames[req.Method]) > i {
AddParam(req, cn.pnames[req.Method][i], v)
}
}
brokenPrefix := strings.Split(prefix, "/")
prefix = ""
k := 0
for _, v := range brokenPrefix {
if v != "" {
prefix += "/"
if v == ":" {
if pnames, ok := cn.pnames[req.Method]; ok {
prefix += v + pnames[k]
}
k++
} else {
prefix += v
}
}
}
}
return
}
pl := 0 // Prefix length
l := 0 // LCP length
if cn.label != ':' {
sl := len(search)
pl = len(cn.prefix)
prefix += cn.prefix
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
}
if l == pl {
// Continue search
search = search[l:]
if search == "" && cn != nil && cn.parent != nil && cn.resource.allowedMethods == "" {
parent := cn.parent
search = cn.prefix
for parent != nil {
if sib := parent.findChildWithLabel('*'); sib != nil {
search = parent.prefix + search
cn = parent
goto MatchAny
}
parent = parent.parent
}
}
}
if search == "" {
// TODO: Needs improvement
if cn.findChildWithType(mtype) == nil {
continue
}
// Empty value
goto MatchAny
}
// Static node
c = cn.findChild(search, stype)
if c != nil {
cn = c
continue
}
// Param node
Param:
c = cn.findChildWithType(ptype)
if c != nil {
cn = c
i, l := 0, len(search)
for ; i < l && search[i] != '/'; i++ {
}
collectedPnames = append(collectedPnames, search[0:i])
prefix += ":"
n++
search = search[i:]
if len(cn.children) == 0 && len(search) != 0 {
return
}
continue
}
// Match-any node
MatchAny:
// c = cn.getChild()
c = cn.findChildWithType(mtype)
if c != nil {
cn = c
collectedPnames = append(collectedPnames, search)
search = "" // End search
continue
}
// last ditch effort to match on wildcard (issue #8)
var tmpsearch = search
for {
if cn != nil && cn.parent != nil && cn.prefix != ":" {
tmpsearch = cn.prefix + tmpsearch
cn = cn.parent
if cn.prefix == "/" {
var sib *node = cn.findChildWithLabel(':')
if sib != nil {
search = tmpsearch
goto Param
}
if sib := cn.findChildWithLabel('*'); sib != nil {
search = tmpsearch
goto MatchAny
}
}
} else {
break
}
}
// Not found
return
}
}
// insert - insert a route into the router tree
func (r *Router) insert(method, path string, h http.HandlerFunc, t ntype, pnames pNames, cors *CorsAccessControl) {
// Adjust max param
cn := r.root
if !validMethod(method) && method != "CORS" {
panic("invalid method")
}
search := path
for {
sl := len(search)
pl := len(cn.prefix)
l := 0
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
if cn.pnames == nil {
cn.pnames = make(pNames)
}
if l == 0 {
// At root node
cn.label = search[0]
cn.prefix = search
if h != nil {
cn.typ = t
cn.resource = newResource()
cn.resource.Cors = cn.resource.Cors.Merge(cors)
if method != "CORS" {
cn.resource.AddMethodHandler(method, h)
}
if method == "GET" {
cn.pnames["HEAD"] = pnames[method]
}
cn.pnames[method] = pnames[method]
}
} else if l < pl {
// Split node
nr := newResource()
cn.resource.CopyTo(nr)
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, nr, cn.pnames)
for i := 0; i < len(n.children); i++ {
n.children[i].parent = n
}
// Reset parent node
cn.typ = stype
cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l]
cn.children = nil
cn.resource = newResource()
cn.pnames = make(pNames)
cn.addChild(n)
if l == sl {
// At parent node
cn.typ = t
cn.resource.Cors = cn.resource.Cors.Merge(cors)
if method != "CORS" {
cn.resource.AddMethodHandler(method, h)
}
if method == "GET" {
cn.pnames["HEAD"] = pnames[method]
}
cn.pnames[method] = pnames[method]
} else {
// Create child node
nr := newResource()
nr.Cors = nr.Cors.Merge(cors)
if method != "CORS" {
nr.AddMethodHandler(method, h)
}
cn.pnames[method] = pnames[method]
n = newNode(t, search[l:], cn, nil, nr, cn.pnames)
cn.addChild(n)
}
} else if l < sl {
search = search[l:]
c := cn.findChildWithLabel(search[0])
if c != nil {
// Go deeper
cn = c
continue
}
// Create child node
nr := newResource()
if method != "CORS" {
nr.AddMethodHandler(method, h)
}
nr.Cors = nr.Cors.Merge(cors)
n := newNode(t, search, cn, nil, nr, pnames)
cn.addChild(n)
cn.resource.Clean()
n.resource.Clean()
} else {
if cors != nil {
cn.resource.Cors = cn.resource.Cors.Merge(cors)
}
// Node already exists
if h != nil {
// add the handler to the node's map of methods to handlers
if method != "CORS" {
cn.resource.AddMethodHandler(method, h)
}
if method == "GET" {
cn.pnames["HEAD"] = pnames[method]
}
cn.pnames[method] = pnames[method]
}
}
return
}
}
func buildChain(f http.HandlerFunc, m ...Middleware) http.HandlerFunc {
// if our chain is done, use the original handlerfunc
if len(m) == 0 {
return f
}
// otherwise nest the handlerfuncs
return m[0](buildChain(f, m[1:cap(m)]...))
}