mirror of
https://github.com/labstack/echo.git
synced 2025-11-29 22:48:07 +02:00
@@ -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)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user