@@ -286,6 +286,8 @@ async def handle_request(self, scope: Scope, receive: Receive, send: Send) -> No
286286 await self ._handle_get_request (request , send )
287287 elif request .method == "DELETE" :
288288 await self ._handle_delete_request (request , send )
289+ elif await request .method == "OPTIONS" :
290+ await self ._handle_options_request (request , send )
289291 else :
290292 await self ._handle_unsupported_request (request , send )
291293
@@ -620,6 +622,21 @@ async def _handle_delete_request(self, request: Request, send: Send) -> None:
620622 )
621623 await response (request .scope , request .receive , send )
622624
625+ async def _handle_options_request (self , request : Request , send : Send ) -> None :
626+ """Handle OPTIONS requests for CORS preflight."""
627+ headers = {
628+ "Access-Control-Allow-Origin" : "*" ,
629+ "Access-Control-Allow-Methods" : "GET, POST, DELETE, OPTIONS" ,
630+ "Access-Control-Allow-Headers" : request .headers .get ("Access-Control-Request-Headers" , "" )
631+ }
632+
633+ response = Response (
634+ content = None ,
635+ status_code = HTTPStatus .NO_CONTENT ,
636+ headers = headers
637+ )
638+ await response (request .scope , request .receive , send )
639+
623640 async def terminate (self ) -> None :
624641 """Terminate the current session, closing all streams.
625642
0 commit comments