header.go 739 B

12345678910111213141516171819202122232425262728293031323334
  1. package binding
  2. import (
  3. "net/http"
  4. "net/textproto"
  5. "reflect"
  6. )
  7. type headerBinding struct{}
  8. func (headerBinding) Name() string {
  9. return "header"
  10. }
  11. func (headerBinding) Bind(req *http.Request, obj interface{}) error {
  12. if err := mapHeader(obj, req.Header); err != nil {
  13. return err
  14. }
  15. return validate(obj)
  16. }
  17. func mapHeader(ptr interface{}, h map[string][]string) error {
  18. return mappingByPtr(ptr, headerSource(h), "header")
  19. }
  20. type headerSource map[string][]string
  21. var _ setter = headerSource(nil)
  22. func (hs headerSource) TrySet(value reflect.Value, field reflect.StructField, tagValue string, opt setOptions) (isSetted bool, err error) {
  23. return setByForm(value, field, hs, textproto.CanonicalMIMEHeaderKey(tagValue), opt)
  24. }