[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher/HTTP - HTTPCharsets.py:1.3

Fred L. Drake, Jr. fdrake@acm.org
Fri, 14 Jun 2002 13:33:32 -0400


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

Modified Files:
	HTTPCharsets.py 
Log Message:
Move my changes from BrowserCharsets (now obsolete) to HTTPCharsets,
and update affected tests.
Remove the now-obslete BrowserCharsets module and its test.


=== Zope3/lib/python/Zope/Publisher/HTTP/HTTPCharsets.py 1.2 => 1.3 ===
 
 def sort_charsets(x, y):
-    if y[1] == 'UTF-8':
+    if y[1] == 'utf-8':
         return 1
-    if x[1] == 'UTF-8':
+    if x[1] == 'utf-8':
         return -1    
     return cmp(y, x)
 
@@ -42,13 +42,22 @@
         '''See interface IUserPreferredCharsets'''
         charsets = []
         for charset in self.request.get('HTTP_ACCEPT_CHARSET', '').split(','):
-            charset = charset.strip()
+            charset = charset.strip().lower()
             if charset:
                 if ';' in charset:
                     charset, quality = charset.split(';')
-                    quality = float(quality[2:])
+                    if not quality.startswith('q='):
+                        # not a quality parameter
+                        quality = 1.0
+                    else:
+                        try:
+                            quality = float(quality[2:])
+                        except ValueError:
+                            continue
                 else:
                     quality = 1.0
+                if quality == 0.0:
+                    continue
                 charsets.append((quality, charset))
         # UTF-8 is **always** preferred over anything else
         charsets.sort(sort_charsets)