I've added java servlet-style filters to Webware, and would like to
contribute the code to the project, if people are interested.
I've attached files and patches -- it's a relatively small amount of
code. A few patches to Application.py and Transaction.py, and then
four new files. (Note: I *think* I got all the tabs right, but I'm
not sure ;-)
I've been working with filters for a while (I wrote the feature
against 0.8.1, and just found time to port the patches to 0.9.x), and
have found them to be enormously useful.
Here's the basic idea:
- In Application.config, the user can specify a sequence of (regex,
Filter) pairs, like so:
FilterMappings = [ (r'/', 'filters.Authorizer'),
(r'/admin/', 'filters.admin'),
(r'/customers/', 'filters.customers'),
(r'/users/', 'filters.users') ]
Each Filter will be a subclass of WebKit.Filter and must define a
filter(trans, next) method.
- When the Application receives a new request, it constructs a
'chain' of Filters which match the URL
E.g. in the example above, for a request to /users/3/view, that chain
would be: [ filters.Authorizer(), filters.users() ]
- The Application then calls the filter(trans, next) method of the
first Filter in the chain
After examining and possibly modifying the transaction, each filter
can pass control on to the succeeding one by calling next(trans).
Or, if it can terminate the chain by just returning.
- If all the filters execute and pass control to next(), control
returns to the Application, which will lookup and execute a servlet
as usual
Some uses:
- Path-based Authorization
An Authorizer filter can look in the session for credentials, and, if
none are found, can redirect to a login page (after storing the
originally request URL in the session, possibly)
- Move parameters from the URL to the Request (REST-style)
Given a request of form:
/users/3/<serv>
a filter can modify the URL to be /users/<serv>, and store user_id=3
in the request.
- Logging
- Compression
Many others.
Basically, filters provide the user a clean, simple way to construct
their own request-handling pipeline.
If people are interested, I can write up some documentation.
-Dan Milstein
|