-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmiddleware_new.go
151 lines (135 loc) · 4.53 KB
/
middleware_new.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
// +build go1.7
package kami
import (
"context"
"net/http"
"unicode/utf8"
"github.com/zenazn/goji/web/mutil"
)
// 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
}
// 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
}