form.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "net/http"
  7. )
  8. const defaultMemory = 32 << 20
  9. type formBinding struct{}
  10. type formPostBinding struct{}
  11. type formMultipartBinding struct{}
  12. func (formBinding) Name() string {
  13. return "form"
  14. }
  15. func (formBinding) Bind(req *http.Request, obj interface{}) error {
  16. if err := req.ParseForm(); err != nil {
  17. return err
  18. }
  19. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  20. if err != http.ErrNotMultipart {
  21. return err
  22. }
  23. }
  24. if err := mapForm(obj, req.Form); err != nil {
  25. return err
  26. }
  27. return validate(obj)
  28. }
  29. func (formPostBinding) Name() string {
  30. return "form-urlencoded"
  31. }
  32. func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
  33. if err := req.ParseForm(); err != nil {
  34. return err
  35. }
  36. if err := mapForm(obj, req.PostForm); err != nil {
  37. return err
  38. }
  39. return validate(obj)
  40. }
  41. func (formMultipartBinding) Name() string {
  42. return "multipart/form-data"
  43. }
  44. func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
  45. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  46. return err
  47. }
  48. if err := mappingByPtr(obj, (*multipartRequest)(req), "form"); err != nil {
  49. return err
  50. }
  51. return validate(obj)
  52. }