Skip to content

Always follow link header if content-type is not json. #141

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

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 14 additions & 12 deletions lib/pyld/documentloader/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
.. moduleauthor:: Olaf Conradi <olaf@conradi.org>
"""
import string
import re
import urllib.parse as urllib_parse

from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL)
from pyld.jsonld import (JsonLdError, parse_link_header, LINK_HEADER_REL, prepend_base)


def requests_document_loader(secure=False, **kwargs):
Expand Down Expand Up @@ -69,30 +70,31 @@ def loader(url, options={}):
'contentType': content_type,
'contextUrl': None,
'documentUrl': response.url,
'document': response.json()
}
link_header = response.headers.get('link')
if link_header:
linked_context = parse_link_header(link_header).get(
LINK_HEADER_REL)
# only 1 related link header permitted
if linked_context and content_type != 'application/ld+json':
if isinstance(linked_context, list):
raise JsonLdError(
'URL could not be dereferenced, '
'it has more than one '
'associated HTTP Link Header.',
'jsonld.LoadDocumentError',
{'url': url},
code='multiple context link headers')
doc['contextUrl'] = linked_context['target']
if isinstance(linked_context, list):
raise JsonLdError(
"URL could not be dereferenced, "
"it has more than one "
"associated HTTP Link Header.",
"jsonld.LoadDocumentError",
{"url": url},
code="multiple context link headers")
doc["contextUrl"] = linked_context["target"]
linked_alternate = parse_link_header(link_header).get('alternate')
# if not JSON-LD, alternate may point there
if (linked_alternate and
linked_alternate.get('type') == 'application/ld+json' and
not re.match(r'^application\/(\w*\+)?json$', content_type)):
doc['contentType'] = 'application/ld+json'
doc['documentUrl'] = jsonld.prepend_base(url, linked_alternate['target'])
doc['documentUrl'] = prepend_base(url, linked_alternate['target'])
return loader(doc['documentUrl'], options=options)
doc["document"] = response.json()
return doc
except JsonLdError as e:
raise e
Expand Down