]> Cypherpunks repositories - gostls13.git/commitdiff
mime/multipart: test for presence of filename instead of content-type
authorHenry Clifford <h.a.clifford@gmail.com>
Fri, 9 Mar 2018 22:28:55 +0000 (22:28 +0000)
committerAndrew Bonventre <andybons@golang.org>
Sat, 10 Mar 2018 00:33:10 +0000 (00:33 +0000)
Fixes #24041

Preserving the intended fix in https://go.googlesource.com/go/+/81ec7256072ed5e20b8827c583193258769aebc0

Change-Id: I600d3d7edc74ca072a066739e2ef3235877d808f
GitHub-Last-Rev: 14973d7c2bf4022c28ef4b4984ec3cd352c1d4d8
GitHub-Pull-Request: golang/go#24104
Reviewed-on: https://go-review.googlesource.com/96975
Reviewed-by: Andrew Bonventre <andybons@golang.org>
Run-TryBot: Andrew Bonventre <andybons@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/mime/multipart/formdata.go
src/mime/multipart/formdata_test.go
src/mime/multipart/multipart.go

index 2a4ebdd4a06b8bf263280af82c967db5a094c0f4..22e2c8d32340c193751391d9de88fae8011202e8 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 !p.hasFileName() {
                        // value, store as string in memory
                        n, err := io.CopyN(&b, p, maxValueBytes+1)
                        if err != nil && err != io.EOF {
index 69333d3d0ded391d07cdb7829b05262927354470..5e3c3330f34143c5a94c2245d3fa5ac7a1f72923 100644 (file)
@@ -55,6 +55,21 @@ func TestReadFormWithNamelessFile(t *testing.T) {
 
 }
 
+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 {
        if fh.Filename != efn {
                t.Errorf("filename = %q, want %q", fh.Filename, efn)
@@ -94,6 +109,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"
index 19548081769d6dffbe55e63fb19cb200a4e9fc98..06fa736f9599198867009d6a4dfb2c5294596e4c 100644 (file)
@@ -81,6 +81,16 @@ func (p *Part) FileName() string {
        return p.dispositionParams["filename"]
 }
 
+// hasFileName determines if a (empty or otherwise)
+// filename parameter was included in the Content-Disposition header
+func (p *Part) hasFileName() bool {
+       if p.dispositionParams == nil {
+               p.parseContentDisposition()
+       }
+       _, ok := p.dispositionParams["filename"]
+       return ok
+}
+
 func (p *Part) parseContentDisposition() {
        v := p.Header.Get("Content-Disposition")
        var err error