Improve handling of content-disposition

* Check array returned by split("=")
* Add unit tests for this case
* Also add unit tests for quoting removal

Bug: 3040796
Change-Id: I170f3cd483fe35186194edeb0c3142fb0e2e9b75
This commit is contained in:
Andrew Stadler 2010-09-27 15:09:35 -07:00
parent fb4333b3a5
commit 87fcd03a1b
2 changed files with 32 additions and 13 deletions

View File

@ -185,7 +185,7 @@ public class MimeUtility {
* *
* @param header * @param header
* @param name * @param name
* @return * @return the entire header (if name=null), the found parameter, or null
*/ */
public static String getHeaderParameter(String header, String name) { public static String getHeaderParameter(String header, String name) {
if (header == null) { if (header == null) {
@ -198,11 +198,14 @@ public class MimeUtility {
String lowerCaseName = name.toLowerCase(); String lowerCaseName = name.toLowerCase();
for (String part : parts) { for (String part : parts) {
if (part.trim().toLowerCase().startsWith(lowerCaseName)) { 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("\"")) { if (parameter.startsWith("\"") && parameter.endsWith("\"")) {
return parameter.substring(1, parameter.length() - 1); return parameter.substring(1, parameter.length() - 1);
} } else {
else {
return parameter; return parameter;
} }
} }

View File

@ -83,11 +83,17 @@ public class MimeUtilityTest extends TestCase {
"=?UTF-8?Q?=F0=9D=8C=80._Monogram_for_Human_=E2=9A=8B.?="; "=?UTF-8?Q?=F0=9D=8C=80._Monogram_for_Human_=E2=9A=8B.?=";
/** a typical no-param header */ /** a typical no-param header */
private final String HEADER_NO_PARAMETER = private final String HEADER_NO_PARAMETER =
"header"; "header";
/** a typical multi-param header */ /** a typical multi-param header */
private final String HEADER_MULTI_PARAMETER = private final String HEADER_MULTI_PARAMETER =
"header; Param1Name=Param1Value; Param2Name=Param2Value"; "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: * 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 // NOTE: The docs are wrong - it returns the header (no params) in that case
// assertEquals("null name first param per docs", "Param1Value", // assertEquals("null name first param per docs", "Param1Value",
// MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, null)); // 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)); 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)); MimeUtility.getHeaderParameter(HEADER_NO_PARAMETER, null));
// find name // find name
assertEquals("get 1st param", "Param1Value", assertEquals("get 1st param", "Param1Value",
MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param1Name")); MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param1Name"));
assertEquals("get 2nd param", "Param2Value", assertEquals("get 2nd param", "Param2Value",
MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param2Name")); MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param2Name"));
assertEquals("get missing param", null, assertEquals("get missing param", null,
MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param3Name")); MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "Param3Name"));
// case insensitivity // case insensitivity
assertEquals("get 2nd param all LC", "Param2Value", assertEquals("get 2nd param all LC", "Param2Value",
MimeUtility.getHeaderParameter(HEADER_MULTI_PARAMETER, "param2name")); 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")); 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) // TODO: tests for findFirstPartByMimeType(Part part, String mimeType)