-
Notifications
You must be signed in to change notification settings - Fork 1
/
mux.go
268 lines (258 loc) · 7.88 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
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package bear
import (
"fmt"
"net/http"
"strings"
)
// Mux is an HTTP multiplexer. It uses a tree structure for fast routing,
// supports dynamic parameters, middleware, and accepts both native
// http.HandlerFunc or bear.HandlerFunc, which accepts an extra *Context
// argument that allows storing state (using the Get() and Set() methods) and
// calling the Next() middleware.
type Mux struct {
trees [8]*tree // pointers to a tree for each HTTP verb
always []HandlerFunc // list of handlers that run for all requests
wild [8]bool // true if a tree has wildcard (requires back-references)
}
func parsePath(s string) (components []string, last int) {
start, offset := 0, 0
if slashr == s[0] {
start = 1
}
if slashr == s[len(s)-1] {
offset = 1
}
components = strings.SplitAfter(s, slash)
if start == 1 || offset == 1 {
components = components[start : len(components)-offset]
}
last = len(components) - 1
if offset == 0 {
components[last] = components[last] + slash
}
return components, last
}
// Always adds one or more handlers that will run before every single request.
// Multiple calls to Always will append the current list of Always handlers with
// the newly added handlers.
//
// Handlers must be either bear.HandlerFunc functions or functions that match
// the bear.HandlerFunc signature and they should call (*Context).Next to
// continue the response life cycle.
func (mux *Mux) Always(handlers ...interface{}) error {
if functions, err := handlerizeStrict(handlers); err != nil {
return err
} else {
mux.always = append(mux.always, functions...)
return err
}
}
// On adds HTTP verb handler(s) for a URL pattern. The handler argument(s)
// should either be http.HandlerFunc or bear.HandlerFunc or conform to the
// signature of one of those two. NOTE: if http.HandlerFunc (or a function
// conforming to its signature) is used no other handlers can *follow* it, i.e.
// it is not middleware.
//
// It returns an error if it fails, but does not panic. Verb strings are
// uppercase HTTP methods. There is a special verb "*" which can be used to
// answer *all* HTTP methods. It is not uncommon for the verb "*" to return
// errors, because a path may already have a listener associated with one HTTP
// verb before the "*" verb is called. For example, this common and useful
// pattern will return an error that can safely be ignored (see error example).
//
// Pattern strings are composed of tokens that are separated by "/" characters.
// There are three kinds of tokens:
//
// 1. static path strings: "/foo/bar/baz/etc"
//
// 2. dynamically populated parameters "/foo/{bar}/baz" (where "bar" will be
// populated in the *Context.Params)
//
// 3. wildcard tokens "/foo/bar/*" where * has to be the final token.
// Parsed URL params are available in handlers via the Params map of the
// *Context argument.
//
// Notes:
//
// 1. A trailing slash / is always implied, even when not explicit.
//
// 2. Wildcard (*) patterns are only matched if no other (more specific)
// pattern matches. If multiple wildcard rules match, the most specific takes
// precedence.
//
// 3. Wildcard patterns do *not* match empty strings: a request to /foo/bar will
// not match the pattern "/foo/bar/*". The only exception to this is the root
// wildcard pattern "/*" which will match the request path / if no root
// handler exists.
func (mux *Mux) On(verb string, pattern string, handlers ...interface{}) error {
if verb == asterisk {
errors := []string{}
for _, verb := range verbs {
if err := mux.On(verb, pattern, handlers...); err != nil {
errors = append(errors, err.Error())
}
}
if 0 == len(errors) {
return nil
}
return fmt.Errorf(strings.Join(errors, "\n"))
}
tr, wildcards := mux.tree(verb)
if nil == tr {
return fmt.Errorf("bear: %s isn't a valid HTTP verb", verb)
}
if functions, err := handlerizeLax(verb, pattern, handlers); err != nil {
return err
} else {
tr.set(verb, pattern, functions, wildcards, &err)
return err
}
}
// ServeHTTP allows a Mux instance to conform to the http.Handler interface.
func (mux *Mux) ServeHTTP(res http.ResponseWriter, req *http.Request) {
tr, wildcards := mux.tree(req.Method)
if nil == tr { // if req.Method is not found in HTTP verbs
http.NotFound(res, req)
return
}
// root is a special case because it is the top node in the tree
if req.URL.Path == slash || req.URL.Path == empty {
if nil != tr.handlers { // root match
(&Context{
handler: -1,
mux: mux,
tree: tr,
ResponseWriter: res,
Request: req,
}).Next()
return
} else if wild := tr.children[wildcard]; nil != wild {
// root level wildcard pattern match
(&Context{
handler: -1,
mux: mux,
tree: wild,
ResponseWriter: res,
Request: req,
}).Next()
return
}
http.NotFound(res, req)
return
}
var key string
components, last := parsePath(req.URL.Path)
capacity := last + 1 // maximum number of params possible for this request
context := &Context{
handler: -1,
mux: mux,
Request: req,
ResponseWriter: res}
current := &tr.children
// If no wildcards: simpler, slightly faster logic (this if *always* returns).
if !*wildcards {
for index, component := range components {
key = component
if nil == *current {
http.NotFound(res, req)
return
} else if nil == (*current)[key] {
if nil == (*current)[dynamic] {
http.NotFound(res, req)
return
} else {
key = dynamic
context.param((*current)[key].name, component, capacity)
}
}
if index == last {
if nil == (*current)[key].handlers {
http.NotFound(res, req)
} else {
context.tree = (*current)[key]
context.Next()
}
return
}
current = &(*current)[key].children
}
}
// If wildcards exist, more involved logic.
wild := tr.children[wildcard]
for index, component := range components {
key = component
if nil == (*current)[key] {
if nil == (*current)[dynamic] && nil == (*current)[wildcard] {
if nil == wild { // there's no wildcard up the tree
http.NotFound(res, req)
} else { // wildcard pattern match
context.tree = wild
context.Next()
}
return
} else {
if nil != (*current)[wildcard] {
// i.e. there is a more proximate wildcard
wild = (*current)[wildcard]
context.param(asterisk,
strings.Join(components[index:], empty), capacity)
}
if nil != (*current)[dynamic] {
key = dynamic
context.param((*current)[key].name, component, capacity)
} else { // wildcard pattern match
context.tree = wild
context.Next()
return
}
}
}
if index == last {
if nil == (*current)[key].handlers {
http.NotFound(res, req)
} else { // non-wildcard pattern match
context.tree = (*current)[key]
context.Next()
}
return
}
current = &(*current)[key].children
if nil != (*current)[wildcard] {
wild = (*current)[wildcard] // there's a more proximate wildcard
context.param(asterisk,
strings.Join(components[index:], empty), capacity)
}
}
}
func (mux *Mux) tree(name string) (*tree, *bool) {
switch name {
case "CONNECT":
return mux.trees[0], &mux.wild[0]
case "DELETE":
return mux.trees[1], &mux.wild[1]
case "GET":
return mux.trees[2], &mux.wild[2]
case "HEAD":
return mux.trees[3], &mux.wild[3]
case "OPTIONS":
return mux.trees[4], &mux.wild[4]
case "POST":
return mux.trees[5], &mux.wild[5]
case "PUT":
return mux.trees[6], &mux.wild[6]
case "TRACE":
return mux.trees[7], &mux.wild[7]
default:
return nil, nil
}
}
// New returns a pointer to a Mux instance
func New() *Mux {
mux := new(Mux)
mux.trees = [8]*tree{{}, {}, {}, {}, {}, {}, {}, {}}
mux.wild = [8]bool{false, false, false, false, false, false, false, false}
return mux
}