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
This commit is contained in:
Andy Stadler 2010-09-27 15:16:24 -07:00
parent 658df8f69f
commit ac916da48e
2 changed files with 32 additions and 13 deletions

View File

@ -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;
}
}

View File

@ -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)