tor-browser

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

file_IconTestServer.sjs (2878B)


      1 const TIMEOUT_INTERVAL_MS = 100;
      2 
      3 function handleRequest(request, response) {
      4   // Allow us to asynchronously construct the response with timeouts
      5   // rather than forcing us to make the whole thing in one call. See
      6   // bug 396226.
      7   response.processAsync();
      8 
      9   // Figure out whether the client wants to load the image, or just
     10   // to tell us to finish the previous load
     11   var query = {};
     12   request.queryString.split("&").forEach(function (val) {
     13     var [name, value] = val.split("=");
     14     query[name] = unescape(value);
     15   });
     16   if (query.continue == "true") {
     17     // Debugging information so we can figure out the hang
     18     dump("file_IconTestServer.js DEBUG - Got continue command\n");
     19 
     20     // Get the context structure and finish the old request
     21     getObjectState("context", function (obj) {
     22       // magic or goop, depending on how you look at it
     23       savedCtx = obj.wrappedJSObject;
     24 
     25       // Write the rest of the data
     26       savedCtx.ostream.writeFrom(
     27         savedCtx.istream,
     28         savedCtx.istream.available()
     29       );
     30 
     31       // Close the streams
     32       savedCtx.ostream.close();
     33       savedCtx.istream.close();
     34 
     35       // Finish off 'the old response'
     36       savedCtx.response.finish();
     37     });
     38 
     39     // Finish off 'the current response'
     40     response.finish();
     41     return;
     42   }
     43 
     44   // Debugging information so we can figure out the hang
     45   dump("file_IconTestServer.js DEBUG - Got initial request\n");
     46 
     47   // Context structure - we need to set this up properly to pass to setObjectState
     48   var ctx = {
     49     QueryInterface: function (iid) {
     50       if (iid.equals(Ci.nsISupports)) {
     51         return this;
     52       }
     53       throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
     54     },
     55   };
     56   ctx.wrappedJSObject = ctx;
     57 
     58   // Save the response
     59   ctx.response = response;
     60 
     61   // We're serving up a png
     62   response.setHeader("Content-Type", "image/png", false);
     63 
     64   // Get the output stream
     65   ctx.ostream = response.bodyOutputStream;
     66 
     67   // Ugly hack, but effective - copied from dom/media/test/contentDuration1.sjs
     68   var pngFile = Cc["@mozilla.org/file/directory_service;1"]
     69     .getService(Ci.nsIProperties)
     70     .get("CurWorkD", Ci.nsIFile);
     71   var paths = "tests/layout/generic/test/file_Dolske.png";
     72   var split = paths.split("/");
     73   for (var i = 0; i < split.length; ++i) {
     74     pngFile.append(split[i]);
     75   }
     76 
     77   // Get an input stream for the png data
     78   ctx.istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
     79     Ci.nsIFileInputStream
     80   );
     81   ctx.istream.init(pngFile, -1, 0, 0);
     82 
     83   // Write the first 10 bytes, which is just boilerplate/magic bytes
     84   ctx.ostream.writeFrom(ctx.istream, 10);
     85 
     86   // Save the context structure for retrieval when we get pinged
     87   setObjectState("context", ctx);
     88 
     89   // Now we play the waiting game...
     90 
     91   // Debugging information so we can figure out the hang
     92   dump("file_IconTestServer.js DEBUG - Playing the waiting game\n");
     93 }