-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
371 lines (324 loc) · 9.08 KB
/
utils.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
package rapi
import (
"compress/flate"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
"time"
"unicode"
)
// validateContentType validates whether the content type is in the given valid media types.
func validateContentType(contentType string, validMediaTypes ...string) (mediaType, charset string, err error) {
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return "", "", fmt.Errorf("media type parse error: %w", err)
}
mediaType = strings.ToLower(mediaType)
ok := false
for _, validMediaType := range validMediaTypes {
validMediaType = strings.ToLower(validMediaType)
if mediaType == validMediaType {
ok = true
break
}
}
if !ok {
return mediaType, "", fmt.Errorf("invalid media type %q", mediaType)
}
charset, ok = params["charset"]
if ok {
charset = strings.ToLower(charset)
switch charset {
case "ascii":
case "utf-8":
default:
return mediaType, charset, fmt.Errorf("invalid charset %q", charset)
}
}
return mediaType, charset, nil
}
// copyReflectValue copies val and always returns pointer value if val is not pointer.
func copyReflectValue(val reflect.Value) (copiedVal reflect.Value, err error) {
if !val.IsValid() {
return reflect.Value{}, errors.New("invalid value")
}
var indirectVal reflect.Value
if val.Kind() != reflect.Ptr {
indirectVal = val
} else {
if val.IsNil() {
return reflect.New(val.Type().Elem()), nil
}
indirectVal = val.Elem()
}
copiedVal = reflect.New(indirectVal.Type())
data, err := json.Marshal(indirectVal.Interface())
if err != nil {
return copiedVal, fmt.Errorf("json marshal error: %w", err)
}
err = json.Unmarshal(data, copiedVal.Interface())
if err != nil {
return copiedVal, fmt.Errorf("json unmarshal error: %w", err)
}
return copiedVal, nil
}
// valuesToStruct puts url.Values to the given struct.
// target must be non-nil struct pointer otherwise it panics.
func valuesToStruct(values url.Values, target interface{}) (err error) {
if target == nil {
panic(errors.New("target is nil"))
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || typ.Elem().Kind() != reflect.Struct {
panic(errors.New("target must be struct pointer"))
}
if val.IsNil() {
panic(errors.New("target struct pointer is nil"))
}
indirectVal := val.Elem()
indirectValType := indirectVal.Type()
for i, j := 0, indirectValType.NumField(); i < j; i++ {
field := indirectValType.Field(i)
if !field.IsExported() || field.Anonymous {
continue
}
fieldName, _ := parseJSONField(field)
if fieldName == "" {
continue
}
if !values.Has(fieldName) {
continue
}
value := values.Get(fieldName)
fieldVal := indirectVal.Field(i)
ifc, kind := fieldVal.Interface(), fieldVal.Kind()
switch ifc.(type) {
case string, *string:
if kind != reflect.Ptr {
fieldVal.Set(reflect.ValueOf(value))
} else {
fieldVal.Set(reflect.ValueOf(&value))
}
case []byte, *[]byte, time.Time, *time.Time:
value = strconv.Quote(value)
err = json.Unmarshal([]byte(value), fieldVal.Addr().Interface())
if err != nil {
return fmt.Errorf("unable to unmarshal field %q value: %w", fieldName, err)
}
default:
err = json.Unmarshal([]byte(value), fieldVal.Addr().Interface())
if err != nil {
return fmt.Errorf("unable to unmarshal field %q value: %w", fieldName, err)
}
}
}
return nil
}
// structToValues returns url.Values containing struct fields as values.
// source must be nil or struct or struct pointer otherwise it panics.
func structToValues(source interface{}) (values url.Values, err error) {
values = make(url.Values)
if source == nil {
return values, nil
}
val := reflect.ValueOf(source)
typ := val.Type()
if typ.Kind() != reflect.Struct && !(typ.Kind() == reflect.Ptr && typ.Elem().Kind() == reflect.Struct) {
panic(errors.New("source must be struct or struct pointer or nil"))
}
var indirectVal reflect.Value
if val.Kind() != reflect.Ptr {
indirectVal = val
} else {
if val.IsNil() {
return values, nil
}
indirectVal = val.Elem()
}
indirectValType := indirectVal.Type()
for i, j := 0, indirectValType.NumField(); i < j; i++ {
field := indirectValType.Field(i)
if !field.IsExported() || field.Anonymous {
continue
}
fieldName, fieldOmitempty := parseJSONField(field)
if fieldName == "" {
continue
}
fieldVal := indirectVal.Field(i)
ifc, kind := fieldVal.Interface(), fieldVal.Kind()
if fieldOmitempty {
if fieldVal.IsZero() {
continue
}
if (kind == reflect.Array || kind == reflect.Slice || kind == reflect.Map) && fieldVal.Len() == 0 {
continue
}
}
switch ifc.(type) {
case string, *string:
if kind != reflect.Ptr {
values.Set(fieldName, ifc.(string))
} else {
values.Set(fieldName, *ifc.(*string))
}
case []byte, *[]byte, time.Time, *time.Time:
var data []byte
data, err = json.Marshal(fieldVal.Interface())
if err != nil {
return values, fmt.Errorf("unable to marshal field %q value: %w", fieldName, err)
}
value := string(data)
value, _ = strconv.Unquote(value)
values.Set(fieldName, value)
default:
var data []byte
data, err = json.Marshal(fieldVal.Interface())
if err != nil {
return values, fmt.Errorf("unable to marshal field %q value: %w", fieldName, err)
}
value := string(data)
values.Set(fieldName, value)
}
}
return values, nil
}
// parseJSONField parses the JSON field from the structure field.
func parseJSONField(sf reflect.StructField) (name string, omitempty bool) {
name = toJSONFieldName(sf.Name)
if v, ok := sf.Tag.Lookup("json"); ok {
sl := strings.Split(v, ",")
s := sl[0]
if s != "-" {
s = toJSONFieldName(s)
if s != "" {
name = s
}
} else {
name = ""
}
for _, s = range sl[1:] {
switch s {
case "omitempty":
omitempty = true
case "string":
}
}
}
return
}
// toJSONFieldName converts the given string to the JSON field name.
func toJSONFieldName(s string) string {
sl := []rune(s)
result := make([]rune, 0, len(sl))
for _, r := range sl {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && !(unicode.IsPunct(r) && r <= unicode.MaxASCII) {
continue
}
if r == '?' || r == '\\' || r == ',' {
continue
}
result = append(result, r)
}
return string(result)
}
// getContentEncoder returns the content encoder according to the given accept encoding for the given http.ResponseWriter.
func getContentEncoder(w http.ResponseWriter, acceptEncoding string) (result io.WriteCloser, err error) {
for _, opt := range parseHTTPHeaderOptions(acceptEncoding) {
var q *float64
if s, ok := opt.Map["q"]; ok {
if f, e := strconv.ParseFloat(s, 64); e == nil {
q = &f
} else {
return nil, fmt.Errorf("quality level parse error: %w", e)
}
}
switch key := opt.KeyVals[0].Key; key {
case "gzip":
level := gzip.DefaultCompression
if q != nil {
newLevel := int(*q)
if gzip.NoCompression <= newLevel && newLevel <= gzip.BestCompression {
level = newLevel
} else {
return nil, fmt.Errorf("invalid quality level %d", newLevel)
}
}
w.Header().Set("Content-Encoding", key)
result, _ = gzip.NewWriterLevel(w, level)
return result, nil
case "deflate":
level := flate.DefaultCompression
if q != nil {
newLevel := int(*q)
if flate.NoCompression <= newLevel && newLevel <= flate.BestCompression {
level = newLevel
} else {
return nil, fmt.Errorf("invalid quality level %d", newLevel)
}
}
w.Header().Set("Content-Encoding", key)
result, _ = flate.NewWriter(w, level)
return result, nil
}
}
return nopCloserForWriter{w}, nil
}
// nopCloserForWriter implements io.WriteCloser with a no-op Close method wrapping the provided io.Writer.
type nopCloserForWriter struct {
io.Writer
}
// Close is the implementation of io.WriteCloser.
func (nopCloserForWriter) Close() error { return nil }
// httpHeaderOption defines single http header option.
type httpHeaderOption struct {
KeyVals []httpHeaderOptionKeyVal
Map map[string]string
}
// httpHeaderOptionKeyVal is a key-value holder for httpHeaderOption.
type httpHeaderOptionKeyVal struct {
Key string
Val string
}
// parseHTTPHeaderOptions parses single http header to return list of httpHeaderOption's.
func parseHTTPHeaderOptions(directive string) (options []httpHeaderOption) {
options = []httpHeaderOption{}
for _, o := range strings.Split(directive, ",") {
o = strings.TrimSpace(o)
option := &httpHeaderOption{
KeyVals: []httpHeaderOptionKeyVal{},
Map: map[string]string{},
}
for _, kv := range strings.Split(o, ";") {
kv = strings.TrimSpace(kv)
kvs := strings.SplitN(kv, "=", 2)
optionKeyVal := &httpHeaderOptionKeyVal{
Key: strings.TrimSpace(kvs[0]),
}
if optionKeyVal.Key == "" {
continue
}
if len(kvs) > 1 {
optionKeyVal.Val = strings.TrimSpace(kvs[1])
}
option.KeyVals = append(option.KeyVals, *optionKeyVal)
if _, ok := option.Map[optionKeyVal.Key]; !ok {
option.Map[optionKeyVal.Key] = optionKeyVal.Val
}
}
if len(option.KeyVals) <= 0 {
continue
}
options = append(options, *option)
}
return
}