Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Line wrapping for text/plain content #135

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
// from quoted-printable to base64 encoding.
const b64Percent = 20

// lineWrapLength is the length at which we wrap base64 and plain text content
const lineWrapLength = 76

type transferEncoding byte

const (
Expand Down Expand Up @@ -159,25 +162,25 @@ func (p *Part) encodeContent(b *bufio.Writer, cte transferEncoding) (err error)
text := make([]byte, enc.EncodedLen(len(p.Content)))
base64.StdEncoding.Encode(text, p.Content)
// Wrap lines.
lineLen := 76
for len(text) > 0 {
if lineLen > len(text) {
lineLen = len(text)
}
if _, err = b.Write(text[:lineLen]); err != nil {
return err
}
b.Write(crnl)
text = text[lineLen:]
if err := wrapContent(text, b); err != nil {
return err
}
//b.Write(crnl)
case teQuoted:
qp := quotedprintable.NewWriter(b)
if _, err = qp.Write(p.Content); err != nil {
return err
}
err = qp.Close()
default:
_, err = b.Write(p.Content)
if p.ContentType == ctTextPlain {
// Wrap lines.
if err := wrapContent(p.Content, b); err != nil {
return err
}
} else {
_, err = b.Write(p.Content)
}
}
return err
}
Expand Down Expand Up @@ -213,3 +216,22 @@ func setParamValue(p map[string]string, k, v string) {
p[k] = v
}
}

func wrapContent(text []byte, b *bufio.Writer) error {
lineLen := lineWrapLength
beginning := true
for len(text) > 0 {
if !beginning {
b.Write(crnl)
}
if lineLen > len(text) {
lineLen = len(text)
}
if _, err := b.Write(text[:lineLen]); err != nil {
return err
}
text = text[lineLen:]
beginning = false
}
return nil
}
4 changes: 2 additions & 2 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestEncodePartBinaryHeader(t *testing.T) {
0xa2, 0xb2, 0xc0, 0x90, 0x59, 0xe3, 0x35, 0xf8, 0x60, 0xb7, 0xb1, 0x63, 0x77, 0xd7,
0x5f, 0x92, 0x58, 0xa8, 0x75,
}))
p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.\r\n")
p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.")

b := &bytes.Buffer{}
err := p.Encode(b)
Expand All @@ -101,7 +101,7 @@ func TestEncodePartContentOnly(t *testing.T) {

func TestEncodePartPlain(t *testing.T) {
p := enmime.NewPart("text/plain")
p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.\r\n")
p.Content = []byte("This is a test of a plain text part.\r\n\r\nAnother line.")

b := &bytes.Buffer{}
err := p.Encode(b)
Expand Down
2 changes: 1 addition & 1 deletion testdata/encode/part-bin-content.golden
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ rq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm
ICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldY
WVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CR
kpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnK
y8zNzs8=
y8zNzs8=
2 changes: 1 addition & 1 deletion testdata/encode/part-bin-header.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ X-Data: =?utf-8?b?AxfhfujropadladnggnfjgwsaiubvnmkadiuhterqHJSFfuAjkfhrqpeorLA?=

This is a test of a plain text part.

Another line.
Another line.
2 changes: 1 addition & 1 deletion testdata/encode/part-default-headers.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Content-Transfer-Encoding: base64
Content-Type: application/zip; boundary=enmime-abcdefg0123456789;
charset=binary; name=stuff.zip

WklQWklQWklQ
WklQWklQWklQ
2 changes: 1 addition & 1 deletion testdata/encode/part-plain.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ Content-Type: text/plain; charset=utf-8

This is a test of a plain text part.

Another line.
Another line.
2 changes: 1 addition & 1 deletion testdata/encode/part-quoted-headers.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Content-Transfer-Encoding: base64
Content-Type: application/zip; boundary=enmime-abcdefg0123456789;
charset=binary; name="arvizturo \"x\" tukorfurogep.zip"

WklQWklQWklQ
WklQWklQWklQ