-
Notifications
You must be signed in to change notification settings - Fork 3
/
formstream.go
154 lines (129 loc) · 3.52 KB
/
formstream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package formstream
import (
"io"
"mime"
"net/textproto"
)
type Parser struct {
boundary string
valueMap map[string][]Value
hookMap map[string]streamHook
parserConfig
}
func NewParser(boundary string, options ...ParserOption) *Parser {
c := parserConfig{
maxParts: defaultMaxParts,
maxHeaders: defaultMaxHeaders,
maxMemSize: defaultMaxMemSize,
maxMemFileSize: defaultMaxMemFileSize,
}
for _, opt := range options {
opt(&c)
}
return &Parser{
boundary: boundary,
valueMap: make(map[string][]Value),
hookMap: make(map[string]streamHook),
parserConfig: c,
}
}
type parserConfig struct {
maxParts uint
maxHeaders uint
maxMemSize DataSize
maxMemFileSize DataSize
}
type ParserOption func(*parserConfig)
type DataSize int64
const (
_ DataSize = 1 << (iota * 10)
KB
MB
GB
)
const (
defaultMaxParts = 10000
defaultMaxHeaders = 10000
defaultMaxMemSize = 32 * MB
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
}
}
type Value struct {
content []byte
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
}
type Header struct {
dispositionParams map[string]string
header textproto.MIMEHeader
}
func newHeader(h textproto.MIMEHeader) Header {
contentDisposition := h.Get("Content-Disposition")
_, params, err := mime.ParseMediaType(contentDisposition)
if err != nil {
params = make(map[string]string)
}
return Header{
dispositionParams: params,
header: h,
}
}
// 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"]
}
type StreamHookFunc = func(r io.Reader, header Header) error
type streamHook struct {
fn StreamHookFunc
requireParts []string
}