From ca596c8ecd11dbbcc53f74f03cc3729aea2c09f5 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Thu, 2 Nov 2017 15:44:36 +0100 Subject: [PATCH 1/4] Add strict_slashes to {Sanic, Blueprint}().static() --- sanic/app.py | 5 +++-- sanic/blueprints.py | 6 +++++- sanic/static.py | 5 +++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 0f2141c2..121ea306 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -345,13 +345,14 @@ class Sanic: # Static Files def static(self, uri, file_or_directory, pattern=r'/?.+', 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 file or a directory. See """ static_register(self, uri, file_or_directory, pattern, use_modified_since, use_content_range, - stream_large_files, name, host) + stream_large_files, name, host, strict_slashes) def blueprint(self, blueprint, **options): """Register a blueprint on the application. diff --git a/sanic/blueprints.py b/sanic/blueprints.py index e8018326..28329abe 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -221,8 +221,12 @@ class Blueprint: name = kwargs.pop('name', 'static') if not name.startswith(self.name + '.'): name = '{}.{}'.format(self.name, name) - kwargs.update(name=name) + + strict_slashes = kwargs.pop('strict_slashes', None) + 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) self.statics.append(static) diff --git a/sanic/static.py b/sanic/static.py index 1ebd7291..46649fd8 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -18,7 +18,7 @@ from sanic.response import file, file_stream, HTTPResponse def register(app, uri, file_or_directory, pattern, 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 # make a good effort here. Modified-since is nice, but we could # also look into etags, expires, and caching @@ -122,4 +122,5 @@ def register(app, uri, file_or_directory, pattern, if not name.startswith('_static_'): 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) From ff5786d61bcefd4f3f09bcc5ebcc7a3387d713c8 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Fri, 3 Nov 2017 14:33:24 +0100 Subject: [PATCH 2/4] pep8 --- sanic/static.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sanic/static.py b/sanic/static.py index 46649fd8..0798e2fe 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -18,7 +18,8 @@ from sanic.response import file, file_stream, HTTPResponse def register(app, uri, file_or_directory, pattern, use_modified_since, use_content_range, - stream_large_files, name='static', host=None, strict_slashes=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 # make a good effort here. Modified-since is nice, but we could # 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): threshold = stream_large_files else: - threshold = 1024*1000 + threshold = 1024 * 1000 if not stats: stats = await stat(file_path) From f128ed5b1fc1472e5939a78099ebecd892deef1f Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Fri, 3 Nov 2017 14:37:01 +0100 Subject: [PATCH 3/4] Set threshold to 1MiB instead of 0.97MiB Reference: https://en.wikipedia.org/wiki/Mebibyte#Definition --- sanic/static.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/static.py b/sanic/static.py index 0798e2fe..f2d02ab0 100644 --- a/sanic/static.py +++ b/sanic/static.py @@ -104,7 +104,7 @@ def register(app, uri, file_or_directory, pattern, if isinstance(stream_large_files, int): threshold = stream_large_files else: - threshold = 1024 * 1000 + threshold = 1024 * 1024 if not stats: stats = await stat(file_path) From e70535e8d70b7965a3ac3c2a149e61b6961d5e76 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Tue, 7 Nov 2017 10:34:17 +0100 Subject: [PATCH 4/4] Use .get instead of .pop --- sanic/blueprints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 28329abe..f9159168 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -223,7 +223,7 @@ class Blueprint: name = '{}.{}'.format(self.name, name) kwargs.update(name=name) - strict_slashes = kwargs.pop('strict_slashes', None) + 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)