shared_data.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. """
  2. Serve Shared Static Files
  3. =========================
  4. .. autoclass:: SharedDataMiddleware
  5. :members: is_allowed
  6. :copyright: 2007 Pallets
  7. :license: BSD-3-Clause
  8. """
  9. from __future__ import annotations
  10. import collections.abc as cabc
  11. import importlib.util
  12. import mimetypes
  13. import os
  14. import posixpath
  15. import typing as t
  16. from datetime import datetime
  17. from datetime import timezone
  18. from io import BytesIO
  19. from time import time
  20. from zlib import adler32
  21. from ..http import http_date
  22. from ..http import is_resource_modified
  23. from ..security import safe_join
  24. from ..utils import get_content_type
  25. from ..wsgi import get_path_info
  26. from ..wsgi import wrap_file
  27. _TOpener = t.Callable[[], tuple[t.IO[bytes], datetime, int]]
  28. _TLoader = t.Callable[[t.Optional[str]], tuple[t.Optional[str], t.Optional[_TOpener]]]
  29. if t.TYPE_CHECKING:
  30. from _typeshed.wsgi import StartResponse
  31. from _typeshed.wsgi import WSGIApplication
  32. from _typeshed.wsgi import WSGIEnvironment
  33. class SharedDataMiddleware:
  34. """A WSGI middleware which provides static content for development
  35. environments or simple server setups. Its usage is quite simple::
  36. import os
  37. from werkzeug.middleware.shared_data import SharedDataMiddleware
  38. app = SharedDataMiddleware(app, {
  39. '/shared': os.path.join(os.path.dirname(__file__), 'shared')
  40. })
  41. The contents of the folder ``./shared`` will now be available on
  42. ``http://example.com/shared/``. This is pretty useful during development
  43. because a standalone media server is not required. Files can also be
  44. mounted on the root folder and still continue to use the application because
  45. the shared data middleware forwards all unhandled requests to the
  46. application, even if the requests are below one of the shared folders.
  47. If `pkg_resources` is available you can also tell the middleware to serve
  48. files from package data::
  49. app = SharedDataMiddleware(app, {
  50. '/static': ('myapplication', 'static')
  51. })
  52. This will then serve the ``static`` folder in the `myapplication`
  53. Python package.
  54. The optional `disallow` parameter can be a list of :func:`~fnmatch.fnmatch`
  55. rules for files that are not accessible from the web. If `cache` is set to
  56. `False` no caching headers are sent.
  57. Currently the middleware does not support non-ASCII filenames. If the
  58. encoding on the file system happens to match the encoding of the URI it may
  59. work but this could also be by accident. We strongly suggest using ASCII
  60. only file names for static files.
  61. The middleware will guess the mimetype using the Python `mimetype`
  62. module. If it's unable to figure out the charset it will fall back
  63. to `fallback_mimetype`.
  64. :param app: the application to wrap. If you don't want to wrap an
  65. application you can pass it :exc:`NotFound`.
  66. :param exports: a list or dict of exported files and folders.
  67. :param disallow: a list of :func:`~fnmatch.fnmatch` rules.
  68. :param cache: enable or disable caching headers.
  69. :param cache_timeout: the cache timeout in seconds for the headers.
  70. :param fallback_mimetype: The fallback mimetype for unknown files.
  71. .. versionchanged:: 1.0
  72. The default ``fallback_mimetype`` is
  73. ``application/octet-stream``. If a filename looks like a text
  74. mimetype, the ``utf-8`` charset is added to it.
  75. .. versionadded:: 0.6
  76. Added ``fallback_mimetype``.
  77. .. versionchanged:: 0.5
  78. Added ``cache_timeout``.
  79. """
  80. def __init__(
  81. self,
  82. app: WSGIApplication,
  83. exports: (
  84. cabc.Mapping[str, str | tuple[str, str]]
  85. | t.Iterable[tuple[str, str | tuple[str, str]]]
  86. ),
  87. disallow: None = None,
  88. cache: bool = True,
  89. cache_timeout: int = 60 * 60 * 12,
  90. fallback_mimetype: str = "application/octet-stream",
  91. ) -> None:
  92. self.app = app
  93. self.exports: list[tuple[str, _TLoader]] = []
  94. self.cache = cache
  95. self.cache_timeout = cache_timeout
  96. if isinstance(exports, cabc.Mapping):
  97. exports = exports.items()
  98. for key, value in exports:
  99. if isinstance(value, tuple):
  100. loader = self.get_package_loader(*value)
  101. elif isinstance(value, str):
  102. if os.path.isfile(value):
  103. loader = self.get_file_loader(value)
  104. else:
  105. loader = self.get_directory_loader(value)
  106. else:
  107. raise TypeError(f"unknown def {value!r}")
  108. self.exports.append((key, loader))
  109. if disallow is not None:
  110. from fnmatch import fnmatch
  111. self.is_allowed = lambda x: not fnmatch(x, disallow)
  112. self.fallback_mimetype = fallback_mimetype
  113. def is_allowed(self, filename: str) -> bool:
  114. """Subclasses can override this method to disallow the access to
  115. certain files. However by providing `disallow` in the constructor
  116. this method is overwritten.
  117. """
  118. return True
  119. def _opener(self, filename: str) -> _TOpener:
  120. return lambda: (
  121. open(filename, "rb"),
  122. datetime.fromtimestamp(os.path.getmtime(filename), tz=timezone.utc),
  123. int(os.path.getsize(filename)),
  124. )
  125. def get_file_loader(self, filename: str) -> _TLoader:
  126. return lambda x: (os.path.basename(filename), self._opener(filename))
  127. def get_package_loader(self, package: str, package_path: str) -> _TLoader:
  128. load_time = datetime.now(timezone.utc)
  129. spec = importlib.util.find_spec(package)
  130. reader = spec.loader.get_resource_reader(package) # type: ignore[union-attr]
  131. def loader(
  132. path: str | None,
  133. ) -> tuple[str | None, _TOpener | None]:
  134. if path is None:
  135. return None, None
  136. path = safe_join(package_path, path)
  137. if path is None:
  138. return None, None
  139. basename = posixpath.basename(path)
  140. try:
  141. resource = reader.open_resource(path)
  142. except OSError:
  143. return None, None
  144. if isinstance(resource, BytesIO):
  145. return (
  146. basename,
  147. lambda: (resource, load_time, len(resource.getvalue())),
  148. )
  149. return (
  150. basename,
  151. lambda: (
  152. resource,
  153. datetime.fromtimestamp(
  154. os.path.getmtime(resource.name), tz=timezone.utc
  155. ),
  156. os.path.getsize(resource.name),
  157. ),
  158. )
  159. return loader
  160. def get_directory_loader(self, directory: str) -> _TLoader:
  161. def loader(
  162. path: str | None,
  163. ) -> tuple[str | None, _TOpener | None]:
  164. if path is not None:
  165. path = safe_join(directory, path)
  166. if path is None:
  167. return None, None
  168. else:
  169. path = directory
  170. if os.path.isfile(path):
  171. return os.path.basename(path), self._opener(path)
  172. return None, None
  173. return loader
  174. def generate_etag(self, mtime: datetime, file_size: int, real_filename: str) -> str:
  175. fn_str = os.fsencode(real_filename)
  176. timestamp = mtime.timestamp()
  177. checksum = adler32(fn_str) & 0xFFFFFFFF
  178. return f"wzsdm-{timestamp}-{file_size}-{checksum}"
  179. def __call__(
  180. self, environ: WSGIEnvironment, start_response: StartResponse
  181. ) -> t.Iterable[bytes]:
  182. path = get_path_info(environ)
  183. file_loader = None
  184. for search_path, loader in self.exports:
  185. if search_path == path:
  186. real_filename, file_loader = loader(None)
  187. if file_loader is not None:
  188. break
  189. if not search_path.endswith("/"):
  190. search_path += "/"
  191. if path.startswith(search_path):
  192. real_filename, file_loader = loader(path[len(search_path) :])
  193. if file_loader is not None:
  194. break
  195. if file_loader is None or not self.is_allowed(real_filename): # type: ignore
  196. return self.app(environ, start_response)
  197. guessed_type = mimetypes.guess_type(real_filename) # type: ignore
  198. mime_type = get_content_type(guessed_type[0] or self.fallback_mimetype, "utf-8")
  199. f, mtime, file_size = file_loader()
  200. headers = [("Date", http_date())]
  201. if self.cache:
  202. timeout = self.cache_timeout
  203. etag = self.generate_etag(mtime, file_size, real_filename) # type: ignore
  204. headers += [
  205. ("Etag", f'"{etag}"'),
  206. ("Cache-Control", f"max-age={timeout}, public"),
  207. ]
  208. if not is_resource_modified(environ, etag, last_modified=mtime):
  209. f.close()
  210. start_response("304 Not Modified", headers)
  211. return []
  212. headers.append(("Expires", http_date(time() + timeout)))
  213. else:
  214. headers.append(("Cache-Control", "public"))
  215. headers.extend(
  216. (
  217. ("Content-Type", mime_type),
  218. ("Content-Length", str(file_size)),
  219. ("Last-Modified", http_date(mtime)),
  220. )
  221. )
  222. start_response("200 OK", headers)
  223. return wrap_file(environ, f)