Skip to content

Commit

Permalink
Gzip fix labstack#437
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 1, 2016
1 parent 26dc90e commit be5148a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
33 changes: 10 additions & 23 deletions engine/standard/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type (
}

responseAdapter struct {
http.ResponseWriter
Response *Response
writer http.ResponseWriter
*Response
}
)

Expand Down Expand Up @@ -100,36 +100,23 @@ func (r *Response) CloseNotify() <-chan bool {
}

func (r *Response) reset(w http.ResponseWriter, a *responseAdapter, h engine.Header) {
// r.ResponseWriter = a
r.ResponseWriter = w
r.ResponseWriter = a
r.header = h
r.status = http.StatusOK
r.size = 0
r.committed = false
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.Response.Write(b)
}

func (r *responseAdapter) Flush() {
r.ResponseWriter.(http.Flusher).Flush()
func (a *responseAdapter) Header() http.Header {
return a.writer.Header()
}

func (r *responseAdapter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
return r.ResponseWriter.(http.Hijacker).Hijack()
}

func (r *responseAdapter) CloseNotify() <-chan bool {
return r.ResponseWriter.(http.CloseNotifier).CloseNotify()
func (a *responseAdapter) WriteHeader(code int) {
a.writer.WriteHeader(code)
}

func (r *responseAdapter) reset(rw http.ResponseWriter, rq *Response) {
r.ResponseWriter = rw
r.Response = rq
func (a *responseAdapter) reset(w http.ResponseWriter, r *Response) {
a.writer = w
a.Response = r
}
17 changes: 9 additions & 8 deletions middleware/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,27 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {

return func(next echo.Handler) echo.Handler {
return echo.HandlerFunc(func(c echo.Context) error {
c.Response().Header().Add(echo.Vary, echo.AcceptEncoding)
rs := c.Response()
rs.Header().Add(echo.Vary, echo.AcceptEncoding)
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
rw := c.Response().Writer()
rw := rs.Writer()
gw := pool.Get().(*gzip.Writer)
gw.Reset(rw)
defer func() {
if c.Response().Size() == 0 {
if rs.Size() == 0 {
// We have to reset response to it's pristine state when
// nothing is written to body or error is returned.
// See issue #424, #407.
c.Response().SetWriter(rw)
c.Response().Header().Del(echo.ContentEncoding)
rs.SetWriter(rw)
rs.Header().Del(echo.ContentEncoding)
gw.Reset(ioutil.Discard)
}
gw.Close()
pool.Put(gw)
}()
g := gzipResponseWriter{Response: c.Response(), Writer: gw}
c.Response().Header().Set(echo.ContentEncoding, scheme)
c.Response().SetWriter(g)
g := gzipResponseWriter{Response: rs, Writer: gw}
rs.Header().Set(echo.ContentEncoding, scheme)
rs.SetWriter(g)
}
return next.Handle(c)
})
Expand Down

0 comments on commit be5148a

Please sign in to comment.