[Zope3-checkins] SVN: Zope3/trunk/src/zope/publisher/ Fixed a bug in the publisher reported by Roger.

Stephan Richter srichter at cosmos.phy.tufts.edu
Mon Sep 19 14:10:56 EDT 2005


Log message for revision 38524:
  Fixed a bug in the publisher reported by Roger.
  
  When the base is inserted into the body, the body is already encoded t a 
  string. If the base is a unicode string, then the entire body used to end 
  up as a unicode string. This bug was fixed by applying the correct 
  encoding on the base URL, if it is a unicode string.
  
  

Changed:
  U   Zope3/trunk/src/zope/publisher/browser.py
  U   Zope3/trunk/src/zope/publisher/tests/test_browserresponse.py

-=-
Modified: Zope3/trunk/src/zope/publisher/browser.py
===================================================================
--- Zope3/trunk/src/zope/publisher/browser.py	2005-09-19 18:09:04 UTC (rev 38523)
+++ Zope3/trunk/src/zope/publisher/browser.py	2005-09-19 18:10:56 UTC (rev 38524)
@@ -31,6 +31,7 @@
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from zope.publisher.interfaces.browser import IBrowserApplicationRequest
 
+from zope.publisher import contenttype
 from zope.publisher.http import HTTPRequest, HTTPResponse
 from zope.publisher.base import BaseRequest
 
@@ -686,15 +687,21 @@
         if content_type and not is_text_html(content_type):
             return body
 
-        if getattr(self, '_base', ''):
+        if self.getBase():
             if body:
                 match = start_of_header_search(body)
                 if match is not None:
                     index = match.start(0) + len(match.group(0))
                     ibase = base_re_search(body)
                     if ibase is None:
+                        base = self.getBase()
+                        # Make sure the base URL is not a unicode string.
+                        if isinstance(base, unicode):
+                            ma, mi, params = contenttype.parse(content_type)
+                            encoding = params.get('charset', 'utf8')
+                            base = base.encode(encoding)
                         body = ('%s\n<base href="%s" />\n%s' %
-                                (body[:index], self._base, body[index:]))
+                                (body[:index], base, body[index:]))
         return body
 
     def getBase(self):

Modified: Zope3/trunk/src/zope/publisher/tests/test_browserresponse.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_browserresponse.py	2005-09-19 18:09:04 UTC (rev 38523)
+++ Zope3/trunk/src/zope/publisher/tests/test_browserresponse.py	2005-09-19 18:10:56 UTC (rev 38524)
@@ -81,6 +81,31 @@
             response.getHeader('content-type').startswith("text/plain")
             )
 
+
+    def testInsertBase(self):
+        response = BrowserResponse()
+        response.setHeader('content-type', 'text/html')
+
+        insertBase = response._BrowserResponse__insertBase
+
+        # Make sure that bases are inserted
+        response.setBase('http://localhost/folder/')
+        self.assert_(
+            '<base href="http://localhost/folder/" />' in
+            insertBase('<html><head></head><body>Page</body></html>'))
+
+        # Ensure that unicode bases work as well
+        response.setBase(u'http://localhost/folder/')
+        body = insertBase('<html><head></head><body>Page</body></html>')
+        self.assert_(isinstance(body, str))
+        self.assert_('<base href="http://localhost/folder/" />' in body)
+
+        response.setBase(u'http://localhost/\xdcbung')
+        result = insertBase('<html><head></head><body>Page</body></html>')
+        self.assert_(isinstance(body, str))
+        self.assert_('<base href="http://localhost/\xc3\x9cbung" />' in result)
+
+
     def test_interface(self):
         from zope.publisher.interfaces.http import IHTTPResponse
         from zope.publisher.interfaces.http import IHTTPApplicationResponse



More information about the Zope3-Checkins mailing list