Skip to content

Commit

Permalink
Default charset to binary for non-text/* attachments if charset is no…
Browse files Browse the repository at this point in the history
…t defined (Issue: #18)
  • Loading branch information
mash-symc committed Apr 27, 2018
1 parent 4eeffc0 commit a53e8fa
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 8 deletions.
102 changes: 102 additions & 0 deletions src/node-unit.js
Expand Up @@ -255,6 +255,108 @@ describe('MimeNode tests', function () {
expect(node._isMultipart).to.equal('mixed')
expect(node._multipartBoundary).to.equal('zzzz')
})

it('should set charset to binary for attachment when there is no charset', function () {
node.headers['content-type'] = [{
value: 'application/pdf'
}]

node.headers['content-disposition'] = [{
value: 'attachment'
}]

node._processContentType()

expect(node.contentType).to.deep.equal({
value: 'application/pdf',
type: 'application'
})
expect(node.charset).to.equal('binary')
})

it('should set charset to binary for inline attachment when there is no charset', function () {
node.headers['content-type'] = [{
value: 'image/png'
}]

node.headers['content-disposition'] = [{
value: 'inline'
}]

node._processContentType()

expect(node.contentType).to.deep.equal({
value: 'image/png',
type: 'image'
})
expect(node.charset).to.equal('binary')
})

it('should not set charset to binary for inline attachment when there is a charset', function () {
node.headers['content-type'] = [{
value: 'image/png',
params: {
charset: 'US-ASCII'
}
}]

node.headers['content-disposition'] = [{
value: 'inline'
}]

node._processContentType()

expect(node.contentType).to.deep.equal({
value: 'image/png',
type: 'image',
params: {
charset: 'US-ASCII'
}
})
expect(node.charset).to.equal('US-ASCII')
})

it('should not set charset to binary for text/* attachment when there is no charset', function () {
node.headers['content-type'] = [{
value: 'text/plain'
}]

node.headers['content-disposition'] = [{
value: 'attachment'
}]

node._processContentType()

expect(node.contentType).to.deep.equal({
value: 'text/plain',
type: 'text'
})
expect(node.charset).to.be.undefined
})

it('should not set charset to binary for text/* attachment when there is a charset', function () {
node.headers['content-type'] = [{
value: 'text/plain',
params: {
charset: 'US-ASCII'
}
}]

node.headers['content-disposition'] = [{
value: 'attachment'
}]

node._processContentType()

expect(node.contentType).to.deep.equal({
value: 'text/plain',
type: 'text',
params: {
charset: 'US-ASCII'
}
})
expect(node.charset).to.equal('US-ASCII')
})
})

describe('#_processContentTransferEncoding', function () {
Expand Down
25 changes: 17 additions & 8 deletions src/node.js
Expand Up @@ -206,18 +206,27 @@ export default class MimeNode {
this._multipartBoundary = this.contentType.params.boundary
}

if (this.contentType.value === 'message/rfc822') {
/**
* For attachment (inline/regular) if charset is not defined and attachment is non-text/*,
* then default charset to binary.
* Refer to issue: https://github.com/emailjs/emailjs-mime-parser/issues/18
*/
const defaultContentDispositionValue = parseHeaderValue('')
const contentDisposition = pathOr(defaultContentDispositionValue, ['headers', 'content-disposition', '0'])(this)
const isAttachment = (contentDisposition.value || '').toLowerCase().trim() === 'attachment'
const isInlineAttachment = (contentDisposition.value || '').toLowerCase().trim() === 'inline'
if ((isAttachment || isInlineAttachment) && this.contentType.type !== 'text' && !this.charset) {
this.charset = 'binary'
}

if (this.contentType.value === 'message/rfc822' && !isAttachment) {
/**
* Parse message/rfc822 only if the mime part is not marked with content-disposition: attachment,
* otherwise treat it like a regular attachment
*/
const defaultValue = parseHeaderValue('')
const contentDisposition = pathOr(defaultValue, ['headers', 'content-disposition', '0'])(this)
if ((contentDisposition.value || '').toLowerCase().trim() !== 'attachment') {
this._currentChild = new MimeNode(this)
this.childNodes = [this._currentChild]
this._isRfc822 = true
}
this._currentChild = new MimeNode(this)
this.childNodes = [this._currentChild]
this._isRfc822 = true
}
}

Expand Down

0 comments on commit a53e8fa

Please sign in to comment.