Movatterモバイル変換


[0]ホーム

URL:


multipart

packagestandard library
go1.25.4Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2025 License:BSD-3-ClauseImports:18Imported by:44,552

Details

Repository

cs.opensource.google/go/go

Links

Documentation

Overview

Package multipart implements MIME multipart parsing, as defined inRFC2046.

The implementation is sufficient for HTTP (RFC 2388) and the multipartbodies generated by popular browsers.

Limits

To protect against malicious inputs, this package sets limits on the sizeof the MIME data it processes.

Reader.NextPart andReader.NextRawPart limit the number of headers in apart to 10000 andReader.ReadForm limits the total number of headers in allFileHeaders to 10000.These limits may be adjusted with the GODEBUG=multipartmaxheaders=<values>setting.

Reader.ReadForm further limits the number of parts in a form to 1000.This limit may be adjusted with the GODEBUG=multipartmaxparts=<value>setting.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrMessageTooLarge =errors.New("multipart: message too large")

ErrMessageTooLarge is returned by ReadForm if the message formdata is too large to be processed.

Functions

funcFileContentDispositionadded ingo1.25.0

func FileContentDisposition(fieldname, filenamestring)string

FileContentDisposition returns the value of a Content-Disposition headerwith the provided field name and file name.

Types

typeFile

type File interface {io.Readerio.ReaderAtio.Seekerio.Closer}

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.

typeFileHeader

type FileHeader struct {FilenamestringHeadertextproto.MIMEHeaderSizeint64// 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 theFileHeader's associated File.

typeForm

type Form struct {Value map[string][]stringFile  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.

func (*Form)RemoveAll

func (f *Form) RemoveAll()error

RemoveAll removes any temporary files associated with aForm.

typePart

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"Headertextproto.MIMEHeader// contains filtered or unexported fields}

A Part represents a single part in a multipart body.

func (*Part)Close

func (p *Part) Close()error

func (*Part)FileName

func (p *Part) FileName()string

FileName returns the filename parameter of thePart's Content-Dispositionheader. If not empty, the filename is passed through filepath.Base (which isplatform dependent) before being returned.

func (*Part)FormName

func (p *Part) FormName()string

FormName returns the name parameter if p has a Content-Dispositionof type "form-data". Otherwise it returns the empty string.

func (*Part)Read

func (p *Part) Read(d []byte) (nint, errerror)

Read reads the body of a part, after its headers and before thenext part (if any) begins.

typeReader

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. Seekingisn't supported.

funcNewReader

func NewReader(rio.Reader, boundarystring) *Reader

NewReader creates a new multipartReader reading from r using thegiven MIME boundary.

The boundary is usually obtained from the "boundary" parameter ofthe message's "Content-Type" header. Usemime.ParseMediaType toparse such headers.

Example
package mainimport ("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

func (r *Reader) NextPart() (*Part,error)

NextPart returns the next part in the multipart or an error.When there are no more parts, the errorio.EOF is returned.

As a special case, if the "Content-Transfer-Encoding" headerhas a value of "quoted-printable", that header is insteadhidden and the body is transparently decoded during Read calls.

func (*Reader)NextRawPartadded ingo1.14

func (r *Reader) NextRawPart() (*Part,error)

NextRawPart returns the next part in the multipart or an error.When there are no more parts, the errorio.EOF is returned.

UnlikeReader.NextPart, it does not have special handling for"Content-Transfer-Encoding: quoted-printable".

func (*Reader)ReadForm

func (r *Reader) ReadForm(maxMemoryint64) (*Form,error)

ReadForm parses an entire multipart message whose parts havea 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 ondisk in temporary files.It returnsErrMessageTooLarge if all non-file parts can't be stored inmemory.

typeWriter

type Writer struct {// contains filtered or unexported fields}

A Writer generates multipart messages.

funcNewWriter

func NewWriter(wio.Writer) *Writer

NewWriter returns a new multipartWriter with a random boundary,writing to w.

func (*Writer)Boundary

func (w *Writer) Boundary()string

Boundary returns theWriter's boundary.

func (*Writer)Close

func (w *Writer) Close()error

Close finishes the multipart message and writes the trailingboundary end line to the output.

func (*Writer)CreateFormField

func (w *Writer) CreateFormField(fieldnamestring) (io.Writer,error)

CreateFormField callsWriter.CreatePart with a header using thegiven field name.

func (*Writer)CreateFormFile

func (w *Writer) CreateFormFile(fieldname, filenamestring) (io.Writer,error)

CreateFormFile is a convenience wrapper aroundWriter.CreatePart. It createsa new form-data header with the provided field name and file name.

func (*Writer)CreatePart

func (w *Writer) CreatePart(headertextproto.MIMEHeader) (io.Writer,error)

CreatePart creates a new multipart section with the providedheader. The body of the part should be written to the returnedWriter. After calling CreatePart, any previous part may no longerbe written to.

func (*Writer)FormDataContentType

func (w *Writer) FormDataContentType()string

FormDataContentType returns the Content-Type for an HTTPmultipart/form-data with thisWriter's Boundary.

func (*Writer)SetBoundaryadded ingo1.1

func (w *Writer) SetBoundary(boundarystring)error

SetBoundary overrides theWriter's default randomly-generatedboundary separator with an explicit value.

SetBoundary must be called before any parts are created, may onlycontain certain ASCII characters, and must be non-empty andat most 70 bytes long.

func (*Writer)WriteField

func (w *Writer) WriteField(fieldname, valuestring)error

WriteField callsWriter.CreateFormField and then writes the given value.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp