forked from Kopano-dev/konnect
/
http.go
96 lines (83 loc) · 2.81 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright 2017-2019 Kopano and its licensors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package utils
import (
"crypto/tls"
"net"
"net/http"
"time"
"golang.org/x/net/http2"
"stash.kopano.io/kc/konnect/version"
)
const (
defaultHTTPTimeout = 30 * time.Second
defaultHTTPKeepAlive = 30 * time.Second
defaultHTTPMaxIdleConns = 100
defaultHTTPIdleConnTimeout = 90 * time.Second
defaultHTTPTLSHandshakeTimeout = 10 * time.Second
defaultHTTPExpectContinueTimeout = 1 * time.Second
)
// DefaultHTTPUserAgent is the User-Agent Header which should be used when
// making HTTP requests.
var DefaultHTTPUserAgent = "Kopano-Konnect/" + version.Version
// HTTPTransportWithTLSClientConfig creates a new http.Transport with sane
// default settings using the provided tls.Config.
func HTTPTransportWithTLSClientConfig(tlsClientConfig *tls.Config) *http.Transport {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultHTTPTimeout,
KeepAlive: defaultHTTPKeepAlive,
DualStack: true,
}).DialContext,
MaxIdleConns: defaultHTTPMaxIdleConns,
IdleConnTimeout: defaultHTTPIdleConnTimeout,
TLSHandshakeTimeout: defaultHTTPTLSHandshakeTimeout,
ExpectContinueTimeout: defaultHTTPExpectContinueTimeout,
}
if tlsClientConfig != nil {
transport.TLSClientConfig = tlsClientConfig
err := http2.ConfigureTransport(transport)
if err != nil {
panic(err)
}
}
return transport
}
// DefaultTLSConfig returns a new tls.Config.
func DefaultTLSConfig() *tls.Config {
return &tls.Config{
ClientSessionCache: tls.NewLRUClientSessionCache(0),
}
}
// InsecureSkipVerifyTLSConfig returns a new tls.Config which does skip TLS verification.
func InsecureSkipVerifyTLSConfig() *tls.Config {
config := DefaultTLSConfig()
config.InsecureSkipVerify = true
return config
}
// DefaultHTTPClient is a http.Client with a timeout set.
var DefaultHTTPClient = &http.Client{
Timeout: defaultHTTPTimeout,
Transport: HTTPTransportWithTLSClientConfig(DefaultTLSConfig()),
}
// InsecureHTTPClient is a http.Client with a timeout set and with TLS
// varification disabled.
var InsecureHTTPClient = &http.Client{
Timeout: defaultHTTPTimeout,
Transport: HTTPTransportWithTLSClientConfig(InsecureSkipVerifyTLSConfig()),
}