-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathutil.go
395 lines (345 loc) · 9.11 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
package httpreq
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/netutil/httpctype"
"github.com/gookit/goutil/strutil"
)
// BasicAuthConf struct
type BasicAuthConf struct {
Username string `json:"username"`
Password string `json:"password"`
}
// IsValid value
func (ba *BasicAuthConf) IsValid() bool {
return ba.Password != "" && ba.Username != ""
}
// Value build to auth header "Authorization".
func (ba *BasicAuthConf) Value() string {
return BuildBasicAuth(ba.Username, ba.Password)
}
// String build to auth header "Authorization".
func (ba *BasicAuthConf) String() string {
return ba.Username + ":" + ba.Password
}
// IsOK check response status code is 200
func IsOK(statusCode int) bool {
return statusCode == http.StatusOK
}
// IsSuccessful check response status code is in 200 - 300
func IsSuccessful(statusCode int) bool {
return statusCode >= http.StatusOK && statusCode < 300
}
// IsRedirect check response status code is in [301, 302, 303, 307]
func IsRedirect(statusCode int) bool {
return statusCode == http.StatusMovedPermanently ||
statusCode == http.StatusFound ||
statusCode == http.StatusSeeOther ||
statusCode == http.StatusTemporaryRedirect
}
// IsForbidden is this response forbidden(403)
func IsForbidden(statusCode int) bool {
return statusCode == http.StatusForbidden
}
// IsNotFound is this response not found(404)
func IsNotFound(statusCode int) bool {
return statusCode == http.StatusNotFound
}
// IsClientError check response is client error (400 - 500)
func IsClientError(statusCode int) bool {
return statusCode >= http.StatusBadRequest && statusCode < http.StatusInternalServerError
}
// IsServerError check response is server error (500 - 600)
func IsServerError(statusCode int) bool {
return statusCode >= http.StatusInternalServerError && statusCode <= 600
}
// IsNoBodyMethod check
func IsNoBodyMethod(method string) bool {
return method != "POST" && method != "PUT" && method != "PATCH"
}
// BuildBasicAuth returns the base64 encoded username:password for basic auth.
// Then set to header "Authorization".
//
// copied from net/http.
func BuildBasicAuth(username, password string) string {
auth := username + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
// AddHeaders adds the key, value pairs from the given http.Header to the
// request. Values for existing keys are appended to the keys values.
func AddHeaders(req *http.Request, header http.Header) {
for key, values := range header {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
// SetHeaders sets the key, value pairs from the given http.Header to the
// request. Values for existing keys are overwritten.
func SetHeaders(req *http.Request, headers ...http.Header) {
for _, header := range headers {
for key, values := range header {
req.Header[key] = values
}
}
}
// AddHeaderMap to reqeust instance.
func AddHeaderMap(req *http.Request, headerMap map[string]string) {
for k, v := range headerMap {
req.Header.Add(k, v)
}
}
// SetHeaderMap to reqeust instance.
func SetHeaderMap(req *http.Request, headerMap map[string]string) {
for k, v := range headerMap {
req.Header.Set(k, v)
}
}
// HeaderToStringMap convert
func HeaderToStringMap(rh http.Header) map[string]string {
if len(rh) == 0 {
return nil
}
mp := make(map[string]string, len(rh))
for name, values := range rh {
mp[name] = strings.Join(values, "; ")
}
return mp
}
// MakeQuery make query string, convert data to url.Values
func MakeQuery(data any) url.Values {
return ToQueryValues(data)
}
// ToQueryValues convert string-map or any-map to url.Values
//
// data support:
// - url.Values
// - []byte
// - string
// - map[string][]string
// - map[string]string
// - map[string]any
func ToQueryValues(data any) url.Values {
uv := make(url.Values)
switch typData := data.(type) {
// use url.Values directly if we have it
case url.Values:
return typData
case map[string][]string:
return typData
case []byte:
m, err := url.ParseQuery(string(typData))
if err != nil {
return uv
}
return m
case string:
m, err := url.ParseQuery(typData)
if err != nil {
return uv
}
return m
case map[string]string:
for k, v := range typData {
uv.Add(k, v)
}
case map[string]any:
for k, v := range typData {
uv.Add(k, strutil.QuietString(v))
}
}
return uv
}
// MergeURLValues merge url.Values by overwrite.
//
// values support: url.Values, map[string]string, map[string][]string
func MergeURLValues(uv url.Values, values ...any) url.Values {
if uv == nil {
uv = make(url.Values)
}
for _, v := range values {
switch tv := v.(type) {
case url.Values:
for k, vs := range tv {
uv[k] = vs
}
case map[string]any:
for k, v := range tv {
uv[k] = arrutil.AnyToStrings(v)
}
case map[string]string:
for k, v := range tv {
uv[k] = []string{v}
}
case map[string][]string:
for k, vs := range tv {
uv[k] = vs
}
}
}
return uv
}
// AppendQueryToURL appends the given query string to the given url.
func AppendQueryToURL(reqURL *url.URL, uv url.Values) error {
urlValues, err := url.ParseQuery(reqURL.RawQuery)
if err != nil {
return err
}
for key, values := range uv {
for _, value := range values {
urlValues.Add(key, value)
}
}
// url.Values format to a sorted "url encoded" string.
// e.g. "key=val&foo=bar"
reqURL.RawQuery = urlValues.Encode()
return nil
}
// AppendQueryToURLString appends the given query data to the given url.
func AppendQueryToURLString(urlStr string, query url.Values) string {
if len(query) == 0 {
return urlStr
}
if strings.ContainsRune(urlStr, '?') {
return urlStr + "&" + query.Encode()
}
return urlStr + "?" + query.Encode()
}
// MakeBody make request body, convert data to io.Reader
func MakeBody(data any, cType string) io.Reader {
return ToRequestBody(data, cType)
}
// ToRequestBody make request body, convert data to io.Reader
//
// Allow type for data:
// - string
// - []byte
// - map[string]string
// - map[string][]string/url.Values
// - io.Reader(eg: bytes.Buffer, strings.Reader)
func ToRequestBody(data any, cType string) io.Reader {
if data == nil {
return nil // nobody
}
var reader io.Reader
kind := httpctype.ToKind(cType, "")
switch typVal := data.(type) {
case io.Reader:
reader = typVal
case []byte:
reader = bytes.NewBuffer(typVal)
case string:
reader = bytes.NewBufferString(typVal)
case url.Values:
reader = bytes.NewBufferString(typVal.Encode())
case map[string]string:
if kind == httpctype.KindJSON {
reader = toJSONReader(data)
} else {
reader = bytes.NewBufferString(ToQueryValues(typVal).Encode())
}
case map[string][]string:
if kind == httpctype.KindJSON {
reader = toJSONReader(data)
} else {
reader = bytes.NewBufferString(url.Values(typVal).Encode())
}
default:
// encode body data to json
if kind == httpctype.KindJSON {
reader = toJSONReader(data)
} else {
panic("httpreq: invalid data type for request body, content-type: " + cType)
}
}
return reader
}
func toJSONReader(data any) io.Reader {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
// close escape &, <, > TO \u0026, \u003c, \u003e
enc.SetEscapeHTML(false)
if err := enc.Encode(data); err != nil {
panic("encode data as json fail. error=" + err.Error())
}
return buf
}
// HeaderToString convert http Header to string
func HeaderToString(h http.Header) string {
var sb strings.Builder
for key, values := range h {
sb.WriteString(key)
sb.WriteString(": ")
sb.WriteString(strings.Join(values, ";"))
sb.WriteByte('\n')
}
return sb.String()
}
// RequestToString convert http Request to string
func RequestToString(r *http.Request) string {
buf := &bytes.Buffer{}
buf.WriteString(r.Method)
buf.WriteByte(' ')
buf.WriteString(r.URL.String())
buf.WriteByte(' ')
buf.WriteString(r.Proto)
buf.WriteByte('\n')
for key, values := range r.Header {
buf.WriteString(key)
buf.WriteString(": ")
buf.WriteString(strings.Join(values, ";"))
buf.WriteByte('\n')
}
if r.Body != nil {
buf.WriteByte('\n')
_, _ = buf.ReadFrom(r.Body)
}
return buf.String()
}
// ResponseToString convert http Response to string
func ResponseToString(w *http.Response) string {
if w == nil {
return ""
}
buf := &bytes.Buffer{}
buf.WriteString(w.Proto)
buf.WriteByte(' ')
buf.WriteString(w.Status)
buf.WriteByte('\n')
if len(w.Header) > 0 {
for key, values := range w.Header {
buf.WriteString(key)
buf.WriteString(": ")
buf.WriteString(strings.Join(values, ";"))
buf.WriteByte('\n')
}
}
if w.Body != nil {
buf.WriteByte('\n')
_, _ = buf.ReadFrom(w.Body)
_ = w.Body.Close()
}
return buf.String()
}
// ParseAccept header to strings. referred from gin framework
//
// eg: acceptHeader = "application/json, text/plain, */*"
func ParseAccept(acceptHeader string) []string {
if acceptHeader == "" {
return []string{}
}
parts := strings.Split(acceptHeader, ",")
outs := make([]string, 0, len(parts))
for _, part := range parts {
if part = strings.TrimSpace(strings.Split(part, ";")[0]); part != "" {
outs = append(outs, part)
}
}
return outs
}