github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/lib/server/http_params.go (about)

     1  package rpcserver
     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_INT     = regexp.MustCompile(`^-?[0-9]+$`)
    19  	RE_HEX     = regexp.MustCompile(`^(?i)[a-f0-9]+$`)
    20  	RE_EMAIL   = regexp.MustCompile(`^(?i)(` + dotAtom + `)@(` + dotAtom + `)$`)
    21  	RE_ADDRESS = regexp.MustCompile(`^(?i)[a-z0-9]{25,34}$`)
    22  	RE_HOST    = regexp.MustCompile(`^(?i)(` + domain + `)$`)
    23  
    24  	//RE_ID12       = regexp.MustCompile(`^[a-zA-Z0-9]{12}$`)
    25  )
    26  
    27  func GetParam(r *http.Request, param string) string {
    28  	s := r.URL.Query().Get(param)
    29  	if s == "" {
    30  		s = r.FormValue(param)
    31  	}
    32  	return s
    33  }
    34  
    35  func GetParamByteSlice(r *http.Request, param string) ([]byte, error) {
    36  	s := GetParam(r, param)
    37  	return hex.DecodeString(s)
    38  }
    39  
    40  func GetParamInt64(r *http.Request, param string) (int64, error) {
    41  	s := GetParam(r, param)
    42  	i, err := strconv.ParseInt(s, 10, 64)
    43  	if err != nil {
    44  		return 0, errors.Errorf(param, err.Error())
    45  	}
    46  	return i, nil
    47  }
    48  
    49  func GetParamInt32(r *http.Request, param string) (int32, error) {
    50  	s := GetParam(r, param)
    51  	i, err := strconv.ParseInt(s, 10, 32)
    52  	if err != nil {
    53  		return 0, errors.Errorf(param, err.Error())
    54  	}
    55  	return int32(i), nil
    56  }
    57  
    58  func GetParamUint64(r *http.Request, param string) (uint64, error) {
    59  	s := GetParam(r, param)
    60  	i, err := strconv.ParseUint(s, 10, 64)
    61  	if err != nil {
    62  		return 0, errors.Errorf(param, err.Error())
    63  	}
    64  	return i, nil
    65  }
    66  
    67  func GetParamUint(r *http.Request, param string) (uint, error) {
    68  	s := GetParam(r, param)
    69  	i, err := strconv.ParseUint(s, 10, 64)
    70  	if err != nil {
    71  		return 0, errors.Errorf(param, err.Error())
    72  	}
    73  	return uint(i), nil
    74  }
    75  
    76  func GetParamRegexp(r *http.Request, param string, re *regexp.Regexp) (string, error) {
    77  	s := GetParam(r, param)
    78  	if !re.MatchString(s) {
    79  		return "", errors.Errorf(param, "did not match regular expression %v", re.String())
    80  	}
    81  	return s, nil
    82  }
    83  
    84  func GetParamFloat64(r *http.Request, param string) (float64, error) {
    85  	s := GetParam(r, param)
    86  	f, err := strconv.ParseFloat(s, 64)
    87  	if err != nil {
    88  		return 0, errors.Errorf(param, err.Error())
    89  	}
    90  	return f, nil
    91  }