content/base/test/csp/file_bug836922_npolicies_violation.sjs

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:96509550735c
1 // SJS file that receives violation reports and then responds with nothing.
2
3 const CC = Components.Constructor;
4 const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
5 "nsIBinaryInputStream",
6 "setInputStream");
7
8 const STATE = "bug836922_violations";
9
10 function handleRequest(request, response)
11 {
12 var query = {};
13 request.queryString.split('&').forEach(function (val) {
14 var [name, value] = val.split('=');
15 query[name] = unescape(value);
16 });
17
18
19 if ('results' in query) {
20 // if asked for the received data, send it.
21 response.setHeader("Content-Type", "text/javascript", false);
22 if (getState(STATE)) {
23 response.write(getState(STATE));
24 } else {
25 // no state has been recorded.
26 response.write(JSON.stringify({}));
27 }
28 } else if ('reset' in query) {
29 //clear state
30 setState(STATE, JSON.stringify(null));
31 } else {
32 // ... otherwise, just respond "ok".
33 response.write("null");
34
35 var bodystream = new BinaryInputStream(request.bodyInputStream);
36 var avail;
37 var bytes = [];
38 while ((avail = bodystream.available()) > 0)
39 Array.prototype.push.apply(bytes, bodystream.readByteArray(avail));
40
41 var data = String.fromCharCode.apply(null, bytes);
42
43 // figure out which test was violating a policy
44 var testpat = new RegExp("testid=([a-z0-9_]+)");
45 var testid = testpat.exec(data)[1];
46
47 // store the violation in the persistent state
48 var s = getState(STATE);
49 if (!s) s = "{}";
50 s = JSON.parse(s);
51 if (!s) s = {};
52
53 if (!s[testid]) s[testid] = 0;
54 s[testid]++;
55 setState(STATE, JSON.stringify(s));
56 }
57 }
58
59

mercurial