Skip to content

Commit

Permalink
fix for cache-control headers
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-bm authored and Diego committed Jul 24, 2019
1 parent 4cca348 commit 8cd179c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flask_iiif/restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .decorators import api_decorator, error_handler
from .signals import iiif_after_info_request, iiif_after_process_request, \
iiif_before_info_request, iiif_before_process_request
from .utils import should_cache

current_iiif = LocalProxy(lambda: current_app.extensions['iiif'])

Expand Down Expand Up @@ -73,7 +74,8 @@ def get(self, version, uuid):
data = current_iiif.uuid_to_image_opener(uuid)
image = IIIFImageAPIWrapper.open_image(data)
width, height = image.size()
cache_handler.set(key, "{0},{1}".format(width, height))
if should_cache(request.args):
cache_handler.set(key, "{0},{1}".format(width, height))

data = current_app.config['IIIF_API_INFO_RESPONSE_SKELETON'][version]

Expand Down Expand Up @@ -166,7 +168,8 @@ def get(self, version, uuid, region, size, rotation, quality,
# prepare image to be serve
to_serve = image.serve(image_format=image_format)
# to_serve = image.serve(image_format=image_format)
cache_handler.set(key, to_serve.getvalue())
if should_cache(request.args):
cache_handler.set(key, to_serve.getvalue())

# decide the mime_type from the requested image_format
mimetype = current_app.config['IIIF_FORMATS'].get(
Expand Down
11 changes: 11 additions & 0 deletions flask_iiif/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,14 @@ def fill_background(image, demand_width, demand_height):
offset_y = max(1, int((demand_height - h) / 2))
background.paste(image, (offset_x, offset_y))
return background


def should_cache(request_args):
"""Check the request args for cache-control specifications.
:param request_args: flask request args
"""
if "cache-control" in request_args \
and request_args['cache-control'] in ["no-cache", "no-store"]:
return False
return True
69 changes: 69 additions & 0 deletions tests/test_restful_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,72 @@ def test_api_abort_all_methods_except_get(self):
urlargs=data
)
self.assert405(get_the_response)

def test_api_cache_control(self):
"""Test cache-control headers"""

urlargs = dict(
uuid=u'valid:id-üni',
version='v2',
region='200,200,200,200',
size='300,300',
rotation='!50',
quality='color',
image_format='pdf',
)

key = u'iiif:{0}/{1}/{2}/{3}/{4}.{5}'.format(
urlargs['uuid'],
urlargs['region'],
urlargs['size'],
urlargs['quality'],
urlargs['rotation'],
urlargs['image_format']
).encode('utf8')

cache = self.app.config['IIIF_CACHE_HANDLER'].cache

get_the_response = self.get(
'iiifimageapi',
urlargs=urlargs,
)

self.assertFalse('cache-control' in urlargs)

self.assert200(get_the_response)

self.assertTrue(cache.get(key))

cache.clear()

for cache_control, name in (('no-cache', "foo.pdf"),
('no-store', "foo.pdf")):

urlargs['cache-control'] = cache_control

get_the_response = self.get(
'iiifimageapi',
urlargs=urlargs,
)

self.assert200(get_the_response)

self.assertFalse(cache.get(key))

cache.clear()

for cache_control, name in (('public', "foo.pdf"),
('no-transform', "foo.pdf")):

urlargs['cache-control'] = cache_control

get_the_response = self.get(
'iiifimageapi',
urlargs=urlargs,
)

self.assert200(get_the_response)

self.assertTrue(cache.get(key))

cache.clear()

0 comments on commit 8cd179c

Please sign in to comment.