-
Notifications
You must be signed in to change notification settings - Fork 2
/
mux.go
520 lines (458 loc) · 15.5 KB
/
mux.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Package route implements HTTP request miltiplexer.
package route
import (
"errors"
"fmt"
"html/template"
"io"
"net/http"
"net/url"
"path"
"path/filepath"
"reflect"
"runtime"
"sync"
)
type (
// Mux is the top-level framework instance.
Mux struct {
premiddleware []MiddlewareFunc
middleware []MiddlewareFunc
maxParam *int
router *router
notFoundHandler HandlerFunc
pool sync.Pool
Debug bool
HTTPErrorHandler HTTPErrorHandler
Binder Binder
Renderer Renderer
}
// Route contains a handler and information for matching against requests.
Route struct {
Method string `json:"method"`
Path string `json:"path"`
Name string `json:"name"`
}
// HTTPError represents an error that occurred while handling a request.
HTTPError struct {
Code int
Message interface{}
Internal error // Stores the error returned by an external dependency
}
// HandlerFunc defines a function to serve HTTP requests.
HandlerFunc func(Context) error
// HTTPErrorHandler is a centralized HTTP error handler.
HTTPErrorHandler func(error, Context)
// Renderer is the interface that wraps the Render function.
Renderer interface {
Render(io.Writer, string, interface{}, Context) error
}
// i is the interface for Mux and Group.
i interface {
GET(string, HandlerFunc, ...MiddlewareFunc) *Route
}
)
// MIME types
const (
MIMEApplicationJSON = "application/json"
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
MIMEApplicationJavaScript = "application/javascript"
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
MIMEApplicationXML = "application/xml"
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
MIMETextXML = "text/xml"
MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8
MIMEApplicationForm = "application/x-www-form-urlencoded"
MIMEApplicationProtobuf = "application/protobuf"
MIMEApplicationMsgpack = "application/msgpack"
MIMETextHTML = "text/html"
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
MIMETextPlain = "text/plain"
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
MIMEMultipartForm = "multipart/form-data"
MIMEOctetStream = "application/octet-stream"
)
const (
charsetUTF8 = "charset=UTF-8"
// PROPFIND HTTP Header
PROPFIND = "PROPFIND"
)
// Headers
const (
HeaderAccept = "Accept"
HeaderAcceptEncoding = "Accept-Encoding"
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderCookie = "Cookie"
HeaderSetCookie = "Set-Cookie"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderUpgrade = "Upgrade"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXForwardedProtocol = "X-Forwarded-Protocol"
HeaderXForwardedSsl = "X-Forwarded-Ssl"
HeaderXUrlScheme = "X-Url-Scheme"
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
HeaderXRealIP = "X-Real-IP"
HeaderXRequestID = "X-Request-ID"
HeaderXRequestedWith = "X-Requested-With"
HeaderServer = "Server"
HeaderOrigin = "Origin"
// Access control
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
// Security
HeaderStrictTransportSecurity = "Strict-Transport-Security"
HeaderXContentTypeOptions = "X-Content-Type-Options"
HeaderXXSSProtection = "X-XSS-Protection"
HeaderXFrameOptions = "X-Frame-Options"
HeaderContentSecurityPolicy = "Content-Security-Policy"
HeaderXCSRFToken = "X-CSRF-Token"
)
var (
methods = [...]string{
http.MethodConnect,
http.MethodDelete,
http.MethodGet,
http.MethodHead,
http.MethodOptions,
http.MethodPatch,
http.MethodPost,
PROPFIND,
http.MethodPut,
http.MethodTrace,
}
)
// Errors
var (
ErrUnsupportedMediaType = NewHTTPError(http.StatusUnsupportedMediaType)
ErrNotFound = NewHTTPError(http.StatusNotFound)
ErrUnauthorized = NewHTTPError(http.StatusUnauthorized)
ErrForbidden = NewHTTPError(http.StatusForbidden)
ErrMethodNotAllowed = NewHTTPError(http.StatusMethodNotAllowed)
ErrStatusRequestEntityTooLarge = NewHTTPError(http.StatusRequestEntityTooLarge)
ErrTooManyRequests = NewHTTPError(http.StatusTooManyRequests)
ErrBadRequest = NewHTTPError(http.StatusBadRequest)
ErrBadGateway = NewHTTPError(http.StatusBadGateway)
ErrInternalServerError = NewHTTPError(http.StatusInternalServerError)
ErrRequestTimeout = NewHTTPError(http.StatusRequestTimeout)
ErrServiceUnavailable = NewHTTPError(http.StatusServiceUnavailable)
ErrValidatorNotRegistered = errors.New("validator not registered")
ErrRendererNotRegistered = errors.New("Renderer not registered")
ErrInvalidRedirectCode = errors.New("invalid redirect status code")
ErrCookieNotFound = errors.New("cookie not found")
)
// Error handlers
var (
NotFoundHandler = func(c Context) error {
return ErrNotFound
}
MethodNotAllowedHandler = func(c Context) error {
return ErrMethodNotAllowed
}
)
type options struct {
binder Binder
renderer Renderer
httpErrorHandler HTTPErrorHandler
}
// A Option sets options such as credentials, tls, etc.
type Option func(*options)
// WithBinder allows to override default mux Binder.
func WithBinder(binder Binder) Option {
return func(o *options) {
o.binder = binder
}
}
// WithRenderer allows to register mux view Renderer.
func WithRenderer(renderer Renderer) Option {
return func(o *options) {
o.renderer = renderer
}
}
// WithHTTPErrorHandler allows to override default mux global error handler.
func WithHTTPErrorHandler(handler HTTPErrorHandler) Option {
return func(o *options) {
o.httpErrorHandler = handler
}
}
// NewServeMux creates an instance of mux.
func NewServeMux(opt ...Option) (e *Mux) {
opts := options{
binder: &DefaultBinder{},
renderer: nil,
}
for _, o := range opt {
o(&opts)
}
e = &Mux{
maxParam: new(int),
Binder: opts.binder,
Renderer: opts.renderer,
}
// http error handler must be set after mux instance.
if opts.httpErrorHandler != nil {
e.HTTPErrorHandler = opts.httpErrorHandler
} else {
e.HTTPErrorHandler = e.defaultHTTPErrorHandler
}
e.pool.New = func() interface{} {
return e.NewContext(nil, nil)
}
e.router = newRouter(e)
return
}
// NewContext returns a Context instance.
func (mux *Mux) NewContext(r *http.Request, w http.ResponseWriter) Context {
return &context{
request: r,
response: NewResponse(w),
store: make(map[string]interface{}),
mux: mux,
pvalues: make([]string, *mux.maxParam),
handler: NotFoundHandler,
}
}
// Pre adds middleware to the chain which is run before router.
func (mux *Mux) Pre(middleware ...MiddlewareFunc) {
mux.premiddleware = append(mux.premiddleware, middleware...)
}
// Use adds middleware to the chain which is run after router.
func (mux *Mux) Use(middleware ...MiddlewareFunc) {
mux.middleware = append(mux.middleware, middleware...)
}
// CONNECT registers a new CONNECT route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) CONNECT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodConnect, path, h, m...)
}
// DELETE registers a new DELETE route for a path with matching handler in the router
// with optional route-level middleware.
func (mux *Mux) DELETE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodDelete, path, h, m...)
}
// GET registers a new GET route for a path with matching handler in the router
// with optional route-level middleware.
func (mux *Mux) GET(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodGet, path, h, m...)
}
// HEAD registers a new HEAD route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) HEAD(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodHead, path, h, m...)
}
// OPTIONS registers a new OPTIONS route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) OPTIONS(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodOptions, path, h, m...)
}
// PATCH registers a new PATCH route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) PATCH(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodPatch, path, h, m...)
}
// POST registers a new POST route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) POST(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodPost, path, h, m...)
}
// PUT registers a new PUT route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) PUT(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodPut, path, h, m...)
}
// TRACE registers a new TRACE route for a path with matching handler in the
// router with optional route-level middleware.
func (mux *Mux) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
return mux.Add(http.MethodTrace, path, h, m...)
}
// Any registers a new route for all HTTP methods and path with matching handler
// in the router with optional route-level middleware.
func (mux *Mux) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
routes := make([]*Route, len(methods))
for i, m := range methods {
routes[i] = mux.Add(m, path, handler, middleware...)
}
return routes
}
// Match registers a new route for multiple HTTP methods and path with matching
// handler in the router with optional route-level middleware.
func (mux *Mux) Match(methods []string, path string, handler HandlerFunc, middleware ...MiddlewareFunc) []*Route {
routes := make([]*Route, len(methods))
for i, m := range methods {
routes[i] = mux.Add(m, path, handler, middleware...)
}
return routes
}
// Static registers a new route with path prefix to serve static files from the
// provided root directory.
func (mux *Mux) Static(prefix, root string) *Route {
if root == "" {
root = "." // For security we want to restrict to CWD.
}
return static(mux, prefix, root)
}
func static(i i, prefix, root string) *Route {
h := func(c Context) error {
p, err := url.PathUnescape(c.Param("*"))
if err != nil {
return err
}
name := filepath.Join(root, path.Clean("/"+p)) // "/"+ for security
return c.File(name)
}
i.GET(prefix, h)
if prefix == "/" {
return i.GET(prefix+"*", h)
}
return i.GET(prefix+"/*", h)
}
// File registers a new route with path to serve a static file with optional route-level middleware.
func (mux *Mux) File(path, file string, m ...MiddlewareFunc) *Route {
return mux.GET(path, func(c Context) error {
return c.File(file)
}, m...)
}
// Add registers a new route for an HTTP method and path with matching handler
// in the router with optional route-level middleware.
func (mux *Mux) Add(method, path string, handler HandlerFunc, middleware ...MiddlewareFunc) *Route {
name := handlerName(handler)
mux.router.add(method, path, func(c Context) error {
h := handler
// Chain middleware
for i := len(middleware) - 1; i >= 0; i-- {
h = compose(h, middleware[i])
}
return h(c)
})
r := &Route{
Method: method,
Path: path,
Name: name,
}
mux.router.routes[method+path] = r
return r
}
// Group creates a new router group with prefix and optional group-level middleware.
func (mux *Mux) Group(prefix string, m ...MiddlewareFunc) (g *Group) {
g = &Group{prefix: prefix, mux: mux}
g.Use(m...)
return
}
// Routes returns the registered routes.
func (mux *Mux) Routes() []*Route {
routes := make([]*Route, 0, len(mux.router.routes))
for _, v := range mux.router.routes {
routes = append(routes, v)
}
return routes
}
// ServeHTTP implements `http.Handler` interface, which serves HTTP requests.
func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Acquire context
c := mux.pool.Get().(*context)
c.reset(r, w)
var h HandlerFunc
if mux.premiddleware == nil {
mux.router.find(r.Method, getPath(r), c)
h = c.Handler()
for i := len(mux.middleware) - 1; i >= 0; i-- {
h = compose(h, mux.middleware[i])
}
} else {
h = func(c Context) error {
mux.router.find(r.Method, getPath(r), c)
h := c.Handler()
for i := len(mux.middleware) - 1; i >= 0; i-- {
h = compose(h, mux.middleware[i])
}
return h(c)
}
for i := len(mux.premiddleware) - 1; i >= 0; i-- {
h = compose(h, mux.premiddleware[i])
}
}
// Execute chain
if err := h(c); err != nil {
mux.HTTPErrorHandler(err, c)
}
// Release context
mux.pool.Put(c)
}
// defaultHTTPErrorHandler is the default HTTP error handler. It sends a JSON response
// with status code.
func (mux *Mux) defaultHTTPErrorHandler(err error, c Context) {
var (
code = http.StatusInternalServerError
msg interface{}
)
if he, ok := err.(*HTTPError); ok {
code = he.Code
msg = he.Message
if he.Internal != nil {
err = fmt.Errorf("%v, %v", err, he.Internal)
}
} else if mux.Debug {
msg = err.Error()
} else {
msg = http.StatusText(code)
}
if _, ok := msg.(string); ok {
msg = map[string]interface{}{"message": msg}
}
// Send response
if !c.Response().Committed {
if c.Request().Method == http.MethodHead {
_ = c.NoContent(code)
} else {
_ = c.JSON(code, msg)
}
}
}
// WrapHandler wraps `http.Handler` into `mux.HandlerFunc`.
func WrapHandler(h http.Handler) HandlerFunc {
return func(c Context) error {
h.ServeHTTP(c.Response(), c.Request())
return nil
}
}
func getPath(r *http.Request) string {
rawPath := r.URL.RawPath
if rawPath == "" {
rawPath = r.URL.Path
}
return rawPath
}
func handlerName(h HandlerFunc) string {
t := reflect.ValueOf(h).Type()
if t.Kind() == reflect.Func {
return runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
}
return t.String()
}
// NewDefaultTemplateRenderer creates default template Renderer with given pattern.
func NewDefaultTemplateRenderer(pattern string) *templateRenderer {
return &templateRenderer{
templates: template.Must(template.ParseGlob(pattern)),
}
}
type templateRenderer struct {
templates *template.Template
}
func (t *templateRenderer) Render(w io.Writer, name string, data interface{}, c Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}