forked from kubeedge/kubeedge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
125 lines (109 loc) · 3.02 KB
/
httpclient.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
package util
import (
"bytes"
"crypto/tls"
"errors"
"net/http"
"strings"
"time"
)
// SignRequest sign a http request so that it can talk to API Server
var SignRequest func(*http.Request) error
// URLClientOption is a struct which provides options for client
type URLClientOption struct {
SSLEnabled bool
TLSConfig *tls.Config
Compressed bool
HandshakeTimeout time.Duration
ResponseHeaderTimeout time.Duration
Verbose bool
}
// URLClient is a struct used for storing details of a client
type URLClient struct {
*http.Client
TLS *tls.Config
Request *http.Request
options URLClientOption
}
// HTTPDo is a method used for http connection
func (client *URLClient) HTTPDo(method, rawURL string, headers http.Header, body []byte) (resp *http.Response, err error) {
client.clientHasPrefix(rawURL, "https")
if headers == nil {
headers = make(http.Header)
}
if _, ok := headers["Accept"]; !ok {
headers["Accept"] = []string{"*/*"}
}
if _, ok := headers["Accept-Encoding"]; !ok && client.options.Compressed {
headers["Accept-Encoding"] = []string{"deflate, gzip"}
}
req, err := http.NewRequest(method, rawURL, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
client.Request = req
req.Header = headers
//sign a request
if SignRequest != nil {
if err = SignRequest(req); err != nil {
return nil, errors.New("Add auth info failed, err: " + err.Error())
}
}
resp, err = client.Client.Do(req)
if err != nil {
return nil, err
}
return resp, nil
}
func (client *URLClient) clientHasPrefix(url, pro string) {
if strings.HasPrefix(url, pro) {
if transport, ok := client.Client.Transport.(*http.Transport); ok {
transport.TLSClientConfig = client.TLS
}
}
}
// DefaultURLClientOption is a struct object which has default client option
var DefaultURLClientOption = &URLClientOption{
Compressed: true,
HandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 60 * time.Second,
}
// GetURLClient is a function which sets client options
func GetURLClient(option *URLClientOption) (client *URLClient, err error) {
if option == nil {
option = DefaultURLClientOption
} else {
switch {
case option.HandshakeTimeout == 0:
option.HandshakeTimeout = DefaultURLClientOption.HandshakeTimeout
fallthrough
case option.ResponseHeaderTimeout == 0:
option.ResponseHeaderTimeout = DefaultURLClientOption.HandshakeTimeout
}
}
if !option.SSLEnabled {
client = &URLClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: option.HandshakeTimeout,
ResponseHeaderTimeout: option.ResponseHeaderTimeout,
DisableCompression: !option.Compressed,
},
},
options: *option,
}
return
}
client = &URLClient{
Client: &http.Client{
Transport: &http.Transport{
TLSHandshakeTimeout: option.HandshakeTimeout,
ResponseHeaderTimeout: option.ResponseHeaderTimeout,
DisableCompression: !option.Compressed,
},
},
TLS: option.TLSConfig,
options: *option,
}
return
}