shared_data.py 9.3 KB

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