1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

fix gzip not sending response code for no content responses (404, 301/302 redirects etc)

This commit is contained in:
toimtoimtoim 2023-07-16 20:15:24 +03:00 committed by Martti T
parent 60af056959
commit 130be07425
2 changed files with 38 additions and 20 deletions

View File

@ -107,10 +107,16 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength, buffer: buf}
defer func() {
// There are different reasons for cases when we have not yet written response to the client and now need to do so.
// a) handler response had only response code and no response body (ala 404 or redirects etc). Response code need to be written now.
// b) body is shorter than our minimum length threshold and being buffered currently and needs to be written
if !grw.wroteBody {
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
res.Header().Del(echo.HeaderContentEncoding)
}
if grw.wroteHeader {
rw.WriteHeader(grw.code)
}
// We have to reset response to it's pristine state when
// nothing is written to body or error is returned.
// See issue #424, #407.

View File

@ -89,8 +89,6 @@ func TestGzip(t *testing.T) {
}
func TestGzipWithMinLength(t *testing.T) {
assert := assert.New(t)
e := echo.New()
// Minimal response length
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
@ -103,19 +101,17 @@ func TestGzipWithMinLength(t *testing.T) {
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
r, err := gzip.NewReader(rec.Body)
if assert.NoError(err) {
if assert.NoError(t, err) {
buf := new(bytes.Buffer)
defer r.Close()
buf.ReadFrom(r)
assert.Equal("foobarfoobar", buf.String())
assert.Equal(t, "foobarfoobar", buf.String())
}
}
func TestGzipWithMinLengthTooShort(t *testing.T) {
assert := assert.New(t)
e := echo.New()
// Minimal response length
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
@ -127,13 +123,29 @@ func TestGzipWithMinLengthTooShort(t *testing.T) {
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal("", rec.Header().Get(echo.HeaderContentEncoding))
assert.Contains(rec.Body.String(), "test")
assert.Equal(t, "", rec.Header().Get(echo.HeaderContentEncoding))
assert.Contains(t, rec.Body.String(), "test")
}
func TestGzipWithResponseWithoutBody(t *testing.T) {
e := echo.New()
e.Use(Gzip())
e.GET("/", func(c echo.Context) error {
return c.Redirect(http.StatusMovedPermanently, "http://localhost")
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
assert.Equal(t, "", rec.Header().Get(echo.HeaderContentEncoding))
}
func TestGzipWithMinLengthChunked(t *testing.T) {
assert := assert.New(t)
e := echo.New()
// Gzip chunked
@ -155,36 +167,36 @@ func TestGzipWithMinLengthChunked(t *testing.T) {
c.Response().Flush()
// Read the first part of the data
assert.True(rec.Flushed)
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
assert.True(t, rec.Flushed)
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
var err error
r, err = gzip.NewReader(rec.Body)
assert.NoError(err)
assert.NoError(t, err)
_, err = io.ReadFull(r, chunkBuf)
assert.NoError(err)
assert.Equal("test\n", string(chunkBuf))
assert.NoError(t, err)
assert.Equal(t, "test\n", string(chunkBuf))
// Write and flush the second part of the data
c.Response().Write([]byte("test\n"))
c.Response().Flush()
_, err = io.ReadFull(r, chunkBuf)
assert.NoError(err)
assert.Equal("test\n", string(chunkBuf))
assert.NoError(t, err)
assert.Equal(t, "test\n", string(chunkBuf))
// Write the final part of the data and return
c.Response().Write([]byte("test"))
return nil
})(c)
assert.NotNil(r)
assert.NotNil(t, r)
buf := new(bytes.Buffer)
buf.ReadFrom(r)
assert.Equal("test", buf.String())
assert.Equal(t, "test", buf.String())
r.Close()
}