-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathutil.go
423 lines (357 loc) · 9.52 KB
/
util.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
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved.
// resty source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
package resty
import (
"bytes"
"crypto/md5"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"reflect"
"runtime"
"sort"
"strings"
"sync/atomic"
"time"
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Logger interface
//_______________________________________________________________________
// Logger interface is to abstract the logging from Resty. Gives control to
// the Resty users, choice of the logger.
type Logger interface {
Errorf(format string, v ...any)
Warnf(format string, v ...any)
Debugf(format string, v ...any)
}
func createLogger() *logger {
l := &logger{l: log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds)}
return l
}
var _ Logger = (*logger)(nil)
type logger struct {
l *log.Logger
}
func (l *logger) Errorf(format string, v ...any) {
l.output("ERROR RESTY "+format, v...)
}
func (l *logger) Warnf(format string, v ...any) {
l.output("WARN RESTY "+format, v...)
}
func (l *logger) Debugf(format string, v ...any) {
l.output("DEBUG RESTY "+format, v...)
}
func (l *logger) output(format string, v ...any) {
if len(v) == 0 {
l.l.Print(format)
return
}
l.l.Printf(format, v...)
}
// credentials type is to hold an username and password information
type credentials struct {
Username, Password string
}
// Clone method returns clone of c.
func (c *credentials) Clone() *credentials {
cc := new(credentials)
*cc = *c
return cc
}
// String method returns masked value of username and password
func (c credentials) String() string {
return "Username: **********, Password: **********"
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Package Helper methods
//_______________________________________________________________________
// isStringEmpty method tells whether given string is empty or not
func isStringEmpty(str string) bool {
return len(strings.TrimSpace(str)) == 0
}
// detectContentType method is used to figure out `Request.Body` content type for request header
func detectContentType(body any) string {
contentType := plainTextType
kind := inferKind(body)
switch kind {
case reflect.Struct, reflect.Map:
contentType = jsonContentType
case reflect.String:
contentType = plainTextType
default:
if b, ok := body.([]byte); ok {
contentType = http.DetectContentType(b)
} else if kind == reflect.Slice { // check slice here to differentiate between any slice vs byte slice
contentType = jsonContentType
}
}
return contentType
}
func isJSONContentType(ct string) bool {
return strings.Contains(ct, jsonKey)
}
func isXMLContentType(ct string) bool {
return strings.Contains(ct, xmlKey)
}
func inferContentTypeMapKey(v string) string {
if isJSONContentType(v) {
return jsonKey
} else if isXMLContentType(v) {
return xmlKey
}
return ""
}
func firstNonEmpty(v ...string) string {
for _, s := range v {
if !isStringEmpty(s) {
return s
}
}
return ""
}
var (
mkdirAll = os.MkdirAll
createFile = os.Create
ioCopy = io.Copy
)
func createDirectory(dir string) (err error) {
if _, err = os.Stat(dir); err != nil {
if os.IsNotExist(err) {
if err = mkdirAll(dir, 0755); err != nil {
return
}
}
}
return
}
func getPointer(v any) any {
if v == nil {
return nil
}
vv := reflect.ValueOf(v)
if vv.Kind() == reflect.Ptr {
return v
}
return reflect.New(vv.Type()).Interface()
}
func inferType(v any) reflect.Type {
return reflect.Indirect(reflect.ValueOf(v)).Type()
}
func inferKind(v any) reflect.Kind {
return inferType(v).Kind()
}
func newInterface(v any) any {
if v == nil {
return nil
}
return reflect.New(inferType(v)).Interface()
}
func functionName(i any) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func acquireBuffer() *bytes.Buffer {
buf := bufPool.Get().(*bytes.Buffer)
if buf.Len() == 0 {
buf.Reset()
return buf
}
bufPool.Put(buf)
return new(bytes.Buffer)
}
func releaseBuffer(buf *bytes.Buffer) {
if buf != nil {
buf.Reset()
bufPool.Put(buf)
}
}
func backToBufPool(buf *bytes.Buffer) {
if buf != nil {
bufPool.Put(buf)
}
}
func closeq(v any) {
if c, ok := v.(io.Closer); ok {
silently(c.Close())
}
}
func silently(_ ...any) {}
var sanitizeHeaderToken = []string{
"authorization",
"auth",
"token",
}
func isSanitizeHeader(k string) bool {
kk := strings.ToLower(k)
for _, v := range sanitizeHeaderToken {
if strings.Contains(kk, v) {
return true
}
}
return false
}
func sanitizeHeaders(hdr http.Header) http.Header {
for k := range hdr {
if isSanitizeHeader(k) {
hdr[k] = []string{"********************"}
}
}
return hdr
}
func composeHeaders(hdr http.Header) string {
str := make([]string, 0, len(hdr))
for _, k := range sortHeaderKeys(hdr) {
str = append(str, "\t"+strings.TrimSpace(fmt.Sprintf("%25s: %s", k, strings.Join(hdr[k], ", "))))
}
return strings.Join(str, "\n")
}
func sortHeaderKeys(hdr http.Header) []string {
keys := make([]string, 0, len(hdr))
for key := range hdr {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
func wrapErrors(n error, inner error) error {
if n == nil && inner == nil {
return nil
}
if inner == nil {
return n
}
if n == nil {
return inner
}
return &restyError{
err: n,
inner: inner,
}
}
type restyError struct {
err error
inner error
}
func (e *restyError) Error() string {
return e.err.Error()
}
func (e *restyError) Unwrap() error {
return e.inner
}
// cloneURLValues is a helper function to deep copy url.Values.
func cloneURLValues(v url.Values) url.Values {
if v == nil {
return nil
}
return url.Values(http.Header(v).Clone())
}
func cloneCookie(c *http.Cookie) *http.Cookie {
return &http.Cookie{
Name: c.Name,
Value: c.Value,
Path: c.Path,
Domain: c.Domain,
Expires: c.Expires,
RawExpires: c.RawExpires,
MaxAge: c.MaxAge,
Secure: c.Secure,
HttpOnly: c.HttpOnly,
SameSite: c.SameSite,
Raw: c.Raw,
Unparsed: c.Unparsed,
}
}
type invalidRequestError struct {
Err error
}
func (ire *invalidRequestError) Error() string {
return ire.Err.Error()
}
func drainBody(res *Response) {
if res != nil && res.Body != nil {
defer closeq(res.Body)
_, _ = io.Copy(io.Discard, res.Body)
}
}
func toJSON(v any) string {
buf := acquireBuffer()
defer releaseBuffer(buf)
_ = encodeJSON(buf, v)
return buf.String()
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// GUID generation
// Code inspired from mgo/bson ObjectId
// Code obtained from https://github.com/go-aah/aah/blob/edge/essentials/guid.go
//___________________________________
var (
// guidCounter is atomically incremented when generating a new GUID
// using UniqueID() function. It's used as a counter part of an id.
guidCounter = readRandomUint32()
// machineID stores machine id generated once and used in subsequent calls
// to UniqueId function.
machineID = readMachineID()
// processID is current Process Id
processID = os.Getpid()
)
// newGUID method returns a new Globally Unique Identifier (GUID).
//
// The 12-byte `UniqueId` consists of-
// - 4-byte value representing the seconds since the Unix epoch,
// - 3-byte machine identifier,
// - 2-byte process id, and
// - 3-byte counter, starting with a random value.
//
// Uses Mongo Object ID algorithm to generate globally unique ids -
// https://docs.mongodb.com/manual/reference/method/ObjectId/
func newGUID() string {
var b [12]byte
// Timestamp, 4 bytes, big endian
binary.BigEndian.PutUint32(b[:], uint32(time.Now().Unix()))
// Machine, first 3 bytes of md5(hostname)
b[4], b[5], b[6] = machineID[0], machineID[1], machineID[2]
// Pid, 2 bytes, specs don't specify endianness, but we use big endian.
b[7], b[8] = byte(processID>>8), byte(processID)
// Increment, 3 bytes, big endian
i := atomic.AddUint32(&guidCounter, 1)
b[9], b[10], b[11] = byte(i>>16), byte(i>>8), byte(i)
return hex.EncodeToString(b[:])
}
var ioReadFull = io.ReadFull
// readRandomUint32 returns a random guidCounter.
func readRandomUint32() uint32 {
var b [4]byte
if _, err := ioReadFull(rand.Reader, b[:]); err == nil {
return (uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
}
// To initialize package unexported variable 'guidCounter'.
// This panic would happen at program startup, so no worries at runtime panic.
panic(errors.New("resty - guid: unable to generate random object id"))
}
var osHostname = os.Hostname
// readMachineID generates and returns a machine id.
// If this function fails to get the hostname it will cause a runtime error.
func readMachineID() []byte {
var sum [3]byte
id := sum[:]
if hostname, err := osHostname(); err == nil {
hw := md5.New()
_, _ = hw.Write([]byte(hostname))
copy(id, hw.Sum(nil))
return id
}
if _, err := ioReadFull(rand.Reader, id); err == nil {
return id
}
// To initialize package unexported variable 'machineID'.
// This panic would happen at program startup, so no worries at runtime panic.
panic(errors.New("resty - guid: unable to get hostname and random bytes"))
}