-
Notifications
You must be signed in to change notification settings - Fork 102
/
parser.go
48 lines (40 loc) · 1.56 KB
/
parser.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
package enmime
// ReadPartErrorPolicy allows to recover the buffer (or not) on an error when reading a Part content.
//
// See AllowCorruptTextPartErrorPolicy for usage.
type ReadPartErrorPolicy func(*Part, error) bool
// AllowCorruptTextPartErrorPolicy recovers partial content from base64.CorruptInputError when content type is text/plain or text/html.
func AllowCorruptTextPartErrorPolicy(p *Part, err error) bool {
if IsBase64CorruptInputError(err) && (p.ContentType == ctTextHTML || p.ContentType == ctTextPlain) {
return true
}
return false
}
// CustomParseMediaType parses media type. See ParseMediaType for more details
type CustomParseMediaType func(ctype string) (mtype string, params map[string]string, invalidParams []string, err error)
// Parser parses MIME. Create with NewParser to inherit recommended defaults.
type Parser struct {
maxStoredPartErrors int
multipartWOBoundaryAsSinglePart bool
readPartErrorPolicy ReadPartErrorPolicy
skipMalformedParts bool
rawContent bool
customParseMediaType CustomParseMediaType
stripMediaTypeInvalidCharacters bool
disableTextConversion bool
disableCharacterDetection bool
minCharsetDetectRunes int
}
// defaultParser is a Parser with default configuration.
var defaultParser = *NewParser()
// NewParser creates new parser with given options.
func NewParser(ops ...Option) *Parser {
// Construct parser with default options.
p := Parser{
minCharsetDetectRunes: 100,
}
for _, o := range ops {
o.apply(&p)
}
return &p
}