Skip to content

Commit

Permalink
net/http: make ParseForm ignore unknown content types.
Browse files Browse the repository at this point in the history
Also fix a shadowed error variable bug.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5573072
  • Loading branch information
rogpeppe committed Jan 26, 2012
1 parent 974fa75 commit 32d7a73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 1 addition & 3 deletions src/pkg/net/http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ func (r *Request) ParseForm() (err error) {
return errors.New("missing form body")
}
ct := r.Header.Get("Content-Type")
ct, _, err := mime.ParseMediaType(ct)
ct, _, err = mime.ParseMediaType(ct)
switch {
case ct == "application/x-www-form-urlencoded":
var reader io.Reader = r.Body
Expand Down Expand Up @@ -646,8 +646,6 @@ func (r *Request) ParseForm() (err error) {
// Clean this up and write more tests.
// request_test.go contains the start of this,
// in TestRequestMultipartCallOrder.
default:
return &badStringError{"unknown Content-Type", ct}
}
}
return err
Expand Down
19 changes: 11 additions & 8 deletions src/pkg/net/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,31 @@ func TestPostQuery(t *testing.T) {

type stringMap map[string][]string
type parseContentTypeTest struct {
shouldError bool
contentType stringMap
}

var parseContentTypeTests = []parseContentTypeTest{
{contentType: stringMap{"Content-Type": {"text/plain"}}},
{contentType: stringMap{}}, // Non-existent keys are not placed. The value nil is illegal.
{contentType: stringMap{"Content-Type": {"text/plain; boundary="}}},
{
contentType: stringMap{"Content-Type": {"application/unknown"}},
},
{false, stringMap{"Content-Type": {"text/plain"}}},
// Non-existent keys are not placed. The value nil is illegal.
{true, stringMap{}},
{true, stringMap{"Content-Type": {"text/plain; boundary="}}},
{false, stringMap{"Content-Type": {"application/unknown"}}},
}

func TestParseFormBadContentType(t *testing.T) {
func TestParseFormUnknownContentType(t *testing.T) {
for i, test := range parseContentTypeTests {
req := &Request{
Method: "POST",
Header: Header(test.contentType),
Body: ioutil.NopCloser(bytes.NewBufferString("body")),
}
err := req.ParseForm()
if err == nil {
switch {
case err == nil && test.shouldError:
t.Errorf("test %d should have returned error", i)
case err != nil && !test.shouldError:
t.Errorf("test %d should not have returned error, got %v", i, err)
}
}
}
Expand Down

0 comments on commit 32d7a73

Please sign in to comment.