tor-browser

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

delay-image-response.sjs (2974B)


      1 const BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream", "setOutputStream");
      2 /* This data is picked from image/test/reftest/generic/check-header.sjs */
      3 const IMAGE_DATA =
      4   [
      5    0x89,  0x50,  0x4E,  0x47,  0x0D,  0x0A,  0x1A,  0x0A,  0x00,  0x00,  0x00,
      6    0x0D,  0x49,  0x48,  0x44,  0x52,  0x00,  0x00,  0x00,  0x64,  0x00,  0x00,
      7    0x00,  0x64,  0x08,  0x02,  0x00,  0x00,  0x00,  0xFF,  0x80,  0x02,  0x03,
      8    0x00,  0x00,  0x00,  0x01,  0x73,  0x52,  0x47,  0x42,  0x00,  0xAE,  0xCE,
      9    0x1C,  0xE9,  0x00,  0x00,  0x00,  0x9E,  0x49,  0x44,  0x41,  0x54,  0x78,
     10    0xDA,  0xED,  0xD0,  0x31,  0x01,  0x00,  0x00,  0x08,  0x03,  0xA0,  0x69,
     11    0xFF,  0xCE,  0x5A,  0xC1,  0xCF,  0x07,  0x22,  0x50,  0x99,  0x70,  0xD4,
     12    0x0A,  0x64,  0xC9,  0x92,  0x25,  0x4B,  0x96,  0x2C,  0x05,  0xB2,  0x64,
     13    0xC9,  0x92,  0x25,  0x4B,  0x96,  0x02,  0x59,  0xB2,  0x64,  0xC9,  0x92,
     14    0x25,  0x4B,  0x81,  0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0xA5,  0x40,
     15    0x96,  0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x52,  0x20,  0x4B,  0x96,  0x2C,
     16    0x59,  0xB2,  0x64,  0x29,  0x90,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0xB2,
     17    0x14,  0xC8,  0x92,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0x0A,  0x64,  0xC9,
     18    0x92,  0x25,  0x4B,  0x96,  0x2C,  0x05,  0xB2,  0x64,  0xC9,  0x92,  0x25,
     19    0x4B,  0x96,  0x02,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0x25,  0x4B,  0x81,
     20    0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0xA5,  0x40,  0x96,  0x2C,  0x59,
     21    0xB2,  0x64,  0xC9,  0x52,  0x20,  0x4B,  0x96,  0x2C,  0x59,  0xB2,  0x64,
     22    0x29,  0x90,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0xB2,  0x14,  0xC8,  0x92,
     23    0x25,  0x4B,  0x96,  0x2C,  0x59,  0x0A,  0x64,  0xC9,  0xFA,  0xB6,  0x89,
     24    0x5F,  0x01,  0xC7,  0x24,  0x83,  0xB2,  0x0C,  0x00,  0x00,  0x00,  0x00,
     25    0x49,  0x45,  0x4E,  0x44,  0xAE,  0x42,  0x60,  0x82,
     26   ];
     27 
     28 /**
     29  * The timer is needed when a delay is set. We need it to be out of the method
     30  * so it is not eaten alive by the GC.
     31  */
     32 var timer;
     33 
     34 function handleRequest(request, response) {
     35   var query = {};
     36   request.queryString.split('&').forEach(function (val) {
     37     var [name, value] = val.split('=');
     38     query[name] = unescape(value);
     39   });
     40 
     41   response.setStatusLine(request.httpVersion, 200, "OK");
     42   response.setHeader("Content-Type", "image/png", false);
     43 
     44   function imageWrite() {
     45     var stream = new BinaryOutputStream(response.bodyOutputStream);
     46     stream.writeByteArray(IMAGE_DATA);
     47   }
     48 
     49   // If there is no delay, we write the image and leave.
     50   if (!("delay" in query)) {
     51     imageWrite();
     52     return;
     53   }
     54 
     55   // If there is a delay, we create a timer which, when it fires, will write
     56   // image and leave.
     57   response.processAsync();
     58   const nsITimer = Components.interfaces.nsITimer;
     59 
     60   timer = Components.classes["@mozilla.org/timer;1"].createInstance(nsITimer);
     61   timer.initWithCallback(function() {
     62     imageWrite();
     63     response.finish();
     64   }, query["delay"], nsITimer.TYPE_ONE_SHOT);
     65 }