form.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package httputils // import "github.com/docker/docker/api/server/httputils"
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/distribution/reference"
  8. )
  9. // BoolValue transforms a form value in different formats into a boolean type.
  10. func BoolValue(r *http.Request, k string) bool {
  11. s := strings.ToLower(strings.TrimSpace(r.FormValue(k)))
  12. return !(s == "" || s == "0" || s == "no" || s == "false" || s == "none")
  13. }
  14. // BoolValueOrDefault returns the default bool passed if the query param is
  15. // missing, otherwise it's just a proxy to boolValue above.
  16. func BoolValueOrDefault(r *http.Request, k string, d bool) bool {
  17. if _, ok := r.Form[k]; !ok {
  18. return d
  19. }
  20. return BoolValue(r, k)
  21. }
  22. // Int64ValueOrZero parses a form value into an int64 type.
  23. // It returns 0 if the parsing fails.
  24. func Int64ValueOrZero(r *http.Request, k string) int64 {
  25. val, err := Int64ValueOrDefault(r, k, 0)
  26. if err != nil {
  27. return 0
  28. }
  29. return val
  30. }
  31. // Int64ValueOrDefault parses a form value into an int64 type. If there is an
  32. // error, returns the error. If there is no value returns the default value.
  33. func Int64ValueOrDefault(r *http.Request, field string, def int64) (int64, error) {
  34. if r.Form.Get(field) != "" {
  35. value, err := strconv.ParseInt(r.Form.Get(field), 10, 64)
  36. return value, err
  37. }
  38. return def, nil
  39. }
  40. // RepoTagReference parses form values "repo" and "tag" and returns a valid
  41. // reference with repository and tag.
  42. // If repo is empty, then a nil reference is returned.
  43. // If no tag is given, then the default "latest" tag is set.
  44. func RepoTagReference(repo, tag string) (reference.NamedTagged, error) {
  45. if repo == "" {
  46. return nil, nil
  47. }
  48. ref, err := reference.ParseNormalizedNamed(repo)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if _, isDigested := ref.(reference.Digested); isDigested {
  53. return nil, fmt.Errorf("cannot import digest reference")
  54. }
  55. if tag != "" {
  56. return reference.WithTag(ref, tag)
  57. }
  58. withDefaultTag := reference.TagNameOnly(ref)
  59. namedTagged, ok := withDefaultTag.(reference.NamedTagged)
  60. if !ok {
  61. return nil, fmt.Errorf("unexpected reference: %q", ref.String())
  62. }
  63. return namedTagged, nil
  64. }
  65. // ArchiveOptions stores archive information for different operations.
  66. type ArchiveOptions struct {
  67. Name string
  68. Path string
  69. }
  70. type badParameterError struct {
  71. param string
  72. }
  73. func (e badParameterError) Error() string {
  74. return "bad parameter: " + e.param + "cannot be empty"
  75. }
  76. func (e badParameterError) InvalidParameter() {}
  77. // ArchiveFormValues parses form values and turns them into ArchiveOptions.
  78. // It fails if the archive name and path are not in the request.
  79. func ArchiveFormValues(r *http.Request, vars map[string]string) (ArchiveOptions, error) {
  80. if err := ParseForm(r); err != nil {
  81. return ArchiveOptions{}, err
  82. }
  83. name := vars["name"]
  84. if name == "" {
  85. return ArchiveOptions{}, badParameterError{"name"}
  86. }
  87. path := r.Form.Get("path")
  88. if path == "" {
  89. return ArchiveOptions{}, badParameterError{"path"}
  90. }
  91. return ArchiveOptions{name, path}, nil
  92. }