Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if defined, use proxy_path for canonical link (issue #359) #395

Merged
merged 2 commits into from
Jan 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A second test for the case when we don't have a proxy_path set would be good.

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