-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
304 lines (254 loc) · 7.04 KB
/
response.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
package ep
import (
"io"
"log"
"net/http"
"reflect"
"github.com/advanderveer/ep/epcoding"
)
// ResponseWriter extends the traditional http.ResponseWriter interface with
// functionality that standardizes input decoding and output enepcoding.
type ResponseWriter interface {
Bind(in interface{}) bool
Render(outs ...interface{})
Recover()
http.ResponseWriter
}
type response struct {
http.ResponseWriter
req *http.Request
reqHooks []RequestHook
resHooks []ResponseHook
errHooks []ErrorHook
enc epcoding.Encoder
dec epcoding.Decoder
encContentType string
encNegotiateErr error
decNegotiateErr error
wroteHeader bool
runningReqHooks bool
currentOutput interface{}
}
func newResponse(
w http.ResponseWriter,
r *http.Request,
reqh []RequestHook,
resh []ResponseHook,
errh []ErrorHook,
decs []epcoding.Decoding,
encs []epcoding.Encoding,
) *response {
res := &response{
ResponseWriter: w,
req: r,
reqHooks: reqh,
resHooks: resh,
errHooks: errh,
}
// any failure to negotiate is only important if we actually wanna decode
// something during a call to bind
res.dec, res.decNegotiateErr = negotiateDecoder(res.req, decs)
// Failing to negotiate an encoder is only important when we know for sure
// that it will be used during a call to render. The user might decide to
// write to the response itself, or the API doesn't need encoding at all.
// So we keep the error in the response to be reported later.
res.enc, res.encContentType, res.encNegotiateErr = negotiateEncoder(res.req, res, encs)
return res
}
// NewResponse initializes a ResponseWriter
func NewResponse(
w http.ResponseWriter,
r *http.Request,
reqh []RequestHook,
resh []ResponseHook,
errh []ErrorHook,
decs []epcoding.Decoding,
encs []epcoding.Encoding,
) ResponseWriter {
return newResponse(w, r, reqh, resh, errh, decs, encs)
}
// Write some data to the response body. If the header was not yet written this
// method will make an implicit call to WriteHeader which will call any hooks
// in order.
func (res *response) Write(b []byte) (int, error) {
if !res.wroteHeader {
res.WriteHeader(http.StatusOK)
}
return res.ResponseWriter.Write(b)
}
// WriteHeader will call any configured hooks and sends the http response header
// with the resulting status code.
func (res *response) WriteHeader(statusCode int) {
if res.wroteHeader {
return
}
if !res.runningReqHooks {
res.runningReqHooks = true
defer func() { res.runningReqHooks = false }()
for _, h := range res.resHooks {
h(
res,
res.req,
res.currentOutput, // might be nil
)
}
}
// this check ensures that if any hooks called writeHeader we won't be
// calling it again.
if res.wroteHeader {
return
}
res.ResponseWriter.WriteHeader(statusCode)
res.wroteHeader = true
}
// Bind will decode the next value from the request into the input 'in'
func (res *response) Bind(in interface{}) bool {
ok, err := res.bind(in)
if err != nil {
res.Render(nil, err)
return false
}
return ok
}
func (res *response) bind(in interface{}) (ok bool, err error) {
const op Op = "response.bind"
for _, h := range res.reqHooks {
if err := h(res.req, in); err != nil {
return false, Err(op, "request hook failed", err, RequestHookError)
}
}
// if the input is nil or has an SkipDecode() method we skip decoding
switch vt := in.(type) {
case nil:
return true, nil
case interface{ SkipDecode() bool }:
if vt.SkipDecode() {
return true, nil
}
}
if res.decNegotiateErr != nil {
return false, res.decNegotiateErr
}
if res.dec == nil {
return true, nil
}
err = res.dec.Decode(in)
if err == io.EOF {
return false, nil
} else if err != nil {
return false, Err(op, "request body decoder failed", err, DecoderError)
}
return true, nil
}
// Render will encode the first non-nil argument into the response body. If any
// of the arguments is an error, it takes precedence and is rendered instead.
func (res *response) Render(outs ...interface{}) {
var out interface{}
for _, o := range outs {
switch o.(type) {
case nil:
case error:
out = o
default:
if out != nil {
continue
}
// as a special exception it should also skip arguments that are nil
// pointers. Overhead has been benchmarked at 3-4ns
if rv := reflect.ValueOf(o); rv.Kind() == reflect.Ptr && rv.IsNil() {
continue
}
out = o
}
}
err := res.render(out) // first pass
if err != nil {
err = res.render(err) // second pass
if err != nil {
panic("ep/response: failed to render: " + err.Error())
}
}
}
// render just the output value
func (res *response) render(v interface{}) (err error) {
const op Op = "response.render"
if errv, ok := v.(error); ok {
// If there was an error but no hooks to turn it into an output
// we log this situation to the default logger so the user knows
// whats going on.
if len(res.errHooks) < 1 {
log.Printf("ep: no error hooks to render error: %v", errv)
}
// Error hooks are responsible for turning any error into an output
// that can be rendered by the encoder.
// var foundErrOutput bool
for _, h := range res.errHooks {
if eout := h(errv); eout != nil {
v = eout
break
}
}
}
// WriteHeader needs access to the output value but the interface refrains
// us from passing it as an argument so we need to set it as an temporary
// struct member.
res.currentOutput = v
defer func() { res.currentOutput = nil }()
// If the value turns out to be nil or implements the Empty() method, we won't
// be needing any encoder but still wanna write the header and call any
// response hooks.
switch vt := v.(type) {
case nil:
res.WriteHeader(http.StatusOK)
return nil
case interface{ Empty() bool }:
if vt.Empty() {
res.WriteHeader(http.StatusOK)
return nil
}
}
// We for sure have a value to encode, so if we had any issues with getting
// an encoder we will stop here
if res.encNegotiateErr != nil {
return res.encNegotiateErr
}
// The encoder is nil without any enc negotiation error
if res.enc == nil {
return Err(op, "no encoder to serialize non-nil output value", ServerError)
}
var ctFromEnc bool
if res.Header().Get("Content-Type") == "" {
ctFromEnc = true
res.Header().Set("Content-Type", res.encContentType)
// we know the content-type for sure so we can prevent content sniffing
res.Header().Set("X-Content-Type-Options", "nosniff")
}
err = res.enc.Encode(v)
if err != nil {
// If we just added the content-type header but the encoding fails we
// reset it such that a subsequent call to render can set it again.
if ctFromEnc {
res.Header().Del("Content-Type")
res.Header().Del("X-Content-Type-Options")
}
return Err(op, "response body encoder failed", err, EncoderError)
}
return
}
func (res *response) Recover() {
r := recover()
if r == nil {
return
}
var perr error
switch rt := r.(type) {
case error:
perr = Err(Op("response.Recover"), "error", ServerError, rt)
case string:
perr = Err(Op("response.Recover"), rt, ServerError)
default:
perr = Err(Op("response.Recover"), "unknown panic", ServerError)
return
}
res.Render(nil, perr)
}