staticfiles.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import importlib.util
  2. import os
  3. import stat
  4. import typing
  5. from email.utils import parsedate
  6. import anyio
  7. from starlette.datastructures import URL, Headers
  8. from starlette.exceptions import HTTPException
  9. from starlette.responses import FileResponse, RedirectResponse, Response
  10. from starlette.types import Receive, Scope, Send
  11. PathLike = typing.Union[str, "os.PathLike[str]"]
  12. class NotModifiedResponse(Response):
  13. NOT_MODIFIED_HEADERS = (
  14. "cache-control",
  15. "content-location",
  16. "date",
  17. "etag",
  18. "expires",
  19. "vary",
  20. )
  21. def __init__(self, headers: Headers):
  22. super().__init__(
  23. status_code=304,
  24. headers={
  25. name: value
  26. for name, value in headers.items()
  27. if name in self.NOT_MODIFIED_HEADERS
  28. },
  29. )
  30. class StaticFiles:
  31. def __init__(
  32. self,
  33. *,
  34. directory: typing.Optional[PathLike] = None,
  35. packages: typing.Optional[
  36. typing.List[typing.Union[str, typing.Tuple[str, str]]]
  37. ] = None,
  38. html: bool = False,
  39. check_dir: bool = True,
  40. follow_symlink: bool = False,
  41. ) -> None:
  42. self.directory = directory
  43. self.packages = packages
  44. self.all_directories = self.get_directories(directory, packages)
  45. self.html = html
  46. self.config_checked = False
  47. self.follow_symlink = follow_symlink
  48. if check_dir and directory is not None and not os.path.isdir(directory):
  49. raise RuntimeError(f"Directory '{directory}' does not exist")
  50. def get_directories(
  51. self,
  52. directory: typing.Optional[PathLike] = None,
  53. packages: typing.Optional[
  54. typing.List[typing.Union[str, typing.Tuple[str, str]]]
  55. ] = None,
  56. ) -> typing.List[PathLike]:
  57. """
  58. Given `directory` and `packages` arguments, return a list of all the
  59. directories that should be used for serving static files from.
  60. """
  61. directories = []
  62. if directory is not None:
  63. directories.append(directory)
  64. for package in packages or []:
  65. if isinstance(package, tuple):
  66. package, statics_dir = package
  67. else:
  68. statics_dir = "statics"
  69. spec = importlib.util.find_spec(package)
  70. assert spec is not None, f"Package {package!r} could not be found."
  71. assert spec.origin is not None, f"Package {package!r} could not be found."
  72. package_directory = os.path.normpath(
  73. os.path.join(spec.origin, "..", statics_dir)
  74. )
  75. assert os.path.isdir(
  76. package_directory
  77. ), f"Directory '{statics_dir!r}' in package {package!r} could not be found."
  78. directories.append(package_directory)
  79. return directories
  80. async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
  81. """
  82. The ASGI entry point.
  83. """
  84. assert scope["type"] == "http"
  85. if not self.config_checked:
  86. await self.check_config()
  87. self.config_checked = True
  88. path = self.get_path(scope)
  89. response = await self.get_response(path, scope)
  90. await response(scope, receive, send)
  91. def get_path(self, scope: Scope) -> str:
  92. """
  93. Given the ASGI scope, return the `path` string to serve up,
  94. with OS specific path separators, and any '..', '.' components removed.
  95. """
  96. return os.path.normpath(os.path.join(*scope["path"].split("/")))
  97. async def get_response(self, path: str, scope: Scope) -> Response:
  98. """
  99. Returns an HTTP response, given the incoming path, method and request headers.
  100. """
  101. if scope["method"] not in ("GET", "HEAD"):
  102. raise HTTPException(status_code=405)
  103. try:
  104. full_path, stat_result = await anyio.to_thread.run_sync(
  105. self.lookup_path, path
  106. )
  107. except PermissionError:
  108. raise HTTPException(status_code=401)
  109. except OSError:
  110. raise
  111. if stat_result and stat.S_ISREG(stat_result.st_mode):
  112. # We have a static file to serve.
  113. return self.file_response(full_path, stat_result, scope)
  114. elif stat_result and stat.S_ISDIR(stat_result.st_mode) and self.html:
  115. # We're in HTML mode, and have got a directory URL.
  116. # Check if we have 'index.html' file to serve.
  117. index_path = os.path.join(path, "index.html")
  118. full_path, stat_result = await anyio.to_thread.run_sync(
  119. self.lookup_path, index_path
  120. )
  121. if stat_result is not None and stat.S_ISREG(stat_result.st_mode):
  122. if not scope["path"].endswith("/"):
  123. # Directory URLs should redirect to always end in "/".
  124. url = URL(scope=scope)
  125. url = url.replace(path=url.path + "/")
  126. return RedirectResponse(url=url)
  127. return self.file_response(full_path, stat_result, scope)
  128. if self.html:
  129. # Check for '404.html' if we're in HTML mode.
  130. full_path, stat_result = await anyio.to_thread.run_sync(
  131. self.lookup_path, "404.html"
  132. )
  133. if stat_result and stat.S_ISREG(stat_result.st_mode):
  134. return FileResponse(
  135. full_path,
  136. stat_result=stat_result,
  137. method=scope["method"],
  138. status_code=404,
  139. )
  140. raise HTTPException(status_code=404)
  141. def lookup_path(
  142. self, path: str
  143. ) -> typing.Tuple[str, typing.Optional[os.stat_result]]:
  144. for directory in self.all_directories:
  145. joined_path = os.path.join(directory, path)
  146. if self.follow_symlink:
  147. full_path = os.path.abspath(joined_path)
  148. else:
  149. full_path = os.path.realpath(joined_path)
  150. directory = os.path.realpath(directory)
  151. if os.path.commonpath([full_path, directory]) != directory:
  152. # Don't allow misbehaving clients to break out of the static files
  153. # directory.
  154. continue
  155. try:
  156. return full_path, os.stat(full_path)
  157. except (FileNotFoundError, NotADirectoryError):
  158. continue
  159. return "", None
  160. def file_response(
  161. self,
  162. full_path: PathLike,
  163. stat_result: os.stat_result,
  164. scope: Scope,
  165. status_code: int = 200,
  166. ) -> Response:
  167. method = scope["method"]
  168. request_headers = Headers(scope=scope)
  169. response = FileResponse(
  170. full_path, status_code=status_code, stat_result=stat_result, method=method
  171. )
  172. if self.is_not_modified(response.headers, request_headers):
  173. return NotModifiedResponse(response.headers)
  174. return response
  175. async def check_config(self) -> None:
  176. """
  177. Perform a one-off configuration check that StaticFiles is actually
  178. pointed at a directory, so that we can raise loud errors rather than
  179. just returning 404 responses.
  180. """
  181. if self.directory is None:
  182. return
  183. try:
  184. stat_result = await anyio.to_thread.run_sync(os.stat, self.directory)
  185. except FileNotFoundError:
  186. raise RuntimeError(
  187. f"StaticFiles directory '{self.directory}' does not exist."
  188. )
  189. if not (stat.S_ISDIR(stat_result.st_mode) or stat.S_ISLNK(stat_result.st_mode)):
  190. raise RuntimeError(
  191. f"StaticFiles path '{self.directory}' is not a directory."
  192. )
  193. def is_not_modified(
  194. self, response_headers: Headers, request_headers: Headers
  195. ) -> bool:
  196. """
  197. Given the request and response headers, return `True` if an HTTP
  198. "Not Modified" response could be returned instead.
  199. """
  200. try:
  201. if_none_match = request_headers["if-none-match"]
  202. etag = response_headers["etag"]
  203. if if_none_match == etag:
  204. return True
  205. except KeyError:
  206. pass
  207. try:
  208. if_modified_since = parsedate(request_headers["if-modified-since"])
  209. last_modified = parsedate(response_headers["last-modified"])
  210. if (
  211. if_modified_since is not None
  212. and last_modified is not None
  213. and if_modified_since >= last_modified
  214. ):
  215. return True
  216. except KeyError:
  217. pass
  218. return False