]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: fix duplicate status code in Response.Write
authorBrad Fitzpatrick <bradfitz@golang.org>
Mon, 21 May 2012 18:07:27 +0000 (11:07 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 21 May 2012 18:07:27 +0000 (11:07 -0700)
Fixes #3636

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/6203094

src/pkg/net/http/response.go
src/pkg/net/http/response_test.go
src/pkg/net/http/transfer.go

index b79022097867992c5b6c011dec4ae9b9ea6df76d..945ecd8a4b04dbb755ac3f9909041521488d28f0 100644 (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)
index 165ec3624a4585303f97ac6959c4f4f1bca7a934..6eed4887ddceb8620cc434fd9be18f34413d480e 100644 (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())
+       }
+}
index 3c8fe7f5b510110e49d6e07ebccbdbffb9ddbb44..9e9d84172d00effda81bb0ee7e45739d5a161f8e 100644 (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