/
request.go
123 lines (98 loc) · 2.24 KB
/
request.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package httpdump
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"strconv"
)
func ReadRequest(b []byte) (*http.Request, error) {
b = bytes.TrimSpace(b)
b = denormalizeNewlines(b)
r, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(b)))
if err != nil {
return nil, err
}
return normalizeRequest(r)
}
func DumpRequest(r *http.Request) ([]byte, error) {
r, err := normalizeRequest(r)
if err != nil {
return nil, err
}
// Use `DumpRequestOut` instead of `DumpRequest` to preserve the querystring.
// Just don't forgot to strip of the user-agent=Go-http-client/1.1 and accept-encoding=gzip
b, err := httputil.DumpRequest(r, true)
if err != nil {
return nil, err
}
b = normalizeNewlines(b)
return b, nil
}
func FromRequest(r *http.Request) (*Dump, error) {
return requestToDump(r)
}
func normalizeRequest(r *http.Request) (*http.Request, error) {
// httputil.DumpRequest seem to strip the querystring
// when constructed with httptest.NewRequest.
if len(r.URL.RequestURI()) > len(r.RequestURI) {
r.RequestURI = r.URL.RequestURI()
}
if r.Body == nil {
return r, nil
}
// Prettify the request body.
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
b, err = bytesPretty(b)
if err != nil {
return nil, err
}
r.Body = io.NopCloser(bytes.NewReader(b))
n := strconv.Itoa(len(b))
if o := r.Header.Get("Content-Length"); o != n && len(b) > 0 {
// Update the content length.
r.Header.Set("Content-Length", n)
}
return r, nil
}
func requestToDump(r *http.Request) (*Dump, error) {
reqLine := formatRequestLine(r)
b, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
var a any
if json.Valid(b) {
if err := json.Unmarshal(b, &a); err != nil {
return nil, err
}
} else {
a = string(b)
}
r.Body = io.NopCloser(bytes.NewReader(b))
return &Dump{
Line: reqLine,
Header: r.Header.Clone(),
Body: a,
}, nil
}
func formatRequestLine(req *http.Request) string {
reqURI := req.RequestURI
if reqURI == "" {
reqURI = req.URL.RequestURI()
}
return fmt.Sprintf("%s %s HTTP/%d.%d", valueOrDefault(req.Method, "GET"),
reqURI, req.ProtoMajor, req.ProtoMinor)
}
func valueOrDefault(v, d string) string {
if v != "" {
return v
}
return d
}