File: report.py

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (57 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (6)
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
import time
import json
import re

def retrieve_from_stash(request, key, timeout, min_count, default_value):
  t0 = time.time()
  while time.time() - t0 < timeout:
    time.sleep(0.5)
    value = request.server.stash.take(key=key)
    if value is not None and len(value) >= min_count:
      request.server.stash.put(key=key, value=value)
      return json.dumps(value)

  return default_value

def main(request, response):
  # Handle CORS preflight requests
  if request.method == 'OPTIONS':
    # Always reject preflights for one subdomain
    if "www2" in request.headers["Origin"]:
      return (400, [], "CORS preflight rejected for www2")
    return [
      ("Content-Type", "text/plain"),
      ("Access-Control-Allow-Origin", "*"),
      ("Access-Control-Allow-Methods", "post"),
      ("Access-Control-Allow-Headers", "Content-Type"),
    ], "CORS allowed"

  op = request.GET.first("op");
  key = request.GET.first("reportID")

  if op == "retrieve_report":
    try:
      timeout = float(request.GET.first("timeout"))
    except:
      timeout = 0.5
    try:
      min_count = int(request.GET.first("min_count"))
    except:
      min_count = 1
    return [("Content-Type", "application/json")], retrieve_from_stash(request, key, timeout, min_count, '[]')

  # append new reports
  new_reports = json.loads(request.body)
  for report in new_reports:
    report["metadata"] = {
      "content_type": request.headers["Content-Type"],
    }
  with request.server.stash.lock:
    reports = request.server.stash.take(key=key)
    if reports is None:
      reports = []
    reports.extend(new_reports)
    request.server.stash.put(key=key, value=reports)

  # return acknowledgement report
  return [("Content-Type", "text/plain")], "Recorded report"