This repository has been archived by the owner on Dec 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
router.go
462 lines (394 loc) · 12.9 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
// Copyright 2013 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package clevergo
import (
"fmt"
"net/http"
"net/url"
"strings"
"sync"
)
// Handle is a function which handle incoming request and manage outgoing response.
type Handle func(ctx *Context) error
// HandleHandler converts http.Handler to Handle.
func HandleHandler(handler http.Handler) Handle {
return func(ctx *Context) error {
handler.ServeHTTP(ctx.Response, ctx.Request)
return nil
}
}
// HandleHandlerFunc converts http.HandlerFunc to Handle.
func HandleHandlerFunc(f http.HandlerFunc) Handle {
return func(ctx *Context) error {
f(ctx.Response, ctx.Request)
return nil
}
}
// Router is a http.Handler which can be used to dispatch requests to different
// handler functions via configurable routes
type Router struct {
trees map[string]*node
// Named routes.
routes map[string]*Route
contextPool sync.Pool
paramsPool sync.Pool
maxParams uint16
// Enables automatic redirection if the current route can't be matched but a
// handler for the path with (without) the trailing slash exists.
// For example if /foo/ is requested but a route only exists for /foo, the
// client is redirected to /foo with http status code 301 for Get requests
// and 308 for all other request methods.
RedirectTrailingSlash bool
// If enabled, the router tries to fix the current request path, if no
// handle is registered for it.
// First superfluous path elements like ../ or // are removed.
// Afterwards the router does a case-insensitive lookup of the cleaned path.
// If a handle can be found for this route, the router makes a redirection
// to the corrected path with status code 301 for Get requests and 308 for
// all other request methods.
// For example /FOO and /..//Foo could be redirected to /foo.
// RedirectTrailingSlash is independent of this option.
RedirectFixedPath bool
// If enabled, the router checks if another method is allowed for the
// current route, if the current request can not be routed.
// If this is the case, the request is answered with 'Method Not Allowed'
// and HTTP status code 405.
// If no other Method is allowed, the request is delegated to the NotFound
// handler.
HandleMethodNotAllowed bool
// If enabled, the router automatically replies to OPTIONS requests.
// Custom OPTIONS handlers take priority over automatic replies.
HandleOPTIONS bool
// An optional http.Handler that is called on automatic OPTIONS requests.
// The handler is only called if HandleOPTIONS is true and no OPTIONS
// handler for the specific path was set.
// The "Allowed" header is set before calling the handler.
GlobalOPTIONS http.Handler
// Cached value of global (*) allowed methods
globalAllowed string
// Configurable http.Handler which is called when no matching route is
// found. If it is not set, http.NotFound is used.
NotFound http.Handler
// Configurable http.Handler which is called when a request
// cannot be routed and HandleMethodNotAllowed is true.
// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
// The "Allow" header with allowed request methods is set before the handler
// is called.
MethodNotAllowed http.Handler
// Error Handler.
ErrorHandler ErrorHandler
// If enabled, use the request.URL.RawPath instead of request.URL.Path.
UseRawPath bool
middlewares []MiddlewareFunc
handle Handle
}
// Make sure the Router conforms with the http.Handler interface
var _ http.Handler = NewRouter()
// NewRouter returns a new initialized Router.
// Path auto-correction, including trailing slashes, is enabled by default.
func NewRouter() *Router {
return &Router{
RedirectTrailingSlash: true,
RedirectFixedPath: true,
HandleMethodNotAllowed: true,
HandleOPTIONS: true,
}
}
func (r *Router) getContext() *Context {
ctx := r.contextPool.Get().(*Context)
ctx.reset()
return ctx
}
func (r *Router) putContext(ctx *Context) {
r.contextPool.Put(ctx)
}
func (r *Router) getParams() *Params {
ps := r.paramsPool.Get().(*Params)
*ps = (*ps)[0:0] // reset slice
return ps
}
func (r *Router) putParams(ps *Params) {
r.paramsPool.Put(ps)
}
// URL creates an url with the given route name and arguments.
func (r *Router) URL(name string, args ...string) (*url.URL, error) {
if route, ok := r.routes[name]; ok {
return route.URL(args...)
}
return nil, fmt.Errorf("route %q does not exist", name)
}
// Group implements IRouter.Group.
func (r *Router) Group(path string, opts ...RouteGroupOption) IRouter {
return newRouteGroup(r, path, opts...)
}
// Use attaches global middlewares.
func (r *Router) Use(middlewares ...MiddlewareFunc) {
r.middlewares = append(r.middlewares, middlewares...)
r.handle = Chain(r.handleRequest, r.middlewares...)
}
// Get implements IRouter.Get.
func (r *Router) Get(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodGet, path, handle, opts...)
}
// Head implements IRouter.Head.
func (r *Router) Head(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodHead, path, handle)
}
// Options implements IRouter.Options.
func (r *Router) Options(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodOptions, path, handle)
}
// Post implements IRouter.Post.
func (r *Router) Post(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodPost, path, handle)
}
// Put implements IRouter.Put.
func (r *Router) Put(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodPut, path, handle)
}
// Patch implements IRouter.Patch.
func (r *Router) Patch(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodPatch, path, handle)
}
// Delete implements IRouter.Delete.
func (r *Router) Delete(path string, handle Handle, opts ...RouteOption) {
r.Handle(http.MethodDelete, path, handle, opts...)
}
// Handle implements IRouter.Handle.
func (r *Router) Handle(method, path string, handle Handle, opts ...RouteOption) {
if method == "" {
panic("method must not be empty")
}
if len(path) < 1 || path[0] != '/' {
panic("path must begin with '/' in path '" + path + "'")
}
if handle == nil {
panic("handle must not be nil")
}
if r.trees == nil {
r.trees = make(map[string]*node)
}
root := r.trees[method]
if root == nil {
root = new(node)
r.trees[method] = root
r.globalAllowed = r.allowed("*", "")
}
route := newRoute(path, handle, opts...)
if route.name != "" {
if _, ok := r.routes[route.name]; ok {
panic("route name " + route.name + " is already registered")
}
if r.routes == nil {
r.routes = make(map[string]*Route)
}
r.routes[route.name] = route
}
root.addRoute(path, route)
// Update maxParams
if pc := countParams(path); pc > r.maxParams {
r.maxParams = pc
}
// Lazy-init paramsPool alloc func
if r.paramsPool.New == nil && r.maxParams > 0 {
r.paramsPool.New = func() interface{} {
ps := make(Params, 0, r.maxParams)
return &ps
}
}
if r.contextPool.New == nil {
r.contextPool.New = func() interface{} {
return &Context{}
}
}
}
// Handler implements IRouter.Handler.
func (r *Router) Handler(method, path string, handler http.Handler, opts ...RouteOption) {
r.Handle(method, path, HandleHandler(handler), opts...)
}
// HandlerFunc implements IRouter.HandlerFunc.
func (r *Router) HandlerFunc(method, path string, f http.HandlerFunc, opts ...RouteOption) {
r.Handle(method, path, HandleHandlerFunc(f), opts...)
}
// ServeFiles serves files from the given file system root.
// The path must end with "/*filepath", files are then served from the local
// path /defined/root/dir/*filepath.
// For example if root is "/etc" and *filepath is "passwd", the local file
// "/etc/passwd" would be served.
// Internally a http.FileServer is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// To use the operating system's file system implementation,
// use http.Dir:
// router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (r *Router) ServeFiles(path string, root http.FileSystem) {
if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
panic("path must end with /*filepath in path '" + path + "'")
}
fileServer := http.FileServer(root)
r.Get(path, func(ctx *Context) error {
ctx.Request.URL.Path = ctx.Params.String("filepath")
fileServer.ServeHTTP(ctx.Response, ctx.Request)
return nil
})
}
// Lookup allows the manual lookup of a method + path combo.
// This is e.g. useful to build a framework around this router.
// If the path was found, it returns the handle function and the path parameter
// values. Otherwise the third return value indicates whether a redirection to
// the same path with an extra / without the trailing slash should be performed.
func (r *Router) Lookup(method, path string) (*Route, Params, bool) {
if root := r.trees[method]; root != nil {
route, ps, tsr := root.getValue(path, r.getParams, r.UseRawPath)
if route == nil {
return nil, nil, tsr
}
if ps == nil {
return route, nil, tsr
}
return route, *ps, tsr
}
return nil, nil, false
}
func (r *Router) allowed(path, reqMethod string) (allow string) {
allowed := make([]string, 0, 9)
if path == "*" { // server-wide
// empty method is used for internal calls to refresh the cache
if reqMethod == "" {
for method := range r.trees {
if method == http.MethodOptions {
continue
}
// Add request method to list of allowed methods
allowed = append(allowed, method)
}
} else {
return r.globalAllowed
}
} else { // specific path
for method := range r.trees {
// Skip the requested method - we already tried this one
if method == reqMethod || method == http.MethodOptions {
continue
}
handle, _, _ := r.trees[method].getValue(path, nil, r.UseRawPath)
if handle != nil {
// Add request method to list of allowed methods
allowed = append(allowed, method)
}
}
}
if len(allowed) > 0 {
// Add request method to list of allowed methods
allowed = append(allowed, http.MethodOptions)
// Sort allowed methods.
// sort.Strings(allowed) unfortunately causes unnecessary allocations
// due to allowed being moved to the heap and interface conversion
for i, l := 1, len(allowed); i < l; i++ {
for j := i; j > 0 && allowed[j] < allowed[j-1]; j-- {
allowed[j], allowed[j-1] = allowed[j-1], allowed[j]
}
}
// return as comma separated list
return strings.Join(allowed, ", ")
}
return
}
// ServeHTTP makes the router implement the http.Handler interface.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := r.getContext()
ctx.Request = req
ctx.Response = w
defer r.putContext(ctx)
var err error
if r.handle != nil {
err = r.handle(ctx)
} else {
err = r.handleRequest(ctx)
}
if err != nil {
r.HandleError(ctx, err)
}
}
func (r *Router) handleRequest(ctx *Context) (err error) {
path := ctx.Request.URL.Path
if r.UseRawPath && ctx.Request.URL.RawPath != "" {
path = ctx.Request.URL.RawPath
}
if root := r.trees[ctx.Request.Method]; root != nil {
if route, ps, tsr := root.getValue(path, r.getParams, r.UseRawPath); route != nil {
ctx.Route = route
if ps != nil {
r.putParams(ps)
ctx.Params = *ps
}
err = route.handle(ctx)
return
} else if ctx.Request.Method != http.MethodConnect && path != "/" {
// Moved Permanently, request with Get method
code := http.StatusMovedPermanently
if ctx.Request.Method != http.MethodGet {
// Permanent Redirect, request with same method
code = http.StatusPermanentRedirect
}
if tsr && r.RedirectTrailingSlash {
if len(path) > 1 && path[len(path)-1] == '/' {
path = path[:len(path)-1]
} else {
path = path + "/"
}
ctx.Redirect(path, code)
return
}
// Try to fix the request path
if r.RedirectFixedPath {
fixedPath, found := root.findCaseInsensitivePath(
CleanPath(path),
r.RedirectTrailingSlash,
)
if found {
ctx.Redirect(fixedPath, code)
return
}
}
}
}
if ctx.Request.Method == http.MethodOptions && r.HandleOPTIONS {
// Handle OPTIONS requests
if allow := r.allowed(path, http.MethodOptions); allow != "" {
ctx.Response.Header().Set("Allow", allow)
if r.GlobalOPTIONS != nil {
r.GlobalOPTIONS.ServeHTTP(ctx.Response, ctx.Request)
}
return
}
} else if r.HandleMethodNotAllowed { // Handle 405
if allow := r.allowed(path, ctx.Request.Method); allow != "" {
ctx.Response.Header().Set("Allow", allow)
if r.MethodNotAllowed != nil {
r.MethodNotAllowed.ServeHTTP(ctx.Response, ctx.Request)
return
}
return ErrMethodNotAllowed
}
}
// Handle 404
if r.NotFound != nil {
r.NotFound.ServeHTTP(ctx.Response, ctx.Request)
return
}
return ErrNotFound
}
// HandleError handles error.
func (r *Router) HandleError(ctx *Context, err error) {
if r.ErrorHandler != nil {
r.ErrorHandler.Handle(ctx, err)
return
}
switch e := err.(type) {
case StatusError:
ctx.Error(err.Error(), e.Status())
default:
ctx.Error(err.Error(), http.StatusInternalServerError)
}
}