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

Add support for RFC5861 Cache-Control headers #2949

Merged
merged 1 commit into from
Oct 25, 2024
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Unreleased
- ``CacheControl.no_transform`` is a boolean when present. ``min_fresh`` is
``None`` when not present. Added the ``must_understand`` attribute. Fixed
some typing issues on cache control. :issue:`2881`
- Add ``stale_while_revalidate`` and ``stale_if_error`` properties to
``ResponseCacheControl``. :issue:`2948`
- Add 421 ``MisdirectedRequest`` HTTP exception. :issue:`2850`


Expand Down
22 changes: 4 additions & 18 deletions docs/datastructures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,12 @@ HTTP Related
.. autoclass:: LanguageAccept

.. autoclass:: RequestCacheControl
:members:

.. autoattribute:: no_cache

.. autoattribute:: no_store

.. autoattribute:: max_age

.. autoattribute:: no_transform
:members:
:inherited-members:

.. autoclass:: ResponseCacheControl
:members:

.. autoattribute:: no_cache

.. autoattribute:: no_store

.. autoattribute:: max_age

.. autoattribute:: no_transform
:members:
:inherited-members:

.. autoclass:: ETags
:members:
Expand Down
5 changes: 4 additions & 1 deletion src/werkzeug/datastructures/cache_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ class ResponseCacheControl(_CacheControl):
``no_transform`` is a boolean when present.

.. versionchanged:: 3.1
Added the ``must_understand`` attribute.
Added the ``must_understand``, ``stale_while_revalidate``, and
``stale_if_error`` attributes.

.. versionchanged:: 2.1.1
``s_maxage`` converts the value to an int.
Expand All @@ -186,6 +187,8 @@ class ResponseCacheControl(_CacheControl):
s_maxage = cache_control_property("s-maxage", None, int)
immutable = cache_control_property("immutable", None, bool)
must_understand = cache_control_property("must-understand", None, bool)
stale_while_revalidate = cache_control_property("stale-while-revalidate", None, int)
stale_if_error = cache_control_property("stale-if-error", None, int)


# circular dependencies
Expand Down
12 changes: 12 additions & 0 deletions src/werkzeug/datastructures/cache_control.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,15 @@ class ResponseCacheControl(_CacheControl):
def must_understand(self, value: bool | None) -> None: ...
@must_understand.deleter
def must_understand(self) -> None: ...
@property
def stale_while_revalidate(self) -> int | None: ...
@stale_while_revalidate.setter
def stale_while_revalidate(self, value: int | None) -> None: ...
@stale_while_revalidate.deleter
def stale_while_revalidate(self) -> None: ...
@property
def stale_if_error(self) -> int | None: ...
@stale_if_error.setter
def stale_if_error(self, value: int | None) -> None: ...
@stale_if_error.deleter
def stale_if_error(self) -> None: ...
12 changes: 12 additions & 0 deletions tests/test_datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,18 @@ def test_must_understand(self):
cc = ds.ResponseCacheControl()
assert cc.must_understand is False

def test_stale_while_revalidate(self):
cc = ds.ResponseCacheControl([("stale-while-revalidate", "1")])
assert cc.stale_while_revalidate == 1
cc = ds.ResponseCacheControl()
assert cc.stale_while_revalidate is None

def test_stale_if_error(self):
cc = ds.ResponseCacheControl([("stale-if-error", "1")])
assert cc.stale_if_error == 1
cc = ds.ResponseCacheControl()
assert cc.stale_while_revalidate is None


class TestContentSecurityPolicy:
def test_construct(self):
Expand Down