1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-04-01 15:44:09 -07:00
parent 26dc90edfd
commit be5148ae27
2 changed files with 19 additions and 31 deletions

View File

@ -23,8 +23,8 @@ type (
}
responseAdapter struct {
http.ResponseWriter
Response *Response
writer http.ResponseWriter
*Response
}
)
@ -100,8 +100,7 @@ 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
@ -109,27 +108,15 @@ func (r *Response) reset(w http.ResponseWriter, a *responseAdapter, h engine.Hea
r.writer = w
}
func (r *responseAdapter) WriteHeader(code int) {
r.Response.WriteHeader(code)
func (a *responseAdapter) Header() http.Header {
return a.writer.Header()
}
func (r *responseAdapter) Write(b []byte) (n int, err error) {
return r.Response.Write(b)
func (a *responseAdapter) WriteHeader(code int) {
a.writer.WriteHeader(code)
}
func (r *responseAdapter) Flush() {
r.ResponseWriter.(http.Flusher).Flush()
}
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 (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
}

View File

@ -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)
})