forked from grafana/grafana-plugin-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
249 lines (205 loc) · 7.23 KB
/
http_client.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
package httpclient
import (
"crypto/tls"
"crypto/x509"
"errors"
"net"
"net/http"
)
// New creates a new http.Client.
// If opts is nil the http.DefaultClient will be returned.
// If no middlewares are provided the DefaultMiddlewares will be used. If you
// provide middlewares you have to manually add the DefaultMiddlewares for it to be
// enabled.
// Note: Middlewares will be executed in the same order as provided.
// Note: If more than one Options is provided a panic is raised.
func New(opts ...Options) (*http.Client, error) {
if opts == nil {
return http.DefaultClient, nil
}
clientOpts := createOptions(opts...)
transport, err := GetTransport(clientOpts)
if err != nil {
return nil, err
}
c := &http.Client{
Transport: transport,
Timeout: clientOpts.Timeouts.Timeout,
}
if clientOpts.ConfigureClient != nil {
clientOpts.ConfigureClient(clientOpts, c)
}
return c, nil
}
// GetTransport creates a new http.RoundTripper given provided options.
// If opts is nil the http.DefaultTransport will be returned.
// If no middlewares are provided the DefaultMiddlewares() will be used. If you
// provide middlewares you have to manually add the DefaultMiddlewares() for it to be
// enabled.
// Note: Middlewares will be executed in the same order as provided.
// Note: If more than one Options is provided a panic is raised.
func GetTransport(opts ...Options) (http.RoundTripper, error) {
if opts == nil {
return http.DefaultTransport, nil
}
clientOpts := createOptions(opts...)
tlsConfig, err := GetTLSConfig(clientOpts)
if err != nil {
return nil, err
}
if clientOpts.ConfigureTLSConfig != nil {
clientOpts.ConfigureTLSConfig(clientOpts, tlsConfig)
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: clientOpts.Timeouts.DialTimeout,
KeepAlive: clientOpts.Timeouts.KeepAlive,
}).DialContext,
ResponseHeaderTimeout: clientOpts.Timeouts.Timeout,
TLSHandshakeTimeout: clientOpts.Timeouts.TLSHandshakeTimeout,
ExpectContinueTimeout: clientOpts.Timeouts.ExpectContinueTimeout,
MaxConnsPerHost: clientOpts.Timeouts.MaxConnsPerHost,
MaxIdleConns: clientOpts.Timeouts.MaxIdleConns,
MaxIdleConnsPerHost: clientOpts.Timeouts.MaxIdleConnsPerHost,
IdleConnTimeout: clientOpts.Timeouts.IdleConnTimeout,
}
if clientOpts.ConfigureTransport != nil {
clientOpts.ConfigureTransport(clientOpts, transport)
}
if clientOpts.ConfigureMiddleware != nil {
clientOpts.Middlewares = clientOpts.ConfigureMiddleware(clientOpts, clientOpts.Middlewares)
}
return roundTripperFromMiddlewares(clientOpts, clientOpts.Middlewares, transport), nil
}
// GetTLSConfig creates a new tls.Config given provided options.
// Note: If more than one Options is provided a panic is raised.
func GetTLSConfig(opts ...Options) (*tls.Config, error) {
clientOpts := createOptions(opts...)
if clientOpts.TLS == nil {
// #nosec
return &tls.Config{}, nil
}
tlsOpts := clientOpts.TLS
// #nosec
config := &tls.Config{
InsecureSkipVerify: tlsOpts.InsecureSkipVerify,
ServerName: tlsOpts.ServerName,
}
if len(tlsOpts.CACertificate) > 0 {
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM([]byte(tlsOpts.CACertificate))
if !ok {
return nil, errors.New("failed to parse TLS CA PEM certificate")
}
config.RootCAs = caPool
}
if len(tlsOpts.ClientCertificate) > 0 && len(tlsOpts.ClientKey) > 0 {
cert, err := tls.X509KeyPair([]byte(tlsOpts.ClientCertificate), []byte(tlsOpts.ClientKey))
if err != nil {
return nil, err
}
config.Certificates = []tls.Certificate{cert}
}
if tlsOpts.MinVersion > 0 {
config.MinVersion = tlsOpts.MinVersion
}
if tlsOpts.MaxVersion > 0 {
config.MaxVersion = tlsOpts.MaxVersion
}
return config, nil
}
func createOptions(providedOpts ...Options) Options {
var opts Options
switch len(providedOpts) {
case 0:
opts = Options{}
case 1:
opts = providedOpts[0]
default:
panic("only an empty or one Options is valid as argument")
}
if opts.Timeouts == nil {
opts.Timeouts = &DefaultTimeoutOptions
}
if opts.Middlewares == nil {
opts.Middlewares = DefaultMiddlewares()
}
return opts
}
// The RoundTripperFunc type is an adapter to allow the use of ordinary
// functions as RoundTrippers. If f is a function with the appropriate
// signature, RountTripperFunc(f) is a RoundTripper that calls f.
type RoundTripperFunc func(req *http.Request) (*http.Response, error)
// RoundTrip implements the RoundTripper interface.
func (rt RoundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return rt(r)
}
// Middleware is an interface representing the ability to create a middleware
// that implements the http.RoundTripper interface.
type Middleware interface {
// CreateMiddleware creates a new middleware.
CreateMiddleware(opts Options, next http.RoundTripper) http.RoundTripper
}
// The MiddlewareFunc type is an adapter to allow the use of ordinary
// functions as Middlewares. If f is a function with the appropriate
// signature, MiddlewareFunc(f) is a Middleware that calls f.
type MiddlewareFunc func(opts Options, next http.RoundTripper) http.RoundTripper
// CreateMiddleware implements the Middleware interface.
func (fn MiddlewareFunc) CreateMiddleware(opts Options, next http.RoundTripper) http.RoundTripper {
return fn(opts, next)
}
// MiddlewareName is an interface representing the ability for a middleware to have a name.
type MiddlewareName interface {
// MiddlewareName returns the middleware name.
MiddlewareName() string
}
// ConfigureMiddlewareFunc function signature for configuring middleware chain.
type ConfigureMiddlewareFunc func(opts Options, existingMiddleware []Middleware) []Middleware
// DefaultMiddlewares is the default middleware applied when creating
// new HTTP clients and no middleware is provided.
// BasicAuthenticationMiddleware and CustomHeadersMiddleware are
// the default middlewares.
func DefaultMiddlewares() []Middleware {
return []Middleware{
BasicAuthenticationMiddleware(),
CustomHeadersMiddleware(),
ContextualMiddleware(),
}
}
func roundTripperFromMiddlewares(opts Options, middlewares []Middleware, finalRoundTripper http.RoundTripper) http.RoundTripper {
reversed := reverseMiddlewares(middlewares)
next := finalRoundTripper
for _, m := range reversed {
next = m.CreateMiddleware(opts, next)
}
return next
}
func reverseMiddlewares(middlewares []Middleware) []Middleware {
reversed := make([]Middleware, len(middlewares))
copy(reversed, middlewares)
for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
reversed[i], reversed[j] = reversed[j], reversed[i]
}
return reversed
}
type namedMiddleware struct {
Name string
Middleware Middleware
}
// NamedMiddlewareFunc type is an adapter to allow the use of ordinary
// functions as Middleware. If f is a function with the appropriate
// signature, NamedMiddlewareFunc(f) is a Middleware that calls f.
func NamedMiddlewareFunc(name string, fn MiddlewareFunc) Middleware {
return &namedMiddleware{
Name: name,
Middleware: fn,
}
}
func (nm *namedMiddleware) CreateMiddleware(opts Options, next http.RoundTripper) http.RoundTripper {
return nm.Middleware.CreateMiddleware(opts, next)
}
func (nm *namedMiddleware) MiddlewareName() string {
return nm.Name
}