]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.10] mime/multipart: restore 1.9 handling of missing/empty form...
authorIan Lance Taylor <iant@golang.org>
Tue, 26 Jun 2018 23:21:56 +0000 (16:21 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 26 Jun 2018 23:56:31 +0000 (23:56 +0000)
Revert the code change of CL 70931, but keep the test, appropriately
modified for the code changes. Also add a new test. This restores the
1.9 handling of form-data entries with missing or empty file names.

Changing the handling of this simply confused existing programs for no
useful benefit. Go back to the old behavior.

Updates #19183
Fixes #24041

Change-Id: Ie7a0309a061218ceda3bbc2a7da85e6fb3dd016d
Reviewed-on: https://go-review.googlesource.com/121075
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/mime/multipart/formdata.go
src/mime/multipart/formdata_test.go

index 2a4ebdd4a06b8bf263280af82c967db5a094c0f4..832d0ad693666605cfdeea6d07c9ebe548ee6ee5 100644 (file)
@@ -58,8 +58,7 @@ func (r *Reader) readForm(maxMemory int64) (_ *Form, err error) {
 
                var b bytes.Buffer
 
-               _, hasContentTypeHeader := p.Header["Content-Type"]
-               if !hasContentTypeHeader && filename == "" {
+               if filename == "" {
                        // value, store as string in memory
                        n, err := io.CopyN(&b, p, maxValueBytes+1)
                        if err != nil && err != io.EOF {
index 69333d3d0ded391d07cdb7829b05262927354470..2d6a830cb669b791999aa98ef603272aa806bb6c 100644 (file)
@@ -47,12 +47,24 @@ func TestReadFormWithNamelessFile(t *testing.T) {
        }
        defer f.RemoveAll()
 
-       fd := testFile(t, f.File["hiddenfile"][0], "", filebContents)
-       if _, ok := fd.(sectionReadCloser); !ok {
-               t.Errorf("file has unexpected underlying type %T", fd)
+       if g, e := f.Value["hiddenfile"][0], filebContents; g != e {
+               t.Errorf("hiddenfile value = %q, want %q", g, e)
        }
-       fd.Close()
+}
 
+func TestReadFormWithTextContentType(t *testing.T) {
+       // From https://github.com/golang/go/issues/24041
+       b := strings.NewReader(strings.Replace(messageWithTextContentType, "\n", "\r\n", -1))
+       r := NewReader(b, boundary)
+       f, err := r.ReadForm(25)
+       if err != nil {
+               t.Fatal("ReadForm:", err)
+       }
+       defer f.RemoveAll()
+
+       if g, e := f.Value["texta"][0], textaValue; g != e {
+               t.Errorf("texta value = %q, want %q", g, e)
+       }
 }
 
 func testFile(t *testing.T, fh *FileHeader, efn, econtent string) File {
@@ -94,6 +106,15 @@ Content-Type: text/plain
 --MyBoundary--
 `
 
+const messageWithTextContentType = `
+--MyBoundary
+Content-Disposition: form-data; name="texta"
+Content-Type: text/plain
+
+` + textaValue + `
+--MyBoundary
+`
+
 const message = `
 --MyBoundary
 Content-Disposition: form-data; name="filea"; filename="filea.txt"