aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2018-06-26 16:21:56 -0700
committerIan Lance Taylor <iant@golang.org>2018-06-26 23:56:31 +0000
commitb5375d70d1d626595ec68568b68cc28dddc859d1 (patch)
tree5c7dcf976fc00468b58fd76108158c84c9706f77
parent3d59583836630cf13ec4bfbed977d27b1b7adbdc (diff)
downloadgo-b5375d70d1d626595ec68568b68cc28dddc859d1.tar.gz
go-b5375d70d1d626595ec68568b68cc28dddc859d1.zip
[release-branch.go1.10] mime/multipart: restore 1.9 handling of missing/empty form-data file name
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>
-rw-r--r--src/mime/multipart/formdata.go3
-rw-r--r--src/mime/multipart/formdata_test.go29
2 files changed, 26 insertions, 6 deletions
diff --git a/src/mime/multipart/formdata.go b/src/mime/multipart/formdata.go
index 2a4ebdd4a0..832d0ad693 100644
--- a/src/mime/multipart/formdata.go
+++ b/src/mime/multipart/formdata.go
@@ -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 {
diff --git a/src/mime/multipart/formdata_test.go b/src/mime/multipart/formdata_test.go
index 69333d3d0d..2d6a830cb6 100644
--- a/src/mime/multipart/formdata_test.go
+++ b/src/mime/multipart/formdata_test.go
@@ -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"