request.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // FormIntValue returns a form value as integer.
  22. func FormIntValue(r *http.Request, param string) int64 {
  23. value := r.FormValue(param)
  24. integer, _ := strconv.Atoi(value)
  25. return int64(integer)
  26. }
  27. // IntParam returns an URL route parameter as integer.
  28. func IntParam(r *http.Request, param string) (int64, error) {
  29. vars := mux.Vars(r)
  30. value, err := strconv.Atoi(vars[param])
  31. if err != nil {
  32. return 0, fmt.Errorf("request: %s parameter is not an integer", param)
  33. }
  34. if value < 0 {
  35. return 0, nil
  36. }
  37. return int64(value), nil
  38. }
  39. // Param returns an URL route parameter as string.
  40. func Param(r *http.Request, param, defaultValue string) string {
  41. vars := mux.Vars(r)
  42. value := vars[param]
  43. if value == "" {
  44. value = defaultValue
  45. }
  46. return value
  47. }
  48. // QueryParam returns a querystring parameter as string.
  49. func QueryParam(r *http.Request, param, defaultValue string) string {
  50. value := r.URL.Query().Get(param)
  51. if value == "" {
  52. value = defaultValue
  53. }
  54. return value
  55. }
  56. // QueryIntParam returns a querystring parameter as integer.
  57. func QueryIntParam(r *http.Request, param string, defaultValue int) int {
  58. value := r.URL.Query().Get(param)
  59. if value == "" {
  60. return defaultValue
  61. }
  62. val, err := strconv.Atoi(value)
  63. if err != nil {
  64. return defaultValue
  65. }
  66. if val < 0 {
  67. return defaultValue
  68. }
  69. return val
  70. }
  71. // HasQueryParam checks if the query string contains the given parameter.
  72. func HasQueryParam(r *http.Request, param string) bool {
  73. values := r.URL.Query()
  74. _, ok := values[param]
  75. return ok
  76. }
  77. // RealIP returns client's real IP address.
  78. func RealIP(r *http.Request) string {
  79. headers := []string{"X-Forwarded-For", "X-Real-Ip"}
  80. for _, header := range headers {
  81. value := r.Header.Get(header)
  82. if value != "" {
  83. addresses := strings.Split(value, ",")
  84. address := strings.TrimSpace(addresses[0])
  85. if net.ParseIP(address) != nil {
  86. return address
  87. }
  88. }
  89. }
  90. // Fallback to TCP/IP source IP address.
  91. var remoteIP string
  92. if strings.ContainsRune(r.RemoteAddr, ':') {
  93. remoteIP, _, _ = net.SplitHostPort(r.RemoteAddr)
  94. } else {
  95. remoteIP = r.RemoteAddr
  96. }
  97. return remoteIP
  98. }