Skip to content

Commit

Permalink
feat: add parser option to parse raw content
Browse files Browse the repository at this point in the history
  • Loading branch information
jerjako committed Oct 10, 2023
1 parent 6241177 commit 522ab2d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
te7Bit transferEncoding = iota
teQuoted
teBase64
teRaw
)

const (
Expand Down Expand Up @@ -194,6 +195,10 @@ func (p *Part) encodeContent(b *bufio.Writer, cte transferEncoding) (err error)
return p.encodeContentFromReader(b)
}

if p.parser != nil && p.parser.rawContent {
cte = teRaw
}

switch cte {
case teBase64:
enc := base64.StdEncoding
Expand Down
12 changes: 12 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ func (o maxStoredPartErrorsOption) apply(p *Parser) {
max := int(o)
p.maxStoredPartErrors = &max
}

// RawContent if set to true will not try to decode the CTE and return the raw part content.
// Otherwise, will try to automatically decode the CTE.
func RawContent(a bool) Option {
return rawContentOption(a)
}

type rawContentOption bool

func (o rawContentOption) apply(p *Parser) {
p.rawContent = bool(o)
}
1 change: 1 addition & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Parser struct {
multipartWOBoundaryAsSinglePart bool
readPartErrorPolicy ReadPartErrorPolicy
skipMalformedParts bool
rawContent bool
}

// defaultParser is a Parser with default configuration.
Expand Down
5 changes: 4 additions & 1 deletion part.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ func (p *Part) decodeContent(r io.Reader, readPartErrorPolicy ReadPartErrorPolic
// b64cleaner aggregates errors, must maintain a reference to it to get them later.
var b64cleaner *coding.Base64Cleaner
// Build content decoding reader.
encoding := header.Get(hnContentEncoding)
encoding := ""
if p.parser != nil && !p.parser.rawContent {
encoding = header.Get(hnContentEncoding)
}
validEncoding := true
switch strings.ToLower(encoding) {
case cteQuotedPrintable:
Expand Down

0 comments on commit 522ab2d

Please sign in to comment.