-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathresponse.go
46 lines (39 loc) · 1010 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
package falcore
import (
"net/http"
"strings"
)
func SimpleResponse(req *http.Request, status int, headers http.Header, body string) *http.Response {
res := new(http.Response)
body_rdr := (*fixedResBody)(strings.NewReader(body))
res.StatusCode = status
res.ProtoMajor = 1
res.ProtoMinor = 1
res.ContentLength = int64((*strings.Reader)(body_rdr).Len())
res.Request = req
res.Header = make(map[string][]string)
res.Body = body_rdr
if headers != nil {
res.Header = headers
}
return res
}
// string type for response objects
type fixedResBody strings.Reader
func (s *fixedResBody) Close() error {
return nil
}
func (s *fixedResBody) Read(b []byte) (int, error) {
return (*strings.Reader)(s).Read(b)
}
func RedirectResponse(req *http.Request, url string) *http.Response {
res := new(http.Response)
res.StatusCode = 302
res.ProtoMajor = 1
res.ProtoMinor = 1
res.ContentLength = 0
res.Request = req
res.Header = make(map[string][]string)
res.Header.Set("Location", url)
return res
}