tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

document-builder.sjs (3518B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2  * License, v. 2.0. If a copy of the MPL was not distributed with this
      3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { NetUtil } = ChromeUtils.importESModule(
      8   "resource://gre/modules/NetUtil.sys.mjs"
      9 );
     10 
     11 function loadHTMLFromFile(path) {
     12   // Load the HTML to return in the response from file.
     13   // Since it's relative to the cwd of the test runner, we start there and
     14   // append to get to the actual path of the file.
     15   const testHTMLFile =
     16     // eslint-disable-next-line mozilla/use-services
     17     Cc["@mozilla.org/file/directory_service;1"]
     18       .getService(Ci.nsIProperties)
     19       .get("CurWorkD", Ci.nsIFile);
     20   const dirs = path.split("/");
     21   for (let i = 0; i < dirs.length; i++) {
     22     testHTMLFile.append(dirs[i]);
     23   }
     24 
     25   const testHTMLFileStream = Cc[
     26     "@mozilla.org/network/file-input-stream;1"
     27   ].createInstance(Ci.nsIFileInputStream);
     28   testHTMLFileStream.init(testHTMLFile, -1, 0, 0);
     29   const testHTML = NetUtil.readInputStreamToString(
     30     testHTMLFileStream,
     31     testHTMLFileStream.available()
     32   );
     33 
     34   return testHTML;
     35 }
     36 
     37 /**
     38  * document-builder.sjs can be used to dynamically build documents that will be used in
     39  * mochitests. It does handle the following GET parameters:
     40  * - file: The path to an (X)HTML file whose content will be used as a response.
     41  *        Example: document-builder.sjs?file=/tests/dom/security/test/csp/file_web_manifest_mixed_content.html
     42  * - html: A string representation of the HTML document you want to get.
     43  *        Example: document-builder.sjs?html=<h1>Hello</h1>
     44  * - headers: A <key:value> string representation of headers that will be set on the response
     45  *            This is only applied when the html GET parameter is passed as well
     46  *        Example: document-builder.sjs?headers=Cross-Origin-Opener-Policy:same-origin&html=<h1>Hello</h1>
     47  *                 document-builder.sjs?headers=X-Header1:a&headers=X-Header2:b&html=<h1>Multiple headers</h1>
     48  * - delay: Delay the response by X millisecond.
     49  */
     50 async function handleRequest(request, response) {
     51   response.processAsync();
     52 
     53   const queryString = new URLSearchParams(request.queryString);
     54   const html = queryString.get("html");
     55   const delay = queryString.get("delay");
     56 
     57   if (delay) {
     58     await new Promise(resolve => {
     59       let timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
     60       timer.initWithCallback(
     61         () => {
     62           // to avoid garbage collection
     63           timer = null;
     64           resolve();
     65         },
     66         delay,
     67         Ci.nsITimer.TYPE_ONE_SHOT
     68       );
     69     });
     70   }
     71 
     72   function setHeaders() {
     73     if (queryString.has("headers")) {
     74       for (const header of queryString.getAll("headers")) {
     75         const [key, value] = header.split(":");
     76         response.setHeader(key, value, false);
     77       }
     78     }
     79   }
     80 
     81   response.setHeader("Cache-Control", "no-cache", false);
     82   if (html) {
     83     response.setHeader("Content-Type", "text/html", false);
     84     setHeaders();
     85     response.write(html);
     86   } else {
     87     const path = queryString.get("file");
     88     const doc = loadHTMLFromFile(path);
     89     response.setHeader(
     90       "Content-Type",
     91       path.endsWith(".xhtml") ? "application/xhtml+xml" : "text/html",
     92       false
     93     );
     94     setHeaders();
     95     // This is a hack to set the correct id for the content document that is to be
     96     // loaded in the iframe.
     97     response.write(doc.replace(`id="body"`, `id="default-iframe-body-id"`));
     98   }
     99 
    100   response.finish();
    101 }