-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrecorder.go
357 lines (317 loc) · 8.76 KB
/
recorder.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Package harhar provides a minimal set of methods and structs to enable
// HAR logging in a go net/http-based application.
package harhar
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"net"
"net/http"
"net/http/httptrace"
"os"
"sync"
"time"
)
// Client embeds an upstream RoundTripper and wraps its methods to perform transparent HAR
// logging for every request and response
type Recorder struct {
mu sync.Mutex
RoundTripper http.RoundTripper `json:"-"`
Handler http.Handler `json:"-"`
DisableHTTP2 func(bool)
HAR *HAR
}
// NewRecorder returns a new Recorder object that fulfills the http.RoundTripper interface
func NewRecorder() *Recorder {
h := NewHAR(os.Args[0])
// copy of DefaultTransport but with the
// ability to disable HTTP/2 as needed
tport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// if nil, then HTTP/2 is enabled
// if non-nil, then HTTP/2 is disabled
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
}
return &Recorder{
RoundTripper: tport,
Handler: http.DefaultServeMux,
HAR: h,
DisableHTTP2: func(disable2 bool) {
if disable2 {
if tport.TLSNextProto == nil {
tport.TLSNextProto = make(map[string]func(authority string, c *tls.Conn) http.RoundTripper)
}
} else {
tport.TLSNextProto = nil
}
},
}
}
// WriteLog writes the HAR log format to the filename given, then returns the
// number of bytes.
func (c *Recorder) WriteFile(filename string) (int, error) {
data, err := json.Marshal(c.HAR)
if err != nil {
return 0, err
}
return len(data), os.WriteFile(filename, data, 0644)
}
// RoundTrip implements http.RoundTripper
func (c *Recorder) RoundTrip(req *http.Request) (*http.Response, error) {
// http.RoundTripper must be safe for concurrent use
c.mu.Lock()
defer c.mu.Unlock()
var err error
ent := Entry{}
ent.Request, err = makeRequest(req)
if err != nil {
return nil, err
}
// if we re-use a connection many trace hooks don't fire, so
// set a start time for everything
now := time.Now()
dnsStart := now
tlsStart := now
connWaitStart := now
connStart := now
sendStart := now
waitStart := now
respStart := now
trace := &httptrace.ClientTrace{
GetConn: func(hostPort string) {
connWaitStart = time.Now()
},
GotConn: func(connInfo httptrace.GotConnInfo) {
ent.Timings.Blocked = int(time.Since(connWaitStart).Milliseconds())
},
DNSStart: func(dnsInfo httptrace.DNSStartInfo) {
dnsStart = time.Now()
},
DNSDone: func(dnsInfo httptrace.DNSDoneInfo) {
ent.Timings.DNS = int(time.Since(dnsStart).Milliseconds())
if len(dnsInfo.Addrs) > 0 {
ent.ServerIP = dnsInfo.Addrs[0].String()
} else {
ent.ServerIP = "0.0.0.0"
}
},
ConnectStart: func(network, addr string) {
connStart = time.Now()
},
ConnectDone: func(network, addr string, err error) {
ent.Timings.Connect = int(time.Since(connStart).Milliseconds())
sendStart = time.Now()
},
TLSHandshakeStart: func() {
tlsStart = time.Now()
},
TLSHandshakeDone: func(connState tls.ConnectionState, err error) {
ent.Timings.SSL = int(time.Since(tlsStart).Milliseconds())
},
WroteRequest: func(info httptrace.WroteRequestInfo) {
ent.Timings.Send = int(time.Since(sendStart).Milliseconds())
waitStart = time.Now()
},
GotFirstResponseByte: func() {
ent.Timings.Wait = int(time.Since(waitStart).Milliseconds())
respStart = time.Now()
},
}
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
startTime := time.Now()
resp, err := c.RoundTripper.RoundTrip(req)
if err != nil {
return resp, err
}
ent.Response, err = makeResponse(resp)
ent.Timings.Receive = int(time.Since(respStart).Milliseconds())
ent.Time = int(time.Since(startTime).Milliseconds())
ent.Start = startTime.Format(time.RFC3339Nano)
c.HAR.Log.Entries = append(c.HAR.Log.Entries, ent)
return resp, err
}
// convert an http.Request to a harhar.Request
func makeRequest(hr *http.Request) (Request, error) {
r := Request{
Method: hr.Method,
URL: hr.URL.String(),
HTTPVersion: hr.Proto,
HeadersSize: -1,
BodySize: -1,
}
h2 := hr.Header.Clone()
buf := &bytes.Buffer{}
h2.Write(buf)
r.HeadersSize = buf.Len() + 4 // incl. CRLF CRLF
// parse out headers
r.Headers = make([]NameValuePair, 0, len(hr.Header))
for name, vals := range hr.Header {
for _, val := range vals {
r.Headers = append(r.Headers, NameValuePair{Name: name, Value: val})
}
}
// parse out cookies
r.Cookies = make([]Cookie, 0, len(hr.Cookies()))
for _, c := range hr.Cookies() {
nc := Cookie{
Name: c.Name,
Path: c.Path,
Value: c.Value,
Domain: c.Domain,
Expires: c.Expires.Format(time.RFC3339Nano),
HTTPOnly: c.HttpOnly,
Secure: c.Secure,
}
r.Cookies = append(r.Cookies, nc)
}
// parse query params
qp := hr.URL.Query()
r.QueryParams = make([]NameValuePair, 0, len(qp))
for name, vals := range qp {
for _, val := range vals {
r.QueryParams = append(r.QueryParams, NameValuePair{Name: name, Value: val})
}
}
if hr.Body == nil {
r.BodySize = 0
return r, nil
}
// read in all the data and replace the ReadCloser
bodyData, err := io.ReadAll(hr.Body)
if err != nil {
return r, err
}
hr.Body.Close()
bodbuf := bytes.NewReader(bodyData)
hr.Body = io.NopCloser(bodbuf)
r.BodySize = len(bodyData)
r.Body.MIMEType = hr.Header.Get("Content-Type")
if r.Body.MIMEType == "" {
// default per RFC2616
r.Body.MIMEType = "application/octet-stream"
}
switch r.Body.MIMEType {
case "form-data", "multipart/form-data":
err = hr.ParseMultipartForm(32 << 20) // 32 MB
if err != nil {
return r, err
}
bodbuf.Seek(0, io.SeekStart)
for key, fheads := range hr.MultipartForm.File {
for _, fh := range fheads {
fhandle, err := fh.Open()
if err != nil {
return r, err
}
fileContents, err := io.ReadAll(fhandle)
fhandle.Close()
if err != nil {
return r, err
}
r.Body.Params = append(r.Body.Params, PostNameValuePair{
Name: key,
Value: string(fileContents),
FileName: fh.Filename,
ContentType: fh.Header.Get("Content-Type"),
})
}
}
for key, vals := range hr.MultipartForm.Value {
for _, val := range vals {
r.Body.Params = append(r.Body.Params, PostNameValuePair{
Name: key,
Value: val,
})
}
}
case "application/x-www-form-urlencoded":
err = hr.ParseForm()
if err != nil {
return r, err
}
bodbuf.Seek(0, io.SeekStart)
for key, vals := range hr.PostForm {
for _, val := range vals {
r.Body.Params = append(r.Body.Params, PostNameValuePair{
Name: key,
Value: val,
})
}
}
default:
r.Body.Content = string(bodyData)
}
return r, nil
}
// convert an http.Response to a harhar.Response
func makeResponse(hr *http.Response) (Response, error) {
r := Response{
StatusCode: hr.StatusCode,
StatusText: http.StatusText(hr.StatusCode),
HTTPVersion: hr.Proto,
HeadersSize: -1,
BodySize: -1,
}
h2 := hr.Header.Clone()
buf := &bytes.Buffer{}
h2.Write(buf)
r.HeadersSize = buf.Len() + 4 // incl. CRLF CRLF
// parse out headers
r.Headers = make([]NameValuePair, 0, len(hr.Header))
for name, vals := range hr.Header {
for _, val := range vals {
r.Headers = append(r.Headers, NameValuePair{Name: name, Value: val})
}
}
rurl, err := hr.Location()
if err == nil {
r.RedirectURL = rurl.String()
}
// parse out cookies
r.Cookies = make([]Cookie, 0, len(hr.Cookies()))
for _, c := range hr.Cookies() {
nc := Cookie{
Name: c.Name,
Path: c.Path,
Value: c.Value,
Domain: c.Domain,
Expires: c.Expires.Format(time.RFC3339Nano),
HTTPOnly: c.HttpOnly,
Secure: c.Secure,
}
r.Cookies = append(r.Cookies, nc)
}
// FIXME: net/http transparently decompresses content,
// so r.Body.Size and r.Body.Compression are not true to the server's response
// also, if the response is not utf-8, then r.Body.Content and r.Body.Encoding
// are not properly handled (spec says to decode anything into UTF-8)
//
// see hr.Uncompressed for next steps
// read in all the data and replace the ReadCloser
bodyData, err := io.ReadAll(hr.Body)
if err != nil {
return r, err
}
hr.Body.Close()
hr.Body = io.NopCloser(bytes.NewReader(bodyData))
r.Body.Content = string(bodyData)
r.Body.Compression = 0
r.Body.Size = len(bodyData)
r.BodySize = r.Body.Size
r.Body.MIMEType = hr.Header.Get("Content-Type")
if r.Body.MIMEType == "" {
// default per RFC2616
r.Body.MIMEType = "application/octet-stream"
}
return r, nil
}