-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
107 lines (88 loc) · 2.8 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
103
104
105
106
107
package net
import (
_ "crypto/sha512" // #82254112: http://bridge.grumpy-troll.org/2014/05/golang-tls-comodo/
"crypto/x509"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"golang.org/x/net/websocket"
)
//go:generate counterfeiter . HTTPClientInterface
type HTTPClientInterface interface {
RequestDumperInterface
Do(*http.Request) (*http.Response, error)
ExecuteCheckRedirect(req *http.Request, via []*http.Request) error
}
type client struct {
*http.Client
dumper RequestDumper
}
var NewHTTPClient = func(tr *http.Transport, dumper RequestDumper) HTTPClientInterface {
c := client{
&http.Client{
Transport: tr,
},
dumper,
}
c.CheckRedirect = c.checkRedirect
return &c
}
func (cl *client) ExecuteCheckRedirect(req *http.Request, via []*http.Request) error {
return cl.CheckRedirect(req, via)
}
func (cl *client) checkRedirect(req *http.Request, via []*http.Request) error {
if len(via) > 1 {
return errors.New(T("stopped after 1 redirect"))
}
prevReq := via[len(via)-1]
cl.copyHeaders(prevReq, req, getBaseDomain(req.URL.String()) == getBaseDomain(via[0].URL.String()))
cl.dumper.DumpRequest(req)
return nil
}
func (cl *client) 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 (cl *client) DumpRequest(req *http.Request) {
cl.dumper.DumpRequest(req)
}
func (cl *client) DumpResponse(res *http.Response) {
cl.dumper.DumpResponse(res)
}
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 typedInnerErr := 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, "")
case *net.OpError:
if typedInnerErr.Op == "dial" {
return fmt.Errorf("%s: %s\n%s", T("Error performing request"), err.Error(), T("TIP: If you are behind a firewall and require an HTTP proxy, verify the https_proxy environment variable is correctly set. Else, check your network connection."))
}
}
}
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]
}