tor-browser

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

early_hint_asset_html.sjs (3573B)


      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") === "1";
      7   let httpCode = qs.get("code");
      8   let uuid = qs.get("uuid");
      9   let cached = qs.get("cached") === "1";
     10 
     11   let url = `early_hint_asset.sjs?as=${asset}${uuid ? `&uuid=${uuid}` : ""}${
     12     cached ? "&cached=1" : ""
     13   }`;
     14 
     15   // write to raw socket
     16   response.seizePower();
     17   let link = "";
     18   if (hinted) {
     19     response.write("HTTP/1.1 103 Early Hint\r\n");
     20     if (asset === "fetch" || asset === "font") {
     21       // fetch and font has to specify the crossorigin attribute
     22       // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link#attr-as
     23       link = `Link: <${url}>; rel=preload; as=${asset}; crossorigin=anonymous\r\n`;
     24       response.write(link);
     25     } else if (asset === "module") {
     26       // module preloads are handled differently
     27       link = `Link: <${url}>; rel=modulepreload\r\n`;
     28       response.write(link);
     29     } else {
     30       link = `Link: <${url}>; rel=preload; as=${asset}\r\n`;
     31       response.write(link);
     32     }
     33     response.write("\r\n");
     34   }
     35 
     36   let body = "";
     37   if (asset === "image") {
     38     body = `<!DOCTYPE html>
     39       <html>
     40       <body>
     41       <img 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 === "module") {
     69     // this code assumes that the .sjs for the module is in the same directory
     70     var file_name = url.split("/");
     71     file_name = file_name[file_name.length - 1];
     72     body = `<!DOCTYPE html>
     73       <html>
     74       <head>
     75       </head>
     76       <body>
     77       <h1>Test preload module<h1>
     78       <div id="square" style="width:100px;height:100px;">
     79       <script type="module">
     80         import { draw } from "./${file_name}";
     81         draw();
     82       </script>
     83       </body>
     84       </html>
     85     `;
     86   } else if (asset === "fetch") {
     87     body = `<!DOCTYPE html>
     88       <html>
     89       <body onload="onLoad()">
     90       <script>
     91       function onLoad() {
     92         fetch("${url}")
     93           .then(r => r.text())
     94           .then(r => document.getElementsByTagName("h2")[0].textContent = r);
     95       }
     96       </script>
     97       <h1>Test preload fetch</h1>
     98       <h2>Fetching...</h2>
     99       </body>
    100       </html>
    101     `;
    102   } else if (asset === "font") {
    103     body = `<!DOCTYPE html>
    104       <html>
    105       <head>
    106       <style>
    107       @font-face {
    108         font-family: "preloadFont";
    109         src: url("${url}");
    110       }
    111       body {
    112         font-family: "preloadFont";
    113       }
    114       </style>
    115       </head>
    116       <body>
    117       <h1>Test preload font<h1>
    118       </body>
    119       </html>
    120     `;
    121   }
    122 
    123   if (!httpCode) {
    124     response.write(`HTTP/1.1 200 OK\r\n`);
    125   } else {
    126     response.write(`HTTP/1.1 ${httpCode} Error\r\n`);
    127   }
    128   response.write(link);
    129   response.write("Content-Type: text/html;charset=utf-8\r\n");
    130   response.write("Cache-Control: no-cache\r\n");
    131   response.write(`Content-Length: ${body.length}\r\n`);
    132   response.write("\r\n");
    133   response.write(body);
    134   response.finish();
    135 }