-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmiddleware_17.go
145 lines (137 loc) · 4.81 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
// +build go1.7,!go1.9
package kami
import (
"context"
"fmt"
"net/http"
"github.com/zenazn/goji/web/mutil"
netcontext "golang.org/x/net/context"
)
// 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))
}
// 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
}