-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
95 lines (88 loc) · 2.64 KB
/
client.go
File metadata and controls
95 lines (88 loc) · 2.64 KB
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
package sidecar
import (
"context"
"crypto/tls"
"net"
"net/http"
"strings"
"time"
)
// Client represents a gRPC client and includes an http.Client,
// a host name, and a header to be sent with all requests.
type Client struct {
Host string
Header http.Header
HttpClient *http.Client
}
type ClientOptions struct {
Address string
Insecure bool
Headers []string
}
// NewClient creates a client representation from an address.
// Addresses must be in the format "HOSTNAME:PORT" or "unix:@SOCKET".
// Connections to port 443 use TLS. All others are cleartext (h2c).
func NewClient(options ClientOptions) *Client {
// Expect TLS on port 443 and use the default HTTP client.
if strings.HasSuffix(options.Address, ":443") {
return (&Client{
Host: "https://" + options.Address,
Header: defaultHeader(),
HttpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: options.Insecure},
},
},
}).addHeaders(options.Headers)
}
// All other clients need h2c-only support (HTTP/2 cleartext).
protocols := new(http.Protocols)
protocols.SetUnencryptedHTTP2(true) // Enable h2c (HTTP/2 cleartext)
protocols.SetHTTP1(false) // Explicitly disable HTTP/1.1
protocols.SetHTTP2(false) // Explicitly disable encrypted HTTP/2 (HTTPS)
// If required, create a client that can call unix sockets.
if strings.HasPrefix(options.Address, "unix:") {
address := strings.TrimPrefix(options.Address, "unix:")
return (&Client{
Host: "http://socket", // The name "socket" is arbitrary.
Header: defaultHeader(),
HttpClient: &http.Client{
Transport: &http.Transport{
Protocols: protocols,
DialContext: func(ctx context.Context, _ string, _ string) (net.Conn, error) {
return net.DialTimeout("unix", address, 5*time.Second)
},
},
},
}).addHeaders(options.Headers)
}
// Create a client for networked h2c connections.
return (&Client{
Host: "http://" + options.Address,
Header: defaultHeader(),
HttpClient: &http.Client{
Transport: &http.Transport{
Protocols: protocols,
},
},
}).addHeaders(options.Headers)
}
func (c *Client) Close() {
c.HttpClient.Transport.(*http.Transport).CloseIdleConnections()
}
func defaultHeader() http.Header {
var header http.Header
header = make(map[string][]string)
header.Set("Content-Type", "application/grpc")
header.Set("TE", "trailers")
return header
}
func (client *Client) addHeaders(headers []string) *Client {
for _, h := range headers {
parts := strings.Split(h, ":")
if len(parts) == 2 {
client.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
}
return client
}