Skip to content

Commit dc649e2

Browse files
committed
set If-Modified-Since on remote requests
1 parent 85e7aec commit dc649e2

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/cryptojwt/key_bundle.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def __init__(self, keys=None, source="", cache_time=300, verify_ssl=True,
181181
self._keys = []
182182
self.remote = False
183183
self.cache_time = cache_time
184+
self.last_fetched = None
184185
self.time_out = 0
185186
self.etag = ""
186187
self.source = None
@@ -336,6 +337,10 @@ def do_remote(self):
336337

337338
try:
338339
LOGGER.debug('KeyBundle fetch keys from: %s', self.source)
340+
if self.last_fetched is not None:
341+
if "headers" not in self.httpc_params:
342+
self.httpc_params["headers"] = {}
343+
self.httpc_params["headers"]["If-Modified-Since"] = self.last_fetched
339344
_http_resp = self.httpc('GET', self.source, **self.httpc_params)
340345
except Exception as err:
341346
LOGGER.error(err)
@@ -357,6 +362,14 @@ def do_remote(self):
357362
LOGGER.error("No 'keys' keyword in JWKS")
358363
raise UpdateFailed(MALFORMED.format(self.source))
359364

365+
if hasattr(_http_resp, "headers"):
366+
headers = getattr(_http_resp, "headers")
367+
self.last_fetched = headers.get("last-modified") or headers.get("date")
368+
369+
elif _http_resp.status_code == 304: # Not modified
370+
LOGGER.debug("%s not modified since %s", self.source, self.last_fetched)
371+
pass
372+
360373
else:
361374
raise UpdateFailed(
362375
REMOTE_FAILED.format(self.source, _http_resp.status_code))

0 commit comments

Comments
 (0)