Skip to content

Commit

Permalink
Upgrade to aiohttp 1.2 (#4964)
Browse files Browse the repository at this point in the history
* Upgrade to aiohttp 1.2

* Clean up emulated_hue tests
  • Loading branch information
balloob authored Jan 11, 2017
1 parent 1cf9ae5 commit e68e29e
Show file tree
Hide file tree
Showing 6 changed files with 373 additions and 387 deletions.
2 changes: 1 addition & 1 deletion homeassistant/components/emulated_hue/hue_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def __init__(self, config):
self.config = config

@core.callback
def get(self, request, username, entity_id=None):
def get(self, request, username, entity_id):
"""Process a request to get the state of an individual light."""
hass = request.app['hass']
entity_id = self.config.number_to_entity_id(entity_id)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
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'
Expand Down Expand Up @@ -272,7 +272,7 @@ def register_static_path(self, url_root, path, cache_length=31):
@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
Expand Down
69 changes: 20 additions & 49 deletions homeassistant/components/http/static.py
Original file line number Diff line number Diff line change
@@ -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')

if gzip_path.is_file():
filepath = gzip_path
gzip = True

st = filepath.stat()

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)
orig_sendfile = self._sendfile

return resp
@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)

yield from orig_sendfile(request, resp, fobj, count)

# Overwriting like this because __init__ can change implementation.
self._sendfile = sendfile


GZIP_FILE_SENDER = GzipFileSender()
FILE_SENDER = FileSender()
CACHING_FILE_SENDER = CachingFileSender()


@asyncio.coroutine
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pip>=7.0.0
jinja2>=2.8
voluptuous==0.9.2
typing>=3,<4
aiohttp==1.1.6
aiohttp==1.2
async_timeout==1.1.0

# homeassistant.components.nuimo_controller
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
'jinja2>=2.8',
'voluptuous==0.9.2',
'typing>=3,<4',
'aiohttp==1.1.6',
'aiohttp==1.2',
'async_timeout==1.1.0',
]

Expand Down
Loading

0 comments on commit e68e29e

Please sign in to comment.