forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
router_connection.go
118 lines (102 loc) · 3.06 KB
/
router_connection.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
package router
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/router/routererror"
)
// ConnectionConfig is for configuring the RouterConnection
type ConnectionConfig struct {
DialTimeout time.Duration
SkipSSLValidation bool
}
// RouterConnection represents the connection to Router
type RouterConnection struct {
HTTPClient *http.Client
}
// NewConnection returns a pointer to a new RouterConnection with the provided configuration
func NewConnection(config ConnectionConfig) *RouterConnection {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.SkipSSLValidation,
},
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
KeepAlive: 30 * time.Second,
Timeout: config.DialTimeout,
}).DialContext,
}
return &RouterConnection{
HTTPClient: &http.Client{Transport: tr},
}
}
// Make performs the request and parses the response.
func (connection *RouterConnection) Make(request *Request, responseToPopulate *Response) error {
// In case this function is called from a retry, passedResponse may already
// be populated with a previous response. We reset in case there's an HTTP
// error and we don't repopulate it in populateResponse.
responseToPopulate.reset()
httpResponse, err := connection.HTTPClient.Do(request.Request)
if err != nil {
// request could not be made, e.g., ssl handshake or tcp dial timeout
return connection.processRequestErrors(request.Request, err)
}
return connection.populateResponse(httpResponse, responseToPopulate)
}
func (*RouterConnection) handleStatusCodes(httpResponse *http.Response, responseToPopulate *Response) error {
if httpResponse.StatusCode >= 400 {
return routererror.RawHTTPStatusError{
StatusCode: httpResponse.StatusCode,
RawResponse: responseToPopulate.RawResponse,
}
}
return nil
}
func (connection *RouterConnection) populateResponse(httpResponse *http.Response, responseToPopulate *Response) error {
responseToPopulate.HTTPResponse = httpResponse
rawBytes, err := ioutil.ReadAll(httpResponse.Body)
defer httpResponse.Body.Close()
if err != nil {
return err
}
responseToPopulate.RawResponse = rawBytes
err = connection.handleStatusCodes(httpResponse, responseToPopulate)
if err != nil {
return err
}
if responseToPopulate.Result != nil {
decoder := json.NewDecoder(bytes.NewBuffer(responseToPopulate.RawResponse))
decoder.UseNumber()
err = decoder.Decode(responseToPopulate.Result)
if err != nil {
return err
}
}
return nil
}
func (*RouterConnection) processRequestErrors(request *http.Request, err error) error {
switch e := err.(type) {
case *url.Error:
switch urlErr := e.Err.(type) {
case x509.UnknownAuthorityError:
return ccerror.UnverifiedServerError{
URL: request.URL.String(),
}
case x509.HostnameError:
return ccerror.SSLValidationHostnameError{
Message: urlErr.Error(),
}
default:
return ccerror.RequestError{Err: e}
}
default:
return err
}
}