close Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Initial Version and Version 1 of RequestFilters

Jun 25, 2008, 1:52:52 PM (14 years ago)



  • RequestFilters

    v1 v1  
     1= Request Filters =
     3Request filters are Python functions that can act on the request/response before/after the actual [RequestHandlers request handler] is invoked. The application establishes a chain of request filters that are to be applied. Request filters generally look as follows:
     7def my_filter(request, response, chain):
     8    # Here you do anything you need to do before the request handler kicks in
     9    try:
     10        return, response, chain)
     11    finally:
     12        # Here you do anything you want after the request handler has processed the request
     15The `chain` parameter is simply an iterator over the request filter functions, and the last function is the actual request handler.
     17A request filter can of course also manipulate the return value of the handler. For example, if the return value is a Genshi markup stream, it could apply stream filters.
     19== Default Request Filters ==
     21The following request filters are available by default unless you remove them:
     23 `diva.errors.error_filter`:: Intercepts exceptions and displays either a friendly error page or the debugging page
     24 `diva.templating.template_filter`:: Serializes Genshi template output streams
     25 ``:: Injects any programmatically added scripts, style sheets, and links into the output stream
     26 `diva.i18n.l10n_filter`:: Handles locale negotiation and makes the corresponding translations catalog available
     27 `diva.http.caching_filter`:: Adds default cache control headers to responses
     28 `diva.forms.form_filter`:: Automatically populates HTML forms from previously entered data, and provides optional CSRF protection
     30== Setting up Request Filters ==
     32To customize which request filters are run, you can override the `get_filters()` method of the `Application` class. For example:
     36from diva.core import Application
     37from diva.errors import error_filter
     38from diva.http import caching_filter
     39from diva.templating import template_filter
     41class MyApp(Application):
     43    def get_filters(self, environ):
     44        return [
     45            error_filter,
     46            template_filter,
     47            caching_filter
     48        ]
     51This implementation removes a number of the default filters. You could also add any custom filters you may have, etc.
     53Note that the ordering of request filters is quite important. For example, if you insert your custom filter before the `error_filter`, an exception raised in your filter function will not result in a friendly error page, but rather see the exception propagated all the way up. If you insert your filter before the `template_filter`, you will not get a Genshi stream as return value of the next function in the chain, but rather the rendered text.
     55Generally it makes the most sense to add custom filters close to the end of the list.
     57== When to Use Request Filters ==
     59Some things that you may be tempted to implement as request filters would be better done by using WSGI middleware or features of the web server. For example, GZIP compression is available in Apache as a module, and there are various implementations of GZIP compression as WSGI middleware.
     61A good rule of thumb is that request filters are better suited if the functionality needs access to parts of the application or framework.