github.com/gunjan5/docker@v1.8.2/api/server/form.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    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  
    15  // boolValueOrDefault returns the default bool passed if the query param is
    16  // missing, otherwise it's just a proxy to boolValue above
    17  func boolValueOrDefault(r *http.Request, k string, d bool) bool {
    18  	if _, ok := r.Form[k]; !ok {
    19  		return d
    20  	}
    21  	return boolValue(r, k)
    22  }
    23  
    24  func int64ValueOrZero(r *http.Request, k string) int64 {
    25  	val, err := strconv.ParseInt(r.FormValue(k), 10, 64)
    26  	if err != nil {
    27  		return 0
    28  	}
    29  	return val
    30  }
    31  
    32  type archiveOptions struct {
    33  	name string
    34  	path string
    35  }
    36  
    37  func archiveFormValues(r *http.Request, vars map[string]string) (archiveOptions, error) {
    38  	if vars == nil {
    39  		return archiveOptions{}, fmt.Errorf("Missing parameter")
    40  	}
    41  	if err := parseForm(r); err != nil {
    42  		return archiveOptions{}, err
    43  	}
    44  
    45  	name := vars["name"]
    46  	path := r.Form.Get("path")
    47  
    48  	switch {
    49  	case name == "":
    50  		return archiveOptions{}, fmt.Errorf("bad parameter: 'name' cannot be empty")
    51  	case path == "":
    52  		return archiveOptions{}, fmt.Errorf("bad parameter: 'path' cannot be empty")
    53  	}
    54  
    55  	return archiveOptions{name, path}, nil
    56  }