Skip to content

Commit

Permalink
Fix and improve file cache control header calculation (#2486)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihweiLHBird committed Jun 26, 2022
1 parent 0e1bf89 commit 70382f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
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

0 comments on commit 70382f2

Please sign in to comment.