2018-02-05 16:05:59 -05:00
|
|
|
package httputils // import "github.com/docker/docker/api/server/httputils"
|
2015-09-23 19:42:08 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BoolValue transforms a form value in different formats into a boolean type.
|
|
|
|
func BoolValue(r *http.Request, k string) bool {
|
|
|
|
s := strings.ToLower(strings.TrimSpace(r.FormValue(k)))
|
|
|
|
return !(s == "" || s == "0" || s == "no" || s == "false" || s == "none")
|
|
|
|
}
|
|
|
|
|
|
|
|
// BoolValueOrDefault returns the default bool passed if the query param is
|
2017-02-07 07:29:32 -05:00
|
|
|
// missing, otherwise it's just a proxy to boolValue above.
|
2015-09-23 19:42:08 -04:00
|
|
|
func BoolValueOrDefault(r *http.Request, k string, d bool) bool {
|
|
|
|
if _, ok := r.Form[k]; !ok {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return BoolValue(r, k)
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:51:43 -04:00
|
|
|
// Int64ValueOrZero parses a form value into an int64 type.
|
2015-09-23 19:42:08 -04:00
|
|
|
// It returns 0 if the parsing fails.
|
|
|
|
func Int64ValueOrZero(r *http.Request, k string) int64 {
|
2015-09-23 14:51:43 -04:00
|
|
|
val, err := Int64ValueOrDefault(r, k, 0)
|
2015-09-23 19:42:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2015-09-23 14:51:43 -04:00
|
|
|
// Int64ValueOrDefault parses a form value into an int64 type. If there is an
|
|
|
|
// error, returns the error. If there is no value returns the default value.
|
|
|
|
func Int64ValueOrDefault(r *http.Request, field string, def int64) (int64, error) {
|
|
|
|
if r.Form.Get(field) != "" {
|
|
|
|
value, err := strconv.ParseInt(r.Form.Get(field), 10, 64)
|
2016-12-14 16:16:12 -05:00
|
|
|
return value, err
|
2015-09-23 14:51:43 -04:00
|
|
|
}
|
|
|
|
return def, nil
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
// ArchiveOptions stores archive information for different operations.
|
|
|
|
type ArchiveOptions struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type badParameterError struct {
|
|
|
|
param string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e badParameterError) Error() string {
|
|
|
|
return "bad parameter: " + e.param + "cannot be empty"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e badParameterError) InvalidParameter() {}
|
|
|
|
|
2015-09-23 19:42:08 -04:00
|
|
|
// ArchiveFormValues parses form values and turns them into ArchiveOptions.
|
|
|
|
// It fails if the archive name and path are not in the request.
|
|
|
|
func ArchiveFormValues(r *http.Request, vars map[string]string) (ArchiveOptions, error) {
|
|
|
|
if err := ParseForm(r); err != nil {
|
|
|
|
return ArchiveOptions{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := vars["name"]
|
2017-07-19 10:20:13 -04:00
|
|
|
if name == "" {
|
|
|
|
return ArchiveOptions{}, badParameterError{"name"}
|
2015-09-23 19:42:08 -04:00
|
|
|
}
|
2017-08-03 20:22:00 -04:00
|
|
|
path := r.Form.Get("path")
|
2017-07-19 10:20:13 -04:00
|
|
|
if path == "" {
|
|
|
|
return ArchiveOptions{}, badParameterError{"path"}
|
|
|
|
}
|
2015-09-23 19:42:08 -04:00
|
|
|
return ArchiveOptions{name, path}, nil
|
|
|
|
}
|