-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_stat.go
52 lines (41 loc) · 938 Bytes
/
response_stat.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
package mrserver
import (
"net/http"
)
const (
traceResponseBodyMaxLen = 1024
)
type (
StatResponseWriter struct {
http.ResponseWriter
statusCode int
bytes int
onWrite func(buf []byte)
}
)
func NewStatResponseWriter(w http.ResponseWriter, onWrite func(buf []byte)) *StatResponseWriter {
return &StatResponseWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
onWrite: onWrite,
}
}
func (w *StatResponseWriter) WriteHeader(statusCode int) {
w.statusCode = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *StatResponseWriter) Write(buf []byte) (int, error) {
bytes, err := w.ResponseWriter.Write(buf)
w.bytes += bytes
if len(buf) > traceResponseBodyMaxLen {
buf = buf[0:traceResponseBodyMaxLen]
}
w.onWrite(buf)
return bytes, err
}
func (w *StatResponseWriter) Bytes() int {
return w.bytes
}
func (w *StatResponseWriter) StatusCode() int {
return w.statusCode
}