tor-browser

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

bug767779.sjs (1639B)


      1 /* Any copyright is dedicated to the Public Domain.
      2  * http://creativecommons.org/publicdomain/zero/1.0/
      3  */
      4 
      5 var timer = Cc["@mozilla.org/timer;1"];
      6 var partTimer = timer.createInstance(Ci.nsITimer);
      7 
      8 function getFileAsInputStream(aFilename) {
      9   var file = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
     10 
     11   file.append("tests");
     12   file.append("image");
     13   file.append("test");
     14   file.append("mochitest");
     15   file.append(aFilename);
     16 
     17   var fileStream = Cc[
     18     "@mozilla.org/network/file-input-stream;1"
     19   ].createInstance(Ci.nsIFileInputStream);
     20   fileStream.init(file, 1, 0, false);
     21   return fileStream;
     22 }
     23 
     24 function handleRequest(request, response) {
     25   response.setHeader("Content-Type", "image/gif", false);
     26   response.setHeader("Cache-Control", "no-cache", false);
     27   response.setStatusLine(request.httpVersion, 200, "OK");
     28   // We're sending data off in a delayed fashion
     29   response.processAsync();
     30   var inputStream = getFileAsInputStream("animated-gif_trailing-garbage.gif");
     31   // Should be 4029 bytes available.
     32   // Send the good data at once
     33   response.bodyOutputStream.writeFrom(inputStream, 285);
     34   sendParts(inputStream, response);
     35 }
     36 
     37 function sendParts(inputStream, response) {
     38   // 3744 left, send in 8 chunks of 468 each
     39   partTimer.initWithCallback(
     40     getSendNextPart(inputStream, response),
     41     500,
     42     Ci.nsITimer.TYPE_ONE_SHOT
     43   );
     44 }
     45 
     46 function getSendNextPart(inputStream, response) {
     47   return function () {
     48     response.bodyOutputStream.writeFrom(inputStream, 468);
     49     if (!inputStream.available()) {
     50       inputStream.close();
     51       response.finish();
     52     } else {
     53       sendParts(inputStream, response);
     54     }
     55   };
     56 }