Skip to content

Commit

Permalink
Revert "Fix issue on status code when writer was flushed"
Browse files Browse the repository at this point in the history
This reverts commit 0f67f3f.
  • Loading branch information
tmthrgd authored and jprobinson committed Nov 20, 2017
1 parent 843330b commit 6c51bf0
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 24 deletions.
8 changes: 1 addition & 7 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type GzipResponseWriter struct {
buf []byte // Holds the first part of the write before reaching the minSize or the end of the write.

contentTypes []string // Only compress if the response is one of these content-types. All are accepted if empty.
flushed bool // Indicate if the stream was already flushed
}

// Write appends data to the gzip writer.
Expand Down Expand Up @@ -170,8 +169,7 @@ func (w *GzipResponseWriter) init() {
func (w *GzipResponseWriter) Close() error {
if w.gw == nil {
// Gzip not trigged yet, write out regular response.
// WriteHeader only if it wasn't already wrote by a Flush
if !w.flushed && w.code != 0 {
if w.code != 0 {
w.ResponseWriter.WriteHeader(w.code)
}
if w.buf != nil {
Expand Down Expand Up @@ -199,11 +197,7 @@ func (w *GzipResponseWriter) Flush() {
}

if fw, ok := w.ResponseWriter.(http.Flusher); ok {
if !w.flushed && w.code != 0 {
w.ResponseWriter.WriteHeader(w.code)
}
fw.Flush()
w.flushed = true
}
}

Expand Down
17 changes: 0 additions & 17 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,23 +306,6 @@ func TestStatusCodes(t *testing.T) {
}
}

func TestStatusCodesFlushed(t *testing.T) {
handler := GzipHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusNotFound)
rw.(http.Flusher).Flush()
rw.Write([]byte("Not found"))
}))
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set(acceptEncoding, "gzip")
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)

result := w.Result()
if result.StatusCode != http.StatusNotFound {
t.Errorf("StatusCode should have been 404 but was %d", result.StatusCode)
}
}

func TestIgnoreSubsequentWriteHeader(t *testing.T) {
handler := GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
Expand Down

0 comments on commit 6c51bf0

Please sign in to comment.