KeepSafe/aiohttp — pull request #189 by asvetlov (2014-12-07 20:18:10+00:00)
Title: Middleware

Please review.

Long story short: the most trivial aiohttp.web middleware is a coroutine like:

@asyncio.coroutine
def middleware(request, handler):
return (yield from handler(request))

It accepts `request` like regular web handler but has also `handler` parameter.

*handler* is a next web handler in middleware stack.
The most bottom handler is web handler itself (found by `router.resolve` call).
Handlers on top of that are middlewares.

Middleware usually calls descending handler (but may do something other, like displaying 403 Forbidden page or raising HTTPForbiddenException if user has no permissions to access underlying resource).
Also middleware may render errors raised by handler, do some pre- and post- processing like handling CORS and so on.

The concept is pretty widespread: Pyramid has tweens with similar functionality for example.

The PR has no tests, examples and docs -- I'll do it later but before merging.