[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/Browser - BrowserCharsets.py:1.2

Fred L. Drake, Jr. fdrake@acm.org
Fri, 14 Jun 2002 11:12:14 -0400


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher/Browser
In directory cvs.zope.org:/tmp/cvs-serv28814

Modified Files:
	BrowserCharsets.py 
Log Message:
Ensure reasonable handling of some cases of "broken" input:
- quality values which are not valid floats are converted to 0
- charset parameters which are not quality values are ignored

If a quality value is 0.0, do not include that charset in the return.

Added tests.


=== Zope3/lib/python/Zope/Publisher/Browser/BrowserCharsets.py 1.1 => 1.2 ===
     def getPreferredCharsets(self):
         '''See interface IUserPreferredCharsets'''
+        # XXX If the browser supplies a "quality value" of 0.0, we
+        # shouldn't simply remove it from the list, but specifically
+        # return a separate list of "denied" charsets so we don't use
+        # them as our interpretation of the "*" charset.
         charsets = []
         for charset in self.request['HTTP_ACCEPT_CHARSET'].split(','):
             charset = charset.strip()
             if charset:
                 if ';' in charset:
                     charset, quality = charset.split(';')
-                    quality = float(quality[2:])
+                    if not (quality.startswith('q=')
+                            or quality.startswith('Q=')):
+                        # not a quality parameter
+                        quality = 1.0
+                    else:
+                        try:
+                            quality = float(quality[2:])
+                        except ValueError:
+                            continue
                 else:
                     quality = 1.0
-                charsets.append((quality, charset))
+                if quality == 0.0:
+                    continue
+                charsets.append((quality, charset.lower()))
         charsets.sort()
         charsets.reverse()
         return map(lambda c: c[1], charsets)