Skip to content

Assign new ResponseWriter after calling http.HandlerFunc #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
jszwedko committed May 27, 2019
commit c68d8452b4f4ec13aaadf295d514ff54f48fd609
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 @@ -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
Expand Down