From ac916da48e2a550815ac13445038528aa4011442 Mon Sep 17 00:00:00 2001 From: Andy Stadler Date: Mon, 27 Sep 2010 15:16:24 -0700 Subject: [PATCH] Improve handling of content-disposition DO NOT MERGE * Check array returned by split("=") * Add unit tests for this case * Also add unit tests for quoting removal Bug: 3040796 Backport from: I170f3cd483fe35186194edeb0c3142fb0e2e9b75 Change-Id: I32ccbdbc7264a95a9cd279218cae390e65e82eeb --- .../email/mail/internet/MimeUtility.java | 11 +++--- .../email/mail/internet/MimeUtilityTest.java | 34 ++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/com/android/email/mail/internet/MimeUtility.java b/src/com/android/email/mail/internet/MimeUtility.java index c89dc0a64..47cd6152e 100644 --- a/src/com/android/email/mail/internet/MimeUtility.java +++ b/src/com/android/email/mail/internet/MimeUtility.java @@ -185,7 +185,7 @@ public class MimeUtility { * * @param header * @param name - * @return + * @return the entire header (if name=null), the found parameter, or null */ public static String getHeaderParameter(String header, String name) { if (header == null) { @@ -198,11 +198,14 @@ public class MimeUtility { String lowerCaseName = name.toLowerCase(); for (String part : parts) { if (part.trim().toLowerCase().startsWith(lowerCaseName)) { - String parameter = part.split("=", 2)[1].trim(); + String[] parameterParts = part.split("=", 2); + if (parameterParts.length < 2) { + return null; + } + String parameter = parameterParts[1].trim(); if (parameter.startsWith("\"") && parameter.endsWith("\"")) { return parameter.substring(1, parameter.length() - 1); - } - else { + } else { return parameter; } } diff --git a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java index d648cceaa..8052a62df 100644 --- a/tests/src/com/android/email/mail/internet/MimeUtilityTest.java +++ b/tests/src/com/android/email/mail/internet/MimeUtilityTest.java @@ -83,11 +83,17 @@ public class MimeUtilityTest extends TestCase { "=?UTF-8?Q?=F0=9D=8C=80._Monogram_for_Human_=E2=9A=8B.?="; /** a typical no-param header */ - private final String HEADER_NO_PARAMETER = + private final String HEADER_NO_PARAMETER = "header"; /** a typical multi-param header */ - private final String HEADER_MULTI_PARAMETER = + private final String HEADER_MULTI_PARAMETER = "header; Param1Name=Param1Value; Param2Name=Param2Value"; + /** a multi-param header with quoting */ + private final String HEADER_QUOTED_MULTI_PARAMETER = + "header; Param1Name=\"Param1Value\"; Param2Name=\"Param2Value\""; + /** a malformed header we're seeing in production servers */ + private final String HEADER_MALFORMED_PARAMETER = + "header; Param1Name=Param1Value; filename"; /** * a string generated by google calendar that contains two interesting gotchas: @@ -264,24 +270,34 @@ public class MimeUtilityTest extends TestCase { // NOTE: The docs are wrong - it returns the header (no params) in that case // assertEquals("null name first param per docs", "Param1Value", // MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null)); - assertEquals("null name first param per code", "header", + assertEquals("null name first param per code", "header", MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null)); - assertEquals("null name full header", HEADER_NO_PARAMETER, + assertEquals("null name full header", HEADER_NO_PARAMETER, MimeUtility.getHeaderParameter(HEADER_NO_PARAMETER, null)); // find name - assertEquals("get 1st param", "Param1Value", + assertEquals("get 1st param", "Param1Value", MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param1Name")); - assertEquals("get 2nd param", "Param2Value", + assertEquals("get 2nd param", "Param2Value", MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param2Name")); - assertEquals("get missing param", null, + assertEquals("get missing param", null, MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param3Name")); // case insensitivity - assertEquals("get 2nd param all LC", "Param2Value", + assertEquals("get 2nd param all LC", "Param2Value", MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "param2name")); - assertEquals("get 2nd param all UC", "Param2Value", + assertEquals("get 2nd param all UC", "Param2Value", MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "PARAM2NAME")); + + // quoting + assertEquals("get 1st param", "Param1Value", + MimeUtility.getHeaderParameter(HEADER_QUOTED_MULTI_PARAMETER, "Param1Name")); + assertEquals("get 2nd param", "Param2Value", + MimeUtility.getHeaderParameter(HEADER_QUOTED_MULTI_PARAMETER, "Param2Name")); + + // Don't fail when malformed + assertEquals("malformed filename param", null, + MimeUtility.getHeaderParameter(HEADER_MALFORMED_PARAMETER, "filename")); } // TODO: tests for findFirstPartByMimeType(Part part, String mimeType)