-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.go
435 lines (371 loc) · 11.2 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package utils
import (
"bytes"
"context"
"crypto/rand"
"crypto/tls"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
"github.com/Laisky/errors/v2"
"github.com/Laisky/go-chaining"
"github.com/Laisky/zap"
"github.com/Laisky/go-utils/v4/json"
"github.com/Laisky/go-utils/v4/log"
)
const (
defaultHTTPClientOptTimeout = 30 * time.Second
defaultHTTPClientOptMaxConn = 20
// HTTPHeaderHost HTTP header name
HTTPHeaderHost = "Host"
// HTTPHeaderReferer HTTP header name
HTTPHeaderReferer = "Referer"
// HTTPHeaderContentType HTTP header name
HTTPHeaderContentType = "Content-Type"
// HTTPHeaderContentTypeValJSON HTTP header value
HTTPHeaderContentTypeValJSON = "application/json"
// TracingKey default trace key
//
// https://www.jaegertracing.io/docs/1.22/client-libraries/#key
//
// `{trace-id}:{span-id}:{parent-span-id}:{flags}`
TracingKey = "Uber-Trace-Id"
)
var (
internalHttpCli *http.Client
)
func init() {
var err error
// new http client
opts := []HTTPClientOptFunc{
WithHTTPClientTimeout(30 * time.Second),
}
if len(GetEnvInsensitive("HTTP_PROXY")) != 0 {
opts = append(opts, WithHTTPClientProxy(GetEnvInsensitive("HTTP_PROXY")[0]))
}
if internalHttpCli, err = NewHTTPClient(opts...); err != nil {
log.Shared.Panic("new http client got error", zap.Error(err))
}
}
type httpClientOption struct {
timeout time.Duration
maxConn int
insecure bool
tlsConfig *tls.Config
proxy func(*http.Request) (*url.URL, error)
}
// HTTPClientOptFunc http client options
type HTTPClientOptFunc func(*httpClientOption) error
// NewJaegerTracingID generate jaeger tracing id
//
// Args:
// - traceID: trace id, 64bit number, will encode to hex string
// - spanID: span id, 64bit number, will encode to hex string
// - parentSpanID: parent span id, 64bit number, will encode to hex string
// - flag: 8bit number, one byte bitmap, as one or two hex digits (leading zero may be omitted)
//
// Even if some of the parameters have incorrect formatting,
// it won't result in an error; instead, it will generate a new random value.
func NewJaegerTracingID(traceID, spanID, parentSpanID uint64, flag byte) (traceVal JaegerTracingID, err error) {
if traceID == 0 {
if traceID, err = RandomNonZeroUint64(); err != nil {
return "", errors.Wrapf(err, "generate random trace id")
}
}
if spanID == 0 {
if spanID, err = RandomNonZeroUint64(); err != nil {
return "", errors.Wrapf(err, "generate random span id")
}
}
if flag == 0 {
flag = 0x04 // default to not used
}
traceIDVal := strings.TrimLeft(fmt.Sprintf("%016x", traceID), "0")
spanIDVal := strings.TrimLeft(fmt.Sprintf("%016x", spanID), "0")
parentSpanIDVal := strings.TrimLeft(fmt.Sprintf("%016x", parentSpanID), "0")
flagVal := strings.TrimLeft(fmt.Sprintf("%02x", flag), "0")
return JaegerTracingID(fmt.Sprintf("%s:%s:%s:%s", traceIDVal, spanIDVal, parentSpanIDVal, flagVal)), nil
}
// PaddingLeft padding string to left
func PaddingLeft(s string, padStr string, pLen int) string {
if len(s) >= pLen {
return s
}
return strings.Repeat(padStr, pLen-len(s)) + s
}
// JaegerTracingID jaeger tracing id
type JaegerTracingID string
// String implement fmt.Stringer
func (t JaegerTracingID) String() string {
return string(t)
}
// Parse parse jaeger tracing id from string
func (t JaegerTracingID) Parse() (traceID, spanID, parentSpanID uint64, flag byte, err error) {
traceVal := t.String()
vals := strings.Split(traceVal, ":")
if len(vals) != 4 {
return 0, 0, 0, 0, errors.Errorf("invalid trace value `%s`", traceVal)
}
if traceID, err = strconv.ParseUint(PaddingLeft(vals[0], "0", 16), 16, 64); err != nil {
return 0, 0, 0, 0, errors.Wrapf(err, "parse traceID")
}
if spanID, err = strconv.ParseUint(PaddingLeft(vals[1], "0", 16), 16, 64); err != nil {
return 0, 0, 0, 0, errors.Wrapf(err, "parse spanID")
}
if parentSpanID, err = strconv.ParseUint(PaddingLeft(vals[2], "0", 16), 16, 64); err != nil {
return 0, 0, 0, 0, errors.Wrapf(err, "parse parentSpanID")
}
if flagSlice, err := hex.DecodeString(PaddingLeft(vals[3], "0", 2)); err != nil {
return 0, 0, 0, 0, errors.Wrapf(err, "parse flag")
} else if len(flagSlice) != 1 {
return 0, 0, 0, 0, errors.Errorf("invalid flag `%s`", vals[3])
} else {
flag = flagSlice[0]
}
return traceID, spanID, parentSpanID, flag, nil
}
// RandomNonZeroUint64 generate random uint64 number
func RandomNonZeroUint64() (uint64, error) {
var num uint64
for {
if err := binary.Read(rand.Reader, binary.BigEndian, &num); err != nil {
return 0, errors.Wrap(err, "generate random number")
}
if num != 0 {
return num, nil
}
}
}
// NewSpan generate new span
func (t JaegerTracingID) NewSpan() (JaegerTracingID, error) {
traceID, spanID, _, flag, err := t.Parse()
if err != nil {
return "", errors.Wrapf(err, "parse traceID")
}
newSpanID, err := RandomNonZeroUint64()
if err != nil {
return "", errors.Wrapf(err, "generate new spanID")
}
return NewJaegerTracingID(traceID, newSpanID, spanID, flag)
}
// WithHTTPClientTimeout set http client timeout
//
// default to 30s
func WithHTTPClientTimeout(timeout time.Duration) HTTPClientOptFunc {
return func(opt *httpClientOption) error {
if timeout <= 0 {
return errors.Errorf("timeout should greater than 0")
}
opt.timeout = timeout
return nil
}
}
// WithHTTPClientMaxConn set http client max connection
//
// default to 20
func WithHTTPClientMaxConn(maxConn int) HTTPClientOptFunc {
return func(opt *httpClientOption) error {
if maxConn <= 0 {
return errors.Errorf("maxConn should greater than 0")
}
opt.maxConn = maxConn
return nil
}
}
// WithHTTPClientProxy set http client proxy
func WithHTTPClientProxy(proxy string) HTTPClientOptFunc {
return func(opt *httpClientOption) (err error) {
proxy, err := url.Parse(proxy)
if err != nil {
return errors.Wrap(err, "cannot parse proxy")
}
opt.proxy = http.ProxyURL(proxy)
return nil
}
}
// WithHTTPClientInsecure set http client igonre ssl issue
//
// default to false
//
// Deprecated: use WithHTTPTlsConfig instead
func WithHTTPClientInsecure() HTTPClientOptFunc {
return func(opt *httpClientOption) error {
opt.insecure = true
return nil
}
}
// WithHTTPTlsConfig set tls config
func WithHTTPTlsConfig(cfg *tls.Config) HTTPClientOptFunc {
return func(opt *httpClientOption) error {
opt.tlsConfig = cfg
return nil
}
}
// NewHTTPClient create http client
func NewHTTPClient(opts ...HTTPClientOptFunc) (c *http.Client, err error) {
opt := &httpClientOption{
maxConn: defaultHTTPClientOptMaxConn,
timeout: defaultHTTPClientOptTimeout,
}
for _, optf := range opts {
if err = optf(opt); err != nil {
return nil, errors.Wrap(err, "set option")
}
}
// deprecated in 5.0
if opt.tlsConfig == nil && opt.insecure {
opt.tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
c = &http.Client{
Transport: &http.Transport{
Proxy: opt.proxy,
MaxIdleConnsPerHost: opt.maxConn,
TLSClientConfig: opt.tlsConfig,
},
Timeout: opt.timeout,
}
return c, nil
}
// RequestData http request
type RequestData struct {
Headers map[string]string
Data any
}
// RequestJSON request JSON and return JSON by default client
func RequestJSON(method, url string, request *RequestData, resp any) (err error) {
return RequestJSONWithClient(internalHttpCli, method, url, request, resp)
}
// RequestJSONWithClient request JSON and return JSON with specific client
func RequestJSONWithClient(httpClient *http.Client,
method,
url string,
request *RequestData,
resp any,
) (err error) {
log.Shared.Debug("try to request with json", zap.String("method", method), zap.String("url", url))
var (
jsonBytes []byte
)
jsonBytes, err = json.Marshal(request.Data)
if err != nil {
return errors.Wrap(err, "marshal request data error")
}
log.Shared.Debug("request json", zap.String("body", string(jsonBytes[:])))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx,
strings.ToUpper(method), url, bytes.NewBuffer(jsonBytes))
if err != nil {
return errors.Wrap(err, "new request")
}
req.Header.Set(HTTPHeaderContentType, HTTPHeaderContentTypeValJSON)
for k, v := range request.Headers {
req.Header.Set(k, v)
}
r, err := httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "try to request url error")
}
defer func() { _ = r.Body.Close() }()
if r.StatusCode/100 != 2 {
respBytes, err := io.ReadAll(r.Body)
if err != nil {
return errors.Wrap(err, "try to read response data error")
}
return errors.New(string(respBytes[:]))
}
if err = json.NewDecoder(r.Body).Decode(resp); err != nil {
return errors.Wrapf(err, "unmarshal response")
}
return nil
}
// CheckResp check HTTP response's status code and return the error with body message
func CheckResp(resp *http.Response) error {
c := chaining.Flow(
checkRespStatus,
checkRespErr,
)(resp, nil)
return c.GetError()
}
// HTTPInvalidStatusError return error about status code
func HTTPInvalidStatusError(statusCode int) error {
return errors.Errorf("got http invalid status code `%d`", statusCode)
}
func checkRespStatus(c *chaining.Chain) (r any, err error) {
resp, ok := c.GetVal().(*http.Response)
if !ok {
return nil, errors.Errorf("got invalid response type `%T`", c.GetVal())
}
code := resp.StatusCode
if code/100 != 2 {
return resp, HTTPInvalidStatusError(code)
}
return resp, nil
}
func checkRespErr(c *chaining.Chain) (any, error) {
upErr := c.GetError()
if upErr == nil {
return c.GetVal(), nil
}
resp, ok := c.GetVal().(*http.Response)
if !ok {
return nil, errors.Join(upErr, errors.Errorf("got invalid response type `%T`", c.GetVal()))
}
defer func() { _ = resp.Body.Close() }()
respB, err := io.ReadAll(resp.Body)
if err != nil {
return resp, errors.Wrapf(upErr, "read body got error: %v", err.Error())
}
return resp, errors.Wrapf(upErr, "got http body: %v", string(respB[:]))
}
// OpenURLInDefaultBrowser opens the specified URL in the default browser of the user.
//
// Inspired by https://gist.github.com/sevkin/9798d67b2cb9d07cb05f89f14ba682f8?permalink_comment_id=5019685#gistcomment-5019685
//
//nolint:lll
func OpenURLInDefaultBrowser(ctx context.Context, url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
// Check if running under WSL
if isWSL(ctx) {
// Use 'cmd.exe /c start' to open the URL in the default Windows browser
cmd = "cmd.exe"
args = []string{"/c", "start", url}
} else {
// Use xdg-open on native Linux environments
cmd = "xdg-open"
args = []string{url}
}
}
if len(args) > 1 {
// args[0] is used for 'start' command argument, to prevent issues with URLs starting with a quote
args = append(args[:1], append([]string{""}, args[1:]...)...)
}
//nolint:gosec //G204: Subprocess launched with variable
return exec.CommandContext(ctx, cmd, args...).Start()
}
// isWSL checks if the Go program is running inside Windows Subsystem for Linux
func isWSL(ctx context.Context) bool {
releaseData, err := exec.CommandContext(ctx, "uname", "-r").Output()
if err != nil {
return false
}
return strings.Contains(strings.ToLower(string(releaseData)), "microsoft")
}