2016-02-09 21:43:00 +02:00
|
|
|
// +build !appengine
|
|
|
|
|
2016-01-29 09:46:11 +02:00
|
|
|
package fasthttp
|
|
|
|
|
2016-02-09 18:12:37 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-03-13 01:06:52 +02:00
|
|
|
"mime/multipart"
|
2016-08-16 10:45:41 +02:00
|
|
|
"net"
|
2016-01-29 09:46:11 +02:00
|
|
|
|
2016-05-03 07:41:07 +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
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
*fasthttp.RequestCtx
|
|
|
|
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-04-17 00:53:27 +02:00
|
|
|
// NewRequest returns `Request` instance.
|
2016-06-01 05:59:52 +02:00
|
|
|
func NewRequest(c *fasthttp.RequestCtx, l log.Logger) *Request {
|
2016-04-16 18:15:37 +02:00
|
|
|
return &Request{
|
2016-04-17 00:53:27 +02:00
|
|
|
RequestCtx: c,
|
|
|
|
url: &URL{URI: c.URI()},
|
|
|
|
header: &RequestHeader{RequestHeader: &c.Request.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-04-19 21:05:05 +02:00
|
|
|
return r.RequestCtx.IsTLS()
|
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-03-10 22:05:33 +02:00
|
|
|
return string(r.RequestCtx.URI().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 string(r.RequestCtx.Host())
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-09-21 23:08:18 +02:00
|
|
|
// SetHost implements `engine.Request#SetHost` function.
|
|
|
|
func (r *Request) SetHost(host string) {
|
|
|
|
r.RequestCtx.Request.SetHost(host)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// URL implements `engine.Request#URL` function.
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Request) URL() engine.URL {
|
|
|
|
return r.url
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Header implements `engine.Request#Header` function.
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Request) Header() engine.Header {
|
|
|
|
return r.header
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-10 04:13:02 +02:00
|
|
|
// Referer implements `engine.Request#Referer` function.
|
|
|
|
func (r *Request) Referer() string {
|
2016-06-07 04:23:59 +02:00
|
|
|
return string(r.Request.Header.Referer())
|
2016-05-10 04:13:02 +02:00
|
|
|
}
|
|
|
|
|
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 int64(r.Request.Header.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 string(r.RequestCtx.UserAgent())
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// RemoteAddress implements `engine.Request#RemoteAddress` function.
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Request) RemoteAddress() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return r.RemoteAddr().String()
|
2016-02-09 02:48:03 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 03:26:58 +02:00
|
|
|
// RealIP implements `engine.Request#RealIP` function.
|
|
|
|
func (r *Request) RealIP() string {
|
2016-08-16 10:45:41 +02:00
|
|
|
ra := r.RemoteAddress()
|
|
|
|
if ip := r.Header().Get(echo.HeaderXForwardedFor); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else if ip := r.Header().Get(echo.HeaderXRealIP); ip != "" {
|
|
|
|
ra = ip
|
|
|
|
} else {
|
|
|
|
ra, _, _ = net.SplitHostPort(ra)
|
|
|
|
}
|
|
|
|
return ra
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// Method implements `engine.Request#Method` function.
|
2016-02-09 02:48:03 +02:00
|
|
|
func (r *Request) Method() string {
|
2016-03-10 22:05:33 +02:00
|
|
|
return string(r.RequestCtx.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) {
|
2016-06-07 04:23:59 +02:00
|
|
|
r.Request.Header.SetMethodBytes([]byte(method))
|
2016-03-15 07:45:29 +02:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:47:20 +02:00
|
|
|
// URI implements `engine.Request#URI` function.
|
2016-03-15 04:58:46 +02:00
|
|
|
func (r *Request) URI() string {
|
|
|
|
return string(r.RequestURI())
|
|
|
|
}
|
|
|
|
|
2016-04-13 07:39:29 +02:00
|
|
|
// SetURI implements `engine.Request#SetURI` function.
|
|
|
|
func (r *Request) SetURI(uri string) {
|
|
|
|
r.Request.Header.SetRequestURI(uri)
|
|
|
|
}
|
|
|
|
|
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-05-01 06:56:35 +02:00
|
|
|
return bytes.NewBuffer(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) {
|
2016-05-01 06:56:35 +02:00
|
|
|
r.Request.SetBodyStream(reader, 0)
|
2016-05-01 05:08:06 +02:00
|
|
|
}
|
|
|
|
|
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-13 01:06:52 +02:00
|
|
|
return string(r.RequestCtx.FormValue(name))
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:10:22 +02:00
|
|
|
// FormParams implements `engine.Request#FormParams` function.
|
|
|
|
func (r *Request) FormParams() (params map[string][]string) {
|
|
|
|
params = make(map[string][]string)
|
2016-06-07 04:23:59 +02:00
|
|
|
mf, err := r.RequestCtx.MultipartForm()
|
|
|
|
|
|
|
|
if err == fasthttp.ErrNoMultipartForm {
|
|
|
|
r.PostArgs().VisitAll(func(k, v []byte) {
|
2016-06-09 19:20:02 +02:00
|
|
|
key := string(k)
|
|
|
|
if _, ok := params[key]; ok {
|
|
|
|
params[key] = append(params[key], string(v))
|
|
|
|
} else {
|
|
|
|
params[string(k)] = []string{string(v)}
|
|
|
|
}
|
2016-06-07 04:23:59 +02:00
|
|
|
})
|
|
|
|
} else if err == nil {
|
|
|
|
for k, v := range mf.Value {
|
|
|
|
if len(v) > 0 {
|
|
|
|
params[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:10:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return r.RequestCtx.FormFile(name)
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return r.RequestCtx.MultipartForm()
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
2016-02-09 18:12:37 +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) {
|
2016-05-03 01:19:35 +02:00
|
|
|
c := new(fasthttp.Cookie)
|
2016-05-03 07:41:07 +02:00
|
|
|
b := r.Request.Header.Cookie(name)
|
|
|
|
if b == nil {
|
|
|
|
return nil, echo.ErrCookieNotFound
|
|
|
|
}
|
2016-06-07 04:23:59 +02:00
|
|
|
c.SetKey(name)
|
2016-06-21 23:50:57 +02:00
|
|
|
c.SetValueBytes(b)
|
2016-05-03 07:41:07 +02:00
|
|
|
return &Cookie{c}, nil
|
2016-05-03 01:19:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cookies implements `engine.Request#Cookies` function.
|
|
|
|
func (r *Request) Cookies() []engine.Cookie {
|
2016-06-21 23:50:57 +02:00
|
|
|
cookies := []engine.Cookie{}
|
2016-05-03 01:19:35 +02:00
|
|
|
r.Request.Header.VisitAllCookie(func(name, value []byte) {
|
|
|
|
c := new(fasthttp.Cookie)
|
2016-06-21 23:50:57 +02:00
|
|
|
c.SetKeyBytes(name)
|
|
|
|
c.SetValueBytes(value)
|
2016-06-07 04:23:59 +02:00
|
|
|
cookies = append(cookies, &Cookie{c})
|
2016-05-03 01:19:35 +02:00
|
|
|
})
|
|
|
|
return cookies
|
|
|
|
}
|
|
|
|
|
2016-02-22 05:58:19 +02:00
|
|
|
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
|
2016-03-10 22:05:33 +02:00
|
|
|
r.RequestCtx = c
|
2016-02-22 05:58:19 +02:00
|
|
|
r.header = h
|
|
|
|
r.url = u
|
|
|
|
}
|