Skip to content

Commit b715fac

Browse files
committed
Issue python#3839: wsgiref should not override a Content-Length header set by
the application. Initial patch by Clovis Fabricio.
1 parent f1397ad commit b715fac

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

Lib/test/test_wsgiref.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@ def trivial_app3(e,s):
520520
s('200 OK',[])
521521
return ['\u0442\u0435\u0441\u0442'.encode("utf-8")]
522522

523+
def trivial_app4(e,s):
524+
# Simulate a response to a HEAD request
525+
s('200 OK',[('Content-Length', '12345')])
526+
return []
527+
523528
h = TestHandler()
524529
h.run(trivial_app1)
525530
self.assertEqual(h.stdout.getvalue(),
@@ -543,10 +548,12 @@ def trivial_app3(e,s):
543548
b'\r\n'
544549
b'\xd1\x82\xd0\xb5\xd1\x81\xd1\x82')
545550

546-
547-
548-
549-
551+
h = TestHandler()
552+
h.run(trivial_app4)
553+
self.assertEqual(h.stdout.getvalue(),
554+
b'Status: 200 OK\r\n'
555+
b'Content-Length: 12345\r\n'
556+
b'\r\n')
550557

551558
def testBasicErrorOutput(self):
552559

Lib/wsgiref/handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,9 @@ def sendfile(self):
302302
def finish_content(self):
303303
"""Ensure headers and content have both been sent"""
304304
if not self.headers_sent:
305-
self.headers['Content-Length'] = "0"
305+
# Only zero Content-Length if not set by the application (so
306+
# that HEAD requests can be satisfied properly, see #3839)
307+
self.headers.setdefault('Content-Length', "0")
306308
self.send_headers()
307309
else:
308310
pass # XXX check if content-length was too short?

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Paul Everitt
261261
David Everly
262262
Greg Ewing
263263
Martijn Faassen
264+
Clovis Fabricio
264265
Andreas Faerber
265266
Bill Fancher
266267
Troy J. Farrell

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- Issue #3839: wsgiref should not override a Content-Length header set by
34+
the application. Initial patch by Clovis Fabricio.
35+
3336
- Issue #10492: bdb.Bdb.run() only traces the execution of the code, not the
3437
compilation (if the input is a string).
3538

0 commit comments

Comments
 (0)