header.go 728 B

123456789101112131415161718192021222324252627282930313233
  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 nil
  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. }