2021-05-07 13:02:34 +02:00
|
|
|
package httpin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2021-05-08 09:27:37 +02:00
|
|
|
// formValueExtractor implements the "form" executor who extracts values from
|
2021-05-07 16:35:34 +02:00
|
|
|
// the forms of an HTTP request.
|
2021-05-08 09:27:37 +02:00
|
|
|
func formValueExtractor(ctx *DirectiveContext) error {
|
2021-05-07 13:02:34 +02:00
|
|
|
return extractFromKVS(ctx, ctx.Request.Form, false)
|
|
|
|
}
|
|
|
|
|
2021-05-08 09:27:37 +02:00
|
|
|
// headerValueExtractor implements the "header" executor who extracts values
|
2021-05-07 16:35:34 +02:00
|
|
|
// from the HTTP headers.
|
2021-05-08 09:27:37 +02:00
|
|
|
func headerValueExtractor(ctx *DirectiveContext) error {
|
2021-05-07 13:02:34 +02:00
|
|
|
return extractFromKVS(ctx, ctx.Request.Header, true)
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:35:34 +02:00
|
|
|
func extractFromKVS(ctx *DirectiveContext, kvs map[string][]string, headerKey bool) error {
|
2021-05-08 09:27:37 +02:00
|
|
|
for _, key := range ctx.directive.Argv {
|
|
|
|
debug(" > execute directive %q with key %q\n", ctx.directive.Executor, key)
|
2021-05-07 16:35:34 +02:00
|
|
|
if headerKey {
|
|
|
|
key = http.CanonicalHeaderKey(key)
|
|
|
|
}
|
|
|
|
if err := extractFromKVSWithKey(ctx, kvs, key); err != nil {
|
|
|
|
return err
|
2021-05-07 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractFromKVSWithKey(ctx *DirectiveContext, kvs map[string][]string, key string) error {
|
2021-05-08 09:27:37 +02:00
|
|
|
if ctx.Context.Value(FieldSet) == true {
|
2021-05-07 13:02:34 +02:00
|
|
|
debug(" > field already set, skip\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(ggicci): Array?
|
|
|
|
if ctx.ValueType.Kind() == reflect.Slice {
|
|
|
|
return extractFromKVSWithKeyForSlice(ctx, kvs, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := decoderOf(ctx.ValueType)
|
|
|
|
if decoder == nil {
|
|
|
|
return UnsupportedTypeError{ctx.ValueType}
|
|
|
|
}
|
|
|
|
|
|
|
|
formValues, exists := kvs[key]
|
|
|
|
if !exists {
|
|
|
|
debug(" > key %q not found in %s\n", key, ctx.Executor)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var got string
|
|
|
|
if len(formValues) > 0 {
|
|
|
|
got = formValues[0]
|
|
|
|
}
|
2021-05-08 05:50:24 +02:00
|
|
|
if interfaceValue, err := decoder.Decode([]byte(got)); err != nil {
|
2021-05-07 13:02:34 +02:00
|
|
|
return err
|
2021-05-08 05:50:24 +02:00
|
|
|
} else {
|
|
|
|
ctx.Value.Elem().Set(reflect.ValueOf(interfaceValue))
|
2021-05-07 13:02:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 09:27:37 +02:00
|
|
|
ctx.DeliverContextValue(FieldSet, true)
|
2021-05-07 13:02:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-07 16:35:34 +02:00
|
|
|
func extractFromKVSWithKeyForSlice(ctx *DirectiveContext, kvs map[string][]string, key string) error {
|
|
|
|
elemType := ctx.ValueType.Elem()
|
|
|
|
|
|
|
|
decoder := decoderOf(elemType)
|
|
|
|
if decoder == nil {
|
|
|
|
return UnsupportedTypeError{ctx.ValueType}
|
|
|
|
}
|
|
|
|
|
|
|
|
formValues, exists := kvs[key]
|
|
|
|
if !exists {
|
|
|
|
debug(" > key %q not found in %s\n", key, ctx.Executor)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
theSlice := reflect.MakeSlice(ctx.ValueType, len(formValues), len(formValues))
|
|
|
|
for i, formValue := range formValues {
|
2021-05-08 05:50:24 +02:00
|
|
|
if interfaceValue, err := decoder.Decode([]byte(formValue)); err != nil {
|
2021-05-07 16:35:34 +02:00
|
|
|
return fmt.Errorf("at index %d: %w", i, err)
|
2021-05-08 05:50:24 +02:00
|
|
|
} else {
|
|
|
|
theSlice.Index(i).Set(reflect.ValueOf(interfaceValue))
|
2021-05-07 13:02:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-07 16:35:34 +02:00
|
|
|
|
|
|
|
ctx.Value.Elem().Set(theSlice)
|
2021-05-08 09:27:37 +02:00
|
|
|
ctx.DeliverContextValue(FieldSet, true)
|
2021-05-07 13:02:34 +02:00
|
|
|
return nil
|
|
|
|
}
|