mime/multipart and HTTP multipart/form-data support

Somewhat of a work-in-progress (in that MIME is a large spec), but this is
functional and enough for discussion and/or code review.

In addition to the unit tests, I've tested with curl and Chrome with
a variety of test files, making sure the digests of files are unaltered
when read via a multipart Part.

R=rsc, adg, dsymonds1, agl1
CC=golang-dev
https://golang.org/cl/1681049
This commit is contained in:
Brad Fitzpatrick 2010-07-14 17:26:14 -07:00 committed by Russ Cox
parent e9bcbc5398
commit 9b64fef71a
11 changed files with 820 additions and 8 deletions

View file

@ -16,6 +16,8 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"os"
"strconv"
"strings"
@ -40,6 +42,8 @@ var (
ErrNotSupported = &ProtocolError{"feature not supported"}
ErrUnexpectedTrailer = &ProtocolError{"trailer header without chunked transfer encoding"}
ErrMissingContentLength = &ProtocolError{"missing ContentLength in HEAD response"}
ErrNotMultipart = &ProtocolError{"request Content-Type isn't multipart/form-data"}
ErrMissingBoundary = &ProtocolError{"no multipart boundary param Content-Type"}
)
type badStringError struct {
@ -139,6 +143,24 @@ func (r *Request) ProtoAtLeast(major, minor int) bool {
r.ProtoMajor == major && r.ProtoMinor >= minor
}
// MultipartReader returns a MIME multipart reader if this is a
// multipart/form-data POST request, else returns nil and an error.
func (r *Request) MultipartReader() (multipart.Reader, os.Error) {
v, ok := r.Header["Content-Type"]
if !ok {
return nil, ErrNotMultipart
}
d, params := mime.ParseMediaType(v)
if d != "multipart/form-data" {
return nil, ErrNotMultipart
}
boundary, ok := params["boundary"]
if !ok {
return nil, ErrMissingBoundary
}
return multipart.NewReader(r.Body, boundary), nil
}
// Return value if nonempty, def otherwise.
func valueOrDefault(value, def string) string {
if value != "" {