forked from mozillazg/request
/
request.go
446 lines (399 loc) · 9.56 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
package request
import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
// Version export version
const Version = "0.9.0"
// DefaultClient for NewArgs and NewRequest
var DefaultClient = new(http.Client)
// FileField struct for upload file
type FileField struct {
FieldName string
FileName string
File io.Reader
}
// BasicAuth struct for http basic auth
type BasicAuth struct {
Username string
Password string
}
// Args for request args
type Args struct {
Client *http.Client
Headers map[string]string
Cookies map[string]string
Data map[string]string
Params map[string]string
Files []FileField
Json interface{}
Proxy string
BasicAuth BasicAuth
Body io.Reader
Hooks []Hook
Close bool
}
// Request is alias Args
type Request struct {
*Args
}
// NewArgs return a *Args
func NewArgs(c *http.Client) *Args {
if c == nil {
c = DefaultClient
}
if c.Jar == nil {
options := cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, _ := cookiejar.New(&options)
c.Jar = jar
}
headers := map[string]string{}
for k, v := range DefaultHeaders {
headers[k] = v
}
return &Args{
Client: c,
Headers: headers,
Cookies: nil,
Data: nil,
Params: nil,
Files: nil,
Json: nil,
Proxy: "",
BasicAuth: BasicAuth{},
Body: nil,
Hooks: []Hook{},
}
}
// NewRequest return a *Request
func NewRequest(c *http.Client) *Request {
return &Request{NewArgs(c)}
}
func newURL(u string, params map[string]string) string {
if params == nil {
return u
}
p := url.Values{}
for k, v := range params {
p.Set(k, v)
}
if strings.Contains(u, "?") {
return u + "&" + p.Encode()
}
return u + "?" + p.Encode()
}
func newBody(a *Args) (body io.Reader, contentType string, err error) {
if a.Body != nil {
return a.Body, "", nil
}
if a.Data == nil && a.Files == nil && a.Json == nil {
return nil, "", nil
}
if a.Files != nil {
return newMultipartBody(a, nil)
} else if a.Json != nil {
return newJSONBody(a)
}
d := url.Values{}
for k, v := range a.Data {
d.Set(k, v)
}
return strings.NewReader(d.Encode()), DefaultContentType, nil
}
func buildHTTPRequest(method string, url string, a *Args) (req *http.Request, err error) {
body, contentType, err := newBody(a)
if err != nil {
return nil, err
}
u := newURL(url, a.Params)
req, err = http.NewRequest(method, u, body)
if err != nil {
return nil, err
}
applyHeaders(a, req, contentType)
applyCookies(a, req)
err = applyProxy(a)
if err != nil {
return nil, err
}
applyCheckRdirect(a)
if a.BasicAuth.Username != "" {
req.SetBasicAuth(a.BasicAuth.Username, a.BasicAuth.Password)
}
if a.Close == true {
req.Close = true
}
return
}
func newRequest(method string, url string, a *Args) (resp *Response, err error) {
if a == nil {
a = NewArgs(DefaultClient)
}
req, err := buildHTTPRequest(method, url, a)
if err != nil {
return nil, err
}
// apply BeforeRequest hook
s, err := applyBeforeReqHooks(req, a.Hooks)
if err != nil {
return nil, err
} else if s != nil {
resp = &Response{s, nil}
return resp, err
}
s, err = a.Client.Do(req)
// apply AfterRequest hook
newResp, newErr := applyAfterReqHooks(req, s, err, a.Hooks)
if newErr != nil {
err = newErr
}
if newResp != nil {
s = newResp
}
resp = &Response{s, nil}
return
}
// Get issues a GET to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Get(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("GET", url, a)
return
}
// Get issues a GET to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Get(url interface{}) (resp *Response, err error) {
resp, err = Get(url2string(url), req2arg(req))
return
}
// GetResult issues a GET to the specified URL.
//
// url can be string or *url.URL or ur.URL
// result is an url *output struct
func (req *Request) GetResult(url,result interface{}) (err error) {
var resp *Response
if resp, err = Get(url2string(url), req2arg(req));err !=nil{
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(url2string(url) + "http status code is :"+resp.Status)
return
}
var resultBytes []byte
if resultBytes, err = resp.Content(); err != nil {
return
}
if err = json.Unmarshal(resultBytes, result); err != nil {
return
}
return
}
// Head issues a HEAD to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Head(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("HEAD", url, a)
return
}
// Head issues a HEAD to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Head(url interface{}) (resp *Response, err error) {
resp, err = Head(url2string(url), req2arg(req))
return
}
// Post issues a POST to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Post(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("POST", url, a)
return
}
// Post issues a POST to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Post(url interface{}) (resp *Response, err error) {
resp, err = Post(url2string(url), req2arg(req))
return
}
// PostResult issues a POST to the specified URL.
//
// url can be string or *url.URL or ur.URL
//
// result is an url *output struct
func (req *Request) PostResult(url , result interface{}) (err error) {
var resp *Response
if resp, err = Post(url2string(url), req2arg(req));err !=nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
err = errors.New(url2string(url) + "http status code is :"+resp.Status)
return
}
var resultBytes []byte
if resultBytes, err = resp.Content(); err != nil {
return
}
if err = json.Unmarshal(resultBytes, result); err != nil {
return
}
return
}
// PostForm send post form request.
//
// url can be string or *url.URL or ur.URL
//
// data can be map[string]string or map[string][]string or string or io.Reader
//
// data := map[string]string{
// "a": "1",
// "b": "2",
// }
//
// data := map[string][]string{
// "a": []string{"1", "2"},
// "b": []string{"2", "3"},
// }
//
// data : = "a=1&b=2"
//
// data : = strings.NewReader("a=1&b=2")
//
func (req *Request) PostForm(url interface{}, data interface{}) (resp *Response, err error) {
args := req2arg(req)
contentType := ""
switch data.(type) {
case io.Reader:
req.Body = data.(io.Reader)
case string:
req.Body = strings.NewReader(data.(string))
case map[string]string, map[string][]string:
req.Body, contentType, err = newFormBody(args, data)
if err != nil {
return nil, err
}
}
if contentType == "" {
_, ok := req.Headers["Content-Type"]
if !ok && req.Files == nil {
req.Headers["Content-Type"] = DefaultContentType
}
} else {
req.Headers["Content-Type"] = contentType
}
args = req2arg(req)
resp, err = Post(url2string(url), args)
return
}
// Put issues a PUT to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Put(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("PUT", url, a)
return
}
// Put issues a PUT to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Put(url interface{}) (resp *Response, err error) {
resp, err = Put(url2string(url), req2arg(req))
return
}
// Patch issues a PATCH to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Patch(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("PATCH", url, a)
return
}
// Patch issues a PATCH to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Patch(url interface{}) (resp *Response, err error) {
resp, err = Patch(url2string(url), req2arg(req))
return
}
// Delete issues a DELETE to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Delete(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("DELETE", url, a)
return
}
// Delete issues a DELETE to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Delete(url interface{}) (resp *Response, err error) {
resp, err = Delete(url2string(url), req2arg(req))
return
}
// Options issues a OPTIONS to the specified URL.
//
// Caller should close resp.Body when done reading from it.
func Options(url string, a *Args) (resp *Response, err error) {
resp, err = newRequest("OPTIONS", url, a)
return
}
// Options issues a OPTIONS to the specified URL.
//
// url can be string or *url.URL or ur.URL
func (req *Request) Options(url interface{}) (resp *Response, err error) {
resp, err = Options(url2string(url), req2arg(req))
return
}
// Reset all fields to default values
func (req *Request) Reset() {
req.Headers = map[string]string{}
for k, v := range DefaultHeaders {
req.Headers[k] = v
}
req.Cookies = nil
req.Data = nil
req.Params = nil
req.Files = nil
req.Json = nil
req.Proxy = ""
req.BasicAuth = BasicAuth{}
req.Body = nil
return
}
func url2string(u interface{}) string {
switch u.(type) {
case string:
return u.(string)
case url.URL:
s := u.(url.URL)
return s.String()
case *url.URL:
s := u.(*url.URL)
return s.String()
}
return ""
}
func req2arg(req *Request) (a *Args) {
return &Args{
Client: req.Client,
Headers: req.Headers,
Cookies: req.Cookies,
Data: req.Data,
Params: req.Params,
Files: req.Files,
Json: req.Json,
Proxy: req.Proxy,
BasicAuth: req.BasicAuth,
Body: req.Body,
Hooks: req.Hooks,
Close: req.Close,
}
}