report.py (1381B)
1 import json 2 3 def main(request, response): 4 response.headers.set(b'Access-Control-Allow-Origin', b'*') 5 response.headers.set(b'Access-Control-Allow-Methods', b'OPTIONS, GET, POST') 6 response.headers.set(b'Access-Control-Allow-Headers', b'Content-Type') 7 response.headers.set(b'Cache-Control', b'no-cache, no-store, must-revalidate') 8 9 # CORS preflight 10 if request.method == u'OPTIONS': 11 return u'' 12 13 uuidMap = { 14 b'endpoint': b'01234567-0123-0123-0123-0123456789AB', 15 b'report-only-endpoint': b'01234567-0123-0123-0123-0123456789CD' 16 } 17 key = 0 18 if b'endpoint' in request.GET: 19 key = uuidMap.get(request.GET[b'endpoint'], 0) 20 21 if b'key' in request.GET: 22 key = request.GET[b'key'] 23 24 if key == 0: 25 response.status = 400 26 return u'invalid endpoint' 27 28 path = u'/'.join(request.url_parts.path.split(u'/')[:-1]) + u'/' 29 if request.method == u'POST': 30 reports = request.server.stash.take(key, path) or [] 31 for report in json.loads(request.body): 32 reports.append(report) 33 request.server.stash.put(key, reports, path) 34 return u'done' 35 36 if request.method == u'GET': 37 response.headers.set(b'Content-Type', b'application/json') 38 return json.dumps(request.server.stash.take(key, path) or []) 39 40 response.status = 400 41 return u'invalid method'