forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_recorder.go
390 lines (339 loc) · 11.4 KB
/
response_recorder.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package context
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"strconv"
"sync"
)
// Recorder the middleware to enable response writer recording ( ResponseWriter -> ResponseRecorder)
var Recorder = func(ctx *Context) {
ctx.Record()
ctx.Next()
}
var rrpool = sync.Pool{New: func() interface{} { return &ResponseRecorder{} }}
// AcquireResponseRecorder returns a new *AcquireResponseRecorder from the pool.
// Releasing is done automatically when request and response is done.
func AcquireResponseRecorder() *ResponseRecorder {
return rrpool.Get().(*ResponseRecorder)
}
func releaseResponseRecorder(w *ResponseRecorder) {
rrpool.Put(w)
}
// A ResponseRecorder is used mostly for testing
// in order to record and modify, if necessary, the body and status code and headers.
//
// See `context.Recorder“ method too.
type ResponseRecorder struct {
ResponseWriter
// keep track of the body written.
chunks []byte
// the saved headers
headers http.Header
result *http.Response
}
var _ ResponseWriter = (*ResponseRecorder)(nil)
// Naive returns the simple, underline and original http.ResponseWriter
// that backends this response writer.
func (w *ResponseRecorder) Naive() http.ResponseWriter {
return w.ResponseWriter.Naive()
}
// BeginRecord accepts its parent ResponseWriter and
// prepares itself, the response recorder, to record and send response to the client.
func (w *ResponseRecorder) BeginRecord(underline ResponseWriter) {
w.ResponseWriter = underline
w.headers = underline.Header().Clone()
w.result = nil
w.ResetBody()
}
// EndResponse is auto-called when the whole client's request is done,
// releases the response recorder and its underline ResponseWriter.
func (w *ResponseRecorder) EndResponse() {
w.ResponseWriter.EndResponse()
releaseResponseRecorder(w)
}
// Write Adds the contents to the body reply, it writes the contents temporarily
// to a value in order to be flushed at the end of the request,
// this method give us the opportunity to reset the body if needed.
//
// If WriteHeader has not yet been called, Write calls
// WriteHeader(http.StatusOK) before writing the data. If the Header
// does not contain a Content-Type line, Write adds a Content-Type set
// to the result of passing the initial 512 bytes of written data to
// DetectContentType.
//
// Depending on the HTTP protocol version and the client, calling
// Write or WriteHeader may prevent future reads on the
// Request.Body. For HTTP/1.x requests, handlers should read any
// needed request body data before writing the response. Once the
// headers have been flushed (due to either an explicit Flusher.Flush
// call or writing enough data to trigger a flush), the request body
// may be unavailable. For HTTP/2 requests, the Go HTTP server permits
// handlers to continue to read the request body while concurrently
// writing the response. However, such behavior may not be supported
// by all HTTP/2 clients. Handlers should read before writing if
// possible to maximize compatibility.
func (w *ResponseRecorder) Write(contents []byte) (int, error) {
w.chunks = append(w.chunks, contents...)
// Remember that we should not return all the written length within `Write`:
// see https://github.com/kataras/iris/pull/931
return len(contents), nil
}
// Header returns the temporary header map that, on flush response,
// will be sent by the underline's ResponseWriter's WriteHeader method.
func (w *ResponseRecorder) Header() http.Header {
return w.headers
}
// SetBody overrides the body and sets it to a slice of bytes value.
func (w *ResponseRecorder) SetBody(b []byte) {
w.chunks = b
}
// SetBodyString overrides the body and sets it to a string value.
func (w *ResponseRecorder) SetBodyString(s string) {
w.SetBody([]byte(s))
}
// Body returns the body tracked from the writer so far,
// do not use this for edit.
func (w *ResponseRecorder) Body() []byte {
return w.chunks
}
// ResetBody resets the response body.
func (w *ResponseRecorder) ResetBody() {
w.chunks = w.chunks[0:0]
}
// ResetHeaders sets the headers to the underline's response writer's headers, may empty.
func (w *ResponseRecorder) ResetHeaders() {
w.headers = w.ResponseWriter.Header().Clone()
}
// ClearHeaders clears all headers, both temp and underline's response writer.
func (w *ResponseRecorder) ClearHeaders() {
w.headers = http.Header{}
h := w.ResponseWriter.Header()
for k := range h {
delete(h, k)
}
}
// Reset clears headers, sets the status code to 200
// and clears the cached body.
//
// - Use ResetBody() and ResetHeaders() instead to keep compression after reseting.
//
// - Use Reset() & ResponseRecorder.ResponseWriter.(*context.CompressResponseWriter).Disabled = true
// to set a new body without compression when the previous handler was iris.Compression.
//
// Implements the `ResponseWriterReseter`.
func (w *ResponseRecorder) Reset() bool {
w.ClearHeaders()
w.WriteHeader(defaultStatusCode)
w.ResetBody()
return true
}
// FlushResponse the full body, headers and status code to the underline response writer
// called automatically at the end of each request.
func (w *ResponseRecorder) FlushResponse() {
// copy the headers to the underline response writer
if w.headers != nil {
h := w.ResponseWriter.Header()
// note: we don't reset the current underline's headers.
for k, v := range w.headers {
h[k] = v
}
}
cw, mustWriteToClose := w.ResponseWriter.(*CompressResponseWriter)
if mustWriteToClose { // see #1569#issuecomment-664003098
cw.FlushHeaders()
} else {
// NOTE: before the ResponseWriter.Write in order to:
// set the given status code even if the body is empty.
w.ResponseWriter.FlushResponse()
}
if len(w.chunks) > 0 {
// ignore error
w.ResponseWriter.Write(w.chunks)
}
if mustWriteToClose {
cw.ResponseWriter.FlushResponse()
cw.CompressWriter.Close()
}
}
// Clone returns a clone of this response writer
// it copies the header, status code, headers and the beforeFlush finally returns a new ResponseRecorder
func (w *ResponseRecorder) Clone() ResponseWriter {
wc := &ResponseRecorder{}
// copy headers.
wc.headers = w.headers.Clone()
// copy body.
chunksCopy := make([]byte, len(w.chunks))
copy(chunksCopy, w.chunks)
wc.chunks = chunksCopy
if resW, ok := w.ResponseWriter.(*responseWriter); ok {
wc.ResponseWriter = &responseWriter{
ResponseWriter: resW.ResponseWriter,
statusCode: resW.statusCode,
written: resW.written,
beforeFlush: resW.beforeFlush,
} // clone it
} else { // else just copy, may pointer, developer can change its behavior
wc.ResponseWriter = w.ResponseWriter
}
return wc
}
// CopyTo writes a response writer (temp: status code, headers and body) to another response writer
func (w *ResponseRecorder) CopyTo(res ResponseWriter) {
if to, ok := res.(*ResponseRecorder); ok {
// set the status code, to is first ( probably an error? (context.StatusCodeNotSuccessful, defaults to >=400).
if statusCode := w.ResponseWriter.StatusCode(); statusCode == defaultStatusCode {
to.WriteHeader(statusCode)
}
if beforeFlush := w.ResponseWriter.GetBeforeFlush(); beforeFlush != nil {
// if to had a before flush, lets combine them
if to.GetBeforeFlush() != nil {
nextBeforeFlush := beforeFlush
prevBeforeFlush := to.GetBeforeFlush()
to.SetBeforeFlush(func() {
prevBeforeFlush()
nextBeforeFlush()
})
} else {
to.SetBeforeFlush(w.ResponseWriter.GetBeforeFlush())
}
}
// if "to" is *responseWriter and it never written before (if -1),
// set the "w"'s written length.
if resW, ok := to.ResponseWriter.(*responseWriter); ok {
if resW.Written() != StatusCodeWritten {
resW.written = w.ResponseWriter.Written()
}
}
// append the headers
if w.headers != nil {
for k, values := range w.headers {
for _, v := range values {
if to.headers.Get(v) == "" {
to.headers.Add(k, v)
}
}
}
}
// append the body
if len(w.chunks) > 0 {
// ignore error
to.Write(w.chunks)
}
}
}
// Flush sends any buffered data to the client.
func (w *ResponseRecorder) Flush() {
// This fixes response recorder when chunked + Flush is used.
if w.headers.Get("Transfer-Encoding") == "chunked" {
if w.Written() == NoWritten {
if len(w.headers) > 0 {
h := w.ResponseWriter.Header()
// note: we don't reset the current underline's headers.
for k, v := range w.headers {
h[k] = v
}
}
}
if len(w.chunks) > 0 {
w.ResponseWriter.Write(w.chunks)
}
}
w.ResponseWriter.Flush()
w.ResetBody()
}
// ErrPushNotSupported is returned by the Push method to
// indicate that HTTP/2 Push support is not available.
var ErrPushNotSupported = errors.New("push feature is not supported by this ResponseWriter")
// Push initiates an HTTP/2 server push. This constructs a synthetic
// request using the given target and options, serializes that request
// into a PUSH_PROMISE frame, then dispatches that request using the
// server's request handler. If opts is nil, default options are used.
//
// The target must either be an absolute path (like "/path") or an absolute
// URL that contains a valid host and the same scheme as the parent request.
// If the target is a path, it will inherit the scheme and host of the
// parent request.
//
// The HTTP/2 spec disallows recursive pushes and cross-authority pushes.
// Push may or may not detect these invalid pushes; however, invalid
// pushes will be detected and canceled by conforming clients.
//
// Handlers that wish to push URL X should call Push before sending any
// data that may trigger a request for URL X. This avoids a race where the
// client issues requests for X before receiving the PUSH_PROMISE for X.
//
// Push returns ErrPushNotSupported if the client has disabled push or if push
// is not supported on the underlying connection.
func (w *ResponseRecorder) Push(target string, opts *http.PushOptions) (err error) {
w.FlushResponse()
if pusher, ok := w.ResponseWriter.Naive().(http.Pusher); ok {
err = pusher.Push(target, opts)
if err != nil && err.Error() == http.ErrNotSupported.ErrorString {
return ErrPushNotSupported
}
}
// NOTE: we have to reset them even if the push failed.
w.ResetBody()
w.ResetHeaders()
return ErrPushNotSupported
}
// Result returns the response generated by the handler.
// It does set all provided headers.
//
// Result must only be called after the handler has finished running.
func (w *ResponseRecorder) Result() *http.Response { // a modified copy of net/http/httptest
if w.result != nil {
return w.result
}
headers := w.headers.Clone()
// for k, v := range w.ResponseWriter.Header() {
// headers[k] = v
// }
/*
dateFound := false
for k := range headers {
if strings.ToLower(k) == "date" {
dateFound = true
break
}
}
if !dateFound {
headers["Date"] = []string{time.Now().Format(http.TimeFormat)}
}
*/
res := &http.Response{
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
StatusCode: w.StatusCode(),
Header: headers,
}
if res.StatusCode == 0 {
res.StatusCode = 200
}
res.Status = fmt.Sprintf("%03d %s", res.StatusCode, http.StatusText(res.StatusCode))
if w.chunks != nil {
res.Body = io.NopCloser(bytes.NewReader(w.chunks))
} else {
res.Body = http.NoBody
}
res.ContentLength = parseContentLength(res.Header.Get("Content-Length"))
w.result = res
return res
}
// copy of net/http/httptest
func parseContentLength(cl string) int64 {
cl = textproto.TrimString(cl)
if cl == "" {
return -1
}
n, err := strconv.ParseUint(cl, 10, 63)
if err != nil {
return -1
}
return int64(n)
}