summaryrefslogtreecommitdiffstats
path: root/dom/security/test/csp/file_report_for_import_server.sjs
blob: 624c7e657b2c470366535345929c7d3b9dc0e7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Custom *.sjs file specifically for the needs of Bug:
// Bug 1048048 - CSP violation report not sent for @import

const CC = Components.Constructor;
const BinaryInputStream = CC(
  "@mozilla.org/binaryinputstream;1",
  "nsIBinaryInputStream",
  "setInputStream"
);

function handleRequest(request, response) {
  // avoid confusing cache behaviors
  response.setHeader("Cache-Control", "no-cache", false);
  response.setHeader("Content-Type", "text/html", false);
  var queryString = request.queryString;

  // (1) lets process the queryresult request async and
  // wait till we have received the image request.
  if (queryString === "queryresult") {
    response.processAsync();
    setObjectState("queryResult", response);
    return;
  }

  // (2) handle the csp-report and return the JSON back to
  // the testfile using the afore stored xml request in (1).
  if (queryString === "report") {
    getObjectState("queryResult", function (queryResponse) {
      if (!queryResponse) {
        return;
      }

      // send the report back to the XML request for verification
      var report = new BinaryInputStream(request.bodyInputStream);
      var avail;
      var bytes = [];
      while ((avail = report.available()) > 0) {
        Array.prototype.push.apply(bytes, report.readByteArray(avail));
      }
      var data = String.fromCharCode.apply(null, bytes);
      queryResponse.bodyOutputStream.write(data, data.length);
      queryResponse.finish();
    });
    return;
  }

  // we should not get here ever, but just in case return
  // something unexpected.
  response.write("doh!");
}