-
Notifications
You must be signed in to change notification settings - Fork 4
/
servemux.go
216 lines (179 loc) · 6.68 KB
/
servemux.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
package powermux
import (
"bytes"
"context"
"net/http"
)
// ServeMux is the multiplexer for http requests
type ServeMux struct {
baseRoute *Route
hostRoutes map[string]*Route
executionPool *executionPool
}
// ctxKey is the key type used for path parameters in the request context
type ctxKey string
var (
executionKey = ctxKey("ex")
)
func getRequestExecution(req *http.Request) *routeExecution {
ex := req.Context().Value(executionKey).(*routeExecution)
return ex
}
// PathParam gets named path parameters and their values from the request
//
// the path '/users/:name' given '/users/andrew' will have `PathParam(r, "name")` => `"andrew"`
// unset values return an empty stringRoutes
func PathParam(req *http.Request, name string) (value string) {
ex := getRequestExecution(req)
return ex.params[name]
}
// PathParams returns the map of all path parameters and their values from the request.
//
// Altering the values of this map will not affect future calls to PathParam and PathParams.
func PathParams(req *http.Request) (params map[string]string) {
ex := getRequestExecution(req)
params = make(map[string]string)
for k, v := range ex.params {
params[k] = v
}
return
}
// RequestPath returns the path definition that the router used to serve this request,
// without any parameter substitution.
func RequestPath(req *http.Request) (value string) {
ex := getRequestExecution(req)
return ex.pattern
}
// NewServeMux creates a new multiplexer, and sets up a default not found handler
func NewServeMux() *ServeMux {
s := &ServeMux{
baseRoute: newRoute(),
hostRoutes: make(map[string]*Route),
executionPool: newExecutionPool(),
}
s.NotFound(http.NotFoundHandler())
return s
}
func (s *ServeMux) getAll(r *http.Request, ex *routeExecution) {
path := r.URL.EscapedPath()
// fill it
if route, ok := s.hostRoutes[r.URL.Host]; ok {
route.execute(ex, r.Method, path)
} else {
s.baseRoute.execute(ex, r.Method, path)
}
// fall back on not found handler if necessary
if ex.handler == nil {
ex.handler = ex.notFound
}
return
}
// ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (s *ServeMux) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Get a route execution from the pool
ex := s.executionPool.Get()
s.getAll(req, ex)
// Save the execution
ctx := context.WithValue(req.Context(), executionKey, ex)
// Save context into request
req = req.WithContext(ctx)
// Run a middleware/handler closure to nest all middleware
f := getNextMiddleware(ex.middleware, ex.handler)
f(rw, req)
s.executionPool.Put(ex)
}
// Handle registers the handler for the given pattern.
// If a handler already exists for pattern it is overwritten.
func (s *ServeMux) Handle(path string, handler http.Handler) {
s.Route(path).Any(handler)
}
// HandleHost registers the handler for the given pattern and host.
// If a handler already exists for pattern it is overwritten.
func (s *ServeMux) HandleHost(host, path string, handler http.Handler) {
s.RouteHost(host, path).Any(handler)
}
// Middleware adds middleware for the given pattern.
func (s *ServeMux) Middleware(path string, middleware Middleware) {
s.Route(path).Middleware(middleware)
}
// MiddlewareFor adds a middleware to this node, but will only be executed
// for requests with the verb specified.
// Verbs are case sensitive, and should use the `http.Method*` constants.
// Panics if any of the verbs provided are unknown.
func (s *ServeMux) MiddlewareFor(path string, middleware Middleware, verbs ...string) {
s.Route(path).MiddlewareFor(middleware, verbs...)
}
// MiddlewareExceptFor adds a middleware to this node, but will only be executed
// for requests that are not in the list of verbs.
// Verbs are case sensitive, and should use the `http.Method*` constants.
// Panics if any of the verbs provided are unknown.
func (s *ServeMux) MiddlewareExceptFor(path string, middleware Middleware, verbs ...string) {
s.Route(path).MiddlewareExceptFor(middleware, verbs...)
}
// MiddlewareFunc registers a plain function as a middleware.
func (s *ServeMux) MiddlewareFunc(path string, m MiddlewareFunc) *Route {
return s.Route(path).MiddlewareFunc(m)
}
// MiddlewareHost adds middleware for the given pattern.
func (s *ServeMux) MiddlewareHost(host, path string, middleware Middleware) {
s.RouteHost(host, path).Middleware(middleware)
}
// HandleFunc registers the handler function for the given pattern.
func (s *ServeMux) HandleFunc(path string, handler func(http.ResponseWriter, *http.Request)) {
s.Handle(path, http.HandlerFunc(handler))
}
// Handler returns the handler to use for the given request, consulting r.Method, r.Host, and r.URL.Path.
// It always returns a non-nil handler. If the path is not in its canonical form, the handler will be an
// internally-generated handler that redirects to the canonical path.
//
// Handler also returns the registered pattern that matches the request or, in the case of internally-generated
// redirects, the pattern that will match after following the redirect.
//
// If there is no registered handler that applies to the request, Handler returns a “page not found” handler
// and an empty pattern.
func (s *ServeMux) Handler(r *http.Request) (http.Handler, string) {
handler, _, pattern := s.HandlerAndMiddleware(r)
return handler, pattern
}
// HandlerAndMiddleware returns the same as Handler, but with the addition of an array of middleware, in the order
// they would have been executed
func (s *ServeMux) HandlerAndMiddleware(r *http.Request) (http.Handler, []Middleware, string) {
// create a new execution so fields will live outside of this function
ex := newExecution()
s.getAll(r, ex)
return ex.handler, ex.middleware, ex.pattern
}
// Route returns the route from the root of the domain to the given pattern
func (s *ServeMux) Route(path string) *Route {
return s.baseRoute.Route(path)
}
// RouteHost returns the route from the root of the domain to the given pattern on a specific domain
func (s *ServeMux) RouteHost(host, path string) *Route {
r, ok := s.hostRoutes[host]
if !ok {
r = newRoute()
s.hostRoutes[host] = r
}
return r.Route(path)
}
// NotFound sets the default not found handler for the server
func (s *ServeMux) NotFound(handler http.Handler) {
s.baseRoute.NotFound(handler)
}
// String returns a list of all routes registered with this server
func (s *ServeMux) String() string {
routes := make([]string, 0, 1)
s.baseRoute.stringRoutes(&routes)
buf := bytes.Buffer{}
for _, route := range routes {
buf.WriteString(route + "\n")
}
for host, baseRoute := range s.hostRoutes {
routes = routes[0:0]
baseRoute.stringRoutes(&routes)
for _, route := range routes {
buf.WriteString(host + route + "\n")
}
}
return buf.String()
}