Skip to content

Commit

Permalink
fix: don't encode header value with the RawContent parser option
Browse files Browse the repository at this point in the history
  • Loading branch information
jerjako committed Jan 3, 2024
1 parent ed0394e commit a505242
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,19 @@ func (p *Part) encodeHeader(b *bufio.Writer) error {
for k := range p.Header {
keys = append(keys, k)
}
rawContent := p.parser != nil && p.parser.rawContent

sort.Strings(keys)
for _, k := range keys {
for _, v := range p.Header[k] {
encv := v
switch selectTransferEncoding([]byte(v), true) {
case teBase64:
encv = mime.BEncoding.Encode(utf8, v)
case teQuoted:
encv = mime.QEncoding.Encode(utf8, v)
if !rawContent {
switch selectTransferEncoding([]byte(v), true) {
case teBase64:
encv = mime.BEncoding.Encode(utf8, v)
case teQuoted:
encv = mime.QEncoding.Encode(utf8, v)
}
}
// _ used to prevent early wrapping
wb := stringutil.Wrap(76, k, ":_", encv, "\r\n")
Expand Down
16 changes: 16 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,19 @@ func TestParseRawContentTextOptionFalse(t *testing.T) {
}
test.DiffGolden(t, b.Bytes(), "testdata", "encode", "parser-raw-content-text-option-false.raw.golden")
}

// TestRawContentUTF8Headers test if not encoded UTF8 headers are not edited with the rawContent parser option.
func TestRawContentUTF8Headers(t *testing.T) {
r := test.OpenTestData("encode", "utf8-to.raw.golden")
p := enmime.NewParser(enmime.RawContent(true))
e, err := p.ReadEnvelope(r)
if err != nil {
t.Fatal("Failed to parse MIME:", err)
}
b := &bytes.Buffer{}
if err := e.Root.Encode(b); err != nil {
t.Fatal(err)
}

test.DiffGolden(t, b.Bytes(), "testdata", "encode", "utf8-to.raw.golden")
}
7 changes: 7 additions & 0 deletions testdata/encode/utf8-to.raw.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Content-Type: text/plain; charset=utf-8
From: Weiß foo@bar.com
Mime-Version: 1.0
Subject: test utf8 to
To: Jøhn Døe <bar@baz.com>

This is a test mailing

0 comments on commit a505242

Please sign in to comment.