SVN: Zope3/branches/3.3/src/zope/publisher/ - LP #253362: better dealing with malformed HTTP_ACCEPT_CHARSET headers

Andreas Jung <[email protected]>
Newsgroups gmane.comp.web.zope.zope3.cvs
Message-ID <[email protected]>
Log message for revision 89417:
  - LP #253362: better dealing with malformed HTTP_ACCEPT_CHARSET headers
  

Changed:
  U   Zope3/branches/3.3/src/zope/publisher/http.py
  U   Zope3/branches/3.3/src/zope/publisher/tests/test_httpcharsets.py

-=-
Modified: Zope3/branches/3.3/src/zope/publisher/http.py
===================================================================
--- Zope3/branches/3.3/src/zope/publisher/http.py	2008-08-05 18:27:55 UTC (rev 89416)
+++ Zope3/branches/3.3/src/zope/publisher/http.py	2008-08-05 18:57:12 UTC (rev 89417)
@@ -998,7 +998,10 @@
             charset = charset.strip().lower()
             if charset:
                 if ';' in charset:
-                    charset, quality = charset.split(';')
+                    try:
+                        charset, quality = charset.split(';')
+                    except ValueError: 
+                        continue
                     if not quality.startswith('q='):
                         # not a quality parameter
                         quality = 1.0

Modified: Zope3/branches/3.3/src/zope/publisher/tests/test_httpcharsets.py
===================================================================
--- Zope3/branches/3.3/src/zope/publisher/tests/test_httpcharsets.py	2008-08-05 18:27:55 UTC (rev 89416)
+++ Zope3/branches/3.3/src/zope/publisher/tests/test_httpcharsets.py	2008-08-05 18:57:12 UTC (rev 89417)
@@ -78,7 +78,12 @@
         self.assertEqual(list(browser_charsets.getPreferredCharsets()),
                          [])
 
-
+    def testMalformedHTTP_ACCEPT_CHARSET(self):
+        """ Test for Launchpad #253362 """
+        request = {'HTTP_ACCEPT_CHARSET': 'utf-8;q=0.7,iso-8859-1;q=0.2*;q=0.1'}
+        browser_charsets = HTTPCharsets(request)
+        self.assertEqual(list(browser_charsets.getPreferredCharsets()),
+                         ['utf-8', 'iso-8859-1'])
 def test_suite():
     loader=unittest.TestLoader()
     return loader.loadTestsFromTestCase(HTTPCharsetTest)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.