aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/http/request.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <brad@danga.com>2010-07-14 17:26:14 -0700
committerRuss Cox <rsc@golang.org>2010-07-14 17:26:14 -0700
commit9b64fef71acf71175bc74e2d04ea7fc0a011b03b (patch)
treef3921ff64154fc3d50cbb810e255d10ad44d5634 /src/pkg/http/request.go
parente9bcbc539890020668cbd361a0d8edbb8f6ab8a1 (diff)
downloadgo-9b64fef71acf71175bc74e2d04ea7fc0a011b03b.tar.xz
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
Diffstat (limited to 'src/pkg/http/request.go')
-rw-r--r--src/pkg/http/request.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/pkg/http/request.go b/src/pkg/http/request.go
index 8a72d6cfad..a6836856d8 100644
--- a/src/pkg/http/request.go
+++ b/src/pkg/http/request.go
@@ -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 != "" {