Skip to content

Commit 937bd4f

Browse files
committed
Document middlewares
1 parent df52c32 commit 937bd4f

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

docs/web.rst

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ Response
666666
*Content-Length* HTTP header.
667667

668668

669+
.. _aiohttp-web-app-and-router:
670+
669671
Application and Router
670672
----------------------
671673

@@ -696,7 +698,8 @@ arbitrary properties for later access from
696698
conn.execute("DELETE * FROM table")
697699

698700

699-
.. class:: Application(*, loop=None, router=None, logger=<default>, **kwargs)
701+
.. class:: Application(*, loop=None, router=None, logger=<default>, \
702+
middlewares=(), **kwargs)
700703
701704
The class inherits :class:`dict`.
702705

@@ -715,6 +718,9 @@ arbitrary properties for later access from
715718

716719
By default the value is ``logging.getLogger("aiohttp.web")``
717720

721+
:param middlewares: sequence of middleware factories, see
722+
:ref:`aiohttp-web-middlewares` for details.
723+
718724
:param kwargs: optional params for initializing self dict.
719725

720726
.. attribute:: router
@@ -1038,6 +1044,7 @@ Utilities
10381044

10391045
.. seealso:: :ref:`aiohttp-web-file-upload`
10401046

1047+
.. _aiohttp-web-exceptions:
10411048

10421049
Exceptions
10431050
-----------
@@ -1117,7 +1124,7 @@ Http Exception hierarchy chart::
11171124

11181125
All http exceptions have the same constructor::
11191126

1120-
HTTPNotFound(*, headers=None, reason=None, \
1127+
HTTPNotFound(*, headers=None, reason=None,
11211128
body=None, text=None, content_type=None)
11221129

11231130
if other not directly specified. *headers* will be added to *default
@@ -1127,7 +1134,7 @@ Classes :class:`HTTPMultipleChoices`, :class:`HTTPMovedPermanently`,
11271134
:class:`HTTPFound`, :class:`HTTPSeeOther`, :class:`HTTPUseProxy`,
11281135
:class:`HTTPTemporaryRedirect` has constructor signature like::
11291136

1130-
HTTPFound(location, *, headers=None, reason=None, \
1137+
HTTPFound(location, *, headers=None, reason=None,
11311138
body=None, text=None, content_type=None)
11321139

11331140
where *location* is value for *Location HTTP header*.
@@ -1136,5 +1143,49 @@ where *location* is value for *Location HTTP header*.
11361143
and list of allowed methods::
11371144

11381145
HTTPMethodNotAllowed(method, allowed_methods, *,
1139-
headers=None, reason=None, \
1146+
headers=None, reason=None,
11401147
body=None, text=None, content_type=None)
1148+
1149+
.. _aiohttp-web-middlewares:
1150+
1151+
Middlewares
1152+
-----------
1153+
1154+
:class:`Application` accepts *middlewares* keyword-only parameter,
1155+
which should be sequence of *middleware factories*.
1156+
1157+
The most trivial *middleware factory* example::
1158+
1159+
@asyncio.coroutine
1160+
def middleware_factory(app, handler):
1161+
@asyncio.coroutine
1162+
def middleware(request):
1163+
return (yield from handler(request))
1164+
return middleware
1165+
1166+
Every factory is a coroutine that accepts two parameters: *app*
1167+
(:class:`Application` instance) and *handler* (next handler in
1168+
middleware chain. The last handler is
1169+
:ref:`web-handler<aiohttp-web-handler>` selected by routing itself
1170+
(:meth:`~UrlDispatcher.resolve` call). Middleware should return new
1171+
coroutine by wrapping *handler* parameter. Signature of returned
1172+
handler should be the same as for
1173+
:ref:`web-handler<aiohttp-web-handler>`: accept single *request*
1174+
parameter, return *response* or raise exception. Factory is coroutine,
1175+
thus it can do extra ``yield from`` calls on making new handler.
1176+
1177+
After constructing outermost handler by applying middleware chain to
1178+
:ref:`web-handler<aiohttp-web-handler>` in reversed order
1179+
:class:`RequestHandler` executes that outermost handler as regular
1180+
*web-handler*.
1181+
1182+
Middleware usually calls inner handler, but may do something
1183+
other, like displaying *403 Forbidden page* or raising
1184+
:exc:`HTTPForbidden` exception if user has no permissions to access underlying
1185+
resource. Also middleware may render errors raised by handler, do
1186+
some pre- and post- processing like handling *CORS* and so on.
1187+
1188+
.. warning::
1189+
1190+
Middlewares are executed **after** routing, so it cannot process
1191+
route exceptions.

0 commit comments

Comments
 (0)