]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: fix Accept-Language header parsing 42183/head
author胡玮文 <huww98@outlook.com>
Mon, 5 Jul 2021 13:27:57 +0000 (21:27 +0800)
committer胡玮文 <huww98@outlook.com>
Mon, 5 Jul 2021 13:39:14 +0000 (21:39 +0800)
Fix 400 error caused by multi-subtag language in Accept-Language header.
Reproduced by Firefox for Android in Chinese.

The regex expression is fixed according to https://datatracker.ietf.org/doc/html/rfc4647#section-2.1

Fixes: https://tracker.ceph.com/issues/51528
Signed-off-by: 胡玮文 <huww98@outlook.com>
src/pybind/mgr/dashboard/controllers/home.py
src/pybind/mgr/dashboard/tests/test_home.py

index 5f9454c85cf884380083161744fb2d16c773a53d..809b50617b10299f5581070e292346908bb7501a 100644 (file)
@@ -58,7 +58,7 @@ class LanguageMixin(object):
 class HomeController(BaseController, LanguageMixin):
     LANG_TAG_SEQ_RE = re.compile(r'\s*([^,]+)\s*,?\s*')
     LANG_TAG_RE = re.compile(
-        r'^(?P<locale>[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})?)(;q=(?P<weight>[01]\.\d{0,3}))?$')
+        r'^(?P<locale>[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*|\*)(;q=(?P<weight>[01]\.\d{0,3}))?$')
     MAX_ACCEPTED_LANGS = 10
 
     @lru_cache()
index dc73a6d2ead62279d5725dc5f8706ea772519dfc..b0641f068f30ea483427f2b853f57143448cfd9c 100644 (file)
@@ -64,3 +64,10 @@ class HomeTest(ControllerTestCase, FakeFsMixin):
         self.assertStatus(200)
         logger.info(self.body)
         self.assertIn('<html lang="en">', self.body.decode('utf-8'))
+
+    @mock.patch(FakeFsMixin.builtins_open, new=FakeFsMixin.f_open)
+    @mock.patch('os.stat', new=FakeFsMixin.f_os.stat)
+    @mock.patch('os.listdir', new=FakeFsMixin.f_os.listdir)
+    def test_home_multiple_subtags_lang(self):
+        self._get('/', headers=[('Accept-Language', 'zh-Hans-CN')])
+        self.assertStatus(200)