Skip to content

Commit e52687f

Browse files
committed
use rfc2047decode in fixMangledMediaType for jhillyerd#135
1 parent d24345a commit e52687f

File tree

3 files changed

+20
-53
lines changed

3 files changed

+20
-53
lines changed

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ require (
1212
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
1313
golang.org/x/text v0.3.2
1414
)
15+
16+
go 1.13

header.go

+1-53
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func fixMangledMediaType(mtype, sep string) string {
295295
}
296296

297297
// RFC-2047 encoded attribute name
298-
p = rfc2047AttributeName(p)
298+
p = rfc2047decode(p)
299299

300300
pair := strings.SplitAfter(p, "=")
301301
if strings.Contains(mtype, pair[0]) {
@@ -606,55 +606,3 @@ func fixUnescapedQuotes(hvalue string) string {
606606
func whiteSpaceRune(r rune) bool {
607607
return r == ' ' || r == '\t' || r == '\r' || r == '\n'
608608
}
609-
610-
// rfc2047AttributeName checks if the attribute name is encoded in RFC2047 format
611-
// RFC2047 Example:
612-
// `=?UTF-8?B?bmFtZT0iw7DCn8KUwoo=?=`
613-
func rfc2047AttributeName(name string) string {
614-
if !strings.Contains(name, "?=") {
615-
return name
616-
}
617-
// copy the string so we can return the original if we encounter any issues
618-
s := name
619-
620-
// handle n-number of RFC2047 chunk occurrences
621-
count := strings.Count(name, "?=")
622-
result := &strings.Builder{}
623-
var beginning, ending int
624-
for i := 0; i < count; i++ {
625-
beginning = strings.Index(s, "=?")
626-
ending = strings.Index(s, "?=")
627-
628-
if beginning == -1 || ending == -1 {
629-
// the RFC2047 chunk is either malformed or is not an RFC2047 chunk
630-
return name
631-
}
632-
633-
_, err := result.WriteString(s[:beginning])
634-
if err != nil {
635-
return name
636-
}
637-
_, err = result.WriteString(decodeHeader(s[beginning : ending+2]))
638-
if err != nil {
639-
return name
640-
}
641-
642-
s = s[ending+2:]
643-
}
644-
_, err := result.WriteString(s)
645-
if err != nil {
646-
return name
647-
}
648-
keyValuePair := strings.SplitAfter(result.String(), "=")
649-
if len(keyValuePair) < 2 {
650-
return result.String()
651-
}
652-
// Add quotes as needed
653-
if !strings.HasPrefix(keyValuePair[1], "\"") {
654-
keyValuePair[1] = fmt.Sprintf("\"%s", keyValuePair[1])
655-
}
656-
if !strings.HasSuffix(keyValuePair[1], "\"") {
657-
keyValuePair[1] = fmt.Sprintf("%s\"", keyValuePair[1])
658-
}
659-
return strings.Join(keyValuePair, "")
660-
}

inspect.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package enmime
33
import (
44
"bufio"
55
"bytes"
6+
"fmt"
67
"io"
78
"mime"
89
"net/textproto"
@@ -86,12 +87,28 @@ func rfc2047decode(s string) string {
8687
return r
8788
}, s)
8889
var err error
90+
decoded := false
8991
for {
9092
s, err = rfc2047recurse(s)
9193
switch err {
9294
case nil:
95+
decoded = true
9396
continue
9497
default:
98+
if decoded {
99+
keyValuePair := strings.SplitAfter(s, "=")
100+
if len(keyValuePair) < 2 {
101+
return s
102+
}
103+
// Add quotes as needed
104+
if !strings.HasPrefix(keyValuePair[1], "\"") {
105+
keyValuePair[1] = fmt.Sprintf("\"%s", keyValuePair[1])
106+
}
107+
if !strings.HasSuffix(keyValuePair[1], "\"") {
108+
keyValuePair[1] = fmt.Sprintf("%s\"", keyValuePair[1])
109+
}
110+
return strings.Join(keyValuePair, "")
111+
}
95112
return s
96113
}
97114
}

0 commit comments

Comments
 (0)