form.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "net/http"
  6. const defaultMemory = 32 * 1024 * 1024
  7. type formBinding struct{}
  8. type formPostBinding struct{}
  9. type formMultipartBinding struct{}
  10. func (formBinding) Name() string {
  11. return "form"
  12. }
  13. func (formBinding) Bind(req *http.Request, obj interface{}) error {
  14. if err := req.ParseForm(); err != nil {
  15. return err
  16. }
  17. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  18. if err != http.ErrNotMultipart {
  19. return err
  20. }
  21. }
  22. if err := mapForm(obj, req.Form); err != nil {
  23. return err
  24. }
  25. return validate(obj)
  26. }
  27. func (formPostBinding) Name() string {
  28. return "form-urlencoded"
  29. }
  30. func (formPostBinding) Bind(req *http.Request, obj interface{}) error {
  31. if err := req.ParseForm(); err != nil {
  32. return err
  33. }
  34. if err := mapForm(obj, req.PostForm); err != nil {
  35. return err
  36. }
  37. return validate(obj)
  38. }
  39. func (formMultipartBinding) Name() string {
  40. return "multipart/form-data"
  41. }
  42. func (formMultipartBinding) Bind(req *http.Request, obj interface{}) error {
  43. if err := req.ParseMultipartForm(defaultMemory); err != nil {
  44. return err
  45. }
  46. if err := mapForm(obj, req.MultipartForm.Value); err != nil {
  47. return err
  48. }
  49. return validate(obj)
  50. }