correctly handle recorded compressed response

see https://github.com/kataras/iris/issues/1569#issuecomment-664003098
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-26 20:28:31 +03:00
parent 613d3fc749
commit ad5e5d82ce
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
3 changed files with 24 additions and 8 deletions

View File

@ -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 package main
import ( import (

View File

@ -241,17 +241,22 @@ func releaseCompressResponseWriter(w *CompressResponseWriter) {
// and writes the status code. // and writes the status code.
// Called automatically before `EndResponse`. // Called automatically before `EndResponse`.
func (w *CompressResponseWriter) FlushResponse() { 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 { if w.Disabled {
w.Header().Del(VaryHeaderKey) w.Header().Del(VaryHeaderKey)
w.Header().Del(ContentEncodingHeaderKey) w.Header().Del(ContentEncodingHeaderKey)
w.CompressWriter.Reset(&noOpWriter{}) w.CompressWriter.Reset(&noOpWriter{})
w.CompressWriter.Close()
} else { } else {
w.ResponseWriter.Header().Del(ContentLengthHeaderKey) w.ResponseWriter.Header().Del(ContentLengthHeaderKey)
w.CompressWriter.Close() // flushes and closes.
} }
w.ResponseWriter.FlushResponse()
} }
// EndResponse reeases the writers. // EndResponse reeases the writers.

View File

@ -152,14 +152,24 @@ func (w *ResponseRecorder) FlushResponse() {
} }
} }
// NOTE: before the ResponseWriter.Write in order to: cw, mustWriteToClose := w.ResponseWriter.(*CompressResponseWriter)
// set the given status code even if the body is empty. if mustWriteToClose { // see #1569#issuecomment-664003098
w.ResponseWriter.FlushResponse() 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 { if len(w.chunks) > 0 {
// ignore error // ignore error
w.ResponseWriter.Write(w.chunks) w.ResponseWriter.Write(w.chunks)
} }
if mustWriteToClose {
cw.CompressWriter.Close()
cw.ResponseWriter.FlushResponse()
}
} }
// Clone returns a clone of this response writer // Clone returns a clone of this response writer