-
Notifications
You must be signed in to change notification settings - Fork 0
/
xhttp.go
42 lines (36 loc) · 822 Bytes
/
xhttp.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
package xhttp
import (
"crypto/tls"
"net"
"net/http"
"time"
"github.com/buglloc/certifi"
)
const (
dialTimeout = 1 * time.Second
requestTimeout = 5 * time.Second
keepAlive = 60 * time.Second
)
func NewHTTPClient() *http.Client {
return &http.Client{
Transport: NewTransport(),
Timeout: requestTimeout,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
}
func NewTransport() http.RoundTripper {
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.TLSClientConfig = NewTLSClientConfig()
transport.DialContext = (&net.Dialer{
Timeout: dialTimeout,
KeepAlive: keepAlive,
}).DialContext
return transport
}
func NewTLSClientConfig() *tls.Config {
return &tls.Config{
RootCAs: certifi.NewCertPool(),
}
}