From d1dbb45078f931c6bb584d764090b0db80fe1429 Mon Sep 17 00:00:00 2001 From: Giovanni Barillari Date: Thu, 3 Oct 2024 11:57:12 +0200 Subject: [PATCH] Rebase changes --- starlette/responses.py | 8 +++++--- tests/middleware/test_base.py | 3 ++- tests/test_responses.py | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/starlette/responses.py b/starlette/responses.py index 346b8f94e..62cad0802 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -336,6 +336,8 @@ def set_stat_headers(self, stat_result: os.stat_result) -> None: async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: send_header_only: bool = scope["method"].upper() == "HEAD" + send_pathsend: bool = "http.response.pathsend" in scope.get("extensions", {}) + if self.stat_result is None: try: stat_result = await anyio.to_thread.run_sync(os.stat, self.path) @@ -354,7 +356,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: http_if_range = headers.get("if-range") if http_range is None or (http_if_range is not None and not self._should_use_range(http_if_range)): - await self._handle_simple(send, send_header_only) + await self._handle_simple(send, send_header_only, send_pathsend) else: try: ranges = self._parse_range_header(http_range, stat_result.st_size) @@ -373,11 +375,11 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if self.background is not None: await self.background() - async def _handle_simple(self, send: Send, send_header_only: bool) -> None: + async def _handle_simple(self, send: Send, send_header_only: bool, send_pathsend: bool) -> None: await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers}) if send_header_only: await send({"type": "http.response.body", "body": b"", "more_body": False}) - elif "http.response.pathsend" in scope["extensions"]: + elif send_pathsend: await send({"type": "http.response.pathsend", "path": str(self.path)}) else: async with await anyio.open_file(self.path, mode="rb") as file: diff --git a/tests/middleware/test_base.py b/tests/middleware/test_base.py index c0440f5cc..6618233b9 100644 --- a/tests/middleware/test_base.py +++ b/tests/middleware/test_base.py @@ -4,7 +4,7 @@ from collections.abc import AsyncGenerator, AsyncIterator, Generator from contextlib import AsyncExitStack from pathlib import Path -from typing import Any, AsyncGenerator, AsyncIterator, Generator +from typing import Any import anyio import pytest @@ -1182,6 +1182,7 @@ async def passthrough(request: Request, call_next: RequestResponseEndpoint) -> R "version": "3", "method": "GET", "path": "/", + "headers": [], "extensions": {"http.response.pathsend": {}}, } diff --git a/tests/test_responses.py b/tests/test_responses.py index 4be069989..f019db807 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -379,7 +379,7 @@ async def send(message: Message) -> None: # Since the TestClient doesn't support `pathsend`, we need to test this directly. await app( - {"type": "http", "method": "get", "extensions": {"http.response.pathsend": {}}}, + {"type": "http", "method": "get", "headers": [], "extensions": {"http.response.pathsend": {}}}, receive, send, )