-
-
Notifications
You must be signed in to change notification settings - Fork 552
/
capture.go
61 lines (52 loc) Β· 1.61 KB
/
capture.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
package middleware
import (
"bufio"
"errors"
"fmt"
"net"
"net/http"
)
// ResponseCapture is a http.ResponseWriter which captures the response status
// code and content length.
type ResponseCapture struct {
http.ResponseWriter
StatusCode int
ContentLength int
}
// CaptureResponse creates a ResponseCapture that wraps the given ResponseWriter.
func CaptureResponse(w http.ResponseWriter) *ResponseCapture {
return &ResponseCapture{ResponseWriter: w}
}
// WriteHeader records the value of the status code before writing it.
func (w *ResponseCapture) WriteHeader(code int) {
w.StatusCode = code
w.ResponseWriter.WriteHeader(code)
}
// Write computes the written len and stores it in ContentLength.
func (w *ResponseCapture) Write(b []byte) (int, error) {
n, err := w.ResponseWriter.Write(b)
w.ContentLength += n
return n, err
}
// Flush implements the http.Flusher interface if the underlying response
// writer supports it.
func (w *ResponseCapture) Flush() {
if f, ok := w.ResponseWriter.(http.Flusher); ok {
f.Flush()
}
}
// Push implements the http.Pusher interface if the underlying response
// writer supports it.
func (w *ResponseCapture) Push(target string, opts *http.PushOptions) error {
if p, ok := w.ResponseWriter.(http.Pusher); ok {
return p.Push(target, opts)
}
return errors.New("push not supported")
}
// Hijack supports the http.Hijacker interface.
func (w *ResponseCapture) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if h, ok := w.ResponseWriter.(http.Hijacker); ok {
return h.Hijack()
}
return nil, nil, fmt.Errorf("response writer does not support hijacking: %T", w.ResponseWriter)
}