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 comparison from wrong formats with datetime objects #2697

Merged
merged 6 commits into from
Mar 20, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion sanic/response/convenience.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,26 @@ async def validate_file(
last_modified = datetime.fromtimestamp(
float(last_modified), tz=timezone.utc
).replace(microsecond=0)
if last_modified <= if_modified_since:

if (
last_modified.utcoffset() is None
and if_modified_since.utcoffset() is not None
):
logger.warning(
"Cannot compare tz-aware and tz-naive datetimes. To avoid "
"this conflict Sanic is converting last_modified to UTC."
)
last_modified.replace(tzinfo=timezone.utc)
elif (
last_modified.utcoffset() is not None
and if_modified_since.utcoffset() is None
):
logger.warning(
"Cannot compare tz-aware and tz-naive datetimes. To avoid "
"this conflict Sanic is converting if_modified_since to UTC."
)
if_modified_since.replace(tzinfo=timezone.utc)
if last_modified.timestamp() <= if_modified_since.timestamp():
return HTTPResponse(status=304)


Expand Down
55 changes: 55 additions & 0 deletions tests/test_response_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from datetime import datetime, timezone
from logging import INFO

import pytest

from sanic.compat import Header
from sanic.response.convenience import validate_file


@pytest.mark.parametrize(
"ifmod,lastmod,expected",
(
("Sat, 01 Apr 2023 00:00:00 GMT", 1672524000, None),
(
"Sat, 01 Apr 2023 00:00:00",
1672524000,
"converting if_modified_since",
),
(
"Sat, 01 Apr 2023 00:00:00 GMT",
datetime(2023, 1, 1, 0, 0, 0),
"converting last_modified",
),
(
"Sat, 01 Apr 2023 00:00:00",
datetime(2023, 1, 1, 0, 0, 0),
None,
),
(
"Sat, 01 Apr 2023 00:00:00 GMT",
datetime(2023, 1, 1, 0, 0, 0).replace(tzinfo=timezone.utc),
None,
),
(
"Sat, 01 Apr 2023 00:00:00",
datetime(2023, 1, 1, 0, 0, 0).replace(tzinfo=timezone.utc),
"converting if_modified_since",
),
),
)
@pytest.mark.asyncio
async def test_file_timestamp_validation(
lastmod, ifmod, expected, caplog: pytest.LogCaptureFixture
):
headers = Header([["If-Modified-Since", ifmod]])

with caplog.at_level(INFO):
response = await validate_file(headers, lastmod)
assert response.status == 304
records = caplog.records
if not expected:
assert len(records) == 0
else:
record = records[0]
assert expected in record.message