shared_data.py 9.3 KB

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