phased out shared data middleware in flask for send_file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent d0c6ad7 · Browse files · 2 files·+23 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+23 added−12 removed
- other:+23 added−12 removed
Provenance
- Commit
532347d- Tree
981f2fd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d0c6ad7
2 files changed+23 added−12 removed
modified:flask/app.py+21 added-11 removed
7 unmodified lines8 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11 12+import os13+import posixpath14 from threading import Lock15 from datetime import timedelta, datetime16 from itertools import chain17 18 from jinja2 import Environment, PackageLoader, FileSystemLoader17-from werkzeug import ImmutableDict, SharedDataMiddleware, create_environ19+from werkzeug import ImmutableDict, create_environ20 from werkzeug.routing import Map, Rule19-from werkzeug.exceptions import HTTPException, InternalServerError21+from werkzeug.exceptions import HTTPException, InternalServerError, NotFound22 23 from flask.helpers import _PackageBoundObject, url_for, get_flashed_messages, \22- _tojson_filter, get_pkg_resources24+ _tojson_filter, get_pkg_resources, send_file25 from flask.wrappers import Request, Response26 from flask.config import ConfigAttribute, Config27 from flask.ctx import _default_template_ctx_processor, _RequestContext28 from flask.globals import _request_ctx_stack, request230 unmodified lines259 self.url_map = Map()260 261 if self.static_path is not None:262 self.add_url_rule(self.static_path + '/<filename>',261- build_only=True, endpoint='static')262- if get_pkg_resources() is not None:263- target = (self.import_name, 'static')264- else:265- target = os.path.join(self.root_path, 'static')266- self.wsgi_app = SharedDataMiddleware(self.wsgi_app, {267- self.static_path: target268- })263+ endpoint='static',264+ view_func=self.send_static_file)265 266 #: The Jinja2 environment. It is created from the267 #: :attr:`jinja_options` and the loader that is returned268 #: by the :meth:`create_jinja_loader` function.109 unmodified lines378 options.setdefault('use_reloader', self.debug)379 options.setdefault('use_debugger', self.debug)380 return run_simple(host, port, self, **options)381 382+ def send_static_file(self, filename):383+ """Function used internally to send static files from the static384+ folder to the browser.385+386+ .. versionadded:: 0.5387+ """388+ filename = posixpath.normpath(filename)389+ if filename.startswith('../'):390+ raise NotFound()391+ filename = os.path.join(self.root_path, 'static', filename)392+ if not os.path.isfile(filename):393+ raise NotFound()394+ return send_file(filename, conditional=True)395+396 def test_client(self):397 """Creates a test client for this application. For information398 about unit testing head over to :ref:`testing`.399 446 unmodified linesmodified:flask/helpers.py+2 added-1 removed
248 unmodified lines249 else:250 file = filename_or_fp251 filename = getattr(file, 'name', None)252 if filename is not None:253+ if not os.path.isabs(filename):253- filename = os.path.join(current_app.root_path, filename)254+ filename = os.path.join(current_app.root_path, filename)255 if mimetype is None and (filename or attachment_filename):256 mimetype = mimetypes.guess_type(filename or attachment_filename)[0]257 if mimetype is None:258 mimetype = 'application/octet-stream'113 unmodified lines