Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced a method to adjust static file cache-control headers based on filename #433

Merged
merged 1 commit into from Mar 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions flask/helpers.py
Expand Up @@ -495,7 +495,8 @@ def download_file(filename):
filename = safe_join(directory, filename) filename = safe_join(directory, filename)
if not os.path.isfile(filename): if not os.path.isfile(filename):
raise NotFound() raise NotFound()
return send_file(filename, conditional=True, **options) options.setdefault('conditional', True)
return send_file(filename, **options)




def get_root_path(import_name): def get_root_path(import_name):
Expand Down Expand Up @@ -651,6 +652,11 @@ def jinja_loader(self):
return FileSystemLoader(os.path.join(self.root_path, return FileSystemLoader(os.path.join(self.root_path,
self.template_folder)) self.template_folder))


def get_static_file_options(self, filename):
"""Function used internally to determine what keyword arguments
to send to :func:`send_from_directory` for a specific file."""
return {}

def send_static_file(self, filename): def send_static_file(self, filename):
"""Function used internally to send static files from the static """Function used internally to send static files from the static
folder to the browser. folder to the browser.
Expand All @@ -659,7 +665,8 @@ def send_static_file(self, filename):
""" """
if not self.has_static_folder: if not self.has_static_folder:
raise RuntimeError('No static folder for this object') raise RuntimeError('No static folder for this object')
return send_from_directory(self.static_folder, filename) return send_from_directory(self.static_folder, filename,
**self.get_static_file_options(filename))


def open_resource(self, resource, mode='rb'): def open_resource(self, resource, mode='rb'):
"""Opens a resource from the application's resource folder. To see """Opens a resource from the application's resource folder. To see
Expand Down
26 changes: 25 additions & 1 deletion flask/testsuite/helpers.py
Expand Up @@ -17,7 +17,7 @@
from logging import StreamHandler from logging import StreamHandler
from StringIO import StringIO from StringIO import StringIO
from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr from flask.testsuite import FlaskTestCase, catch_warnings, catch_stderr
from werkzeug.http import parse_options_header from werkzeug.http import parse_cache_control_header, parse_options_header




def has_encoding(name): def has_encoding(name):
Expand Down Expand Up @@ -204,6 +204,30 @@ def test_attachment(self):
self.assert_equal(value, 'attachment') self.assert_equal(value, 'attachment')
self.assert_equal(options['filename'], 'index.txt') self.assert_equal(options['filename'], 'index.txt')


def test_static_file(self):
app = flask.Flask(__name__)
# default cache timeout is 12 hours (hard-coded)
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 12 * 60 * 60)
# override get_static_file_options with some new values and check them
class StaticFileApp(flask.Flask):
def __init__(self):
super(StaticFileApp, self).__init__(__name__)
def get_static_file_options(self, filename):
opts = super(StaticFileApp, self).get_static_file_options(filename)
opts['cache_timeout'] = 10
# this test catches explicit inclusion of the conditional
# keyword arg in the guts
opts['conditional'] = True
return opts
app = StaticFileApp()
with app.test_request_context():
rv = app.send_static_file('index.html')
cc = parse_cache_control_header(rv.headers['Cache-Control'])
self.assert_equal(cc.max_age, 10)



class LoggingTestCase(FlaskTestCase): class LoggingTestCase(FlaskTestCase):


Expand Down