| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""CORS support for aiohttp.""" |
| 16 |
|
| 17 |
from collections.abc import Mapping |
| 18 |
from typing import Any, Union |
| 19 |
|
| 20 |
from aiohttp import web |
| 21 |
|
| 22 |
from .__about__ import ( |
| 23 |
__author__, |
| 24 |
__copyright__, |
| 25 |
__email__, |
| 26 |
__license__, |
| 27 |
__summary__, |
| 28 |
__title__, |
| 29 |
__uri__, |
| 30 |
__version__, |
| 31 |
) |
| 32 |
from .cors_config import CorsConfig |
| 33 |
from .mixin import CorsViewMixin, custom_cors |
| 34 |
from .resource_options import ResourceOptions |
| 35 |
|
| 36 |
__all__ = ( |
| 37 |
"CorsConfig", |
| 38 |
"CorsViewMixin", |
| 39 |
"ResourceOptions", |
| 40 |
"__author__", |
| 41 |
"__copyright__", |
| 42 |
"__email__", |
| 43 |
"__license__", |
| 44 |
"__summary__", |
| 45 |
"__title__", |
| 46 |
"__uri__", |
| 47 |
"__version__", |
| 48 |
"custom_cors", |
| 49 |
"setup", |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
APP_CONFIG_KEY: web.AppKey[CorsConfig] = web.AppKey("aiohttp_cors", CorsConfig) |
| 54 |
|
| 55 |
|
| 56 |
def setup( |
| 57 |
app: web.Application, |
| 58 |
*, |
| 59 |
defaults: Mapping[str, Union[ResourceOptions, Mapping[str, Any]]] = None |
| 60 |
) -> CorsConfig: |
| 61 |
"""Setup CORS processing for the application. |
| 62 |
|
| 63 |
To enable CORS for a resource you need to explicitly add route for |
| 64 |
that resource using `CorsConfig.add()` method:: |
| 65 |
|
| 66 |
app = aiohttp.web.Application() |
| 67 |
cors = aiohttp_cors.setup(app) |
| 68 |
cors.add( |
| 69 |
app.router.add_route("GET", "/resource", handler), |
| 70 |
{ |
| 71 |
"*": aiohttp_cors.ResourceOptions( |
| 72 |
allow_credentials=True, |
| 73 |
expose_headers="*", |
| 74 |
allow_headers="*"), |
| 75 |
}) |
| 76 |
|
| 77 |
:param app: |
| 78 |
The application for which CORS will be configured. |
| 79 |
:param defaults: |
| 80 |
Default settings for origins. |
| 81 |
) |
| 82 |
""" |
| 83 |
cors = CorsConfig(app, defaults=defaults) |
| 84 |
app[APP_CONFIG_KEY] = cors |
| 85 |
return cors |