-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
53 lines (44 loc) · 1.19 KB
/
util.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
package main
import (
"bufio"
"bytes"
"io"
"log"
"net/http"
)
// Writes response into response writer
func WriteResponse(rw http.ResponseWriter, res *http.Response) {
copyHeaders(rw.Header(), res.Header)
// The preferred way to send Trailers is to predeclare in the headers
// which trailers you will later send by setting the "Trailer" header
// to the names of the trailer keys which will come later.
for key := range res.Trailer {
rw.Header().Add("Trailer", key)
}
rw.WriteHeader(res.StatusCode)
// Write body
io.Copy(rw, res.Body)
copyHeaders(rw.Header(), res.Trailer)
}
// Dumps a http.Response into a bytes.Buffer
func DumpResponse(res *http.Response) (b bytes.Buffer) {
if err := res.Write(&b); err != nil {
log.Println("Error dumping response to buffer")
}
return
}
// Read response data into http.Response
func ReadResponse(resData []byte, req *http.Request) *http.Response {
res, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(resData)), req)
if err != nil {
log.Println("Error reading response from buffer")
}
return res
}
func copyHeaders(dst, src http.Header) {
for key, values := range src {
for _, value := range values {
dst.Add(key, value)
}
}
}