-
Notifications
You must be signed in to change notification settings - Fork 2
/
params.go
492 lines (448 loc) · 12.1 KB
/
params.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright 2022 RapidLoop, Inc.
*
* Licensed 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 rapidrows
import (
"compress/flate"
"compress/gzip"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
"github.com/rs/zerolog"
)
//------------------------------------------------------------------------------
// parameters
type paramInfo struct {
rx *regexp.Regexp // compiled "^{.Pattern}$"
enum any // []string, []int64 or []float64
}
func (a *APIServer) prepareParams() {
for _, ep := range a.cfg.Endpoints {
for _, p := range ep.Params {
var info paramInfo
// pattern
if len(p.Pattern) > 0 {
if rx, err := regexp.Compile("^" + p.Pattern + "$"); err == nil {
info.rx = rx
}
}
// enum
if len(p.Enum) > 0 && (p.Type == "string" || p.Type == "integer" || p.Type == "number") {
var sa []string
var ia []int64
var na []float64
for _, v := range p.Enum {
switch p.Type {
case "string":
if s, ok := v.(string); ok {
sa = append(sa, s)
}
case "integer":
if i, ok := v.(int64); ok {
ia = append(ia, i)
} else if i, ok := v.(uint64); ok {
ia = append(ia, int64(i)) // checked to be <=math.MaxInt64
} else if f, ok := v.(float64); ok {
if i, ok := float2int(f); ok {
ia = append(ia, i)
}
} else if s, ok := v.(string); ok {
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
ia = append(ia, i)
}
}
case "number":
if i, ok := v.(int64); ok {
na = append(na, float64(i))
} else if i, ok := v.(uint64); ok {
na = append(na, float64(i))
} else if f, ok := v.(float64); ok {
na = append(na, f)
} else if s, ok := v.(string); ok {
if f, err := strconv.ParseFloat(s, 64); err == nil {
na = append(na, f)
}
}
}
}
if len(sa) > 0 {
info.enum = sa
} else if len(ia) > 0 {
info.enum = ia
} else if len(na) > 0 {
info.enum = na
}
} // enum
if info.rx != nil || info.enum != nil {
a.pinfo.Store(ep.URI+"#"+p.Name, &info)
}
} // for each param
} // for each endpoint
}
func (a *APIServer) isSuitable(ep *Endpoint, p *Param, v any) (out any, err error) {
// note: in case of query param or POST form body, v is always a []string
var s string
sv := false
if sa, ok := v.([]string); ok && len(sa) == 1 {
s = sa[0]
sv = true
} else {
s, sv = v.(string)
}
switch p.Type {
case "string":
if sv {
return a.checkString(ep, p, s)
}
return nil, errors.New("not a string")
case "integer":
if sv {
return a.checkIntegerAny(ep, p, s)
}
return a.checkIntegerAny(ep, p, v)
case "number":
if sv {
return a.checkFloatAny(ep, p, s)
}
return a.checkFloatAny(ep, p, v)
case "boolean":
if sv {
return a.checkBoolAny(ep, p, s)
}
return a.checkBoolAny(ep, p, v)
case "array":
return a.checkArrayAny(ep, p, v)
}
// should not happen if valid cfg
return nil, errors.New("unknown parameter type")
}
func (a *APIServer) checkStringAny(ep *Endpoint, p *Param, v any) (string, error) {
if s, ok := v.(string); ok {
return a.checkString(ep, p, s)
}
return "", fmt.Errorf("cannot convert value of type %T to string", v)
}
func (a *APIServer) checkString(ep *Endpoint, p *Param, s string) (string, error) {
// enum
if len(p.Enum) > 0 {
if pi, ok := a.pinfo.Load(ep.URI + "#" + p.Name); ok && pi != nil {
for _, v := range (pi.(*paramInfo)).enum.([]string) {
if v == s {
return s, nil
}
}
}
return "", errors.New("does not match any of the enumerated values")
}
// maxLength
if p.MaxLength != nil && *p.MaxLength >= 0 && len(s) > *p.MaxLength {
return "", fmt.Errorf("exceeds specified max length of %d", *p.MaxLength)
}
// pattern
if len(p.Pattern) > 0 {
if pi, ok := a.pinfo.Load(ep.URI + "#" + p.Name); ok && pi != nil {
if rx := (pi.(*paramInfo)).rx; rx != nil {
if !rx.MatchString(s) {
return "", fmt.Errorf("does not match pattern %s", p.Pattern)
}
}
}
}
return s, nil
}
func (a *APIServer) checkIntegerAny(ep *Endpoint, p *Param, v any) (int64, error) {
if s, ok := v.(string); ok {
// allow both "200.00" and "200"
if f, err := strconv.ParseFloat(s, 64); err == nil {
if i, ok := float2int(f); ok {
return a.checkInteger(ep, p, i)
}
}
return 0, errors.New("not a valid integer")
} else if f, ok := v.(float64); ok {
if i, ok := float2int(f); ok {
return a.checkInteger(ep, p, i)
}
}
return 0, fmt.Errorf("cannot convert value of type %T to integer", v)
}
func (a *APIServer) checkInteger(ep *Endpoint, p *Param, i int64) (int64, error) {
// enum
if len(p.Enum) > 0 {
if pi, ok := a.pinfo.Load(ep.URI + "#" + p.Name); ok && pi != nil {
for _, v := range (pi.(*paramInfo)).enum.([]int64) {
if v == i {
return i, nil
}
}
}
return 0, errors.New("does not match any of the enumerated values")
}
// minimum
if p.Minimum != nil {
if min := int64(*p.Minimum); i < min {
return 0, fmt.Errorf("is lower than the minimum of %d", min)
}
}
// maximum
if p.Maximum != nil {
if max := int64(*p.Maximum); i > max {
return 0, fmt.Errorf("is higher than the maximum of %d", max)
}
}
return i, nil
}
func (a *APIServer) checkFloatAny(ep *Endpoint, p *Param, v any) (float64, error) {
if s, ok := v.(string); ok {
if f, err := strconv.ParseFloat(s, 64); err != nil {
return 0, errors.New("not a valid number")
} else {
return a.checkFloat(ep, p, f)
}
} else if f, ok := v.(float64); ok && !math.IsNaN(f) && !math.IsInf(f, 0) {
return a.checkFloat(ep, p, f)
}
return 0, fmt.Errorf("cannot convert value of type %T to number", v)
}
func (a *APIServer) checkFloat(ep *Endpoint, p *Param, f float64) (float64, error) {
// enum
if len(p.Enum) > 0 {
if pi, ok := a.pinfo.Load(ep.URI + "#" + p.Name); ok && pi != nil {
for _, v := range (pi.(*paramInfo)).enum.([]float64) {
if v == f {
return f, nil
}
}
}
return 0, errors.New("does not match any of the enumerated values")
}
// minimum
if p.Minimum != nil {
if min := float64(*p.Minimum); f < min {
return 0, fmt.Errorf("is lower than the minimum of %g", min)
}
}
// maximum
if p.Maximum != nil {
if max := float64(*p.Maximum); f > max {
return 0, fmt.Errorf("is higher than the maximum of %g", max)
}
}
return f, nil
}
func float2int(f float64) (i int64, ok bool) {
if i, frac := math.Modf(f); math.Abs(frac) < 1e-9 {
return int64(i), true
}
return 0, false
}
func (a *APIServer) checkBoolAny(ep *Endpoint, p *Param, v any) (out bool, err error) {
if s, ok := v.(string); ok {
s = strings.ToLower(s)
if s == "true" {
return true, nil
} else if s == "false" {
return false, nil
}
} else if b, ok := v.(bool); ok {
return b, nil
}
return false, fmt.Errorf("cannot convert value of type %T to boolean", v)
}
func (a *APIServer) checkArrayAny(ep *Endpoint, p *Param, v any) (out any, err error) {
if sa, ok := v.([]string); ok {
aa := make([]any, len(sa))
for i := range sa {
aa[i] = sa[i]
}
return a.checkArray(ep, p, aa)
} else if aa, ok := v.([]any); ok {
return a.checkArray(ep, p, aa)
}
return nil, fmt.Errorf("cannot convert value of type %T to array", v)
}
func (a *APIServer) checkArray(ep *Endpoint, p *Param, v []any) (out any, err error) {
// minItems
if p.MinItems != nil && len(v) < *p.MinItems {
return nil, fmt.Errorf("fewer than the specified minimum of %d items", *p.MinItems)
}
// maxItems
if p.MaxItems != nil && len(v) > *p.MaxItems {
return nil, fmt.Errorf("more than the specified maximum of %d items", *p.MaxItems)
}
// result is one of:
var (
sa []string
ia []int64
fa []float64
ba []bool
)
// for each element:
for j, ev := range v {
switch p.ElemType {
case "integer":
if i, err := a.checkIntegerAny(ep, p, ev); err != nil {
return nil, fmt.Errorf("enum value #%d: %v", j+1, err)
} else {
ia = append(ia, i)
}
case "number":
if f, err := a.checkFloatAny(ep, p, ev); err != nil {
return nil, fmt.Errorf("enum value #%d: %v", j+1, err)
} else {
fa = append(fa, f)
}
case "string":
if s, err := a.checkStringAny(ep, p, ev); err != nil {
return nil, fmt.Errorf("enum value #%d: %v", j+1, err)
} else {
sa = append(sa, s)
}
case "boolean":
if b, err := a.checkBoolAny(ep, p, ev); err != nil {
return nil, fmt.Errorf("enum value #%d: %v", j+1, err)
} else {
ba = append(ba, b)
}
}
}
// done, return appropriately
switch p.ElemType {
case "integer":
return ia, nil
case "number":
return fa, nil
case "string":
return sa, nil
case "boolean":
return ba, nil
}
// should not happen for valid cfg
return nil, fmt.Errorf("invalid elemType %q", p.ElemType)
}
func getCT(req *http.Request) (out string) {
out = req.Header.Get("Content-Type")
if pos := strings.IndexByte(out, ';'); pos > 0 {
out = out[:pos]
}
return
}
func getJSON(req *http.Request, data any) error {
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
return json.Unmarshal(b, data)
}
func (a *APIServer) getParams(req *http.Request, ep *Endpoint,
logger zerolog.Logger) ([]any, error) {
var (
jsonData map[string]any
formData url.Values
urlData url.Values
)
if req.Method == "GET" {
urlData = req.URL.Query()
} else {
var wrapped bool
if ce := req.Header.Get("Content-Encoding"); ce == "gzip" {
if r, err := gzip.NewReader(req.Body); err != nil {
logger.Error().Err(err).Msg("failed to initialize gzip reader")
return nil, fmt.Errorf("failed to initialize gzip reader: %v", err)
} else {
wrapped = true
req.Body = r
}
} else if ce == "deflate" {
wrapped = true
req.Body = flate.NewReader(req.Body)
}
if ct := getCT(req); ct == "application/json" {
if err := getJSON(req, &jsonData); err != nil {
logger.Warn().Err(err).Msg("failed to decode json object in request body")
jsonData = nil
}
} else if ct == "application/x-www-form-urlencoded" {
if err := req.ParseForm(); err != nil {
logger.Warn().Err(err).Msg("failed to parse form data in request body")
} else {
formData = req.PostForm
}
}
if wrapped {
if rc, ok := req.Body.(io.Closer); ok {
if err := rc.Close(); err != nil {
logger.Warn().Err(err).Msg("failed to close gzip/deflate reader")
}
}
}
}
// discard (the rest of the) body, ignore errors, no need to close req.Body
_, _ = io.CopyN(io.Discard, req.Body, 4096)
getParam := func(in, key string) (v any, ok bool) {
switch in {
case "path":
v = chi.URLParam(req, key)
ok = v != ""
case "query":
v, ok = urlData[key]
case "body":
if jsonData != nil {
v, ok = jsonData[key]
} else if formData != nil {
v, ok = formData[key]
}
}
return
}
out := make([]any, len(ep.Params))
for i := range ep.Params {
p := &ep.Params[i]
v, ok := getParam(p.In, p.Name)
if !ok {
if p.Required {
logger.Error().Str("param", p.Name).Msg("value required but not supplied")
return nil, fmt.Errorf("param %q: value required but not supplied", p.Name)
} else {
out[i] = nil
continue
}
}
// special case: boolean url/form parameters with no value will be considered
// as true
if p.Type == "boolean" &&
(p.In == "query" || (p.In == "body" && jsonData == nil && formData != nil)) &&
ok {
if sa := v.([]string); len(sa) == 1 && len(sa[0]) == 0 {
v = true
}
}
if v2, err := a.isSuitable(ep, p, v); err != nil {
logger.Error().Str("param", p.Name).Err(err).Msg("invalid value")
return nil, fmt.Errorf("param %q: invalid value: %v", p.Name, err)
} else {
out[i] = v2
}
}
return out, nil
}