2013-09-30 21:49:07 +04:00
|
|
|
// SJS file for CSP mochitests
|
2016-04-08 00:13:09 +03:00
|
|
|
"use strict";
|
2013-09-30 21:49:07 +04:00
|
|
|
Components.utils.import("resource://gre/modules/NetUtil.jsm");
|
2016-04-08 00:13:09 +03:00
|
|
|
Components.utils.importGlobalProperties(["URLSearchParams"]);
|
2013-09-30 21:49:07 +04:00
|
|
|
|
|
|
|
function loadHTMLFromFile(path) {
|
|
|
|
// Load the HTML to return in the response from file.
|
|
|
|
// Since it's relative to the cwd of the test runner, we start there and
|
|
|
|
// append to get to the actual path of the file.
|
2016-04-08 00:13:09 +03:00
|
|
|
const testHTMLFile =
|
2013-09-30 21:49:07 +04:00
|
|
|
Components.classes["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Components.interfaces.nsIProperties).
|
|
|
|
get("CurWorkD", Components.interfaces.nsILocalFile);
|
2016-04-08 00:13:09 +03:00
|
|
|
|
|
|
|
const testHTMLFileStream =
|
2013-09-30 21:49:07 +04:00
|
|
|
Components.classes["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Components.interfaces.nsIFileInputStream);
|
2016-04-08 00:13:09 +03:00
|
|
|
|
|
|
|
path
|
|
|
|
.split("/")
|
|
|
|
.filter(path => path)
|
|
|
|
.reduce((file, path) => {
|
|
|
|
testHTMLFile.append(path)
|
|
|
|
return testHTMLFile;
|
|
|
|
}, testHTMLFile);
|
2013-09-30 21:49:07 +04:00
|
|
|
testHTMLFileStream.init(testHTMLFile, -1, 0, 0);
|
2016-04-08 00:13:09 +03:00
|
|
|
const isAvailable = testHTMLFileStream.available();
|
|
|
|
return NetUtil.readInputStreamToString(testHTMLFileStream, isAvailable);
|
2013-09-30 21:49:07 +04:00
|
|
|
}
|
|
|
|
|
2016-04-08 00:13:09 +03:00
|
|
|
function handleRequest(request, response) {
|
|
|
|
const query = new URLSearchParams(request.queryString);
|
2015-02-06 00:48:18 +03:00
|
|
|
|
2013-09-30 21:49:07 +04:00
|
|
|
// avoid confusing cache behaviors
|
|
|
|
response.setHeader("Cache-Control", "no-cache", false);
|
|
|
|
|
2016-04-08 00:13:09 +03:00
|
|
|
// Deliver the CSP policy encoded in the URL
|
|
|
|
if(query.has("csp")){
|
|
|
|
response.setHeader("Content-Security-Policy", query.get("csp"), false);
|
2015-06-02 22:42:19 +03:00
|
|
|
}
|
2016-04-08 00:13:09 +03:00
|
|
|
|
2016-06-29 00:03:45 +03:00
|
|
|
// Deliver the CSP report-only policy encoded in the URI
|
|
|
|
if(query.has("cspRO")){
|
|
|
|
response.setHeader("Content-Security-Policy-Report-Only", query.get("cspRO"), false);
|
|
|
|
}
|
|
|
|
|
2016-04-08 00:13:09 +03:00
|
|
|
// Deliver the CORS header in the URL
|
|
|
|
if(query.has("cors")){
|
|
|
|
response.setHeader("Access-Control-Allow-Origin", query.get("cors"), false);
|
2015-06-02 22:42:19 +03:00
|
|
|
}
|
2016-04-08 00:13:09 +03:00
|
|
|
|
2015-02-06 00:48:18 +03:00
|
|
|
// Send HTML to test allowed/blocked behaviors
|
|
|
|
response.setHeader("Content-Type", "text/html", false);
|
2016-06-29 00:03:45 +03:00
|
|
|
if(query.has("file")){
|
|
|
|
response.write(loadHTMLFromFile(query.get("file")));
|
|
|
|
}
|
2013-09-30 21:49:07 +04:00
|
|
|
}
|