-
Notifications
You must be signed in to change notification settings - Fork 0
/
capture_response_writer.go
95 lines (82 loc) · 2.06 KB
/
capture_response_writer.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
package events
import (
"bufio"
"fmt"
"net"
"net/http"
"time"
)
type CaptureResponseWriter struct {
http.ResponseWriter
StatusCode int
ResponseLength int64
Start time.Time
WriteStart time.Time
Hijacked bool
HeaderWritten bool
}
func NewCaptureResponseWriter(w http.ResponseWriter) *CaptureResponseWriter {
return &CaptureResponseWriter{
ResponseWriter: w,
StatusCode: http.StatusOK,
ResponseLength: 0,
Start: time.Now().UTC(),
Hijacked: false,
HeaderWritten: false,
}
}
func (w *CaptureResponseWriter) Write(buf []byte) (int, error) {
if w.WriteStart.IsZero() {
w.WriteStart = time.Now()
}
if !w.HeaderWritten {
w.HeaderWritten = true
}
if w.Hijacked {
panic("Write on hijacked CaptureResponseWriter")
}
n, err := w.ResponseWriter.Write(buf)
w.ResponseLength += int64(n)
return n, err
}
func (w *CaptureResponseWriter) WriteHeader(statusCode int) {
if w.HeaderWritten {
panic("header already written")
}
if w.Hijacked {
panic("WriteHeader on hijacked CaptureResponseWriter")
}
w.StatusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
w.HeaderWritten = true
}
func (w *CaptureResponseWriter) Duration() time.Duration {
return time.Since(w.Start)
}
func (w *CaptureResponseWriter) WriteDuration() time.Duration {
if w.WriteStart.IsZero() {
return 0
}
return time.Since(w.WriteStart)
}
func (w *CaptureResponseWriter) Flush() {
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func (w *CaptureResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := w.ResponseWriter.(http.Hijacker); ok {
w.Hijacked = true
return hijacker.Hijack()
}
return nil, nil, fmt.Errorf("%w", http.ErrNotSupported)
}
func (w *CaptureResponseWriter) SetWriteDeadline(t time.Time) error {
if d, ok := w.ResponseWriter.(interface{ SetWriteDeadline(time.Time) error }); ok {
return d.SetWriteDeadline(t)
}
return fmt.Errorf("%w", http.ErrNotSupported)
}
func (w *CaptureResponseWriter) Unwrap() http.ResponseWriter {
return w.ResponseWriter
}