Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check minsize when writing first byte #91

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to introduce another issue found in the failing test case due to the reason previously mentioned. This is preventing w.init() from being called. Also, the following conditional check len(w.buf) >= w.minSize is already made in the write function.

// Initialize the GZIP response.
w.init()
n, err := w.gw.Write(w.buf)
Expand Down
45 changes: 45 additions & 0 deletions gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,51 @@ func TestGzipHandlerNoBody(t *testing.T) {
}
}

func TestGzipHnalderStream(t *testing.T) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean write TestGzipHandlerStream instead of TestGzipHnalderStream

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops yep! Also I’m not confident in the fix but the test case reproduces the issue at hand.

Startgzip gets called but not init then subsequently startPlain gets called and then errors

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 {
Expand Down