forked from guregu/kami
/
middleware_17.go
286 lines (265 loc) · 9.24 KB
/
middleware_17.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
// +build go1.7
package kami
import (
"context"
"fmt"
"net/http"
"unicode/utf8"
"github.com/zenazn/goji/web/mutil"
netcontext "golang.org/x/net/context"
)
// Middleware is a function that takes the current request context and returns a new request context.
// You can use middleware to build your context before your handler handles a request.
// As a special case, middleware that returns nil will halt middleware and handler execution (LogHandler will still run).
type Middleware func(context.Context, http.ResponseWriter, *http.Request) context.Context
// MiddlewareType represents types that kami can convert to Middleware.
// kami will try its best to convert standard, non-context middleware.
// See the Use function for important information about how kami middleware is run.
// The following concrete types are accepted:
// - Middleware
// - func(context.Context, http.ResponseWriter, *http.Request) context.Context
// - func(http.ResponseWriter, *http.Request) context.Context
// - func(http.Handler) http.Handler [* see Use docs]
// - func(http.ContextHandler) http.ContextHandler [* see Use docs]
// - http.Handler [read only]
// - func(http.ResponseWriter, *http.Request) [read only]
// The old x/net/context is also supported.
type MiddlewareType interface{}
// Afterware is a function that will run after middleware and the request.
// Afterware takes the request context and returns a new context, but unlike middleware,
// returning nil won't halt execution of other afterware.
type Afterware func(context.Context, mutil.WriterProxy, *http.Request) context.Context
// Afterware represents types that kami can convert to Afterware.
// The following concrete types are accepted:
// - Afterware
// - func(context.Context, mutil.WriterProxy, *http.Request) context.Context
// - func(context.Context, http.ResponseWriter, *http.Request) context.Context
// - func(context.Context, *http.Request) context.Context
// - func(context.Context) context.Context
// - Middleware types
// The old x/net/context is also supported.
type AfterwareType interface{}
// run runs the middleware chain for a particular request.
// run returns false if it should stop early.
func (m *wares) run(ctx context.Context, w http.ResponseWriter, r *http.Request) (*http.Request, context.Context, bool) {
if m.middleware != nil {
// hierarchical middleware
for i, c := range r.URL.Path {
if c == '/' || i == len(r.URL.Path)-1 {
mws, ok := m.middleware[r.URL.Path[:i+1]]
if !ok {
continue
}
for _, mw := range mws {
// return nil context to stop
result := mw(ctx, w, r)
if result == nil {
return r, ctx, false
}
if result != ctx {
r = r.WithContext(result)
}
ctx = result
}
}
}
}
if m.wildcards != nil {
// wildcard middleware
if wild, params := m.wildcards.Get(r.URL.Path); wild != nil {
if mws, ok := wild.(*[]Middleware); ok {
ctx = mergeParams(ctx, params)
r = r.WithContext(ctx)
for _, mw := range *mws {
result := mw(ctx, w, r)
if result == nil {
return r, ctx, false
}
if result != ctx {
r = r.WithContext(result)
}
ctx = result
}
}
}
}
return r, ctx, true
}
// after runs the afterware chain for a particular request.
// after can't stop early
func (m *wares) after(ctx context.Context, w mutil.WriterProxy, r *http.Request) (*http.Request, context.Context) {
if m.afterWildcards != nil {
// wildcard afterware
if wild, params := m.afterWildcards.Get(r.URL.Path); wild != nil {
if aws, ok := wild.(*[]Afterware); ok {
ctx = mergeParams(ctx, params)
r = r.WithContext(ctx)
for _, aw := range *aws {
result := aw(ctx, w, r)
if result != nil {
if result != ctx {
r = r.WithContext(result)
}
ctx = result
}
}
}
}
}
if m.afterware != nil {
// hierarchical afterware, like middleware in reverse
path := r.URL.Path
for len(path) > 0 {
chr, size := utf8.DecodeLastRuneInString(path)
if chr == '/' || len(path) == len(r.URL.Path) {
for _, aw := range m.afterware[path] {
result := aw(ctx, w, r)
if result != nil {
if result != ctx {
r = r.WithContext(result)
}
ctx = result
}
}
}
path = path[:len(path)-size]
}
}
return r, ctx
}
// convert turns standard http middleware into kami Middleware if needed.
func convert(mw MiddlewareType) Middleware {
switch x := mw.(type) {
case Middleware:
return x
case func(context.Context, http.ResponseWriter, *http.Request) context.Context:
return Middleware(x)
case func(netcontext.Context, http.ResponseWriter, *http.Request) netcontext.Context:
return Middleware(func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return x(ctx, w, r)
})
case func(ContextHandler) ContextHandler:
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
var dh dummyHandler
x(&dh).ServeHTTPContext(ctx, w, r)
if !dh {
return nil
}
return ctx
}
case func(OldContextHandler) OldContextHandler:
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
var dh oldDummyHandler
x(&dh).ServeHTTPContext(ctx, w, r)
if !dh {
return nil
}
return ctx
}
case func(http.Handler) http.Handler:
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
var dh dummyHandler
x(&dh).ServeHTTP(w, r)
if !dh {
return nil
}
return ctx
}
case http.Handler:
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
x.ServeHTTP(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request):
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context {
return x(w, r)
})
}
panic(fmt.Errorf("unsupported MiddlewareType: %T", mw))
}
// convertAW
func convertAW(aw AfterwareType) Afterware {
switch x := aw.(type) {
case Afterware:
return x
case func(context.Context, mutil.WriterProxy, *http.Request) context.Context:
return Afterware(x)
case func(netcontext.Context, mutil.WriterProxy, *http.Request) netcontext.Context:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(context.Context, *http.Request) context.Context:
return func(ctx context.Context, _ mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, r)
}
case func(netcontext.Context, *http.Request) netcontext.Context:
return func(ctx context.Context, _ mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, r)
}
case func(context.Context) context.Context:
return func(ctx context.Context, _ mutil.WriterProxy, _ *http.Request) context.Context {
return x(ctx)
}
case func(netcontext.Context) netcontext.Context:
return func(ctx context.Context, _ mutil.WriterProxy, _ *http.Request) context.Context {
return x(ctx)
}
case Middleware:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(context.Context, http.ResponseWriter, *http.Request) context.Context:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(netcontext.Context, http.ResponseWriter, *http.Request) netcontext.Context:
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(ctx, w, r)
}
case func(w http.ResponseWriter, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case func(w mutil.WriterProxy, r *http.Request) context.Context:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
return x(w, r)
})
case http.Handler:
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x.ServeHTTP(w, r)
return r.Context()
})
case func(w http.ResponseWriter, r *http.Request):
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
case func(w mutil.WriterProxy, r *http.Request):
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context {
x(w, r)
return r.Context()
})
}
panic(fmt.Errorf("unsupported AfterwareType: %T", aw))
}
// dummyHandler is used to keep track of whether the next middleware was called or not.
type dummyHandler bool
func (dh *dummyHandler) ServeHTTP(http.ResponseWriter, *http.Request) {
*dh = true
}
func (dh *dummyHandler) ServeHTTPContext(_ context.Context, _ http.ResponseWriter, _ *http.Request) {
*dh = true
}
// oldDummyHandler is dummyHandler compatible with the old context type.
type oldDummyHandler bool
func (dh *oldDummyHandler) ServeHTTP(http.ResponseWriter, *http.Request) {
*dh = true
}
func (dh *oldDummyHandler) ServeHTTPContext(_ netcontext.Context, _ http.ResponseWriter, _ *http.Request) {
*dh = true
}