/
http_client.go
269 lines (244 loc) · 6.55 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package web
import (
"context"
"fmt"
"github.com/bhatti/api-mock-service/internal/types"
"github.com/bhatti/api-mock-service/internal/utils"
log "github.com/sirupsen/logrus"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
// AWSAccessKey constant
const AWSAccessKey = "AWS_ACCESS_KEY_ID"
// AWSSecretKey constant
const AWSSecretKey = "AWS_SECRET_ACCESS_KEY"
// AWSSecurityToken constant
const AWSSecurityToken = "AWS_SECURITY_TOKEN"
// AWSSessionToken constant
const AWSSessionToken = "AWS_SESSION_TOKEN"
var internalParamKeys = []string{AWSAccessKey, AWSSecretKey, AWSSecurityToken, AWSSessionToken}
// HTTPClient defines methods for http get and post methods
type HTTPClient interface {
Handle(
ctx context.Context,
url string,
method string,
headers map[string][]string,
params map[string]string,
body io.ReadCloser,
) (int, string, io.ReadCloser, map[string][]string, error)
}
// DefaultHTTPClient implements HTTPClient
type DefaultHTTPClient struct {
config *types.Configuration
authAdapter AuthAdapter
}
// NewHTTPClient creates structure for HTTPClient
func NewHTTPClient(config *types.Configuration, authAdapter AuthAdapter) *DefaultHTTPClient {
return &DefaultHTTPClient{
config: config,
authAdapter: authAdapter,
}
}
// Handle makes HTTP request
func (w *DefaultHTTPClient) Handle(
ctx context.Context,
url string,
method string,
headers map[string][]string,
params map[string]string,
body io.ReadCloser,
) (statusCode int, httpVersion string, respBody io.ReadCloser, respHeader map[string][]string, err error) {
started := time.Now()
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"Method": method,
"URL": url,
}).Debug("handle BEGIN")
var bodyB []byte
bodyB, body, err = utils.ReadAll(body)
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return 500, "", nil, make(map[string][]string), err
}
req.ContentLength = int64(len(bodyB))
statusCode, httpVersion, respBody, respHeader, err = w.execute(req, headers, params)
elapsed := time.Since(started).String()
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"URL": url,
"Method": method,
"StatusCode": statusCode,
"Elapsed": elapsed,
"Error": err}).Debug("handle END")
return
}
// ////////////////////////////////// PRIVATE METHODS ///////////////////////////////////////////
func (w *DefaultHTTPClient) execute(
req *http.Request,
headers map[string][]string,
params map[string]string) (int, string, io.ReadCloser, map[string][]string, error) {
if req == nil {
return 500, "", nil, make(map[string][]string), fmt.Errorf("request not specified")
}
internalKeyMap := make(map[string]string)
if w.config.UserAgent != "" {
req.Header.Set("User-Agent", w.config.UserAgent)
}
if len(params) > 0 {
paramVals := url.Values{}
for k, v := range params {
if isInternalParamKeys(k) {
internalKeyMap[strings.ToUpper(k)] = v
} else {
paramVals.Add(k, v)
}
}
req.URL.RawQuery = paramVals.Encode()
}
for name, vals := range headers {
for _, val := range vals {
if isInternalParamKeys(name) {
internalKeyMap[strings.ToUpper(name)] = val
} else {
req.Header.Add(name, val)
}
}
}
awsAuthSig4, awsInfo, awsErr := w.authAdapter.HandleAuth(req)
headers = req.Header
client := httpClient(w.config)
resp, err := client.Do(req)
if err != nil {
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"URL": req.URL,
"Method": req.Method,
"Headers": req.Header,
"Params": params,
"AWSAuthSig4": awsAuthSig4,
"AWSInfo": awsInfo,
"AWSError": awsErr,
"Error": err,
}).Warnf("failed to invoke http client")
} else {
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"URL": req.URL,
"Method": req.Method,
"StatusCode": resp.StatusCode,
"Status": resp.Status,
"Headers": req.Header,
"Params": params,
"AWSAuthSig4": awsAuthSig4,
"AWSInfo": awsInfo,
"AWSError": awsErr,
"Error": err,
}).Infof("invoked http client")
}
if err != nil {
return 500, "", nil, make(map[string][]string), err
}
return resp.StatusCode, resp.Proto, resp.Body, resp.Header, nil
}
// GetHeaderParamOrEnvValue searches key in map or env variables
func GetHeaderParamOrEnvValue(params map[string]string, names ...string) string {
for _, name := range names {
if len(params[name]) > 0 {
return params[name]
}
if len(os.Getenv(name)) > 0 {
return os.Getenv(name)
}
}
return ""
}
func getLocalIPAddresses() []string {
ips := make([]string, 0)
interfaces, err := net.Interfaces()
if err != nil {
return ips
}
// handle err
for _, i := range interfaces {
addresses, err := i.Addrs()
if err != nil {
return ips
}
for _, addr := range addresses {
switch v := addr.(type) {
case *net.IPNet:
ips = append(ips, v.IP.String())
case *net.IPAddr:
ips = append(ips, v.IP.String())
}
}
}
return ips
}
func getRemoteIPAddressFromURL(targetURL string) string {
hostIP := ""
u, err := url.Parse(targetURL)
if err == nil {
addr, err := net.LookupIP(u.Host)
if err == nil {
hostIP = ""
for i, a := range addr {
if i > 0 {
hostIP = hostIP + " "
}
hostIP = hostIP + a.String()
}
}
}
return hostIP
}
func getProxyEnv() map[string]string {
proxies := make(map[string]string)
proxies["HTTP_PROXY"] = os.Getenv("HTTP_PROXY")
proxies["HTTPS_PROXY"] = os.Getenv("HTTPS_PROXY")
proxies["NO_PROXY"] = os.Getenv("NO_PROXY")
return proxies
}
func httpClient(config *types.Configuration) *http.Client {
if config.ProxyURL == "" {
return &http.Client{}
}
proxyURL, err := url.Parse(config.ProxyURL)
if err != nil {
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"IP": getRemoteIPAddressFromURL(config.ProxyURL),
"Error": err,
"Proxy": config.ProxyURL}).Warn("Failed to parse proxy header")
return &http.Client{}
}
headers := make(http.Header, 0)
headers.Set("User-Agent", config.UserAgent)
//adding the proxy settings to the Transport object
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
ProxyConnectHeader: headers,
}
log.WithFields(log.Fields{
"Component": "DefaultHTTPClient",
"LocalIP": getLocalIPAddresses(),
"EnvProxy": getProxyEnv(),
"Proxy": proxyURL}).Info("Http client using proxy")
return &http.Client{
Transport: transport,
}
}
func isInternalParamKeys(k string) bool {
for _, next := range internalParamKeys {
if strings.EqualFold(next, k) {
return true
}
}
return false
}