2022-09-28 20:30:40 +03:00
|
|
|
// Copyright 2022 Gin Core Team. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-08-12 22:03:24 +03:00
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/textproto"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type headerBinding struct{}
|
|
|
|
|
|
|
|
func (headerBinding) Name() string {
|
|
|
|
return "header"
|
|
|
|
}
|
|
|
|
|
2022-09-28 20:30:40 +03:00
|
|
|
func (headerBinding) Bind(req *http.Request, obj any) error {
|
2021-08-12 22:03:24 +03:00
|
|
|
|
|
|
|
if err := mapHeader(obj, req.Header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return validate(obj)
|
|
|
|
}
|
|
|
|
|
2022-09-28 20:30:40 +03:00
|
|
|
func mapHeader(ptr any, h map[string][]string) error {
|
2021-08-12 22:03:24 +03:00
|
|
|
return mappingByPtr(ptr, headerSource(h), "header")
|
|
|
|
}
|
|
|
|
|
|
|
|
type headerSource map[string][]string
|
|
|
|
|
|
|
|
var _ setter = headerSource(nil)
|
|
|
|
|
2022-09-28 20:30:40 +03:00
|
|
|
func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (bool, error) {
|
2021-08-12 22:03:24 +03:00
|
|
|
return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)
|
|
|
|
}
|