-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfirehttp.go
More file actions
514 lines (422 loc) · 11.2 KB
/
Copy pathfirehttp.go
File metadata and controls
514 lines (422 loc) · 11.2 KB
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
514
package firehttp
import (
"bytes"
"crypto/tls"
"fmt"
"github.com/Greyh4t/dnscache"
"github.com/google/go-querystring/query"
"github.com/pkg/errors"
"golang.org/x/net/publicsuffix"
"io"
"mime/multipart"
"net"
"net/http"
"net/http/cookiejar"
"net/textproto"
"net/url"
"os"
"runtime"
"strconv"
"strings"
"time"
)
const VERSION = "0.1"
type FireHttp struct {
Setting *HTTPOptions
}
// HTTP Request 参数
type HTTPOptions struct {
// 代理地址
Proxy string
// DNS缓存有效时间
DNSCacheExpire time.Duration
// 空闲连接数
MaxIdleConn int
// TLS握手超时时间
TLSHandshakeTimeout time.Duration
// 拨号建立连接完成超时时间
DialTimeout time.Duration
// KeepAlive 超时时间
DialKeepAlive time.Duration
// 预先设置的Header
ParentHeader map[string]string
// 预先设置好的http请求超时时间
ParentHTTPTimeout time.Duration
}
var resolver *dnscache.Resolver
// 发送请求
func (f *FireHttp) DoRequest(method string, rawUrl string, ro *ReqOptions) (*Response, error) {
if ro == nil {
ro = &ReqOptions{}
}
// 默认请求超时时间
if ro.Timeout == 0 {
if f.Setting.ParentHTTPTimeout != 0 {
ro.Timeout = f.Setting.ParentHTTPTimeout
} else {
ro.Timeout = 60 * time.Second
}
}
var httpClient *http.Client
rawURL, err := buildQuery(rawUrl, ro.Params)
if err != nil {
return nil, err
}
req, err := f.BuildRequest(method, rawURL, ro)
if err != nil {
return nil, err
}
if ro.HTTPClient != nil {
httpClient = ro.HTTPClient
} else {
httpClient = f.buildHTTPClient(ro)
}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
return &Response{RawResponse: resp}, nil
}
// 构建HTTP Client
func (f *FireHttp) buildHTTPClient(ro *ReqOptions) *http.Client {
var cookieJar http.CookieJar
if ro.UseCookieJar {
if ro.CookieJar != nil {
cookieJar = ro.CookieJar
} else {
cookieJar, _ = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
}
}
return &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if ro.DisableRedirect {
return http.ErrUseLastResponse
} else {
return nil
}
},
Jar: cookieJar,
Transport: f.buildHTTPTransport(ro),
Timeout: ro.Timeout,
}
}
// 构建Transport
func (f *FireHttp) buildHTTPTransport(ro *ReqOptions) *http.Transport {
transport := &http.Transport{
Proxy: func(request *http.Request) (*url.URL, error) {
if f.Setting.Proxy != "" {
return url.Parse(f.Setting.Proxy)
}
return nil, nil
},
TLSClientConfig: &tls.Config{InsecureSkipVerify: ro.InsecureSkipVerify},
DisableCompression: ro.DisableCompression,
MaxIdleConns: f.Setting.MaxIdleConn,
IdleConnTimeout: f.Setting.DialKeepAlive,
TLSHandshakeTimeout: f.Setting.DialKeepAlive,
ExpectContinueTimeout: f.Setting.DialKeepAlive,
Dial: func(network, addr string) (net.Conn, error) {
deadline := time.Now().Add(f.Setting.DialKeepAlive)
if resolver != nil {
host, port, err := net.SplitHostPort(addr)
if err == nil {
ip, err := resolver.FetchOneString(host)
if err != nil {
return nil, err
}
addr = net.JoinHostPort(ip, port)
}
}
c, err := net.DialTimeout(network, addr, f.Setting.DialKeepAlive)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
c.SetReadDeadline(deadline)
c.SetWriteDeadline(deadline)
return c, nil
},
}
EnsureTransporterFinalized(transport)
return transport
}
// 摘抄grequests,解决资源泄露问题
// EnsureTransporterFinalized will ensure that when the HTTP client is GCed
// the runtime will close the idle connections (so that they won't leak)
// this function was adopted from Hashicorp's go-cleanhttp package
func EnsureTransporterFinalized(httpTransport *http.Transport) {
runtime.SetFinalizer(&httpTransport, func(transportInt **http.Transport) {
(*transportInt).CloseIdleConnections()
})
}
// 构建请求
// 支持body格式string或[]byte
func (f *FireHttp) BuildRequest(method string, rawURL string, ro *ReqOptions) (*http.Request, error) {
req, err := http.NewRequest(method, rawURL, nil)
if err != nil {
return nil, err
}
if ro.Body != nil {
req, err = f.BuildBasicRequest(method, rawURL, ro)
if err != nil {
return nil, err
}
}
if ro.Files != nil {
req, err = BuildFileUploadRequest(method, rawURL, ro)
if err != nil {
return nil, err
}
}
err = f.SetHeaders(req, ro)
if err != nil {
return nil, err
}
return req, nil
}
// 构建基础请求
func (f *FireHttp) BuildBasicRequest(method string, rawURL string, ro *ReqOptions) (*http.Request, error) {
var reader io.Reader
switch ro.Body.(type) {
case string:
reader = strings.NewReader(ro.Body.(string))
case []byte:
reader = bytes.NewReader(ro.Body.([]byte))
case map[string]string:
urlValues := &url.Values{}
for key, value := range ro.Body.(map[string]string) {
urlValues.Set(key, value)
}
reader = strings.NewReader(urlValues.Encode())
default:
return nil, errors.New("body type error, only support string and map[string]string or []byte type")
}
req, err := http.NewRequest(method, rawURL, reader)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return req, nil
}
// 构建文件上传请求
func BuildFileUploadRequest(method string, rawURL string, ro *ReqOptions) (*http.Request, error) {
if method == "POST" {
return buildPostFileUploadRequest(method, rawURL, ro)
}
// 目前仅支持POST上传
return nil, errors.New("Upload method is wrong, currently only supports POST method to upload files")
}
// 构建post上传文件
// 支持上传多个文件
func buildPostFileUploadRequest(method string, rawURL string, ro *ReqOptions) (*http.Request, error) {
requestBody := &bytes.Buffer{}
multipartWriter := multipart.NewWriter(requestBody)
for i, f := range ro.Files {
fd, err := os.Open(f.FileName)
if err != nil {
return nil, err
}
ro.Files[i].FileBody = fd
}
for i, f := range ro.Files {
fieldName := f.FieldName
if fieldName == "" {
if len(ro.Files) > 1 {
fieldName = strings.Join([]string{"file", strconv.Itoa(i + 1)}, "")
} else {
fieldName = "file"
}
}
var writer io.Writer
var err error
if f.FileMime != "" {
if f.FileName == "" {
f.FileName = "filename"
}
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, escapeQuotes(fieldName), escapeQuotes(f.FileName)))
h.Set("Content-Type", f.FileMime)
writer, err = multipartWriter.CreatePart(h)
} else {
writer, err = multipartWriter.CreateFormFile(fieldName, f.FileName)
}
if err != nil {
return nil, err
}
if _, err = io.Copy(writer, f.FileBody); err != nil && err != io.EOF {
return nil, err
}
if err := f.FileBody.Close(); err != nil {
return nil, err
}
}
if ro.Body != nil {
switch ro.Body.(type) {
case string:
requestBody.Write([]byte(ro.Body.(string)))
case []byte:
requestBody.Write(ro.Body.([]byte))
case map[string]string:
for key, value := range ro.Body.(map[string]string) {
_ = multipartWriter.WriteField(key, value)
}
default:
return nil, errors.New("body type error, only support string and map[string]string or []byte type")
}
}
if err := multipartWriter.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest(method, rawURL, requestBody)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", multipartWriter.FormDataContentType())
return req, err
}
// 设置header
// 仅支持string和map[string]string类型
func (f *FireHttp) SetHeaders(req *http.Request, ro *ReqOptions) error {
var err error
if f.Setting.ParentHeader != nil {
for key, val := range f.Setting.ParentHeader {
req.Header.Set(key, val)
}
}
if ro.Header != nil {
switch ro.Header.(type) {
case map[string]string:
for key, val := range ro.Header.(map[string]string) {
req.Header.Set(key, val)
}
case string:
headers := strings.Split(ro.Header.(string), "\r\n")
for _, val := range headers {
header := strings.Split(val, ":")
if len(header) == 2 {
req.Header.Set(header[0], header[1])
}
}
default:
return errors.New("header type error, only support string and map[string]string type")
}
}
// 基础认证
if ro.BasicAuthUserAndPass != nil {
if len(ro.BasicAuthUserAndPass) == 2 {
req.SetBasicAuth(ro.BasicAuthUserAndPass[0], ro.BasicAuthUserAndPass[1])
}
}
// 启用ajax请求
if ro.IsAjax == true {
req.Header.Set("X-Requested-With", "XMLHttpRequest")
}
if ro.IsJSON == true {
req.Header.Set("Content-Type", "application/json")
}
if ro.IsXML == true {
req.Header.Set("Content-Type", "application/xml")
}
if req.Header.Get("User-Agent") == "" {
req.Header.Set("User-Agent", "firehttp/"+VERSION)
}
return err
}
// 解析params,生成带Query的url
// params 支持string类型和map[string]string或struct类型
func buildQuery(rawURL string, params interface{}) (string, error) {
if params != nil {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", err
}
parsedQuery, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return "", err
}
switch params.(type) {
case string:
urlQuery := params.(string)
queryStr, err := url.ParseQuery(urlQuery)
if err != nil {
return "", err
}
for key, value := range queryStr {
for _, v := range value {
parsedQuery.Add(key, v)
}
}
case map[string]string:
for key, value := range params.(map[string]string) {
parsedQuery.Set(key, value)
}
case struct{}:
queryStruct, err := query.Values(params)
if err != nil {
return "", err
}
for key, value := range queryStruct {
for _, v := range value {
parsedQuery.Add(key, v)
}
}
default:
return "", errors.New("params type error, only support string and map[string]string or struct type")
}
rawURL = strings.Join([]string{strings.Replace(parsedURL.String(), "?"+parsedURL.RawQuery, "", -1), parsedQuery.Encode()}, "?")
}
return rawURL, nil
}
// HTTP Request 参数
type ReqOptions struct {
// 请求get参数
Params interface{}
// 设置请求header
Header interface{}
// 整个请求(包括拨号/请求/重定向)等待的最长时间。
Timeout time.Duration
// 上传文件
Files []FileUpload
// 禁用跳转
DisableRedirect bool
// 请求body
Body interface{}
// 基础认证账号密码
BasicAuthUserAndPass []string
// 设置ajax header
IsAjax bool
// 设置JSON header
IsJSON bool
// 设置XML header
IsXML bool
// 自定义http client
HTTPClient *http.Client
// 使用cookie会话
UseCookieJar bool
CookieJar http.CookieJar
// 跳过证书验证
InsecureSkipVerify bool
// 禁用请求gzip压缩
DisableCompression bool
}
func New(options *HTTPOptions) *FireHttp {
if options == nil {
options = &HTTPOptions{}
}
// 设置默认超时
if options.TLSHandshakeTimeout == 0 {
options.TLSHandshakeTimeout = 10 * time.Second
}
if options.DialTimeout == 0 {
options.DialTimeout = 30 * time.Second
}
if options.DialKeepAlive == 0 {
options.DialKeepAlive = 30 * time.Second
}
if options.DNSCacheExpire > 0 {
resolver = dnscache.New(options.DNSCacheExpire)
}
return &FireHttp{
Setting: options,
}
}