tor-browser

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

early_hint_csp_options_html.sjs (2976B)


      1 "use strict";
      2 
      3 function handleRequest(request, response) {
      4   let qs = new URLSearchParams(request.queryString);
      5   let asset = qs.get("as");
      6   let hinted = qs.get("hinted") !== "0";
      7   let httpCode = qs.get("code");
      8   let csp = qs.get("csp");
      9   let csp_in_early_hint = qs.get("csp_in_early_hint");
     10   let host = qs.get("host");
     11 
     12   // eslint-disable-next-line mozilla/use-services
     13   let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"].getService(
     14     Ci.nsIUUIDGenerator
     15   );
     16   let uuid = uuidGenerator.generateUUID().toString();
     17   let url = `early_hint_pixel.sjs?as=${asset}&uuid=${uuid}`;
     18   if (host) {
     19     url = host + url;
     20   }
     21 
     22   // write to raw socket
     23   response.seizePower();
     24 
     25   if (hinted) {
     26     response.write("HTTP/1.1 103 Early Hint\r\n");
     27     if (csp_in_early_hint) {
     28       response.write(
     29         `Content-Security-Policy: ${csp_in_early_hint.replaceAll('"', "")}\r\n`
     30       );
     31     }
     32     response.write(`Link: <${url}>; rel=preload; as=${asset}\r\n`);
     33     response.write("\r\n");
     34   }
     35 
     36   let body = "";
     37   if (asset === "image") {
     38     body = `<!DOCTYPE html>
     39       <html>
     40       <body>
     41       <img id="test_image" src="${url}" width="100px">
     42       </body>
     43       </html>`;
     44   } else if (asset === "style") {
     45     body = `<!DOCTYPE html>
     46       <html>
     47       <head>
     48       <link rel="stylesheet" type="text/css" href="${url}">
     49       </head>
     50       <body>
     51       <h1>Test preload css<h1>
     52       <div id="square" style="width:100px;height:100px;">
     53       </body>
     54       </html>
     55     `;
     56   } else if (asset === "script") {
     57     body = `<!DOCTYPE html>
     58       <html>
     59       <head>
     60       <script src="${url}"></script>
     61       </head>
     62       <body>
     63       <h1>Test preload javascript<h1>
     64       <div id="square" style="width:100px;height:100px;">
     65       </body>
     66       </html>
     67     `;
     68   } else if (asset === "fetch") {
     69     body = `<!DOCTYPE html>
     70       <html>
     71       <body onload="onLoad()">
     72       <script>
     73       function onLoad() {
     74         fetch("${url}")
     75           .then(r => r.text())
     76           .then(r => document.getElementsByTagName("h2")[0].textContent = r);
     77       }
     78       </script>
     79       <h1>Test preload fetch</h1>
     80       <h2>Fetching...</h2>
     81       </body>
     82       </html>
     83     `;
     84   } else if (asset === "font") {
     85     body = `<!DOCTYPE html>
     86     <html>
     87     <head>
     88     <style>
     89     @font-face {
     90       font-family: "preloadFont";
     91       src: url("${url}") format("woff");
     92     }
     93     body {
     94       font-family: "preloadFont";
     95     }
     96     </style>
     97     </head>
     98     <body>
     99     <h1>Test preload font<h1>
    100     </body>
    101     </html>
    102   `;
    103   }
    104 
    105   if (!httpCode) {
    106     response.write(`HTTP/1.1 200 OK\r\n`);
    107   } else {
    108     response.write(`HTTP/1.1 ${httpCode} Error\r\n`);
    109   }
    110   response.write("Content-Type: text/html;charset=utf-8\r\n");
    111   response.write("Cache-Control: no-cache\r\n");
    112   response.write(`Content-Length: ${body.length}\r\n`);
    113   if (csp) {
    114     response.write(`Content-Security-Policy: ${csp.replaceAll('"', "")}\r\n`);
    115   }
    116   response.write("\r\n");
    117   response.write(body);
    118 
    119   response.finish();
    120 }