Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore trailing semicolons when parsing Accept-Language header #32259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -502,6 +502,9 @@ public void setAcceptLanguage(List<Locale.LanguageRange> languages) {
*/
public List<Locale.LanguageRange> getAcceptLanguage() {
String value = getFirst(ACCEPT_LANGUAGE);
if (value != null) {
Copy link

@kmesiab kmesiab Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think a null check is fine, why not also validate whether this string is non empty and at least [n] chars and avoid the noop?

value = StringUtils.trimTrailingCharacter(value, ';');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

';' should be a variable

}
return (StringUtils.hasText(value) ? Locale.LanguageRange.parse(value) : Collections.emptyList());
}

Expand Down
Expand Up @@ -489,6 +489,19 @@ void acceptLanguage() {
assertThat(headers.getAcceptLanguageAsLocales()).element(0).isEqualTo(Locale.FRANCE);
}

@Test
void acceptLanguageTrailingSemicolon() {
String headerValue = "en-us,en;";
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")
);
assertThat(headers.getAcceptLanguage()).isEqualTo(expectedRanges);
}

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