-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.go
267 lines (223 loc) · 6.37 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
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
package httpstat
import (
"bytes"
"io"
"net"
"net/http"
"time"
)
// TODO: distinct timeout errors for TLS etc
// DefaultMaxRedirects is the max number of redirects.
var DefaultMaxRedirects = 5
// DefaultClient used for requests.
var DefaultClient = &http.Client{
CheckRedirect: checkRedirect,
Timeout: 10 * time.Second,
Transport: &http.Transport{
DisableCompression: true,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 0,
}).DialContext,
DisableKeepAlives: true,
MaxIdleConns: 10,
TLSHandshakeTimeout: 5 * time.Second,
},
}
// Check redirect.
func checkRedirect(req *http.Request, via []*http.Request) error {
if len(via) > DefaultMaxRedirects {
return ErrMaxRedirectsExceeded
}
return nil
}
// Size writer.
type sizeWriter int
// Write implementation.
func (w *sizeWriter) Write(b []byte) (int, error) {
*w += sizeWriter(len(b))
return len(b), nil
}
// Size of writes.
func (w sizeWriter) Size() int {
return int(w)
}
// Response interface.
type Response interface {
Status() int
Redirects() int
TLS() bool
Header() http.Header
HeaderSize() int
BodySize() int
TimeDNS() time.Duration
TimeConnect() time.Duration
TimeTLS() time.Duration
TimeWait() time.Duration
TimeResponse(time.Time) time.Duration
TimeDownload(time.Time) time.Duration
TimeTotal(time.Time) time.Duration
TimeTotalWithRedirects(time.Time) time.Duration
TimeRedirects() time.Duration
Traces() []Trace
Stats() *Stats
}
// Stats is an opaque struct which can be useful for JSON marshaling.
type Stats struct {
Status int `json:"status,omitempty"`
Redirects int `json:"redirects,omitempty"`
TLS bool `json:"tls"`
Header http.Header `json:"header,omitempty"`
HeaderSize int `json:"header_size,omitempty"`
BodySize int `json:"body_size,omitempty"`
TimeDNS time.Duration `json:"time_dns"`
TimeConnect time.Duration `json:"time_connect"`
TimeTLS time.Duration `json:"time_tls"`
TimeWait time.Duration `json:"time_wait"`
TimeResponse time.Duration `json:"time_response"`
TimeDownload time.Duration `json:"time_download"`
TimeTotal time.Duration `json:"time_total"`
TimeTotalWithRedirects time.Duration `json:"time_total_with_redirects,omitempty"`
TimeRedirects time.Duration `json:"time_redirects,omitempty"`
Traces []*Stats `json:"traces,omitempty"`
}
// Response struct.
type response struct {
status int
traces []Trace
headerSize int
header http.Header
bodySize sizeWriter
}
// Stats returns a struct of stats.
func (r response) Stats() *Stats {
now := time.Now()
var traces []*Stats
for _, t := range r.Traces() {
traces = append(traces, t.Stats())
}
return &Stats{
Status: r.Status(),
Redirects: r.Redirects(),
TLS: r.TLS(),
Header: r.Header(),
HeaderSize: r.HeaderSize(),
BodySize: r.BodySize(),
TimeDNS: r.TimeDNS(),
TimeConnect: r.TimeConnect(),
TimeTLS: r.TimeTLS(),
TimeWait: r.TimeWait(),
TimeResponse: r.TimeResponse(now),
TimeDownload: r.TimeDownload(now),
TimeTotal: r.TimeTotal(now),
TimeTotalWithRedirects: r.TimeTotalWithRedirects(now),
TimeRedirects: r.TimeRedirects(),
Traces: traces,
}
}
// Status code.
func (r *response) Status() int {
return r.status
}
// Last trace.
func (r *response) last() Trace {
return r.traces[len(r.traces)-1]
}
// TLS implementation.
func (r *response) TLS() bool {
return r.last().TLS()
}
// Redirects implementation.
func (r *response) Redirects() int {
return len(r.traces) - 1
}
// BodySize implementation.
func (r *response) BodySize() int {
return int(r.bodySize)
}
// HeaderSize implementation.
func (r *response) HeaderSize() int {
return r.headerSize
}
// TimeDownload implementation.
func (r *response) TimeDownload(now time.Time) time.Duration {
return r.last().TimeDownload(now)
}
// TimeResponse implementation.
func (r *response) TimeResponse(now time.Time) time.Duration {
return r.last().TimeResponse(now)
}
// TimeDNS implementation.
func (r *response) TimeDNS() time.Duration {
return r.last().TimeDNS()
}
// TimeConnect implementation.
func (r *response) TimeConnect() time.Duration {
return r.last().TimeConnect()
}
// TimeTLS implementation.
func (r *response) TimeTLS() time.Duration {
return r.last().TimeTLS()
}
// TimeWait implementation.
func (r *response) TimeWait() time.Duration {
return r.last().TimeWait()
}
// TimeTotal implementation.
func (r *response) TimeTotal(now time.Time) time.Duration {
return r.last().TimeTotal(now)
}
// TimeTotalWithRedirects implementation.
func (r *response) TimeTotalWithRedirects(now time.Time) time.Duration {
return r.traces[0].TimeTotal(now)
}
// TimeRedirects implementation.
func (r *response) TimeRedirects() time.Duration {
if len(r.traces) == 1 {
return 0
}
first := r.traces[0]
last := r.traces[len(r.traces)-1]
return last.Start().Sub(first.Start())
}
// Header implementation.
func (r *response) Header() http.Header {
return r.header
}
// Traces implementation.
func (r *response) Traces() []Trace {
return r.traces
}
// RequestWithClient performs a traced request.
func RequestWithClient(client *http.Client, method, uri string, header http.Header, body io.Reader) (Response, error) {
req, err := http.NewRequest(method, uri, body)
if err != nil {
return nil, err
}
for name, field := range header {
for _, v := range field {
req.Header.Set(name, v)
}
}
var out response
req = req.WithContext(WithTraces(req.Context(), &out.traces))
res, err := client.Do(req)
if err != nil {
return nil, normalizeError(err)
}
defer res.Body.Close()
out.status = res.StatusCode
if _, err := io.Copy(&out.bodySize, res.Body); err != nil {
return nil, err
}
var resHeader bytes.Buffer
res.Header.Write(&resHeader)
out.header = res.Header
out.headerSize = resHeader.Len()
return &out, nil
}
// Request performs a traced request.
func Request(method, uri string, header http.Header, body io.Reader) (Response, error) {
return RequestWithClient(DefaultClient, method, uri, header, body)
}