tor-browser

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

server_fetch_synthetic.sjs (1555B)


      1 const CC = Components.Constructor;
      2 const BinaryInputStream = CC(
      3   "@mozilla.org/binaryinputstream;1",
      4   "nsIBinaryInputStream",
      5   "setInputStream"
      6 );
      7 
      8 function log(str) {
      9   //dump(`SJS LOG: ${str}\n`);
     10 }
     11 
     12 /**
     13  * Given a multipart/form-data encoded string that we know to have only a single
     14  * part, return the contents of the part.  (MIME multipart encoding is too
     15  * exciting to delve into.)
     16  */
     17 function extractBlobFromMultipartFormData(text) {
     18   const lines = text.split(/\r\n/g);
     19   const firstBlank = lines.indexOf("");
     20   const foo = lines.slice(firstBlank + 1, -2).join("\n");
     21   return foo;
     22 }
     23 
     24 async function handleRequest(request, response) {
     25   let blobContents = "";
     26   if (request.method !== "POST") {
     27   } else {
     28     var body = new BinaryInputStream(request.bodyInputStream);
     29 
     30     var avail;
     31     var bytes = [];
     32 
     33     while ((avail = body.available()) > 0) {
     34       Array.prototype.push.apply(bytes, body.readByteArray(avail));
     35     }
     36     let requestBodyContents = String.fromCharCode.apply(null, bytes);
     37     log(requestBodyContents);
     38     blobContents = extractBlobFromMultipartFormData(requestBodyContents);
     39   }
     40 
     41   log("Setting Headers");
     42   response.setHeader("Content-Type", "text/html", false);
     43   response.setStatusLine(request.httpVersion, "200", "OK");
     44   response.write(`<!DOCTYPE HTML><head><meta charset="utf-8"/></head><body>
     45           <h1 id="url">${request.scheme}${request.host}${request.port}${request.path}</h1>
     46           <div id="source">ServerJS</div>
     47           <div id="blob">${blobContents}</div>
     48         </body>`);
     49   log("Done");
     50 }