file_bug836922_npolicies_ro_violation.sjs (1609B)
1 // SJS file that receives violation reports and then responds with nothing. 2 3 const CC = Components.Constructor; 4 const BinaryInputStream = CC( 5 "@mozilla.org/binaryinputstream;1", 6 "nsIBinaryInputStream", 7 "setInputStream" 8 ); 9 10 const STATE_KEY = "bug836922_ro_violations"; 11 12 function handleRequest(request, response) { 13 var query = {}; 14 request.queryString.split("&").forEach(function (val) { 15 var [name, value] = val.split("="); 16 query[name] = unescape(value); 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_KEY)) { 23 response.write(getState(STATE_KEY)); 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_KEY, 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 42 var data = String.fromCharCode.apply(null, bytes); 43 44 // figure out which test was violating a policy 45 var testpat = new RegExp("testid=([a-z0-9_]+)"); 46 var testid = testpat.exec(data)[1]; 47 48 // store the violation in the persistent state 49 var s = JSON.parse(getState(STATE_KEY) || "{}"); 50 s[testid] ? s[testid]++ : (s[testid] = 1); 51 setState(STATE_KEY, JSON.stringify(s)); 52 } 53 }