github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/api/server/form.go (about)

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