-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
middleware_old.go
189 lines (171 loc) · 5.67 KB
/
middleware_old.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
// +build !go1.7
package kami
import (
"fmt"
"net/http"
"unicode/utf8"
"github.com/zenazn/goji/web/mutil"
"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.Handler) http.Handler [* see Use docs]
// - func(http.ContextHandler) http.ContextHandler [* see Use docs]
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
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) (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 ctx, false
}
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)
for _, mw := range *mws {
result := mw(ctx, w, r)
if result == nil {
return ctx, false
}
ctx = result
}
}
}
}
return 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) 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)
for _, aw := range *aws {
result := aw(ctx, w, r)
if result != nil {
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 {
ctx = result
}
}
}
path = path[:len(path)-size]
}
}
return 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(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(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
}
}
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(context.Context, *http.Request) context.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 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)
}
}
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
}