forked from vicanso/pike
/
response.go
65 lines (55 loc) · 1.07 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
package pike
import (
"bytes"
"net/http"
)
type (
// Response http response
Response struct {
body *bytes.Buffer
headers http.Header
code int
Committed bool
}
)
// NewResponse create a response
func NewResponse() *Response {
return &Response{
body: new(bytes.Buffer),
headers: make(http.Header),
code: http.StatusNotFound,
}
}
// WriteHeader write header
func (w *Response) WriteHeader(code int) {
w.code = code
}
// Header get header
func (w *Response) Header() http.Header {
return w.headers
}
// Write write buffer
func (w *Response) Write(b []byte) (int, error) {
return w.body.Write(b)
}
// Status get the response status
func (w *Response) Status() int {
return w.code
}
// Size get the response size
func (w *Response) Size() int {
return w.body.Len()
}
// Reset reset the response sturct
func (w *Response) Reset() {
w.body.Reset()
w.code = http.StatusNotFound
w.Committed = false
for k := range w.headers {
delete(w.headers, k)
}
}
// Bytes get the bytes of response
func (w *Response) Bytes() []byte {
return w.body.Bytes()
}