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

Barry Warsaw barry@wooz.org
Fri, 14 Jun 2002 15:24:29 -0400


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

Modified Files:
	HTTPCharsets.py 
Log Message:
getPreferredCharsets(): RFC 2616, $14.2 says that if * is not present,
then all charsets get a quality of 0, except iso-8859-1 which gets a
quality of 1 if not explicitly mentioned.


=== Zope3/lib/python/Zope/Publisher/HTTP/HTTPCharsets.py 1.3 => 1.4 ===
         '''See interface IUserPreferredCharsets'''
         charsets = []
+        sawstar = sawiso88591 = 0
         for charset in self.request.get('HTTP_ACCEPT_CHARSET', '').split(','):
             charset = charset.strip().lower()
             if charset:
@@ -58,10 +59,21 @@
                     quality = 1.0
                 if quality == 0.0:
                     continue
+                if charset == '*':
+                    sawstar = 1
+                if charset == 'iso-8859-1':
+                    sawiso88591 = 1
                 charsets.append((quality, charset))
-        # UTF-8 is **always** preferred over anything else
+        # Quoting RFC 2616, $14.2: If no "*" is present in an Accept-Charset
+        # field, then all character sets not explicitly mentioned get a
+        # quality value of 0, except for ISO-8859-1, which gets a quality
+        # value of 1 if not explicitly mentioned.
+        if not sawstar and not sawiso88591:
+            charsets.append((1.0, 'iso-8859-1'))
+        # UTF-8 is **always** preferred over anything else.
+        # XXX Please give more details as to why!
         charsets.sort(sort_charsets)
-        return map(lambda c: c[1], charsets)
+        return [c[1] for c in charsets]
 
     #
     ############################################################