miniflux/server/core/request.go

131 lines
3 KiB
Go
Raw Normal View History

2017-11-20 06:10:04 +01:00
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package core
import (
"fmt"
"io"
"mime/multipart"
"net/http"
"strconv"
"github.com/gorilla/mux"
2017-12-16 03:55:57 +01:00
"github.com/miniflux/miniflux/logger"
2017-11-20 06:10:04 +01:00
)
2017-11-22 03:14:45 +01:00
// Request is a thin wrapper around "http.Request".
2017-11-20 06:10:04 +01:00
type Request struct {
writer http.ResponseWriter
request *http.Request
}
2017-11-22 03:14:45 +01:00
// Request returns the raw Request struct.
func (r *Request) Request() *http.Request {
2017-11-20 06:10:04 +01:00
return r.request
}
2017-11-22 03:14:45 +01:00
// Body returns the request body.
func (r *Request) Body() io.ReadCloser {
2017-11-20 06:10:04 +01:00
return r.request.Body
}
2017-11-22 03:14:45 +01:00
// File returns uploaded file properties.
func (r *Request) File(name string) (multipart.File, *multipart.FileHeader, error) {
2017-11-20 06:10:04 +01:00
return r.request.FormFile(name)
}
2017-11-22 03:14:45 +01:00
// IsHTTPS returns if the request is made over HTTPS.
2017-11-20 06:10:04 +01:00
func (r *Request) IsHTTPS() bool {
return r.request.URL.Scheme == "https"
}
2017-11-22 03:14:45 +01:00
// Cookie returns the cookie value.
func (r *Request) Cookie(name string) string {
2017-11-20 06:10:04 +01:00
cookie, err := r.request.Cookie(name)
if err == http.ErrNoCookie {
return ""
}
return cookie.Value
}
2017-12-04 02:44:27 +01:00
// FormValue returns a form value as integer.
func (r *Request) FormValue(param string) string {
return r.request.FormValue(param)
}
// FormIntegerValue returns a form value as integer.
func (r *Request) FormIntegerValue(param string) int64 {
value := r.request.FormValue(param)
integer, _ := strconv.Atoi(value)
return int64(integer)
}
2017-11-22 03:14:45 +01:00
// IntegerParam returns an URL parameter as integer.
func (r *Request) IntegerParam(param string) (int64, error) {
2017-11-20 06:10:04 +01:00
vars := mux.Vars(r.request)
value, err := strconv.Atoi(vars[param])
if err != nil {
2017-12-16 03:55:57 +01:00
logger.Error("[IntegerParam] %v", err)
2017-11-20 06:10:04 +01:00
return 0, fmt.Errorf("%s parameter is not an integer", param)
}
if value < 0 {
return 0, nil
}
return int64(value), nil
}
2017-11-22 03:14:45 +01:00
// StringParam returns an URL parameter as string.
func (r *Request) StringParam(param, defaultValue string) string {
2017-11-20 06:10:04 +01:00
vars := mux.Vars(r.request)
value := vars[param]
if value == "" {
value = defaultValue
}
return value
}
2017-11-22 03:14:45 +01:00
// QueryStringParam returns a querystring parameter as string.
func (r *Request) QueryStringParam(param, defaultValue string) string {
2017-11-20 06:10:04 +01:00
value := r.request.URL.Query().Get(param)
if value == "" {
value = defaultValue
}
return value
}
2017-11-22 03:14:45 +01:00
// QueryIntegerParam returns a querystring parameter as string.
func (r *Request) QueryIntegerParam(param string, defaultValue int) int {
2017-11-20 06:10:04 +01:00
value := r.request.URL.Query().Get(param)
if value == "" {
return defaultValue
}
val, err := strconv.Atoi(value)
if err != nil {
return defaultValue
}
if val < 0 {
return defaultValue
}
return val
}
2017-12-04 02:44:27 +01:00
// HasQueryParam checks if the query string contains the given parameter.
func (r *Request) HasQueryParam(param string) bool {
values := r.request.URL.Query()
_, ok := values[param]
return ok
}
2017-11-22 03:14:45 +01:00
// NewRequest returns a new Request struct.
2017-11-20 06:10:04 +01:00
func NewRequest(w http.ResponseWriter, r *http.Request) *Request {
return &Request{writer: w, request: r}
}