]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: fix Accept-Language header parsing 42297/head
author胡玮文 <huww98@outlook.com>
Mon, 5 Jul 2021 13:27:57 +0000 (21:27 +0800)
committerLaura Paduano <lpaduano@suse.com>
Tue, 13 Jul 2021 08:37:25 +0000 (10:37 +0200)
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>
(cherry picked from commit c7b2d079a3323765e2c56e24309c89728c1b8b6a)

src/pybind/mgr/dashboard/controllers/home.py
src/pybind/mgr/dashboard/tests/test_home.py

index 793b241dd75597ce63a9e0224b9c041af5a94a59..cb4a4a5a18533188387c292e2bb648a5a468ddc2 100644 (file)
@@ -59,7 +59,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 37e09b4b2af9e2fcbe2a203e695d688937ac3690..1b1b6b7063902f04a571bdc715e7320bf6e63dd5 100644 (file)
@@ -65,3 +65,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)