Remove fingerprint middleware (#20682)

* Remove fingerprint middleware

* Lint
This commit is contained in:
Paulus Schoutsen 2019-02-02 02:52:34 -08:00 committed by Pascal Vizeli
parent 384a9625c9
commit 47f60e6cf2
2 changed files with 3 additions and 26 deletions

View File

@ -25,8 +25,7 @@ from .auth import setup_auth
from .ban import setup_bans from .ban import setup_bans
from .cors import setup_cors from .cors import setup_cors
from .real_ip import setup_real_ip from .real_ip import setup_real_ip
from .static import ( from .static import CachingFileResponse, CachingStaticResource
CachingFileResponse, CachingStaticResource, staticresource_middleware)
# Import as alias # Import as alias
from .const import KEY_AUTHENTICATED, KEY_REAL_IP # noqa from .const import KEY_AUTHENTICATED, KEY_REAL_IP # noqa
@ -192,8 +191,7 @@ class HomeAssistantHTTP:
use_x_forwarded_for, trusted_proxies, trusted_networks, use_x_forwarded_for, trusted_proxies, trusted_networks,
login_threshold, is_ban_enabled, ssl_profile): login_threshold, is_ban_enabled, ssl_profile):
"""Initialize the HTTP Home Assistant server.""" """Initialize the HTTP Home Assistant server."""
app = self.app = web.Application( app = self.app = web.Application(middlewares=[])
middlewares=[staticresource_middleware])
# This order matters # This order matters
setup_real_ip(app, use_x_forwarded_for, trusted_proxies) setup_real_ip(app, use_x_forwarded_for, trusted_proxies)

View File

@ -1,15 +1,10 @@
"""Static file handling for HTTP component.""" """Static file handling for HTTP component."""
import re
from aiohttp import hdrs from aiohttp import hdrs
from aiohttp.web import FileResponse, middleware from aiohttp.web import FileResponse
from aiohttp.web_exceptions import HTTPNotFound from aiohttp.web_exceptions import HTTPNotFound
from aiohttp.web_urldispatcher import StaticResource from aiohttp.web_urldispatcher import StaticResource
from yarl import URL from yarl import URL
_FINGERPRINT = re.compile(r'^(.+)-[a-z0-9]{32}\.(\w+)$', re.IGNORECASE)
class CachingStaticResource(StaticResource): class CachingStaticResource(StaticResource):
"""Static Resource handler that will add cache headers.""" """Static Resource handler that will add cache headers."""
@ -56,19 +51,3 @@ class CachingFileResponse(FileResponse):
# Overwriting like this because __init__ can change implementation. # Overwriting like this because __init__ can change implementation.
self._sendfile = sendfile self._sendfile = sendfile
@middleware
async def staticresource_middleware(request, handler):
"""Middleware to strip out fingerprint from fingerprinted assets."""
path = request.path
if not path.startswith('/static/') and not path.startswith('/frontend'):
return await handler(request)
fingerprinted = _FINGERPRINT.match(request.match_info['filename'])
if fingerprinted:
request.match_info['filename'] = \
'{}.{}'.format(*fingerprinted.groups())
return await handler(request)