From f0783fc291feb774e4b3e58bfc554cafa4d52326 Mon Sep 17 00:00:00 2001 From: Yves Arrouye Date: Wed, 17 Feb 2021 14:17:50 +0000 Subject: [PATCH] Do not expand languages that starts with the primary subtag 'x' or 'i' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Any language that starts with the primary language subtag 'x' is defined by RFC 5646 section 2.2.1 as being defined by private agreement. Therefore, 'x' isn't a language. Similarly, language tags starting with the primarty subtag 'i' such as "i-klingon" are not of the form "language-country" either. Bug: chromium:1179073 Test: unit tests Change-Id: I67d0b090df6941339fbbd0d4337d8db0288d74f8 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2699487 Auto-Submit: Yves Arrouye Commit-Queue: Bence Béky Reviewed-by: Bence Béky Cr-Commit-Position: refs/heads/master@{#854754} --- net/http/http_util.cc | 12 +++++++++--- net/http/http_util_unittest.cc | 6 ++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/net/http/http_util.cc b/net/http/http_util.cc index e9068e553149c7..17bb8d20c8ae2f 100644 --- a/net/http/http_util.cc +++ b/net/http/http_util.cc @@ -787,11 +787,17 @@ std::string HttpUtil::ExpandLanguageList(const std::string& language_prefs) { const std::string& language = languages[i]; builder.AddLanguageCode(language); - // Extract the base language + // Extract the primary language subtag. const std::string& base_language = GetBaseLanguageCode(language); - // Look ahead and add the base language if the next language is not part - // of the same family. + // Skip 'x' and 'i' as a primary language subtag per RFC 5646 section 2.1.1. + if (base_language == "x" || base_language == "i") + continue; + + // Look ahead and add the primary language subtag as a language if the next + // language is not part of the same family. This may not be perfect because + // an input of "en-US,fr,en" will yield "en-US,en,fr,en" and later make "en" + // a higher priority than "fr" despite the original preference. const size_t j = i + 1; if (j >= size || GetBaseLanguageCode(languages[j]) != base_language) { builder.AddLanguageCode(base_language); diff --git a/net/http/http_util_unittest.cc b/net/http/http_util_unittest.cc index e6291f077972a5..75d09aff47c712 100644 --- a/net/http/http_util_unittest.cc +++ b/net/http/http_util_unittest.cc @@ -1633,6 +1633,12 @@ TEST(HttpUtilTest, ExpandLanguageList) { HttpUtil::ExpandLanguageList("en-US,fr-CA,it,fr,es-AR,it-IT")); // Trims a whitespace. EXPECT_EQ("en-US,en,fr", HttpUtil::ExpandLanguageList("en-US, fr")); + + // Do not expand the single character subtag 'x' as a language. + EXPECT_EQ("x-private-agreement-subtags", + HttpUtil::ExpandLanguageList("x-private-agreement-subtags")); + // Do not expand the single character subtag 'i' as a language. + EXPECT_EQ("i-klingon", HttpUtil::ExpandLanguageList("i-klingon")); } } // namespace net