shared_data.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. import mimetypes
  10. import os
  11. import pkgutil
  12. import posixpath
  13. import typing as t
  14. from datetime import datetime
  15. from datetime import timezone
  16. from io import BytesIO
  17. from time import time
  18. from zlib import adler32
  19. from ..filesystem import get_filesystem_encoding
  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: t.Union[
  83. t.Dict[str, t.Union[str, t.Tuple[str, str]]],
  84. t.Iterable[t.Tuple[str, t.Union[str, t.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: t.List[t.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. provider = pkgutil.get_loader(package)
  129. if hasattr(provider, "get_resource_reader"):
  130. # Python 3
  131. reader = provider.get_resource_reader(package) # type: ignore
  132. def loader(
  133. path: t.Optional[str],
  134. ) -> t.Tuple[t.Optional[str], t.Optional[_TOpener]]:
  135. if path is None:
  136. return None, None
  137. path = safe_join(package_path, path)
  138. if path is None:
  139. return None, None
  140. basename = posixpath.basename(path)
  141. try:
  142. resource = reader.open_resource(path)
  143. except OSError:
  144. return None, None
  145. if isinstance(resource, BytesIO):
  146. return (
  147. basename,
  148. lambda: (resource, load_time, len(resource.getvalue())),
  149. )
  150. return (
  151. basename,
  152. lambda: (
  153. resource,
  154. datetime.fromtimestamp(
  155. os.path.getmtime(resource.name), tz=timezone.utc
  156. ),
  157. os.path.getsize(resource.name),
  158. ),
  159. )
  160. else:
  161. # Python 3.6
  162. package_filename = provider.get_filename(package) # type: ignore
  163. is_filesystem = os.path.exists(package_filename)
  164. root = os.path.join(os.path.dirname(package_filename), package_path)
  165. def loader(
  166. path: t.Optional[str],
  167. ) -> t.Tuple[t.Optional[str], t.Optional[_TOpener]]:
  168. if path is None:
  169. return None, None
  170. path = safe_join(root, path)
  171. if path is None:
  172. return None, None
  173. basename = posixpath.basename(path)
  174. if is_filesystem:
  175. if not os.path.isfile(path):
  176. return None, None
  177. return basename, self._opener(path)
  178. try:
  179. data = provider.get_data(path) # type: ignore
  180. except OSError:
  181. return None, None
  182. return basename, lambda: (BytesIO(data), load_time, len(data))
  183. return loader
  184. def get_directory_loader(self, directory: str) -> _TLoader:
  185. def loader(
  186. path: t.Optional[str],
  187. ) -> t.Tuple[t.Optional[str], t.Optional[_TOpener]]:
  188. if path is not None:
  189. path = safe_join(directory, path)
  190. if path is None:
  191. return None, None
  192. else:
  193. path = directory
  194. if os.path.isfile(path):
  195. return os.path.basename(path), self._opener(path)
  196. return None, None
  197. return loader
  198. def generate_etag(self, mtime: datetime, file_size: int, real_filename: str) -> str:
  199. if not isinstance(real_filename, bytes):
  200. real_filename = real_filename.encode( # type: ignore
  201. get_filesystem_encoding()
  202. )
  203. timestamp = mtime.timestamp()
  204. checksum = adler32(real_filename) & 0xFFFFFFFF # type: ignore
  205. return f"wzsdm-{timestamp}-{file_size}-{checksum}"
  206. def __call__(
  207. self, environ: "WSGIEnvironment", start_response: "StartResponse"
  208. ) -> t.Iterable[bytes]:
  209. path = get_path_info(environ)
  210. file_loader = None
  211. for search_path, loader in self.exports:
  212. if search_path == path:
  213. real_filename, file_loader = loader(None)
  214. if file_loader is not None:
  215. break
  216. if not search_path.endswith("/"):
  217. search_path += "/"
  218. if path.startswith(search_path):
  219. real_filename, file_loader = loader(path[len(search_path) :])
  220. if file_loader is not None:
  221. break
  222. if file_loader is None or not self.is_allowed(real_filename): # type: ignore
  223. return self.app(environ, start_response)
  224. guessed_type = mimetypes.guess_type(real_filename) # type: ignore
  225. mime_type = get_content_type(guessed_type[0] or self.fallback_mimetype, "utf-8")
  226. f, mtime, file_size = file_loader()
  227. headers = [("Date", http_date())]
  228. if self.cache:
  229. timeout = self.cache_timeout
  230. etag = self.generate_etag(mtime, file_size, real_filename) # type: ignore
  231. headers += [
  232. ("Etag", f'"{etag}"'),
  233. ("Cache-Control", f"max-age={timeout}, public"),
  234. ]
  235. if not is_resource_modified(environ, etag, last_modified=mtime):
  236. f.close()
  237. start_response("304 Not Modified", headers)
  238. return []
  239. headers.append(("Expires", http_date(time() + timeout)))
  240. else:
  241. headers.append(("Cache-Control", "public"))
  242. headers.extend(
  243. (
  244. ("Content-Type", mime_type),
  245. ("Content-Length", str(file_size)),
  246. ("Last-Modified", http_date(mtime)),
  247. )
  248. )
  249. start_response("200 OK", headers)
  250. return wrap_file(environ, f)