web_urldispatcher.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. import abc
  2. import asyncio
  3. import base64
  4. import hashlib
  5. import inspect
  6. import keyword
  7. import os
  8. import re
  9. import warnings
  10. from contextlib import contextmanager
  11. from functools import wraps
  12. from pathlib import Path
  13. from types import MappingProxyType
  14. from typing import (
  15. TYPE_CHECKING,
  16. Any,
  17. Awaitable,
  18. Callable,
  19. Container,
  20. Dict,
  21. Generator,
  22. Iterable,
  23. Iterator,
  24. List,
  25. Mapping,
  26. Optional,
  27. Pattern,
  28. Set,
  29. Sized,
  30. Tuple,
  31. Type,
  32. Union,
  33. cast,
  34. )
  35. from yarl import URL, __version__ as yarl_version # type: ignore[attr-defined]
  36. from . import hdrs
  37. from .abc import AbstractMatchInfo, AbstractRouter, AbstractView
  38. from .helpers import DEBUG
  39. from .http import HttpVersion11
  40. from .typedefs import Final, Handler, PathLike, TypedDict
  41. from .web_exceptions import (
  42. HTTPException,
  43. HTTPExpectationFailed,
  44. HTTPForbidden,
  45. HTTPMethodNotAllowed,
  46. HTTPNotFound,
  47. )
  48. from .web_fileresponse import FileResponse
  49. from .web_request import Request
  50. from .web_response import Response, StreamResponse
  51. from .web_routedef import AbstractRouteDef
  52. __all__ = (
  53. "UrlDispatcher",
  54. "UrlMappingMatchInfo",
  55. "AbstractResource",
  56. "Resource",
  57. "PlainResource",
  58. "DynamicResource",
  59. "AbstractRoute",
  60. "ResourceRoute",
  61. "StaticResource",
  62. "View",
  63. )
  64. if TYPE_CHECKING: # pragma: no cover
  65. from .web_app import Application
  66. BaseDict = Dict[str, str]
  67. else:
  68. BaseDict = dict
  69. YARL_VERSION: Final[Tuple[int, ...]] = tuple(map(int, yarl_version.split(".")[:2]))
  70. HTTP_METHOD_RE: Final[Pattern[str]] = re.compile(
  71. r"^[0-9A-Za-z!#\$%&'\*\+\-\.\^_`\|~]+$"
  72. )
  73. ROUTE_RE: Final[Pattern[str]] = re.compile(
  74. r"(\{[_a-zA-Z][^{}]*(?:\{[^{}]*\}[^{}]*)*\})"
  75. )
  76. PATH_SEP: Final[str] = re.escape("/")
  77. _ExpectHandler = Callable[[Request], Awaitable[None]]
  78. _Resolve = Tuple[Optional["UrlMappingMatchInfo"], Set[str]]
  79. class _InfoDict(TypedDict, total=False):
  80. path: str
  81. formatter: str
  82. pattern: Pattern[str]
  83. directory: Path
  84. prefix: str
  85. routes: Mapping[str, "AbstractRoute"]
  86. app: "Application"
  87. domain: str
  88. rule: "AbstractRuleMatching"
  89. http_exception: HTTPException
  90. class AbstractResource(Sized, Iterable["AbstractRoute"]):
  91. def __init__(self, *, name: Optional[str] = None) -> None:
  92. self._name = name
  93. @property
  94. def name(self) -> Optional[str]:
  95. return self._name
  96. @property
  97. @abc.abstractmethod
  98. def canonical(self) -> str:
  99. """Exposes the resource's canonical path.
  100. For example '/foo/bar/{name}'
  101. """
  102. @abc.abstractmethod # pragma: no branch
  103. def url_for(self, **kwargs: str) -> URL:
  104. """Construct url for resource with additional params."""
  105. @abc.abstractmethod # pragma: no branch
  106. async def resolve(self, request: Request) -> _Resolve:
  107. """Resolve resource.
  108. Return (UrlMappingMatchInfo, allowed_methods) pair.
  109. """
  110. @abc.abstractmethod
  111. def add_prefix(self, prefix: str) -> None:
  112. """Add a prefix to processed URLs.
  113. Required for subapplications support.
  114. """
  115. @abc.abstractmethod
  116. def get_info(self) -> _InfoDict:
  117. """Return a dict with additional info useful for introspection"""
  118. def freeze(self) -> None:
  119. pass
  120. @abc.abstractmethod
  121. def raw_match(self, path: str) -> bool:
  122. """Perform a raw match against path"""
  123. class AbstractRoute(abc.ABC):
  124. def __init__(
  125. self,
  126. method: str,
  127. handler: Union[Handler, Type[AbstractView]],
  128. *,
  129. expect_handler: Optional[_ExpectHandler] = None,
  130. resource: Optional[AbstractResource] = None,
  131. ) -> None:
  132. if expect_handler is None:
  133. expect_handler = _default_expect_handler
  134. assert asyncio.iscoroutinefunction(
  135. expect_handler
  136. ), f"Coroutine is expected, got {expect_handler!r}"
  137. method = method.upper()
  138. if not HTTP_METHOD_RE.match(method):
  139. raise ValueError(f"{method} is not allowed HTTP method")
  140. assert callable(handler), handler
  141. if asyncio.iscoroutinefunction(handler):
  142. pass
  143. elif inspect.isgeneratorfunction(handler):
  144. warnings.warn(
  145. "Bare generators are deprecated, " "use @coroutine wrapper",
  146. DeprecationWarning,
  147. )
  148. elif isinstance(handler, type) and issubclass(handler, AbstractView):
  149. pass
  150. else:
  151. warnings.warn(
  152. "Bare functions are deprecated, " "use async ones", DeprecationWarning
  153. )
  154. @wraps(handler)
  155. async def handler_wrapper(request: Request) -> StreamResponse:
  156. result = old_handler(request)
  157. if asyncio.iscoroutine(result):
  158. return await result
  159. return result # type: ignore[return-value]
  160. old_handler = handler
  161. handler = handler_wrapper
  162. self._method = method
  163. self._handler = handler
  164. self._expect_handler = expect_handler
  165. self._resource = resource
  166. @property
  167. def method(self) -> str:
  168. return self._method
  169. @property
  170. def handler(self) -> Handler:
  171. return self._handler
  172. @property
  173. @abc.abstractmethod
  174. def name(self) -> Optional[str]:
  175. """Optional route's name, always equals to resource's name."""
  176. @property
  177. def resource(self) -> Optional[AbstractResource]:
  178. return self._resource
  179. @abc.abstractmethod
  180. def get_info(self) -> _InfoDict:
  181. """Return a dict with additional info useful for introspection"""
  182. @abc.abstractmethod # pragma: no branch
  183. def url_for(self, *args: str, **kwargs: str) -> URL:
  184. """Construct url for route with additional params."""
  185. async def handle_expect_header(self, request: Request) -> None:
  186. await self._expect_handler(request)
  187. class UrlMappingMatchInfo(BaseDict, AbstractMatchInfo):
  188. def __init__(self, match_dict: Dict[str, str], route: AbstractRoute):
  189. super().__init__(match_dict)
  190. self._route = route
  191. self._apps = [] # type: List[Application]
  192. self._current_app = None # type: Optional[Application]
  193. self._frozen = False
  194. @property
  195. def handler(self) -> Handler:
  196. return self._route.handler
  197. @property
  198. def route(self) -> AbstractRoute:
  199. return self._route
  200. @property
  201. def expect_handler(self) -> _ExpectHandler:
  202. return self._route.handle_expect_header
  203. @property
  204. def http_exception(self) -> Optional[HTTPException]:
  205. return None
  206. def get_info(self) -> _InfoDict: # type: ignore[override]
  207. return self._route.get_info()
  208. @property
  209. def apps(self) -> Tuple["Application", ...]:
  210. return tuple(self._apps)
  211. def add_app(self, app: "Application") -> None:
  212. if self._frozen:
  213. raise RuntimeError("Cannot change apps stack after .freeze() call")
  214. if self._current_app is None:
  215. self._current_app = app
  216. self._apps.insert(0, app)
  217. @property
  218. def current_app(self) -> "Application":
  219. app = self._current_app
  220. assert app is not None
  221. return app
  222. @contextmanager
  223. def set_current_app(self, app: "Application") -> Generator[None, None, None]:
  224. if DEBUG: # pragma: no cover
  225. if app not in self._apps:
  226. raise RuntimeError(
  227. "Expected one of the following apps {!r}, got {!r}".format(
  228. self._apps, app
  229. )
  230. )
  231. prev = self._current_app
  232. self._current_app = app
  233. try:
  234. yield
  235. finally:
  236. self._current_app = prev
  237. def freeze(self) -> None:
  238. self._frozen = True
  239. def __repr__(self) -> str:
  240. return f"<MatchInfo {super().__repr__()}: {self._route}>"
  241. class MatchInfoError(UrlMappingMatchInfo):
  242. def __init__(self, http_exception: HTTPException) -> None:
  243. self._exception = http_exception
  244. super().__init__({}, SystemRoute(self._exception))
  245. @property
  246. def http_exception(self) -> HTTPException:
  247. return self._exception
  248. def __repr__(self) -> str:
  249. return "<MatchInfoError {}: {}>".format(
  250. self._exception.status, self._exception.reason
  251. )
  252. async def _default_expect_handler(request: Request) -> None:
  253. """Default handler for Expect header.
  254. Just send "100 Continue" to client.
  255. raise HTTPExpectationFailed if value of header is not "100-continue"
  256. """
  257. expect = request.headers.get(hdrs.EXPECT, "")
  258. if request.version == HttpVersion11:
  259. if expect.lower() == "100-continue":
  260. await request.writer.write(b"HTTP/1.1 100 Continue\r\n\r\n")
  261. else:
  262. raise HTTPExpectationFailed(text="Unknown Expect: %s" % expect)
  263. class Resource(AbstractResource):
  264. def __init__(self, *, name: Optional[str] = None) -> None:
  265. super().__init__(name=name)
  266. self._routes = [] # type: List[ResourceRoute]
  267. def add_route(
  268. self,
  269. method: str,
  270. handler: Union[Type[AbstractView], Handler],
  271. *,
  272. expect_handler: Optional[_ExpectHandler] = None,
  273. ) -> "ResourceRoute":
  274. for route_obj in self._routes:
  275. if route_obj.method == method or route_obj.method == hdrs.METH_ANY:
  276. raise RuntimeError(
  277. "Added route will never be executed, "
  278. "method {route.method} is already "
  279. "registered".format(route=route_obj)
  280. )
  281. route_obj = ResourceRoute(method, handler, self, expect_handler=expect_handler)
  282. self.register_route(route_obj)
  283. return route_obj
  284. def register_route(self, route: "ResourceRoute") -> None:
  285. assert isinstance(
  286. route, ResourceRoute
  287. ), f"Instance of Route class is required, got {route!r}"
  288. self._routes.append(route)
  289. async def resolve(self, request: Request) -> _Resolve:
  290. allowed_methods = set() # type: Set[str]
  291. match_dict = self._match(request.rel_url.raw_path)
  292. if match_dict is None:
  293. return None, allowed_methods
  294. for route_obj in self._routes:
  295. route_method = route_obj.method
  296. allowed_methods.add(route_method)
  297. if route_method == request.method or route_method == hdrs.METH_ANY:
  298. return (UrlMappingMatchInfo(match_dict, route_obj), allowed_methods)
  299. else:
  300. return None, allowed_methods
  301. @abc.abstractmethod
  302. def _match(self, path: str) -> Optional[Dict[str, str]]:
  303. pass # pragma: no cover
  304. def __len__(self) -> int:
  305. return len(self._routes)
  306. def __iter__(self) -> Iterator[AbstractRoute]:
  307. return iter(self._routes)
  308. # TODO: implement all abstract methods
  309. class PlainResource(Resource):
  310. def __init__(self, path: str, *, name: Optional[str] = None) -> None:
  311. super().__init__(name=name)
  312. assert not path or path.startswith("/")
  313. self._path = path
  314. @property
  315. def canonical(self) -> str:
  316. return self._path
  317. def freeze(self) -> None:
  318. if not self._path:
  319. self._path = "/"
  320. def add_prefix(self, prefix: str) -> None:
  321. assert prefix.startswith("/")
  322. assert not prefix.endswith("/")
  323. assert len(prefix) > 1
  324. self._path = prefix + self._path
  325. def _match(self, path: str) -> Optional[Dict[str, str]]:
  326. # string comparison is about 10 times faster than regexp matching
  327. if self._path == path:
  328. return {}
  329. else:
  330. return None
  331. def raw_match(self, path: str) -> bool:
  332. return self._path == path
  333. def get_info(self) -> _InfoDict:
  334. return {"path": self._path}
  335. def url_for(self) -> URL: # type: ignore[override]
  336. return URL.build(path=self._path, encoded=True)
  337. def __repr__(self) -> str:
  338. name = "'" + self.name + "' " if self.name is not None else ""
  339. return f"<PlainResource {name} {self._path}>"
  340. class DynamicResource(Resource):
  341. DYN = re.compile(r"\{(?P<var>[_a-zA-Z][_a-zA-Z0-9]*)\}")
  342. DYN_WITH_RE = re.compile(r"\{(?P<var>[_a-zA-Z][_a-zA-Z0-9]*):(?P<re>.+)\}")
  343. GOOD = r"[^{}/]+"
  344. def __init__(self, path: str, *, name: Optional[str] = None) -> None:
  345. super().__init__(name=name)
  346. pattern = ""
  347. formatter = ""
  348. for part in ROUTE_RE.split(path):
  349. match = self.DYN.fullmatch(part)
  350. if match:
  351. pattern += "(?P<{}>{})".format(match.group("var"), self.GOOD)
  352. formatter += "{" + match.group("var") + "}"
  353. continue
  354. match = self.DYN_WITH_RE.fullmatch(part)
  355. if match:
  356. pattern += "(?P<{var}>{re})".format(**match.groupdict())
  357. formatter += "{" + match.group("var") + "}"
  358. continue
  359. if "{" in part or "}" in part:
  360. raise ValueError(f"Invalid path '{path}'['{part}']")
  361. part = _requote_path(part)
  362. formatter += part
  363. pattern += re.escape(part)
  364. try:
  365. compiled = re.compile(pattern)
  366. except re.error as exc:
  367. raise ValueError(f"Bad pattern '{pattern}': {exc}") from None
  368. assert compiled.pattern.startswith(PATH_SEP)
  369. assert formatter.startswith("/")
  370. self._pattern = compiled
  371. self._formatter = formatter
  372. @property
  373. def canonical(self) -> str:
  374. return self._formatter
  375. def add_prefix(self, prefix: str) -> None:
  376. assert prefix.startswith("/")
  377. assert not prefix.endswith("/")
  378. assert len(prefix) > 1
  379. self._pattern = re.compile(re.escape(prefix) + self._pattern.pattern)
  380. self._formatter = prefix + self._formatter
  381. def _match(self, path: str) -> Optional[Dict[str, str]]:
  382. match = self._pattern.fullmatch(path)
  383. if match is None:
  384. return None
  385. else:
  386. return {
  387. key: _unquote_path(value) for key, value in match.groupdict().items()
  388. }
  389. def raw_match(self, path: str) -> bool:
  390. return self._formatter == path
  391. def get_info(self) -> _InfoDict:
  392. return {"formatter": self._formatter, "pattern": self._pattern}
  393. def url_for(self, **parts: str) -> URL:
  394. url = self._formatter.format_map({k: _quote_path(v) for k, v in parts.items()})
  395. return URL.build(path=url, encoded=True)
  396. def __repr__(self) -> str:
  397. name = "'" + self.name + "' " if self.name is not None else ""
  398. return "<DynamicResource {name} {formatter}>".format(
  399. name=name, formatter=self._formatter
  400. )
  401. class PrefixResource(AbstractResource):
  402. def __init__(self, prefix: str, *, name: Optional[str] = None) -> None:
  403. assert not prefix or prefix.startswith("/"), prefix
  404. assert prefix in ("", "/") or not prefix.endswith("/"), prefix
  405. super().__init__(name=name)
  406. self._prefix = _requote_path(prefix)
  407. self._prefix2 = self._prefix + "/"
  408. @property
  409. def canonical(self) -> str:
  410. return self._prefix
  411. def add_prefix(self, prefix: str) -> None:
  412. assert prefix.startswith("/")
  413. assert not prefix.endswith("/")
  414. assert len(prefix) > 1
  415. self._prefix = prefix + self._prefix
  416. self._prefix2 = self._prefix + "/"
  417. def raw_match(self, prefix: str) -> bool:
  418. return False
  419. # TODO: impl missing abstract methods
  420. class StaticResource(PrefixResource):
  421. VERSION_KEY = "v"
  422. def __init__(
  423. self,
  424. prefix: str,
  425. directory: PathLike,
  426. *,
  427. name: Optional[str] = None,
  428. expect_handler: Optional[_ExpectHandler] = None,
  429. chunk_size: int = 256 * 1024,
  430. show_index: bool = False,
  431. follow_symlinks: bool = False,
  432. append_version: bool = False,
  433. ) -> None:
  434. super().__init__(prefix, name=name)
  435. try:
  436. directory = Path(directory)
  437. if str(directory).startswith("~"):
  438. directory = Path(os.path.expanduser(str(directory)))
  439. directory = directory.resolve()
  440. if not directory.is_dir():
  441. raise ValueError("Not a directory")
  442. except (FileNotFoundError, ValueError) as error:
  443. raise ValueError(f"No directory exists at '{directory}'") from error
  444. self._directory = directory
  445. self._show_index = show_index
  446. self._chunk_size = chunk_size
  447. self._follow_symlinks = follow_symlinks
  448. self._expect_handler = expect_handler
  449. self._append_version = append_version
  450. self._routes = {
  451. "GET": ResourceRoute(
  452. "GET", self._handle, self, expect_handler=expect_handler
  453. ),
  454. "HEAD": ResourceRoute(
  455. "HEAD", self._handle, self, expect_handler=expect_handler
  456. ),
  457. }
  458. def url_for( # type: ignore[override]
  459. self,
  460. *,
  461. filename: Union[str, Path],
  462. append_version: Optional[bool] = None,
  463. ) -> URL:
  464. if append_version is None:
  465. append_version = self._append_version
  466. if isinstance(filename, Path):
  467. filename = str(filename)
  468. filename = filename.lstrip("/")
  469. url = URL.build(path=self._prefix, encoded=True)
  470. # filename is not encoded
  471. if YARL_VERSION < (1, 6):
  472. url = url / filename.replace("%", "%25")
  473. else:
  474. url = url / filename
  475. if append_version:
  476. try:
  477. filepath = self._directory.joinpath(filename).resolve()
  478. if not self._follow_symlinks:
  479. filepath.relative_to(self._directory)
  480. except (ValueError, FileNotFoundError):
  481. # ValueError for case when path point to symlink
  482. # with follow_symlinks is False
  483. return url # relatively safe
  484. if filepath.is_file():
  485. # TODO cache file content
  486. # with file watcher for cache invalidation
  487. with filepath.open("rb") as f:
  488. file_bytes = f.read()
  489. h = self._get_file_hash(file_bytes)
  490. url = url.with_query({self.VERSION_KEY: h})
  491. return url
  492. return url
  493. @staticmethod
  494. def _get_file_hash(byte_array: bytes) -> str:
  495. m = hashlib.sha256() # todo sha256 can be configurable param
  496. m.update(byte_array)
  497. b64 = base64.urlsafe_b64encode(m.digest())
  498. return b64.decode("ascii")
  499. def get_info(self) -> _InfoDict:
  500. return {
  501. "directory": self._directory,
  502. "prefix": self._prefix,
  503. "routes": self._routes,
  504. }
  505. def set_options_route(self, handler: Handler) -> None:
  506. if "OPTIONS" in self._routes:
  507. raise RuntimeError("OPTIONS route was set already")
  508. self._routes["OPTIONS"] = ResourceRoute(
  509. "OPTIONS", handler, self, expect_handler=self._expect_handler
  510. )
  511. async def resolve(self, request: Request) -> _Resolve:
  512. path = request.rel_url.raw_path
  513. method = request.method
  514. allowed_methods = set(self._routes)
  515. if not path.startswith(self._prefix2) and path != self._prefix:
  516. return None, set()
  517. if method not in allowed_methods:
  518. return None, allowed_methods
  519. match_dict = {"filename": _unquote_path(path[len(self._prefix) + 1 :])}
  520. return (UrlMappingMatchInfo(match_dict, self._routes[method]), allowed_methods)
  521. def __len__(self) -> int:
  522. return len(self._routes)
  523. def __iter__(self) -> Iterator[AbstractRoute]:
  524. return iter(self._routes.values())
  525. async def _handle(self, request: Request) -> StreamResponse:
  526. rel_url = request.match_info["filename"]
  527. try:
  528. filename = Path(rel_url)
  529. if filename.anchor:
  530. # rel_url is an absolute name like
  531. # /static/\\machine_name\c$ or /static/D:\path
  532. # where the static dir is totally different
  533. raise HTTPForbidden()
  534. filepath = self._directory.joinpath(filename).resolve()
  535. if not self._follow_symlinks:
  536. filepath.relative_to(self._directory)
  537. except (ValueError, FileNotFoundError) as error:
  538. # relatively safe
  539. raise HTTPNotFound() from error
  540. except HTTPForbidden:
  541. raise
  542. except Exception as error:
  543. # perm error or other kind!
  544. request.app.logger.exception(error)
  545. raise HTTPNotFound() from error
  546. # on opening a dir, load its contents if allowed
  547. if filepath.is_dir():
  548. if self._show_index:
  549. try:
  550. return Response(
  551. text=self._directory_as_html(filepath), content_type="text/html"
  552. )
  553. except PermissionError:
  554. raise HTTPForbidden()
  555. else:
  556. raise HTTPForbidden()
  557. elif filepath.is_file():
  558. return FileResponse(filepath, chunk_size=self._chunk_size)
  559. else:
  560. raise HTTPNotFound
  561. def _directory_as_html(self, filepath: Path) -> str:
  562. # returns directory's index as html
  563. # sanity check
  564. assert filepath.is_dir()
  565. relative_path_to_dir = filepath.relative_to(self._directory).as_posix()
  566. index_of = f"Index of /{relative_path_to_dir}"
  567. h1 = f"<h1>{index_of}</h1>"
  568. index_list = []
  569. dir_index = filepath.iterdir()
  570. for _file in sorted(dir_index):
  571. # show file url as relative to static path
  572. rel_path = _file.relative_to(self._directory).as_posix()
  573. file_url = self._prefix + "/" + rel_path
  574. # if file is a directory, add '/' to the end of the name
  575. if _file.is_dir():
  576. file_name = f"{_file.name}/"
  577. else:
  578. file_name = _file.name
  579. index_list.append(
  580. '<li><a href="{url}">{name}</a></li>'.format(
  581. url=file_url, name=file_name
  582. )
  583. )
  584. ul = "<ul>\n{}\n</ul>".format("\n".join(index_list))
  585. body = f"<body>\n{h1}\n{ul}\n</body>"
  586. head_str = f"<head>\n<title>{index_of}</title>\n</head>"
  587. html = f"<html>\n{head_str}\n{body}\n</html>"
  588. return html
  589. def __repr__(self) -> str:
  590. name = "'" + self.name + "'" if self.name is not None else ""
  591. return "<StaticResource {name} {path} -> {directory!r}>".format(
  592. name=name, path=self._prefix, directory=self._directory
  593. )
  594. class PrefixedSubAppResource(PrefixResource):
  595. def __init__(self, prefix: str, app: "Application") -> None:
  596. super().__init__(prefix)
  597. self._app = app
  598. for resource in app.router.resources():
  599. resource.add_prefix(prefix)
  600. def add_prefix(self, prefix: str) -> None:
  601. super().add_prefix(prefix)
  602. for resource in self._app.router.resources():
  603. resource.add_prefix(prefix)
  604. def url_for(self, *args: str, **kwargs: str) -> URL:
  605. raise RuntimeError(".url_for() is not supported " "by sub-application root")
  606. def get_info(self) -> _InfoDict:
  607. return {"app": self._app, "prefix": self._prefix}
  608. async def resolve(self, request: Request) -> _Resolve:
  609. if (
  610. not request.url.raw_path.startswith(self._prefix2)
  611. and request.url.raw_path != self._prefix
  612. ):
  613. return None, set()
  614. match_info = await self._app.router.resolve(request)
  615. match_info.add_app(self._app)
  616. if isinstance(match_info.http_exception, HTTPMethodNotAllowed):
  617. methods = match_info.http_exception.allowed_methods
  618. else:
  619. methods = set()
  620. return match_info, methods
  621. def __len__(self) -> int:
  622. return len(self._app.router.routes())
  623. def __iter__(self) -> Iterator[AbstractRoute]:
  624. return iter(self._app.router.routes())
  625. def __repr__(self) -> str:
  626. return "<PrefixedSubAppResource {prefix} -> {app!r}>".format(
  627. prefix=self._prefix, app=self._app
  628. )
  629. class AbstractRuleMatching(abc.ABC):
  630. @abc.abstractmethod # pragma: no branch
  631. async def match(self, request: Request) -> bool:
  632. """Return bool if the request satisfies the criteria"""
  633. @abc.abstractmethod # pragma: no branch
  634. def get_info(self) -> _InfoDict:
  635. """Return a dict with additional info useful for introspection"""
  636. @property
  637. @abc.abstractmethod # pragma: no branch
  638. def canonical(self) -> str:
  639. """Return a str"""
  640. class Domain(AbstractRuleMatching):
  641. re_part = re.compile(r"(?!-)[a-z\d-]{1,63}(?<!-)")
  642. def __init__(self, domain: str) -> None:
  643. super().__init__()
  644. self._domain = self.validation(domain)
  645. @property
  646. def canonical(self) -> str:
  647. return self._domain
  648. def validation(self, domain: str) -> str:
  649. if not isinstance(domain, str):
  650. raise TypeError("Domain must be str")
  651. domain = domain.rstrip(".").lower()
  652. if not domain:
  653. raise ValueError("Domain cannot be empty")
  654. elif "://" in domain:
  655. raise ValueError("Scheme not supported")
  656. url = URL("http://" + domain)
  657. assert url.raw_host is not None
  658. if not all(self.re_part.fullmatch(x) for x in url.raw_host.split(".")):
  659. raise ValueError("Domain not valid")
  660. if url.port == 80:
  661. return url.raw_host
  662. return f"{url.raw_host}:{url.port}"
  663. async def match(self, request: Request) -> bool:
  664. host = request.headers.get(hdrs.HOST)
  665. if not host:
  666. return False
  667. return self.match_domain(host)
  668. def match_domain(self, host: str) -> bool:
  669. return host.lower() == self._domain
  670. def get_info(self) -> _InfoDict:
  671. return {"domain": self._domain}
  672. class MaskDomain(Domain):
  673. re_part = re.compile(r"(?!-)[a-z\d\*-]{1,63}(?<!-)")
  674. def __init__(self, domain: str) -> None:
  675. super().__init__(domain)
  676. mask = self._domain.replace(".", r"\.").replace("*", ".*")
  677. self._mask = re.compile(mask)
  678. @property
  679. def canonical(self) -> str:
  680. return self._mask.pattern
  681. def match_domain(self, host: str) -> bool:
  682. return self._mask.fullmatch(host) is not None
  683. class MatchedSubAppResource(PrefixedSubAppResource):
  684. def __init__(self, rule: AbstractRuleMatching, app: "Application") -> None:
  685. AbstractResource.__init__(self)
  686. self._prefix = ""
  687. self._app = app
  688. self._rule = rule
  689. @property
  690. def canonical(self) -> str:
  691. return self._rule.canonical
  692. def get_info(self) -> _InfoDict:
  693. return {"app": self._app, "rule": self._rule}
  694. async def resolve(self, request: Request) -> _Resolve:
  695. if not await self._rule.match(request):
  696. return None, set()
  697. match_info = await self._app.router.resolve(request)
  698. match_info.add_app(self._app)
  699. if isinstance(match_info.http_exception, HTTPMethodNotAllowed):
  700. methods = match_info.http_exception.allowed_methods
  701. else:
  702. methods = set()
  703. return match_info, methods
  704. def __repr__(self) -> str:
  705. return "<MatchedSubAppResource -> {app!r}>" "".format(app=self._app)
  706. class ResourceRoute(AbstractRoute):
  707. """A route with resource"""
  708. def __init__(
  709. self,
  710. method: str,
  711. handler: Union[Handler, Type[AbstractView]],
  712. resource: AbstractResource,
  713. *,
  714. expect_handler: Optional[_ExpectHandler] = None,
  715. ) -> None:
  716. super().__init__(
  717. method, handler, expect_handler=expect_handler, resource=resource
  718. )
  719. def __repr__(self) -> str:
  720. return "<ResourceRoute [{method}] {resource} -> {handler!r}".format(
  721. method=self.method, resource=self._resource, handler=self.handler
  722. )
  723. @property
  724. def name(self) -> Optional[str]:
  725. if self._resource is None:
  726. return None
  727. return self._resource.name
  728. def url_for(self, *args: str, **kwargs: str) -> URL:
  729. """Construct url for route with additional params."""
  730. assert self._resource is not None
  731. return self._resource.url_for(*args, **kwargs)
  732. def get_info(self) -> _InfoDict:
  733. assert self._resource is not None
  734. return self._resource.get_info()
  735. class SystemRoute(AbstractRoute):
  736. def __init__(self, http_exception: HTTPException) -> None:
  737. super().__init__(hdrs.METH_ANY, self._handle)
  738. self._http_exception = http_exception
  739. def url_for(self, *args: str, **kwargs: str) -> URL:
  740. raise RuntimeError(".url_for() is not allowed for SystemRoute")
  741. @property
  742. def name(self) -> Optional[str]:
  743. return None
  744. def get_info(self) -> _InfoDict:
  745. return {"http_exception": self._http_exception}
  746. async def _handle(self, request: Request) -> StreamResponse:
  747. raise self._http_exception
  748. @property
  749. def status(self) -> int:
  750. return self._http_exception.status
  751. @property
  752. def reason(self) -> str:
  753. return self._http_exception.reason
  754. def __repr__(self) -> str:
  755. return "<SystemRoute {self.status}: {self.reason}>".format(self=self)
  756. class View(AbstractView):
  757. async def _iter(self) -> StreamResponse:
  758. if self.request.method not in hdrs.METH_ALL:
  759. self._raise_allowed_methods()
  760. method: Callable[[], Awaitable[StreamResponse]] = getattr(
  761. self, self.request.method.lower(), None
  762. )
  763. if method is None:
  764. self._raise_allowed_methods()
  765. resp = await method()
  766. return resp
  767. def __await__(self) -> Generator[Any, None, StreamResponse]:
  768. return self._iter().__await__()
  769. def _raise_allowed_methods(self) -> None:
  770. allowed_methods = {m for m in hdrs.METH_ALL if hasattr(self, m.lower())}
  771. raise HTTPMethodNotAllowed(self.request.method, allowed_methods)
  772. class ResourcesView(Sized, Iterable[AbstractResource], Container[AbstractResource]):
  773. def __init__(self, resources: List[AbstractResource]) -> None:
  774. self._resources = resources
  775. def __len__(self) -> int:
  776. return len(self._resources)
  777. def __iter__(self) -> Iterator[AbstractResource]:
  778. yield from self._resources
  779. def __contains__(self, resource: object) -> bool:
  780. return resource in self._resources
  781. class RoutesView(Sized, Iterable[AbstractRoute], Container[AbstractRoute]):
  782. def __init__(self, resources: List[AbstractResource]):
  783. self._routes = [] # type: List[AbstractRoute]
  784. for resource in resources:
  785. for route in resource:
  786. self._routes.append(route)
  787. def __len__(self) -> int:
  788. return len(self._routes)
  789. def __iter__(self) -> Iterator[AbstractRoute]:
  790. yield from self._routes
  791. def __contains__(self, route: object) -> bool:
  792. return route in self._routes
  793. class UrlDispatcher(AbstractRouter, Mapping[str, AbstractResource]):
  794. NAME_SPLIT_RE = re.compile(r"[.:-]")
  795. def __init__(self) -> None:
  796. super().__init__()
  797. self._resources = [] # type: List[AbstractResource]
  798. self._named_resources = {} # type: Dict[str, AbstractResource]
  799. async def resolve(self, request: Request) -> UrlMappingMatchInfo:
  800. method = request.method
  801. allowed_methods = set() # type: Set[str]
  802. for resource in self._resources:
  803. match_dict, allowed = await resource.resolve(request)
  804. if match_dict is not None:
  805. return match_dict
  806. else:
  807. allowed_methods |= allowed
  808. if allowed_methods:
  809. return MatchInfoError(HTTPMethodNotAllowed(method, allowed_methods))
  810. else:
  811. return MatchInfoError(HTTPNotFound())
  812. def __iter__(self) -> Iterator[str]:
  813. return iter(self._named_resources)
  814. def __len__(self) -> int:
  815. return len(self._named_resources)
  816. def __contains__(self, resource: object) -> bool:
  817. return resource in self._named_resources
  818. def __getitem__(self, name: str) -> AbstractResource:
  819. return self._named_resources[name]
  820. def resources(self) -> ResourcesView:
  821. return ResourcesView(self._resources)
  822. def routes(self) -> RoutesView:
  823. return RoutesView(self._resources)
  824. def named_resources(self) -> Mapping[str, AbstractResource]:
  825. return MappingProxyType(self._named_resources)
  826. def register_resource(self, resource: AbstractResource) -> None:
  827. assert isinstance(
  828. resource, AbstractResource
  829. ), f"Instance of AbstractResource class is required, got {resource!r}"
  830. if self.frozen:
  831. raise RuntimeError("Cannot register a resource into frozen router.")
  832. name = resource.name
  833. if name is not None:
  834. parts = self.NAME_SPLIT_RE.split(name)
  835. for part in parts:
  836. if keyword.iskeyword(part):
  837. raise ValueError(
  838. f"Incorrect route name {name!r}, "
  839. "python keywords cannot be used "
  840. "for route name"
  841. )
  842. if not part.isidentifier():
  843. raise ValueError(
  844. "Incorrect route name {!r}, "
  845. "the name should be a sequence of "
  846. "python identifiers separated "
  847. "by dash, dot or column".format(name)
  848. )
  849. if name in self._named_resources:
  850. raise ValueError(
  851. "Duplicate {!r}, "
  852. "already handled by {!r}".format(name, self._named_resources[name])
  853. )
  854. self._named_resources[name] = resource
  855. self._resources.append(resource)
  856. def add_resource(self, path: str, *, name: Optional[str] = None) -> Resource:
  857. if path and not path.startswith("/"):
  858. raise ValueError("path should be started with / or be empty")
  859. # Reuse last added resource if path and name are the same
  860. if self._resources:
  861. resource = self._resources[-1]
  862. if resource.name == name and resource.raw_match(path):
  863. return cast(Resource, resource)
  864. if not ("{" in path or "}" in path or ROUTE_RE.search(path)):
  865. resource = PlainResource(_requote_path(path), name=name)
  866. self.register_resource(resource)
  867. return resource
  868. resource = DynamicResource(path, name=name)
  869. self.register_resource(resource)
  870. return resource
  871. def add_route(
  872. self,
  873. method: str,
  874. path: str,
  875. handler: Union[Handler, Type[AbstractView]],
  876. *,
  877. name: Optional[str] = None,
  878. expect_handler: Optional[_ExpectHandler] = None,
  879. ) -> AbstractRoute:
  880. resource = self.add_resource(path, name=name)
  881. return resource.add_route(method, handler, expect_handler=expect_handler)
  882. def add_static(
  883. self,
  884. prefix: str,
  885. path: PathLike,
  886. *,
  887. name: Optional[str] = None,
  888. expect_handler: Optional[_ExpectHandler] = None,
  889. chunk_size: int = 256 * 1024,
  890. show_index: bool = False,
  891. follow_symlinks: bool = False,
  892. append_version: bool = False,
  893. ) -> AbstractResource:
  894. """Add static files view.
  895. prefix - url prefix
  896. path - folder with files
  897. """
  898. assert prefix.startswith("/")
  899. if prefix.endswith("/"):
  900. prefix = prefix[:-1]
  901. resource = StaticResource(
  902. prefix,
  903. path,
  904. name=name,
  905. expect_handler=expect_handler,
  906. chunk_size=chunk_size,
  907. show_index=show_index,
  908. follow_symlinks=follow_symlinks,
  909. append_version=append_version,
  910. )
  911. self.register_resource(resource)
  912. return resource
  913. def add_head(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  914. """Shortcut for add_route with method HEAD."""
  915. return self.add_route(hdrs.METH_HEAD, path, handler, **kwargs)
  916. def add_options(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  917. """Shortcut for add_route with method OPTIONS."""
  918. return self.add_route(hdrs.METH_OPTIONS, path, handler, **kwargs)
  919. def add_get(
  920. self,
  921. path: str,
  922. handler: Handler,
  923. *,
  924. name: Optional[str] = None,
  925. allow_head: bool = True,
  926. **kwargs: Any,
  927. ) -> AbstractRoute:
  928. """Shortcut for add_route with method GET.
  929. If allow_head is true, another
  930. route is added allowing head requests to the same endpoint.
  931. """
  932. resource = self.add_resource(path, name=name)
  933. if allow_head:
  934. resource.add_route(hdrs.METH_HEAD, handler, **kwargs)
  935. return resource.add_route(hdrs.METH_GET, handler, **kwargs)
  936. def add_post(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  937. """Shortcut for add_route with method POST."""
  938. return self.add_route(hdrs.METH_POST, path, handler, **kwargs)
  939. def add_put(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  940. """Shortcut for add_route with method PUT."""
  941. return self.add_route(hdrs.METH_PUT, path, handler, **kwargs)
  942. def add_patch(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  943. """Shortcut for add_route with method PATCH."""
  944. return self.add_route(hdrs.METH_PATCH, path, handler, **kwargs)
  945. def add_delete(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute:
  946. """Shortcut for add_route with method DELETE."""
  947. return self.add_route(hdrs.METH_DELETE, path, handler, **kwargs)
  948. def add_view(
  949. self, path: str, handler: Type[AbstractView], **kwargs: Any
  950. ) -> AbstractRoute:
  951. """Shortcut for add_route with ANY methods for a class-based view."""
  952. return self.add_route(hdrs.METH_ANY, path, handler, **kwargs)
  953. def freeze(self) -> None:
  954. super().freeze()
  955. for resource in self._resources:
  956. resource.freeze()
  957. def add_routes(self, routes: Iterable[AbstractRouteDef]) -> List[AbstractRoute]:
  958. """Append routes to route table.
  959. Parameter should be a sequence of RouteDef objects.
  960. Returns a list of registered AbstractRoute instances.
  961. """
  962. registered_routes = []
  963. for route_def in routes:
  964. registered_routes.extend(route_def.register(self))
  965. return registered_routes
  966. def _quote_path(value: str) -> str:
  967. if YARL_VERSION < (1, 6):
  968. value = value.replace("%", "%25")
  969. return URL.build(path=value, encoded=False).raw_path
  970. def _unquote_path(value: str) -> str:
  971. return URL.build(path=value, encoded=True).path
  972. def _requote_path(value: str) -> str:
  973. # Quote non-ascii characters and other characters which must be quoted,
  974. # but preserve existing %-sequences.
  975. result = _quote_path(value)
  976. if "%" in value:
  977. result = result.replace("%25", "%")
  978. return result