forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
84 lines (69 loc) · 1.53 KB
/
response.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
package echo
import (
"bufio"
"net"
"net/http"
)
type (
Response struct {
writer http.ResponseWriter
status int
size int64
committed bool
echo *Echo
}
)
func NewResponse(w http.ResponseWriter, e *Echo) *Response {
return &Response{writer: w, echo: e}
}
func (r *Response) SetWriter(w http.ResponseWriter) {
r.writer = w
}
func (r *Response) Header() http.Header {
return r.writer.Header()
}
func (r *Response) Writer() http.ResponseWriter {
return r.writer
}
func (r *Response) WriteHeader(code int) {
if r.committed {
r.echo.Logger().Warn("response already committed")
return
}
r.status = code
r.writer.WriteHeader(code)
r.committed = true
}
func (r *Response) Write(b []byte) (n int, err error) {
n, err = r.writer.Write(b)
r.size += int64(n)
return n, err
}
// Flush wraps response writer's Flush function.
func (r *Response) Flush() {
r.writer.(http.Flusher).Flush()
}
// Hijack wraps response writer's Hijack function.
func (r *Response) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.writer.(http.Hijacker).Hijack()
}
// CloseNotify wraps response writer's CloseNotify function.
func (r *Response) CloseNotify() <-chan bool {
return r.writer.(http.CloseNotifier).CloseNotify()
}
func (r *Response) Status() int {
return r.status
}
func (r *Response) Size() int64 {
return r.size
}
func (r *Response) Committed() bool {
return r.committed
}
func (r *Response) reset(w http.ResponseWriter, e *Echo) {
r.writer = w
r.size = 0
r.status = http.StatusOK
r.committed = false
r.echo = e
}