CP WSGI requires a full read of request body

guest avatarguest created an issue

If a request body is not read then it is prepended to the REQUEST_METHOD of the following request.

# encoding: utf-8

import cherrypy.wsgiserver as wsgiserver
from webob import Request, Response

def wsgi_app(environ, start_response):
    print environ['REQUEST_METHOD']
    request = Request(environ)
    
    print request.method
    
    # FIXME: force a read of the request body (workaround for CherryPy)
    # request.body
    
    response = Response(body="<html><body><form method='post'><input name='aaa' /></form></body></html>")
    return response(environ, start_response)


server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', 8080), wsgi_app)

if __name__ == '__main__':
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()

The first POST is OK, the second one has the body of the previous post prepended to the request method:

Console output: GET GET POST POST aaa=aaaaaPOST aaa=aaaaaPOST

Forcing a full read of the body gives a workaround for the problem.

(rev-1956 and 3.1.0-beta3)

Comments (4)

  1. Robert Brewer

    Hmmm. Your demo works fine for me with CP trunk and webob trunk. Since [1786] HTTPRequest.send_headers has said:

    if (not self.close_connection) and (not self.chunked_read):
        # Read any remaining request body data on the socket.
    

    I wonder how you're avoiding that. It's probably not that close_connection is True, because in that case the second request would be on its own conn. Are you doing chunked reads? That seems highly unlikely, but even if you were, wsgiserver currently reads the entire body into a StringIO before calling the app. I'm baffled.

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.