-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
415 lines (375 loc) · 10.2 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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rum
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"sync"
)
const (
options = iota
get
head
post
put
delete
trace
connect
patch
)
// ErrGroupExisted is the error returned by Group when registers a existed group.
var ErrGroupExisted = errors.New("Group Existed")
// ErrParamsKeyEmpty is the error returned by HandleFunc when the params key is empty.
var ErrParamsKeyEmpty = errors.New("Params key must be not empty")
// contextKey is a key for use with context.WithValue. It's used as
// a pointer so it fits in an interface{} without allocation.
type contextKey struct {
name string
}
// String returns a context key.
func (k *contextKey) String() string { return "github.com/hslam/rum context key " + k.name }
// RecoveryContextKey is a context key.
var RecoveryContextKey = &contextKey{"recovery"}
// Mux is an HTTP request multiplexer.
type Mux struct {
mut sync.RWMutex
prefixes map[string]*prefix
group string
groups map[string]*Mux
context struct {
middlewares []http.Handler
recovery http.Handler
notFound http.Handler
}
}
type prefix struct {
prefix string
m map[string]*Entry
}
// Entry represents an HTTP HandlerFunc entry.
type Entry struct {
handler http.Handler
handlers [9]http.Handler
key string
match []string
params map[string]string
}
// NewMux returns a new Mux.
func NewMux() *Mux {
m := &Mux{
prefixes: make(map[string]*prefix),
groups: make(map[string]*Mux),
}
return m
}
func newGroup(group string) *Mux {
m := &Mux{
prefixes: make(map[string]*prefix),
groups: make(map[string]*Mux),
group: group,
}
return m
}
// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
path := m.replace(r.URL.Path)
m.mut.RLock()
entry := m.searchEntry(path, w, r)
m.mut.RUnlock()
if entry != nil {
m.serveEntry(entry, w, r)
return
}
if m.context.notFound != nil {
m.context.notFound.ServeHTTP(w, r)
return
}
http.Error(w, "404 Not Found : "+r.URL.String(), http.StatusNotFound)
}
func (m *Mux) searchEntry(path string, w http.ResponseWriter, r *http.Request) *Entry {
if entry := m.getHandlerFunc(path); entry != nil {
return entry
}
for _, groupMux := range m.groups {
if entry := groupMux.searchEntry(path, w, r); entry != nil {
return entry
}
}
return nil
}
func (m *Mux) serveEntry(entry *Entry, w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" && entry.handlers[get] != nil {
m.serveHandler(entry.handlers[get], w, r)
} else if r.Method == "POST" && entry.handlers[post] != nil {
m.serveHandler(entry.handlers[post], w, r)
} else if r.Method == "PUT" && entry.handlers[put] != nil {
m.serveHandler(entry.handlers[put], w, r)
} else if r.Method == "DELETE" && entry.handlers[delete] != nil {
m.serveHandler(entry.handlers[delete], w, r)
} else if r.Method == "PATCH" && entry.handlers[patch] != nil {
m.serveHandler(entry.handlers[patch], w, r)
} else if r.Method == "HEAD" && entry.handlers[head] != nil {
m.serveHandler(entry.handlers[head], w, r)
} else if r.Method == "OPTIONS" && entry.handlers[options] != nil {
m.serveHandler(entry.handlers[options], w, r)
} else if r.Method == "TRACE" && entry.handlers[trace] != nil {
m.serveHandler(entry.handlers[trace], w, r)
} else if r.Method == "CONNECT" && entry.handlers[connect] != nil {
m.serveHandler(entry.handlers[connect], w, r)
} else {
m.serveHandler(entry.handler, w, r)
}
}
// Recovery returns a recovery handler function that recovers from any panics and writes a 500 status code.
func Recovery(w http.ResponseWriter, r *http.Request) {
err := r.Context().Value(RecoveryContextKey)
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "500 Internal Server Error : %v\n", err)
}
func (m *Mux) serveHandler(handler http.Handler, w http.ResponseWriter, r *http.Request) {
if m.context.recovery != nil {
defer func() {
if err := recover(); err != nil {
ctx := context.WithValue(r.Context(), RecoveryContextKey, err)
m.context.recovery.ServeHTTP(w, r.WithContext(ctx))
}
}()
}
m.middleware(w, r)
if handler != nil {
handler.ServeHTTP(w, r)
}
}
func (m *Mux) getHandlerFunc(path string) *Entry {
if prefix, key, ok := m.matchParams(path); ok {
if entry, ok := m.prefixes[prefix].m[key]; ok {
return entry
}
}
return nil
}
// HandleFunc registers a handler function with the given pattern to the Mux.
func (m *Mux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) *Entry {
return m.Handle(pattern, http.HandlerFunc(handler))
}
// Handle registers a handler with the given pattern to the Mux.
func (m *Mux) Handle(pattern string, handler http.Handler) *Entry {
m.mut.Lock()
defer m.mut.Unlock()
pattern = m.replace(pattern)
pre, key, match, params := m.parseParams(m.group + pattern)
if v, ok := m.prefixes[pre]; ok {
if entry, ok := v.m[key]; ok {
entry.handler = handler
entry.key = key
entry.match = match
entry.params = params
m.prefixes[pre].m[key] = entry
return entry
}
entry := &Entry{}
entry.handler = handler
entry.key = key
entry.match = match
entry.params = params
m.prefixes[pre].m[key] = entry
return entry
}
m.prefixes[pre] = &prefix{m: make(map[string]*Entry), prefix: pre}
entry := &Entry{}
entry.handler = handler
entry.key = key
entry.match = match
entry.params = params
m.prefixes[pre].m[key] = entry
return entry
}
// Group registers a group with the given pattern to the Mux.
func (m *Mux) Group(group string, f func(m *Mux)) {
m.mut.Lock()
defer m.mut.Unlock()
group = m.replace(group)
groupMux := newGroup(group)
f(groupMux)
if _, ok := m.groups[group]; ok {
panic(ErrGroupExisted)
}
groupMux.context = m.context
m.groups[group] = groupMux
}
// NotFound registers a not found handler function to the Mux.
func (m *Mux) NotFound(handler http.HandlerFunc) {
m.mut.Lock()
defer m.mut.Unlock()
m.context.notFound = handler
}
// Recovery registers a recovery handler function to the Mux.
func (m *Mux) Recovery(handler http.HandlerFunc) {
m.mut.Lock()
defer m.mut.Unlock()
m.context.recovery = handler
}
// Use uses middleware.
func (m *Mux) Use(handler http.HandlerFunc) {
m.mut.Lock()
defer m.mut.Unlock()
m.context.middlewares = append(m.context.middlewares, handler)
}
func (m *Mux) middleware(w http.ResponseWriter, r *http.Request) {
for _, handler := range m.context.middlewares {
handler.ServeHTTP(w, r)
}
}
// Params returns http request params.
func (m *Mux) Params(r *http.Request) map[string]string {
params := make(map[string]string)
path := m.replace(r.URL.Path)
m.mut.RLock()
if prefix, key, ok := m.matchParams(path); ok {
if entry, ok := m.prefixes[prefix].m[key]; ok &&
len(entry.match) > 0 && len(path) > len(prefix) {
strs := strings.Split(path[len(prefix):], "/")
if len(strs) == len(entry.match) {
for i := 0; i < len(strs); i++ {
if entry.match[i] != "" {
params[entry.match[i]] = strs[i]
}
}
}
}
}
m.mut.RUnlock()
return params
}
func (m *Mux) matchParams(path string) (string, string, bool) {
for _, p := range m.prefixes {
if strings.HasPrefix(path, p.prefix) {
r := path[len(p.prefix):]
if r == "" {
return p.prefix, "", true
}
for _, v := range p.m {
count := strings.Count(r, "/")
if count+1 == len(v.match) {
form := strings.Split(r, "/")
key := ""
for i := 0; i < len(form); i++ {
if v.match[i] != "" {
if i > 0 {
key += "/:"
} else {
key += ":"
}
} else {
key += "/" + form[i]
}
}
if key == v.key {
return p.prefix, v.key, true
}
}
}
}
}
return "", "", false
}
func (m *Mux) parseParams(pattern string) (string, string, []string, map[string]string) {
prefix := ""
var match []string
key := ""
params := make(map[string]string)
if strings.Contains(pattern, ":") {
idx := strings.Index(pattern, ":")
prefix = pattern[:idx]
if idx+1 == len(pattern) || strings.Contains(pattern, ":/") {
panic(ErrParamsKeyEmpty)
}
match = strings.Split(pattern[idx:], "/")
for i := 0; i < len(match); i++ {
if strings.Contains(match[i], ":") {
match[i] = strings.Trim(match[i], ":")
params[match[i]] = ""
if i > 0 {
key += "/:"
} else {
key += ":"
}
} else {
key += "/" + match[i]
match[i] = ""
}
}
} else {
prefix = pattern
}
return prefix, key, match, params
}
func (m *Mux) replace(s string) string {
for strings.Contains(s, "//") {
s = strings.ReplaceAll(s, "//", "/")
}
return s
}
// GET adds a GET HTTP method to the entry.
func (entry *Entry) GET() *Entry {
entry.handlers[get] = entry.handler
return entry
}
// POST adds a POST HTTP method to the entry.
func (entry *Entry) POST() *Entry {
entry.handlers[post] = entry.handler
return entry
}
// PUT adds a PUT HTTP method to the entry.
func (entry *Entry) PUT() *Entry {
entry.handlers[put] = entry.handler
return entry
}
// DELETE adds a DELETE HTTP method to the entry.
func (entry *Entry) DELETE() *Entry {
entry.handlers[delete] = entry.handler
return entry
}
// PATCH adds a PATCH HTTP method to the entry.
func (entry *Entry) PATCH() *Entry {
entry.handlers[patch] = entry.handler
return entry
}
// HEAD adds a HEAD HTTP method to the entry.
func (entry *Entry) HEAD() *Entry {
entry.handlers[head] = entry.handler
return entry
}
// OPTIONS adds a OPTIONS HTTP method to the entry.
func (entry *Entry) OPTIONS() *Entry {
entry.handlers[options] = entry.handler
return entry
}
// TRACE adds a TRACE HTTP method to the entry.
func (entry *Entry) TRACE() *Entry {
entry.handlers[trace] = entry.handler
return entry
}
// CONNECT adds a CONNECT HTTP method to the entry.
func (entry *Entry) CONNECT() *Entry {
entry.handlers[connect] = entry.handler
return entry
}
// All adds all HTTP method to the entry.
func (entry *Entry) All() {
entry.GET()
entry.POST()
entry.HEAD()
entry.OPTIONS()
entry.PUT()
entry.PATCH()
entry.DELETE()
entry.TRACE()
entry.CONNECT()
}