forked from thoas/stats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
recorder.go
60 lines (49 loc) · 1.15 KB
/
recorder.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
package stats
import (
"bufio"
"fmt"
"net"
"net/http"
)
type Recorder interface {
http.ResponseWriter
Status() int
}
type RecorderResponseWriter struct {
http.ResponseWriter
status int
size int
}
func (r *RecorderResponseWriter) WriteHeader(code int) {
r.ResponseWriter.WriteHeader(code)
r.status = code
}
func (r *RecorderResponseWriter) Flush() {
flusher, ok := r.ResponseWriter.(http.Flusher)
if ok {
flusher.Flush()
}
}
func (r *RecorderResponseWriter) Status() int {
return r.status
}
// Proxy method to Status to add support for gocraft
func (r *RecorderResponseWriter) StatusCode() int {
return r.Status()
}
func (r *RecorderResponseWriter) Size() int {
return r.size
}
func (r *RecorderResponseWriter) Written() bool {
return r.StatusCode() != 0
}
func (r *RecorderResponseWriter) CloseNotify() <-chan bool {
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
}
func (r *RecorderResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hijacker, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, fmt.Errorf("the ResponseWriter doesn't support the Hijacker interface")
}
return hijacker.Hijack()
}