Skip to content

Commit

Permalink
Never write to ResponseWriter if we were not written to (#49)
Browse files Browse the repository at this point in the history
add TestDontWriteWhenNotWrittenTo
  • Loading branch information
EmielM authored and jprobinson committed Jun 23, 2017
1 parent 22d4470 commit 56545f4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
38 changes: 15 additions & 23 deletions gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (w *GzipResponseWriter) Write(b []byte) (int, error) {
}

// Save the write into a buffer for later use in GZIP responseWriter (if content is long enough) or at close with regular responseWriter.
// On the first write, w.buf changes from nil to a valid slice
w.buf = append(w.buf, b...)

// If the global writes are bigger than the minSize, compression is enable.
Expand All @@ -122,7 +123,9 @@ func (w *GzipResponseWriter) startGzip() error {
w.Header().Del(contentLength)

// Write the header to gzip response.
w.writeHeader()
if w.code != 0 {
w.ResponseWriter.WriteHeader(w.code)
}

// Initialize the GZIP response.
w.init()
Expand All @@ -146,14 +149,6 @@ func (w *GzipResponseWriter) WriteHeader(code int) {
w.code = code
}

// writeHeader uses the saved code to send it to the ResponseWriter.
func (w *GzipResponseWriter) writeHeader() {
if w.code == 0 {
w.code = http.StatusOK
}
w.ResponseWriter.WriteHeader(w.code)
}

// init graps a new gzip writer from the gzipWriterPool and writes the correct
// content encoding header.
func (w *GzipResponseWriter) init() {
Expand All @@ -166,19 +161,18 @@ func (w *GzipResponseWriter) init() {

// Close will close the gzip.Writer and will put it back in the gzipWriterPool.
func (w *GzipResponseWriter) Close() error {
// Buffer not nil means the regular response must be returned.
if w.buf != nil {
w.writeHeader()
// Make the write into the regular response.
_, writeErr := w.ResponseWriter.Write(w.buf)
// Returns the error if any at write.
if writeErr != nil {
return fmt.Errorf("gziphandler: write to regular responseWriter at close gets error: %q", writeErr.Error())
}
}

// If the GZIP responseWriter is not set no needs to close it.
if w.gw == nil {
// Gzip not trigged yet, write out regular response.
if w.code != 0 {
w.ResponseWriter.WriteHeader(w.code)
}
if w.buf != nil {
_, writeErr := w.ResponseWriter.Write(w.buf)
// Returns the error if any at write.
if writeErr != nil {
return fmt.Errorf("gziphandler: write to regular responseWriter at close gets error: %q", writeErr.Error())
}
}
return nil
}

Expand Down Expand Up @@ -253,8 +247,6 @@ func NewGzipLevelAndMinSize(level, minSize int) (func(http.Handler) http.Handler
ResponseWriter: w,
index: index,
minSize: minSize,

buf: []byte{},
}
defer gw.Close()

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

func TestDontWriteWhenNotWrittenTo(t *testing.T) {
// When using gzip as middleware without ANY writes in the handler,
// ensure the gzip middleware doesn't touch the actual ResponseWriter
// either.

handler0 := GzipHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
}))

handler1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler0.ServeHTTP(w, r)
w.WriteHeader(404) // this only works if gzip didn't do a WriteHeader(200)
})

r := httptest.NewRequest("GET", "/", nil)
r.Header.Set("Accept-Encoding", "gzip")
w := httptest.NewRecorder()
handler1.ServeHTTP(w, r)

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

// --------------------------------------------------------------------

func BenchmarkGzipHandler_S2k(b *testing.B) { benchmark(b, false, 2048) }
Expand Down

0 comments on commit 56545f4

Please sign in to comment.