Skip to content

Commit

Permalink
Rebase changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Jan 13, 2025
1 parent 065dbcd commit d1dbb45
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tests/middleware/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1182,6 +1182,7 @@ async def passthrough(request: Request, call_next: RequestResponseEndpoint) -> R
"version": "3",
"method": "GET",
"path": "/",
"headers": [],
"extensions": {"http.response.pathsend": {}},
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down

0 comments on commit d1dbb45

Please sign in to comment.