-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.go
490 lines (450 loc) · 17.4 KB
/
route.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
487
488
489
490
package trout
import (
"math"
"net/http"
"strconv"
"strings"
"time"
)
const (
catchAllMethod = "*"
)
var (
default404Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 Page Not Found")) //nolint:errcheck
}))
default405Handler = http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Allow", strings.Join(r.Header[http.CanonicalHeaderKey("Trout-Methods")], ", "))
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("405 Method Not Allowed")) //nolint:errcheck
}))
)
// RequestVars returns easy-to-access mappings of parameters to values for URL
// templates. Any {parameter} in your URL template will be available in the
// returned Header as a slice of strings, one for each instance of the
// {parameter}. In the case of a parameter name being used more than once in
// the same URL template, the values will be in the slice in the order they
// appeared in the template.
//
// Values can easily be accessed by using the .Get() method of the returned
// Header, though to access multiple values, they must be accessed through the
// map. All parameters use http.CanonicalHeaderKey for their formatting. When
// using .Get(), the parameter name will be transformed automatically. When
// utilising the Header as a map, the parameter name needs to have
// http.CanonicalHeaderKey applied manually.
func RequestVars(r *http.Request) http.Header {
res := http.Header{}
for h, v := range r.Header {
stripped := strings.TrimPrefix(h, http.CanonicalHeaderKey("Trout-Param-"))
if stripped != h {
res[stripped] = v
}
}
return res
}
// Router defines a set of Endpoints that map requests to the http.Handlers.
// The http.Handler assigned to Handle404, if set, will be called when no
// Endpoint matches the current request. The http.Handler assigned to
// Handle405, if set, will be called when an Endpoint matches the current
// request, but has no http.Handler set for the HTTP method that the request
// used. Should either of these properties be unset, a default http.Handler
// will be used.
//
// The Router type is safe for use with empty values, but makes no attempt at
// concurrency-safety in adding Endpoints or in setting properties. It should
// also be noted that the adding Endpoints while simultaneously routing
// requests will lead to undefined and (almost certainly) undesirable
// behaviour. Routers are intended to be initialised with a set of Endpoints,
// and then start serving requests. Using them outside of this use case is
// unsupported.
type Router struct {
Handle404 http.Handler
Handle405 http.Handler
prefix string
trie *trie
middleware []func(http.Handler) http.Handler
}
// get404 returns the http.Handler `router` should use when serving a 404 page
func (router Router) get404() http.Handler {
h := default404Handler
if router.Handle404 != nil {
h = router.Handle404
}
return h
}
// get405 returns the http.Handler `router` should use when serving a 405 page
func (router Router) get405() http.Handler {
h := default405Handler
if router.Handle405 != nil {
h = router.Handle405
}
return h
}
// route represents an endpoint match from the router, which should be served,
// and all the data needed to serve it.
//
// A route may not necessarily support the method a request used, but if it
// does not, no endpoint that uses those methods was matched. The methods
// property should be checked, and a 405 returned if a method is unsupported.
type route struct {
// the http.Handler that needs to be served
handler http.Handler
// the pattern that was matched
pattern string
// the parsed parameters from the pattern
params map[string][]string
// the methods this endpoint can serve
methods []string
// middleware to use when serving the handler on this route
middleware []func(http.Handler) http.Handler
}
// route uses the pieces of the request URL and the method of the request to
// find a route that should be used to serve the request.
//
// routes are chosen based on a weighting; see `scoreNode` for more details on
// the algorithm. routes that can support the supplied method are always chosen
// over routes that cannot; if a route that cannot support the supplied method
// is returned, it is safe to assume no route can.
func (router Router) route(pieces []string, method string) *route {
result := &route{}
nodes := router.trie.findNodes(pieces)
if nodes == nil || len(nodes) < 1 {
return nil
}
node := pickNode(nodes, pieces, method)
if node == nil {
return nil
}
result.params = router.trie.vars(node, pieces)
result.pattern = strings.TrimSuffix(router.prefix, "/") + router.trie.pathString(node)
for method := range node.methods {
result.methods = append(result.methods, method)
}
var ok bool
result.handler, ok = node.methods[method]
result.middleware = node.middleware[method]
if !ok {
result.handler = node.methods[catchAllMethod]
result.middleware = node.middleware[catchAllMethod]
}
return result
}
// pickNode selects a node that has the highest score, according to
// `scoreNode`, to serve a request.
func pickNode(nodes []*node, pieces []string, method string) *node {
var maxScore float64
var bestNode *node
for _, node := range nodes {
if node == nil {
continue
}
// if this node has no terminator/methods associated with it,
// it can't be picked
if node.terminator == nil {
continue
}
score := scoreNode(node, pieces, 0)
// any path that can serve the specified method should score
// higher than paths that cannot
if _, ok := node.terminator.methods[method]; !ok {
score = score - math.Pow10(len(pieces)+1)
}
if bestNode == nil || score > maxScore {
maxScore = score
bestNode = node
}
}
if bestNode == nil {
return nil
}
return bestNode.terminator
}
// scoreNode assigns a raw score to how good a match a node is for a given set
// of pieces. A higher score is a better match.
//
// paths that have a 1:1 match between pieces and nodes should score higher
// - this should be taken care of by having more nodes to score
//
// nodes that are dynamic should score lower than static matches
// nodes that are prefixes should score lower than static matches
// nodes that are prefixes should score lower than nodes that are dynamic
// - this should be taken care of by having more nodes to score
//
// nodes earlier in the path should be worth more than nodes later in the path
func scoreNode(node *node, pieces []string, power int) float64 {
var score float64
if node.parent != nil {
parPower := power + 1
score = scoreNode(node.parent, pieces[:len(pieces)-1], parPower)
}
if node.value.nul {
return score
}
myScore := 1
if !node.value.dynamic && !node.value.prefix {
myScore++
}
score += math.Pow10(power) * float64(myScore)
return score
}
func (router Router) getHandler(r *http.Request) http.Handler {
// do our time tracking
start := time.Now()
defer func() {
r.Header.Set("Trout-Timer", strconv.FormatInt(time.Since(start).Nanoseconds(), 10))
}()
// if our router is nil, everything's a 404
if router.trie == nil {
return router.get404()
}
// break the request URL down into pieces
u := strings.TrimPrefix(r.URL.Path, router.prefix)
pieces := strings.Split(strings.Trim(u, "/"), "/")
// find the best match for our pieces and request method
route := router.route(pieces, r.Method)
// if we're nil, nothing was found, it's a 404
if route == nil {
return router.get404()
}
// if anything was found all, let's set our diagnostic headers
r.Header[http.CanonicalHeaderKey("Trout-Methods")] = route.methods
r.Header.Set("Trout-Pattern", route.pattern)
for key, vals := range route.params {
r.Header[http.CanonicalHeaderKey("Trout-Param-"+key)] = vals
for _, val := range vals {
setBuiltinRequestPathVar(r, key, val)
}
}
// if no handler is set, it could be because there's no handler for
// this endpoint, which we can safely assume is a 404
if route.handler == nil {
if len(route.methods) < 1 {
return router.get404()
}
// but it could also mean that there's an endpoint that just
// doesn't support the method we used, which is a 405
return router.get405()
}
// apply any middleware on the route
handler := route.handler
for i := len(route.middleware) - 1; i >= 0; i-- {
handler = route.middleware[i](handler)
}
// after all that, if we still haven't found a problem, use the handler
// we have
return handler
}
// ServeHTTP finds the best handler for the request, using the 404 or 405
// handlers if necessary, and serves the request.
func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler := router.getHandler(r)
for i := len(router.middleware) - 1; i >= 0; i-- {
handler = router.middleware[i](handler)
}
handler.ServeHTTP(w, r)
}
// SetPrefix sets a string prefix for the Router that won't be taken into
// account when matching Endpoints. This is usually set whenever the Router is
// not passed directly to http.ListenAndServe, and is sent through some sort of
// muxer first. It should be set to whatever string the muxer is using when
// passing requests to the Router.
//
// This function is not concurrency-safe; it should not be used while the
// Router is actively serving requests.
func (router *Router) SetPrefix(prefix string) {
router.prefix = prefix
}
// SetMiddleware sets one or more middleware functions that will wrap all
// handlers defined on the router. Middleware will run after routing, but
// before any route-specific middleware or the route handler.
//
// Middleware is applied in the order it appears in the SetMiddleware call. So,
// for example, if router.SetMiddleware(A, B, C) is called, trout will call
// A(B(C(handler))) for any handler defined on the router.
func (router *Router) SetMiddleware(mw ...func(http.Handler) http.Handler) {
router.middleware = mw
}
// Endpoint defines a single URL template that requests can be matched against.
// It is only valid to instantiate an Endpoint by calling `Router.Endpoint`.
// Endpoints, on their own, are only useful for calling their methods, as they
// don't do anything until an http.Handler is associated with them.
type Endpoint node
// Endpoint defines a new Endpoint on the Router. The Endpoint should be a URL
// template, using curly braces to denote parameters that should be filled at
// runtime. For example, `{id}` denotes a parameter named `id` that should be
// filled with whatever the request has in that space.
//
// Parameters are always `/`-separated strings. There is no support for regular
// expressions or other limitations on what may be in those strings. A
// parameter is simply defined as "whatever is between these two / characters".
//
// Endpoints are always case-insensitive and coerced to lowercase. Endpoints
// will only match requests with URLs that match the entire Endpoint and have
// no extra path elements.
func (router *Router) Endpoint(e string) *Endpoint {
if router.trie == nil {
router.trie = &trie{
root: &node{
children: map[string]*node{},
},
}
}
keys := keysFromString(e)
node := router.trie.add(keys, map[string]http.Handler{})
return (*Endpoint)(node)
}
// keysFromString parses `in` and returns the keys that represent it.
func keysFromString(in string) []key {
in = strings.Trim(in, "/")
pieces := strings.Split(in, "/")
keys := make([]key, 0, len(pieces))
for _, piece := range pieces {
k := key{
value: piece,
}
if strings.HasPrefix(piece, "{") && strings.HasSuffix(piece, "}") {
k.dynamic = true
k.value = piece[1 : len(piece)-1]
}
keys = append(keys, k)
}
return keys
}
// Handler sets the default http.Handler for `e`, to be used for all requests
// that `e` matches that don't match a method explicitly set for `e` using the
// Methods method.
//
// Handler is not concurrency-safe, and should not be used while the Router `e`
// belongs to is actively routing traffic.
func (e *Endpoint) Handler(h http.Handler) {
(*node)(e).methods[catchAllMethod] = h
}
// Middleware sets one or more middleware functions that will wrap the default
// http.Handler for `e`, to be used for all requests that `e` matches that
// don't match a method explicitly set for `e` using the Methods method.
// Middleware will run after routing, after any Router middleware, but before
// the route handler.
//
// Middleware is applied in the order it appears in the Middleware call. So,
// for example, if Endpoint.SetMiddleware(A, B, C) is called, trout will call
// A(B(C(handler))) when calling the Endpoint's handler.
func (e *Endpoint) Middleware(mw ...func(http.Handler) http.Handler) *Endpoint {
(*node)(e).middleware[catchAllMethod] = mw
return e
}
// Prefix defines a URL template that requests can be matched against. It is
// only valid to instantiate a prefix by calling `Router.Prefix`. Prefixes, on
// their own, are only useful for calling their methods, as they don't do
// anything until an http.Handler is associated with them.
//
// Unlike Endpoints, Prefixes will match any request that starts with their
// prefix, no matter whether or not the request is for a URL that is longer
// than the Prefix.
type Prefix node
// Prefix defines a new Prefix on the Router. The Prefix should be a URL
// template, using curly braces to denote parameters that should be filled at
// runtime. For example, `{id}` denotes a parameter named `id` that should be
// filled with whatever the request has in that space.
//
// Parameters are always `/`-separated strings. There is no support for regular
// expressions or other limitations on what may be in those strings. A
// parameter is simply defined as "whatever is between these two / characters".
//
// Prefixes are always case-insensitive and coerced to lowercase. Prefixes will
// only match requests with URLs that match the entire Prefix, but the URL may
// have additional path elements after the Prefix and still be considered a
// match.
func (router *Router) Prefix(p string) *Prefix {
if router.trie == nil {
router.trie = &trie{
root: &node{
children: map[string]*node{},
},
}
}
keys := keysFromString(p)
last := keys[len(keys)-1]
last.prefix = true
keys[len(keys)-1] = last
node := router.trie.add(keys, map[string]http.Handler{})
return (*Prefix)(node)
}
// Handler sets the default http.Handler for `p`, to be used for all requests
// that `p` matches that don't match a method explicitly set for `p` using the
// Methods method.
//
// Handler is not concurrency-safe, and should not be used while the Router `p`
// belongs to is actively routing traffic.
func (p *Prefix) Handler(h http.Handler) {
(*node)(p).methods[catchAllMethod] = h
}
// Middleware sets one or more middleware functions that will wrap the default
// http.Handler for `p`, to be used for all requests that `p` matches that
// don't match a method explicitly set for `e` using the Methods method.
// Middleware will run after routing, after any Router middleware, but before
// the route handler.
//
// Middleware is applied in the order it appears in the Middleware call. So,
// for example, if Prefix.SetMiddleware(A, B, C) is called, trout will call
// A(B(C(handler))) when calling the Endpoint's handler.
func (p *Prefix) Middleware(mw ...func(http.Handler) http.Handler) *Prefix {
(*node)(p).middleware[catchAllMethod] = mw
return p
}
// Methods defines a pairing of an Endpoint to HTTP request methods, to map
// designate specific http.Handlers for requests matching that Endpoint made
// using the specified methods. It is only valid to instantiate Methods by
// calling `Endpoint.Methods`. Methods, on their own, are only useful for
// calling the `Methods.Handler` method, as they don't modify the Router until
// their `Methods.Handler` method is called.
type Methods struct {
n *node
m []string
}
// Methods returns a Methods object that will enable the mapping of the passed
// HTTP request methods to the Endpoint. On its own, this function does not
// modify anything. It should, instead, be used as a friendly shorthand to get
// to the Methods.Handler method.
func (e *Endpoint) Methods(m ...string) Methods {
return Methods{
n: (*node)(e),
m: m,
}
}
// Methods returns a Methods object that will enable the mapping of the passed
// HTTP request methods to the Prefix. On its own, this function does not
// modify anything. It should, instead, be used as a friendly shorthand to get
// to the Methods.Handler method.
func (p *Prefix) Methods(m ...string) Methods {
return Methods{
n: (*node)(p),
m: m,
}
}
// Handler associates an http.Handler with the Endpoint associated with `m`, to
// be used whenever a request that matches the Endpoint also matches one of the
// Methods associated with `m`.
//
// Handler is not concurrency-safe. It should not be called while the Router
// that owns the Endpoint that `m` belongs to is actively serving traffic.
func (m Methods) Handler(h http.Handler) {
for _, method := range m.m {
m.n.methods[method] = h
}
}
// Middleware sets one or more middleware functions that will wrap the
// http.Handler associated with `m`, to be used whenever a request that matches
// the Endpoint also matches one of the Methods associated with m. Middleware
// will run after routing, after any Router middleware, but before the route
// handler.
//
// Middleware is applied in the order it appears in the Middleware call. So,
// for example, if Methods.SetMiddleware(A, B, C) is called, trout will call
// A(B(C(handler))) when calling the Methods' handler.
func (m Methods) Middleware(mw ...func(http.Handler) http.Handler) Methods {
for _, method := range m.m {
m.n.middleware[method] = mw
}
return m
}