]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: Don't set Content-Length: -1 when responding to a POST
authorPeter Waldschmidt <peter@waldschmidt.com>
Wed, 29 Apr 2015 20:59:38 +0000 (16:59 -0400)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 29 Apr 2015 22:58:37 +0000 (22:58 +0000)
Fixes an issue where Response.Write writes out a Content-Length: -1
header when the corresponding Request is a POST or PUT and the
ContentLength was not previously set.

This was encountered when using httputil.DumpResponse
to write out the response from a server that responded to a PUT
request with no Content-Length header. The dumped output is
thus invalid.

Change-Id: I52c6ae8ef3443f1f9de92aeee9f9581dabb05991
Reviewed-on: https://go-review.googlesource.com/9496
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>

src/net/http/responsewrite_test.go
src/net/http/transfer.go

index 585b13b8504191f2c4db333f39712e54cfca9f53..5b8d47ab5819df23e33c348caf4da86a8e3a1897 100644 (file)
@@ -207,6 +207,21 @@ func TestResponseWrite(t *testing.T) {
                        },
                        "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
                },
+
+               // When a response to a POST has Content-Length: -1, make sure we don't
+               // write the Content-Length as -1.
+               {
+                       Response{
+                               StatusCode:    StatusOK,
+                               ProtoMajor:    1,
+                               ProtoMinor:    1,
+                               Request:       &Request{Method: "POST"},
+                               Header:        Header{},
+                               ContentLength: -1,
+                               Body:          ioutil.NopCloser(strings.NewReader("abcdef")),
+                       },
+                       "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nabcdef",
+               },
        }
 
        for i := range respWriteTests {
index c39d6cff6788b8a3af349bb207b8d5e1d06c66a7..7372d7537e81d4fd17f81da4d95512d08bc4595a 100644 (file)
@@ -138,6 +138,9 @@ func (t *transferWriter) shouldSendContentLength() bool {
        if t.ContentLength > 0 {
                return true
        }
+       if t.ContentLength < 0 {
+               return false
+       }
        // Many servers expect a Content-Length for these methods
        if t.Method == "POST" || t.Method == "PUT" {
                return true