-
Notifications
You must be signed in to change notification settings - Fork 0
/
rescapture.go
51 lines (43 loc) · 1.68 KB
/
rescapture.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
package reqlogger
import "net/http"
// ResponseWriterCapturer wraps an http.ResponseWriter to capture
// the response status code and number of bytes written.
type ResponseWriterCapturer struct {
http.ResponseWriter
statusCode int
bytesWritten int
}
// NewResponseWriterCapturer constructs a new ResponseWriterCapturer,
// wrapping the provided http.ResponseWriter and capturing pertinent information.
func NewResponseWriterCapturer(w http.ResponseWriter) *ResponseWriterCapturer {
return &ResponseWriterCapturer{
ResponseWriter: w,
statusCode: http.StatusOK, // ResponseWriter defaults to OK
}
}
// StatusCode returns the status code written by the wrapped http.ResponseWriter.
func (rwc *ResponseWriterCapturer) StatusCode() int {
return rwc.statusCode
}
// BytesWritten returns the number of bytes written to the wrapped http.ResponseWriter.
func (rwc *ResponseWriterCapturer) BytesWritten() int {
return rwc.bytesWritten
}
// Wrap wraps an http.ResponseWriter. This can be used to reset the
// wrapped response writer when fetching a previously-created instance
// from an instance pool.
func (rwc *ResponseWriterCapturer) Wrap(w http.ResponseWriter) {
rwc.ResponseWriter = w
rwc.statusCode = http.StatusOK
rwc.bytesWritten = 0
}
// Write overrides the ResponseWriter.Write method to capture the number of bytes written.
func (rwc *ResponseWriterCapturer) Write(buf []byte) (int, error) {
rwc.bytesWritten += len(buf)
return rwc.ResponseWriter.Write(buf)
}
// WriteHeader overrides the ResponseWriter.WriteHeader to capture the response status code.
func (rwc *ResponseWriterCapturer) WriteHeader(statusCode int) {
rwc.statusCode = statusCode
rwc.ResponseWriter.WriteHeader(statusCode)
}