Skip to content
Myr

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

3 files+36 added−5 removed

modified:flask/app.py+1 added-1 removed
326 unmodified lines
327 return FileSystemLoader(os.path.join(self.root_path, 'templates'))328 return PackageLoader(self.import_name)329330 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.334335 .. versionadded:: 0.5
500 unmodified lines
modified:flask/helpers.py+33 added-4 removed
10 unmodified lines
1112import os13import sys14import mimetypes15from time import time16from zlib import adler321718# try to load the best simplejson implementation available. If JSON19# is not installed, we add a failing class.20json_available = True
4 unmodified lines
25 import json26 except ImportError:27 json_available = False2827from werkzeug import Headers, wrap_file29from werkzeug import Headers, wrap_file, is_resource_modified3031from flask.globals import session, _request_ctx_stack, current_app, request32from flask.wrappers import Response33
166 unmodified lines
200 return flashes201202203def 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` attribute
12 unmodified lines
222 abort(404)223224 .. versionadded:: 0.2225226 .. versionadded:: 0.5227 The `add_etags`, `cache_timeout` and `conditional` parameters were added.228 The default behaviour is now to attach etags.229230 :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 provided
4 unmodified lines
238 :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 = None
26 unmodified lines
275 if file is None:276 file = open(filename, 'rb')277 data = wrap_file(request.environ, file)278269 return Response(data, mimetype=mimetype, headers=headers,270 direct_passthrough=True)279 rv = Response(data, mimetype=mimetype, headers=headers,280 direct_passthrough=True)281282 rv.cache_control.public = True283 if cache_timeout:284 rv.cache_control.max_age = cache_timeout285 rv.expires = int(time() + cache_timeout)286287 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 rv300301302def render_template(template_name, **context):303 """Renders a template from the template folder with the given304 context.305
65 unmodified lines
modified:CHANGES+2 added
15 unmodified lines
16 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.2223Version 0.424-----------25
66 unmodified lines