1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
package enmime
import (
"bufio"
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"mime/quotedprintable"
"net/textproto"
"strconv"
"strings"
"time"
"github.com/gogits/chardet"
"github.com/jhillyerd/enmime/internal/coding"
"github.com/jhillyerd/enmime/mediatype"
"github.com/pkg/errors"
)
const (
minCharsetConfidence = 85
minCharsetRuneLength = 100
)
// Part represents a node in the MIME multipart tree. The Content-Type, Disposition and File Name
// are parsed out of the header for easier access.
type Part struct {
PartID string // PartID labels this parts position within the tree.
Parent *Part // Parent of this part (can be nil.)
FirstChild *Part // FirstChild is the top most child of this part.
NextSibling *Part // NextSibling of this part.
Header textproto.MIMEHeader // Header for this Part.
Boundary string // Boundary marker used within this part.
ContentID string // ContentID header for cid URL scheme.
ContentType string // ContentType header without parameters.
ContentTypeParams map[string]string // Params, added to ContentType header.
Disposition string // Content-Disposition header without parameters.
FileName string // The file-name from disposition or type header.
FileModDate time.Time // The modification date of the file.
Charset string // The content charset encoding, may differ from charset in header.
OrigCharset string // The original content charset when a different charset was detected.
Errors []*Error // Errors encountered while parsing this part.
Content []byte // Content after decoding, UTF-8 conversion if applicable.
Epilogue []byte // Epilogue contains data following the closing boundary marker.
}
// NewPart creates a new Part object.
func NewPart(contentType string) *Part {
return &Part{
Header: make(textproto.MIMEHeader),
ContentType: contentType,
ContentTypeParams: make(map[string]string),
}
}
// AddChild adds a child part to either FirstChild or the end of the children NextSibling chain.
// The child may have siblings and children attached. This method will set the Parent field on
// child and all its siblings. Safe to call on nil.
func (p *Part) AddChild(child *Part) {
if p == child {
// Prevent paradox.
return
}
if p != nil {
if p.FirstChild == nil {
// Make it the first child.
p.FirstChild = child
} else {
// Append to sibling chain.
current := p.FirstChild
for current.NextSibling != nil {
current = current.NextSibling
}
if current == child {
// Prevent infinite loop.
return
}
current.NextSibling = child
}
}
// Update all new first-level children Parent pointers.
for c := child; c != nil; c = c.NextSibling {
if c == c.NextSibling {
// Prevent infinite loop.
return
}
c.Parent = p
}
}
// TextContent indicates whether the content is text based on its content type. This value
// determines what content transfer encoding scheme to use.
func (p *Part) TextContent() bool {
if p.ContentType == "" {
// RFC 2045: no CT is equivalent to "text/plain; charset=us-ascii"
return true
}
return strings.HasPrefix(p.ContentType, "text/") ||
strings.HasPrefix(p.ContentType, ctMultipartPrefix)
}
// setupHeaders reads the header, then populates the MIME header values for this Part.
func (p *Part) setupHeaders(r *bufio.Reader, defaultContentType string) error {
header, err := readHeader(r, p)
if err != nil {
return err
}
p.Header = header
ctype := header.Get(hnContentType)
if ctype == "" {
if defaultContentType == "" {
p.addWarning(ErrorMissingContentType, "MIME parts should have a Content-Type header")
return nil
}
ctype = defaultContentType
}
// Parse Content-Type header.
mtype, mparams, minvalidParams, err := mediatype.Parse(ctype)
if err != nil {
return err
}
for i := range minvalidParams {
p.addWarning(
ErrorMalformedHeader,
"Content-Type header has malformed parameter %q",
minvalidParams[i])
}
p.ContentType = mtype
// Set disposition, filename, charset if available.
p.setupContentHeaders(mparams)
p.Boundary = mparams[hpBoundary]
p.ContentID = coding.FromIDHeader(header.Get(hnContentID))
return nil
}
// setupContentHeaders uses Content-Type media params and Content-Disposition headers to populate
// the disposition, filename, and charset fields.
func (p *Part) setupContentHeaders(mediaParams map[string]string) {
// Determine content disposition, filename, character set.
disposition, dparams, _, err := mediatype.Parse(p.Header.Get(hnContentDisposition))
if err == nil {
// Disposition is optional
p.Disposition = disposition
p.FileName = coding.DecodeExtHeader(dparams[hpFilename])
}
if p.FileName == "" && mediaParams[hpName] != "" {
p.FileName = coding.DecodeExtHeader(mediaParams[hpName])
}
if p.FileName == "" && mediaParams[hpFile] != "" {
p.FileName = coding.DecodeExtHeader(mediaParams[hpFile])
}
if p.Charset == "" {
p.Charset = mediaParams[hpCharset]
}
if p.FileModDate.IsZero() {
p.FileModDate, _ = time.Parse(time.RFC822, mediaParams[hpModDate])
}
}
// convertFromDetectedCharset attempts to detect the character set for the given part, and returns
// an io.Reader that will convert from that charset to UTF-8. If the charset cannot be detected,
// this method adds a warning to the part and automatically falls back to using
// `convertFromStatedCharset` and returns the reader from that method.
func (p *Part) convertFromDetectedCharset(r io.Reader) (io.Reader, error) {
// Attempt to detect character set from part content.
var cd *chardet.Detector
switch p.ContentType {
case "text/html":
cd = chardet.NewHtmlDetector()
default:
cd = chardet.NewTextDetector()
}
buf, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.WithStack(err)
}
cs, err := cd.DetectBest(buf)
switch err {
case nil:
// Carry on
default:
p.addWarning(ErrorCharsetDeclaration, "charset could not be detected: %v", err)
}
// Restore r.
r = bytes.NewReader(buf)
if cs == nil || cs.Confidence < minCharsetConfidence || len(bytes.Runes(buf)) < minCharsetRuneLength {
// Low confidence or not enough characters, use declared character set.
return p.convertFromStatedCharset(r), nil
}
// Confidence exceeded our threshold, use detected character set.
if p.Charset != "" && !strings.EqualFold(cs.Charset, p.Charset) {
p.addWarning(ErrorCharsetDeclaration,
"declared charset %q, detected %q, confidence %d",
p.Charset, cs.Charset, cs.Confidence)
}
if reader, err := coding.NewCharsetReader(cs.Charset, r); err == nil {
r = reader
p.OrigCharset = p.Charset
p.Charset = cs.Charset
}
return r, nil
}
// convertFromStatedCharset returns a reader that will convert from the charset specified for the
// current `*Part` to UTF-8. In case of error, or an unhandled character set, a warning will be
// added to the `*Part` and the original io.Reader will be returned.
func (p *Part) convertFromStatedCharset(r io.Reader) io.Reader {
if p.Charset == "" {
// US-ASCII. Just read.
return r
}
reader, err := coding.NewCharsetReader(p.Charset, r)
if err != nil {
// Failed to get a conversion reader.
p.addWarning(ErrorCharsetConversion, "failed to get reader for charset %q: %v", p.Charset, err)
} else {
return reader
}
// Try to parse charset again here to see if we can salvage some badly formed
// ones like charset="charset=utf-8".
charsetp := strings.Split(p.Charset, "=")
if strings.EqualFold(charsetp[0], "charset") && len(charsetp) > 1 ||
strings.EqualFold(charsetp[0], "iso") && len(charsetp) > 1 {
p.Charset = charsetp[1]
reader, err = coding.NewCharsetReader(p.Charset, r)
if err != nil {
// Failed to get a conversion reader.
p.addWarning(ErrorCharsetConversion, "failed to get reader for charset %q: %v", p.Charset, err)
} else {
return reader
}
}
return r
}
// decodeContent performs transport decoding (base64, quoted-printable) and charset decoding,
// placing the result into Part.Content. IO errors will be returned immediately; other errors
// and warnings will be added to Part.Errors.
func (p *Part) decodeContent(r io.Reader) error {
// contentReader will point to the end of the content decoding pipeline.
contentReader := r
// b64cleaner aggregates errors, must maintain a reference to it to get them later.
var b64cleaner *coding.Base64Cleaner
// Build content decoding reader.
encoding := p.Header.Get(hnContentEncoding)
validEncoding := true
switch strings.ToLower(encoding) {
case cteQuotedPrintable:
contentReader = coding.NewQPCleaner(contentReader)
contentReader = quotedprintable.NewReader(contentReader)
case cteBase64:
b64cleaner = coding.NewBase64Cleaner(contentReader)
contentReader = base64.NewDecoder(base64.RawStdEncoding, b64cleaner)
case cte8Bit, cte7Bit, cteBinary, "":
// No decoding required.
default:
// Unknown encoding.
validEncoding = false
p.addWarning(
ErrorContentEncoding,
"Unrecognized Content-Transfer-Encoding type %q",
encoding)
}
// Build charset decoding reader.
if validEncoding && strings.HasPrefix(p.ContentType, "text/") {
var err error
contentReader, err = p.convertFromDetectedCharset(contentReader)
if err != nil {
return p.base64CorruptInputCheck(err)
}
}
// Decode and store content.
content, err := ioutil.ReadAll(contentReader)
if err != nil {
return p.base64CorruptInputCheck(errors.WithStack(err))
}
p.Content = content
// Collect base64 errors.
if b64cleaner != nil {
for _, err := range b64cleaner.Errors {
p.addWarning(ErrorMalformedBase64, err.Error())
}
}
// Set empty content-type error.
if p.ContentType == "" {
p.addWarning(
ErrorMissingContentType, "content-type is empty for part id: %s", p.PartID)
}
return nil
}
// base64CorruptInputCheck will avoid fatal failure on corrupt base64 input
//
// This is a switch on errors.Cause(err).(type) for base64.CorruptInputError
func (p *Part) base64CorruptInputCheck(err error) error {
switch errors.Cause(err).(type) {
case base64.CorruptInputError:
p.Content = nil
p.Errors = append(p.Errors, &Error{
Name: ErrorMalformedBase64,
Detail: err.Error(),
Severe: true,
})
return nil
default:
return err
}
}
// Clone returns a clone of the current Part.
func (p *Part) Clone(parent *Part) *Part {
if p == nil {
return nil
}
newPart := &Part{
PartID: p.PartID,
Header: p.Header,
Parent: parent,
Boundary: p.Boundary,
ContentID: p.ContentID,
ContentType: p.ContentType,
Disposition: p.Disposition,
FileName: p.FileName,
Charset: p.Charset,
Errors: p.Errors,
Content: p.Content,
Epilogue: p.Epilogue,
}
newPart.FirstChild = p.FirstChild.Clone(newPart)
newPart.NextSibling = p.NextSibling.Clone(parent)
return newPart
}
// ReadParts reads a MIME document from the provided reader and parses it into tree of Part objects.
func ReadParts(r io.Reader) (*Part, error) {
br := bufio.NewReader(r)
root := &Part{PartID: "0"}
// Read header; top-level default CT is text/plain us-ascii according to RFC 822.
err := root.setupHeaders(br, `text/plain; charset="us-ascii"`)
if err != nil {
return nil, err
}
if strings.HasPrefix(root.ContentType, ctMultipartPrefix) {
// Content is multipart, parse it.
err = parseParts(root, br)
if err != nil {
return nil, err
}
} else {
// Content is text or data, decode it.
if err := root.decodeContent(br); err != nil {
return nil, err
}
}
return root, nil
}
// parseParts recursively parses a MIME multipart document and sets each Parts PartID.
func parseParts(parent *Part, reader *bufio.Reader) error {
firstRecursion := parent.Parent == nil
// Loop over MIME boundaries.
br := newBoundaryReader(reader, parent.Boundary)
for indexPartID := 1; true; indexPartID++ {
next, err := br.Next()
if err != nil && errors.Cause(err) != io.EOF {
return err
}
if br.unbounded {
parent.addWarning(ErrorMissingBoundary, "Boundary %q was not closed correctly",
parent.Boundary)
}
if !next {
break
}
p := &Part{}
// Set this Part's PartID, indicating its position within the MIME Part tree.
if firstRecursion {
p.PartID = strconv.Itoa(indexPartID)
} else {
p.PartID = parent.PartID + "." + strconv.Itoa(indexPartID)
}
// Look for part header.
bbr := bufio.NewReader(br)
if err = p.setupHeaders(bbr, ""); err != nil {
return err
}
// Insert this Part into the MIME tree.
parent.AddChild(p)
if p.Boundary == "" {
// Content is text or data, decode it.
if err = p.decodeContent(bbr); err != nil {
return err
}
} else {
// Content is another multipart.
err = parseParts(p, bbr)
if err != nil {
return err
}
}
}
// Store any content following the closing boundary marker into the epilogue.
epilogue, err := ioutil.ReadAll(reader)
if err != nil {
return errors.WithStack(err)
}
parent.Epilogue = epilogue
// If a Part is "multipart/" Content-Type, it will have .0 appended to its PartID
// i.e. it is the root of its MIME Part subtree.
if !firstRecursion {
parent.PartID += ".0"
}
return nil
}
|