request.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2017 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 core
  5. import (
  6. "fmt"
  7. "io"
  8. "mime/multipart"
  9. "net/http"
  10. "strconv"
  11. "github.com/gorilla/mux"
  12. "github.com/miniflux/miniflux/logger"
  13. )
  14. // Request is a thin wrapper around "http.Request".
  15. type Request struct {
  16. writer http.ResponseWriter
  17. request *http.Request
  18. }
  19. // Request returns the raw Request struct.
  20. func (r *Request) Request() *http.Request {
  21. return r.request
  22. }
  23. // Body returns the request body.
  24. func (r *Request) Body() io.ReadCloser {
  25. return r.request.Body
  26. }
  27. // File returns uploaded file properties.
  28. func (r *Request) File(name string) (multipart.File, *multipart.FileHeader, error) {
  29. return r.request.FormFile(name)
  30. }
  31. // IsHTTPS returns if the request is made over HTTPS.
  32. func (r *Request) IsHTTPS() bool {
  33. return r.request.URL.Scheme == "https"
  34. }
  35. // Cookie returns the cookie value.
  36. func (r *Request) Cookie(name string) string {
  37. cookie, err := r.request.Cookie(name)
  38. if err == http.ErrNoCookie {
  39. return ""
  40. }
  41. return cookie.Value
  42. }
  43. // FormValue returns a form value as integer.
  44. func (r *Request) FormValue(param string) string {
  45. return r.request.FormValue(param)
  46. }
  47. // FormIntegerValue returns a form value as integer.
  48. func (r *Request) FormIntegerValue(param string) int64 {
  49. value := r.request.FormValue(param)
  50. integer, _ := strconv.Atoi(value)
  51. return int64(integer)
  52. }
  53. // IntegerParam returns an URL parameter as integer.
  54. func (r *Request) IntegerParam(param string) (int64, error) {
  55. vars := mux.Vars(r.request)
  56. value, err := strconv.Atoi(vars[param])
  57. if err != nil {
  58. logger.Error("[IntegerParam] %v", err)
  59. return 0, fmt.Errorf("%s parameter is not an integer", param)
  60. }
  61. if value < 0 {
  62. return 0, nil
  63. }
  64. return int64(value), nil
  65. }
  66. // StringParam returns an URL parameter as string.
  67. func (r *Request) StringParam(param, defaultValue string) string {
  68. vars := mux.Vars(r.request)
  69. value := vars[param]
  70. if value == "" {
  71. value = defaultValue
  72. }
  73. return value
  74. }
  75. // QueryStringParam returns a querystring parameter as string.
  76. func (r *Request) QueryStringParam(param, defaultValue string) string {
  77. value := r.request.URL.Query().Get(param)
  78. if value == "" {
  79. value = defaultValue
  80. }
  81. return value
  82. }
  83. // QueryIntegerParam returns a querystring parameter as string.
  84. func (r *Request) QueryIntegerParam(param string, defaultValue int) int {
  85. value := r.request.URL.Query().Get(param)
  86. if value == "" {
  87. return defaultValue
  88. }
  89. val, err := strconv.Atoi(value)
  90. if err != nil {
  91. return defaultValue
  92. }
  93. if val < 0 {
  94. return defaultValue
  95. }
  96. return val
  97. }
  98. // HasQueryParam checks if the query string contains the given parameter.
  99. func (r *Request) HasQueryParam(param string) bool {
  100. values := r.request.URL.Query()
  101. _, ok := values[param]
  102. return ok
  103. }
  104. // NewRequest returns a new Request struct.
  105. func NewRequest(w http.ResponseWriter, r *http.Request) *Request {
  106. return &Request{writer: w, request: r}
  107. }