]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: don't send chunked encoding on 204 responses
authorBrad Fitzpatrick <bradfitz@golang.org>
Fri, 30 Nov 2012 02:00:51 +0000 (18:00 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 30 Nov 2012 02:00:51 +0000 (18:00 -0800)
RFC 2616: "The 204 response MUST NOT include a message-body,
and thus is always terminated by the first empty line after
the header fields."

Previously we'd trigger chunked encoding by default on
responses, and then when finishing the request we'd write the
chunk trailers, which counted as a message-body.

Fixes #4454

R=golang-dev
CC=golang-dev
https://golang.org/cl/6782139

src/pkg/net/http/server.go
src/pkg/net/http/transport_test.go

index b50c03ed3a1e73c3bad687a3073241c8f00275f8..f786e81b9f460f07056ce2d4d61fbe53a9e54f18 100644 (file)
@@ -369,6 +369,8 @@ func (w *response) WriteHeader(code int) {
 
        if w.req.Method == "HEAD" || code == StatusNotModified {
                // do nothing
+       } else if code == StatusNoContent {
+               w.header.Del("Transfer-Encoding")
        } else if hasCL {
                w.contentLength = contentLength
                w.header.Del("Transfer-Encoding")
index 2f4eb88f96fff510f13cfaf6a1ee3eb1ed9b614f..e49f14fa581c22e1a5e91751bfa4c0a50b929750 100644 (file)
@@ -857,6 +857,30 @@ func TestIssue3595(t *testing.T) {
        }
 }
 
+// From http://golang.org/issue/4454 ,
+// "client fails to handle requests with no body and chunked encoding"
+func TestChunkedNoContent(t *testing.T) {
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               w.WriteHeader(StatusNoContent)
+       }))
+       defer ts.Close()
+
+       for _, closeBody := range []bool{true, false} {
+               c := &Client{Transport: &Transport{}}
+               const n = 4
+               for i := 1; i <= n; i++ {
+                       res, err := c.Get(ts.URL)
+                       if err != nil {
+                               t.Errorf("closingBody=%v, req %d/%d: %v", closeBody, i, n, err)
+                       } else {
+                               if closeBody {
+                                       res.Body.Close()
+                               }
+                       }
+               }
+       }
+}
+
 func TestTransportConcurrency(t *testing.T) {
        const maxProcs = 16
        const numReqs = 500