|
1 // SJS file for CSP mochitests |
|
2 |
|
3 Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
|
4 |
|
5 function loadHTMLFromFile(path) { |
|
6 // Load the HTML to return in the response from file. |
|
7 // Since it's relative to the cwd of the test runner, we start there and |
|
8 // append to get to the actual path of the file. |
|
9 var testHTMLFile = |
|
10 Components.classes["@mozilla.org/file/directory_service;1"]. |
|
11 getService(Components.interfaces.nsIProperties). |
|
12 get("CurWorkD", Components.interfaces.nsILocalFile); |
|
13 var dirs = path.split("/"); |
|
14 for (var i = 0; i < dirs.length; i++) { |
|
15 testHTMLFile.append(dirs[i]); |
|
16 } |
|
17 var testHTMLFileStream = |
|
18 Components.classes["@mozilla.org/network/file-input-stream;1"]. |
|
19 createInstance(Components.interfaces.nsIFileInputStream); |
|
20 testHTMLFileStream.init(testHTMLFile, -1, 0, 0); |
|
21 var testHTML = NetUtil.readInputStreamToString(testHTMLFileStream, testHTMLFileStream.available()); |
|
22 return testHTML; |
|
23 } |
|
24 |
|
25 function handleRequest(request, response) |
|
26 { |
|
27 var query = {}; |
|
28 request.queryString.split('&').forEach(function (val) { |
|
29 var [name, value] = val.split('='); |
|
30 query[name] = unescape(value); |
|
31 }); |
|
32 |
|
33 var csp = unescape(query['csp']); |
|
34 var file = unescape(query['file']); |
|
35 |
|
36 // avoid confusing cache behaviors |
|
37 response.setHeader("Cache-Control", "no-cache", false); |
|
38 |
|
39 // Deliver the CSP policy encoded in the URI |
|
40 response.setHeader("Content-Security-Policy", csp, false); |
|
41 |
|
42 // Send HTML to test allowed/blocked behaviors |
|
43 response.setHeader("Content-Type", "text/html", false); |
|
44 response.write(loadHTMLFromFile(file)); |
|
45 } |