]> Cypherpunks repositories - gostls13.git/commitdiff
http: permit empty Reason-Phrase in response Status-Line
authorBrad Fitzpatrick <brad@danga.com>
Wed, 5 Jan 2011 18:09:38 +0000 (13:09 -0500)
committerRuss Cox <rsc@golang.org>
Wed, 5 Jan 2011 18:09:38 +0000 (13:09 -0500)
Fixes #1388.

R=rsc
CC=golang-dev
https://golang.org/cl/3749043

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

index 6a209c9f88ddeceef21aa7a89c32a7e8ca5f2b06..a24726110c8e8508fb4a82b7138bc45fce391243 100644 (file)
@@ -86,10 +86,14 @@ func ReadResponse(r *bufio.Reader, requestMethod string) (resp *Response, err os
                return nil, err
        }
        f := strings.Split(line, " ", 3)
-       if len(f) < 3 {
+       if len(f) < 2 {
                return nil, &badStringError{"malformed HTTP response", line}
        }
-       resp.Status = f[1] + " " + f[2]
+       reasonPhrase := ""
+       if len(f) > 2 {
+               reasonPhrase = f[2]
+       }
+       resp.Status = f[1] + " " + reasonPhrase
        resp.StatusCode, err = strconv.Atoi(f[1])
        if err != nil {
                return nil, &badStringError{"malformed HTTP status code", f[1]}
index f21587fd46b8a512217f69eba426d43609badfc6..89a8c3b44d2481886b3fd42d8b526e57d6bb4320 100644 (file)
@@ -122,6 +122,44 @@ var respTests = []respTest{
 
                "Body here\n",
        },
+
+       // Status line without a Reason-Phrase, but trailing space.
+       // (permitted by RFC 2616)
+       {
+               "HTTP/1.0 303 \r\n\r\n",
+               Response{
+                       Status:        "303 ",
+                       StatusCode:    303,
+                       Proto:         "HTTP/1.0",
+                       ProtoMajor:    1,
+                       ProtoMinor:    0,
+                       RequestMethod: "GET",
+                       Header:        map[string]string{},
+                       Close:         true,
+                       ContentLength: -1,
+               },
+
+               "",
+       },
+
+       // Status line without a Reason-Phrase, and no trailing space.
+       // (not permitted by RFC 2616, but we'll accept it anyway)
+       {
+               "HTTP/1.0 303\r\n\r\n",
+               Response{
+                       Status:        "303 ",
+                       StatusCode:    303,
+                       Proto:         "HTTP/1.0",
+                       ProtoMajor:    1,
+                       ProtoMinor:    0,
+                       RequestMethod: "GET",
+                       Header:        map[string]string{},
+                       Close:         true,
+                       ContentLength: -1,
+               },
+
+               "",
+       },
 }
 
 func TestReadResponse(t *testing.T) {