Skip to content

Commit

Permalink
Merge branch 'master' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanBogarin authored Dec 5, 2024
2 parents 43c5e67 + 9456955 commit 2d7c529
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
14 changes: 7 additions & 7 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ async def _prepare_open_file(
#
# Will do the same below. Many servers ignore this and do not
# send a Content-Range header with HTTP 416
self.headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
return await super().prepare(request)

Expand Down Expand Up @@ -348,7 +348,7 @@ async def _prepare_open_file(
# suffix-byte-range-spec with a non-zero suffix-length,
# then the byte-range-set is satisfiable. Otherwise, the
# byte-range-set is unsatisfiable.
self.headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self._headers[hdrs.CONTENT_RANGE] = f"bytes */{file_size}"
self.set_status(HTTPRequestRangeNotSatisfiable.status_code)
return await super().prepare(request)

Expand All @@ -360,16 +360,16 @@ async def _prepare_open_file(
# If the Content-Type header is not already set, guess it based on the
# extension of the request path. The encoding returned by guess_type
# can be ignored since the map was cleared above.
if hdrs.CONTENT_TYPE not in self.headers:
if hdrs.CONTENT_TYPE not in self._headers:
if sys.version_info >= (3, 13):
guesser = CONTENT_TYPES.guess_file_type
else:
guesser = CONTENT_TYPES.guess_type
self.content_type = guesser(self._path)[0] or FALLBACK_CONTENT_TYPE

if file_encoding:
self.headers[hdrs.CONTENT_ENCODING] = file_encoding
self.headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING
self._headers[hdrs.CONTENT_ENCODING] = file_encoding
self._headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING
# Disable compression if we are already sending
# a compressed file since we don't want to double
# compress.
Expand All @@ -379,12 +379,12 @@ async def _prepare_open_file(
self.last_modified = st.st_mtime # type: ignore[assignment]
self.content_length = count

self.headers[hdrs.ACCEPT_RANGES] = "bytes"
self._headers[hdrs.ACCEPT_RANGES] = "bytes"

real_start = cast(int, start)

if status == HTTPPartialContent.status_code:
self.headers[hdrs.CONTENT_RANGE] = "bytes {}-{}/{}".format(
self._headers[hdrs.CONTENT_RANGE] = "bytes {}-{}/{}".format(
real_start, real_start + count - 1, file_size
)

Expand Down
51 changes: 46 additions & 5 deletions tests/test_benchmarks_web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import asyncio
import pathlib

from multidict import CIMultiDict
from pytest_codspeed import BenchmarkFixture

from aiohttp import web
from aiohttp import ClientResponse, web
from aiohttp.pytest_plugin import AiohttpClient


Expand All @@ -24,15 +25,15 @@ async def handler(request: web.Request) -> web.FileResponse:
app = web.Application()
app.router.add_route("GET", "/", handler)

async def run_file_resonse_benchmark() -> None:
async def run_file_response_benchmark() -> None:
client = await aiohttp_client(app)
for _ in range(response_count):
await client.get("/")
await client.close()

@benchmark
def _run() -> None:
loop.run_until_complete(run_file_resonse_benchmark())
loop.run_until_complete(run_file_response_benchmark())


def test_simple_web_file_sendfile_fallback_response(
Expand All @@ -53,12 +54,52 @@ async def handler(request: web.Request) -> web.FileResponse:
app = web.Application()
app.router.add_route("GET", "/", handler)

async def run_file_resonse_benchmark() -> None:
async def run_file_response_benchmark() -> None:
client = await aiohttp_client(app)
for _ in range(response_count):
await client.get("/")
await client.close()

@benchmark
def _run() -> None:
loop.run_until_complete(run_file_resonse_benchmark())
loop.run_until_complete(run_file_response_benchmark())


def test_simple_web_file_response_not_modified(
loop: asyncio.AbstractEventLoop,
aiohttp_client: AiohttpClient,
benchmark: BenchmarkFixture,
) -> None:
"""Benchmark web.FileResponse that return a 304."""
response_count = 100
filepath = pathlib.Path(__file__).parent / "sample.txt"

async def handler(request: web.Request) -> web.FileResponse:
return web.FileResponse(path=filepath)

app = web.Application()
app.router.add_route("GET", "/", handler)

async def make_last_modified_header() -> CIMultiDict[str]:
client = await aiohttp_client(app)
resp = await client.get("/")
last_modified = resp.headers["Last-Modified"]
headers = CIMultiDict({"If-Modified-Since": last_modified})
return headers

async def run_file_response_benchmark(
headers: CIMultiDict[str],
) -> ClientResponse:
client = await aiohttp_client(app)
for _ in range(response_count):
resp = await client.get("/", headers=headers)

await client.close()
return resp # type: ignore[possibly-undefined]

headers = loop.run_until_complete(make_last_modified_header())

@benchmark
def _run() -> None:
resp = loop.run_until_complete(run_file_response_benchmark(headers))
assert resp.status == 304

0 comments on commit 2d7c529

Please sign in to comment.