2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
//
|
|
|
|
|
|
|
|
/*
|
|
|
|
Package multipart implements MIME multipart parsing, as defined in RFC
|
|
|
|
2046.
|
|
|
|
|
|
|
|
The implementation is sufficient for HTTP (RFC 2388) and the multipart
|
|
|
|
bodies generated by popular browsers.
|
|
|
|
*/
|
|
|
|
package multipart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2011-05-20 02:18:15 +02:00
|
|
|
"fmt"
|
2010-12-03 05:34:57 +01:00
|
|
|
"io"
|
2011-05-20 02:18:15 +02:00
|
|
|
"io/ioutil"
|
2010-12-03 05:34:57 +01:00
|
|
|
"mime"
|
2011-03-25 00:46:17 +01:00
|
|
|
"net/textproto"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// TODO(bradfitz): inline these once the compiler can inline them in
|
|
|
|
// read-only situation (such as bytes.HasSuffix)
|
|
|
|
var lf = []byte("\n")
|
|
|
|
var crlf = []byte("\r\n")
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
var emptyParams = make(map[string]string)
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// A Part represents a single part in a multipart body.
|
|
|
|
type Part struct {
|
|
|
|
// The headers of the body, if any, with the keys canonicalized
|
|
|
|
// in the same fashion that the Go http.Request headers are.
|
|
|
|
// i.e. "foo-bar" changes case to "Foo-Bar"
|
2011-03-25 00:46:17 +01:00
|
|
|
Header textproto.MIMEHeader
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
buffer *bytes.Buffer
|
2011-09-16 17:47:21 +02:00
|
|
|
mr *Reader
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
disposition string
|
|
|
|
dispositionParams map[string]string
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormName returns the name parameter if p has a Content-Disposition
|
|
|
|
// of type "form-data". Otherwise it returns the empty string.
|
|
|
|
func (p *Part) FormName() string {
|
|
|
|
// See http://tools.ietf.org/html/rfc2183 section 2 for EBNF
|
|
|
|
// of Content-Disposition value format.
|
2011-05-20 02:18:15 +02:00
|
|
|
if p.dispositionParams == nil {
|
|
|
|
p.parseContentDisposition()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if p.disposition != "form-data" {
|
2010-12-03 05:34:57 +01:00
|
|
|
return ""
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
return p.dispositionParams["name"]
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileName returns the filename parameter of the Part's
|
|
|
|
// Content-Disposition header.
|
|
|
|
func (p *Part) FileName() string {
|
|
|
|
if p.dispositionParams == nil {
|
|
|
|
p.parseContentDisposition()
|
|
|
|
}
|
|
|
|
return p.dispositionParams["filename"]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Part) parseContentDisposition() {
|
|
|
|
v := p.Header.Get("Content-Disposition")
|
|
|
|
p.disposition, p.dispositionParams = mime.ParseMediaType(v)
|
|
|
|
if p.dispositionParams == nil {
|
|
|
|
p.dispositionParams = emptyParams
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewReader creates a new multipart Reader reading from r using the
|
|
|
|
// given MIME boundary.
|
2011-09-16 17:47:21 +02:00
|
|
|
func NewReader(reader io.Reader, boundary string) *Reader {
|
2011-05-20 02:18:15 +02:00
|
|
|
b := []byte("\r\n--" + boundary + "--")
|
2011-09-16 17:47:21 +02:00
|
|
|
return &Reader{
|
2011-05-20 02:18:15 +02:00
|
|
|
bufReader: bufio.NewReader(reader),
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
nl: b[:2],
|
2011-05-20 02:18:15 +02:00
|
|
|
nlDashBoundary: b[:len(b)-2],
|
|
|
|
dashBoundaryDash: b[2:],
|
|
|
|
dashBoundary: b[2 : len(b)-2],
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func newPart(mr *Reader) (*Part, os.Error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
bp := &Part{
|
|
|
|
Header: make(map[string][]string),
|
|
|
|
mr: mr,
|
|
|
|
buffer: new(bytes.Buffer),
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if err := bp.populateHeaders(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return bp, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *Part) populateHeaders() os.Error {
|
2011-09-16 17:47:21 +02:00
|
|
|
r := textproto.NewReader(bp.mr.bufReader)
|
|
|
|
header, err := r.ReadMIMEHeader()
|
|
|
|
if err == nil {
|
|
|
|
bp.Header = header
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads the body of a part, after its headers and before the
|
|
|
|
// next part (if any) begins.
|
|
|
|
func (bp *Part) Read(p []byte) (n int, err os.Error) {
|
2011-05-20 02:18:15 +02:00
|
|
|
if bp.buffer.Len() >= len(p) {
|
|
|
|
// Internal buffer of unconsumed data is large enough for
|
|
|
|
// the read request. No need to parse more at the moment.
|
|
|
|
return bp.buffer.Read(p)
|
|
|
|
}
|
|
|
|
peek, err := bp.mr.bufReader.Peek(4096) // TODO(bradfitz): add buffer size accessor
|
|
|
|
unexpectedEof := err == os.EOF
|
|
|
|
if err != nil && !unexpectedEof {
|
|
|
|
return 0, fmt.Errorf("multipart: Part Read: %v", err)
|
|
|
|
}
|
|
|
|
if peek == nil {
|
|
|
|
panic("nil peek buf")
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
// Search the peek buffer for "\r\n--boundary". If found,
|
|
|
|
// consume everything up to the boundary. If not, consume only
|
|
|
|
// as much of the peek buffer as cannot hold the boundary
|
|
|
|
// string.
|
|
|
|
nCopy := 0
|
|
|
|
foundBoundary := false
|
|
|
|
if idx := bytes.Index(peek, bp.mr.nlDashBoundary); idx != -1 {
|
|
|
|
nCopy = idx
|
|
|
|
foundBoundary = true
|
|
|
|
} else if safeCount := len(peek) - len(bp.mr.nlDashBoundary); safeCount > 0 {
|
|
|
|
nCopy = safeCount
|
|
|
|
} else if unexpectedEof {
|
|
|
|
// If we've run out of peek buffer and the boundary
|
|
|
|
// wasn't found (and can't possibly fit), we must have
|
|
|
|
// hit the end of the file unexpectedly.
|
|
|
|
return 0, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
if nCopy > 0 {
|
|
|
|
if _, err := io.Copyn(bp.buffer, bp.mr.bufReader, int64(nCopy)); err != nil {
|
|
|
|
return 0, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
n, err = bp.buffer.Read(p)
|
|
|
|
if err == os.EOF && !foundBoundary {
|
|
|
|
// If the boundary hasn't been reached there's more to
|
|
|
|
// read, so don't pass through an EOF from the buffer
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bp *Part) Close() os.Error {
|
2011-05-20 02:18:15 +02:00
|
|
|
io.Copy(ioutil.Discard, bp)
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Reader is an iterator over parts in a MIME multipart body.
|
|
|
|
// Reader's underlying parser consumes its input as needed. Seeking
|
|
|
|
// isn't supported.
|
|
|
|
type Reader struct {
|
2011-05-20 02:18:15 +02:00
|
|
|
bufReader *bufio.Reader
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
currentPart *Part
|
|
|
|
partsRead int
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
nl, nlDashBoundary, dashBoundaryDash, dashBoundary []byte
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// NextPart returns the next part in the multipart or an error.
|
|
|
|
// When there are no more parts, the error os.EOF is returned.
|
|
|
|
func (mr *Reader) NextPart() (*Part, os.Error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if mr.currentPart != nil {
|
|
|
|
mr.currentPart.Close()
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
expectNewPart := false
|
2010-12-03 05:34:57 +01:00
|
|
|
for {
|
2011-05-20 02:18:15 +02:00
|
|
|
line, err := mr.bufReader.ReadSlice('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("multipart: NextPart: %v", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
if mr.isBoundaryDelimiterLine(line) {
|
2010-12-03 05:34:57 +01:00
|
|
|
mr.partsRead++
|
|
|
|
bp, err := newPart(mr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mr.currentPart = bp
|
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
if hasPrefixThenNewline(line, mr.dashBoundaryDash) {
|
|
|
|
// Expected EOF
|
|
|
|
return nil, os.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectNewPart {
|
|
|
|
return nil, fmt.Errorf("multipart: expecting a new Part; got line %q", string(line))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if mr.partsRead == 0 {
|
|
|
|
// skip line
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Consume the "\n" or "\r\n" separator between the
|
|
|
|
// body of the previous part and the boundary line we
|
|
|
|
// now expect will follow. (either a new part or the
|
|
|
|
// end boundary)
|
|
|
|
if bytes.Equal(line, mr.nl) {
|
2011-05-20 02:18:15 +02:00
|
|
|
expectNewPart = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("multipart: unexpected line in Next(): %q", line)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func (mr *Reader) isBoundaryDelimiterLine(line []byte) bool {
|
2010-12-03 05:34:57 +01:00
|
|
|
// http://tools.ietf.org/html/rfc2046#section-5.1
|
|
|
|
// The boundary delimiter line is then defined as a line
|
|
|
|
// consisting entirely of two hyphen characters ("-",
|
|
|
|
// decimal value 45) followed by the boundary parameter
|
|
|
|
// value from the Content-Type header field, optional linear
|
|
|
|
// whitespace, and a terminating CRLF.
|
2011-05-20 02:18:15 +02:00
|
|
|
if !bytes.HasPrefix(line, mr.dashBoundary) {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
if bytes.HasSuffix(line, mr.nl) {
|
|
|
|
return onlyHorizontalWhitespace(line[len(mr.dashBoundary) : len(line)-len(mr.nl)])
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
// Violate the spec and also support newlines without the
|
|
|
|
// carriage return...
|
2011-09-16 17:47:21 +02:00
|
|
|
if mr.partsRead == 0 && bytes.HasSuffix(line, lf) {
|
|
|
|
if onlyHorizontalWhitespace(line[len(mr.dashBoundary) : len(line)-1]) {
|
|
|
|
mr.nl = mr.nl[1:]
|
|
|
|
mr.nlDashBoundary = mr.nlDashBoundary[1:]
|
|
|
|
return true
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func onlyHorizontalWhitespace(s []byte) bool {
|
|
|
|
for _, b := range s {
|
|
|
|
if b != ' ' && b != '\t' {
|
2010-12-03 05:34:57 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
func hasPrefixThenNewline(s, prefix []byte) bool {
|
|
|
|
return bytes.HasPrefix(s, prefix) &&
|
|
|
|
(len(s) == len(prefix)+1 && s[len(s)-1] == '\n' ||
|
2011-09-16 17:47:21 +02:00
|
|
|
len(s) == len(prefix)+2 && bytes.HasSuffix(s, crlf))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|