support vhosts in static routes
This commit is contained in:
parent
00d40a35cd
commit
9aec5febb8
|
@ -354,13 +354,13 @@ 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'):
|
stream_large_files=False, name='static', host=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)
|
stream_large_files, name, host)
|
||||||
|
|
||||||
def blueprint(self, blueprint, **options):
|
def blueprint(self, blueprint, **options):
|
||||||
"""Register a blueprint on the application.
|
"""Register a blueprint on the application.
|
||||||
|
|
|
@ -18,7 +18,7 @@ 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'):
|
stream_large_files, name='static', host=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
|
||||||
|
@ -122,4 +122,4 @@ 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)(_handler)
|
app.route(uri, methods=['GET', 'HEAD'], name=name, host=host)(_handler)
|
||||||
|
|
|
@ -161,3 +161,20 @@ def test_static_content_range_error(file_name, static_file_directory):
|
||||||
assert 'Content-Range' in response.headers
|
assert 'Content-Range' in response.headers
|
||||||
assert response.headers['Content-Range'] == "bytes */%s" % (
|
assert response.headers['Content-Range'] == "bytes */%s" % (
|
||||||
len(get_file_content(static_file_directory, file_name)),)
|
len(get_file_content(static_file_directory, file_name)),)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
|
||||||
|
def test_static_file(static_file_directory, file_name):
|
||||||
|
app = Sanic('test_static')
|
||||||
|
app.static(
|
||||||
|
'/testing.file',
|
||||||
|
get_file_path(static_file_directory, file_name),
|
||||||
|
host="www.example.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
headers = {"Host": "www.example.com"}
|
||||||
|
request, response = app.test_client.get('/testing.file', headers=headers)
|
||||||
|
assert response.status == 200
|
||||||
|
assert response.body == get_file_content(static_file_directory, file_name)
|
||||||
|
request, response = app.test_client.get('/testing.file')
|
||||||
|
assert response.status == 404
|
||||||
|
|
Loading…
Reference in New Issue
Block a user