Ticket #318 (closed defect: fixed)

Opened 3 years ago

Last modified 18 months ago

gzipper middleware does not cope with zero length responses.

Reported by: jkp Owned by: ianb
Priority: high Milestone: 1.3
Component: paste Version: released-version
Severity: major Keywords:
Cc: alice@…

Description

If an application starts a response but returns only "" then the gzipper middleware throws an exception:

self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 320, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 615, in __init__
self.handle()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 442, in handle
BaseHTTPRequestHandler.handle(self)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/BaseHTTPServer.py", line 329, in handle
self.handle_one_request()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 437, in handle_one_request
self.wsgi_execute()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 290, in wsgi_execute
self.wsgi_write_chunk(chunk)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 125, in wsgi_write_chunk
"Content returned before start_response called")
RuntimeError: Content returned before start_response called

The reason is that it is testing the result wrongly:

if app_iter:
    response.finish_response(app_iter)

Changing the test to test for None instead fixes the issue:

def __call__(self, environ, start_response):
    if 'gzip' not in environ.get('HTTP_ACCEPT_ENCODING', ''):
        # nothing for us to do, so this middleware will
        # be a no-op:
        return self.application(environ, start_response)
    response = GzipResponse(start_response, self.compress_level)
    app_iter = self.application(environ,
                                response.gzip_start_response)
    if app_iter is not None:
        response.finish_response(app_iter)

    return response.write()

Change History

Changed 2 years ago by GothAlice

  • cc alice@… added

Just encountered this in  WebCore's main site; I'm setting the last_modified and etag headers, set conditional_response to True, and every other request (the ones returning "304 Not Modified") result in the following traceback: (disabling the gzipper middleware solves the problem; I'll temporarily enable compression in the front-side Nginx server instead)

Exception happened during processing of request from ('127.0.0.1', 56131)
Traceback (most recent call last):
  File "/Users/amcgregor/Projects/WebCore/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 1062, in process_request_in_thread
    self.finish_request(request, client_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 320, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/SocketServer.py", line 615, in __init__
    self.handle()
  File "/Users/amcgregor/Projects/WebCore/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 436, in handle
    BaseHTTPRequestHandler.handle(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/BaseHTTPServer.py", line 329, in handle
    self.handle_one_request()
  File "/Users/amcgregor/Projects/WebCore/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 431, in handle_one_request
    self.wsgi_execute()
  File "/Users/amcgregor/Projects/WebCore/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 290, in wsgi_execute
    self.wsgi_write_chunk(chunk)
  File "/Users/amcgregor/Projects/WebCore/lib/python2.6/site-packages/Paste-1.7.2-py2.6.egg/paste/httpserver.py", line 125, in wsgi_write_chunk
    "Content returned before start_response called")
RuntimeError: Content returned before start_response called

Changed 21 months ago by flox

  • priority changed from normal to high
  • version changed from svn-trunk to released-version
  • severity changed from normal to major
  • milestone changed from 1.4.1 to 1.3

Confirmed: both the problem with Paste-1.7.3.1, and the fix.

-    if app_iter:
+    if app_iter is not None:

Changed 18 months ago by ianb

  • status changed from new to closed
  • resolution set to fixed

Should be fixed in 1467:e4084f2376db

Note: See TracTickets for help on using tickets.