forked from heroku/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
171 lines (154 loc) · 3.86 KB
/
http.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"container/list"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/ansel1/merry"
"github.com/dghubble/sling"
"github.com/mitchellh/ioprogress"
"github.com/ulikunitz/xz"
)
var apiHTTPClient *http.Client
func init() {
getClient := func() *http.Client {
return &http.Client{
Timeout: 30 * time.Minute,
Transport: &http.Transport{
TLSClientConfig: httpTLSClientConfig(),
Proxy: http.ProxyFromEnvironment,
Dial: (&net.Dialer{
Timeout: 60 * time.Second,
KeepAlive: 60 * time.Second,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 3 * time.Second,
},
}
}
http.DefaultClient = getClient()
apiHTTPClient = getClient()
apiHTTPClient.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = !shouldVerifyHost(apiURL())
}
func useSystemCerts() bool {
e := os.Getenv("HEROKU_USE_SYSTEM_CERTS")
return e != "false" && e != "0"
}
// APIRequest is for requests to api.heroku.com
type APIRequest struct {
*sling.Sling
}
// Auth the API request with a token
func (api *APIRequest) Auth(token string) *APIRequest {
api.Set("Authorization", "Bearer "+token)
return api
}
func apiRequest() *APIRequest {
req := sling.New().Client(apiHTTPClient).Base(apiURL())
req.Set("User-Agent", version())
req.Set("Accept", "application/vnd.heroku+json; version=3")
if os.Getenv("HEROKU_HEADERS") != "" {
var h map[string]string
json.Unmarshal([]byte(os.Getenv("HEROKU_HEADERS")), &h)
for k, v := range h {
req.Set(k, v)
}
}
return &APIRequest{req}
}
func shouldVerifyHost(host string) bool {
return !(os.Getenv("HEROKU_SSL_VERIFY") == "disable" || strings.HasSuffix(host, "herokudev.com"))
}
func httpTLSClientConfig() (config *tls.Config) {
config = &tls.Config{}
paths := list.New()
if !useSystemCerts() {
path := filepath.Join(AppDir, "lib", "cacert.pem")
paths.PushBack(path)
}
sslCertFile := os.Getenv("SSL_CERT_FILE")
if sslCertFile != "" {
paths.PushBack(sslCertFile)
}
sslCertDir := os.Getenv("SSL_CERT_DIR")
if sslCertDir != "" {
files, err := ioutil.ReadDir(sslCertDir)
if err != nil {
Warn("Error opening " + sslCertDir)
return
}
for _, file := range files {
path := filepath.Join(sslCertDir, file.Name())
paths.PushBack(path)
}
}
if paths.Len() == 0 {
return
}
certs := x509.NewCertPool()
Debugln("Adding the following trusted certificate authorities")
for e := paths.Front(); e != nil; e = e.Next() {
path := e.Value.(string)
Debugln(" " + path)
data, err := ioutil.ReadFile(path)
if err != nil {
WarnIfError(err)
return
}
ok := certs.AppendCertsFromPEM(data)
if !ok {
Warn("Error parsing " + path)
return
}
}
config.RootCAs = certs
return
}
func progressDrawFn(msg string) func(int64, int64) string {
return func(progress, total int64) string {
return fmt.Sprintf(msg+" %15s", ioprogress.DrawTextFormatBytes(progress, total))
}
}
func downloadXZ(url, msg string) (io.Reader, func() string, error) {
req, err := sling.New().Get(url).Request()
if err != nil {
return nil, nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, err
}
var download io.Reader
if msg != "" {
size, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
download = &ioprogress.Reader{
Reader: resp.Body,
Size: int64(size),
DrawFunc: ioprogress.DrawTerminalf(Stderr, progressDrawFn(msg)),
}
} else {
download = resp.Body
}
getSha, reader := computeSha(download)
uncompressed, err := xz.NewReader(reader)
return uncompressed, func() string {
defer resp.Body.Close()
return getSha()
}, err
}
func getHTTPError(rsp *http.Response) error {
if rsp.StatusCode >= 200 && rsp.StatusCode < 300 {
return nil
}
return merry.Errorf("HTTP Error: %s %s", rsp.Request.URL, rsp.Status)
}