diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2011-05-11 12:11:32 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2011-05-11 12:11:32 -0700 |
| commit | ca83cd2c2f75075b6b7b8b06d25dbe50a3659e9f (patch) | |
| tree | 6cd43ca0351cbf9118e46e9ddf5852a05e10a932 /src/pkg/http | |
| parent | 51e6aa1e885fc159343f980a0a709f660e27c2fa (diff) | |
| download | go-ca83cd2c2f75075b6b7b8b06d25dbe50a3659e9f.tar.xz | |
http: fix transport bug with zero-length bodies
An optimization in Transport which re-uses TCP
connections early in the case where there is
no response body interacted poorly with
ErrBodyReadAfterClose. Upon recycling the TCP
connection early we would Close the Response.Body
(in case the user forgot to), but in the case
of a zero-lengthed body, the user's handler might
not have run yet.
This CL makes sure the Transport doesn't try
to Close requests when we're about to immediately
re-use the TCP connection.
This also includes additional tests I wrote
while debugging.
R=rsc, bradfitzgoog
CC=golang-dev
https://golang.org/cl/4529050
Diffstat (limited to 'src/pkg/http')
| -rw-r--r-- | src/pkg/http/serve_test.go | 44 | ||||
| -rw-r--r-- | src/pkg/http/transport.go | 11 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/pkg/http/serve_test.go b/src/pkg/http/serve_test.go index f2fb98e3e2..c9305682d2 100644 --- a/src/pkg/http/serve_test.go +++ b/src/pkg/http/serve_test.go @@ -710,3 +710,47 @@ func TestRedirectMunging(t *testing.T) { t.Errorf("Location header was %q; want %q", g, e) } } + +// TestZeroLengthPostAndResponse exercises an optimization done by the Transport: +// when there is no body (either because the method doesn't permit a body, or an +// explicit Content-Length of zero is present), then the transport can re-use the +// connection immediately. But when it re-uses the connection, it typically closes +// the previous request's body, which is not optimal for zero-lengthed bodies, +// as the client would then see http.ErrBodyReadAfterClose and not 0, os.EOF. +func TestZeroLengthPostAndResponse(t *testing.T) { + ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, r *Request) { + all, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Fatalf("handler ReadAll: %v", err) + } + if len(all) != 0 { + t.Errorf("handler got %d bytes; expected 0", len(all)) + } + rw.Header().Set("Content-Length", "0") + })) + defer ts.Close() + + req, err := NewRequest("POST", ts.URL, strings.NewReader("")) + if err != nil { + t.Fatal(err) + } + req.ContentLength = 0 + + var resp [5]*Response + for i := range resp { + resp[i], err = DefaultClient.Do(req) + if err != nil { + t.Fatalf("client post #%d: %v", i, err) + } + } + + for i := range resp { + all, err := ioutil.ReadAll(resp[i].Body) + if err != nil { + t.Fatalf("req #%d: client ReadAll: %v", i, err) + } + if len(all) != 0 { + t.Errorf("req #%d: client got %d bytes; expected 0", i, len(all)) + } + } +} diff --git a/src/pkg/http/transport.go b/src/pkg/http/transport.go index 73a2c2191e..a7b1b20e63 100644 --- a/src/pkg/http/transport.go +++ b/src/pkg/http/transport.go @@ -469,6 +469,17 @@ func (pc *persistConn) readLoop() { waitForBodyRead <- true } } else { + // When there's no response body, we immediately + // reuse the TCP connection (putIdleConn), but + // we need to prevent ClientConn.Read from + // closing the Response.Body on the next + // loop, otherwise it might close the body + // before the client code has had a chance to + // read it (even though it'll just be 0, EOF). + pc.cc.lk.Lock() + pc.cc.lastbody = nil + pc.cc.lk.Unlock() + pc.t.putIdleConn(pc) } } |
