[Zope3-checkins] SVN: Zope3/branches/ZopeX3-3.0/ Don't unconditionally add Content-Length header in HTTPResponse.getHeaders().

Bjorn Tillenius bjoti777 at student.liu.se
Sat Mar 19 19:19:47 EST 2005


Log message for revision 29588:
  Don't unconditionally add Content-Length header in HTTPResponse.getHeaders().
  Do it only in output(), where the size of the data is known.
  

Changed:
  U   Zope3/branches/ZopeX3-3.0/doc/CHANGES.txt
  U   Zope3/branches/ZopeX3-3.0/src/zope/publisher/http.py
  U   Zope3/branches/ZopeX3-3.0/src/zope/publisher/tests/test_http.py

-=-
Modified: Zope3/branches/ZopeX3-3.0/doc/CHANGES.txt
===================================================================
--- Zope3/branches/ZopeX3-3.0/doc/CHANGES.txt	2005-03-19 23:46:19 UTC (rev 29587)
+++ Zope3/branches/ZopeX3-3.0/doc/CHANGES.txt	2005-03-20 00:19:47 UTC (rev 29588)
@@ -22,6 +22,10 @@
       - If the content-type was text/*, HTTPResponse.write re-set the
         content-length header to the first chunk of data.
 
+      - Don't unconditionally add Content-Length header in
+        HTTPResponse.getHeaders(). Do it only in output(), where the
+        size of the data is known.
+
     Much thanks to everyone who contributed to this release:
 
       Jim Fulton, Stephan Richter, Bjorn Tillenius

Modified: Zope3/branches/ZopeX3-3.0/src/zope/publisher/http.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/publisher/http.py	2005-03-19 23:46:19 UTC (rev 29587)
+++ Zope3/branches/ZopeX3-3.0/src/zope/publisher/http.py	2005-03-20 00:19:47 UTC (rev 29588)
@@ -634,10 +634,6 @@
         result = {}
         headers = self._headers
 
-        if (not ('content-length' in headers)
-            and not ('transfer-encoding' in headers)):
-            self._updateContentLength()
-
         result["X-Powered-By"] = "Zope (www.zope.org), Python (www.python.org)"
 
         for key, val in headers.items():
@@ -890,8 +886,9 @@
         self._outstream.write(string)
 
     def output(self, data):
-        """Output the data to the world. There are a couple of steps we have
-        to do:
+        """Output the data to the world.
+        
+        There are a couple of steps we have to do:
 
         1. Check that there is a character encoding for the data. If not,
            choose UTF-8. Note that if the charset is None, this is a sign of a
@@ -904,6 +901,9 @@
 
         3. If the content type is text-based, let's encode the data and send
            it also out the door.
+
+        4. Make sure that a Content-Length or Transfer-Encoding header is
+           present.
         """
         if self._charset is None:
             self.setCharset('utf-8')
@@ -911,6 +911,10 @@
         if self.getHeader('content-type', '').startswith('text'):
             data = self._encode(data)
             self._updateContentLength(data)
+        
+        if (not ('content-length' in self._headers)
+            and not ('transfer-encoding' in self._headers)):
+            self._updateContentLength()
 
         self.write(data)
 

Modified: Zope3/branches/ZopeX3-3.0/src/zope/publisher/tests/test_http.py
===================================================================
--- Zope3/branches/ZopeX3-3.0/src/zope/publisher/tests/test_http.py	2005-03-19 23:46:19 UTC (rev 29587)
+++ Zope3/branches/ZopeX3-3.0/src/zope/publisher/tests/test_http.py	2005-03-20 00:19:47 UTC (rev 29588)
@@ -472,6 +472,26 @@
         self.assertEqual(headers['Content-Length'], str(len(data)))
         self.assertEqual(body, data)
 
+    def testWrite_noContentLength(self):
+        response, stream = self._createResponse()
+        data = 'a'*10
+        # We have to set all the headers ourself, we choose not to provide a
+        # content-length header
+        response.setHeader('Content-Type', 'text/plain;charset=us-ascii')
+
+        # Stream the data
+        for ch in data:
+            response.write(ch)
+
+        headers, body = self._parseResult(stream.getvalue())
+        # Check that the data have been written, and that the header
+        # has been preserved   
+        self.assertEqual(headers['Content-Type'], 'text/plain;charset=us-ascii')
+        self.assertEqual(body, data)
+
+        # Make sure that no Content-Length header was added
+        self.assert_('Content-Length' not in headers)
+
     def testContentLength(self):
         eq = self.failUnlessEqual
 



More information about the Zope3-Checkins mailing list