-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
513 lines (449 loc) · 12.6 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package quick
import (
"bytes"
"context"
"encoding/base64"
"encoding/xml"
"errors"
"github.com/telanflow/quick/encode"
"io"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
)
// Sequence number is incremented and utilized for all request created.
var sequenceNo uint64
// Request http request payload
type Request struct {
Id uint64
URL *url.URL
Method string
Header http.Header // request headers
Body io.Reader // request encode
RedirectNum int // Number of redirects requested. default 5
Timeout time.Duration // request timeout
Proxy *url.URL // request proxy url
Cookies Cookies // request cookies
host string // customize the request Host field
ctx context.Context
trace bool
clientTrace *clientTrace
}
// NewRequest create a request instance
func NewRequest() *Request {
return NewRequestWithContext(nil)
}
// NewRequestWithContext create a request instance with context.Context
func NewRequestWithContext(ctx context.Context) *Request {
return &Request{
Id: atomic.AddUint64(&sequenceNo, 1),
URL: nil,
Method: http.MethodGet,
Header: make(http.Header),
Body: nil,
RedirectNum: DefaultRedirectNum, // set request redirect num. default 10.
Timeout: 30 * time.Second,
Proxy: nil,
Cookies: nil,
ctx: ctx,
trace: false,
clientTrace: nil,
}
}
// ConvertHttpRequest convert http.Request To Request
func ConvertHttpRequest(r *http.Request) *Request {
// copy the URL
newURL, _ := CopyURL(r.URL)
// copy Cookies
var copyBody io.Reader
if r.Body != nil {
copyBody := new(bytes.Buffer)
_, _ = io.Copy(copyBody, r.Body)
}
// Generate a new request.Id
newReq := NewRequest()
newReq.URL = newURL
newReq.Method = r.Method
newReq.Header = CopyHeader(r.Header)
newReq.Body = copyBody
newReq.RedirectNum = DefaultRedirectNum
newReq.Timeout = 30 * time.Second
newReq.ctx = r.Context()
newReq.trace = false
newReq.clientTrace = nil
return newReq
}
// WithContext with context.Context for Request
func (req *Request) WithContext(ctx context.Context) *Request {
req.ctx = ctx
return req
}
// Context get context.Context for Request
func (req *Request) Context() context.Context {
return req.ctx
}
// SetUrl set request url
func (req *Request) SetUrl(rawurl string) *Request {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
req.URL = u
return req
}
// GetUrl get request url
func (req *Request) GetUrl() string {
return req.URL.String()
}
// SetURL set request url
func (req *Request) SetURL(u *url.URL) *Request {
req.URL = u
return req
}
// GetURL get request url
func (req *Request) GetURL() *url.URL {
return req.URL
}
// SetMethod set request method
func (req *Request) SetMethod(method string) *Request {
req.Method = strings.ToUpper(method)
return req
}
// GetMethod get request method
func (req *Request) GetMethod() string {
return req.Method
}
// SetTimeout set request timeout
func (req *Request) SetTimeout(t time.Duration) *Request {
req.Timeout = t
return req
}
// GetTimeout get request timeout
func (req *Request) GetTimeout() time.Duration {
return req.Timeout
}
// SetHost custom request host field
// GET /index HTTP/1.1
// Host: domain
// ....
func (req *Request) SetHost(host string) *Request {
req.host = host
return req
}
// BasicAuth returns the username and password provided in the request's
// Authorization header, if the request uses HTTP Basic Authentication.
// See RFC 2617, Section 2.
func (req *Request) BasicAuth() (username, password string, ok bool) {
auth := req.Header.Get("Authorization")
if auth == "" {
return
}
return parseBasicAuth(auth)
}
// SetBasicAuth sets the request's Authorization header to use HTTP
// Basic Authentication with the provided username and password.
//
// With HTTP Basic Authentication the provided username and password
// are not encrypted.
//
// Some protocols may impose additional requirements on pre-escaping the
// username and password. For instance, when used with OAuth2, both arguments
// must be URL encoded first with url.QueryEscape.
func (req *Request) SetBasicAuth(username, password string) {
req.Header.Set("Authorization", "Basic "+basicAuth(username, password))
}
// EnableTrace method enables trace for the current request
// using `httptrace.ClientTrace` and provides insights.
//
// resp, err := quick.EnableTrace().Get("https://httpbin.org/get")
// fmt.Println("Error:", err)
// fmt.Println("Trace Info:", resp.TraceInfo())
//
// See `Request.EnableTrace` available too to get trace info for all requests.
//
// Since v0.4.0
func (req *Request) EnableTrace() *Request {
req.trace = true
return req
}
// DisableTrace method disables the Quick client trace. Refer to `Request.EnableTrace`.
//
// Since v0.4.0
func (req *Request) DisableTrace() *Request {
req.trace = false
return req
}
// TraceInfo method returns the trace info for the request.
// If either the Client or Request EnableTrace function has not been called
// prior to the request being made, an empty TraceInfo object will be returned.
//
// Since v0.4.0
func (req *Request) TraceInfo() TraceInfo {
ct := req.clientTrace
if ct == nil {
return TraceInfo{}
}
ti := TraceInfo{
DNSLookup: ct.dnsDone.Sub(ct.dnsStart),
TLSHandshake: ct.tlsHandshakeDone.Sub(ct.tlsHandshakeStart),
ServerTime: ct.gotFirstResponseByte.Sub(ct.gotConn),
IsConnReused: ct.gotConnInfo.Reused,
IsConnWasIdle: ct.gotConnInfo.WasIdle,
ConnIdleTime: ct.gotConnInfo.IdleTime,
}
// Calculate the total time accordingly,
// when connection is reused
if ct.gotConnInfo.Reused {
ti.TotalTime = ct.endTime.Sub(ct.getConn)
} else {
ti.TotalTime = ct.endTime.Sub(ct.dnsStart)
}
// Only calculate on successful connections
if !ct.connectDone.IsZero() {
ti.TCPConnTime = ct.connectDone.Sub(ct.dnsDone)
}
// Only calculate on successful connections
if !ct.gotConn.IsZero() {
ti.ConnTime = ct.gotConn.Sub(ct.getConn)
}
// Only calculate on successful connections
if !ct.gotFirstResponseByte.IsZero() {
ti.ResponseTime = ct.endTime.Sub(ct.gotFirstResponseByte)
}
// Capture remote address info when connection is non-nil
if ct.gotConnInfo.Conn != nil {
ti.RemoteAddr = ct.gotConnInfo.Conn.RemoteAddr()
}
return ti
}
// SetQueryString set GET parameters to request
func (req *Request) SetQueryString(params interface{}) *Request {
buff := new(bytes.Buffer)
form := new(encode.XWwwFormUrlencoded)
form.SetValue(params)
if err := form.Encode(buff); err != nil {
panic(err)
}
// format get request parameters
u, err := MergeQueryString(req.URL, buff.String())
if err != nil {
panic(err)
}
req.URL = u
req.Body = nil
return req
}
// SetBody set POST body to request
func (req *Request) SetBody(params interface{}) *Request {
buff := new(bytes.Buffer)
form := new(encode.XWwwFormUrlencoded)
form.SetValue(params)
if err := form.Encode(buff); err != nil {
panic(err)
}
req.Body = buff
return req
}
// SetBodyFormData set POST body (FormData) to request
func (req *Request) SetBodyFormData(params interface{}) *Request {
buff := new(bytes.Buffer)
form := new(encode.XWwwFormUrlencoded)
form.SetValue(params)
if err := form.Encode(buff); err != nil {
panic(err)
}
req.SetHeaderSingle("Content-Type", "application/form-data")
req.Body = buff
return req
}
// SetBodyJson set POST body (RAW) to request
func (req *Request) SetBodyJson(params interface{}) *Request {
buff, err := json.Marshal(params)
if err != nil {
panic(err)
}
req.SetHeaderSingle("Content-Type", "application/json")
req.Body = bytes.NewReader(buff)
return req
}
// SetBodyXML set POST body (RAW) to request
func (req *Request) SetBodyXML(params interface{}) *Request {
buff, err := xml.Marshal(params)
if err != nil {
panic(err)
}
req.SetHeaderSingle("Content-Type", "application/xml")
req.Body = bytes.NewReader(buff)
return req
}
// SetBodyXWwwFormUrlencoded set request body x-www-form-urlencoded
func (req *Request) SetBodyXWwwFormUrlencoded(params interface{}) *Request {
req.SetHeaderSingle("Content-Type", "application/x-www-form-urlencoded")
return req.SetBody(params)
}
// SetHeader set request header
func (req *Request) SetHeader(header http.Header) *Request {
req.Header = header
return req
}
// GetHeader get request header
func (req *Request) GetHeader() http.Header {
return req.Header
}
// SetHeaderSingle set request header single
func (req *Request) SetHeaderSingle(key, val string) *Request {
req.Header.Set(key, val)
return req
}
// GetHeaderSingle get request header single
func (req *Request) GetHeaderSingle(key string) string {
return req.Header.Get(key)
}
// SetHeaders merge request origin header and header
func (req *Request) SetHeaders(header http.Header) *Request {
for key, val := range header {
for _, v := range val {
req.Header.Set(key, v)
}
}
return req
}
// SetReferer set request referer
func (req *Request) SetReferer(referer string) *Request {
req.Header.Set("Referer", referer)
return req
}
// SetCharset set request charset
func (req *Request) SetCharset(charset string) *Request {
req.SetHeaderSingle("Accept-Charset", charset)
return req
}
// SetUserAgent set request user-agent
func (req *Request) SetUserAgent(ua string) *Request {
req.SetHeaderSingle("User-Agent", ua)
return req
}
// GetUserAgent get request user-agent
func (req *Request) GetUserAgent() string {
return req.GetHeaderSingle("User-Agent")
}
// GetProxyUrl get proxy url for this request
func (req *Request) GetProxyUrl() string {
if req.Proxy == nil {
return ""
}
return req.Proxy.String()
}
// SetProxyUrl set the proxy for this request
// eg. "http://127.0.0.1:8080" "http://username:[email protected]:8080"
func (req *Request) SetProxyUrl(rawurl string) *Request {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
req.Proxy = u
return req
}
// GetProxyURL get proxy url.URL for this request
func (req *Request) GetProxyURL() *url.URL {
return req.Proxy
}
// SetProxyURL set the proxy url for this request
func (req *Request) SetProxyURL(u *url.URL) *Request {
req.Proxy = u
return req
}
// SetCookies set cookies to request
// sample:
// quick.SetCookies(
// quick.NewCookiesWithString("key1=value1; key2=value2; key3=value3")
// )
func (req *Request) SetCookies(cookies Cookies) *Request {
req.Cookies = cookies
return req
}
// Copy copy a new request
func (req *Request) Copy() *Request {
// copy the URL
newURL, _ := CopyURL(req.URL)
var copyBody io.Reader
if req.Body != nil {
copyBody := new(bytes.Buffer)
_, _ = io.Copy(copyBody, req.Body)
}
// copy the proxy url
copyProxy, _ := CopyURL(req.Proxy)
var copyCookies Cookies
if req.Cookies != nil {
copyCookies = make(Cookies, len(req.Cookies))
copy(copyCookies, req.Cookies)
}
// Generate a new request.Id
newReq := NewRequest()
newReq.URL = newURL
newReq.Method = req.Method
newReq.Header = CopyHeader(req.Header)
newReq.Body = copyBody
newReq.RedirectNum = req.RedirectNum
newReq.Timeout = req.Timeout
newReq.Proxy = copyProxy
newReq.Cookies = copyCookies
newReq.host = req.host
newReq.ctx = req.ctx
return newReq
}
// CopyURL copy a new url.URL
func CopyURL(u *url.URL) (URL *url.URL, err error) {
if u == nil {
err = errors.New("copy url.URL is nil")
return
}
// copy basic authentication username,password
var user *url.Userinfo
if u.User != nil {
password, _ := u.User.Password()
user = url.UserPassword(u.User.Username(), password)
}
URL = &url.URL{
Scheme: u.Scheme,
Opaque: u.Opaque,
User: user,
Host: u.Host,
Path: u.Path,
RawPath: u.RawPath,
ForceQuery: u.ForceQuery,
RawQuery: u.RawQuery,
Fragment: u.Fragment,
}
return
}
// parseBasicAuth parses an HTTP Basic Authentication string.
// "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" returns ("Aladdin", "open sesame", true).
func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic "
// Case insensitive prefix match. See Issue 22736.
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
return
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
return cs[:s], cs[s+1:], true
}
// See 2 (end of page 4) https://www.ietf.org/rfc/rfc2617.txt
// "To receive authorization, the client sends the userid and password,
// separated by a single colon (":") character, within a base64
// encoded string in the credentials."
// It is not meant to be urlencoded.
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}