Merge pull request #104 from channelcat/pr/101

Static file support
This commit is contained in:
Channel Cat
2016-10-24 22:42:01 -07:00
committed by GitHub
14 changed files with 217 additions and 12 deletions

View File

@@ -33,6 +33,15 @@ class BlueprintSetup:
"""
self.app.exception(*args, **kwargs)(handler)
def add_static(self, file_or_directory, uri, *args, **kwargs):
"""
Registers static files to sanic
"""
if self.url_prefix:
uri = self.url_prefix + uri
self.app.static(file_or_directory, uri, *args, **kwargs)
def add_middleware(self, middleware, *args, **kwargs):
"""
Registers middleware to sanic
@@ -112,3 +121,9 @@ class Blueprint:
self.record(lambda s: s.add_exception(handler, *args, **kwargs))
return handler
return decorator
def static(self, file_or_directory, uri, *args, **kwargs):
"""
"""
self.record(
lambda s: s.add_static(file_or_directory, uri, *args, **kwargs))

View File

@@ -21,6 +21,15 @@ class ServerError(SanicException):
status_code = 500
class FileNotFound(NotFound):
status_code = 404
def __init__(self, message, path, relative_url):
super().__init__(message)
self.path = path
self.relative_url = relative_url
class Handler:
handlers = None

View File

@@ -1,6 +1,9 @@
from aiofiles import open as open_async
from datetime import datetime
from http.cookies import SimpleCookie
import ujson
from mimetypes import guess_type
from os import path
from ujson import dumps as json_dumps
COMMON_STATUS_CODES = {
200: b'OK',
@@ -136,7 +139,7 @@ class HTTPResponse:
def json(body, status=200, headers=None):
return HTTPResponse(ujson.dumps(body), headers=headers, status=status,
return HTTPResponse(json_dumps(body), headers=headers, status=status,
content_type="application/json")
@@ -148,3 +151,17 @@ def text(body, status=200, headers=None):
def html(body, status=200, headers=None):
return HTTPResponse(body, status=status, headers=headers,
content_type="text/html; charset=utf-8")
async def file(location, mime_type=None, headers=None):
filename = path.split(location)[-1]
async with open_async(location, mode='rb') as _file:
out_stream = await _file.read()
mime_type = mime_type or guess_type(filename)[0] or 'text/plain'
return HTTPResponse(status=200,
headers=headers,
content_type=mime_type,
body_bytes=out_stream)

View File

@@ -82,6 +82,9 @@ class Router:
# Mark the whole route as unhashable if it has the hash key in it
if re.search('(^|[^^]){1}/', pattern):
properties['unhashable'] = True
# Mark the route as unhashable if it matches the hash key
elif re.search(pattern, '/'):
properties['unhashable'] = True
return '({})'.format(pattern)

View File

@@ -13,6 +13,7 @@ from .log import log, logging
from .response import HTTPResponse
from .router import Router
from .server import serve
from .static import register as static_register
from .exceptions import ServerError
@@ -29,6 +30,9 @@ class Sanic:
self.loop = None
self.debug = None
# Register alternative method names
self.go_fast = self.run
# -------------------------------------------------------------------- #
# Registration
# -------------------------------------------------------------------- #
@@ -42,6 +46,11 @@ class Sanic:
:return: decorated function
"""
# Fix case where the user did not prefix the URL with a /
# and will probably get confused as to why it's not working
if not uri.startswith('/'):
uri = '/' + uri
def response(handler):
self.router.add(uri=uri, methods=methods, handler=handler)
return handler
@@ -85,7 +94,17 @@ class Sanic:
attach_to = args[0]
return register_middleware
def register_blueprint(self, blueprint, **options):
# Static Files
def static(self, file_or_directory, uri, pattern='.+',
use_modified_since=True):
"""
Registers a root to serve files from. The input can either be a file
or a directory. See
"""
static_register(self, file_or_directory, uri, pattern,
use_modified_since)
def blueprint(self, blueprint, **options):
"""
Registers a blueprint on the application.
:param blueprint: Blueprint object
@@ -102,6 +121,12 @@ class Sanic:
self._blueprint_order.append(blueprint)
blueprint.register(self, options)
def register_blueprint(self, *args, **kwargs):
# TODO: deprecate 1.0
log.warning("Use of register_blueprint will be deprecated in "
"version 1.0. Please use the blueprint method instead")
return self.blueprint(*args, **kwargs)
# -------------------------------------------------------------------- #
# Request Handling
# -------------------------------------------------------------------- #

59
sanic/static.py Normal file
View File

@@ -0,0 +1,59 @@
from aiofiles.os import stat
from os import path
from re import sub
from time import strftime, gmtime
from .exceptions import FileNotFound, InvalidUsage
from .response import file, HTTPResponse
def register(app, file_or_directory, uri, pattern, use_modified_since):
# TODO: Though sanic is not a file server, I feel like we should atleast
# make a good effort here. Modified-since is nice, but we could
# also look into etags, expires, and caching
"""
Registers a static directory handler with Sanic by adding a route to the
router and registering a handler.
:param app: Sanic
:param file_or_directory: File or directory path to serve from
:param uri: URL to serve from
:param pattern: regular expression used to match files in the URL
:param use_modified_since: If true, send file modified time, and return
not modified if the browser's matches the server's
"""
# If we're not trying to match a file directly,
# serve from the folder
if not path.isfile(file_or_directory):
uri += '<file_uri:' + pattern + '>'
async def _handler(request, file_uri=None):
# Using this to determine if the URL is trying to break out of the path
# served. os.path.realpath seems to be very slow
if file_uri and '../' in file_uri:
raise InvalidUsage("Invalid URL")
# Merge served directory and requested file if provided
# Strip all / that in the beginning of the URL to help prevent python
# from herping a derp and treating the uri as an absolute path
file_path = path.join(file_or_directory, sub('^[/]*', '', file_uri)) \
if file_uri else file_or_directory
try:
headers = {}
# Check if the client has been sent this file before
# and it has not been modified since
if use_modified_since:
stats = await stat(file_path)
modified_since = strftime('%a, %d %b %Y %H:%M:%S GMT',
gmtime(stats.st_mtime))
if request.headers.get('If-Modified-Since') == modified_since:
return HTTPResponse(status=304)
headers['Last-Modified'] = modified_since
return await file(file_path, headers=headers)
except:
raise FileNotFound('File not found',
path=file_or_directory,
relative_url=file_uri)
app.route(uri, methods=['GET'])(_handler)

View File

@@ -11,6 +11,7 @@ async def local_request(method, uri, cookies=None, *args, **kwargs):
async with aiohttp.ClientSession(cookies=cookies) as session:
async with getattr(session, method)(url, *args, **kwargs) as response:
response.text = await response.text()
response.body = await response.read()
return response