[Zope3-checkins] CVS: Zope3/src/zope/publisher - http.py:1.30

Stephan Richter srichter@cosmos.phy.tufts.edu
Tue, 29 Jul 2003 15:50:49 -0400


Update of /cvs-repository/Zope3/src/zope/publisher
In directory cvs.zope.org:/tmp/cvs-serv15675/src/zope/publisher

Modified Files:
	http.py 
Log Message:
dreamcatcher had issues with unicode leaking into the server. This was due
to the fact that the headers were not properly encoded. They are now. 

dreamcatcher: it is up to you to test this code with your setup.

XXX: We have to write HTTPResponse Tests... <grin>


=== Zope3/src/zope/publisher/http.py 1.29 => 1.30 ===
--- Zope3/src/zope/publisher/http.py:1.29	Tue Jul 22 11:09:41 2003
+++ Zope3/src/zope/publisher/http.py	Tue Jul 29 15:50:44 2003
@@ -910,18 +910,33 @@
 
 
     def outputHeaders(self):
+        """This method outputs all headers.
+
+        Since it is a final output method, it must take care of all possible
+        unicode strings and encode them! 
+        """
+        encode = self._encode
         headers = self.getHeaders()
+        # Clean these headers from unicode by possibly encoding them
+        items = headers.items()
+        items = map(lambda i: (encode(i[0]), encode(i[1])), items)
+        headers = {}
+        for key, value in items:
+            headers[key] = value
+        # Cleaning done.
         header_output = self._header_output
         if header_output is not None:
             # Use the IHeaderOutput interface.
-            header_output.setResponseStatus(self._status, self._reason)
+            header_output.setResponseStatus(self._status, encode(self._reason))
             header_output.setResponseHeaders(headers)
-            header_output.appendResponseHeaders(self._cookie_list())
-            header_output.appendResponseHeaders(self._accumulated_headers)
+            cookie_list = map(encode, self._cookie_list())
+            header_output.appendResponseHeaders(cookie_list)
+            accumulated_headers = map(encode, self._accumulated_headers)
+            header_output.appendResponseHeaders(accumulated_headers)
         else:
             # Write directly to outstream.
             headers_text = self.getHeaderText(headers)
-            self._outstream.write(headers_text)
+            self._outstream.write(encode(headers_text))
 
 
     def write(self, string):
@@ -947,9 +962,8 @@
             self.outputHeaders()
             self._wrote_headers = True
 
-        if (self.getHeader('content-type', '').startswith('text') and
-               self._charset is not None and type(data) is UnicodeType):
-            data = data.encode(self._charset)
+        if self.getHeader('content-type', '').startswith('text'):
+            data = self._encode(data)
 
         self._outstream.write(data)
 
@@ -959,6 +973,12 @@
         Outputs the response body.
         """
         self.output(self._body)
+
+
+    def _encode(self, text):
+        if self._charset is not None and isinstance(text, unicode):
+            return text.encode(self._charset)
+        return text
 
 
 class DefaultPublisher: