Commit d45f22e3 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: fix duplicate status code in Response.Write

Fixes #3636

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6203094
parent b4456df6
......@@ -202,9 +202,12 @@ func (r *Response) Write(w io.Writer) error {
text = "status code " + strconv.Itoa(r.StatusCode)
}
}
io.WriteString(w, "HTTP/"+strconv.Itoa(r.ProtoMajor)+".")
io.WriteString(w, strconv.Itoa(r.ProtoMinor)+" ")
io.WriteString(w, strconv.Itoa(r.StatusCode)+" "+text+"\r\n")
protoMajor, protoMinor := strconv.Itoa(r.ProtoMajor), strconv.Itoa(r.ProtoMinor)
statusCode := strconv.Itoa(r.StatusCode) + " "
if strings.HasPrefix(text, statusCode) {
text = text[len(statusCode):]
}
io.WriteString(w, "HTTP/"+protoMajor+"."+protoMinor+" "+statusCode+text+"\r\n")
// Process Body,ContentLength,Close,Trailer
tw, err := newTransferWriter(r)
......
......@@ -14,6 +14,7 @@ import (
"io/ioutil"
"net/url"
"reflect"
"strings"
"testing"
)
......@@ -444,3 +445,17 @@ func TestLocationResponse(t *testing.T) {
}
}
}
func TestResponseStatusStutter(t *testing.T) {
r := &Response{
Status: "123 some status",
StatusCode: 123,
ProtoMajor: 1,
ProtoMinor: 3,
}
var buf bytes.Buffer
r.Write(&buf)
if strings.Contains(buf.String(), "123 123") {
t.Errorf("stutter in status: %s", buf.String())
}
}
......@@ -71,7 +71,9 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) {
}
}
case *Response:
t.Method = rr.Request.Method
if rr.Request != nil {
t.Method = rr.Request.Method
}
t.Body = rr.Body
t.BodyCloser = rr.Body
t.ContentLength = rr.ContentLength
......@@ -79,7 +81,7 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) {
t.TransferEncoding = rr.TransferEncoding
t.Trailer = rr.Trailer
atLeastHTTP11 = rr.ProtoAtLeast(1, 1)
t.ResponseToHEAD = noBodyExpected(rr.Request.Method)
t.ResponseToHEAD = noBodyExpected(t.Method)
}
// Sanitize Body,ContentLength,TransferEncoding
......
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