1
0
mirror of https://github.com/golang/go synced 2024-09-24 21:20:13 -06:00

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

Fixes #3636

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6203094
This commit is contained in:
Brad Fitzpatrick 2012-05-21 11:07:27 -07:00
parent b4456df6d2
commit d45f22e3c8
3 changed files with 25 additions and 5 deletions

View File

@ -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)

View File

@ -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())
}
}

View File

@ -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