Upgrade to aiohttp 1.2 (#4964)
* Upgrade to aiohttp 1.2 * Clean up emulated_hue tests
This commit is contained in:
1 parent
1cf9ae5a01
commit
e68e29e03e
6 files changed
+316
-330
No files matched your search
@@ -32,7 +32,7 @@ from .const import (
|
||||
KEY_USE_X_FORWARDED_FOR, KEY_TRUSTED_NETWORKS,
|
||||
KEY_BANS_ENABLED, KEY_LOGIN_THRESHOLD,
|
||||
KEY_DEVELOPMENT, KEY_AUTHENTICATED)
|
||||
from .static import FILE_SENDER, GZIP_FILE_SENDER, staticresource_middleware
|
||||
from .static import FILE_SENDER, CACHING_FILE_SENDER, staticresource_middleware
|
||||
from .util import get_real_ip
|
||||
|
||||
DOMAIN = 'http'
|
||||
@@ -272,7 +272,7 @@ class HomeAssistantWSGI(object):
|
||||
@asyncio.coroutine
|
||||
def serve_file(request):
|
||||
"""Serve file from disk."""
|
||||
res = yield from GZIP_FILE_SENDER.send(request, filepath)
|
||||
res = yield from CACHING_FILE_SENDER.send(request, filepath)
|
||||
return res
|
||||
|
||||
# aiohttp supports regex matching for variables. Using that as temp
|
||||
|
||||
@@ -1,69 +1,40 @@
|
||||
"""Static file handling for HTTP component."""
|
||||
import asyncio
|
||||
import mimetypes
|
||||
import re
|
||||
|
||||
from aiohttp import hdrs
|
||||
from aiohttp.file_sender import FileSender
|
||||
from aiohttp.web_urldispatcher import StaticResource
|
||||
from aiohttp.web_exceptions import HTTPNotModified
|
||||
|
||||
from .const import KEY_DEVELOPMENT
|
||||
|
||||
_FINGERPRINT = re.compile(r'^(.+)-[a-z0-9]{32}\.(\w+)$', re.IGNORECASE)
|
||||
|
||||
|
||||
class GzipFileSender(FileSender):
|
||||
"""FileSender class capable of sending gzip version if available."""
|
||||
class CachingFileSender(FileSender):
|
||||
"""FileSender class that caches output if not in dev mode."""
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Initialize the hass file sender."""
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
@asyncio.coroutine
|
||||
def send(self, request, filepath):
|
||||
"""Send filepath to client using request."""
|
||||
gzip = False
|
||||
if 'gzip' in request.headers[hdrs.ACCEPT_ENCODING]:
|
||||
gzip_path = filepath.with_name(filepath.name + '.gz')
|
||||
orig_sendfile = self._sendfile
|
||||
|
||||
if gzip_path.is_file():
|
||||
filepath = gzip_path
|
||||
gzip = True
|
||||
@asyncio.coroutine
|
||||
def sendfile(request, resp, fobj, count):
|
||||
"""Sendfile that includes a cache header."""
|
||||
if not request.app[KEY_DEVELOPMENT]:
|
||||
cache_time = 31 * 86400 # = 1 month
|
||||
resp.headers[hdrs.CACHE_CONTROL] = "public, max-age={}".format(
|
||||
cache_time)
|
||||
|
||||
st = filepath.stat()
|
||||
yield from orig_sendfile(request, resp, fobj, count)
|
||||
|
||||
modsince = request.if_modified_since
|
||||
if modsince is not None and st.st_mtime <= modsince.timestamp():
|
||||
raise HTTPNotModified()
|
||||
|
||||
ct, encoding = mimetypes.guess_type(str(filepath))
|
||||
if not ct:
|
||||
ct = 'application/octet-stream'
|
||||
|
||||
resp = self._response_factory()
|
||||
resp.content_type = ct
|
||||
if encoding:
|
||||
resp.headers[hdrs.CONTENT_ENCODING] = encoding
|
||||
if gzip:
|
||||
resp.headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING
|
||||
resp.last_modified = st.st_mtime
|
||||
|
||||
# CACHE HACK
|
||||
if not request.app[KEY_DEVELOPMENT]:
|
||||
cache_time = 31 * 86400 # = 1 month
|
||||
resp.headers[hdrs.CACHE_CONTROL] = "public, max-age={}".format(
|
||||
cache_time)
|
||||
|
||||
file_size = st.st_size
|
||||
|
||||
resp.content_length = file_size
|
||||
with filepath.open('rb') as f:
|
||||
yield from self._sendfile(request, resp, f, file_size)
|
||||
|
||||
return resp
|
||||
# Overwriting like this because __init__ can change implementation.
|
||||
self._sendfile = sendfile
|
||||
|
||||
|
||||
GZIP_FILE_SENDER = GzipFileSender()
|
||||
FILE_SENDER = FileSender()
|
||||
CACHING_FILE_SENDER = CachingFileSender()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -77,7 +48,7 @@ def staticresource_middleware(app, handler):
|
||||
return handler
|
||||
|
||||
# pylint: disable=protected-access
|
||||
inst._file_sender = GZIP_FILE_SENDER
|
||||
inst._file_sender = CACHING_FILE_SENDER
|
||||
|
||||
@asyncio.coroutine
|
||||
def static_middleware_handler(request):
|
||||
|
||||
Reference in new issue
Block a user