Skip to content

Commit

Permalink
Handle trailing semicolon in Accept-Language
Browse files Browse the repository at this point in the history
Closes gh-32259
  • Loading branch information
poutsma committed Jun 12, 2024
1 parent 1d0af7e commit 099d016
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,20 @@ public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
*/
public List<Locale.LanguageRange> getAcceptLanguage() {
String value = getFirst(ACCEPT_LANGUAGE);
return (StringUtils.hasText(value) ? Locale.LanguageRange.parse(value) : Collections.emptyList());
if (StringUtils.hasText(value)) {
try {
return Locale.LanguageRange.parse(value);
}
catch (IllegalArgumentException ignored) {
String[] tokens = StringUtils.tokenizeToStringArray(value, ",");
for (int i = 0; i < tokens.length; i++) {
tokens[i] = StringUtils.trimTrailingCharacter(tokens[i], ';');
}
value = StringUtils.arrayToCommaDelimitedString(tokens);
return Locale.LanguageRange.parse(value);
}
}
return Collections.emptyList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,20 @@ void acceptLanguage() {
assertThat(headers.getAcceptLanguageAsLocales()).first().isEqualTo(Locale.FRANCE);
}

@Test // gh-32259
void acceptLanguageTrailingSemicolon() {
String headerValue = "en-us,en;,nl;";
headers.set(HttpHeaders.ACCEPT_LANGUAGE, headerValue);
assertThat(headers.getFirst(HttpHeaders.ACCEPT_LANGUAGE)).isEqualTo(headerValue);

List<Locale.LanguageRange> expectedRanges = Arrays.asList(
new Locale.LanguageRange("en-us"),
new Locale.LanguageRange("en"),
new Locale.LanguageRange("nl")
);
assertThat(headers.getAcceptLanguage()).isEqualTo(expectedRanges);
}

@Test // SPR-15603
void acceptLanguageWithEmptyValue() {
this.headers.set(HttpHeaders.ACCEPT_LANGUAGE, "");
Expand Down

0 comments on commit 099d016

Please sign in to comment.