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
|
|
|
)
|
|
|
|
|
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.
|
2013-07-16 08:54:42 +02:00
|
|
|
// For example, "foo-bar" changes case to "Foo-Bar"
|
|
|
|
//
|
|
|
|
// As a special case, if the "Content-Transfer-Encoding" header
|
|
|
|
// has a value of "quoted-printable", that header is instead
|
|
|
|
// hidden from this map and the body is transparently decoded
|
|
|
|
// during Read calls.
|
2011-03-25 00:46:17 +01:00
|
|
|
Header textproto.MIMEHeader
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
buffer *bytes.Buffer
|
|
|
|
mr *Reader
|
|
|
|
bytesRead int
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
disposition string
|
|
|
|
dispositionParams map[string]string
|
2012-12-13 00:13:29 +01:00
|
|
|
|
|
|
|
// r is either a reader directly reading from mr, or it's a
|
|
|
|
// wrapper around such a reader, decoding the
|
|
|
|
// Content-Transfer-Encoding
|
|
|
|
r io.Reader
|
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")
|
2011-12-03 03:17:34 +01:00
|
|
|
var err error
|
2011-10-27 01:57:58 +02:00
|
|
|
p.disposition, p.dispositionParams, err = mime.ParseMediaType(v)
|
|
|
|
if err != nil {
|
2011-05-20 02:18:15 +02:00
|
|
|
p.dispositionParams = emptyParams
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
// NewReader creates a new multipart Reader reading from reader using the
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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-12-03 03:17:34 +01:00
|
|
|
func newPart(mr *Reader) (*Part, 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
|
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
bp.r = partReader{bp}
|
|
|
|
const cte = "Content-Transfer-Encoding"
|
|
|
|
if bp.Header.Get(cte) == "quoted-printable" {
|
|
|
|
bp.Header.Del(cte)
|
|
|
|
bp.r = newQuotedPrintableReader(bp.r)
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
return bp, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (bp *Part) populateHeaders() 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.
|
2012-02-09 09:19:58 +01:00
|
|
|
func (p *Part) Read(d []byte) (n int, err error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
return p.r.Read(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
// partReader implements io.Reader by reading raw bytes directly from the
|
|
|
|
// wrapped *Part, without doing any Transfer-Encoding decoding.
|
|
|
|
type partReader struct {
|
|
|
|
p *Part
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pr partReader) Read(d []byte) (n int, err error) {
|
|
|
|
p := pr.p
|
2012-06-25 18:20:03 +02:00
|
|
|
defer func() {
|
|
|
|
p.bytesRead += n
|
|
|
|
}()
|
2012-02-09 09:19:58 +01:00
|
|
|
if p.buffer.Len() >= len(d) {
|
2011-05-20 02:18:15 +02:00
|
|
|
// Internal buffer of unconsumed data is large enough for
|
|
|
|
// the read request. No need to parse more at the moment.
|
2012-02-09 09:19:58 +01:00
|
|
|
return p.buffer.Read(d)
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
peek, err := p.mr.bufReader.Peek(4096) // TODO(bradfitz): add buffer size accessor
|
2012-06-25 18:20:03 +02:00
|
|
|
|
|
|
|
// Look for an immediate empty part without a leading \r\n
|
|
|
|
// before the boundary separator. Some MIME code makes empty
|
|
|
|
// parts like this. Most browsers, however, write the \r\n
|
|
|
|
// before the subsequent boundary even for empty parts and
|
|
|
|
// won't hit this path.
|
|
|
|
if p.bytesRead == 0 && p.mr.peekBufferIsEmptyPart(peek) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
unexpectedEOF := err == io.EOF
|
|
|
|
if err != nil && !unexpectedEOF {
|
2011-05-20 02:18:15 +02:00
|
|
|
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
|
2012-02-09 09:19:58 +01:00
|
|
|
if idx := bytes.Index(peek, p.mr.nlDashBoundary); idx != -1 {
|
2011-05-20 02:18:15 +02:00
|
|
|
nCopy = idx
|
|
|
|
foundBoundary = true
|
2012-02-09 09:19:58 +01:00
|
|
|
} else if safeCount := len(peek) - len(p.mr.nlDashBoundary); safeCount > 0 {
|
2011-05-20 02:18:15 +02:00
|
|
|
nCopy = safeCount
|
2012-06-25 18:20:03 +02:00
|
|
|
} else if unexpectedEOF {
|
2011-05-20 02:18:15 +02:00
|
|
|
// 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 {
|
2012-02-09 09:19:58 +01:00
|
|
|
if _, err := io.CopyN(p.buffer, p.mr.bufReader, int64(nCopy)); err != nil {
|
2011-05-20 02:18:15 +02:00
|
|
|
return 0, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
n, err = p.buffer.Read(d)
|
2011-12-03 03:17:34 +01:00
|
|
|
if err == io.EOF && !foundBoundary {
|
2011-05-20 02:18:15 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func (p *Part) Close() error {
|
|
|
|
io.Copy(ioutil.Discard, p)
|
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
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
nl []byte // "\r\n" or "\n" (set after seeing first boundary line)
|
|
|
|
nlDashBoundary []byte // nl + "--boundary"
|
|
|
|
dashBoundaryDash []byte // "--boundary--"
|
|
|
|
dashBoundary []byte // "--boundary"
|
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.
|
2011-12-07 02:11:29 +01:00
|
|
|
// When there are no more parts, the error io.EOF is returned.
|
2012-02-09 09:19:58 +01:00
|
|
|
func (r *Reader) NextPart() (*Part, error) {
|
|
|
|
if r.currentPart != nil {
|
|
|
|
r.currentPart.Close()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
expectNewPart := false
|
2010-12-03 05:34:57 +01:00
|
|
|
for {
|
2012-02-09 09:19:58 +01:00
|
|
|
line, err := r.bufReader.ReadSlice('\n')
|
2012-06-25 18:20:03 +02:00
|
|
|
if err == io.EOF && r.isFinalBoundary(line) {
|
2012-05-04 17:01:11 +02:00
|
|
|
// If the buffer ends in "--boundary--" without the
|
|
|
|
// trailing "\r\n", ReadSlice will return an error
|
|
|
|
// (since it's missing the '\n'), but this is a valid
|
|
|
|
// multipart EOF so we need to return io.EOF instead of
|
|
|
|
// a fmt-wrapped one.
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("multipart: NextPart: %v", err)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
if r.isBoundaryDelimiterLine(line) {
|
|
|
|
r.partsRead++
|
|
|
|
bp, err := newPart(r)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
r.currentPart = bp
|
2010-12-03 05:34:57 +01:00
|
|
|
return bp, nil
|
|
|
|
}
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
if r.isFinalBoundary(line) {
|
2011-05-20 02:18:15 +02:00
|
|
|
// Expected EOF
|
2011-12-03 03:17:34 +01:00
|
|
|
return nil, io.EOF
|
2011-05-20 02:18:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if expectNewPart {
|
|
|
|
return nil, fmt.Errorf("multipart: expecting a new Part; got line %q", string(line))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
if r.partsRead == 0 {
|
2010-12-03 05:34:57 +01:00
|
|
|
// 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)
|
2012-02-09 09:19:58 +01:00
|
|
|
if bytes.Equal(line, r.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
// isFinalBoundary returns whether line is the final boundary line
|
2013-07-16 08:54:42 +02:00
|
|
|
// indicating that all parts are over.
|
2012-06-25 18:20:03 +02:00
|
|
|
// It matches `^--boundary--[ \t]*(\r\n)?$`
|
|
|
|
func (mr *Reader) isFinalBoundary(line []byte) bool {
|
|
|
|
if !bytes.HasPrefix(line, mr.dashBoundaryDash) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rest := line[len(mr.dashBoundaryDash):]
|
|
|
|
rest = skipLWSPChar(rest)
|
|
|
|
return len(rest) == 0 || bytes.Equal(rest, mr.nl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mr *Reader) isBoundaryDelimiterLine(line []byte) (ret 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
|
|
|
|
}
|
2012-06-25 18:20:03 +02:00
|
|
|
rest := line[len(mr.dashBoundary):]
|
|
|
|
rest = skipLWSPChar(rest)
|
|
|
|
|
|
|
|
// On the first part, see our lines are ending in \n instead of \r\n
|
|
|
|
// and switch into that mode if so. This is a violation of the spec,
|
|
|
|
// but occurs in practice.
|
|
|
|
if mr.partsRead == 0 && len(rest) == 1 && rest[0] == '\n' {
|
|
|
|
mr.nl = mr.nl[1:]
|
|
|
|
mr.nlDashBoundary = mr.nlDashBoundary[1:]
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-06-25 18:20:03 +02:00
|
|
|
return bytes.Equal(rest, mr.nl)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
// peekBufferIsEmptyPart returns whether the provided peek-ahead
|
|
|
|
// buffer represents an empty part. This is only called if we've not
|
|
|
|
// already read any bytes in this part and checks for the case of MIME
|
|
|
|
// software not writing the \r\n on empty parts. Some does, some
|
|
|
|
// doesn't.
|
|
|
|
//
|
|
|
|
// This checks that what follows the "--boundary" is actually the end
|
|
|
|
// ("--boundary--" with optional whitespace) or optional whitespace
|
|
|
|
// and then a newline, so we don't catch "--boundaryFAKE", in which
|
|
|
|
// case the whole line is part of the data.
|
|
|
|
func (mr *Reader) peekBufferIsEmptyPart(peek []byte) bool {
|
|
|
|
// End of parts case.
|
|
|
|
// Test whether peek matches `^--boundary--[ \t]*(?:\r\n|$)`
|
|
|
|
if bytes.HasPrefix(peek, mr.dashBoundaryDash) {
|
|
|
|
rest := peek[len(mr.dashBoundaryDash):]
|
|
|
|
rest = skipLWSPChar(rest)
|
|
|
|
return bytes.HasPrefix(rest, mr.nl) || len(rest) == 0
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-06-25 18:20:03 +02:00
|
|
|
if !bytes.HasPrefix(peek, mr.dashBoundary) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Test whether rest matches `^[ \t]*\r\n`)
|
|
|
|
rest := peek[len(mr.dashBoundary):]
|
|
|
|
rest = skipLWSPChar(rest)
|
|
|
|
return bytes.HasPrefix(rest, mr.nl)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-06-25 18:20:03 +02:00
|
|
|
// skipLWSPChar returns b with leading spaces and tabs removed.
|
|
|
|
// RFC 822 defines:
|
|
|
|
// LWSP-char = SPACE / HTAB
|
|
|
|
func skipLWSPChar(b []byte) []byte {
|
|
|
|
for len(b) > 0 && (b[0] == ' ' || b[0] == '\t') {
|
|
|
|
b = b[1:]
|
|
|
|
}
|
|
|
|
return b
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|