github.com/moby/docker@v26.1.3+incompatible/api/server/httputils/form.go (about)

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