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-01-29 09:46:11 +02:00
|
|
|
|
|
|
|
"github.com/labstack/echo/engine"
|
2016-04-16 18:15:37 +02:00
|
|
|
"github.com/labstack/gommon/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
|
|
|
|
url engine.URL
|
|
|
|
header engine.Header
|
2016-04-16 18:15:37 +02:00
|
|
|
logger *log.Logger
|
2016-01-29 09:46:11 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-04-16 18:15:37 +02:00
|
|
|
// MockRequest returns `Request` instance for testing purpose.
|
|
|
|
func MockRequest() *Request {
|
|
|
|
ctx := new(fasthttp.RequestCtx)
|
|
|
|
return &Request{
|
|
|
|
RequestCtx: ctx,
|
|
|
|
url: &URL{URI: ctx.URI()},
|
|
|
|
header: &RequestHeader{RequestHeader: &ctx.Request.Header},
|
|
|
|
logger: log.New("test"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.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-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-03-28 15:57:31 +02:00
|
|
|
// ContentLength implements `engine.Request#ContentLength` function.
|
|
|
|
func (r *Request) ContentLength() int {
|
|
|
|
return r.Request.Header.ContentLength()
|
|
|
|
}
|
|
|
|
|
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-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) {
|
|
|
|
r.Request.Header.SetMethod(method)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return bytes.NewBuffer(r.PostBody())
|
2016-01-29 09:46:11 +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)
|
|
|
|
r.PostArgs().VisitAll(func(k, v []byte) {
|
|
|
|
// TODO: Filling with only first value
|
|
|
|
params[string(k)] = []string{string(v)}
|
|
|
|
})
|
|
|
|
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-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
|
|
|
|
}
|