]> Cypherpunks repositories - gostls13.git/commitdiff
Fixing HTTP POST handling to work with Chrome and Safari.
authorBill Neubauer <wcn@golang.org>
Sat, 3 Oct 2009 18:09:01 +0000 (11:09 -0700)
committerBill Neubauer <wcn@golang.org>
Sat, 3 Oct 2009 18:09:01 +0000 (11:09 -0700)
request.go does not handle Content-Type correctly for the definition of
Media Types.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7

R=rsc
APPROVED=rsc
DELTA=44  (42 added, 0 deleted, 2 changed)
OCL=35274
CL=35306

src/pkg/http/request.go
src/pkg/http/request_test.go

index 2b425b7322e7e1a997c5dfc81abe7dec53b847f4..7ca7f0dc6c35e15f05bbb52e677eba896164f758 100644 (file)
@@ -627,7 +627,7 @@ func (r *Request) ParseForm() (err os.Error) {
                        return os.ErrorString("missing form body")
                }
                ct, _ := r.Header["Content-Type"];
-               switch ct {
+               switch strings.Split(ct, ";", 2)[0] {
                case "text/plain", "application/x-www-form-urlencoded", "":
                        var b []byte;
                        if b, err = io.ReadAll(r.Body); err != nil {
index c5762ab296aa9c13cb55ecbba48befda1203ae64..d45e0ed6b51b65df892a37f71e9ee105bdd2c731 100644 (file)
@@ -4,7 +4,10 @@
 
 package http
 
-import "testing"
+import (
+       "bytes";
+       "testing";
+)
 
 type stringMultimap map[string] []string
 
@@ -64,3 +67,42 @@ func TestQuery(t *testing.T) {
                t.Errorf(`req.FormValue("q") = %q, want "foo"`, q);
        }
 }
+
+type stringMap map[string]string
+type parseContentTypeTest struct {
+       contentType stringMap;
+       error bool;
+}
+
+var parseContentTypeTests = []parseContentTypeTest{
+       parseContentTypeTest{
+               contentType: stringMap{ "Content-Type": "text/plain" },
+       },
+       parseContentTypeTest{
+               contentType: stringMap{ "Content-Type": "" },
+       },
+       parseContentTypeTest{
+               contentType: stringMap{ "Content-Type": "text/plain; boundary=" },
+       },
+       parseContentTypeTest{
+               contentType: stringMap{ "Content-Type": "application/unknown" },
+               error: true,
+       },
+}
+
+func TestPostContentTypeParsing(t *testing.T) {
+       for i, test := range parseContentTypeTests {
+               req := &Request{
+                               Method: "POST",
+                               Header: test.contentType,
+                               Body: bytes.NewBufferString("body")
+               };
+               err := req.ParseForm();
+               if !test.error && err != nil {
+                       t.Errorf("test %d: Unexpected error: %v", i, err);
+               }
+               if test.error && err == nil {
+                       t.Errorf("test %d should have returned error", i);
+               }
+       }
+}