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 28593c0fc..ee5422c8e 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -1121,7 +1121,9 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc */ 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 6987ef670..d0ddd6ceb 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -16,6 +16,7 @@ import java.util.UUID; 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 class LocalStore extends Store implements Serializable { 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 class LocalStore extends Store implements Serializable { 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 class LocalStore extends Store implements Serializable { } if (name == null && contentDisposition != null) { - name = MimeUtility.getHeaderParameter(contentDisposition, "filename"); + name = MimeUtility.unfoldAndDecode(MimeUtility.getHeaderParameter(contentDisposition, "filename")); } if (attachmentId == -1) { ContentValues cv = new ContentValues();