#918: caching does not respect Cache-Control: max-age header
-------------------------------------+--------------------------------------
Reporter: guest | Owner: fumanchu
Type: defect | Status: new
Priority: high | Milestone:
Component: CherryPy code | Resolution:
Keywords: cache-control cache age |
-------------------------------------+--------------------------------------
Comment (by
shau...@cern.ch):
The following lines inserted to caching.py at line 152 will fix this;
sorry I don't really know (yet?) how to commit this code formally:
#some people still use Pragma : no-cache to demand a fresh resource
pragma_no_cache=False
pragma_header=request.headers.get('Pragma')
if pragma_header:
pragma_values=[header.strip() for header in
pragma_header.split(',')]
for this_value in pragma_values:
if 'no-cache' in this_value:
pragma_no_cache=True
break
#added by shaun, look at cache-control
max_acceptable_age=MemoryCache.delay
age_seconds=int(response.time - create_time)
cache_control_header=request.headers.get('Cache-Control')
if cache_control_header:
#split string on commas to get the multiple words
cache_control_values=[header.strip() for header in
cache_control_header.split(',')]
#look for max-age
for this_value in cache_control_values:
if ('max-age' in this_value) and ('=' in this_value):
age_pair=[age.strip() for age in
this_value.split('=')]
if age_pair[1].isdigit():
max_acceptable_age=int(age_pair[1])
break
#return if the cache is older than the acceptable age
if (age_seconds > max_acceptable_age) or pragma_no_cache:
request.cached = False
request.cacheable = True
return False
# Add the required Age header
response.headers["Age"] = str(age_seconds)