forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
102 lines (87 loc) · 2.9 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
90
91
92
93
94
95
96
97
98
99
100
101
102
package net
import (
_ "crypto/sha512"
"crypto/x509"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/cloudfoundry/cli/cf/trace"
"golang.org/x/net/websocket"
)
type HttpClientInterface interface {
Do(req *http.Request) (resp *http.Response, err error)
}
var NewHttpClient = func(tr *http.Transport) HttpClientInterface {
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]
copyHeaders(prevReq, req, getBaseDomain(req.URL.String()) == getBaseDomain(via[0].URL.String()))
dumpRequest(req)
return nil
}
func copyHeaders(from *http.Request, to *http.Request, sameDomain bool) {
for key, values := range from.Header {
// do not copy POST-specific headers
if key != "Content-Type" && key != "Content-Length" && !(!sameDomain && key == "Authorization") {
to.Header.Set(key, strings.Join(values, ","))
}
}
}
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 fmt.Errorf("%s: %s", T("Error performing request"), err.Error())
}
func getBaseDomain(host string) string {
hostUrl, _ := url.Parse(host)
hostStrs := strings.Split(hostUrl.Host, ".")
return hostStrs[len(hostStrs)-2] + "." + hostStrs[len(hostStrs)-1]
}