Added support for conditional responses to 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 88d9315 · Browse files · 3 files·+36 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+36 added−5 removed
- other:+36 added−5 removed
Provenance
- Commit
d0c6ad7- Tree
9f58f34- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
88d9315
3 files changed+36 added−5 removed
modified:flask/app.py+1 added-1 removed
326 unmodified lines327 return FileSystemLoader(os.path.join(self.root_path, 'templates'))328 return PackageLoader(self.import_name)329 330 def init_jinja_globals(self):331- """Callde directly after the environment was created to inject331+ """Called directly after the environment was created to inject332 some defaults (like `url_for`, `get_flashed_messages` and the333 `tojson` filter.334 335 .. versionadded:: 0.5500 unmodified linesmodified:flask/helpers.py+33 added-4 removed
10 unmodified lines11 12 import os13 import sys14 import mimetypes15+from time import time16+from zlib import adler3217 18 # try to load the best simplejson implementation available. If JSON19 # is not installed, we add a failing class.20 json_available = True4 unmodified lines25 import json26 except ImportError:27 json_available = False28 27-from werkzeug import Headers, wrap_file29+from werkzeug import Headers, wrap_file, is_resource_modified30 31 from flask.globals import session, _request_ctx_stack, current_app, request32 from flask.wrappers import Response33 166 unmodified lines200 return flashes201 202 203 def send_file(filename_or_fp, mimetype=None, as_attachment=False,202- attachment_filename=None):204+ attachment_filename=None, add_etags=True,205+ cache_timeout=60 * 60 * 12, conditional=False):206 """Sends the contents of a file to the client. This will use the207 most efficient method available and configured. By default it will208 try to use the WSGI server's file_wrapper support. Alternatively209 you can set the application's :attr:`~Flask.use_x_sendfile` attribute12 unmodified lines222 abort(404)223 224 .. versionadded:: 0.2225 226+ .. versionadded:: 0.5227+ The `add_etags`, `cache_timeout` and `conditional` parameters were added.228+ The default behaviour is now to attach etags.229+230 :param filename_or_fp: the filename of the file to send. This is231 relative to the :attr:`~Flask.root_path` if a232 relative path is specified.233 Alternatively a file object might be provided4 unmodified lines238 :param as_attachment: set to `True` if you want to send this file with239 a ``Content-Disposition: attachment`` header.240 :param attachment_filename: the filename for the attachment if it241 differs from the file's filename.242+ :param add_etags: set to `False` to disable attaching of etags.243+ :param conditional: set to `True` to enable conditional responses.244+ :param cache_timeout: the timeout in seconds for the headers.245 """246 if isinstance(filename_or_fp, basestring):247 filename = filename_or_fp248 file = None26 unmodified lines275 if file is None:276 file = open(filename, 'rb')277 data = wrap_file(request.environ, file)278 269- return Response(data, mimetype=mimetype, headers=headers,270- direct_passthrough=True)279+ rv = Response(data, mimetype=mimetype, headers=headers,280+ direct_passthrough=True)281 282+ rv.cache_control.public = True283+ if cache_timeout:284+ rv.cache_control.max_age = cache_timeout285+ rv.expires = int(time() + cache_timeout)286 287+ if add_etags and filename is not None:288+ rv.set_etag('flask-%s-%s-%s' % (289+ os.path.getmtime(filename),290+ os.path.getsize(filename),291+ adler32(filename) & 0xffffffff292+ ))293+ if conditional:294+ rv = rv.make_conditional(request)295+ # make sure we don't send x-sendfile for servers that296+ # ignore the 304 status code for x-sendfile.297+ if rv.status_code == 304:298+ rv.headers.pop('x-sendfile', None)299+ return rv300+301+302 def render_template(template_name, **context):303 """Renders a template from the template folder with the given304 context.305 65 unmodified linesmodified:CHANGES+2 added
15 unmodified lines16 Inside templates this behaviour can be changed with the17 ``autoescape`` tag.18 - refactored Flask internally. It now consists of more than a19 single file.20+- :func:`flask.send_file` now emits etags and has the ability to21+ do conditional responses builtin.22 23 Version 0.424 -----------25 66 unmodified lines