report.py (1456B)
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 response.headers.set(b'Cross-Origin-Resource-Policy', b'cross-origin') 9 10 # CORS preflight 11 if request.method == u'OPTIONS': 12 return u'' 13 14 uuidMap = { 15 b'endpoint': b'01234567-0123-0123-0123-0123456789AB', 16 b'report-only-endpoint': b'01234567-0123-0123-0123-0123456789CD' 17 } 18 key = 0 19 if b'endpoint' in request.GET: 20 key = uuidMap.get(request.GET[b'endpoint'], 0) 21 22 if b'key' in request.GET: 23 key = request.GET[b'key'] 24 25 if key == 0: 26 response.status = 400 27 return u'invalid endpoint' 28 29 path = u'/'.join(request.url_parts.path.split(u'/')[:-1]) + u'/' 30 if request.method == u'POST': 31 reports = request.server.stash.take(key, path) or [] 32 for report in json.loads(request.body): 33 reports.append(report) 34 request.server.stash.put(key, reports, path) 35 return u'done' 36 37 if request.method == u'GET': 38 response.headers.set(b'Content-Type', b'application/json') 39 return json.dumps(request.server.stash.take(key, path) or []) 40 41 response.status = 400 42 return u'invalid method'