diff --git a/_examples/request-body/read-query/main.go b/_examples/request-body/read-query/main.go index ec6e8f8b7..ca0975ddb 100644 --- a/_examples/request-body/read-query/main.go +++ b/_examples/request-body/read-query/main.go @@ -1,4 +1,5 @@ -// package main contains an example on how to use the ReadForm, but with the same way you can do the ReadJSON & ReadJSON +// package main contains an example on how to use the ReadQuery, +// same way you can do the ReadJSON & ReadProtobuf and e.t.c. package main import ( diff --git a/context/compress.go b/context/compress.go index cdb62cb09..41f91b6ff 100644 --- a/context/compress.go +++ b/context/compress.go @@ -241,17 +241,22 @@ func releaseCompressResponseWriter(w *CompressResponseWriter) { // and writes the status code. // Called automatically before `EndResponse`. func (w *CompressResponseWriter) FlushResponse() { + w.FlushHeaders() + + // write the status, after header set and before any flushed content sent. + w.ResponseWriter.FlushResponse() + + w.CompressWriter.Close() // flushes and closes. +} + +func (w *CompressResponseWriter) FlushHeaders() { if w.Disabled { w.Header().Del(VaryHeaderKey) w.Header().Del(ContentEncodingHeaderKey) w.CompressWriter.Reset(&noOpWriter{}) - w.CompressWriter.Close() } else { w.ResponseWriter.Header().Del(ContentLengthHeaderKey) - w.CompressWriter.Close() // flushes and closes. } - - w.ResponseWriter.FlushResponse() } // EndResponse reeases the writers. diff --git a/context/response_recorder.go b/context/response_recorder.go index 6e2de58dd..8acdfd1cc 100644 --- a/context/response_recorder.go +++ b/context/response_recorder.go @@ -152,14 +152,24 @@ func (w *ResponseRecorder) FlushResponse() { } } - // NOTE: before the ResponseWriter.Write in order to: - // set the given status code even if the body is empty. - w.ResponseWriter.FlushResponse() + cw, mustWriteToClose := w.ResponseWriter.(*CompressResponseWriter) + if mustWriteToClose { // see #1569#issuecomment-664003098 + cw.FlushHeaders() + } else { + // NOTE: before the ResponseWriter.Write in order to: + // set the given status code even if the body is empty. + w.ResponseWriter.FlushResponse() + } if len(w.chunks) > 0 { // ignore error w.ResponseWriter.Write(w.chunks) } + + if mustWriteToClose { + cw.CompressWriter.Close() + cw.ResponseWriter.FlushResponse() + } } // Clone returns a clone of this response writer