private static int parseParameters(Map<String,String> parameters, String header, int offset) {
while (true) {
int equalsIndex = header.indexOf('=', offset);
if (equalsIndex < 0)
throw new BadRequestException("Malformed parameters: " + header);
String name = header.substring(offset, equalsIndex).trim();
offset = equalsIndex + 1;
if (header.charAt(offset) == '"') {
int end = offset;
++offset;
do {
end = header.indexOf('"', ++end);
if (end < 0)
throw new BadRequestException("Quoted string is not closed: " + header);
} while (header.charAt(end - 1) == '\\');
String value = header.substring(offset, end);
parameters.put(name, value);
offset = end + 1;
int parameterEndIndex = header.indexOf(';', offset);
int itemEndIndex = header.indexOf(',', offset);
if (parameterEndIndex == itemEndIndex) {
assert itemEndIndex == -1;
if (header.substring(offset).trim().length() != 0)
throw new BadRequestException("Tailing garbage: " + header);
return -1;
} else if (parameterEndIndex < 0 || (itemEndIndex >= 0 && itemEndIndex < parameterEndIndex)) {
if (header.substring(offset, itemEndIndex).trim().length() != 0)
throw new BadRequestException("Garbage after quoted string: " + header);
return itemEndIndex + 1;
} else {
if (header.substring(offset, parameterEndIndex).trim().length() != 0)
throw new BadRequestException("Garbage after quoted string: " + header);
offset = parameterEndIndex + 1;
}
} else {
int parameterEndIndex = header.indexOf(';', offset);
int itemEndIndex = header.indexOf(',', offset);