/
httputil.go
101 lines (93 loc) · 3.22 KB
/
httputil.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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package httputil
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"time"
"github.com/vespa-engine/vespa/client/go/internal/build"
"golang.org/x/net/http2"
)
// Client represents a HTTP client usable by the Vespa CLI.
type Client interface {
Do(request *http.Request, timeout time.Duration) (response *http.Response, error error)
}
type defaultClient struct {
client *http.Client
}
func (c *defaultClient) Do(request *http.Request, timeout time.Duration) (response *http.Response, error error) {
if c.client.Timeout != timeout { // Set wanted timeout
c.client.Timeout = timeout
}
if request.Header == nil {
request.Header = make(http.Header)
}
request.Header.Set("User-Agent", fmt.Sprintf("Vespa CLI/%s", build.Version))
return c.client.Do(request)
}
// ConfigureTLS configures the given client with given certificates and caCertificate. If trustAll is true, the client
// will skip verification of the certificate chain.
func ConfigureTLS(client Client, certificates []tls.Certificate, caCertificate []byte, trustAll bool) {
c, ok := client.(*defaultClient)
if !ok {
return
}
var tlsConfig *tls.Config = nil
if certificates != nil {
tlsConfig = &tls.Config{
Certificates: certificates,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: trustAll,
}
if caCertificate != nil {
certs := x509.NewCertPool()
certs.AppendCertsFromPEM(caCertificate)
tlsConfig.RootCAs = certs
}
}
if tr, ok := c.client.Transport.(*http.Transport); ok {
tr.TLSClientConfig = tlsConfig
} else if tr, ok := c.client.Transport.(*http2.Transport); ok {
tr.TLSClientConfig = tlsConfig
} else {
panic(fmt.Sprintf("unknown transport type: %T", c.client.Transport))
}
}
// ForceHTTP2 configures the given client exclusively with a HTTP/2 transport. The other options are passed to
// ConfigureTLS. If certificates is nil, the client will be configured with H2C (HTTP/2 over clear-text).
func ForceHTTP2(client Client, certificates []tls.Certificate, caCertificate []byte, trustAll bool) {
c, ok := client.(*defaultClient)
if !ok {
return
}
var dialFunc func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error)
if certificates == nil {
// No certificate, so force H2C (HTTP/2 over clear-text) by using a non-TLS Dialer
dialer := net.Dialer{}
dialFunc = func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) {
return dialer.DialContext(ctx, network, addr)
}
}
// Use HTTP/2 transport explicitly. Connection reuse does not work properly when using regular http.Transport, even
// though it upgrades to HTTP/2 automatically
// https://github.com/golang/go/issues/16582
// https://github.com/golang/go/issues/22091
c.client.Transport = &http2.Transport{
DisableCompression: true,
AllowHTTP: true,
DialTLSContext: dialFunc,
}
ConfigureTLS(client, certificates, caCertificate, trustAll)
}
// NewClients creates a new HTTP client the given default timeout.
func NewClient(timeout time.Duration) Client {
return &defaultClient{
client: &http.Client{
Timeout: timeout,
Transport: http.DefaultTransport,
},
}
}