/
client.go
384 lines (335 loc) · 9.59 KB
/
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package api
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/google/uuid"
)
// HTTP const definitions
const (
GET string = http.MethodGet
POST string = http.MethodPost
PUT string = http.MethodPut
PATCH string = http.MethodPatch
DELETE string = http.MethodDelete
defaultTimeout = time.Second * 60
responseBufferSize = 2048
)
// Request - the request object used when communicating to an API
type Request struct {
Method string
URL string
QueryParams map[string]string
Headers map[string]string
Body []byte
FormData map[string]string
}
// Response - the response object given back when communicating to an API
type Response struct {
Code int
Body []byte
Headers map[string][]string
}
// Client -
type Client interface {
Send(request Request) (*Response, error)
}
type httpClient struct {
Client
logger log.FieldLogger
httpClient *http.Client
timeout time.Duration
dialer util.Dialer
singleEntryHostMap map[string]string
singleURL string
}
type configAgent struct {
agentName string
environmentName string
isDocker bool
isGRPC bool
singleURL string
singleEntryFilter []string
}
var cfgAgent *configAgent
var cfgAgentMutex *sync.Mutex
func init() {
cfgAgent = &configAgent{}
cfgAgentMutex = &sync.Mutex{}
}
// SetConfigAgent -
func SetConfigAgent(env string, isGRPC, isDocker bool, agentName, singleURL string, singleEntryFilter []string) {
cfgAgentMutex.Lock()
defer cfgAgentMutex.Unlock()
cfgAgent.environmentName = env
cfgAgent.isGRPC = isGRPC
cfgAgent.isDocker = isDocker
cfgAgent.agentName = agentName
cfgAgent.singleURL = singleURL
if cfgAgent.singleEntryFilter != nil {
cfgAgent.singleEntryFilter = append(cfgAgent.singleEntryFilter, singleEntryFilter...)
} else {
cfgAgent.singleEntryFilter = singleEntryFilter
}
}
type ClientOpt func(*httpClient)
func WithTimeout(timeout time.Duration) func(*httpClient) {
return func(h *httpClient) {
h.timeout = timeout
}
}
func WithSingleURL() func(*httpClient) {
return func(h *httpClient) {
h.singleURL = ""
if cfgAgent != nil {
h.singleURL = cfgAgent.singleURL
if h.singleURL != "" {
h.singleEntryHostMap = initializeSingleEntryMapping(h.singleURL, cfgAgent.singleEntryFilter)
}
}
}
}
// NewClient - creates a new HTTP client
func NewClient(tlsCfg config.TLSConfig, proxyURL string, options ...ClientOpt) Client {
timeout := getTimeoutFromEnvironment()
client := newClient(timeout)
for _, o := range options {
o(client)
}
client.initialize(tlsCfg, proxyURL)
return client
}
// NewClientWithTimeout - creates a new HTTP client, with a timeout
func NewClientWithTimeout(tlsCfg config.TLSConfig, proxyURL string, timeout time.Duration) Client {
log.DeprecationWarningReplace("NewClientWithTimeout", "NewClient and WithTimeout optional func")
return NewClient(tlsCfg, proxyURL, WithTimeout(timeout))
}
// NewSingleEntryClient - creates a new HTTP client for single entry point with a timeout
func NewSingleEntryClient(tlsCfg config.TLSConfig, proxyURL string, timeout time.Duration) Client {
log.DeprecationWarningReplace("NewSingleEntryClient", "NewClient and WithSingleURL optional func")
return NewClient(tlsCfg, proxyURL, WithTimeout(timeout), WithSingleURL())
}
func newClient(timeout time.Duration) *httpClient {
return &httpClient{
timeout: timeout,
logger: log.NewFieldLogger().
WithComponent("httpClient").
WithPackage("sdk.api"),
}
}
func initializeSingleEntryMapping(singleEntryURL string, singleEntryFilter []string) map[string]string {
hostMapping := make(map[string]string)
entryURL, err := url.Parse(singleEntryURL)
if err == nil {
for _, filteredURL := range singleEntryFilter {
svcURL, err := url.Parse(filteredURL)
if err == nil {
hostMapping[util.ParseAddr(svcURL)] = util.ParseAddr(entryURL)
}
}
}
return hostMapping
}
func parseProxyURL(proxyURL string) *url.URL {
if proxyURL != "" {
pURL, err := url.Parse(proxyURL)
if err == nil {
return pURL
}
log.Errorf("Error parsing proxyURL from config; creating a non-proxy client: %s", err.Error())
}
return nil
}
func (c *httpClient) initialize(tlsCfg config.TLSConfig, proxyURL string) {
c.httpClient = c.createClient(tlsCfg)
if c.singleURL == "" && proxyURL == "" {
return
}
c.dialer = util.NewDialer(parseProxyURL(proxyURL), c.singleEntryHostMap)
c.httpClient.Transport.(*http.Transport).DialContext = c.httpDialer
}
func (c *httpClient) createClient(tlsCfg config.TLSConfig) *http.Client {
if tlsCfg != nil {
return c.createHTTPSClient(tlsCfg)
}
return c.createHTTPClient()
}
func (c *httpClient) createHTTPClient() *http.Client {
httpClient := &http.Client{
Transport: &http.Transport{},
Timeout: c.timeout,
}
return httpClient
}
func (c *httpClient) createHTTPSClient(tlsCfg config.TLSConfig) *http.Client {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsCfg.BuildTLSConfig(),
},
Timeout: c.timeout,
}
return httpClient
}
func (c *httpClient) httpDialer(ctx context.Context, network, addr string) (net.Conn, error) {
return c.dialer.DialContext(ctx, network, addr)
}
func getTimeoutFromEnvironment() time.Duration {
cfgHTTPClientTimeout := os.Getenv("HTTP_CLIENT_TIMEOUT")
if cfgHTTPClientTimeout == "" {
return defaultTimeout
}
timeout, err := time.ParseDuration(cfgHTTPClientTimeout)
if err != nil {
log.Tracef("Unable to parse the HTTP_CLIENT_TIMEOUT value, using the default http client timeout")
return defaultTimeout
}
return timeout
}
func (c *httpClient) getURLEncodedQueryParams(queryParams map[string]string) string {
params := url.Values{}
for key, value := range queryParams {
params.Add(key, value)
}
return params.Encode()
}
func (c *httpClient) prepareAPIRequest(ctx context.Context, request Request) (*http.Request, error) {
requestURL := request.URL
if len(request.QueryParams) != 0 {
requestURL += "?" + c.getURLEncodedQueryParams(request.QueryParams)
}
var req *http.Request
var err error
if request.FormData != nil {
formData := make(url.Values)
for k, v := range request.FormData {
formData.Add(k, v)
}
req, err = http.NewRequestWithContext(ctx, request.Method, requestURL, strings.NewReader(formData.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} else {
req, err = http.NewRequestWithContext(ctx, request.Method, requestURL, bytes.NewBuffer(request.Body))
}
if err != nil {
return req, err
}
hasUserAgentHeader := false
for key, value := range request.Headers {
req.Header.Set(key, value)
if strings.ToLower(key) == "user-agent" {
hasUserAgentHeader = true
}
}
if !hasUserAgentHeader {
cfgAgentMutex.Lock()
defer cfgAgentMutex.Unlock()
deploymentType := "binary"
if cfgAgent.isDocker {
deploymentType = "docker"
}
ua := fmt.Sprintf("%s/%s SDK/%s %s %s %s", config.AgentTypeName, config.AgentVersion, config.SDKVersion, cfgAgent.environmentName, cfgAgent.agentName, deploymentType)
if cfgAgent.isGRPC {
ua = fmt.Sprintf("%s reactive", ua)
}
req.Header.Set("User-Agent", ua)
}
return req, err
}
func (c *httpClient) prepareAPIResponse(res *http.Response, timer *time.Timer) (*Response, error) {
var err error
var responeBuffer bytes.Buffer
writer := bufio.NewWriter(&responeBuffer)
for {
// Reset the timeout timer for reading the response
timer.Reset(c.timeout)
_, err = io.CopyN(writer, res.Body, responseBufferSize)
if err != nil {
if err == io.EOF {
err = nil
}
break
}
}
if err != nil {
return nil, err
}
response := Response{
Code: res.StatusCode,
Body: responeBuffer.Bytes(),
Headers: res.Header,
}
return &response, err
}
// Send - send the http request and returns the API Response
func (c *httpClient) Send(request Request) (*Response, error) {
startTime := time.Now()
ctx := context.Background()
cancelCtx, cancel := context.WithCancel(ctx)
defer cancel()
req, err := c.prepareAPIRequest(cancelCtx, request)
if err != nil {
log.Errorf("Error preparing api request: %s", err.Error())
return nil, err
}
reqID := uuid.New().String()
// Logging for the HTTP request
statusCode := 0
receivedData := int64(0)
defer func() {
duration := time.Since(startTime)
targetURL := req.URL.String()
if c.dialer != nil {
svcHost := util.ParseAddr(req.URL)
if entryHost, ok := c.singleEntryHostMap[svcHost]; ok {
targetURL = req.URL.Scheme + "://" + entryHost + req.URL.Path
}
}
logger := c.logger.
WithField("id", reqID).
WithField("method", req.Method).
WithField("status", statusCode).
WithField("duration(ms)", duration.Milliseconds()).
WithField("url", targetURL)
if req.ContentLength > 0 {
logger = logger.WithField("sent(bytes)", req.ContentLength)
}
if receivedData > 0 {
logger = logger.WithField("received(bytes)", receivedData)
}
if err != nil {
logger.WithError(err).
Trace("request failed")
} else {
logger.Trace("request succeeded")
}
}()
// Start the timer to manage the timeout
timer := time.AfterFunc(c.timeout, func() {
cancel()
})
// Prevent reuse of the tcp connection to the same host
req.Close = true
if log.IsHTTPLogTraceEnabled() {
req = log.NewRequestWithTraceContext(reqID, req)
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
statusCode = res.StatusCode
receivedData = res.ContentLength
parseResponse, err := c.prepareAPIResponse(res, timer)
return parseResponse, err
}