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

Don't use the cache if the request has a Range header #328

Merged
merged 1 commit into from
Feb 2, 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
5 changes: 5 additions & 0 deletions cachecontrol/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def _load_from_cache(self, request: PreparedRequest) -> HTTPResponse | None:
"""
Load a cached response, or return None if it's not available.
"""
# We do not support caching of partial content: so if the request contains a
# Range header then we don't want to load anything from the cache.
if "Range" in request.headers:
return None

cache_url = request.url
assert cache_url is not None
cache_data = self.cache.get(cache_url)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_etag.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ def test_etags_get_example(self, sess, server):
# Make sure we updated our cache with the new etag'd response.
assert self.cache.get(self.etag_url) == resp.raw

def test_etags_get_no_cache(self, sess, server):
"""A 'Cache-Control: no-cache' header stops us from using the cache directly,
but not from using the 'If-None-Match' header on the request."""
# get our response
r = sess.get(self.etag_url)
assert "if-none-match" not in r.request.headers

r = sess.get(self.etag_url, headers={"Cache-Control": "no-cache"})
assert "if-none-match" in r.request.headers
assert r.status_code == 200

# This response does come from the cache, but only after the 304 response from
# the server told us that was fine.
assert r.from_cache

def test_etags_get_with_range(self, sess, server):
"""A 'Range' header stops us from using the cache altogether."""
# get our response
r = sess.get(self.etag_url)

r = sess.get(self.etag_url, headers={"Range": "0-10"})
assert "if-none-match" not in r.request.headers
assert r.status_code == 200
assert not r.from_cache


class TestDisabledETags:
"""Test our use of ETags when the response is stale and the
Expand Down
Loading