forked from IIIF/presentation-validator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiiif-presentation-validator.py
executable file
·242 lines (205 loc) · 8.67 KB
/
iiif-presentation-validator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
# encoding: utf-8
"""IIIF Presentation Validation Service."""
import argparse
import codecs
import json
import os
from gzip import GzipFile
from io import BytesIO
from jsonschema.exceptions import ValidationError, SchemaError
import traceback
try:
# python3
from urllib.request import urlopen, HTTPError, Request
from urllib.parse import urlparse
except ImportError:
# fall back to python2
from urllib2 import urlopen, HTTPError, Request
from urlparse import urlparse
from bottle import Bottle, request, response, run
from schema import schemavalidator
egg_cache = "/path/to/web/egg_cache"
os.environ['PYTHON_EGG_CACHE'] = egg_cache
from iiif_prezi.loader import ManifestReader
from pyld import jsonld
jsonld.set_document_loader(jsonld.requests_document_loader(timeout=60))
class Validator(object):
"""Validator class that runs with Bottle."""
def __init__(self):
"""Initialize Validator with default_version."""
self.default_version = "2.1"
def fetch(self, url):
"""Fetch manifest from url."""
req = Request(url)
req.add_header('User-Agent', 'IIIF Validation Service')
req.add_header('Accept-Encoding', 'gzip')
try:
wh = urlopen(req)
except HTTPError as wh:
raise wh
data = wh.read()
wh.close()
if wh.headers.get('Content-Encoding') == 'gzip':
with GzipFile(fileobj=BytesIO(data)) as f:
data = f.read()
try:
data = data.decode('utf-8')
except:
raise
return(data, wh)
def check_manifest(self, data, version, url=None, warnings=[]):
"""Check manifest data at version, return JSON."""
infojson = {}
# Check if 3.0 if so run through schema rather than this version...
if version == '3.0':
try:
infojson = schemavalidator.validate(data, version, url)
for error in infojson['errorList']:
error.pop('error', None)
mf = json.loads(data)
if url and 'id' in mf and mf['id'] != url:
raise ValidationError("The manifest id ({}) should be the same as the URL it is published at ({}).".format(mf["id"], url))
except ValidationError as e:
if infojson:
infojson['errorList'].append({
'title': 'Resolve Error',
'detail': str(e),
'description': '',
'path': '/id',
'context': '{ \'id\': \'...\'}'
})
else:
infojson = {
'received': data,
'okay': 0,
'error': str(e),
'url': url,
'warnings': []
}
except Exception as e:
traceback.print_exc()
infojson = {
'received': data,
'okay': 0,
'error': 'Presentation Validator bug: "{}". Please create a <a href="https://github.com/IIIF/presentation-validator/issues">Validator Issue</a>, including a link to the manifest.'.format(e),
'url': url,
'warnings': []
}
else:
reader = ManifestReader(data, version=version)
err = None
try:
mf = reader.read()
mf.toJSON()
if url and mf.id != url:
raise ValidationError("Manifest @id ({}) is different to the location where it was retrieved ({})".format(mf.id, url))
# Passed!
okay = 1
except Exception as e:
# Failed
print (e)
err = e
okay = 0
warnings.extend(reader.get_warnings())
infojson = {
'received': data,
'okay': okay,
'warnings': warnings,
'error': str(err),
'url': url
}
return self.return_json(infojson)
def return_json(self, js):
"""Set header and return JSON response."""
response.content_type = "application/json"
return json.dumps(js)
def do_POST_test(self):
"""Implement POST request to test posted data at default version."""
data = request.json
if not data:
b = request._get_body_string()
try:
b = b.decode('utf-8')
except:
pass
data = json.loads(b)
return self.check_manifest(data, self.default_version)
def do_GET_test(self):
"""Implement GET request to test url at version."""
url = request.query.get('url', '')
version = request.query.get('version', self.default_version)
url = url.strip()
parsed_url = urlparse(url)
if (parsed_url.scheme != 'http' and parsed_url.scheme != 'https'):
return self.return_json({'okay': 0, 'error': 'URLs must use HTTP or HTTPS', 'url': url})
try:
(data, webhandle) = self.fetch(url)
except Exception as error:
return self.return_json({'okay': 0, 'error': 'Cannot fetch url. Got "{}"'.format(error), 'url': url})
# First check HTTP level
ct = webhandle.headers.get('content-type', '')
cors = webhandle.headers.get('access-control-allow-origin', '')
warnings = []
if not ct.startswith('application/json') and not ct.startswith('application/ld+json'):
# not json
warnings.append("URL does not have correct content-type header: got \"%s\", expected JSON" % ct)
if cors != "*":
warnings.append("URL does not have correct access-control-allow-origin header:"
" got \"%s\", expected *" % cors)
content_encoding = webhandle.headers.get('Content-Encoding', '')
if content_encoding != 'gzip':
warnings.append('The remote server did not use the requested gzip'
' transfer compression, which will slow access.'
' (Content-Encoding: %s)' % content_encoding)
elif 'Accept-Encoding' not in webhandle.headers.get('Vary', ''):
warnings.append('gzip transfer compression is enabled but the Vary'
' header does not include Accept-Encoding, which'
' can cause compatibility issues')
return self.check_manifest(data, version, url, warnings)
def index_route(self):
"""Read and return index page."""
with codecs.open(os.path.join(os.path.dirname(__file__), 'index.html'), 'r', 'utf-8') as fh:
data = fh.read()
return data
def dispatch_views(self):
"""Set up path mappings."""
self.app.route("/", "GET", self.index_route)
self.app.route("/validate", "OPTIONS", self.empty_response)
self.app.route("/validate", "GET", self.do_GET_test)
self.app.route("/validate", "POST", self.do_POST_test)
def after_request(self):
"""Used with after_request hook to set response headers."""
methods = 'GET,POST,OPTIONS'
headers = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = methods
response.headers['Access-Control-Allow-Headers'] = headers
response.headers['Allow'] = methods
def empty_response(self, *args, **kwargs):
"""Empty response."""
def get_bottle_app(self):
"""Return bottle instance."""
self.app = Bottle()
self.dispatch_views()
self.app.hook('after_request')(self.after_request)
return self.app
def apache():
"""Run as WSGI application."""
v = Validator()
return v.get_bottle_app()
def main():
"""Parse argument and run server when run from command line."""
parser = argparse.ArgumentParser(description=__doc__.strip(),
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--hostname', default='localhost',
help='Hostname or IP address to bind to (use 0.0.0.0 for all)')
parser.add_argument('--port', default=8080, type=int,
help='Server port to bind to. Values below 1024 require root privileges.')
args = parser.parse_args()
v = Validator()
run(host=args.hostname, port=args.port, app=v.get_bottle_app())
if __name__ == "__main__":
main()
else:
application = apache()