diff --git a/cachecontrol/controller.py b/cachecontrol/controller.py index f826aec0..9c519b0e 100644 --- a/cachecontrol/controller.py +++ b/cachecontrol/controller.py @@ -277,9 +277,11 @@ def cached_request(self, request: PreparedRequest) -> HTTPResponse | Literal[Fal def conditional_headers(self, request: PreparedRequest) -> dict[str, str]: resp = self._load_from_cache(request) - new_headers = {} + if not resp: + return {} + with resp: + new_headers = {} - if resp: headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(resp.headers) if "etag" in headers: @@ -288,7 +290,7 @@ def conditional_headers(self, request: PreparedRequest) -> dict[str, str]: if "last-modified" in headers: new_headers["If-Modified-Since"] = headers["Last-Modified"] - return new_headers + return new_headers def _cache_set( self,