-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
61 lines (53 loc) · 1.34 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
package betproxy
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
)
// NewResponse create a http.Response
func NewResponse(code int, header http.Header, body io.Reader, req *http.Request) *http.Response {
if body == nil {
body = &bytes.Buffer{}
}
if header == nil {
header = http.Header{}
}
rc, ok := body.(io.ReadCloser)
if !ok {
rc = ioutil.NopCloser(body)
}
res := &http.Response{
StatusCode: code,
Status: fmt.Sprintf("%d %s", code, http.StatusText(code)),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: header,
Body: rc,
Request: req,
ContentLength: -1,
}
if req != nil {
res.Close = req.Close
res.Proto = req.Proto
res.ProtoMajor = req.ProtoMajor
res.ProtoMinor = req.ProtoMinor
}
return res
}
// HTTPText create a http.Response with giving text
func HTTPText(code int, header http.Header, text string, req *http.Request) *http.Response {
res := NewResponse(code, header, strings.NewReader(text), req)
res.ContentLength = int64(len(text))
return res
}
// HTTPError create a http.Response with giving error message
func HTTPError(code int, err string, req *http.Request) *http.Response {
return HTTPText(code, http.Header{
"Content-Type": []string{"text/plain; charset=utf-8"},
"Via": []string{"betproxy"},
}, err, req)
}