1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-03-27 15:21:56 -07:00
parent 16c6f478b1
commit d3e33ec4dd
3 changed files with 26 additions and 14 deletions

View File

@ -149,8 +149,12 @@ func (s *Server) ServeHTTP(c *fasthttp.RequestCtx) {
// WrapHandler wraps `fasthttp.RequestHandler` into `echo.HandlerFunc`.
func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := c.Request().(*Request).RequestCtx
rq := c.Request().(*Request)
rs := c.Response().(*Response)
ctx := rq.RequestCtx
h(ctx)
rs.status = ctx.Response.StatusCode()
rs.size = int64(ctx.Response.Header.ContentLength())
return nil
}
}
@ -159,8 +163,12 @@ func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
ctx := c.Request().(*Request).RequestCtx
rq := c.Request().(*Request)
rs := c.Response().(*Response)
ctx := rq.RequestCtx
h(ctx)
rs.status = ctx.Response.StatusCode()
rs.size = int64(ctx.Response.Header.ContentLength())
return next.Handle(c)
})
}

View File

@ -24,7 +24,7 @@ type (
responseAdapter struct {
http.ResponseWriter
writer io.Writer
Response *Response
}
)
@ -108,8 +108,12 @@ func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
r.writer = w
}
func (r *responseAdapter) WriteHeader(code int) {
r.Response.WriteHeader(code)
}
func (r *responseAdapter) Write(b []byte) (n int, err error) {
return r.writer.Write(b)
return r.Response.Write(b)
}
func (r *responseAdapter) Flush() {

View File

@ -138,12 +138,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// WrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
func WrapHandler(h http.Handler) echo.HandlerFunc {
return func(c echo.Context) error {
rq := c.Request().(*Request)
rs := c.Response().(*Response)
w := &responseAdapter{
ResponseWriter: c.Response().(*Response).ResponseWriter,
writer: c.Response(),
ResponseWriter: rs.ResponseWriter,
Response: rs,
}
r := c.Request().(*Request).Request
h.ServeHTTP(w, r)
h.ServeHTTP(w, rq.Request)
return nil
}
}
@ -154,14 +155,13 @@ func WrapMiddleware(m func(http.Handler) http.Handler) echo.MiddlewareFunc {
return echo.HandlerFunc(func(c echo.Context) (err error) {
rq := c.Request().(*Request)
rs := c.Response().(*Response)
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rs.ResponseWriter = &responseAdapter{
w := &responseAdapter{
ResponseWriter: rs.ResponseWriter,
writer: c.Response(),
Response: rs,
}
rq.Request = r
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err = next.Handle(c)
})).ServeHTTP(rs.ResponseWriter, rq.Request)
})).ServeHTTP(w, rq.Request)
return
})
}