From 79a96952f2b8c95c059afee3e564721a09ed98c1 Mon Sep 17 00:00:00 2001 From: Koji Arai Date: Thu, 7 Apr 2011 00:18:14 +0900 Subject: [PATCH] Correct encoding and decoding for the filename parameter in the Content-Disposition. --- src/com/fsck/k9/activity/MessageCompose.java | 4 +++- src/com/fsck/k9/mail/store/LocalStore.java | 13 +++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 28593c0fcac..ee5422c8ea1 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -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); } diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 6987ef67001..d0ddd6cebbf 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -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; @@ -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); @@ -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()); @@ -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();