-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
82 lines (67 loc) · 1.83 KB
/
http.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
package provider
import (
"crypto/tls"
"net"
"net/http"
"net/http/cookiejar"
"runtime"
"time"
"github.com/briandowns/spinner"
"golang.org/x/net/publicsuffix"
)
// HTTPClient saml2aws http client which extends the existing client
type HTTPClient struct {
http.Client
}
// NewDefaultTransport configure a transport with the TLS skip verify option
func NewDefaultTransport(skipVerify bool) *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
}
}
// NewHTTPClient configure the default http client used by the providers
func NewHTTPClient(tr http.RoundTripper) (*HTTPClient, error) {
options := &cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(options)
if err != nil {
return nil, err
}
client := http.Client{Transport: tr, Jar: jar}
return &HTTPClient{client}, nil
}
// Do do the request
func (hc *HTTPClient) Do(req *http.Request) (*http.Response, error) {
cs := spinner.CharSets[14]
// use a NON unicode spinner for windows
if runtime.GOOS == "windows" {
cs = spinner.CharSets[26]
}
s := spinner.New(cs, 100*time.Millisecond)
defer func() {
s.Stop()
}()
s.Start()
return hc.Client.Do(req)
}
// DisableFollowRedirect disable redirects
func (hc *HTTPClient) DisableFollowRedirect() {
hc.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
// EnableFollowRedirect enable redirects
func (hc *HTTPClient) EnableFollowRedirect() {
hc.CheckRedirect = nil
}