diff options
| author | David Lord <davidism@gmail.com> | 2020-02-05 21:24:01 -0800 |
|---|---|---|
| committer | David Lord <davidism@gmail.com> | 2020-02-05 21:24:57 -0800 |
| commit | a8b2df202d81b45f7aeef442a844603e50db9b0c (patch) | |
| tree | ef24a998d053479441b4edd5d1d4ab85c1972974 | |
| parent | 04743549a7e305b46ec8e4a955d389fb6572acfa (diff) | |
| download | werkzeug-shared-data-charset.tar.gz | |
SharedDataMiddleware adds utf-8 charsetshared-data-charset
| -rw-r--r-- | CHANGES.rst | 5 | ||||
| -rw-r--r-- | src/werkzeug/middleware/shared_data.py | 24 | ||||
| -rw-r--r-- | tests/middleware/test_shared_data.py | 4 |
3 files changed, 24 insertions, 9 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index ae5fba0a..90d07e8f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -101,6 +101,11 @@ Unreleased - Range requests that span an entire file respond with 206 instead of 200, to be more compliant with :rfc:`7233`. This may help serving media to older browsers. :issue:`410, 1704` +- The :class:`~middleware.shared_data.SharedDataMiddleware` default + ``fallback_mimetype`` is ``application/octet-stream``. If a filename + looks like a text mimetype, the ``utf-8`` charset is added to it. + This matches the behavior of :class:`~wrappers.BaseResponse` and + Flask's ``send_file()``. :issue:`1689` Version 0.16.1 diff --git a/src/werkzeug/middleware/shared_data.py b/src/werkzeug/middleware/shared_data.py index 5c000c97..ab4ff0ff 100644 --- a/src/werkzeug/middleware/shared_data.py +++ b/src/werkzeug/middleware/shared_data.py @@ -24,6 +24,7 @@ from ..filesystem import get_filesystem_encoding from ..http import http_date from ..http import is_resource_modified from ..security import safe_join +from ..utils import get_content_type from ..wsgi import get_path_info from ..wsgi import wrap_file @@ -70,19 +71,24 @@ class SharedDataMiddleware(object): module. If it's unable to figure out the charset it will fall back to `fallback_mimetype`. - .. versionchanged:: 0.5 - The cache timeout is configurable now. - - .. versionadded:: 0.6 - The `fallback_mimetype` parameter was added. - :param app: the application to wrap. If you don't want to wrap an application you can pass it :exc:`NotFound`. :param exports: a list or dict of exported files and folders. :param disallow: a list of :func:`~fnmatch.fnmatch` rules. - :param fallback_mimetype: the fallback mimetype for unknown files. :param cache: enable or disable caching headers. :param cache_timeout: the cache timeout in seconds for the headers. + :param fallback_mimetype: The fallback mimetype for unknown files. + + .. versionchanged:: 1.0 + The default ``fallback_mimetype`` is + ``application/octet-stream``. If a filename looks like a text + mimetype, the ``utf-8`` charset is added to it. + + .. versionadded:: 0.6 + Added ``fallback_mimetype``. + + .. versionchanged:: 0.5 + Added ``cache_timeout``. """ def __init__( @@ -92,7 +98,7 @@ class SharedDataMiddleware(object): disallow=None, cache=True, cache_timeout=60 * 60 * 12, - fallback_mimetype="text/plain", + fallback_mimetype="application/octet-stream", ): self.app = app self.exports = [] @@ -254,7 +260,7 @@ class SharedDataMiddleware(object): return self.app(environ, start_response) guessed_type = mimetypes.guess_type(real_filename) - mime_type = guessed_type[0] or self.fallback_mimetype + mime_type = get_content_type(guessed_type[0] or self.fallback_mimetype, "utf-8") f, mtime, file_size = file_loader() headers = [("Date", http_date())] diff --git a/tests/middleware/test_shared_data.py b/tests/middleware/test_shared_data.py index fb685f77..175cbd67 100644 --- a/tests/middleware/test_shared_data.py +++ b/tests/middleware/test_shared_data.py @@ -47,6 +47,10 @@ def test_shared_data_middleware(tmpdir): app_iter, status, headers = run_wsgi_app(app, create_environ(p)) assert status == "200 OK" + if p.endswith(".txt"): + content_type = next(v for k, v in headers if k == "Content-Type") + assert content_type == "text/plain; charset=utf-8" + with closing(app_iter) as app_iter: data = b"".join(app_iter).strip() |
