1
0
mirror of https://github.com/golang/go synced 2024-11-14 09:00:21 -07:00

net/http: support multipart/mixed in Request.MultipartReader

Fixes #23959

GitHub-Last-Rev: 08ce026f52
GitHub-Pull-Request: golang/go#24012
Change-Id: I7e71c41330346dbc4dad6ba813cabfa8a54e2f66
Reviewed-on: https://go-review.googlesource.com/95975
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
OneOfOne 2018-02-21 19:58:33 +00:00 committed by Brad Fitzpatrick
parent 669676b7b3
commit e02d6bb621
2 changed files with 15 additions and 5 deletions

View File

@ -411,7 +411,7 @@ var multipartByReader = &multipart.Form{
}
// MultipartReader returns a MIME multipart reader if this is a
// multipart/form-data POST request, else returns nil and an error.
// multipart/form-data or a multipart/mixed POST request, else returns nil and an error.
// Use this function instead of ParseMultipartForm to
// process the request body as a stream.
func (r *Request) MultipartReader() (*multipart.Reader, error) {
@ -422,16 +422,16 @@ func (r *Request) MultipartReader() (*multipart.Reader, error) {
return nil, errors.New("http: multipart handled by ParseMultipartForm")
}
r.MultipartForm = multipartByReader
return r.multipartReader()
return r.multipartReader(true)
}
func (r *Request) multipartReader() (*multipart.Reader, error) {
func (r *Request) multipartReader(allowMixed bool) (*multipart.Reader, error) {
v := r.Header.Get("Content-Type")
if v == "" {
return nil, ErrNotMultipart
}
d, params, err := mime.ParseMediaType(v)
if err != nil || d != "multipart/form-data" {
if err != nil || !(d == "multipart/form-data" || allowMixed && d == "multipart/mixed") {
return nil, ErrNotMultipart
}
boundary, ok := params["boundary"]
@ -1207,7 +1207,7 @@ func (r *Request) ParseMultipartForm(maxMemory int64) error {
return nil
}
mr, err := r.multipartReader()
mr, err := r.multipartReader(false)
if err != nil {
return err
}

View File

@ -143,6 +143,16 @@ func TestMultipartReader(t *testing.T) {
t.Errorf("expected multipart; error: %v", err)
}
req = &Request{
Method: "POST",
Header: Header{"Content-Type": {`multipart/mixed; boundary="foo123"`}},
Body: ioutil.NopCloser(new(bytes.Buffer)),
}
multipart, err = req.MultipartReader()
if multipart == nil {
t.Errorf("expected multipart; error: %v", err)
}
req.Header = Header{"Content-Type": {"text/plain"}}
multipart, err = req.MultipartReader()
if multipart != nil {