[Web-SIG] PEP 444 / WSGI2 Proposal: Filters to suppliment middleware.

Ian Bicking ianb at colorstudy.com
Tue Dec 14 21:02:33 CET 2010


On Sun, Dec 12, 2010 at 9:59 PM, Alice Bevan–McGregor
<alice at gothcandy.com>wrote:

> Howdy!
>
> There's one issue I've seen repeated a lot in working with WSGI1 and that
> is the use of middleware to process incoming data, but not outgoing, and
> vice-versa; middleware which filters the output in some way, but cares not
> about the input.
>
> Wrapping middleware around an application is simple and effective, but
> costly in terms of stack allocation overhead; it also makes debugging a bit
> more of a nightmare as the stack trace can be quite deep.
>
> My updated draft PEP 444[1] includes a section describing Filters, both
> ingress (input filtering) and egress (output filtering).  The API is
> trivially simple, optional (as filters can be easily adapted as middleware
> if the host server doesn't support filters) and easy to implement in a
> server.  (The Marrow HTTP/1.1 server implements them as two for loops.)
>

It's not clear to me how this can be composed or abstracted.

@webob.dec.wsgify does kind of handle this with its request/response
pattern; in a simplified form it's like:

def wsgify(func):
    def replacement(environ):
        req = Request(environ)
        resp = func(req)
        return resp(environ)
    return replacement

This allows you to do an output filter like:

@wsgify
def output_filter(req):
    resp = some_app(req.environ)
    fiddle_with_resp(resp)
    return resp

(Most output filters also need the request.)  And an input filter like:

@wsgify
def input_filter(req):
    fiddle_with_req(req)
    return some_app


But while it handles the input filter case, it doesn't try to generalize
this or move application composition into the server.  An application is an
application and servers are imagined but not actually concrete.  If you
handle filters at the server level you have to have some way of registering
these filters, and it's unclear what order they should be applied.  At
import?  Does the server have to poke around in the app it is running?  How
can it traverse down if you have dispatching apps (like paste.urlmap or
Routes)?

You can still implement this locally of course, as a class that takes an app
and input and output filters.


-- 
Ian Bicking  |  http://blog.ianbicking.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/web-sig/attachments/20101214/ffe3e838/attachment.html>


More information about the Web-SIG mailing list