-
Notifications
You must be signed in to change notification settings - Fork 16
/
http_transport.go
144 lines (126 loc) · 4.34 KB
/
http_transport.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
// Package ldhttp provides internal helper functions for custom HTTP configuration.
//
// Applications will not normally need to use this package. Use the HTTP configuration options provided by
// ldcomponents.HTTPConfiguration() instead.
package ldhttp
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
)
const defaultConnectTimeout = 10 * time.Second
type transportExtraOptions struct {
caCerts *x509.CertPool
connectTimeout time.Duration
proxyURL *url.URL
}
// TransportOption is the interface for optional configuration parameters that can be passed to NewHTTPTransport.
type TransportOption interface {
apply(opts *transportExtraOptions) error
}
type connectTimeoutOption struct {
timeout time.Duration
}
func (o connectTimeoutOption) apply(opts *transportExtraOptions) error {
opts.connectTimeout = o.timeout
return nil
}
// ConnectTimeoutOption specifies the maximum time to wait for a TCP connection, when used with
// NewHTTPTransport.
func ConnectTimeoutOption(timeout time.Duration) TransportOption {
return connectTimeoutOption{timeout: timeout}
}
type caCertOption struct {
certData []byte
}
func (o caCertOption) apply(opts *transportExtraOptions) error {
if opts.caCerts == nil {
var err error
opts.caCerts, err = x509.SystemCertPool() // this returns a *copy* of the existing CA certs
if err != nil {
opts.caCerts = x509.NewCertPool() // COVERAGE: can't simulate this condition in unit tests
}
}
if !opts.caCerts.AppendCertsFromPEM(o.certData) {
return errors.New("invalid CA certificate data")
}
return nil
}
// CACertOption specifies a CA certificate to be added to the trusted root CA list for HTTPS requests,
// when used with NewHTTPTransport.
func CACertOption(certData []byte) TransportOption {
return caCertOption{certData: certData}
}
type caCertFileOption struct {
filePath string
}
func (o caCertFileOption) apply(opts *transportExtraOptions) error {
bytes, err := ioutil.ReadFile(o.filePath)
if err != nil {
return fmt.Errorf("can't read CA certificate file: %v", err)
}
return caCertOption{certData: bytes}.apply(opts)
}
// CACertFileOption specifies a CA certificate to be added to the trusted root CA list for HTTPS requests,
// when used with NewHTTPTransport. It reads the certificate data from a file in PEM format.
func CACertFileOption(filePath string) TransportOption {
return caCertFileOption{filePath: filePath}
}
// ProxyOption specifies a proxy URL to be used for all requests, when used with NewHTTPTransport.
// This overrides any setting of the HTTP_PROXY, HTTPS_PROXY, or NO_PROXY environment variables.
func ProxyOption(url url.URL) TransportOption {
return proxyOption{url}
}
type proxyOption struct {
url url.URL
}
func (o proxyOption) apply(opts *transportExtraOptions) error {
opts.proxyURL = &o.url
return nil
}
// NewHTTPTransport creates a customized http.Transport struct using the specified options. It returns both
// the Transport and an associated net.Dialer.
//
// To configure the LaunchDarkly SDK, rather than calling this function directly, it is simpler to use
// the methods provided by ldcomponents.HTTPConfiguration().
func NewHTTPTransport(options ...TransportOption) (*http.Transport, *net.Dialer, error) {
extraOptions := transportExtraOptions{
connectTimeout: defaultConnectTimeout,
}
for _, o := range options {
err := o.apply(&extraOptions)
if err != nil {
return nil, nil, err
}
}
dialer := &net.Dialer{
Timeout: extraOptions.connectTimeout,
KeepAlive: 1 * time.Minute, // see newStreamProcessor for why we are setting this
}
transport := newDefaultTransport()
transport.DialContext = dialer.DialContext
if extraOptions.caCerts != nil {
transport.TLSClientConfig = &tls.Config{RootCAs: extraOptions.caCerts} //nolint:gosec // not setting TLS.MinVersion
}
if extraOptions.proxyURL != nil {
transport.Proxy = http.ProxyURL(extraOptions.proxyURL)
}
return transport, dialer, nil
}
func newDefaultTransport() *http.Transport {
// The reason we don't just make a copy of http.DefaultTransport is that Transport contains a
// sync.Mutex, and copying a lock by value is bad.
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}