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.

114 lines
2.6 KiB

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