forked from apache/pulsar-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
352 lines (304 loc) · 8.39 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
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package internal
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
"github.com/JEFFTheDev/pulsar-client-go/pulsar/internal/auth"
"github.com/JEFFTheDev/pulsar-client-go/pulsar/log"
"github.com/pkg/errors"
)
// httpClient is a base client that is used to make http httpRequest to the ServiceURL
type httpClient struct {
ServiceNameResolver ServiceNameResolver
HTTPClient *http.Client
requestTimeout time.Duration
log log.Logger
metrics *Metrics
}
func (c *httpClient) Close() {
if c.HTTPClient != nil {
CloseIdleConnections(c.HTTPClient)
}
}
type HTTPClient interface {
Get(endpoint string, obj interface{}, params map[string]string) error
Closable
}
func NewHTTPClient(serviceURL *url.URL, serviceNameResolver ServiceNameResolver, tlsConfig *TLSOptions,
requestTimeout time.Duration, logger log.Logger, metrics *Metrics,
authProvider auth.Provider) (HTTPClient, error) {
h := &httpClient{
ServiceNameResolver: serviceNameResolver,
requestTimeout: requestTimeout,
log: logger.SubLogger(log.Fields{"serviceURL": serviceURL}),
metrics: metrics,
}
c := &http.Client{Timeout: requestTimeout}
transport, err := getDefaultTransport(tlsConfig)
if err != nil {
return nil, err
}
c.Transport = transport
if authProvider.Name() != "" {
err = authProvider.WithTransport(c.Transport)
if err != nil {
return nil, err
}
c.Transport = authProvider
}
h.HTTPClient = c
return h, nil
}
func (c *httpClient) newRequest(method, path string) (*httpRequest, error) {
base, err := c.ServiceNameResolver.ResolveHost()
if err != nil {
return nil, err
}
u, err := url.Parse(path)
if err != nil {
return nil, err
}
req := &httpRequest{
method: method,
url: &url.URL{
Scheme: base.Scheme,
User: base.User,
Host: base.Host,
Path: endpoint(base.Path, u.Path),
},
params: make(url.Values),
}
return req, nil
}
func (c *httpClient) doRequest(r *httpRequest) (*http.Response, error) {
req, err := r.toHTTP()
if err != nil {
return nil, err
}
if r.contentType != "" {
req.Header.Set("Content-Type", r.contentType)
} else if req.Body != nil {
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", c.useragent())
hc := c.HTTPClient
if hc == nil {
hc = http.DefaultClient
}
return hc.Do(req)
}
// MakeRequest can make a simple httpRequest and handle the response by yourself
func (c *httpClient) MakeRequest(method, endpoint string) (*http.Response, error) {
req, err := c.newRequest(method, endpoint)
if err != nil {
return nil, err
}
resp, err := checkSuccessful(c.doRequest(req))
if err != nil {
return nil, err
}
return resp, nil
}
func (c *httpClient) Get(endpoint string, obj interface{}, params map[string]string) error {
_, err := c.GetWithQueryParams(endpoint, obj, params, true)
if _, ok := err.(*url.Error); ok {
// We can retry this kind of requests over a connection error because they're
// not specific to a particular broker.
backoff := Backoff{100 * time.Millisecond}
startTime := time.Now()
var retryTime time.Duration
for time.Since(startTime) < c.requestTimeout {
retryTime = backoff.Next()
c.log.Debugf("Retrying httpRequest in {%v} with timeout in {%v}", retryTime, c.requestTimeout)
time.Sleep(retryTime)
_, err = c.GetWithQueryParams(endpoint, obj, params, true)
if _, ok := err.(*url.Error); ok {
continue
} else {
// We either succeeded or encountered a non connection error
break
}
}
}
return err
}
func (c *httpClient) GetWithQueryParams(endpoint string, obj interface{}, params map[string]string,
decode bool) ([]byte, error) {
return c.GetWithOptions(endpoint, obj, params, decode, nil)
}
func (c *httpClient) GetWithOptions(endpoint string, obj interface{}, params map[string]string,
decode bool, file io.Writer) ([]byte, error) {
req, err := c.newRequest(http.MethodGet, endpoint)
if err != nil {
return nil, err
}
if params != nil {
query := req.url.Query()
for k, v := range params {
query.Add(k, v)
}
req.params = query
}
resp, err := checkSuccessful(c.doRequest(req))
if err != nil {
return nil, err
}
defer safeRespClose(resp)
if obj != nil {
if err := decodeJSONBody(resp, &obj); err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
} else if !decode {
if file != nil {
_, err := io.Copy(file, resp.Body)
if err != nil {
return nil, err
}
} else {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
}
return nil, err
}
func (c *httpClient) useragent() string {
return "Pulsar-httpClient-Go-v2"
}
type httpRequest struct {
method string
contentType string
url *url.URL
params url.Values
obj interface{}
body io.Reader
}
func (r *httpRequest) toHTTP() (*http.Request, error) {
r.url.RawQuery = r.params.Encode()
// add a httpRequest body if there is one
if r.body == nil && r.obj != nil {
body, err := encodeJSONBody(r.obj)
if err != nil {
return nil, err
}
r.body = body
}
req, err := http.NewRequest(r.method, r.url.RequestURI(), r.body)
if err != nil {
return nil, err
}
req.URL.Host = r.url.Host
req.URL.Scheme = r.url.Scheme
req.Host = r.url.Host
return req, nil
}
// respIsOk is used to validate a successful http status code
func respIsOk(resp *http.Response) bool {
return resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusNoContent
}
// checkSuccessful checks for a valid response and parses an error
func checkSuccessful(resp *http.Response, err error) (*http.Response, error) {
if err != nil {
safeRespClose(resp)
return nil, err
}
if !respIsOk(resp) {
defer safeRespClose(resp)
return nil, responseError(resp)
}
return resp, nil
}
func endpoint(parts ...string) string {
return path.Join(parts...)
}
// encodeJSONBody is used to JSON encode a body
func encodeJSONBody(obj interface{}) (io.Reader, error) {
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
if err := enc.Encode(obj); err != nil {
return nil, err
}
return buf, nil
}
// decodeJSONBody is used to JSON decode a body
func decodeJSONBody(resp *http.Response, out interface{}) error {
if resp.ContentLength == 0 {
return nil
}
dec := json.NewDecoder(resp.Body)
return dec.Decode(out)
}
// safeRespClose is used to close a response body
func safeRespClose(resp *http.Response) {
if resp != nil {
// ignore error since it is closing a response body
_ = resp.Body.Close()
}
}
// responseError is used to parse a response into a client error
func responseError(resp *http.Response) error {
var e error
body, err := ioutil.ReadAll(resp.Body)
reason := ""
code := resp.StatusCode
if err != nil {
reason = err.Error()
return errors.Errorf("Code: %d, Reason: %s", code, reason)
}
err = json.Unmarshal(body, &e)
if err != nil {
reason = string(body)
}
if reason == "" {
reason = "Unknown error"
}
return errors.Errorf("Code: %d, Reason: %s", code, reason)
}
func getDefaultTransport(tlsConfig *TLSOptions) (http.RoundTripper, error) {
transport := http.DefaultTransport.(*http.Transport)
if tlsConfig != nil {
cfg := &tls.Config{
InsecureSkipVerify: tlsConfig.AllowInsecureConnection,
}
if len(tlsConfig.TrustCertsFilePath) > 0 {
rootCA, err := ioutil.ReadFile(tlsConfig.TrustCertsFilePath)
if err != nil {
return nil, err
}
cfg.RootCAs = x509.NewCertPool()
cfg.RootCAs.AppendCertsFromPEM(rootCA)
}
transport.TLSClientConfig = cfg
}
transport.MaxIdleConnsPerHost = 10
return transport, nil
}