mirror of
https://github.com/labstack/echo.git
synced 2025-04-25 12:24:55 +02:00
Dropped Engine**#Object functions
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
38d010090f
commit
8ee864ae08
@ -26,6 +26,7 @@ type (
|
|||||||
Context interface {
|
Context interface {
|
||||||
netContext.Context
|
netContext.Context
|
||||||
Request() engine.Request
|
Request() engine.Request
|
||||||
|
SetResponse(engine.Response)
|
||||||
Response() engine.Response
|
Response() engine.Response
|
||||||
Path() string
|
Path() string
|
||||||
P(int) string
|
P(int) string
|
||||||
@ -110,7 +111,12 @@ func (c *context) Request() engine.Request {
|
|||||||
return c.request
|
return c.request
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response returns *Response.
|
// SetResponse sets `engine.Response`.
|
||||||
|
func (c *context) SetResponse(r engine.Response) {
|
||||||
|
c.response = r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response returns `engine.Response`.
|
||||||
func (c *context) Response() engine.Response {
|
func (c *context) Response() engine.Response {
|
||||||
return c.response
|
return c.response
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ type (
|
|||||||
Method() string
|
Method() string
|
||||||
Body() io.ReadCloser
|
Body() io.ReadCloser
|
||||||
FormValue(string) string
|
FormValue(string) string
|
||||||
Object() interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Response defines an interface for HTTP response.
|
// Response defines an interface for HTTP response.
|
||||||
@ -41,9 +40,8 @@ type (
|
|||||||
Status() int
|
Status() int
|
||||||
Size() int64
|
Size() int64
|
||||||
Committed() bool
|
Committed() bool
|
||||||
SetWriter(io.Writer)
|
// SetWriter(io.Writer)
|
||||||
Writer() io.Writer
|
// Writer() io.Writer
|
||||||
Object() interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Header defines an interface for HTTP header.
|
// Header defines an interface for HTTP header.
|
||||||
@ -52,7 +50,6 @@ type (
|
|||||||
Del(string)
|
Del(string)
|
||||||
Get(string) string
|
Get(string) string
|
||||||
Set(string, string)
|
Set(string, string)
|
||||||
Object() interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL defines an interface for HTTP request url.
|
// URL defines an interface for HTTP request url.
|
||||||
@ -60,7 +57,6 @@ type (
|
|||||||
SetPath(string)
|
SetPath(string)
|
||||||
Path() string
|
Path() string
|
||||||
QueryValue(string) string
|
QueryValue(string) string
|
||||||
Object() interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config defines engine configuration.
|
// Config defines engine configuration.
|
||||||
|
@ -6,11 +6,11 @@ import "github.com/valyala/fasthttp"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
RequestHeader struct {
|
RequestHeader struct {
|
||||||
header fasthttp.RequestHeader
|
fasthttp.RequestHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
ResponseHeader struct {
|
ResponseHeader struct {
|
||||||
header fasthttp.ResponseHeader
|
fasthttp.ResponseHeader
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,23 +19,19 @@ func (h *RequestHeader) Add(key, val string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) Del(key string) {
|
func (h *RequestHeader) Del(key string) {
|
||||||
h.header.Del(key)
|
h.RequestHeader.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) Get(key string) string {
|
func (h *RequestHeader) Get(key string) string {
|
||||||
return string(h.header.Peek(key))
|
return string(h.Peek(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) Set(key, val string) {
|
func (h *RequestHeader) Set(key, val string) {
|
||||||
h.header.Set(key, val)
|
h.RequestHeader.Set(key, val)
|
||||||
}
|
|
||||||
|
|
||||||
func (h *RequestHeader) Object() interface{} {
|
|
||||||
return h.header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *RequestHeader) reset(hdr fasthttp.RequestHeader) {
|
func (h *RequestHeader) reset(hdr fasthttp.RequestHeader) {
|
||||||
h.header = hdr
|
h.RequestHeader = hdr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ResponseHeader) Add(key, val string) {
|
func (h *ResponseHeader) Add(key, val string) {
|
||||||
@ -43,21 +39,17 @@ func (h *ResponseHeader) Add(key, val string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *ResponseHeader) Del(key string) {
|
func (h *ResponseHeader) Del(key string) {
|
||||||
h.header.Del(key)
|
h.ResponseHeader.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ResponseHeader) Get(key string) string {
|
func (h *ResponseHeader) Get(key string) string {
|
||||||
return string(h.header.Peek(key))
|
return string(h.Peek(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ResponseHeader) Set(key, val string) {
|
func (h *ResponseHeader) Set(key, val string) {
|
||||||
h.header.Set(key, val)
|
h.ResponseHeader.Set(key, val)
|
||||||
}
|
|
||||||
|
|
||||||
func (h *ResponseHeader) Object() interface{} {
|
|
||||||
return h.header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *ResponseHeader) reset(hdr fasthttp.ResponseHeader) {
|
func (h *ResponseHeader) reset(hdr fasthttp.ResponseHeader) {
|
||||||
h.header = hdr
|
h.ResponseHeader = hdr
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
context *fasthttp.RequestCtx
|
*fasthttp.RequestCtx
|
||||||
url engine.URL
|
url engine.URL
|
||||||
header engine.Header
|
header engine.Header
|
||||||
}
|
}
|
||||||
@ -23,26 +23,26 @@ type (
|
|||||||
|
|
||||||
func NewRequest(c *fasthttp.RequestCtx) *Request {
|
func NewRequest(c *fasthttp.RequestCtx) *Request {
|
||||||
return &Request{
|
return &Request{
|
||||||
context: c,
|
RequestCtx: c,
|
||||||
url: &URL{url: c.URI()},
|
url: &URL{URI: c.URI()},
|
||||||
header: &RequestHeader{c.Request.Header},
|
header: &RequestHeader{c.Request.Header},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) TLS() bool {
|
func (r *Request) TLS() bool {
|
||||||
return r.context.IsTLS()
|
return r.IsTLS()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Scheme() string {
|
func (r *Request) Scheme() string {
|
||||||
return string(r.context.URI().Scheme())
|
return string(r.RequestCtx.URI().Scheme())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Host() string {
|
func (r *Request) Host() string {
|
||||||
return string(r.context.Host())
|
return string(r.RequestCtx.Host())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) URI() string {
|
func (r *Request) URI() string {
|
||||||
return string(r.context.RequestURI())
|
return string(r.RequestURI())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) URL() engine.URL {
|
func (r *Request) URL() engine.URL {
|
||||||
@ -54,27 +54,23 @@ func (r *Request) Header() engine.Header {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.context.RemoteAddr().String()
|
return r.RemoteAddr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Method() string {
|
func (r *Request) Method() string {
|
||||||
return string(r.context.Method())
|
return string(r.RequestCtx.Method())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Body() io.ReadCloser {
|
func (r *Request) Body() io.ReadCloser {
|
||||||
return ioutil.NopCloser(bytes.NewBuffer(r.context.PostBody()))
|
return ioutil.NopCloser(bytes.NewBuffer(r.PostBody()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) FormValue(name string) string {
|
func (r *Request) FormValue(name string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Object() interface{} {
|
|
||||||
return r.context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
|
func (r *Request) reset(c *fasthttp.RequestCtx, h engine.Header, u engine.URL) {
|
||||||
r.context = c
|
r.RequestCtx = c
|
||||||
r.header = h
|
r.header = h
|
||||||
r.url = u
|
r.url = u
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Response struct {
|
Response struct {
|
||||||
context *fasthttp.RequestCtx
|
*fasthttp.RequestCtx
|
||||||
header engine.Header
|
header engine.Header
|
||||||
status int
|
status int
|
||||||
size int64
|
size int64
|
||||||
@ -25,17 +25,13 @@ type (
|
|||||||
|
|
||||||
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
||||||
return &Response{
|
return &Response{
|
||||||
context: c,
|
RequestCtx: c,
|
||||||
header: &ResponseHeader{c.Response.Header},
|
header: &ResponseHeader{c.Response.Header},
|
||||||
writer: c,
|
writer: c,
|
||||||
logger: log.New("test"),
|
logger: log.New("test"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Object() interface{} {
|
|
||||||
return r.context
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) Header() engine.Header {
|
func (r *Response) Header() engine.Header {
|
||||||
return r.header
|
return r.header
|
||||||
}
|
}
|
||||||
@ -46,12 +42,12 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.status = code
|
r.status = code
|
||||||
r.context.SetStatusCode(code)
|
r.SetStatusCode(code)
|
||||||
r.committed = true
|
r.committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Write(b []byte) (int, error) {
|
func (r *Response) Write(b []byte) (int, error) {
|
||||||
return r.context.Write(b)
|
return r.RequestCtx.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Status() int {
|
func (r *Response) Status() int {
|
||||||
@ -66,16 +62,16 @@ func (r *Response) Committed() bool {
|
|||||||
return r.committed
|
return r.committed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) SetWriter(w io.Writer) {
|
// func (r *Response) SetWriter(w io.Writer) {
|
||||||
r.writer = w
|
// r.writer = w
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
func (r *Response) Writer() io.Writer {
|
// func (r *Response) Writer() io.Writer {
|
||||||
return r.writer
|
// return r.writer
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
func (r *Response) reset(c *fasthttp.RequestCtx, h engine.Header) {
|
||||||
r.context = c
|
r.RequestCtx = c
|
||||||
r.header = h
|
r.header = h
|
||||||
r.status = http.StatusOK
|
r.status = http.StatusOK
|
||||||
r.size = 0
|
r.size = 0
|
||||||
|
@ -126,7 +126,7 @@ func (s *Server) Start() {
|
|||||||
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
|
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
|
||||||
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
ctx := c.Request().Object().(*fasthttp.RequestCtx)
|
ctx := c.Request().(*Request).RequestCtx
|
||||||
h(ctx)
|
h(ctx)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -136,7 +136,7 @@ func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
|
|||||||
func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
|
func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
ctx := c.Request().Object().(*fasthttp.RequestCtx)
|
ctx := c.Request().(*Request).RequestCtx
|
||||||
h(ctx)
|
h(ctx)
|
||||||
return next.Handle(c)
|
return next.Handle(c)
|
||||||
})
|
})
|
||||||
|
@ -6,7 +6,7 @@ import "github.com/valyala/fasthttp"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
URL struct {
|
URL struct {
|
||||||
url *fasthttp.URI
|
*fasthttp.URI
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,17 +15,13 @@ func (u *URL) SetPath(path string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) Path() string {
|
func (u *URL) Path() string {
|
||||||
return string(u.url.Path())
|
return string(u.URI.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) QueryValue(name string) string {
|
func (u *URL) QueryValue(name string) string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) Object() interface{} {
|
func (u *URL) reset(uri *fasthttp.URI) {
|
||||||
return u.url
|
u.URI = uri
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URL) reset(url *fasthttp.URI) {
|
|
||||||
u.url = url
|
|
||||||
}
|
}
|
||||||
|
@ -4,30 +4,26 @@ import "net/http"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Header struct {
|
Header struct {
|
||||||
header http.Header
|
http.Header
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *Header) Add(key, val string) {
|
func (h *Header) Add(key, val string) {
|
||||||
h.header.Add(key, val)
|
h.Header.Add(key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Header) Del(key string) {
|
func (h *Header) Del(key string) {
|
||||||
h.header.Del(key)
|
h.Header.Del(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Header) Get(key string) string {
|
func (h *Header) Get(key string) string {
|
||||||
return h.header.Get(key)
|
return h.Header.Get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Header) Set(key, val string) {
|
func (h *Header) Set(key, val string) {
|
||||||
h.header.Set(key, val)
|
h.Header.Set(key, val)
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Header) Object() interface{} {
|
|
||||||
return h.header
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Header) reset(hdr http.Header) {
|
func (h *Header) reset(hdr http.Header) {
|
||||||
h.header = hdr
|
h.Header = hdr
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Request struct {
|
Request struct {
|
||||||
request *http.Request
|
*http.Request
|
||||||
url engine.URL
|
url engine.URL
|
||||||
header engine.Header
|
header engine.Header
|
||||||
}
|
}
|
||||||
@ -17,14 +17,14 @@ type (
|
|||||||
|
|
||||||
func NewRequest(r *http.Request) *Request {
|
func NewRequest(r *http.Request) *Request {
|
||||||
return &Request{
|
return &Request{
|
||||||
request: r,
|
Request: r,
|
||||||
url: &URL{url: r.URL},
|
url: &URL{URL: r.URL},
|
||||||
header: &Header{r.Header},
|
header: &Header{r.Header},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) TLS() bool {
|
func (r *Request) TLS() bool {
|
||||||
return r.request.TLS != nil
|
return r.Request.TLS != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Scheme() string {
|
func (r *Request) Scheme() string {
|
||||||
@ -35,7 +35,7 @@ func (r *Request) Scheme() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Host() string {
|
func (r *Request) Host() string {
|
||||||
return r.request.Host
|
return r.Request.Host
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) URL() engine.URL {
|
func (r *Request) URL() engine.URL {
|
||||||
@ -59,31 +59,27 @@ func (r *Request) Header() engine.Header {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
func (r *Request) RemoteAddress() string {
|
func (r *Request) RemoteAddress() string {
|
||||||
return r.request.RemoteAddr
|
return r.RemoteAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Method() string {
|
func (r *Request) Method() string {
|
||||||
return r.request.Method
|
return r.Request.Method
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) URI() string {
|
func (r *Request) URI() string {
|
||||||
return r.request.RequestURI
|
return r.RequestURI
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) Body() io.ReadCloser {
|
func (r *Request) Body() io.ReadCloser {
|
||||||
return r.request.Body
|
return r.Request.Body
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) FormValue(name string) string {
|
func (r *Request) FormValue(name string) string {
|
||||||
return r.request.FormValue(name)
|
return r.Request.FormValue(name)
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Request) Object() interface{} {
|
|
||||||
return r.request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Request) reset(req *http.Request, h engine.Header, u engine.URL) {
|
func (r *Request) reset(req *http.Request, h engine.Header, u engine.URL) {
|
||||||
r.request = req
|
r.Request = req
|
||||||
r.header = h
|
r.header = h
|
||||||
r.url = u
|
r.url = u
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package standard
|
package standard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/labstack/echo/engine"
|
"github.com/labstack/echo/engine"
|
||||||
@ -10,21 +9,21 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Response struct {
|
Response struct {
|
||||||
response http.ResponseWriter
|
http.ResponseWriter
|
||||||
header engine.Header
|
header engine.Header
|
||||||
status int
|
status int
|
||||||
size int64
|
size int64
|
||||||
committed bool
|
committed bool
|
||||||
writer io.Writer
|
// writer io.Writer
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewResponse(w http.ResponseWriter, l *log.Logger) *Response {
|
func NewResponse(w http.ResponseWriter, l *log.Logger) *Response {
|
||||||
return &Response{
|
return &Response{
|
||||||
response: w,
|
ResponseWriter: w,
|
||||||
header: &Header{w.Header()},
|
header: &Header{w.Header()},
|
||||||
writer: w,
|
// writer: w,
|
||||||
logger: l,
|
logger: l,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,12 +38,12 @@ func (r *Response) WriteHeader(code int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
r.status = code
|
r.status = code
|
||||||
r.response.WriteHeader(code)
|
r.ResponseWriter.WriteHeader(code)
|
||||||
r.committed = true
|
r.committed = true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) Write(b []byte) (n int, err error) {
|
func (r *Response) Write(b []byte) (n int, err error) {
|
||||||
n, err = r.writer.Write(b)
|
n, err = r.ResponseWriter.Write(b)
|
||||||
r.size += int64(n)
|
r.size += int64(n)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -61,23 +60,19 @@ func (r *Response) Committed() bool {
|
|||||||
return r.committed
|
return r.committed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) SetWriter(w io.Writer) {
|
// func (r *Response) SetWriter(w io.Writer) {
|
||||||
r.writer = w
|
// r.writer = w
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (r *Response) Writer() io.Writer {
|
// func (r *Response) Writer() io.Writer {
|
||||||
return r.writer
|
// return r.writer
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (r *Response) Object() interface{} {
|
|
||||||
return r.response
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
||||||
r.response = w
|
r.ResponseWriter = w
|
||||||
r.header = h
|
r.header = h
|
||||||
r.status = http.StatusOK
|
r.status = http.StatusOK
|
||||||
r.size = 0
|
r.size = 0
|
||||||
r.committed = false
|
r.committed = false
|
||||||
r.writer = w
|
// r.writer = w
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,8 @@ func (s *Server) Server() *http.Server {
|
|||||||
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
|
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
|
||||||
func WrapHandler(h http.Handler) echo.HandlerFunc {
|
func WrapHandler(h http.Handler) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
w := c.Response().Object().(http.ResponseWriter)
|
w := c.Response().(*Response).ResponseWriter
|
||||||
r := c.Request().Object().(*http.Request)
|
r := c.Request().(*Request).Request
|
||||||
h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -136,8 +136,8 @@ func WrapHandler(h http.Handler) echo.HandlerFunc {
|
|||||||
func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
|
func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
|
||||||
return func(next echo.Handler) echo.Handler {
|
return func(next echo.Handler) echo.Handler {
|
||||||
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||||
w := c.Response().Object().(http.ResponseWriter)
|
w := c.Response().(*Response).ResponseWriter
|
||||||
r := c.Request().Object().(*http.Request)
|
r := c.Request().(*Request).Request
|
||||||
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
err = next.Handle(c)
|
err = next.Handle(c)
|
||||||
})).ServeHTTP(w, r)
|
})).ServeHTTP(w, r)
|
||||||
|
@ -4,34 +4,26 @@ import "net/url"
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
URL struct {
|
URL struct {
|
||||||
url *url.URL
|
*url.URL
|
||||||
query url.Values
|
query url.Values
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (u *URL) URL() *url.URL {
|
|
||||||
return u.url
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URL) SetPath(path string) {
|
func (u *URL) SetPath(path string) {
|
||||||
u.url.Path = path
|
u.URL.Path = path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) Path() string {
|
func (u *URL) Path() string {
|
||||||
return u.url.Path
|
return u.URL.Path
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) QueryValue(name string) string {
|
func (u *URL) QueryValue(name string) string {
|
||||||
if u.query == nil {
|
if u.query == nil {
|
||||||
u.query = u.url.Query()
|
u.query = u.Query()
|
||||||
}
|
}
|
||||||
return u.query.Get(name)
|
return u.query.Get(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *URL) Object() interface{} {
|
|
||||||
return u.url
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *URL) reset(url *url.URL) {
|
func (u *URL) reset(url *url.URL) {
|
||||||
u.url = url
|
u.URL = url
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -19,9 +17,9 @@ type (
|
|||||||
level int
|
level int
|
||||||
}
|
}
|
||||||
|
|
||||||
gzipWriter struct {
|
gzipResponseWriter struct {
|
||||||
io.Writer
|
|
||||||
engine.Response
|
engine.Response
|
||||||
|
io.Writer
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,15 +31,15 @@ func Gzip(options ...*GzipOptions) echo.MiddlewareFunc {
|
|||||||
return echo.HandlerFunc(func(c echo.Context) error {
|
return echo.HandlerFunc(func(c echo.Context) error {
|
||||||
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
|
||||||
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
|
||||||
w := writerPool.Get().(*gzip.Writer)
|
w := pool.Get().(*gzip.Writer)
|
||||||
w.Reset(c.Response().Writer())
|
w.Reset(c.Response())
|
||||||
defer func() {
|
defer func() {
|
||||||
w.Close()
|
w.Close()
|
||||||
writerPool.Put(w)
|
pool.Put(w)
|
||||||
}()
|
}()
|
||||||
gw := gzipWriter{Writer: w, Response: c.Response()}
|
gw := gzipResponseWriter{Response: c.Response(), Writer: w}
|
||||||
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
c.Response().Header().Set(echo.ContentEncoding, scheme)
|
||||||
c.Response().SetWriter(gw)
|
c.SetResponse(gw)
|
||||||
}
|
}
|
||||||
if err := next.Handle(c); err != nil {
|
if err := next.Handle(c); err != nil {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
@ -51,26 +49,14 @@ func Gzip(options ...*GzipOptions) echo.MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w gzipWriter) Write(b []byte) (int, error) {
|
func (g gzipResponseWriter) Write(b []byte) (int, error) {
|
||||||
if w.Header().Get(echo.ContentType) == "" {
|
if g.Header().Get(echo.ContentType) == "" {
|
||||||
w.Header().Set(echo.ContentType, http.DetectContentType(b))
|
g.Header().Set(echo.ContentType, http.DetectContentType(b))
|
||||||
}
|
}
|
||||||
return w.Writer.Write(b)
|
return g.Writer.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w gzipWriter) Flush() error {
|
var pool = sync.Pool{
|
||||||
return w.Writer.(*gzip.Writer).Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w gzipWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
||||||
return w.Response.(http.Hijacker).Hijack()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *gzipWriter) CloseNotify() <-chan bool {
|
|
||||||
return w.Response.(http.CloseNotifier).CloseNotify()
|
|
||||||
}
|
|
||||||
|
|
||||||
var writerPool = sync.Pool{
|
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return gzip.NewWriter(ioutil.Discard)
|
return gzip.NewWriter(ioutil.Discard)
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user