From c68d8452b4f4ec13aaadf295d514ff54f48fd609 Mon Sep 17 00:00:00 2001 From: Jesse Szwedko Date: Mon, 27 May 2019 11:33:20 +0200 Subject: [PATCH] Assign new ResponseWriter after calling http.HandlerFunc Otherwise, the `http.ResponseWriter` passed to `next()` within the middleware is unused. This precludes middlewares from wrapping the http.ResponseWriter to do things like record the status code. --- context.go | 7 +++++++ echo.go | 1 + 2 files changed, 8 insertions(+) diff --git a/context.go b/context.go index 065f5815a..e0f4cc00f 100644 --- a/context.go +++ b/context.go @@ -26,6 +26,9 @@ type ( // SetRequest sets `*http.Request`. SetRequest(r *http.Request) + // SetResponse sets `*Response`. + SetResponse(r *Response) + // Response returns `*Response`. Response() *Response @@ -228,6 +231,10 @@ func (c *context) Response() *Response { return c.response } +func (c *context) SetResponse(r *Response) { + c.response = r +} + func (c *context) IsTLS() bool { return c.request.TLS != nil } diff --git a/echo.go b/echo.go index 39c30d2e7..c318d31b2 100644 --- a/echo.go +++ b/echo.go @@ -768,6 +768,7 @@ func WrapMiddleware(m func(http.Handler) http.Handler) MiddlewareFunc { return func(c Context) (err error) { m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c.SetRequest(r) + c.SetResponse(NewResponse(w, c.Echo())) err = next(c) })).ServeHTTP(c.Response(), c.Request()) return