From ea94aae1e085e50aa81246748a3bddd09814a8c4 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Mon, 28 Oct 2019 11:35:57 -0400 Subject: [PATCH 1/3] check minsize when writing first byte --- gzip.go | 2 +- gzip_test.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/gzip.go b/gzip.go index c112bbd..b6c332a 100644 --- a/gzip.go +++ b/gzip.go @@ -166,7 +166,7 @@ func (w *GzipResponseWriter) startGzip() error { // Initialize and flush the buffer into the gzip response if there are any bytes. // If there aren't any, we shouldn't initialize it yet because on Close it will // write the gzip header even if nothing was ever written. - if len(w.buf) > 0 { + if len(w.buf) >= w.minSize { // Initialize the GZIP response. w.init() n, err := w.gw.Write(w.buf) diff --git a/gzip_test.go b/gzip_test.go index bed7f52..7e5f416 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -208,6 +208,51 @@ func TestGzipHandlerNoBody(t *testing.T) { } } +func TestGzipHnalderStream(t *testing.T) { + ln, err := net.Listen("tcp", "127.0.0.1:") + if err != nil { + t.Fatalf("failed creating listen socket: %v", err) + } + defer ln.Close() + srv := &http.Server{ + Handler: nil, + } + go srv.Serve(ln) + + handler, ok := GzipHandlerWithOpts(MinSize(0)) + assert.Nil(t, ok) + srv.Handler = handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + flusher, ok := w.(http.Flusher) + assert.Equal(t, true, ok) + + w.WriteHeader(200) + w.Write([]byte("")) + flusher.Flush() + w.Write([]byte("subsequent write with data")) + flusher.Flush() + })) + + req := &http.Request{ + Method: "GET", + URL: &url.URL{Path: "/", Scheme: "http", Host: ln.Addr().String()}, + Header: make(http.Header), + Close: false, + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + t.Fatalf("Unexpected error making http request %v", err) + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatalf("Unexpected error reading response body %v", err) + } + + assert.Equal(t, "subsequent write with data", string(body)) + +} func TestGzipHandlerContentLength(t *testing.T) { testBodyBytes := []byte(testBody) tests := []struct { From 3146790764815bca9cb61a7694e91aadc7f00340 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Thu, 31 Oct 2019 08:35:05 -0400 Subject: [PATCH 2/3] ignore fix, just failing test --- gzip.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gzip.go b/gzip.go index b6c332a..d17479f 100644 --- a/gzip.go +++ b/gzip.go @@ -166,11 +166,10 @@ func (w *GzipResponseWriter) startGzip() error { // Initialize and flush the buffer into the gzip response if there are any bytes. // If there aren't any, we shouldn't initialize it yet because on Close it will // write the gzip header even if nothing was ever written. - if len(w.buf) >= w.minSize { + if len(w.buf) > 0 { // Initialize the GZIP response. w.init() n, err := w.gw.Write(w.buf) - // This should never happen (per io.Writer docs), but if the write didn't // accept the entire buffer but returned no specific error, we have no clue // what's going on, so abort just to be safe. From 3d1da3516453114e4ba11e82fbc8c2e5a4378ac7 Mon Sep 17 00:00:00 2001 From: Drew Bailey <2614075+drewbailey@users.noreply.github.com> Date: Thu, 31 Oct 2019 08:35:56 -0400 Subject: [PATCH 3/3] fix typo --- gzip_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gzip_test.go b/gzip_test.go index 7e5f416..7dd69fa 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -208,7 +208,7 @@ func TestGzipHandlerNoBody(t *testing.T) { } } -func TestGzipHnalderStream(t *testing.T) { +func TestGzipHandlerStream(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:") if err != nil { t.Fatalf("failed creating listen socket: %v", err)