file_bug836922_npolicies_violation.sjs (1675B)
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 = "bug836922_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)) { 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 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 = getState(STATE); 50 if (!s) { 51 s = "{}"; 52 } 53 s = JSON.parse(s); 54 if (!s) { 55 s = {}; 56 } 57 58 if (!s[testid]) { 59 s[testid] = 0; 60 } 61 s[testid]++; 62 setState(STATE, JSON.stringify(s)); 63 } 64 }