-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_writer.go
95 lines (77 loc) · 2.3 KB
/
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 advhttp
import (
"bufio"
"errors"
"net"
"net/http"
"time"
)
// The ResponseWriter structure is a wrapper around http.ResponseWriter
// that provides all the functionality of the respose writer, but adds
// the ability to track what status was written, as well as the number
// of bytes that were written as well.
type ResponseWriter struct {
w http.ResponseWriter
length int64
status int
}
// Creates a new ResponseWriter wrapping the given http.ResponseWriter
func NewResponseWriter(w http.ResponseWriter) *ResponseWriter {
return &ResponseWriter{w: w, length: 0, status: 200}
}
func (trw *ResponseWriter) Header() http.Header {
return trw.w.Header()
}
func (trw *ResponseWriter) WriteHeader(status int) {
trw.status = status
trw.w.WriteHeader(status)
}
func (trw *ResponseWriter) Write(bytes []byte) (int, error) {
n, err := trw.w.Write(bytes)
trw.length += int64(n)
return n, err
}
func (trw *ResponseWriter) Length() int64 {
return trw.length
}
func (trw *ResponseWriter) Status() int {
return trw.status
}
func (trw *ResponseWriter) GetFlusher() (flusher http.Flusher, ok bool) {
flusher, ok = trw.w.(http.Flusher)
return
}
func (trw *ResponseWriter) Flush() {
if flusher, ok := trw.w.(http.Flusher); ok {
flusher.Flush()
}
}
func (trw *ResponseWriter) GetHijacker() (hijacker http.Hijacker, ok bool) {
hijacker, ok = trw.w.(http.Hijacker)
return
}
func (trw *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := trw.w.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, errors.New("Couldn't cast responsewriter to hijacker")
}
func (trw *ResponseWriter) GetCloseNotifier() (closeNotifier http.CloseNotifier, ok bool) {
closeNotifier, ok = trw.w.(http.CloseNotifier)
return
}
func (trw *ResponseWriter) CloseNotify() <-chan bool {
if closeNotifier, ok := trw.w.(http.CloseNotifier); ok {
return closeNotifier.CloseNotify()
}
return nil
}
func (trw *ResponseWriter) LogCommonExtended(r *http.Request) string {
return LogCommonExtended(trw, r)
}
func (trw *ResponseWriter) LogCommonExtendedForwarded(r *http.Request) string {
return LogCommonExtendedForwarded(trw, r)
}
func (trw *ResponseWriter) LogWithOptions(r *http.Request, useXForwarded bool, duration time.Duration) string {
return LogWithOptions(trw, r, useXForwarded, duration)
}