web_app.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import asyncio
  2. import logging
  3. import warnings
  4. from functools import partial, update_wrapper
  5. from typing import (
  6. TYPE_CHECKING,
  7. Any,
  8. AsyncIterator,
  9. Awaitable,
  10. Callable,
  11. Dict,
  12. Iterable,
  13. Iterator,
  14. List,
  15. Mapping,
  16. MutableMapping,
  17. Optional,
  18. Sequence,
  19. Tuple,
  20. Type,
  21. Union,
  22. cast,
  23. )
  24. from . import hdrs
  25. from .abc import (
  26. AbstractAccessLogger,
  27. AbstractMatchInfo,
  28. AbstractRouter,
  29. AbstractStreamWriter,
  30. )
  31. from .frozenlist import FrozenList
  32. from .helpers import DEBUG
  33. from .http_parser import RawRequestMessage
  34. from .log import web_logger
  35. from .signals import Signal
  36. from .streams import StreamReader
  37. from .web_log import AccessLogger
  38. from .web_middlewares import _fix_request_current_app
  39. from .web_protocol import RequestHandler
  40. from .web_request import Request
  41. from .web_response import StreamResponse
  42. from .web_routedef import AbstractRouteDef
  43. from .web_server import Server
  44. from .web_urldispatcher import (
  45. AbstractResource,
  46. AbstractRoute,
  47. Domain,
  48. MaskDomain,
  49. MatchedSubAppResource,
  50. PrefixedSubAppResource,
  51. UrlDispatcher,
  52. )
  53. __all__ = ("Application", "CleanupError")
  54. if TYPE_CHECKING: # pragma: no cover
  55. _AppSignal = Signal[Callable[["Application"], Awaitable[None]]]
  56. _RespPrepareSignal = Signal[Callable[[Request, StreamResponse], Awaitable[None]]]
  57. _Handler = Callable[[Request], Awaitable[StreamResponse]]
  58. _Middleware = Union[
  59. Callable[[Request, _Handler], Awaitable[StreamResponse]],
  60. Callable[["Application", _Handler], Awaitable[_Handler]], # old-style
  61. ]
  62. _Middlewares = FrozenList[_Middleware]
  63. _MiddlewaresHandlers = Optional[Sequence[Tuple[_Middleware, bool]]]
  64. _Subapps = List["Application"]
  65. else:
  66. # No type checker mode, skip types
  67. _AppSignal = Signal
  68. _RespPrepareSignal = Signal
  69. _Handler = Callable
  70. _Middleware = Callable
  71. _Middlewares = FrozenList
  72. _MiddlewaresHandlers = Optional[Sequence]
  73. _Subapps = List
  74. class Application(MutableMapping[str, Any]):
  75. ATTRS = frozenset(
  76. [
  77. "logger",
  78. "_debug",
  79. "_router",
  80. "_loop",
  81. "_handler_args",
  82. "_middlewares",
  83. "_middlewares_handlers",
  84. "_run_middlewares",
  85. "_state",
  86. "_frozen",
  87. "_pre_frozen",
  88. "_subapps",
  89. "_on_response_prepare",
  90. "_on_startup",
  91. "_on_shutdown",
  92. "_on_cleanup",
  93. "_client_max_size",
  94. "_cleanup_ctx",
  95. ]
  96. )
  97. def __init__(
  98. self,
  99. *,
  100. logger: logging.Logger = web_logger,
  101. router: Optional[UrlDispatcher] = None,
  102. middlewares: Iterable[_Middleware] = (),
  103. handler_args: Optional[Mapping[str, Any]] = None,
  104. client_max_size: int = 1024 ** 2,
  105. loop: Optional[asyncio.AbstractEventLoop] = None,
  106. debug: Any = ..., # mypy doesn't support ellipsis
  107. ) -> None:
  108. if router is None:
  109. router = UrlDispatcher()
  110. else:
  111. warnings.warn(
  112. "router argument is deprecated", DeprecationWarning, stacklevel=2
  113. )
  114. assert isinstance(router, AbstractRouter), router
  115. if loop is not None:
  116. warnings.warn(
  117. "loop argument is deprecated", DeprecationWarning, stacklevel=2
  118. )
  119. if debug is not ...:
  120. warnings.warn(
  121. "debug argument is deprecated", DeprecationWarning, stacklevel=2
  122. )
  123. self._debug = debug
  124. self._router = router # type: UrlDispatcher
  125. self._loop = loop
  126. self._handler_args = handler_args
  127. self.logger = logger
  128. self._middlewares = FrozenList(middlewares) # type: _Middlewares
  129. # initialized on freezing
  130. self._middlewares_handlers = None # type: _MiddlewaresHandlers
  131. # initialized on freezing
  132. self._run_middlewares = None # type: Optional[bool]
  133. self._state = {} # type: Dict[str, Any]
  134. self._frozen = False
  135. self._pre_frozen = False
  136. self._subapps = [] # type: _Subapps
  137. self._on_response_prepare = Signal(self) # type: _RespPrepareSignal
  138. self._on_startup = Signal(self) # type: _AppSignal
  139. self._on_shutdown = Signal(self) # type: _AppSignal
  140. self._on_cleanup = Signal(self) # type: _AppSignal
  141. self._cleanup_ctx = CleanupContext()
  142. self._on_startup.append(self._cleanup_ctx._on_startup)
  143. self._on_cleanup.append(self._cleanup_ctx._on_cleanup)
  144. self._client_max_size = client_max_size
  145. def __init_subclass__(cls: Type["Application"]) -> None:
  146. warnings.warn(
  147. "Inheritance class {} from web.Application "
  148. "is discouraged".format(cls.__name__),
  149. DeprecationWarning,
  150. stacklevel=2,
  151. )
  152. if DEBUG: # pragma: no cover
  153. def __setattr__(self, name: str, val: Any) -> None:
  154. if name not in self.ATTRS:
  155. warnings.warn(
  156. "Setting custom web.Application.{} attribute "
  157. "is discouraged".format(name),
  158. DeprecationWarning,
  159. stacklevel=2,
  160. )
  161. super().__setattr__(name, val)
  162. # MutableMapping API
  163. def __eq__(self, other: object) -> bool:
  164. return self is other
  165. def __getitem__(self, key: str) -> Any:
  166. return self._state[key]
  167. def _check_frozen(self) -> None:
  168. if self._frozen:
  169. warnings.warn(
  170. "Changing state of started or joined " "application is deprecated",
  171. DeprecationWarning,
  172. stacklevel=3,
  173. )
  174. def __setitem__(self, key: str, value: Any) -> None:
  175. self._check_frozen()
  176. self._state[key] = value
  177. def __delitem__(self, key: str) -> None:
  178. self._check_frozen()
  179. del self._state[key]
  180. def __len__(self) -> int:
  181. return len(self._state)
  182. def __iter__(self) -> Iterator[str]:
  183. return iter(self._state)
  184. ########
  185. @property
  186. def loop(self) -> asyncio.AbstractEventLoop:
  187. # Technically the loop can be None
  188. # but we mask it by explicit type cast
  189. # to provide more convinient type annotation
  190. warnings.warn("loop property is deprecated", DeprecationWarning, stacklevel=2)
  191. return cast(asyncio.AbstractEventLoop, self._loop)
  192. def _set_loop(self, loop: Optional[asyncio.AbstractEventLoop]) -> None:
  193. if loop is None:
  194. loop = asyncio.get_event_loop()
  195. if self._loop is not None and self._loop is not loop:
  196. raise RuntimeError(
  197. "web.Application instance initialized with different loop"
  198. )
  199. self._loop = loop
  200. # set loop debug
  201. if self._debug is ...:
  202. self._debug = loop.get_debug()
  203. # set loop to sub applications
  204. for subapp in self._subapps:
  205. subapp._set_loop(loop)
  206. @property
  207. def pre_frozen(self) -> bool:
  208. return self._pre_frozen
  209. def pre_freeze(self) -> None:
  210. if self._pre_frozen:
  211. return
  212. self._pre_frozen = True
  213. self._middlewares.freeze()
  214. self._router.freeze()
  215. self._on_response_prepare.freeze()
  216. self._cleanup_ctx.freeze()
  217. self._on_startup.freeze()
  218. self._on_shutdown.freeze()
  219. self._on_cleanup.freeze()
  220. self._middlewares_handlers = tuple(self._prepare_middleware())
  221. # If current app and any subapp do not have middlewares avoid run all
  222. # of the code footprint that it implies, which have a middleware
  223. # hardcoded per app that sets up the current_app attribute. If no
  224. # middlewares are configured the handler will receive the proper
  225. # current_app without needing all of this code.
  226. self._run_middlewares = True if self.middlewares else False
  227. for subapp in self._subapps:
  228. subapp.pre_freeze()
  229. self._run_middlewares = self._run_middlewares or subapp._run_middlewares
  230. @property
  231. def frozen(self) -> bool:
  232. return self._frozen
  233. def freeze(self) -> None:
  234. if self._frozen:
  235. return
  236. self.pre_freeze()
  237. self._frozen = True
  238. for subapp in self._subapps:
  239. subapp.freeze()
  240. @property
  241. def debug(self) -> bool:
  242. warnings.warn("debug property is deprecated", DeprecationWarning, stacklevel=2)
  243. return self._debug
  244. def _reg_subapp_signals(self, subapp: "Application") -> None:
  245. def reg_handler(signame: str) -> None:
  246. subsig = getattr(subapp, signame)
  247. async def handler(app: "Application") -> None:
  248. await subsig.send(subapp)
  249. appsig = getattr(self, signame)
  250. appsig.append(handler)
  251. reg_handler("on_startup")
  252. reg_handler("on_shutdown")
  253. reg_handler("on_cleanup")
  254. def add_subapp(self, prefix: str, subapp: "Application") -> AbstractResource:
  255. if not isinstance(prefix, str):
  256. raise TypeError("Prefix must be str")
  257. prefix = prefix.rstrip("/")
  258. if not prefix:
  259. raise ValueError("Prefix cannot be empty")
  260. factory = partial(PrefixedSubAppResource, prefix, subapp)
  261. return self._add_subapp(factory, subapp)
  262. def _add_subapp(
  263. self, resource_factory: Callable[[], AbstractResource], subapp: "Application"
  264. ) -> AbstractResource:
  265. if self.frozen:
  266. raise RuntimeError("Cannot add sub application to frozen application")
  267. if subapp.frozen:
  268. raise RuntimeError("Cannot add frozen application")
  269. resource = resource_factory()
  270. self.router.register_resource(resource)
  271. self._reg_subapp_signals(subapp)
  272. self._subapps.append(subapp)
  273. subapp.pre_freeze()
  274. if self._loop is not None:
  275. subapp._set_loop(self._loop)
  276. return resource
  277. def add_domain(self, domain: str, subapp: "Application") -> AbstractResource:
  278. if not isinstance(domain, str):
  279. raise TypeError("Domain must be str")
  280. elif "*" in domain:
  281. rule = MaskDomain(domain) # type: Domain
  282. else:
  283. rule = Domain(domain)
  284. factory = partial(MatchedSubAppResource, rule, subapp)
  285. return self._add_subapp(factory, subapp)
  286. def add_routes(self, routes: Iterable[AbstractRouteDef]) -> List[AbstractRoute]:
  287. return self.router.add_routes(routes)
  288. @property
  289. def on_response_prepare(self) -> _RespPrepareSignal:
  290. return self._on_response_prepare
  291. @property
  292. def on_startup(self) -> _AppSignal:
  293. return self._on_startup
  294. @property
  295. def on_shutdown(self) -> _AppSignal:
  296. return self._on_shutdown
  297. @property
  298. def on_cleanup(self) -> _AppSignal:
  299. return self._on_cleanup
  300. @property
  301. def cleanup_ctx(self) -> "CleanupContext":
  302. return self._cleanup_ctx
  303. @property
  304. def router(self) -> UrlDispatcher:
  305. return self._router
  306. @property
  307. def middlewares(self) -> _Middlewares:
  308. return self._middlewares
  309. def _make_handler(
  310. self,
  311. *,
  312. loop: Optional[asyncio.AbstractEventLoop] = None,
  313. access_log_class: Type[AbstractAccessLogger] = AccessLogger,
  314. **kwargs: Any,
  315. ) -> Server:
  316. if not issubclass(access_log_class, AbstractAccessLogger):
  317. raise TypeError(
  318. "access_log_class must be subclass of "
  319. "aiohttp.abc.AbstractAccessLogger, got {}".format(access_log_class)
  320. )
  321. self._set_loop(loop)
  322. self.freeze()
  323. kwargs["debug"] = self._debug
  324. kwargs["access_log_class"] = access_log_class
  325. if self._handler_args:
  326. for k, v in self._handler_args.items():
  327. kwargs[k] = v
  328. return Server(
  329. self._handle, # type: ignore
  330. request_factory=self._make_request,
  331. loop=self._loop,
  332. **kwargs,
  333. )
  334. def make_handler(
  335. self,
  336. *,
  337. loop: Optional[asyncio.AbstractEventLoop] = None,
  338. access_log_class: Type[AbstractAccessLogger] = AccessLogger,
  339. **kwargs: Any,
  340. ) -> Server:
  341. warnings.warn(
  342. "Application.make_handler(...) is deprecated, " "use AppRunner API instead",
  343. DeprecationWarning,
  344. stacklevel=2,
  345. )
  346. return self._make_handler(
  347. loop=loop, access_log_class=access_log_class, **kwargs
  348. )
  349. async def startup(self) -> None:
  350. """Causes on_startup signal
  351. Should be called in the event loop along with the request handler.
  352. """
  353. await self.on_startup.send(self)
  354. async def shutdown(self) -> None:
  355. """Causes on_shutdown signal
  356. Should be called before cleanup()
  357. """
  358. await self.on_shutdown.send(self)
  359. async def cleanup(self) -> None:
  360. """Causes on_cleanup signal
  361. Should be called after shutdown()
  362. """
  363. await self.on_cleanup.send(self)
  364. def _make_request(
  365. self,
  366. message: RawRequestMessage,
  367. payload: StreamReader,
  368. protocol: RequestHandler,
  369. writer: AbstractStreamWriter,
  370. task: "asyncio.Task[None]",
  371. _cls: Type[Request] = Request,
  372. ) -> Request:
  373. return _cls(
  374. message,
  375. payload,
  376. protocol,
  377. writer,
  378. task,
  379. self._loop,
  380. client_max_size=self._client_max_size,
  381. )
  382. def _prepare_middleware(self) -> Iterator[Tuple[_Middleware, bool]]:
  383. for m in reversed(self._middlewares):
  384. if getattr(m, "__middleware_version__", None) == 1:
  385. yield m, True
  386. else:
  387. warnings.warn(
  388. 'old-style middleware "{!r}" deprecated, ' "see #2252".format(m),
  389. DeprecationWarning,
  390. stacklevel=2,
  391. )
  392. yield m, False
  393. yield _fix_request_current_app(self), True
  394. async def _handle(self, request: Request) -> StreamResponse:
  395. loop = asyncio.get_event_loop()
  396. debug = loop.get_debug()
  397. match_info = await self._router.resolve(request)
  398. if debug: # pragma: no cover
  399. if not isinstance(match_info, AbstractMatchInfo):
  400. raise TypeError(
  401. "match_info should be AbstractMatchInfo "
  402. "instance, not {!r}".format(match_info)
  403. )
  404. match_info.add_app(self)
  405. match_info.freeze()
  406. resp = None
  407. request._match_info = match_info # type: ignore
  408. expect = request.headers.get(hdrs.EXPECT)
  409. if expect:
  410. resp = await match_info.expect_handler(request)
  411. await request.writer.drain()
  412. if resp is None:
  413. handler = match_info.handler
  414. if self._run_middlewares:
  415. for app in match_info.apps[::-1]:
  416. for m, new_style in app._middlewares_handlers: # type: ignore
  417. if new_style:
  418. handler = update_wrapper(
  419. partial(m, handler=handler), handler
  420. )
  421. else:
  422. handler = await m(app, handler) # type: ignore
  423. resp = await handler(request)
  424. return resp
  425. def __call__(self) -> "Application":
  426. """gunicorn compatibility"""
  427. return self
  428. def __repr__(self) -> str:
  429. return "<Application 0x{:x}>".format(id(self))
  430. def __bool__(self) -> bool:
  431. return True
  432. class CleanupError(RuntimeError):
  433. @property
  434. def exceptions(self) -> List[BaseException]:
  435. return self.args[1]
  436. if TYPE_CHECKING: # pragma: no cover
  437. _CleanupContextBase = FrozenList[Callable[[Application], AsyncIterator[None]]]
  438. else:
  439. _CleanupContextBase = FrozenList
  440. class CleanupContext(_CleanupContextBase):
  441. def __init__(self) -> None:
  442. super().__init__()
  443. self._exits = [] # type: List[AsyncIterator[None]]
  444. async def _on_startup(self, app: Application) -> None:
  445. for cb in self:
  446. it = cb(app).__aiter__()
  447. await it.__anext__()
  448. self._exits.append(it)
  449. async def _on_cleanup(self, app: Application) -> None:
  450. errors = []
  451. for it in reversed(self._exits):
  452. try:
  453. await it.__anext__()
  454. except StopAsyncIteration:
  455. pass
  456. except Exception as exc:
  457. errors.append(exc)
  458. else:
  459. errors.append(RuntimeError(f"{it!r} has more than one 'yield'"))
  460. if errors:
  461. if len(errors) == 1:
  462. raise errors[0]
  463. else:
  464. raise CleanupError("Multiple errors on cleanup stage", errors)