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

@ -88,6 +88,12 @@ public class MimeUtilityTest extends TestCase {
/** 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:
@ -282,6 +288,16 @@ public class MimeUtilityTest extends TestCase {
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)