-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdumpreq.go
48 lines (40 loc) · 942 Bytes
/
dumpreq.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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
var respExcludeHeader = map[string]bool{
"Content-Length": true,
"Transfer-Encoding": true,
"Trailer": true,
}
func DumpResponse(r *http.Response) ([]byte, error) {
var w bytes.Buffer
// Status line
text := r.Status
if text == "" {
text = http.StatusText(r.StatusCode)
} else {
// Just to reduce stutter, if user set r.Status to "200 OK" and StatusCode to 200.
// Not important.
text = strings.TrimPrefix(text, strconv.Itoa(r.StatusCode)+" ")
}
if _, err := fmt.Fprintf(&w, "HTTP/%d.%d %03d %s\r\n", r.ProtoMajor, r.ProtoMinor, r.StatusCode, text); err != nil {
return nil, err
}
// Rest of header
err := r.Header.WriteSubset(&w, respExcludeHeader)
if err != nil {
return nil, err
}
// End-of-header
if _, err := io.WriteString(&w, "\r\n"); err != nil {
return nil, err
}
// Success
return w.Bytes(), nil
}