mirror of
https://github.com/labstack/echo.git
synced 2025-07-13 01:30:31 +02:00
fix gzip not sending response code for no content responses (404, 301/302 redirects etc)
This commit is contained in:
@ -107,10 +107,16 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
|
|||||||
|
|
||||||
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength, buffer: buf}
|
grw := &gzipResponseWriter{Writer: w, ResponseWriter: rw, minLength: config.MinLength, buffer: buf}
|
||||||
defer func() {
|
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 !grw.wroteBody {
|
||||||
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
|
if res.Header().Get(echo.HeaderContentEncoding) == gzipScheme {
|
||||||
res.Header().Del(echo.HeaderContentEncoding)
|
res.Header().Del(echo.HeaderContentEncoding)
|
||||||
}
|
}
|
||||||
|
if grw.wroteHeader {
|
||||||
|
rw.WriteHeader(grw.code)
|
||||||
|
}
|
||||||
// We have to reset response to it's pristine state when
|
// We have to reset response to it's pristine state when
|
||||||
// nothing is written to body or error is returned.
|
// nothing is written to body or error is returned.
|
||||||
// See issue #424, #407.
|
// See issue #424, #407.
|
||||||
|
@ -89,8 +89,6 @@ func TestGzip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGzipWithMinLength(t *testing.T) {
|
func TestGzipWithMinLength(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
// Minimal response length
|
// Minimal response length
|
||||||
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
||||||
@ -103,19 +101,17 @@ func TestGzipWithMinLength(t *testing.T) {
|
|||||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
e.ServeHTTP(rec, req)
|
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)
|
r, err := gzip.NewReader(rec.Body)
|
||||||
if assert.NoError(err) {
|
if assert.NoError(t, err) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
buf.ReadFrom(r)
|
buf.ReadFrom(r)
|
||||||
assert.Equal("foobarfoobar", buf.String())
|
assert.Equal(t, "foobarfoobar", buf.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGzipWithMinLengthTooShort(t *testing.T) {
|
func TestGzipWithMinLengthTooShort(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
// Minimal response length
|
// Minimal response length
|
||||||
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
e.Use(GzipWithConfig(GzipConfig{MinLength: 10}))
|
||||||
@ -127,13 +123,29 @@ func TestGzipWithMinLengthTooShort(t *testing.T) {
|
|||||||
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
assert.Equal("", rec.Header().Get(echo.HeaderContentEncoding))
|
assert.Equal(t, "", rec.Header().Get(echo.HeaderContentEncoding))
|
||||||
assert.Contains(rec.Body.String(), "test")
|
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) {
|
func TestGzipWithMinLengthChunked(t *testing.T) {
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
|
|
||||||
// Gzip chunked
|
// Gzip chunked
|
||||||
@ -155,36 +167,36 @@ func TestGzipWithMinLengthChunked(t *testing.T) {
|
|||||||
c.Response().Flush()
|
c.Response().Flush()
|
||||||
|
|
||||||
// Read the first part of the data
|
// Read the first part of the data
|
||||||
assert.True(rec.Flushed)
|
assert.True(t, rec.Flushed)
|
||||||
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
r, err = gzip.NewReader(rec.Body)
|
r, err = gzip.NewReader(rec.Body)
|
||||||
assert.NoError(err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
_, err = io.ReadFull(r, chunkBuf)
|
_, err = io.ReadFull(r, chunkBuf)
|
||||||
assert.NoError(err)
|
assert.NoError(t, err)
|
||||||
assert.Equal("test\n", string(chunkBuf))
|
assert.Equal(t, "test\n", string(chunkBuf))
|
||||||
|
|
||||||
// Write and flush the second part of the data
|
// Write and flush the second part of the data
|
||||||
c.Response().Write([]byte("test\n"))
|
c.Response().Write([]byte("test\n"))
|
||||||
c.Response().Flush()
|
c.Response().Flush()
|
||||||
|
|
||||||
_, err = io.ReadFull(r, chunkBuf)
|
_, err = io.ReadFull(r, chunkBuf)
|
||||||
assert.NoError(err)
|
assert.NoError(t, err)
|
||||||
assert.Equal("test\n", string(chunkBuf))
|
assert.Equal(t, "test\n", string(chunkBuf))
|
||||||
|
|
||||||
// Write the final part of the data and return
|
// Write the final part of the data and return
|
||||||
c.Response().Write([]byte("test"))
|
c.Response().Write([]byte("test"))
|
||||||
return nil
|
return nil
|
||||||
})(c)
|
})(c)
|
||||||
|
|
||||||
assert.NotNil(r)
|
assert.NotNil(t, r)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
buf.ReadFrom(r)
|
buf.ReadFrom(r)
|
||||||
assert.Equal("test", buf.String())
|
assert.Equal(t, "test", buf.String())
|
||||||
|
|
||||||
r.Close()
|
r.Close()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user