]> granicus.if.org Git - python/commitdiff
Issue #3839: wsgiref should not override a Content-Length header set by
authorAntoine Pitrou <solipsis@pitrou.net>
Thu, 6 Jan 2011 17:17:04 +0000 (17:17 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Thu, 6 Jan 2011 17:17:04 +0000 (17:17 +0000)
the application.  Initial patch by Clovis Fabricio.

Lib/test/test_wsgiref.py
Lib/wsgiref/handlers.py
Misc/ACKS
Misc/NEWS

index b8cf635129087917f569b6d5f3ac1993ed69bca5..a08f66b7b476b36d07de2cc68a14f9095dbae882 100644 (file)
@@ -520,6 +520,11 @@ class HandlerTests(TestCase):
             s('200 OK',[])
             return ['\u0442\u0435\u0441\u0442'.encode("utf-8")]
 
+        def trivial_app4(e,s):
+            # Simulate a response to a HEAD request
+            s('200 OK',[('Content-Length', '12345')])
+            return []
+
         h = TestHandler()
         h.run(trivial_app1)
         self.assertEqual(h.stdout.getvalue(),
@@ -543,10 +548,12 @@ class HandlerTests(TestCase):
             b'\r\n'
             b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82')
 
-
-
-
-
+        h = TestHandler()
+        h.run(trivial_app4)
+        self.assertEqual(h.stdout.getvalue(),
+            b'Status: 200 OK\r\n'
+            b'Content-Length: 12345\r\n'
+            b'\r\n')
 
     def testBasicErrorOutput(self):
 
index 6d6f80ffd7dc88f3f30cdb0dc3b8009c0b9ecb6f..67064a68756fdcd7df67d722a2803ecdfd7da6f0 100644 (file)
@@ -302,7 +302,9 @@ class BaseHandler:
     def finish_content(self):
         """Ensure headers and content have both been sent"""
         if not self.headers_sent:
-            self.headers['Content-Length'] = "0"
+            # Only zero Content-Length if not set by the application (so
+            # that HEAD requests can be satisfied properly, see #3839)
+            self.headers.setdefault('Content-Length', "0")
             self.send_headers()
         else:
             pass # XXX check if content-length was too short?
index 43abe8d459698c8d948319fc7557d832217735cb..fbc9326e3d2962d73f77fe006a3c9fe928885d40 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -261,6 +261,7 @@ Paul Everitt
 David Everly
 Greg Ewing
 Martijn Faassen
+Clovis Fabricio
 Andreas Faerber
 Bill Fancher
 Troy J. Farrell
index 3fc052aeaa6534f3c18906964bdca3c1884bfb84..bdac346b9aa8a3729334082a6583cdcd653c0dce 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #3839: wsgiref should not override a Content-Length header set by
+  the application.  Initial patch by Clovis Fabricio.
+
 - Issue #10492: bdb.Bdb.run() only traces the execution of the code, not the
   compilation (if the input is a string).