Issue 18099: wsgiref sets Content-Length: 0 on 304 Not Modified (original) (raw)

While testing wsgiref w.r.t RFC2616, I noticed that it sets the Content-Length to 0 when the application returns 304 Not Modified.

This is a violation of RFC 2616 section 10.3.5.

And the net effect is a weird bug with some browsers (tested with Chrome 27) where the resource is truncated on 304 Not Modified. (the resource was a CSS file in that case)

This is loosely related to http://bugs.python.org/issue3839.

How to reproduce:

def test_304(): import time from email.utils import formatdate from wsgiref.simple_server import make_server

def demo_app(environ, start_response):
    if 'HTTP_IF_MODIFIED_SINCE' in environ:
        start_response("304 Not Modified", [])
        return []
    headers = [('Content-Type', 'text/html; charset=utf-8'),
               ('Last-Modified', formatdate(time.time(), usegmt=True))]
    start_response("200 OK", headers)
    return ["Hello World!"]

httpd = make_server('127.0.0.1', 8000, demo_app)
sa = httpd.socket.getsockname()
print("Serving HTTP on %s port %s ..." % sa)
httpd.serve_forever()

if name == 'main': test_304()