-
Notifications
You must be signed in to change notification settings - Fork 4
/
src_response.go
67 lines (59 loc) · 1.18 KB
/
src_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
package hlfhr
import (
"bytes"
"io"
"net/http"
"time"
)
type response struct {
resp http.Response
body *bytes.Buffer
}
func newResponse() *response {
return &response{
resp: http.Response{
ProtoMajor: 1,
ProtoMinor: 1,
Close: true,
Header: http.Header{
"Date": []string{time.Now().UTC().Format(http.TimeFormat)},
},
},
}
}
func (r *response) Header() http.Header {
if r.resp.StatusCode != 0 {
// wrote header
return http.Header{}
}
return r.resp.Header
}
func (r *response) Write(b []byte) (int, error) {
if r.body == nil {
r.body = bytes.NewBuffer(b)
return len(b), nil
}
return r.body.Write(b)
}
func (r *response) WriteString(s string) (int, error) {
if r.body == nil {
r.body = bytes.NewBufferString(s)
return len(s), nil
}
return r.body.WriteString(s)
}
func (r *response) WriteHeader(statusCode int) {
if r.resp.StatusCode == 0 {
// not wrote header
r.resp.StatusCode = statusCode
}
}
// flush flushes buffered data to the client.
func (r *response) flush(w io.Writer) error {
r.WriteHeader(400)
if r.body != nil {
r.resp.ContentLength = int64(r.body.Len())
r.resp.Body = io.NopCloser(r.body)
}
return r.resp.Write(w)
}