Skip to content

Commit

Permalink
Assign new ResponseWriter after calling http.HandlerFunc (#1341)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jszwedko authored and vishr committed Aug 7, 2019
1 parent 09d415c commit 608cebb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type (
// SetRequest sets `*http.Request`.
SetRequest(r *http.Request)

// SetResponse sets `*Response`.
SetResponse(r *Response)

// Response returns `*Response`.
Response() *Response

Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,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
Expand Down

0 comments on commit 608cebb

Please sign in to comment.