mirror of
https://github.com/labstack/echo.git
synced 2025-01-16 02:33:26 +02:00
Merge branch 'freeformz-fix806'
This commit is contained in:
commit
394f533510
@ -67,14 +67,17 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
||||
res := c.Response()
|
||||
res.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
|
||||
if strings.Contains(c.Request().Header.Get(echo.HeaderAcceptEncoding), gzipScheme) {
|
||||
res.Header().Add(echo.HeaderContentEncoding, gzipScheme) // Issue #806
|
||||
rw := res.Writer
|
||||
w, err := gzip.NewWriterLevel(rw, config.Level)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
|
||||
if res.Size == 0 {
|
||||
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
|
||||
res.Header().Del(echo.HeaderContentEncoding)
|
||||
}
|
||||
// We have to reset response to it's pristine state when
|
||||
// nothing is written to body or error is returned.
|
||||
// See issue #424, #407.
|
||||
@ -92,8 +95,8 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
||||
}
|
||||
|
||||
func (w *gzipResponseWriter) WriteHeader(code int) {
|
||||
if code != http.StatusNoContent { // Issue #489
|
||||
w.ResponseWriter.Header().Set(echo.HeaderContentEncoding, gzipScheme)
|
||||
if code == http.StatusNoContent { // Issue #489
|
||||
w.ResponseWriter.Header().Del(echo.HeaderContentEncoding)
|
||||
}
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package middleware
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@ -71,3 +72,29 @@ func TestGzipErrorReturned(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNotFound, rec.Code)
|
||||
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
||||
}
|
||||
|
||||
// Issue #806
|
||||
func TestGzipWithStatic(t *testing.T) {
|
||||
e := echo.New()
|
||||
e.Use(Gzip())
|
||||
e.Static("/test", "../_fixture/images")
|
||||
req, _ := http.NewRequest(echo.GET, "/test/walle.png", nil)
|
||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
// Data is written out in chunks when Content-Length == "", so only
|
||||
// validate the content length if it's not set.
|
||||
if cl := rec.Header().Get("Content-Length"); cl != "" {
|
||||
assert.Equal(t, cl, rec.Body.Len())
|
||||
}
|
||||
r, err := gzip.NewReader(rec.Body)
|
||||
assert.NoError(t, err)
|
||||
defer r.Close()
|
||||
want, err := ioutil.ReadFile("../_fixture/images/walle.png")
|
||||
if assert.NoError(t, err) {
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
assert.Equal(t, want, buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user