-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrappeddHttpClient.go
89 lines (82 loc) · 2.19 KB
/
wrappeddHttpClient.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
package main
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"github.com/certifi/gocertifi"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"io/ioutil"
"net/http"
"sync"
"time"
)
var defaultFastStdHttpClient = &FastStdHttpClient{}
type FastStdHttpClient struct {
*http.Client
once sync.Once
}
var ErrUpstreamTimeout = errors.New("upstream request timeout")
func (f *FastStdHttpClient) DoDeadline(fastReq *fasthttp.Request, fastResp *fasthttp.Response, deadline time.Time) error {
f.init()
ctx, cancel := context.WithDeadline(context.Background(), deadline)
defer cancel()
req, err := http.NewRequestWithContext(ctx, string(fastReq.Header.Method()), fastReq.URI().String(), bytes.NewReader(fastReq.Body()))
if err != nil {
return err
}
fastReq.Header.VisitAll(func(key, value []byte) {
req.Header.Set(string(key), string(value))
})
if log.IsLevelEnabled(log.TraceLevel) {
log.Tracef("requesting to upstream: %s\n%s\n", req.RequestURI, fastReq.String())
}
resp, err := f.Do(req)
if err != nil {
if ctx.Err() != nil {
return ErrUpstreamTimeout
}
return err
}
fastResp.SetStatusCode(resp.StatusCode)
for k, _ := range resp.Header {
fastResp.Header.Set(k, resp.Header.Get(k))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
encodings := resp.Header.Values(fasthttp.HeaderContentEncoding)
var d interface{}
err = json.Unmarshal(body, &d)
if err != nil {
log.Debug(string(body))
}
if len(encodings) == 0 && !isASCII(body) {
log.Info(encodings)
}
fastResp.SetBodyRaw(body)
return nil
}
//init initialize client settings
func (f *FastStdHttpClient) init() {
f.once.Do(func() {
// init client
certPool, err := gocertifi.CACerts()
if err != nil {
panic(err)
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = &tls.Config{RootCAs: certPool}
f.Client = &http.Client{Transport: transport}
// disable redirect following for net/http.Client cause it cannot follow POST request
if f.CheckRedirect == nil {
f.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
})
}