Skip to content

Commit

Permalink
correctly handle recorded compressed response
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jul 26, 2020
1 parent 613d3fc commit ad5e5d8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 2 additions & 1 deletion _examples/request-body/read-query/main.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down
13 changes: 9 additions & 4 deletions context/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 13 additions & 3 deletions context/response_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ad5e5d8

Please sign in to comment.