From 58839cefb50e56ae5b157b37e9814ae83ceee70b Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 20 Jul 2017 15:22:49 -0700 Subject: Upgrading server dependancies (#6984) --- vendor/github.com/NYTimes/gziphandler/gzip.go | 38 +++++++++------------- vendor/github.com/NYTimes/gziphandler/gzip_test.go | 24 ++++++++++++++ 2 files changed, 39 insertions(+), 23 deletions(-) (limited to 'vendor/github.com/NYTimes') diff --git a/vendor/github.com/NYTimes/gziphandler/gzip.go b/vendor/github.com/NYTimes/gziphandler/gzip.go index cccf79de7..ea6dba1e7 100644 --- a/vendor/github.com/NYTimes/gziphandler/gzip.go +++ b/vendor/github.com/NYTimes/gziphandler/gzip.go @@ -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. @@ -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() @@ -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() { @@ -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 } @@ -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() diff --git a/vendor/github.com/NYTimes/gziphandler/gzip_test.go b/vendor/github.com/NYTimes/gziphandler/gzip_test.go index b9e687c8e..0f6488cae 100644 --- a/vendor/github.com/NYTimes/gziphandler/gzip_test.go +++ b/vendor/github.com/NYTimes/gziphandler/gzip_test.go @@ -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) } -- cgit v1.2.3-1-g7c22