-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
53 lines (45 loc) · 1.4 KB
/
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
package logger
import (
"net/http"
)
// ResponseWriterWrapper wraps around the http response writer.
type responseWriterWrapper struct {
status int
size int
http.ResponseWriter
}
// Status returns the status code of the response.
func (wr *responseWriterWrapper) Status() int {
return wr.status
}
// Size returns the size of the body of the response.
func (wr *responseWriterWrapper) Size() int {
return wr.size
}
// Header returns the http.Header of the wrapped response writer.
func (wr *responseWriterWrapper) Header() http.Header {
return wr.ResponseWriter.Header()
}
// Write captures the size and forwards it to the wrapper.
func (wr *responseWriterWrapper) Write(b []byte) (int, error) {
if wr.status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
wr.status = http.StatusOK
}
size, err := wr.ResponseWriter.Write(b)
wr.size += size
return size, err
}
// WriteHeader captures the status and forwards it to the wrapper.
func (wr *responseWriterWrapper) WriteHeader(status int) {
wr.status = status
wr.ResponseWriter.WriteHeader(status)
}
// // Hijack makes the responseWriterWrapper conform to the hijacking interface
// func (wr *responseWriterWrapper) Hijack() (net.Conn, *bufio.ReadWriter, error) {
// hj, ok := wr.ResponseWriter.(http.Hijacker)
// if !ok {
// return nil, nil, fmt.Errorf("ResponseWriter does not support Hijack")
// }
// return hj.Hijack()
// }