Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and improve file cache control header calculation #2486

Merged
merged 5 commits into from Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion sanic/compat.py
Expand Up @@ -3,6 +3,8 @@
import signal
import sys

from typing import Awaitable

from multidict import CIMultiDict # type: ignore


Expand Down Expand Up @@ -51,7 +53,7 @@ def get_all(self, key: str):
if use_trio: # pragma: no cover
import trio # type: ignore

def stat_async(path):
def stat_async(path) -> Awaitable[os.stat_result]:
return trio.Path(path).stat()

open_async = trio.open_file
Expand Down
9 changes: 5 additions & 4 deletions sanic/response.py
Expand Up @@ -5,7 +5,7 @@
from functools import partial
from mimetypes import guess_type
from os import path
from pathlib import Path, PurePath
from pathlib import PurePath
from time import time
from typing import (
TYPE_CHECKING,
Expand All @@ -22,7 +22,7 @@
)
from urllib.parse import quote_plus

from sanic.compat import Header, open_async
from sanic.compat import Header, open_async, stat_async
from sanic.constants import DEFAULT_HTTP_CONTENT_TYPE
from sanic.cookies import CookieJar
from sanic.exceptions import SanicException, ServerError
Expand Down Expand Up @@ -340,9 +340,10 @@ async def file(
)

if isinstance(last_modified, datetime):
last_modified = last_modified.timestamp()
last_modified = last_modified.replace(microsecond=0).timestamp()
elif isinstance(last_modified, Default):
last_modified = Path(location).stat().st_mtime
stat = await stat_async(location)
last_modified = stat.st_mtime

if last_modified:
headers.setdefault(
Expand Down