mirror of
https://github.com/labstack/echo.git
synced 2025-04-02 22:05:34 +02:00
parent
16c6f478b1
commit
d3e33ec4dd
@ -149,8 +149,12 @@ func (s *Server) ServeHTTP(c *fasthttp.RequestCtx) {
|
|||||||
// 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().(*Request).RequestCtx
|
rq := c.Request().(*Request)
|
||||||
|
rs := c.Response().(*Response)
|
||||||
|
ctx := rq.RequestCtx
|
||||||
h(ctx)
|
h(ctx)
|
||||||
|
rs.status = ctx.Response.StatusCode()
|
||||||
|
rs.size = int64(ctx.Response.Header.ContentLength())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,8 +163,12 @@ 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().(*Request).RequestCtx
|
rq := c.Request().(*Request)
|
||||||
|
rs := c.Response().(*Response)
|
||||||
|
ctx := rq.RequestCtx
|
||||||
h(ctx)
|
h(ctx)
|
||||||
|
rs.status = ctx.Response.StatusCode()
|
||||||
|
rs.size = int64(ctx.Response.Header.ContentLength())
|
||||||
return next.Handle(c)
|
return next.Handle(c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ type (
|
|||||||
|
|
||||||
responseAdapter struct {
|
responseAdapter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
writer io.Writer
|
Response *Response
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -108,8 +108,12 @@ func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
|
|||||||
r.writer = w
|
r.writer = w
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *responseAdapter) WriteHeader(code int) {
|
||||||
|
r.Response.WriteHeader(code)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *responseAdapter) Write(b []byte) (n int, err error) {
|
func (r *responseAdapter) Write(b []byte) (n int, err error) {
|
||||||
return r.writer.Write(b)
|
return r.Response.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *responseAdapter) Flush() {
|
func (r *responseAdapter) Flush() {
|
||||||
|
@ -138,12 +138,13 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// 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 {
|
||||||
|
rq := c.Request().(*Request)
|
||||||
|
rs := c.Response().(*Response)
|
||||||
w := &responseAdapter{
|
w := &responseAdapter{
|
||||||
ResponseWriter: c.Response().(*Response).ResponseWriter,
|
ResponseWriter: rs.ResponseWriter,
|
||||||
writer: c.Response(),
|
Response: rs,
|
||||||
}
|
}
|
||||||
r := c.Request().(*Request).Request
|
h.ServeHTTP(w, rq.Request)
|
||||||
h.ServeHTTP(w, r)
|
|
||||||
return nil
|
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) {
|
return echo.HandlerFunc(func(c echo.Context) (err error) {
|
||||||
rq := c.Request().(*Request)
|
rq := c.Request().(*Request)
|
||||||
rs := c.Response().(*Response)
|
rs := c.Response().(*Response)
|
||||||
|
w := &responseAdapter{
|
||||||
|
ResponseWriter: rs.ResponseWriter,
|
||||||
|
Response: rs,
|
||||||
|
}
|
||||||
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
rs.ResponseWriter = &responseAdapter{
|
|
||||||
ResponseWriter: rs.ResponseWriter,
|
|
||||||
writer: c.Response(),
|
|
||||||
}
|
|
||||||
rq.Request = r
|
|
||||||
err = next.Handle(c)
|
err = next.Handle(c)
|
||||||
})).ServeHTTP(rs.ResponseWriter, rq.Request)
|
})).ServeHTTP(w, rq.Request)
|
||||||
return
|
return
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user