* Fix CORS duplicate registration (#15670)

* Bumped version to 0.74.2
This commit is contained in:
Paulus Schoutsen
2018-07-25 13:09:32 +02:00
committed by GitHub
parent 5e9c1098c0
commit 9d59bfbe00
4 changed files with 59 additions and 23 deletions
+23 -17
View File
@@ -27,30 +27,36 @@ def setup_cors(app, origins):
) for host in origins
})
def allow_cors(route, methods):
"""Allow cors on a route."""
cors.add(route, {
'*': aiohttp_cors.ResourceOptions(
allow_headers=ALLOWED_CORS_HEADERS,
allow_methods=methods,
)
})
cors_added = set()
app['allow_cors'] = allow_cors
def _allow_cors(route, config=None):
"""Allow cors on a route."""
if hasattr(route, 'resource'):
path = route.resource
else:
path = route
path = path.canonical
if path in cors_added:
return
cors.add(route, config)
cors_added.add(path)
app['allow_cors'] = lambda route: _allow_cors(route, {
'*': aiohttp_cors.ResourceOptions(
allow_headers=ALLOWED_CORS_HEADERS,
allow_methods='*',
)
})
if not origins:
return
async def cors_startup(app):
"""Initialize cors when app starts up."""
cors_added = set()
for route in list(app.router.routes()):
if hasattr(route, 'resource'):
route = route.resource
if route in cors_added:
continue
cors.add(route)
cors_added.add(route)
_allow_cors(route)
app.on_startup.append(cors_startup)
+3 -5
View File
@@ -69,15 +69,13 @@ class HomeAssistantView(object):
handler = request_handler_factory(self, handler)
for url in urls:
routes.append(
(method, router.add_route(method, url, handler))
)
routes.append(router.add_route(method, url, handler))
if not self.cors_allowed:
return
for method, route in routes:
app['allow_cors'](route, [method.upper()])
for route in routes:
app['allow_cors'](route)
def request_handler_factory(view, handler):
+1 -1
View File
@@ -2,7 +2,7 @@
"""Constants used by Home Assistant components."""
MAJOR_VERSION = 0
MINOR_VERSION = 74
PATCH_VERSION = '1'
PATCH_VERSION = '2'
__short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)
REQUIRED_PYTHON_VER = (3, 5, 3)