Skip to content

Commit

Permalink
if defined, use proxy_path for canonical link (issue #359) (#395)
Browse files Browse the repository at this point in the history
* if defined, use proxy_path for canonical link (issue #359)

* add test for canonical link when proxy_path isn't set
  • Loading branch information
bcail authored Jan 30, 2018
1 parent ac1cb9b commit dff373b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions loris/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,13 @@ def _get_info(self,ident,request,base_uri):

return (info,last_mod)

def _set_canonical_link(self, request, image_request, response):
if self.proxy_path:
root = self.proxy_path
else:
root = request.url_root
canonical_uri = '%s%s' % (root, image_request.canonical_request_path)
response.headers['Link'] = '%s,<%s>;rel="canonical"' % (response.headers['Link'], canonical_uri,)

def get_img(self, request, ident, region, size, rotation, quality, target_fmt, base_uri):
'''Get an Image.
Expand Down Expand Up @@ -612,8 +619,7 @@ def get_img(self, request, ident, region, size, rotation, quality, target_fmt, b
image_request.info = info
# we need to do the above to set the canonical link header

canonical_uri = '%s%s' % (request.url_root, image_request.canonical_request_path)
r.headers['Link'] = '%s,<%s>;rel="canonical"' % (r.headers['Link'], canonical_uri,)
self._set_canonical_link(request, image_request, r)
return r
else:
try:
Expand Down Expand Up @@ -669,8 +675,7 @@ def get_img(self, request, ident, region, size, rotation, quality, target_fmt, b
r.status_code = 200
r.last_modified = datetime.utcfromtimestamp(path.getctime(fp))
r.headers['Content-Length'] = path.getsize(fp)
canonical_uri = '%s%s' % (request.url_root, image_request.canonical_request_path)
r.headers['Link'] = '%s,<%s>;rel="canonical"' % (r.headers['Link'], canonical_uri,)
self._set_canonical_link(request, image_request, r)
r.response = file(fp)

if not self.enable_caching:
Expand Down
15 changes: 15 additions & 0 deletions tests/webapp_t.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,21 @@ def test_image_no_redirect_to_canonical(self):
resp = self.client.get(to_get, follow_redirects=False)
self.assertEqual(resp.status_code, 200)

def test_image_proxy_path_canonical_link(self):
self.app.proxy_path = 'https://proxy_example.org/image/'
to_get = '/%s/full/full/0/default.jpg' % (self.test_jp2_color_id,)
resp = self.client.get(to_get, follow_redirects=False)
self.assertEqual(resp.status_code, 200)
link = '<http://iiif.io/api/image/2/level2.json>;rel="profile",<https://proxy_example.org/image/01%2F02%2F0001.jp2/full/full/0/default.jpg>;rel="canonical"'
self.assertEqual(resp.headers['Link'], link)

def test_image_canonical_link(self):
to_get = '/%s/full/full/0/default.jpg' % (self.test_jp2_color_id,)
resp = self.client.get(to_get, follow_redirects=False)
self.assertEqual(resp.status_code, 200)
link = '<http://iiif.io/api/image/2/level2.json>;rel="profile",<http://localhost/01%2F02%2F0001.jp2/full/full/0/default.jpg>;rel="canonical"'
self.assertEqual(resp.headers['Link'], link)

def test_img_sends_304(self):
to_get = '/%s/full/full/0/default.jpg' % (self.test_jp2_color_id,)

Expand Down

0 comments on commit dff373b

Please sign in to comment.