-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
57 lines (48 loc) · 968 Bytes
/
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
package jago
import (
"log"
"net/http"
)
type (
Response struct {
Writer http.ResponseWriter
Status int
Size int64
Committed bool
}
)
func NewResponse(w http.ResponseWriter) (r *Response) {
return &Response{Writer: w}
}
func (r *Response) Header() http.Header {
return r.Writer.Header()
}
func (r *Response) WriteHeader(code int) {
if r.Committed {
log.Println("response already committed")
return
}
r.Status = code
r.Writer.WriteHeader(r.Status)
r.Committed = true
}
func (r *Response) SetHeader(key string, val string) {
r.Writer.Header().Add(key, val)
}
func (r *Response) Write(b []byte) (n int, err error) {
if !r.Committed {
if r.Status == 0 {
r.Status = http.StatusOK
}
r.WriteHeader(r.Status)
}
n, err = r.Writer.Write(b)
r.Size += int64(n)
return
}
func (r *Response) SetCookie(cookie *http.Cookie) {
http.SetCookie(r.Writer, cookie)
}
func (r *Response) Flush() {
r.Writer.(http.Flusher).Flush()
}