1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/csp/file_bug836922_npolicies_ro_violation.sjs Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,53 @@ 1.4 +// SJS file that receives violation reports and then responds with nothing. 1.5 + 1.6 +const CC = Components.Constructor; 1.7 +const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", 1.8 + "nsIBinaryInputStream", 1.9 + "setInputStream"); 1.10 + 1.11 +const STATE_KEY = "bug836922_ro_violations"; 1.12 + 1.13 +function handleRequest(request, response) 1.14 +{ 1.15 + var query = {}; 1.16 + request.queryString.split('&').forEach(function (val) { 1.17 + var [name, value] = val.split('='); 1.18 + query[name] = unescape(value); 1.19 + }); 1.20 + 1.21 + if ('results' in query) { 1.22 + // if asked for the received data, send it. 1.23 + response.setHeader("Content-Type", "text/javascript", false); 1.24 + if (getState(STATE_KEY)) { 1.25 + response.write(getState(STATE_KEY)); 1.26 + } else { 1.27 + // no state has been recorded. 1.28 + response.write(JSON.stringify({})); 1.29 + } 1.30 + } else if ('reset' in query) { 1.31 + //clear state 1.32 + setState(STATE_KEY, JSON.stringify(null)); 1.33 + } else { 1.34 + // ... otherwise, just respond "ok". 1.35 + response.write("null"); 1.36 + 1.37 + var bodystream = new BinaryInputStream(request.bodyInputStream); 1.38 + var avail; 1.39 + var bytes = []; 1.40 + while ((avail = bodystream.available()) > 0) 1.41 + Array.prototype.push.apply(bytes, bodystream.readByteArray(avail)); 1.42 + 1.43 + var data = String.fromCharCode.apply(null, bytes); 1.44 + 1.45 + // figure out which test was violating a policy 1.46 + var testpat = new RegExp("testid=([a-z0-9_]+)"); 1.47 + var testid = testpat.exec(data)[1]; 1.48 + 1.49 + // store the violation in the persistent state 1.50 + var s = JSON.parse(getState(STATE_KEY) || "{}"); 1.51 + s[testid] ? s[testid]++ : s[testid] = 1; 1.52 + setState(STATE_KEY, JSON.stringify(s)); 1.53 + } 1.54 +} 1.55 + 1.56 +