|
|
|
@ -12,6 +12,7 @@ import (
|
|
|
|
|
const (
|
|
|
|
|
formKey = "form"
|
|
|
|
|
pathKey = "path"
|
|
|
|
|
headerKey = "header"
|
|
|
|
|
emptyJson = "{}"
|
|
|
|
|
maxMemory = 32 << 20 // 32MB
|
|
|
|
|
maxBodyLen = 8 << 20 // 8MB
|
|
|
|
@ -20,8 +21,9 @@ const (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
formUnmarshaler = mapping.NewUnmarshaler(formKey, mapping.WithStringValues())
|
|
|
|
|
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
|
|
|
|
|
formUnmarshaler = mapping.NewUnmarshaler(formKey, mapping.WithStringValues())
|
|
|
|
|
pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
|
|
|
|
|
headerUnmarshaler = mapping.NewUnmarshaler(headerKey, mapping.WithStringValues())
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Parse parses the request.
|
|
|
|
@ -34,9 +36,28 @@ func Parse(r *http.Request, v interface{}) error {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := ParseHeaders(r, v); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ParseJsonBody(r, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseHeaders parses the headers request.
|
|
|
|
|
func ParseHeaders(r *http.Request, v interface{}) error {
|
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
for k, v := range r.Header {
|
|
|
|
|
k = strings.ToLower(k)
|
|
|
|
|
if len(v) == 1 {
|
|
|
|
|
m[k] = v[0]
|
|
|
|
|
} else {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return headerUnmarshaler.Unmarshal(m, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseForm parses the form request.
|
|
|
|
|
func ParseForm(r *http.Request, v interface{}) error {
|
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
|