Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

multifilereader: fix multipart/form-data #8

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 10 additions & 6 deletions multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type MultiFileReader struct {
files []DirIterator
path []string

currentFile Node
buf bytes.Buffer
mpWriter *multipart.Writer
closed bool
mutex *sync.Mutex
currentFile Node
buf bytes.Buffer
mpWriter *multipart.Writer
closed bool
mutex *sync.Mutex
fieldCounter int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go fmt ./...


// if true, the data will be type 'multipart/form-data'
// if false, the data will be type 'multipart/mixed'
Expand Down Expand Up @@ -88,7 +89,10 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
// write the boundary and headers
header := make(textproto.MIMEHeader)
filename := url.QueryEscape(path.Join(path.Join(mfr.path...), entry.Name()))
header.Set("Content-Disposition", fmt.Sprintf("file; filename=\"%s\"", filename))
name := fmt.Sprintf("data%d", mfr.fieldCounter)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do browser set this to when user picks multiple files in file input? Can one name repeat?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://tools.ietf.org/html/rfc7578#section-4.3

To match widely deployed implementations, multiple files MUST be sent by supplying each file in a separate part but all with the same "name" parameter.

I suppose we want to mimic a form with multiple upload from the same field, so I should change that.

mfr.fieldCounter++
header.Set("Content-Disposition",
fmt.Sprintf("form-data; name=\"%s\"; filename=\"%s\"", name, filename))

var contentType string

Expand Down