MediaType parsing supports comma inside quotes

Issue: SPR-17459
This commit is contained in:
Dimitrios Liapis
2018-11-07 00:03:20 +01:00
committed by Rossen Stoyanchev
parent 46a5fb7a91
commit f4b05dc2e7
4 changed files with 139 additions and 11 deletions

View File

@@ -44,6 +44,7 @@ import org.springframework.util.StringUtils;
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Kazuki Shimizu
* @author Dimitrios Liapis
* @since 3.0
* @see <a href="http://tools.ietf.org/html/rfc7231#section-3.1.1.1">
* HTTP 1.1: Semantics and Content, section 3.1.1.1</a>
@@ -552,12 +553,24 @@ public class MediaType extends MimeType implements Serializable {
if (!StringUtils.hasLength(mediaTypes)) {
return Collections.emptyList();
}
String[] tokens = StringUtils.tokenizeToStringArray(mediaTypes, ",");
List<MediaType> result = new ArrayList<>(tokens.length);
for (String token : tokens) {
result.add(parseMediaType(token));
boolean isQuoted = false;
int nextBeginIndex = 0;
List<MediaType> tokens = new ArrayList<>();
for(int i = 0; i < mediaTypes.length() - 1; i++) {
//tokenizing on commas that are not within double quotes
if(mediaTypes.charAt(i) == ',' && !isQuoted) {
tokens.add(parseMediaType(mediaTypes.substring(nextBeginIndex, i)));
nextBeginIndex = i + 1;
//ignoring escaped double quote within double quotes
} else if(isQuoted && mediaTypes.charAt(i) == '"' && mediaTypes.charAt(i-1) == '\\') {
continue;
} else if(mediaTypes.charAt(i) == '"') {
isQuoted = !isQuoted;
}
}
return result;
//either the last part of the tokenization or the original string
tokens.add(parseMediaType(mediaTypes.substring(nextBeginIndex)));
return tokens;
}
/**