-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement CorsViewMixin * fix real browser test * Add more tests to CorsViewMixin * Make python 3.4 compatible * Extract preflight handler to a class
- Loading branch information
1 parent
566c48e
commit 9e0c757
Showing
13 changed files
with
746 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import asyncio | ||
import collections | ||
|
||
from aiohttp import hdrs, web | ||
from .preflight_handler import _PreflightHandler | ||
|
||
def custom_cors(config): | ||
def wrapper(function): | ||
name = "{}_cors_config".format(function.__name__) | ||
setattr(function, name, config) | ||
return function | ||
return wrapper | ||
|
||
|
||
class CorsViewMixin(_PreflightHandler): | ||
cors_config = None | ||
|
||
@classmethod | ||
def get_request_config(cls, request, request_method): | ||
try: | ||
from . import APP_CONFIG_KEY | ||
cors = request.app[APP_CONFIG_KEY] | ||
except KeyError: | ||
raise ValueError("aiohttp-cors is not configured.") | ||
|
||
method = getattr(cls, request_method.lower(), None) | ||
|
||
if not method: | ||
raise KeyError() | ||
|
||
config_property_key = "{}_cors_config".format(request_method.lower()) | ||
|
||
custom_config = getattr(method, config_property_key, None) | ||
if not custom_config: | ||
custom_config = {} | ||
|
||
class_config = cls.cors_config | ||
if not class_config: | ||
class_config = {} | ||
|
||
return collections.ChainMap(custom_config, class_config, cors.defaults) | ||
|
||
@asyncio.coroutine | ||
def _get_config(self, request, origin, request_method): | ||
return self.get_request_config(request, request_method) | ||
|
||
@asyncio.coroutine | ||
def options(self): | ||
response = yield from self._preflight_handler(self.request) | ||
return response |
Oops, something went wrong.