Skip to content

Commit

Permalink
Correct encoding and decoding for the filename parameter in the Conte…
Browse files Browse the repository at this point in the history
…nt-Disposition.
  • Loading branch information
jca02266 committed Apr 7, 2011
1 parent 2cadff7 commit 79a9695
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/com/fsck/k9/activity/MessageCompose.java
Expand Up @@ -1121,7 +1121,9 @@ private void addAttachmentsToMessage(final MimeMultipart mp) throws MessagingExc
*/
bp.addHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, String.format(
"attachment;\n filename=\"%s\";\n size=%d",
attachment.name, attachment.size));
EncoderUtil.encodeIfNecessary(attachment.name,
EncoderUtil.Usage.WORD_ENTITY, 7),
attachment.size));

mp.addBodyPart(bp);
}
Expand Down
13 changes: 9 additions & 4 deletions src/com/fsck/k9/mail/store/LocalStore.java
Expand Up @@ -16,6 +16,7 @@

import com.fsck.k9.helper.HtmlConverter;
import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.codec.EncoderUtil;

import android.app.Application;
import android.content.ContentValues;
Expand Down Expand Up @@ -1671,16 +1672,20 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException {
if (contentUri != null) {
body = new LocalAttachmentBody(Uri.parse(contentUri), mApplication);
}

String encoded_name = EncoderUtil.encodeIfNecessary(name,
EncoderUtil.Usage.WORD_ENTITY, 7);

MimeBodyPart bp = new LocalAttachmentBodyPart(body, id);
bp.setHeader(MimeHeader.HEADER_CONTENT_TYPE,
String.format("%s;\n name=\"%s\"",
type,
name));
encoded_name));
bp.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64");
bp.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION,
String.format("%s;\n filename=\"%s\";\n size=%d",
contentDisposition,
name,
encoded_name, // TODO: Should use encoded word defined in RFC 2231.
size));

bp.setHeader(MimeHeader.HEADER_CONTENT_ID, contentId);
Expand Down Expand Up @@ -2364,7 +2369,7 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException, Unavailab
Utility.combine(attachment.getHeader(
MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA), ',');

String name = MimeUtility.getHeaderParameter(attachment.getContentType(), "name");
String name = MimeUtility.unfoldAndDecode(MimeUtility.getHeaderParameter(attachment.getContentType(), "name"));
String contentId = MimeUtility.getHeaderParameter(attachment.getContentId(), null);

String contentDisposition = MimeUtility.unfoldAndDecode(attachment.getDisposition());
Expand All @@ -2377,7 +2382,7 @@ public Void doDbWork(final SQLiteDatabase db) throws WrappedException, Unavailab
}

if (name == null && contentDisposition != null) {
name = MimeUtility.getHeaderParameter(contentDisposition, "filename");
name = MimeUtility.unfoldAndDecode(MimeUtility.getHeaderParameter(contentDisposition, "filename"));
}
if (attachmentId == -1) {
ContentValues cv = new ContentValues();
Expand Down

0 comments on commit 79a9695

Please sign in to comment.