forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
69 lines (57 loc) · 1.27 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
package echo
import (
"bufio"
"log"
"net"
"net/http"
"github.com/labstack/gommon/color"
)
type (
Response struct {
Writer http.ResponseWriter
status int
size int64
committed bool
}
)
func (r *Response) Header() http.Header {
return r.Writer.Header()
}
func (r *Response) WriteHeader(code int) {
if r.committed {
// TODO: Warning
log.Printf("echo => %s", color.Yellow("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) reset(w http.ResponseWriter) {
r.Writer = w
r.status = http.StatusOK
r.committed = false
}