-
Notifications
You must be signed in to change notification settings - Fork 6
/
response_recorder.go
96 lines (84 loc) · 2.39 KB
/
response_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
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
96
// Copyright (c) 2021, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
import (
"bufio"
"errors"
"io"
"net"
"net/http"
)
// ResponseStatusRecorder implements http.ResponseWriter that keeps tack of HTTP
// response status code and written body size in bytes.
type ResponseStatusRecorder struct {
http.ResponseWriter
status int
size int
}
// NewResponseStatusRecorder wraps an http.ResponseWriter with
// ResponseStatusRecorder in order to record the status code and written body
// size.
func NewResponseStatusRecorder(w http.ResponseWriter) *ResponseStatusRecorder {
return &ResponseStatusRecorder{
ResponseWriter: w,
}
}
// Write implements http.ResponseWriter.
func (r *ResponseStatusRecorder) Write(b []byte) (int, error) {
size, err := r.ResponseWriter.Write(b)
if err != nil {
return 0, err
}
if r.status == 0 {
// The status will be StatusOK if WriteHeader has not been called yet
r.status = http.StatusOK
}
r.size += size
return size, err
}
// WriteHeader implements http.ResponseWriter.
func (r *ResponseStatusRecorder) WriteHeader(s int) {
r.ResponseWriter.WriteHeader(s)
if r.status == 0 {
r.status = s
}
}
// Status returns the responded status code. If it is 0, no response data has
// been written.
func (r *ResponseStatusRecorder) Status() int {
return r.status
}
// ResponseBodySize returns the number of bytes that are written as the response body.
func (r *ResponseStatusRecorder) ResponseBodySize() int {
return r.size
}
func (r *ResponseStatusRecorder) Flush() {
f, ok := r.ResponseWriter.(http.Flusher)
if !ok {
return
}
f.Flush()
}
func (r *ResponseStatusRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) {
h, ok := r.ResponseWriter.(http.Hijacker)
if !ok {
return nil, nil, errors.New("response writer does not implement http.Hijacker")
}
return h.Hijack()
}
func (r *ResponseStatusRecorder) ReadFrom(src io.Reader) (int64, error) {
rf, ok := r.ResponseWriter.(io.ReaderFrom)
if !ok {
return 0, errors.New("response writer does not implement io.ReaderFrom")
}
return rf.ReadFrom(src)
}
func (r *ResponseStatusRecorder) Push(target string, opts *http.PushOptions) error {
p, ok := r.ResponseWriter.(http.Pusher)
if !ok {
return errors.New("response writer does not implement http.Pusher")
}
return p.Push(target, opts)
}