diff --git a/formstream.go b/formstream.go index fe30475..311fab7 100644 --- a/formstream.go +++ b/formstream.go @@ -57,24 +57,32 @@ const ( defaultMaxMemFileSize = 32 * MB ) +// WithMaxParts sets the maximum number of parts to be parsed. +// default: 10000 func WithMaxParts(maxParts uint) ParserOption { return func(c *parserConfig) { c.maxParts = maxParts } } +// WithMaxHeaders sets the maximum number of headers to be parsed. +// default: 10000 func WithMaxHeaders(maxHeaders uint) ParserOption { return func(c *parserConfig) { c.maxHeaders = maxHeaders } } +// WithMaxMemSize sets the maximum memory size to be used for parsing. +// default: 32MB func WithMaxMemSize(maxMemSize DataSize) ParserOption { return func(c *parserConfig) { c.maxMemSize = maxMemSize } } +// WithMaxMemFileSize sets the maximum memory size to be used for parsing a file. +// default: 32MB func WithMaxMemFileSize(maxMemFileSize DataSize) ParserOption { return func(c *parserConfig) { c.maxMemFileSize = maxMemFileSize @@ -86,10 +94,12 @@ type Value struct { header Header } +// Unwrap returns the content and header of the value. func (v Value) Unwrap() (string, Header) { return string(v.content), v.header } +// UnwrapRaw returns the raw content and header of the value. func (v Value) UnwrapRaw() ([]byte, Header) { return v.content, v.header } @@ -112,18 +122,26 @@ func newHeader(h textproto.MIMEHeader) Header { } } +// Get returns the first value associated with the given key. +// If there are no values associated with the key, Get returns "". func (h Header) Get(key string) string { return h.header.Get(key) } +// ContentType returns the value of the "Content-Type" header field. +// If there are no values associated with the key, ContentType returns "". func (h Header) ContentType() string { return h.header.Get("Content-Type") } +// Name returns the value of the "name" parameter in the "Content-Disposition" header field. +// If there are no values associated with the key, Name returns "". func (h Header) Name() string { return h.dispositionParams["name"] } +// FileName returns the value of the "filename" parameter in the "Content-Disposition" header field. +// If there are no values associated with the key, FileName returns "". func (h Header) FileName() string { return h.dispositionParams["filename"] } diff --git a/formstream_test.go b/formstream_test.go index ca6b40c..1e7aa95 100644 --- a/formstream_test.go +++ b/formstream_test.go @@ -118,8 +118,10 @@ func createSampleForm(w io.Writer, fileSize formstream.DataSize, boundary string if err != nil { return fmt.Errorf("failed to create part: %w", err) } + + mbData := make([]byte, formstream.MB) for i := 0; i < int(fileSize/formstream.MB); i++ { - _, err := pw.Write([]byte(strings.Repeat("a", int(formstream.MB)))) + _, err := pw.Write(mbData) if err != nil { return fmt.Errorf("failed to write: %w", err) } diff --git a/parse.go b/parse.go index 5d4b478..51387bb 100644 --- a/parse.go +++ b/parse.go @@ -21,6 +21,7 @@ var ( ErrTooLargeForm = errors.New("too large form") ) +// Parse parses the multipart form from r. func (p *Parser) Parse(r io.Reader) (err error) { hsc := newHookSatisfactionChecker(p.hookMap, &p.parserConfig) defer func() { diff --git a/register.go b/register.go index ae2399b..2e726a5 100644 --- a/register.go +++ b/register.go @@ -4,6 +4,7 @@ import ( "fmt" ) +// Register registers a stream hook with the given name. func (p *Parser) Register(name string, fn StreamHookFunc, options ...RegisterOption) error { if _, ok := p.hookMap[name]; ok { return DuplicateHookNameError{Name: name} @@ -36,6 +37,7 @@ type registerConfig struct { type RegisterOption func(*registerConfig) +// WithRequiredPart sets the required part names for the stream hook. func WithRequiredPart(name string) RegisterOption { return func(c *registerConfig) { c.requireParts = append(c.requireParts, name)