2016-01-29 09:46:11 +02:00
|
|
|
package standard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-05-01 05:08:06 +02:00
|
|
|
"io/ioutil"
|
2016-03-13 01:06:52 +02:00
|
|
|
"mime/multipart"
|
2016-01-29 09:46:11 +02:00
|
|
|
"net/http"
|
2016-05-01 21:38:51 +02:00
|
|
|
"strings"
|
2016-01-29 09:46:11 +02:00
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
"github.com/labstack/echo"
|
2016-01-29 09:46:11 +02:00
|
|
|
"github.com/labstack/echo/engine"
|
2016-06-01 05:59:52 +02:00
|
|
|
"github.com/labstack/echo/log"
|
2016-01-29 09:46:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-03-15 04:58:46 +02:00
|
|
|
// Request implements `engine.Request`.
|
2016-01-29 09:46:11 +02:00
|
|
|
Request struct {
|
2016-03-10 22:05:33 +02:00
|
|
|
*http.Request
|
|
|
|
header engine.Header
|
2016-05-03 01:19:35 +02:00
|
|
|
url engine.URL
|
2016-06-01 05:59:52 +02:00
|
|
|
logger log.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
const (
|
|
|
|
defaultMemory = 32 << 20 // 32 MB
|
|
|
|
)
|
|
|
|
|
2016-04-17 00:53:27 +02:00
|
|
|
// NewRequest returns `Request` instance.
|
2016-06-01 05:59:52 +02:00
|
|
|
func NewRequest(r *http.Request, l log.Logger) *Request {
|
2016-04-16 18:15:37 +02:00
|
|
|
return &Request{
|
2016-04-17 00:53:27 +02:00
|
|
|
Request: r,
|
|
|
|
url: &URL{URL: r.URL},
|
|
|
|
header: &Header{Header: r.Header},
|
|
|
|
logger: l,
|
2016-04-16 18:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 15:57:31 +02:00
|
|
|
// IsTLS implements `engine.Request#TLS` function.
|
|
|
|
func (r *Request) IsTLS() bool {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.Request.TLS != nil
|
2016-02-23 07:13:08 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Scheme implements `engine.Request#Scheme` function.
|
2016-02-23 07:26:36 +02:00
|
|
|
func (r *Request) Scheme() string {
|
2016-06-01 14:43:17 +02:00
|
|
|
return r.Request.URL.Scheme
|
2016-02-23 07:26:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Host implements `engine.Request#Host` function.
|
2016-02-09 18:12:37 +02:00
|
|
|
func (r *Request) Host() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.Request.Host
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// URL implements `engine.Request#URL` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Request) URL() engine.URL {
|
|
|
|
return r.url
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Header implements `engine.Request#URL` function.
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Request) Header() engine.Header {
|
|
|
|
return r.header
|
|
|
|
}
|
|
|
|
|
2016-05-10 04:13:02 +02:00
|
|
|
// Referer implements `engine.Request#Referer` function.
|
|
|
|
func (r *Request) Referer() string {
|
|
|
|
return r.Request.Referer()
|
|
|
|
}
|
|
|
|
|
2016-02-11 02:51:43 +02:00
|
|
|
// func Proto() string {
|
|
|
|
// return r.request.Proto()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func ProtoMajor() int {
|
|
|
|
// return r.request.ProtoMajor()
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// func ProtoMinor() int {
|
|
|
|
// return r.request.ProtoMinor()
|
|
|
|
// }
|
|
|
|
|
2016-03-28 15:57:31 +02:00
|
|
|
// ContentLength implements `engine.Request#ContentLength` function.
|
2016-05-19 17:30:20 +02:00
|
|
|
func (r *Request) ContentLength() int64 {
|
|
|
|
return r.Request.ContentLength
|
2016-03-28 15:57:31 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// UserAgent implements `engine.Request#UserAgent` function.
|
2016-03-15 04:58:46 +02:00
|
|
|
func (r *Request) UserAgent() string {
|
|
|
|
return r.Request.UserAgent()
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// RemoteAddress implements `engine.Request#RemoteAddress` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Request) RemoteAddress() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.RemoteAddr
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Method implements `engine.Request#Method` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Request) Method() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.Request.Method
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// SetMethod implements `engine.Request#SetMethod` function.
|
2016-03-15 07:45:29 +02:00
|
|
|
func (r *Request) SetMethod(method string) {
|
|
|
|
r.Request.Method = method
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// URI implements `engine.Request#URI` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Request) URI() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.RequestURI
|
2016-04-13 07:39:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetURI implements `engine.Request#SetURI` function.
|
|
|
|
func (r *Request) SetURI(uri string) {
|
|
|
|
r.RequestURI = uri
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Body implements `engine.Request#Body` function.
|
2016-03-17 06:24:04 +02:00
|
|
|
func (r *Request) Body() io.Reader {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.Request.Body
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 05:08:06 +02:00
|
|
|
// SetBody implements `engine.Request#SetBody` function.
|
|
|
|
func (r *Request) SetBody(reader io.Reader) {
|
|
|
|
r.Request.Body = ioutil.NopCloser(reader)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// FormValue implements `engine.Request#FormValue` function.
|
2016-01-29 09:46:11 +02:00
|
|
|
func (r *Request) FormValue(name string) string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.Request.FormValue(name)
|
2016-02-09 18:12:37 +02:00
|
|
|
}
|
|
|
|
|
2016-03-23 18:10:22 +02:00
|
|
|
// FormParams implements `engine.Request#FormParams` function.
|
|
|
|
func (r *Request) FormParams() map[string][]string {
|
2016-05-01 21:38:51 +02:00
|
|
|
if strings.HasPrefix(r.header.Get(echo.HeaderContentType), echo.MIMEMultipartForm) {
|
|
|
|
if err := r.ParseMultipartForm(defaultMemory); err != nil {
|
|
|
|
r.logger.Error(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
r.logger.Error(err)
|
|
|
|
}
|
2016-03-23 18:10:22 +02:00
|
|
|
}
|
2016-05-01 21:38:51 +02:00
|
|
|
return map[string][]string(r.Request.Form)
|
2016-03-23 18:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// FormFile implements `engine.Request#FormFile` function.
|
2016-03-13 01:06:52 +02:00
|
|
|
func (r *Request) FormFile(name string) (*multipart.FileHeader, error) {
|
|
|
|
_, fh, err := r.Request.FormFile(name)
|
|
|
|
return fh, err
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// MultipartForm implements `engine.Request#MultipartForm` function.
|
2016-03-13 01:06:52 +02:00
|
|
|
func (r *Request) MultipartForm() (*multipart.Form, error) {
|
2016-05-01 21:38:51 +02:00
|
|
|
err := r.ParseMultipartForm(defaultMemory)
|
2016-03-18 10:42:10 +02:00
|
|
|
return r.Request.MultipartForm, err
|
2016-03-13 01:06:52 +02:00
|
|
|
}
|
|
|
|
|
2016-05-03 01:19:35 +02:00
|
|
|
// Cookie implements `engine.Request#Cookie` function.
|
2016-05-03 07:41:07 +02:00
|
|
|
func (r *Request) Cookie(name string) (engine.Cookie, error) {
|
|
|
|
c, err := r.Request.Cookie(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, echo.ErrCookieNotFound
|
|
|
|
}
|
|
|
|
return &Cookie{c}, nil
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cookies implements `engine.Request#Cookies` function.
|
|
|
|
func (r *Request) Cookies() []engine.Cookie {
|
|
|
|
cs := r.Request.Cookies()
|
|
|
|
cookies := make([]engine.Cookie, len(cs))
|
|
|
|
for i, c := range cs {
|
|
|
|
cookies[i] = &Cookie{c}
|
|
|
|
}
|
|
|
|
return cookies
|
|
|
|
}
|
|
|
|
|
2016-04-24 19:21:23 +02:00
|
|
|
func (r *Request) reset(req *http.Request, h engine.Header, u engine.URL) {
|
|
|
|
r.Request = req
|
2016-02-05 00:40:08 +02:00
|
|
|
r.header = h
|
|
|
|
r.url = u
|
|
|
|
}
|