Skip to content

Commit f5a5f17

Browse files
committed
[release-branch.go1] net/http: fix duplicate status code in Response.Write
««« backport aad801637707 net/http: fix duplicate status code in Response.Write Fixes #3636 R=golang-dev, adg CC=golang-dev https://golang.org/cl/6203094 »»»
1 parent 6a87464 commit f5a5f17

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/pkg/net/http/response.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,12 @@ func (r *Response) Write(w io.Writer) error {
202202
text = "status code " + strconv.Itoa(r.StatusCode)
203203
}
204204
}
205-
io.WriteString(w, "HTTP/"+strconv.Itoa(r.ProtoMajor)+".")
206-
io.WriteString(w, strconv.Itoa(r.ProtoMinor)+" ")
207-
io.WriteString(w, strconv.Itoa(r.StatusCode)+" "+text+"\r\n")
205+
protoMajor, protoMinor := strconv.Itoa(r.ProtoMajor), strconv.Itoa(r.ProtoMinor)
206+
statusCode := strconv.Itoa(r.StatusCode) + " "
207+
if strings.HasPrefix(text, statusCode) {
208+
text = text[len(statusCode):]
209+
}
210+
io.WriteString(w, "HTTP/"+protoMajor+"."+protoMinor+" "+statusCode+text+"\r\n")
208211

209212
// Process Body,ContentLength,Close,Trailer
210213
tw, err := newTransferWriter(r)

src/pkg/net/http/response_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io/ioutil"
1515
"net/url"
1616
"reflect"
17+
"strings"
1718
"testing"
1819
)
1920

@@ -444,3 +445,17 @@ func TestLocationResponse(t *testing.T) {
444445
}
445446
}
446447
}
448+
449+
func TestResponseStatusStutter(t *testing.T) {
450+
r := &Response{
451+
Status: "123 some status",
452+
StatusCode: 123,
453+
ProtoMajor: 1,
454+
ProtoMinor: 3,
455+
}
456+
var buf bytes.Buffer
457+
r.Write(&buf)
458+
if strings.Contains(buf.String(), "123 123") {
459+
t.Errorf("stutter in status: %s", buf.String())
460+
}
461+
}

src/pkg/net/http/transfer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,17 @@ func newTransferWriter(r interface{}) (t *transferWriter, err error) {
7171
}
7272
}
7373
case *Response:
74-
t.Method = rr.Request.Method
74+
if rr.Request != nil {
75+
t.Method = rr.Request.Method
76+
}
7577
t.Body = rr.Body
7678
t.BodyCloser = rr.Body
7779
t.ContentLength = rr.ContentLength
7880
t.Close = rr.Close
7981
t.TransferEncoding = rr.TransferEncoding
8082
t.Trailer = rr.Trailer
8183
atLeastHTTP11 = rr.ProtoAtLeast(1, 1)
82-
t.ResponseToHEAD = noBodyExpected(rr.Request.Method)
84+
t.ResponseToHEAD = noBodyExpected(t.Method)
8385
}
8486

8587
// Sanitize Body,ContentLength,TransferEncoding

0 commit comments

Comments
 (0)