forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
89 lines (76 loc) · 2.49 KB
/
http_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
package net
import (
"crypto/tls"
"crypto/x509"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"code.google.com/p/go.net/websocket"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
)
func newHttpClient(trustedCerts []tls.Certificate, disableSSL bool) *http.Client {
tr := &http.Transport{
Dial: (&net.Dialer{Timeout: 10 * time.Second}).Dial,
TLSClientConfig: NewTLSConfig(trustedCerts, disableSSL),
Proxy: http.ProxyFromEnvironment,
}
return &http.Client{
Transport: tr,
CheckRedirect: PrepareRedirect,
}
}
func PrepareRedirect(req *http.Request, via []*http.Request) error {
if len(via) > 1 {
return errors.New(T("stopped after 1 redirect"))
}
prevReq := via[len(via)-1]
req.Header.Set("Authorization", prevReq.Header.Get("Authorization"))
dumpRequest(req)
return nil
}
func dumpRequest(req *http.Request) {
shouldDisplayBody := !strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data")
dumpedRequest, err := httputil.DumpRequest(req, shouldDisplayBody)
if err != nil {
trace.Logger.Printf(T("Error dumping request\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
trace.Logger.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("REQUEST:")), time.Now().Format(time.RFC3339), trace.Sanitize(string(dumpedRequest)))
if !shouldDisplayBody {
trace.Logger.Println(T("[MULTIPART/FORM-DATA CONTENT HIDDEN]"))
}
}
}
func dumpResponse(res *http.Response) {
dumpedResponse, err := httputil.DumpResponse(res, true)
if err != nil {
trace.Logger.Printf(T("Error dumping response\n{{.Err}}\n", map[string]interface{}{"Err": err}))
} else {
trace.Logger.Printf("\n%s [%s]\n%s\n", terminal.HeaderColor(T("RESPONSE:")), time.Now().Format(time.RFC3339), trace.Sanitize(string(dumpedResponse)))
}
}
func WrapNetworkErrors(host string, err error) error {
var innerErr error
switch typedErr := err.(type) {
case *url.Error:
innerErr = typedErr.Err
case *websocket.DialError:
innerErr = typedErr.Err
}
if innerErr != nil {
switch innerErr.(type) {
case x509.UnknownAuthorityError:
return errors.NewInvalidSSLCert(host, T("unknown authority"))
case x509.HostnameError:
return errors.NewInvalidSSLCert(host, T("not valid for the requested host"))
case x509.CertificateInvalidError:
return errors.NewInvalidSSLCert(host, "")
}
}
return errors.NewWithError(T("Error performing request"), err)
}