You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.2 KiB

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