[Checkins] SVN: zope.publisher/trunk/src/zope/publisher/ Adds support to zope.publisher for unicode output of XML content whose

Tres Seaver tseaver at palladion.com
Sat Apr 24 14:30:51 EDT 2010


Log message for revision 111385:
  Adds support to zope.publisher for unicode output of XML content whose
  mimetype does not begin with text/, per RFC 3023 as well as for
  content-types ending in "+xml" such as Mozilla XUL's application/vnd+xml.
  

Changed:
  U   zope.publisher/trunk/src/zope/publisher/http.py
  U   zope.publisher/trunk/src/zope/publisher/tests/test_http.py

-=-
Modified: zope.publisher/trunk/src/zope/publisher/http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/http.py	2010-04-24 18:29:09 UTC (rev 111384)
+++ zope.publisher/trunk/src/zope/publisher/http.py	2010-04-24 18:30:51 UTC (rev 111385)
@@ -49,6 +49,10 @@
 # Default Encoding
 ENCODING = 'UTF-8'
 
+# not just text/* but RFC 3023 and */*+xml
+import re
+unicode_mimetypes_re = re.compile(r"^text\/.*$|^.*\/xml.*$|^.*\+xml$")
+
 eventlog = logging.getLogger('eventlog')
 
 class CookieMapper(RequestDataMapper):
@@ -795,13 +799,9 @@
         content_type = self.getHeader('content-type')
 
         if isinstance(body, unicode):
-            try:
-                if not content_type.startswith('text/'):
-                    raise ValueError(
-                        'Unicode results must have a text content type.')
-            except AttributeError:
-                    raise ValueError(
-                        'Unicode results must have a text content type.')
+            if not unicode_mimetypes_re.match(content_type):
+                raise ValueError(
+                    'Unicode results must have a text, RFC 3023, or +xml content type.')
 
             major, minor, params = zope.contenttype.parse.parse(content_type)
 

Modified: zope.publisher/trunk/src/zope/publisher/tests/test_http.py
===================================================================
--- zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2010-04-24 18:29:09 UTC (rev 111384)
+++ zope.publisher/trunk/src/zope/publisher/tests/test_http.py	2010-04-24 18:30:51 UTC (rev 111385)
@@ -793,6 +793,37 @@
         eq("text/plain;charset=cp1251", headers["Content-Type"])
         eq("test", body)
 
+        # see https://bugs.launchpad.net/zope.publisher/+bug/98395
+        # RFC 3023 types and */*+xml output as unicode
+
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
+            {"content-type": "text/xml"})
+        eq("text/xml;charset=utf-8", headers["Content-Type"])
+        eq("test", body)
+
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
+            {"content-type": "application/xml"})
+        eq("application/xml;charset=utf-8", headers["Content-Type"])
+        eq("test", body)
+
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
+            {"content-type": "text/xml-external-parsed-entity"})
+        eq("text/xml-external-parsed-entity;charset=utf-8", headers["Content-Type"])
+        eq("test", body)
+
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
+            {"content-type": "application/xml-external-parsed-entity"})
+        eq("application/xml-external-parsed-entity;charset=utf-8", headers["Content-Type"])
+        eq("test", body)
+
+        # Mozilla XUL
+        headers, body = self._getResultFromResponse(u"test", "utf-8",
+            {"content-type": "application/vnd+xml"})
+        eq("application/vnd+xml;charset=utf-8", headers["Content-Type"])
+        eq("test", body)
+
+        # end RFC 3023 / xml as unicode
+
         headers, body = self._getResultFromResponse("test", "utf-8",
             {"content-type": "image/gif"})
         eq("image/gif", headers["Content-Type"])



More information about the checkins mailing list