Skip to content

Commit

Permalink
make some middleware async capable (#125)
Browse files Browse the repository at this point in the history
Changes:

- Some middleware attached send to self. This doesn't work in async
  scenarios.
- bump version
  • Loading branch information
devkral authored Dec 27, 2024
1 parent 930e71e commit 104d2d6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 6 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ hide:
# Release Notes


## 0.11.11

### Fixed

- Some middleware are not multithreading/async capable.

## 0.11.10

### Added
Expand Down
2 changes: 1 addition & 1 deletion lilya/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.10"
__version__ = "0.11.11"
7 changes: 3 additions & 4 deletions lilya/middleware/clickjacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
"""
Expand Down
7 changes: 3 additions & 4 deletions lilya/middleware/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

0 comments on commit 104d2d6

Please sign in to comment.