/
dumper.go
90 lines (79 loc) · 2.06 KB
/
dumper.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package gophers
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"github.com/go-gophers/gophers/jsons"
)
func isChunked(te []string) bool {
for _, v := range te {
if v == "chunked" {
return true
}
}
return false
}
func dump(b []byte, te []string) (status, headers, body []byte, err error) {
p := bytes.SplitN(b, []byte("\r\n\r\n"), 2)
headers, body = p[0], p[1]
p = bytes.SplitN(headers, []byte("\r\n"), 2)
status, headers = p[0], p[1]
if len(body) > 0 && isChunked(te) {
r := httputil.NewChunkedReader(bytes.NewReader(body))
body, err = ioutil.ReadAll(r)
if err != nil {
return
}
}
return
}
// dumpRequest returns representation of req with status line, headers and body.
// It uses httputil.DumpRequestOut and additionally converts body from chunked encoding
// to identity so it can be used in documentation.
func dumpRequest(req *http.Request) (status, headers, body []byte, err error) {
var b []byte
b, err = httputil.DumpRequestOut(req, true)
if err != nil {
return
}
return dump(b, req.TransferEncoding)
}
// dumpRequest returns representation of res with status line, headers and body.
// It uses httputil.DumpResponse and additionally converts body from chunked encoding
// to identity so it can be used in documentation.
func dumpResponse(res *http.Response) (status, headers, body []byte, err error) {
var b []byte
b, err = httputil.DumpResponse(res, true)
if err != nil {
return
}
return dump(b, res.TransferEncoding)
}
// bodyRepr returns representation of body depending on content type.
// It may be indented, shortened or returned as is.
// It returns nil for empty body.
func bodyRepr(contentType string, body []byte) []byte {
if len(body) == 0 {
return nil
}
switch {
case strings.Contains(contentType, "json"):
return []byte(jsons.ParseBytes(body).Indent())
default:
for _, r := range string(body) {
switch {
case r == '\n':
continue
case strconv.IsPrint(r):
continue
default:
return []byte(fmt.Sprintf("[%d bytes data]", len(body)))
}
}
return body
}
}