Merge pull request #1005 from joar/feature/static-strict-slashes

Add strict_slashes to {app, blueprint}.static()
This commit is contained in:
Raphael Deem 2017-11-07 07:49:32 -08:00 committed by GitHub
commit 979b5a52d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 6 deletions

View File

@ -348,13 +348,14 @@ class Sanic:
# Static Files # Static Files
def static(self, uri, file_or_directory, pattern=r'/?.+', def static(self, uri, file_or_directory, pattern=r'/?.+',
use_modified_since=True, use_content_range=False, use_modified_since=True, use_content_range=False,
stream_large_files=False, name='static', host=None): stream_large_files=False, name='static', host=None,
strict_slashes=None):
"""Register a root to serve files from. The input can either be a """Register a root to serve files from. The input can either be a
file or a directory. See file or a directory. See
""" """
static_register(self, uri, file_or_directory, pattern, static_register(self, uri, file_or_directory, pattern,
use_modified_since, use_content_range, use_modified_since, use_content_range,
stream_large_files, name, host) stream_large_files, name, host, strict_slashes)
def blueprint(self, blueprint, **options): def blueprint(self, blueprint, **options):
"""Register a blueprint on the application. """Register a blueprint on the application.

View File

@ -221,8 +221,12 @@ class Blueprint:
name = kwargs.pop('name', 'static') name = kwargs.pop('name', 'static')
if not name.startswith(self.name + '.'): if not name.startswith(self.name + '.'):
name = '{}.{}'.format(self.name, name) name = '{}.{}'.format(self.name, name)
kwargs.update(name=name) kwargs.update(name=name)
strict_slashes = kwargs.get('strict_slashes')
if strict_slashes is None and self.strict_slashes is not None:
kwargs.update(strict_slashes=self.strict_slashes)
static = FutureStatic(uri, file_or_directory, args, kwargs) static = FutureStatic(uri, file_or_directory, args, kwargs)
self.statics.append(static) self.statics.append(static)

View File

@ -18,7 +18,8 @@ from sanic.response import file, file_stream, HTTPResponse
def register(app, uri, file_or_directory, pattern, def register(app, uri, file_or_directory, pattern,
use_modified_since, use_content_range, use_modified_since, use_content_range,
stream_large_files, name='static', host=None): stream_large_files, name='static', host=None,
strict_slashes=None):
# TODO: Though sanic is not a file server, I feel like we should at least # TODO: Though sanic is not a file server, I feel like we should at least
# make a good effort here. Modified-since is nice, but we could # make a good effort here. Modified-since is nice, but we could
# also look into etags, expires, and caching # also look into etags, expires, and caching
@ -103,7 +104,7 @@ def register(app, uri, file_or_directory, pattern,
if isinstance(stream_large_files, int): if isinstance(stream_large_files, int):
threshold = stream_large_files threshold = stream_large_files
else: else:
threshold = 1024*1000 threshold = 1024 * 1024
if not stats: if not stats:
stats = await stat(file_path) stats = await stat(file_path)
@ -122,4 +123,5 @@ def register(app, uri, file_or_directory, pattern,
if not name.startswith('_static_'): if not name.startswith('_static_'):
name = '_static_{}'.format(name) name = '_static_{}'.format(name)
app.route(uri, methods=['GET', 'HEAD'], name=name, host=host)(_handler) app.route(uri, methods=['GET', 'HEAD'], name=name, host=host,
strict_slashes=strict_slashes)(_handler)