Skip to content

Commit 1ce3b3c

Browse files
committed
Don't split Quoted Printable characters during folding procedure
1 parent f7e2be8 commit 1ce3b3c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

header.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package message
33
import (
44
"mime"
55
"net/textproto"
6+
"regexp"
67
"strings"
78

89
"github.com/emersion/go-message/charset"
@@ -40,6 +41,8 @@ func formatHeaderField(k, v string) string {
4041
return s + "\r\n"
4142
}
4243

44+
qpReg := regexp.MustCompile("(=[0-9A-Z]{2,2})+")
45+
4346
first := true
4447
for len(v) > 0 {
4548
maxlen := maxHeaderLen
@@ -60,7 +63,13 @@ func formatHeaderField(k, v string) string {
6063
}
6164
} else {
6265
// Find the closest whitespace before i
63-
foldAt = strings.LastIndexAny(v[:foldBefore], " \t\n")
66+
foldAtQP := qpReg.FindAllStringIndex(v[:foldBefore], -1)
67+
foldAtEOL := strings.LastIndexAny(v[:foldBefore], " \t\n")
68+
if len(foldAtQP) > 0 {
69+
foldAt := foldAtQP[len(foldAtQP)-1][0]
70+
} else {
71+
foldAt = foldAtEOL
72+
}
6473
if foldAt == 0 {
6574
// The whitespace we found was the previous folding WSP
6675
foldAt = foldBefore - 1

header_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ var formatHeaderFieldTests = []struct {
5959
v: "This is yet \t another subject \t with many whitespace characters",
6060
formatted: "Subject: This is yet \t another subject \t \r\n with many whitespace characters\r\n",
6161
},
62+
{
63+
k: "Subject",
64+
v: "=?utf-8?q?=E2=80=9CDeveloper_reads_customer_requested_change.=E2=80=9D=0A?= =?utf-8?q?=0ACaravaggio=0A=0AOil_on...?=",
65+
formatted: "Subject: =?utf-8?q?=E2=80=9CDeveloper_reads_customer_requested_change.\r\n =E2=80=9D=0A?= =?utf-8?q?=0ACaravaggio=0A=0AOil_on...?=\r\n",
66+
},
6267
{
6368
k: "DKIM-Signature",
6469
v: "v=1;\r\n h=From:To:Reply-To:Subject:Message-ID:References:In-Reply-To:MIME-Version;\r\n d=example.org\r\n",

0 commit comments

Comments
 (0)