From 104d2d6f8ff72bc19ad5138db8c59c0d2cac8652 Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 27 Dec 2024 11:07:12 +0100 Subject: [PATCH] make some middleware async capable (#125) Changes: - Some middleware attached send to self. This doesn't work in async scenarios. - bump version --- docs/en/docs/release-notes.md | 6 ++++++ lilya/__init__.py | 2 +- lilya/middleware/clickjacking.py | 7 +++---- lilya/middleware/security.py | 7 +++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/en/docs/release-notes.md b/docs/en/docs/release-notes.md index 54c1fef..adaffe7 100644 --- a/docs/en/docs/release-notes.md +++ b/docs/en/docs/release-notes.md @@ -6,6 +6,12 @@ hide: # Release Notes +## 0.11.11 + +### Fixed + +- Some middleware are not multithreading/async capable. + ## 0.11.10 ### Added diff --git a/lilya/__init__.py b/lilya/__init__.py index c9f772e..14a3fb7 100644 --- a/lilya/__init__.py +++ b/lilya/__init__.py @@ -1 +1 @@ -__version__ = "0.11.10" +__version__ = "0.11.11" diff --git a/lilya/middleware/clickjacking.py b/lilya/middleware/clickjacking.py index 1411063..8ca20a7 100644 --- a/lilya/middleware/clickjacking.py +++ b/lilya/middleware/clickjacking.py @@ -23,17 +23,16 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: Returns: None """ - self.send = send if scope["type"] not in ("http", "websocket"): await self.app(scope, receive, send) return async def send_wrapper(message: Message) -> None: - await self.process_response(scope, message) + await self.process_response(send, scope, message) await self.app(scope, receive, send_wrapper) - async def process_response(self, scope: Scope, message: Message) -> None: + async def process_response(self, send: Send, scope: Scope, message: Message) -> None: """ Process the response message and add the X-Frame-Options header if it is not already present. @@ -50,7 +49,7 @@ async def process_response(self, scope: Scope, message: Message) -> None: if headers.get("X-Frame-Options") is None: headers.add("X-Frame-Options", self.get_xframe_options()) message["headers"] = headers.encoded_multi_items() - await self.send(message) + await send(message) def get_xframe_options(self) -> str: """ diff --git a/lilya/middleware/security.py b/lilya/middleware/security.py index afc0db1..c7caadb 100644 --- a/lilya/middleware/security.py +++ b/lilya/middleware/security.py @@ -102,17 +102,16 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: receive (Receive): The receive channel. send (Send): The send channel. """ - self.send = send if scope["type"] not in ("http", "websocket"): await self.app(scope, receive, send) return async def send_wrapper(message: Message) -> None: - await self.process_response(scope, message) + await self.process_response(send, scope, message) await self.app(scope, receive, send_wrapper) - async def process_response(self, scope: Scope, message: Message) -> None: + async def process_response(self, send: Send, scope: Scope, message: Message) -> None: """ Process the response message and add security-related headers to the message headers. @@ -136,4 +135,4 @@ async def process_response(self, scope: Scope, message: Message) -> None: headers.add("X-XSS-Protection", self.xss_protection) message["headers"] = headers.encoded_multi_items() - await self.send(message) + await send(message)