Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/mime/multipart/formdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions src/mime/multipart/formdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 10 additions & 0 deletions src/mime/multipart/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down