documentation.middleware

Viverra comes with several basic 'built-in' middleware classes, providing support for GET and POST form data, and cookies. Designed to work with the Viverra mod_python server, they have some shortcuts built in for that particular use case; regardless, they will work with any WSGI-compliant server.

Form Data

The BasicForm class provides middleware that uses only the Python standard library (in particular, the cgi module) to read form variables and place them, in the form of a cgi.FieldStorage object, in the viverra.form key in the WSGI environment. It will read from, if available, the request object (in the environment dictionary under viverra.request), or if necessary the environment's query string. It then passes the request along by calling thhe application or the next middleware object in the stack.

The ModPyForm class performs the same functions as BasicForm, but uses mod_python's utility functions instead of those from the CGI module. This provides for better functioning when the Viverra stack is being used on mod_python, and the mod_python request object can be found in the environment dictionary under the viverra.request key.

As with forms, there are two cookie classes provided: BasicCookie, which uses the standard library's Cookie module, and ModPyCookie, which takes advantage of mod_python's utility functions. Both classes provide the same functionality: reading incoming cookies, either from the mod_python request object or from the environment's HTTP_COOKIE value, and placing them in the environment's viverra.cookies key as a dictionary-like object; and providing a set_cookie method on that object to set cookies on the current request if necessary. They also define a closure to intercept the outgoing HTTP headers and ensure the cookie headers are set correctly.