Fix and improve file cache control header calculation (#2486)

This commit is contained in:
Zhiwei 2022-06-26 15:11:48 -05:00 committed by GitHub
parent 0e1bf89fad
commit 70382f21ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -3,6 +3,8 @@ import os
import signal
import sys
from typing import Awaitable
from multidict import CIMultiDict # type: ignore
@ -51,7 +53,7 @@ use_trio = sys.argv[0].endswith("hypercorn") and "trio" in sys.argv
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

View File

@ -5,7 +5,7 @@ from email.utils import formatdate
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,
@ -22,7 +22,7 @@ from typing import (
)
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
@ -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(