github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/rpc/lib/server/http_params.go (about)

     1  package server
     2  
     3  import (
     4  	"encoding/hex"
     5  	"net/http"
     6  	"regexp"
     7  	"strconv"
     8  
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  var (
    13  	// Parts of regular expressions
    14  	atom    = "[A-Z0-9!#$%&'*+\\-/=?^_`{|}~]+"
    15  	dotAtom = atom + `(?:\.` + atom + `)*`
    16  	domain  = `[A-Z0-9.-]+\.[A-Z]{2,4}`
    17  
    18  	RE_HEX     = regexp.MustCompile(`^(?i)[a-f0-9]+$`)
    19  	RE_EMAIL   = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`)
    20  	RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`)
    21  	RE_HOST    = regexp.MustCompile(`^(?i)(` + domain + `)$`)
    22  
    23  	//RE_ID12       = regexp.MustCompile(`^[a-zA-Z0-9]{12}$`)
    24  )
    25  
    26  func GetParam(r *http.Request, param string) string {
    27  	s := r.URL.Query().Get(param)
    28  	if s == "" {
    29  		s = r.FormValue(param)
    30  	}
    31  	return s
    32  }
    33  
    34  func GetParamByteSlice(r *http.Request, param string) ([]byte, error) {
    35  	s := GetParam(r, param)
    36  	return hex.DecodeString(s)
    37  }
    38  
    39  func GetParamInt64(r *http.Request, param string) (int64, error) {
    40  	s := GetParam(r, param)
    41  	i, err := strconv.ParseInt(s, 10, 64)
    42  	if err != nil {
    43  		return 0, errors.Errorf(param, err.Error())
    44  	}
    45  	return i, nil
    46  }
    47  
    48  func GetParamInt32(r *http.Request, param string) (int32, error) {
    49  	s := GetParam(r, param)
    50  	i, err := strconv.ParseInt(s, 10, 32)
    51  	if err != nil {
    52  		return 0, errors.Errorf(param, err.Error())
    53  	}
    54  	return int32(i), nil
    55  }
    56  
    57  func GetParamUint64(r *http.Request, param string) (uint64, error) {
    58  	s := GetParam(r, param)
    59  	i, err := strconv.ParseUint(s, 10, 64)
    60  	if err != nil {
    61  		return 0, errors.Errorf(param, err.Error())
    62  	}
    63  	return i, nil
    64  }
    65  
    66  func GetParamUint(r *http.Request, param string) (uint, error) {
    67  	s := GetParam(r, param)
    68  	i, err := strconv.ParseUint(s, 10, 64)
    69  	if err != nil {
    70  		return 0, errors.Errorf(param, err.Error())
    71  	}
    72  	return uint(i), nil
    73  }
    74  
    75  func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
    76  	s := GetParam(r, param)
    77  	if !re.MatchString(s) {
    78  		return "", errors.Errorf(param, "Did not match regular expression %v", re.String())
    79  	}
    80  	return s, nil
    81  }
    82  
    83  func GetParamFloat64(r *http.Request, param string) (float64, error) {
    84  	s := GetParam(r, param)
    85  	f, err := strconv.ParseFloat(s, 64)
    86  	if err != nil {
    87  		return 0, errors.Errorf(param, err.Error())
    88  	}
    89  	return f, nil
    90  }