-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathextended_writer.go
More file actions
37 lines (32 loc) · 885 Bytes
/
extended_writer.go
File metadata and controls
37 lines (32 loc) · 885 Bytes
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
package golax
import "net/http"
// ExtendedWriter wraps http.ResponseWriter with StatusCode & Length
type ExtendedWriter struct {
StatusCode int
statusCodeSent bool
Length int
http.ResponseWriter
}
// NewExtendedWriter instances a new *ExtendedWriter
func NewExtendedWriter(w http.ResponseWriter) *ExtendedWriter {
return &ExtendedWriter{
StatusCode: 200,
statusCodeSent: false,
Length: 0,
ResponseWriter: w,
}
}
// Write replaces default behaviour of http.ResponseWriter
func (w *ExtendedWriter) Write(p []byte) (int, error) {
n, err := w.ResponseWriter.Write(p)
w.Length += n
return n, err
}
// WriteHeader replaces default behaviour of http.ResponseWriter
func (w *ExtendedWriter) WriteHeader(statusCode int) {
w.StatusCode = statusCode
if !w.statusCodeSent {
w.ResponseWriter.WriteHeader(statusCode)
w.statusCodeSent = true
}
}