Documentation ¶
Overview ¶
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.
Index ¶
- Variables
- type File
- type FileHeader
- type Form
- type Part
- type Reader
- type Writer
- func (w *Writer) Boundary() string
- func (w *Writer) Close() error
- func (w *Writer) CreateFormField(fieldname string) (io.Writer, error)
- func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error)
- func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error)
- func (w *Writer) FormDataContentType() string
- func (w *Writer) SetBoundary(boundary string) error
- func (w *Writer) WriteField(fieldname, value string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrMessageTooLarge = errors.New("multipart: message too large")
ErrMessageTooLarge is returned by ReadForm if the message form data is too large to be processed.
Functions ¶
This section is empty.
Types ¶
type File ¶
File is an interface to access the file part of a multipart message. Its contents may be either stored in memory or on disk. If stored on disk, the File's underlying concrete type will be an *os.File.
type FileHeader ¶
type FileHeader struct { Filename string Header textproto.MIMEHeader Size int64 // contains filtered or unexported fields }
A FileHeader describes a file part of a multipart request.
func (*FileHeader) Open ¶
func (fh *FileHeader) Open() (File, error)
Open opens and returns the FileHeader's associated File.
type Form ¶
type Form struct { Value map[string][]string File map[string][]*FileHeader }
Form is a parsed multipart form. Its File parts are stored either in memory or on disk, and are accessible via the *FileHeader's Open method. Its Value parts are stored as strings. Both are keyed by field name.
type Part ¶
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. // For example, "foo-bar" changes case to "Foo-Bar" Header textproto.MIMEHeader // contains filtered or unexported fields }
A Part represents a single part in a multipart body.
func (*Part) FileName ¶
FileName returns the filename parameter of the Part's Content-Disposition header. If not empty, the filename is passed through filepath.Base (which is platform dependent) before being returned.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader is an iterator over parts in a MIME multipart body. Reader's underlying parser consumes its input as needed. Seeking isn't supported.
func NewReader ¶
NewReader creates a new multipart Reader reading from r using the given MIME boundary.
The boundary is usually obtained from the "boundary" parameter of the message's "Content-Type" header. Use mime.ParseMediaType to parse such headers.
Example ¶
package main import ( "fmt" "io" "log" "mime" "mime/multipart" "net/mail" "strings" ) func main() { msg := &mail.Message{ Header: map[string][]string{ "Content-Type": {"multipart/mixed; boundary=foo"}, }, Body: strings.NewReader( "--foo\r\nFoo: one\r\n\r\nA section\r\n" + "--foo\r\nFoo: two\r\n\r\nAnd another\r\n" + "--foo--\r\n"), } mediaType, params, err := mime.ParseMediaType(msg.Header.Get("Content-Type")) if err != nil { log.Fatal(err) } if strings.HasPrefix(mediaType, "multipart/") { mr := multipart.NewReader(msg.Body, params["boundary"]) for { p, err := mr.NextPart() if err == io.EOF { return } if err != nil { log.Fatal(err) } slurp, err := io.ReadAll(p) if err != nil { log.Fatal(err) } fmt.Printf("Part %q: %q\n", p.Header.Get("Foo"), slurp) } } }
Output: Part "one": "A section" Part "two": "And another"
func (*Reader) NextPart ¶
NextPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned.
As a special case, if the "Content-Transfer-Encoding" header has a value of "quoted-printable", that header is instead hidden and the body is transparently decoded during Read calls.
func (*Reader) NextRawPart ¶
NextRawPart returns the next part in the multipart or an error. When there are no more parts, the error io.EOF is returned.
Unlike NextPart, it does not have special handling for "Content-Transfer-Encoding: quoted-printable".
func (*Reader) ReadForm ¶
ReadForm parses an entire multipart message whose parts have a Content-Disposition of "form-data". It stores up to maxMemory bytes + 10MB (reserved for non-file parts) in memory. File parts which can't be stored in memory will be stored on disk in temporary files. It returns ErrMessageTooLarge if all non-file parts can't be stored in memory.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
A Writer generates multipart messages.
func (*Writer) Close ¶
Close finishes the multipart message and writes the trailing boundary end line to the output.
func (*Writer) CreateFormField ¶
CreateFormField calls CreatePart with a header using the given field name.
func (*Writer) CreateFormFile ¶
CreateFormFile is a convenience wrapper around CreatePart. It creates a new form-data header with the provided field name and file name.
func (*Writer) CreatePart ¶
CreatePart creates a new multipart section with the provided header. The body of the part should be written to the returned Writer. After calling CreatePart, any previous part may no longer be written to.
func (*Writer) FormDataContentType ¶
FormDataContentType returns the Content-Type for an HTTP multipart/form-data with this Writer's Boundary.
func (*Writer) SetBoundary ¶
SetBoundary overrides the Writer's default randomly-generated boundary separator with an explicit value.
SetBoundary must be called before any parts are created, may only contain certain ASCII characters, and must be non-empty and at most 70 bytes long.
func (*Writer) WriteField ¶
WriteField calls CreateFormField and then writes the given value.