| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
"""Abstract base classes.""" |
| 16 |
|
| 17 |
from abc import ABCMeta, abstractmethod |
| 18 |
|
| 19 |
from aiohttp import web |
| 20 |
|
| 21 |
__all__ = ("AbstractRouterAdapter",) |
| 22 |
|
| 23 |
|
| 24 |
class AbstractRouterAdapter(metaclass=ABCMeta): |
| 25 |
"""Router adapter for handling CORS configuration interface. |
| 26 |
|
| 27 |
`AbstractRouter` doesn't specify how HTTP requests are delivered |
| 28 |
to handlers, and aiohttp_cors doesn't rely on specific implementation |
| 29 |
details. |
| 30 |
|
| 31 |
In general Router can be seen as a substance that allows to setup handlers |
| 32 |
for specific HTTP methods and requests paths, lets call these Router's |
| 33 |
items routing entities. |
| 34 |
Generic Router is configured with set of routing entities and their |
| 35 |
handlers. |
| 36 |
|
| 37 |
This adapter assumes that its reasonable to configure CORS for same |
| 38 |
routing entities as used in `AbstractRouter`. |
| 39 |
Routing entities will be added to CorsConfig to enable CORS for them. |
| 40 |
|
| 41 |
For example, for aiohttp < 0.21.0 routing entity would be |
| 42 |
`aiohttp.web.Route` — tuple of (HTTP method, URI path). |
| 43 |
And CORS can be configured for each `aiohttp.web.Route`. |
| 44 |
|
| 45 |
In aiohttp >= 0.21.0 there are two routing entities: Resource and Route. |
| 46 |
You can configure CORS for Resource (which will be interpreted as default |
| 47 |
for all Routes on Resources), and configure CORS for specific Route. |
| 48 |
""" |
| 49 |
|
| 50 |
@abstractmethod |
| 51 |
def add_preflight_handler(self, routing_entity, handler, webview: bool = False): |
| 52 |
"""Add OPTIONS handler for all routes defined by `routing_entity`. |
| 53 |
|
| 54 |
Does nothing if CORS handler already handles routing entity. |
| 55 |
Should fail if there are conflicting user-defined OPTIONS handlers. |
| 56 |
""" |
| 57 |
|
| 58 |
@abstractmethod |
| 59 |
def is_preflight_request(self, request: web.Request) -> bool: |
| 60 |
"""Is `request` is a CORS preflight request.""" |
| 61 |
|
| 62 |
@abstractmethod |
| 63 |
def is_cors_enabled_on_request(self, request: web.Request) -> bool: |
| 64 |
"""Is `request` is a request for CORS-enabled resource.""" |
| 65 |
|
| 66 |
@abstractmethod |
| 67 |
def set_config_for_routing_entity(self, routing_entity, config): |
| 68 |
"""Record configuration for routing entity. |
| 69 |
|
| 70 |
If router implements hierarchical routing entities, stored config |
| 71 |
can be used in hierarchical manner too. |
| 72 |
|
| 73 |
Should raise if there is conflicting configuration for the routing |
| 74 |
entity. |
| 75 |
""" |
| 76 |
|
| 77 |
@abstractmethod |
| 78 |
async def get_preflight_request_config( |
| 79 |
self, preflight_request: web.Request, origin: str, requested_method: str |
| 80 |
): |
| 81 |
"""Get stored CORS configuration for specified HTTP method and origin |
| 82 |
that corresponds to preflight request. |
| 83 |
|
| 84 |
Should raise KeyError if CORS is not configured or not enabled |
| 85 |
for specified HTTP method. |
| 86 |
""" |
| 87 |
|
| 88 |
@abstractmethod |
| 89 |
def get_non_preflight_request_config(self, request: web.Request): |
| 90 |
"""Get stored CORS configuration for routing entity that handles |
| 91 |
specified request.""" |