-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
95 lines (77 loc) · 1.96 KB
/
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
package main
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
type Client struct {
endpoint string
endpointURL *url.URL
unixHTTPClient *http.Client
dialer func(string, string) (net.Conn, error)
}
// NewVersionedClient returns a Client instance ready for communication with
// the given server endpoint
func NewClient(endpoint string) (*Client, error) {
u, err := url.Parse(endpoint)
if err != nil {
return nil, ErrInvalidEndpoint
}
d := net.Dialer{}
dialFunc := func(network, addr string) (net.Conn, error) {
return d.Dial("unix", u.Path)
}
unixHTTPClient := &http.Client{
Transport: &http.Transport{
Dial: dialFunc,
},
}
return &Client{
unixHTTPClient: unixHTTPClient,
endpoint: endpoint,
endpointURL: u,
dialer: dialFunc,
}, nil
}
// getFakeUnixURL returns the URL needed to make an HTTP request over a UNIX
// domain socket to the given path.
func (c *Client) getFakeUnixURL(path string) string {
u := *c.endpointURL // Copy.
// Override URL so that net/http will not complain.
u.Scheme = "http"
u.Host = "unix.sock" // Doesn't matter what this is - it's not used.
u.Path = ""
urlStr := strings.TrimRight(u.String(), "/")
return fmt.Sprintf("%s%s", urlStr, path)
}
func (c *Client) do(method, path string, body io.Reader) (*http.Response, error) {
var u string
httpClient := c.unixHTTPClient
u = c.getFakeUnixURL(path)
req, err := http.NewRequest(method, u, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
if method == "POST" {
req.Header.Set("Content-Type", "application/json")
}
resp, err := httpClient.Do(req)
if logResponse {
if respBytes, err := httputil.DumpResponse(resp, true); err == nil {
log.Printf("Response:\n%v\n", string(respBytes))
}
}
if err != nil {
if strings.Contains(err.Error(), "connection refused") {
return nil, ErrConnectionRefused
}
return resp, err
}
return resp, nil
}