diff --git a/pkg/blobserver/handlers/upload.go b/pkg/blobserver/handlers/upload.go index b0bb6d72c..9f6527d97 100644 --- a/pkg/blobserver/handlers/upload.go +++ b/pkg/blobserver/handlers/upload.go @@ -177,7 +177,7 @@ func handleMultiPartUpload(rw http.ResponseWriter, req *http.Request, blobReceiv receivedBlobs := make([]blob.SizedRef, 0, 10) - multipart, err := req.MultipartReader() + multipart, err := httputil.MultipartReader(req) if multipart == nil { httputil.BadRequestError(rw, fmt.Sprintf( "Expected multipart/form-data POST request; %v", err)) diff --git a/pkg/httputil/multipart.go b/pkg/httputil/multipart.go new file mode 100644 index 000000000..94431f385 --- /dev/null +++ b/pkg/httputil/multipart.go @@ -0,0 +1,59 @@ +/* +Copyright 2015 The Camlistore Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This file provides an alternative function to the stdlib's (r +// *http.Request).MultiMultipartReader(), because we want to obtain a multipart +// Reader from the vendored "future/mime/multipart" instead of from the stdlib's +// "mime/multipart". + +package httputil + +import ( + "future/mime/multipart" // vendored copy of Go tip "mime/multipart" + "mime" + "net/http" +) + +// TODO(mpl, bradfitz): remove that whole file once we depend on Go 1.6 +// See https://camlistore.org/issue/644 + +// MultipartReader returns a MIME multipart reader if this is a +// multipart/form-data POST request, else returns nil and an error. +// Use this function instead of ParseMultipartForm to +// process the request body as a stream. +func MultipartReader(r *http.Request) (*multipart.Reader, error) { + _, err := r.MultipartReader() + if err != nil { + return nil, err + } + return multipartReader(r) +} + +func multipartReader(r *http.Request) (*multipart.Reader, error) { + v := r.Header.Get("Content-Type") + if v == "" { + return nil, http.ErrNotMultipart + } + d, params, err := mime.ParseMediaType(v) + if err != nil || d != "multipart/form-data" { + return nil, http.ErrNotMultipart + } + boundary, ok := params["boundary"] + if !ok { + return nil, http.ErrMissingBoundary + } + return multipart.NewReader(r.Body, boundary), nil +} diff --git a/pkg/server/uploadhelper.go b/pkg/server/uploadhelper.go index a9fec2e3c..2d76752c8 100644 --- a/pkg/server/uploadhelper.go +++ b/pkg/server/uploadhelper.go @@ -46,7 +46,7 @@ func (ui *UIHandler) serveUploadHelper(rw http.ResponseWriter, req *http.Request return } - mr, err := req.MultipartReader() + mr, err := httputil.MultipartReader(req) if err != nil { httputil.ServeJSONError(rw, httputil.ServerError("reading body: "+err.Error())) return