tor-browser

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

early_hint_referrer_policy_html.sjs (3473B)


      1 "use strict";
      2 
      3 function handleRequest(request, response) {
      4   let qs = new URLSearchParams(request.queryString);
      5   let asset = qs.get("as");
      6   var action = qs.get("action");
      7   let hinted = qs.get("hinted") !== "0";
      8   let httpCode = qs.get("code");
      9   let header_referrer_policy = qs.get("header_referrer_policy");
     10   let link_referrer_policy = qs.get("link_referrer_policy");
     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 
     19   if (action === "get_request_referrer_results") {
     20     response.setHeader("Cache-Control", "no-cache", false);
     21     response.setHeader("Content-Type", "text/plain", false);
     22     response.write(getSharedState("requestReferrer"));
     23     return;
     24   } else if (action === "reset_referrer_results") {
     25     response.setHeader("Cache-Control", "no-cache", false);
     26     response.setHeader("Content-Type", "text/plain", false);
     27     response.write(setSharedState("requestReferrer", "not set"));
     28     return;
     29   }
     30 
     31   // write to raw socket
     32   response.seizePower();
     33 
     34   if (hinted) {
     35     response.write("HTTP/1.1 103 Early Hint\r\n");
     36 
     37     if (header_referrer_policy) {
     38       response.write(
     39         `Referrer-Policy: ${header_referrer_policy.replaceAll('"', "")}\r\n`
     40       );
     41     }
     42 
     43     response.write(
     44       `Link: <${url}>; rel=preload; as=${asset}; ${
     45         link_referrer_policy ? "referrerpolicy=" + link_referrer_policy : ""
     46       } \r\n`
     47     );
     48     response.write("\r\n");
     49   }
     50 
     51   let body = "";
     52   if (asset === "image") {
     53     body = `<!DOCTYPE html>
     54       <html>
     55       <body>
     56       <img src="${url}" width="100px">
     57       </body>
     58       </html>`;
     59   } else if (asset === "style") {
     60     body = `<!DOCTYPE html>
     61       <html>
     62       <head>
     63       <link rel="stylesheet" type="text/css" href="${url}">
     64       </head>
     65       <body>
     66       <h1>Test preload css<h1>
     67       <div id="square" style="width:100px;height:100px;">
     68       </body>
     69       </html>
     70     `;
     71   } else if (asset === "script") {
     72     body = `<!DOCTYPE html>
     73       <html>
     74       <head>
     75       <script src="${url}"></script>
     76       </head>
     77       <body>
     78       <h1>Test preload javascript<h1>
     79       <div id="square" style="width:100px;height:100px;">
     80       </body>
     81       </html>
     82     `;
     83   } else if (asset === "fetch") {
     84     body = `<!DOCTYPE html>
     85       <html>
     86       <body onload="onLoad()">
     87       <script>
     88       function onLoad() {
     89         fetch("${url}")
     90           .then(r => r.text())
     91           .then(r => document.getElementsByTagName("h2")[0].textContent = r);
     92       }
     93       </script>
     94       <h1>Test preload fetch</h1>
     95       <h2>Fetching...</h2>
     96       </body>
     97       </html>
     98     `;
     99   } else if (asset === "font") {
    100     body = `<!DOCTYPE html>
    101     <html>
    102     <head>
    103     <style>
    104     @font-face {
    105       font-family: "preloadFont";
    106       src: url("${url}") format("woff");
    107     }
    108     body {
    109       font-family: "preloadFont";
    110     }
    111     </style>
    112     </head>
    113     <body>
    114     <h1>Test preload font<h1>
    115     </body>
    116     </html>
    117   `;
    118   }
    119 
    120   if (!httpCode) {
    121     response.write(`HTTP/1.1 200 OK\r\n`);
    122   } else {
    123     response.write(`HTTP/1.1 ${httpCode} Error\r\n`);
    124   }
    125   response.write("Content-Type: text/html;charset=utf-8\r\n");
    126   response.write("Cache-Control: no-cache\r\n");
    127   response.write(`Content-Length: ${body.length}\r\n`);
    128   response.write("\r\n");
    129   response.write(body);
    130 
    131   response.finish();
    132 }