/
httpc.go
844 lines (769 loc) · 19.1 KB
/
httpc.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//Copyright 2018 The axx Authors. All rights reserved.
package httpc
import (
"bytes"
"compress/gzip"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"path"
"strings"
"time"
"github.com/axfor/bast/logs"
"github.com/axfor/bast/service"
"gopkg.in/yaml.v2"
)
var (
//DefaultRetries default retries 3
DefaultRetries = 3
//DefaultCookieJar default cookie jar
DefaultCookieJar http.CookieJar
//DefaultTransport default transport
defaultTransport *http.Transport
//DefaultClient HTTP clientdefault
defaultClient *http.Client
before func(*Client) error
after func(*Client)
defaultDiscovery *service.Discovery
)
//Client http client
type Client struct {
Req *http.Request
files map[string]string
params url.Values
client *http.Client
Conf Settings
Tag string
resp *http.Response
err error
body []byte
bodyClose bool
}
//Settings of Client
type Settings struct {
Transport http.RoundTripper
EnableCookie bool
Retry int
Log bool
Title, LogTitle string
}
// MarkTag sets an tag field
func (c *Client) MarkTag(tag string) *Client {
c.Tag = tag
return c
}
// Client set http.Client
func (c *Client) Client(client *http.Client) *Client {
if client != nil {
c.client = client
} else {
c.client = defaultClient
}
return c
}
// Request set http.Request
func (c *Client) Request(request *http.Request) *Client {
if request != nil {
c.Req = request
}
return c
}
// Title set title
func (c *Client) Title(title string) *Client {
c.Conf.Title = title
return c
}
// Logging set log
func (c *Client) Logging() *Client {
c.Conf.Log = true
return c
}
// Unlogging unset log
func (c *Client) Unlogging() *Client {
c.Conf.Log = false
return c
}
// Transport specifies the mechanism by which individual
// HTTP requests are made.
// If nil, DefaultTransport is used.
func (c *Client) Transport(transport http.RoundTripper) *Client {
c.Conf.Transport = transport
return c
}
// NewTransport new *http.Transport
func NewTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}
// Settings set settings
// If nil, DefaultSettings is used.
func (c *Client) Settings(settings Settings) *Client {
c.Conf = settings
return c
}
// Retries maximum retries count
func (c *Client) Retries(retries int) *Client {
c.Conf.Retry = retries
return c
}
// EnableCookie set enable cookie jar
func (c *Client) EnableCookie(enableCookie bool) *Client {
c.Conf.EnableCookie = enableCookie
return c
}
// UserAgent sets User-Agent header field
func (c *Client) UserAgent(useragent string) *Client {
if c.Req.UserAgent() == "" {
c.Req.Header.Set("User-Agent", useragent)
}
return c
}
// Accept sets Accept header field
func (c *Client) Accept(accept string) *Client {
c.Req.Header.Set("Accept", accept)
return c
}
// 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 (c *Client) SetBasicAuth(username, password string) *Client {
c.Req.SetBasicAuth(username, password)
return c
}
//CheckRedirect proxy method http.Get
func (c *Client) CheckRedirect(checkRedirect func(req *http.Request, via []*http.Request) error) *Client {
c.client.CheckRedirect = checkRedirect
return c
}
//Jar proxy method http.Get
func (c *Client) Jar(jar http.CookieJar) *Client {
c.client.Jar = jar
if jar != nil {
c.Conf.EnableCookie = true
}
return c
}
// Header add header item string in request.
func (c *Client) Header(key, value string) *Client {
c.Req.Header.Set(key, value)
if key == "Cookie" {
c.Conf.EnableCookie = true
}
return c
}
// Cookie add cookie into request.
func (c *Client) Cookie(cookie *http.Cookie) *Client {
c.Req.AddCookie(cookie)
if cookie != nil {
c.Conf.EnableCookie = true
}
return c
}
// File add file into request.
func (c *Client) File(fileName, path string) *Client {
if c.files == nil {
c.files = map[string]string{}
}
c.files[fileName] = path
return c
}
// Files add files into request.
func (c *Client) Files(paths map[string]string) *Client {
if c.files == nil {
c.files = map[string]string{}
}
for k, v := range paths {
c.files[k] = v
}
return c
}
// Param adds query param in to request.
// params build query string as ?key1=value1&key2=value2...
func (c *Client) Param(key string, value interface{}) *Client {
if c.params == nil {
c.params = url.Values{}
}
if param, ok := c.params[key]; ok {
c.params[key] = append(param, fmt.Sprint(value))
} else {
c.params[key] = []string{fmt.Sprint(value)}
}
return c
}
// Params adds query param in to request.
// params build query string as ?key1=value1&key2=value2...
func (c *Client) Params(values url.Values) *Client {
c.params = values
return c
}
// Body adds request raw body.
func (c *Client) Body(data interface{}) *Client {
switch t := data.(type) {
case string:
c.Req.Body = ioutil.NopCloser(bytes.NewBufferString(t))
c.Req.ContentLength = int64(len(t))
return c
case []byte:
c.Req.Body = ioutil.NopCloser(bytes.NewBuffer(t))
c.Req.ContentLength = int64(len(t))
return c
case uint, uint8, uint16, uint32, uint64:
case int, int8, int16, int32, int64:
case float32, float64:
default:
return c
}
v := fmt.Sprint(data)
c.Req.Body = ioutil.NopCloser(bytes.NewBufferString(v))
c.Req.ContentLength = int64(len(v))
return c
}
// JSONBody adds request raw body encoding by JSON.
func (c *Client) JSONBody(obj interface{}) *Client {
_, err := c.JSONBodyWithError(obj)
if err != nil {
c.err = err
return c
}
return c
}
// JSONBodyWithError adds request raw body encoding by JSON.
func (c *Client) JSONBodyWithError(obj interface{}) (*Client, error) {
if c.Req.Body == nil && obj != nil {
data, err := json.Marshal(obj)
if err != nil {
return c, err
}
c.BodyWithContentType(data, "application/json")
}
return c, nil
}
// XMLBody adds request raw body encoding by XML.
func (c *Client) XMLBody(obj interface{}) *Client {
_, err := c.XMLBodyWithError(obj)
if err != nil {
c.err = err
return c
}
return c
}
// XMLBodyWithError adds request raw body encoding by XML.
func (c *Client) XMLBodyWithError(obj interface{}) (*Client, error) {
if c.Req.Body == nil && obj != nil {
data, err := xml.Marshal(obj)
if err != nil {
return c, err
}
c.BodyWithContentType(data, "application/xml")
}
return c, nil
}
// YAMLBody adds request raw body encoding by YAML.
func (c *Client) YAMLBody(obj interface{}) *Client {
_, err := c.YAMLBodyWithError(obj)
if err != nil {
c.err = err
return c
}
return c
}
// YAMLBodyWithError adds request raw body encoding by YAML.
func (c *Client) YAMLBodyWithError(obj interface{}) (*Client, error) {
if c.Req.Body == nil && obj != nil {
data, err := yaml.Marshal(obj)
if err != nil {
return c, err
}
c.BodyWithContentType(data, "application/x+yaml")
}
return c, nil
}
// BodyWithContentType adds request raw body encoding by XML.
func (c *Client) BodyWithContentType(data []byte, contentType string) *Client {
if c.Req.Body == nil && data != nil && len(data) > 0 {
c.Req.Body = ioutil.NopCloser(bytes.NewReader(data))
c.Req.ContentLength = int64(len(data))
c.Req.Header.Set("Content-Type", contentType)
}
return c
}
func (c *Client) Error() error {
if c.err != nil {
return c.err
}
return nil
}
// HasError has error
func (c *Client) HasError() bool {
return c.err != nil
}
// OK status code is 200
func (c *Client) OK() bool {
if c.resp == nil {
c.Bytes()
}
return c.err == nil && c.resp != nil && c.resp.StatusCode == http.StatusOK
}
func (c *Client) String() (string, error) {
data, err := c.Bytes()
if err != nil {
return "", err
}
return string(data), nil
}
// Result returns the map that marshals from the body bytes as json or xml or yaml in response .
// default json
func (c *Client) Result(v interface{}) error {
data, err := c.Bytes()
if err != nil {
return err
}
contentType := c.resp.Header.Get("Content-Type")
if contentType == "" || strings.HasPrefix(contentType, "application/json") {
return json.Unmarshal(data, v)
} else if strings.HasPrefix(contentType, "application/xml") {
return xml.Unmarshal(data, v)
} else if strings.HasPrefix(contentType, "application/x+yaml") {
return yaml.Unmarshal(data, v)
}
return json.Unmarshal(data, v)
}
// ToJSON returns the map that marshals from the body bytes as json in response .
// it calls Response inner.
func (c *Client) ToJSON(v interface{}) error {
data, err := c.Bytes()
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
// ToMap returns the map that marshals from the body bytes as json in response .
// it calls Response inner.
func (c *Client) ToMap(v *map[string]interface{}) error {
data, err := c.Bytes()
if err != nil {
return err
}
return json.Unmarshal(data, v)
}
// ToXML returns the map that marshals from the body bytes as xml in response .
// it calls Response inner.
func (c *Client) ToXML(v interface{}) error {
data, err := c.Bytes()
if err != nil {
return err
}
return xml.Unmarshal(data, v)
}
// ToYAML returns the map that marshals from the body bytes as yaml in response .
// it calls Response inner.
func (c *Client) ToYAML(v interface{}) error {
data, err := c.Bytes()
if err != nil {
return err
}
return yaml.Unmarshal(data, v)
}
// ToFile saves the body data in response to one file.
// it calls Response inner.
func (c *Client) ToFile(filename string) error {
_, err := c.getResponse()
if err != nil {
return err
}
if c.resp.Body == nil {
return nil
}
defer c.resp.Body.Close()
c.bodyClose = true
err = pathExistAndMkdir(filename)
if err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, c.resp.Body)
return err
}
//Check that the file directory exists, there is no automatically created
func pathExistAndMkdir(filename string) (err error) {
filename = path.Dir(filename)
_, err = os.Stat(filename)
if err == nil {
return nil
}
if os.IsNotExist(err) {
err = os.MkdirAll(filename, os.ModePerm)
if err == nil {
return nil
}
}
return err
}
// Bytes returns the body []byte in response.
// it calls Response inner.
func (c *Client) Bytes() ([]byte, error) {
if c.body != nil {
return c.body, nil
}
_, err := c.getResponse()
if err != nil {
return nil, err
}
if c.resp.Body == nil {
return nil, errors.New("empty body")
}
defer c.resp.Body.Close()
c.bodyClose = true
if c.resp.Header.Get("Content-Encoding") == "gzip" {
reader, err := gzip.NewReader(c.resp.Body)
if err != nil {
return nil, err
}
c.body, err = ioutil.ReadAll(reader)
return c.body, err
}
c.body, err = ioutil.ReadAll(c.resp.Body)
return c.body, err
}
func (c *Client) getResponse() (*http.Response, error) {
if c.resp != nil {
return c.resp, nil
}
if c.resp == nil {
r, err := c.Do()
if err == nil {
return r, nil
}
return nil, err
}
return nil, errors.New("empty response")
}
// Get issues a GET to the specified URL. If the response is one of
// the following redirect codes, Get follows the redirect, up to a
// maximum of 10 redirects:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
//
// An error is returned if there were too many redirects or if there
// was an HTTP protocol error. A non-2xx response doesn't cause an
// error. Any returned error will be of type *url.Error. The url.Error
// value's Timeout method will report true if request timed out or was
// canceled.
//
// When err is nil, resp always contains a non-nil resp.Body.
// Caller should close resp.Body when done reading from it.
//
// Get is a wrapper around DefaultClient.Get.
//
// To make a request with custom headers, use NewRequest and
// DefaultClient.Do.
func Get(url string) *Client {
return createRequest(url, http.MethodGet)
}
//Gets returns *Client with service name ang http get method
func Gets(serviceName string) *Client {
return Service(serviceName, http.MethodGet)
}
// Post issues a POST to the specified URL.
//
// Caller should close resp.Body when done reading from it.
//
// If the provided body is an io.Closer, it is closed after the
// request.
//
// Post is a wrapper around DefaultClient.Post.
//
// To set custom headers, use NewRequest and DefaultClient.Do.
//
// See the Client.Do method documentation for details on how redirects
// are handled.
func Post(url string) *Client {
return createRequest(url, http.MethodPost)
}
//Posts returns *Client with service name ang http post method
func Posts(serviceName string) *Client {
return Service(serviceName, http.MethodPost)
}
// Head issues a HEAD to the specified URL. If the response is one of
// the following redirect codes, Head follows the redirect, up to a
// maximum of 10 redirects:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
// 308 (Permanent Redirect)
//
// Head is a wrapper around DefaultClient.Head
func Head(url string) *Client {
return createRequest(url, http.MethodHead)
}
//Heads returns *Client with service name ang http head method
func Heads(serviceName string) *Client {
return Service(serviceName, http.MethodHead)
}
// Put returns *Client with PUT method
func Put(url string) *Client {
return createRequest(url, http.MethodPut)
}
//Puts returns *Client with service name ang http put method
func Puts(serviceName string) *Client {
return Service(serviceName, http.MethodPut)
}
// Delete returns *Client with Delete method
func Delete(url string) *Client {
return createRequest(url, http.MethodDelete)
}
//Deletes returns *Client with service name ang http delete method
func Deletes(serviceName string) *Client {
return Service(serviceName, http.MethodDelete)
}
// Patch returns *Client with Patch method
func Patch(url string) *Client {
return createRequest(url, http.MethodPatch)
}
//Patchs returns *Client with service name ang http patch method
func Patchs(serviceName string) *Client {
return Service(serviceName, http.MethodPatch)
}
//Service returns *Client with service name ang http xxx method
func Service(serviceName, method string) *Client {
uri := ""
if serviceName != "" && defaultDiscovery != nil {
uri = defaultDiscovery.Name(serviceName)
}
return createRequest(uri, method)
}
func createRequest(uri, method string) *Client {
u, _ := url.Parse(uri)
c := &Client{
client: defaultClient,
Req: &http.Request{
URL: u,
Method: method,
Header: make(http.Header),
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
},
files: map[string]string{},
err: nil,
}
return c
}
// Do add files into request
func (c *Client) Do() (resp *http.Response, err error) {
c.resp = nil
if c.err != nil {
return nil, c.err
}
err = c.build()
if err != nil {
return
}
err = callBefore(c)
if err != nil {
return
}
title := "httpc access"
if c.Conf.Title != "" {
title = c.Conf.Title
}
if c.Conf.Log {
logs.Info(title,
logs.String("module", "httpc"),
logs.String("stage", "start"),
logs.String("url", c.Req.URL.String()),
logs.String("method", c.Req.Method),
logs.String("tag", c.Tag),
)
}
st := time.Now()
rc := 0
for ; c.Conf.Retry == -1 || rc <= c.Conf.Retry; rc++ {
resp, err = c.client.Do(c.Req)
if err == nil {
break
}
}
c.resp = resp
callAfter(c)
if c.Conf.Log {
statusCode := 0
statusText := ""
if c.resp != nil {
statusCode = c.resp.StatusCode
statusText = c.resp.Status
}
logs.Info(title,
logs.String("module", "httpc"),
logs.String("stage", "finish"),
logs.String("url", c.Req.URL.String()),
logs.String("method", c.Req.Method),
logs.Int("retries", rc),
logs.String("cost", time.Since(st).String()),
logs.Int("statusCode", statusCode),
logs.String("statusText", statusText),
logs.String("tag", c.Tag),
)
}
return
}
func (c *Client) build() error {
if c.Conf.EnableCookie && c.client.Jar == nil {
c.client.Jar = DefaultCookieJar
}
if c.Conf.Transport != nil {
if c.client.Transport != c.Conf.Transport {
c.client.Transport = c.Conf.Transport
}
} else {
if c.client.Transport != defaultTransport {
c.client.Transport = defaultTransport
}
}
var urlParam string
var buf bytes.Buffer
for k, v := range c.params {
for i, vv := range v {
if i > 0 {
buf.WriteByte('&')
}
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(vv))
}
}
urlParam = buf.String()
has := len(c.params) > 0
if c.Req.Method == http.MethodGet {
if has {
rurl := c.Req.URL.String()
if strings.Contains(rurl, "?") {
rurl += "&" + urlParam
} else {
rurl += "?" + urlParam
}
urls, err := url.Parse(rurl)
if err != nil {
return err
}
c.Req.URL = urls
}
return nil
}
if (c.Req.Method == http.MethodPost || c.Req.Method == http.MethodPut || c.Req.Method == http.MethodPatch ||
c.Req.Method == http.MethodDelete) && c.Req.Body == nil {
if len(c.files) > 0 {
bodyBuffer := &bytes.Buffer{}
bodyWriter := multipart.NewWriter(bodyBuffer)
for formname, filename := range c.files {
fileWriter, err := bodyWriter.CreateFormFile(formname, filename)
if err != nil {
return err
}
fh, err := os.Open(filename)
if err != nil {
return err
}
//iocopy
_, err = io.Copy(fileWriter, fh)
fh.Close()
if err != nil {
return err
}
}
for k, v := range c.params {
for _, vv := range v {
bodyWriter.WriteField(k, vv)
}
}
bodyWriter.Close()
c.Header("Content-Type", bodyWriter.FormDataContentType())
c.Req.Body = ioutil.NopCloser(bodyBuffer)
} else if has {
c.Header("Content-Type", "application/x-www-form-urlencoded")
c.Body(urlParam)
}
}
return nil
}
//Clear all response data
func (c *Client) Clear() *Client {
if c.resp != nil && !c.bodyClose {
c.resp.Body.Close()
}
c.resp = nil
c.body = nil
c.bodyClose = false
c.err = nil
c.files = map[string]string{}
c.params = url.Values{}
c.Conf = Settings{}
c.Tag = ""
return c
}
func callBefore(c *Client) error {
if before != nil {
return before(c)
}
return nil
}
func callAfter(c *Client) {
if after != nil {
after(c)
}
}
//Before before handler for each network request
func Before(fn func(*Client) error) {
before = fn
}
//After after handler for each network request
func After(fn func(*Client)) {
after = fn
}
//InitDiscovery init service discovery
func InitDiscovery(discovery *service.Discovery) {
defaultDiscovery = discovery
}
//init
func init() {
DefaultCookieJar, _ = cookiejar.New(nil)
defaultTransport = NewTransport()
defaultClient = &http.Client{}
defaultClient.Transport = defaultTransport
}