content/base/test/csp/file_bug836922_npolicies_violation.sjs

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/csp/file_bug836922_npolicies_violation.sjs	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,59 @@
     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 = "bug836922_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 +
    1.22 +  if ('results' in query) {
    1.23 +    // if asked for the received data, send it.
    1.24 +    response.setHeader("Content-Type", "text/javascript", false);
    1.25 +    if (getState(STATE)) {
    1.26 +      response.write(getState(STATE));
    1.27 +    } else {
    1.28 +      // no state has been recorded.
    1.29 +      response.write(JSON.stringify({}));
    1.30 +    }
    1.31 +  } else if ('reset' in query) {
    1.32 +    //clear state
    1.33 +    setState(STATE, JSON.stringify(null));
    1.34 +  } else {
    1.35 +    // ... otherwise, just respond "ok".
    1.36 +    response.write("null");
    1.37 +
    1.38 +    var bodystream = new BinaryInputStream(request.bodyInputStream);
    1.39 +    var avail;
    1.40 +    var bytes = [];
    1.41 +    while ((avail = bodystream.available()) > 0)
    1.42 +      Array.prototype.push.apply(bytes, bodystream.readByteArray(avail));
    1.43 +
    1.44 +    var data = String.fromCharCode.apply(null, bytes);
    1.45 +
    1.46 +    // figure out which test was violating a policy
    1.47 +    var testpat = new RegExp("testid=([a-z0-9_]+)");
    1.48 +    var testid = testpat.exec(data)[1];
    1.49 +
    1.50 +    // store the violation in the persistent state
    1.51 +    var s = getState(STATE);
    1.52 +    if (!s) s = "{}";
    1.53 +    s = JSON.parse(s);
    1.54 +    if (!s) s = {};
    1.55 +
    1.56 +    if (!s[testid]) s[testid] = 0;
    1.57 +    s[testid]++;
    1.58 +    setState(STATE, JSON.stringify(s));
    1.59 +  }
    1.60 +}
    1.61 +
    1.62 +

mercurial