@@ -666,6 +666,8 @@ Response
666
666
*Content-Length * HTTP header.
667
667
668
668
669
+ .. _aiohttp-web-app-and-router :
670
+
669
671
Application and Router
670
672
----------------------
671
673
@@ -696,7 +698,8 @@ arbitrary properties for later access from
696
698
conn.execute("DELETE * FROM table")
697
699
698
700
699
- .. class :: Application(*, loop=None, router=None, logger=<default>, **kwargs)
701
+ .. class :: Application(*, loop=None, router=None, logger=<default>, \
702
+ middlewares=(), **kwargs)
700
703
701
704
The class inherits :class: `dict `.
702
705
@@ -715,6 +718,9 @@ arbitrary properties for later access from
715
718
716
719
By default the value is ``logging.getLogger("aiohttp.web") ``
717
720
721
+ :param middlewares: sequence of middleware factories, see
722
+ :ref: `aiohttp-web-middlewares ` for details.
723
+
718
724
:param kwargs: optional params for initializing self dict.
719
725
720
726
.. attribute :: router
@@ -1038,6 +1044,7 @@ Utilities
1038
1044
1039
1045
.. seealso :: :ref:`aiohttp-web-file-upload`
1040
1046
1047
+ .. _aiohttp-web-exceptions :
1041
1048
1042
1049
Exceptions
1043
1050
-----------
@@ -1117,7 +1124,7 @@ Http Exception hierarchy chart::
1117
1124
1118
1125
All http exceptions have the same constructor::
1119
1126
1120
- HTTPNotFound(*, headers=None, reason=None, \
1127
+ HTTPNotFound(*, headers=None, reason=None,
1121
1128
body=None, text=None, content_type=None)
1122
1129
1123
1130
if other not directly specified. *headers * will be added to *default
@@ -1127,7 +1134,7 @@ Classes :class:`HTTPMultipleChoices`, :class:`HTTPMovedPermanently`,
1127
1134
:class: `HTTPFound `, :class: `HTTPSeeOther `, :class: `HTTPUseProxy `,
1128
1135
:class: `HTTPTemporaryRedirect ` has constructor signature like::
1129
1136
1130
- HTTPFound(location, *, headers=None, reason=None, \
1137
+ HTTPFound(location, *, headers=None, reason=None,
1131
1138
body=None, text=None, content_type=None)
1132
1139
1133
1140
where *location * is value for *Location HTTP header *.
@@ -1136,5 +1143,49 @@ where *location* is value for *Location HTTP header*.
1136
1143
and list of allowed methods::
1137
1144
1138
1145
HTTPMethodNotAllowed(method, allowed_methods, *,
1139
- headers=None, reason=None, \
1146
+ headers=None, reason=None,
1140
1147
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