| 1 |
package enmime |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"encoding/base64" |
| 6 |
"io" |
| 7 |
"mime" |
| 8 |
"mime/quotedprintable" |
| 9 |
"net/textproto" |
| 10 |
"sort" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/jhillyerd/enmime/internal/coding" |
| 14 |
"github.com/jhillyerd/enmime/internal/stringutil" |
| 15 |
) |
| 16 |
|
| 17 |
// b64Percent determines the percent of non-ASCII characters enmime will tolerate before switching |
| 18 |
// from quoted-printable to base64 encoding. |
| 19 |
const b64Percent = 20 |
| 20 |
|
| 21 |
type transferEncoding byte |
| 22 |
|
| 23 |
const ( |
| 24 |
te7Bit transferEncoding = iota |
| 25 |
teQuoted |
| 26 |
teBase64 |
| 27 |
) |
| 28 |
|
| 29 |
var crnl = []byte{'\r', '\n'} |
| 30 |
|
| 31 |
// Encode writes this Part and all its children to the specified writer in MIME format. |
| 32 |
func (p *Part) Encode(writer io.Writer) error { |
| 33 |
if p.Header == nil { |
| 34 |
p.Header = make(textproto.MIMEHeader) |
| 35 |
} |
| 36 |
cte := p.setupMIMEHeaders() |
| 37 |
// Encode this part. |
| 38 |
b := bufio.NewWriter(writer) |
| 39 |
p.encodeHeader(b) |
| 40 |
if len(p.Content) > 0 { |
| 41 |
b.Write(crnl) |
| 42 |
if err := p.encodeContent(b, cte); err != nil { |
| 43 |
return err |
| 44 |
} |
| 45 |
} |
| 46 |
if p.FirstChild == nil { |
| 47 |
return b.Flush() |
| 48 |
} |
| 49 |
// Encode children. |
| 50 |
endMarker := []byte("\r\n--" + p.Boundary + "--") |
| 51 |
marker := endMarker[:len(endMarker)-2] |
| 52 |
c := p.FirstChild |
| 53 |
for c != nil { |
| 54 |
b.Write(marker) |
| 55 |
b.Write(crnl) |
| 56 |
if err := c.Encode(b); err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
c = c.NextSibling |
| 60 |
} |
| 61 |
b.Write(endMarker) |
| 62 |
b.Write(crnl) |
| 63 |
return b.Flush() |
| 64 |
} |
| 65 |
|
| 66 |
// setupMIMEHeaders determines content transfer encoding, generates a boundary string if required, |
| 67 |
// then sets the Content-Type (type, charset, filename, boundary) and Content-Disposition headers. |
| 68 |
func (p *Part) setupMIMEHeaders() transferEncoding { |
| 69 |
// Determine content transfer encoding. |
| 70 |
|
| 71 |
// If we are encoding a part that previously had content-transfer-encoding set, unset it so |
| 72 |
// the correct encoding detection can be done below. |
| 73 |
p.Header.Del(hnContentEncoding) |
| 74 |
|
| 75 |
cte := te7Bit |
| 76 |
if len(p.Content) > 0 { |
| 77 |
cte = teBase64 |
| 78 |
if p.TextContent() { |
| 79 |
cte = selectTransferEncoding(p.Content, false) |
| 80 |
if p.Charset == "" { |
| 81 |
p.Charset = utf8 |
| 82 |
} |
| 83 |
} |
| 84 |
// RFC 2045: 7bit is assumed if CTE header not present. |
| 85 |
switch cte { |
| 86 |
case teBase64: |
| 87 |
p.Header.Set(hnContentEncoding, cteBase64) |
| 88 |
case teQuoted: |
| 89 |
p.Header.Set(hnContentEncoding, cteQuotedPrintable) |
| 90 |
} |
| 91 |
} |
| 92 |
// Setup headers. |
| 93 |
if p.FirstChild != nil && p.Boundary == "" { |
| 94 |
// Multipart, generate random boundary marker. |
| 95 |
p.Boundary = "enmime-" + stringutil.UUID() |
| 96 |
} |
| 97 |
if p.ContentID != "" { |
| 98 |
p.Header.Set(hnContentID, coding.ToIDHeader(p.ContentID)) |
| 99 |
} |
| 100 |
fileName := p.FileName |
| 101 |
switch selectTransferEncoding([]byte(p.FileName), true) { |
| 102 |
case teBase64: |
| 103 |
fileName = mime.BEncoding.Encode(utf8, p.FileName) |
| 104 |
case teQuoted: |
| 105 |
fileName = mime.QEncoding.Encode(utf8, p.FileName) |
| 106 |
} |
| 107 |
if p.ContentType != "" { |
| 108 |
// Build content type header. |
| 109 |
param := make(map[string]string) |
| 110 |
for k, v := range p.ContentTypeParams { |
| 111 |
param[k] = v |
| 112 |
} |
| 113 |
setParamValue(param, hpCharset, p.Charset) |
| 114 |
setParamValue(param, hpName, fileName) |
| 115 |
setParamValue(param, hpBoundary, p.Boundary) |
| 116 |
if mt := mime.FormatMediaType(p.ContentType, param); mt != "" { |
| 117 |
p.ContentType = mt |
| 118 |
} |
| 119 |
p.Header.Set(hnContentType, p.ContentType) |
| 120 |
} |
| 121 |
if p.Disposition != "" { |
| 122 |
// Build disposition header. |
| 123 |
param := make(map[string]string) |
| 124 |
setParamValue(param, hpFilename, fileName) |
| 125 |
if !p.FileModDate.IsZero() { |
| 126 |
setParamValue(param, hpModDate, p.FileModDate.Format(time.RFC822)) |
| 127 |
} |
| 128 |
if mt := mime.FormatMediaType(p.Disposition, param); mt != "" { |
| 129 |
p.Disposition = mt |
| 130 |
} |
| 131 |
p.Header.Set(hnContentDisposition, p.Disposition) |
| 132 |
} |
| 133 |
return cte |
| 134 |
} |
| 135 |
|
| 136 |
// encodeHeader writes out a sorted list of headers. |
| 137 |
func (p *Part) encodeHeader(b *bufio.Writer) { |
| 138 |
keys := make([]string, 0, len(p.Header)) |
| 139 |
for k := range p.Header { |
| 140 |
keys = append(keys, k) |
| 141 |
} |
| 142 |
sort.Strings(keys) |
| 143 |
for _, k := range keys { |
| 144 |
for _, v := range p.Header[k] { |
| 145 |
encv := v |
| 146 |
switch selectTransferEncoding([]byte(v), true) { |
| 147 |
case teBase64: |
| 148 |
encv = mime.BEncoding.Encode(utf8, v) |
| 149 |
case teQuoted: |
| 150 |
encv = mime.QEncoding.Encode(utf8, v) |
| 151 |
} |
| 152 |
// _ used to prevent early wrapping |
| 153 |
wb := stringutil.Wrap(76, k, ":_", encv, "\r\n") |
| 154 |
wb[len(k)+1] = ' ' |
| 155 |
b.Write(wb) |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
// encodeContent writes out the content in the selected encoding. |
| 161 |
func (p *Part) encodeContent(b *bufio.Writer, cte transferEncoding) (err error) { |
| 162 |
switch cte { |
| 163 |
case teBase64: |
| 164 |
enc := base64.StdEncoding |
| 165 |
text := make([]byte, enc.EncodedLen(len(p.Content))) |
| 166 |
base64.StdEncoding.Encode(text, p.Content) |
| 167 |
// Wrap lines. |
| 168 |
lineLen := 76 |
| 169 |
for len(text) > 0 { |
| 170 |
if lineLen > len(text) { |
| 171 |
lineLen = len(text) |
| 172 |
} |
| 173 |
if _, err = b.Write(text[:lineLen]); err != nil { |
| 174 |
return err |
| 175 |
} |
| 176 |
b.Write(crnl) |
| 177 |
text = text[lineLen:] |
| 178 |
} |
| 179 |
case teQuoted: |
| 180 |
qp := quotedprintable.NewWriter(b) |
| 181 |
if _, err = qp.Write(p.Content); err != nil { |
| 182 |
return err |
| 183 |
} |
| 184 |
err = qp.Close() |
| 185 |
default: |
| 186 |
_, err = b.Write(p.Content) |
| 187 |
} |
| 188 |
return err |
| 189 |
} |
| 190 |
|
| 191 |
// selectTransferEncoding scans content for non-ASCII characters and selects 'b' or 'q' encoding. |
| 192 |
func selectTransferEncoding(content []byte, quoteLineBreaks bool) transferEncoding { |
| 193 |
if len(content) == 0 { |
| 194 |
return te7Bit |
| 195 |
} |
| 196 |
// Binary chars remaining before we choose b64 encoding. |
| 197 |
threshold := b64Percent * len(content) / 100 |
| 198 |
bincount := 0 |
| 199 |
for _, b := range content { |
| 200 |
if (b < ' ' || '~' < b) && b != '\t' { |
| 201 |
if !quoteLineBreaks && (b == '\r' || b == '\n') { |
| 202 |
continue |
| 203 |
} |
| 204 |
bincount++ |
| 205 |
if bincount >= threshold { |
| 206 |
return teBase64 |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
if bincount == 0 { |
| 211 |
return te7Bit |
| 212 |
} |
| 213 |
return teQuoted |
| 214 |
} |
| 215 |
|
| 216 |
// setParamValue will ignore empty values |
| 217 |
func setParamValue(p map[string]string, k, v string) { |
| 218 |
if v != "" { |
| 219 |
p[k] = v |
| 220 |
} |
| 221 |
} |