-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponse_writer_std.go
46 lines (35 loc) · 1022 Bytes
/
response_writer_std.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
package xun
import "net/http"
// stdResponseWriter is a wrapper around http.ResponseWriter to implement the ResponseWriter interface.
type stdResponseWriter struct {
http.ResponseWriter
bodySentBytes int
statusCode int
}
// Close implements the ResponseWriter interface Close method.
// It is a no-op for the standard response writer.
func (*stdResponseWriter) Close() {
}
func (rw *stdResponseWriter) WriteHeader(statusCode int) {
if rw.statusCode == 0 {
rw.statusCode = statusCode
rw.ResponseWriter.WriteHeader(statusCode)
}
}
func (rw *stdResponseWriter) StatusCode() int {
if rw.statusCode == 0 {
return http.StatusOK
}
return rw.statusCode
}
func (rw *stdResponseWriter) BodyBytesSent() int {
return rw.bodySentBytes
}
func (rw *stdResponseWriter) Write(b []byte) (int, error) {
n, err := rw.ResponseWriter.Write(b)
rw.bodySentBytes = rw.bodySentBytes + n
return n, err
}
func NewResponseWriter(rw http.ResponseWriter) ResponseWriter {
return &stdResponseWriter{ResponseWriter: rw}
}