Commit 23f6bc5e authored by Russ Cox's avatar Russ Cox

[release-branch.go1.3] net/http: fix double Content-Length in response

««« CL 105040043 / ef8878dbed3b
net/http: fix double Content-Length in response

Fixes #8180

LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/105040043
»»»

TBR=bradfitz
R=golang-codereviews
CC=bradfitz, golang-codereviews, iant
https://golang.org/cl/102300046
parent 74183c5b
......@@ -266,7 +266,10 @@ func (r *Response) Write(w io.Writer) error {
return err
}
if r1.ContentLength == 0 && !chunked(r1.TransferEncoding) {
// contentLengthAlreadySent may have been already sent for
// POST/PUT requests, even if zero length. See Issue 8180.
contentLengthAlreadySent := tw.shouldSendContentLength()
if r1.ContentLength == 0 && !chunked(r1.TransferEncoding) && !contentLengthAlreadySent {
if _, err := io.WriteString(w, "Content-Length: 0\r\n"); err != nil {
return err
}
......
......@@ -191,6 +191,22 @@ func TestResponseWrite(t *testing.T) {
"Foo: Bar Baz\r\n" +
"\r\n",
},
// Want a single Content-Length header. Fixing issue 8180 where
// there were two.
{
Response{
StatusCode: StatusOK,
ProtoMajor: 1,
ProtoMinor: 1,
Request: &Request{Method: "POST"},
Header: Header{},
ContentLength: 0,
TransferEncoding: nil,
Body: nil,
},
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n",
},
}
for i := range respWriteTests {
......
......@@ -155,7 +155,9 @@ func (t *transferWriter) WriteHeader(w io.Writer) error {
// function of the sanitized field triple (Body, ContentLength,
// TransferEncoding)
if t.shouldSendContentLength() {
io.WriteString(w, "Content-Length: ")
if _, err := io.WriteString(w, "Content-Length: "); err != nil {
return err
}
if _, err := io.WriteString(w, strconv.FormatInt(t.ContentLength, 10)+"\r\n"); err != nil {
return err
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment