|
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_KEY = "bug836922_ro_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 if ('results' in query) { |
|
19 // if asked for the received data, send it. |
|
20 response.setHeader("Content-Type", "text/javascript", false); |
|
21 if (getState(STATE_KEY)) { |
|
22 response.write(getState(STATE_KEY)); |
|
23 } else { |
|
24 // no state has been recorded. |
|
25 response.write(JSON.stringify({})); |
|
26 } |
|
27 } else if ('reset' in query) { |
|
28 //clear state |
|
29 setState(STATE_KEY, JSON.stringify(null)); |
|
30 } else { |
|
31 // ... otherwise, just respond "ok". |
|
32 response.write("null"); |
|
33 |
|
34 var bodystream = new BinaryInputStream(request.bodyInputStream); |
|
35 var avail; |
|
36 var bytes = []; |
|
37 while ((avail = bodystream.available()) > 0) |
|
38 Array.prototype.push.apply(bytes, bodystream.readByteArray(avail)); |
|
39 |
|
40 var data = String.fromCharCode.apply(null, bytes); |
|
41 |
|
42 // figure out which test was violating a policy |
|
43 var testpat = new RegExp("testid=([a-z0-9_]+)"); |
|
44 var testid = testpat.exec(data)[1]; |
|
45 |
|
46 // store the violation in the persistent state |
|
47 var s = JSON.parse(getState(STATE_KEY) || "{}"); |
|
48 s[testid] ? s[testid]++ : s[testid] = 1; |
|
49 setState(STATE_KEY, JSON.stringify(s)); |
|
50 } |
|
51 } |
|
52 |
|
53 |