This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: wsgi.simple_server resets 'Content-Length' header on empty content even if app defined it.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: kxroberto, nosklo, pitrou, pje
Priority: normal Keywords: patch

Created on 2008-09-11 18:07 by nosklo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit nosklo, 2008-09-11 18:07 simple patch to wsgiref/ to fix it. review
Messages (4)
msg73045 - (view) Author: Clovis Fabricio (nosklo) * Date: 2008-09-11 18:07
The finish_content method in wsgiref.handlers.BaseHandler class resets
Content-Length to "0" if there was no content returned by the
application, even if the application has set it already.

That makes impossible to implement things like HEAD requests in the
application, which should return the Content-Length without returning
actual content.

The desired behavior would be to respect the header set by the wsgi
application, using "0" as a default value instead.
msg109562 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-08 18:12
As the patch is a one liner couldn't someone pick this up?  I would have done it myself but just don't understand wsgi at all.
msg125569 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-01-06 17:19
Fixed with tests in r87797 (3.2), r87798 (3.1) and r87799 (2.7). Thank you!
msg293323 - (view) Author: kxroberto (kxroberto) Date: 2017-05-09 15:25
However, setting a default "0" when no content, that is still too much in general. 

In case of a '304 Not Modified' for example (which is probably the most frequent HTTP status used on the web overall!) a Content-Length header  obviously is disallowed at all according to

"this prevents inconsistencies between cached entity-bodies and updated headers"

Apache, NGINX and other servers observed indeed do not set Content-Length in 304.  And there were bugfix issues regarding that.

Some browsers obviously pick up a Content-Length "0", update the cached resource and thus zero the cached data. Literally obeying "If a cache uses a received 304 response to update a cache entry, the cache MUST update the entry to reflect any new field values given in the response."  (Though that seems rather silly, as that would mean "Modified". And Content-Length should reasonably perhaps be better always associated with the current transmission for needs of keep-alive connections and buffer management at a lower level, and not with cache & status issues.)

Possibly the same problem for 204.
Date User Action Args
2022-04-11 14:56:39adminsetgithub: 48089
2017-05-09 15:25:31kxrobertosetnosy: + kxroberto
messages: + msg293323
2011-01-06 17:19:41pitrousetstatus: open -> closed
nosy: + pitrou, - BreamoreBoy
messages: + msg125569

resolution: fixed
stage: test needed -> resolved
2010-07-08 18:12:27BreamoreBoysetnosy: + BreamoreBoy

messages: + msg109562
versions: + Python 3.2, - Python 2.6
2009-05-17 02:16:36ajaksu2setpriority: normal
nosy: + pje
versions: - Python 2.5, Python 3.0

stage: test needed
2008-09-11 18:07:51nosklocreate