KeepSafe/aiohttp — pull request #209 by asvetlov (2014-12-27 20:59:02+00:00)
Title: Middleware, second edition

`Application` accepts `middlewares` keyword-only parameter, which should be sequence of *middleware factories*.

Every factory is a coroutine that accepts two parameters: `app` (`Application` instance) and `handler` (next handler in middleware chain. The last handler is `web-handler` selected by routing itself. Middleware should return new coroutine by wrapping `handler` parameter. Signature of returned handler should be the same as for `web-handler`: accept single `request` parameter, return response or raise exception. Factory is coroutine, thus it can do extra `yield from` calls on making new handler.

After constructing outermost handler by applying middleware chain to web-handler in reversed order `RequestHandler` executes that outermost handler as regular web-handler.

N.B. Middlewares are executed **after** routing, so it cannot process route exceptions.

Thoughts?