request.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package request
  5. import (
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "github.com/gorilla/mux"
  12. )
  13. // Cookie returns the cookie value.
  14. func Cookie(r *http.Request, name string) string {
  15. cookie, err := r.Cookie(name)
  16. if err == http.ErrNoCookie {
  17. return ""
  18. }
  19. return cookie.Value
  20. }
  21. // FormInt64Value returns a form value as integer.
  22. func FormInt64Value(r *http.Request, param string) int64 {
  23. value := r.FormValue(param)
  24. integer, err := strconv.ParseInt(value, 10, 64)
  25. if err != nil {
  26. return 0
  27. }
  28. return integer
  29. }
  30. // IntParam returns an URL route parameter as integer.
  31. func IntParam(r *http.Request, param string) (int64, error) {
  32. vars := mux.Vars(r)
  33. value, err := strconv.Atoi(vars[param])
  34. if err != nil {
  35. return 0, fmt.Errorf("request: %s parameter is not an integer", param)
  36. }
  37. if value < 0 {
  38. return 0, nil
  39. }
  40. return int64(value), nil
  41. }
  42. // Param returns an URL route parameter as string.
  43. func Param(r *http.Request, param, defaultValue string) string {
  44. vars := mux.Vars(r)
  45. value := vars[param]
  46. if value == "" {
  47. value = defaultValue
  48. }
  49. return value
  50. }
  51. // QueryParam returns a querystring parameter as string.
  52. func QueryParam(r *http.Request, param, defaultValue string) string {
  53. value := r.URL.Query().Get(param)
  54. if value == "" {
  55. value = defaultValue
  56. }
  57. return value
  58. }
  59. // QueryIntParam returns a querystring parameter as integer.
  60. func QueryIntParam(r *http.Request, param string, defaultValue int) int {
  61. return int(QueryInt64Param(r, param, int64(defaultValue)))
  62. }
  63. // QueryInt64Param returns a querystring parameter as int64.
  64. func QueryInt64Param(r *http.Request, param string, defaultValue int64) int64 {
  65. value := r.URL.Query().Get(param)
  66. if value == "" {
  67. return defaultValue
  68. }
  69. val, err := strconv.ParseInt(value, 10, 64)
  70. if err != nil {
  71. return defaultValue
  72. }
  73. if val < 0 {
  74. return defaultValue
  75. }
  76. return val
  77. }
  78. // HasQueryParam checks if the query string contains the given parameter.
  79. func HasQueryParam(r *http.Request, param string) bool {
  80. values := r.URL.Query()
  81. _, ok := values[param]
  82. return ok
  83. }
  84. // RealIP returns client's real IP address.
  85. func RealIP(r *http.Request) string {
  86. headers := []string{"X-Forwarded-For", "X-Real-Ip"}
  87. for _, header := range headers {
  88. value := r.Header.Get(header)
  89. if value != "" {
  90. addresses := strings.Split(value, ",")
  91. address := strings.TrimSpace(addresses[0])
  92. if net.ParseIP(address) != nil {
  93. return address
  94. }
  95. }
  96. }
  97. // Fallback to TCP/IP source IP address.
  98. var remoteIP string
  99. if strings.ContainsRune(r.RemoteAddr, ':') {
  100. remoteIP, _, _ = net.SplitHostPort(r.RemoteAddr)
  101. } else {
  102. remoteIP = r.RemoteAddr
  103. }
  104. return remoteIP
  105. }