From d71d08af5ab15c7b166d92a31219c4c218438841 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 5 Jan 2011 13:09:38 -0500 Subject: [PATCH] http: permit empty Reason-Phrase in response Status-Line Fixes #1388. R=rsc CC=golang-dev https://golang.org/cl/3749043 --- src/pkg/http/response.go | 8 ++++++-- src/pkg/http/response_test.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/pkg/http/response.go b/src/pkg/http/response.go index 6a209c9f88..a24726110c 100644 --- a/src/pkg/http/response.go +++ b/src/pkg/http/response.go @@ -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]} diff --git a/src/pkg/http/response_test.go b/src/pkg/http/response_test.go index f21587fd46..89a8c3b44d 100644 --- a/src/pkg/http/response_test.go +++ b/src/pkg/http/response_test.go @@ -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) {