tor-browser

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

bug733553.sjs (3076B)


      1 /* Any copyright is dedicated to the Public Domain.
      2  * http://creativecommons.org/publicdomain/zero/1.0/
      3  */
      4 
      5 var bodyPartIndex = -1;
      6 var bodyParts = [
      7   ["red.png", "image/png"],
      8   ["animated-gif2.gif", "image/gif"],
      9   ["red.png", "image/png"],
     10   ["lime100x100.svg", "image/svg+xml"],
     11   ["lime100x100.svg", "image/svg+xml"],
     12   ["animated-gif2.gif", "image/gif"],
     13   ["red.png", "image/png"],
     14   // Mime type intentionally wrong (test for bug 907575)
     15   ["shaver.png", "image/gif"],
     16   ["red.png", "image/png"],
     17   ["damon.jpg", "image/jpeg"],
     18   ["damon.jpg", "application/octet-stream"],
     19   ["damon.jpg", "image/jpeg"],
     20   ["rillybad.jpg", "application/x-unknown-content-type"],
     21   ["damon.jpg", "image/jpeg"],
     22   ["bad.jpg", "image/jpeg"],
     23   ["red.png", "image/png"],
     24   ["invalid.jpg", "image/jpeg"],
     25   ["animated-gif2.gif", "image/gif"],
     26 ];
     27 var timer = Cc["@mozilla.org/timer;1"];
     28 var partTimer = timer.createInstance(Ci.nsITimer);
     29 
     30 function getFileAsInputStream(aFilename) {
     31   var file = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
     32 
     33   file.append("tests");
     34   file.append("image");
     35   file.append("test");
     36   file.append("mochitest");
     37   file.append(aFilename);
     38 
     39   var fileStream = Cc[
     40     "@mozilla.org/network/file-input-stream;1"
     41   ].createInstance(Ci.nsIFileInputStream);
     42   fileStream.init(file, 1, 0, false);
     43   return fileStream;
     44 }
     45 
     46 function handleRequest(request, response) {
     47   if (!getSharedState("next-part")) {
     48     setSharedState("next-part", "-1");
     49   }
     50   response.setHeader(
     51     "Content-Type",
     52     "multipart/x-mixed-replace;boundary=BOUNDARYOMG",
     53     false
     54   );
     55   response.setHeader("Cache-Control", "no-cache", false);
     56   response.setStatusLine(request.httpVersion, 200, "OK");
     57   // We're sending parts off in a delayed fashion, to let the tests occur.
     58   response.processAsync();
     59   response.write("--BOUNDARYOMG\r\n");
     60   sendParts(response);
     61 }
     62 
     63 function sendParts(response) {
     64   let wait = false;
     65   let nextPart = parseInt(getSharedState("next-part"), 10);
     66   if (nextPart == bodyPartIndex) {
     67     // Haven't been signaled yet, remain in holding pattern
     68     wait = true;
     69   } else {
     70     bodyPartIndex = nextPart;
     71   }
     72   if (bodyParts.length > bodyPartIndex) {
     73     let callback;
     74     if (!wait) {
     75       callback = getSendNextPart(response);
     76     } else {
     77       callback = function () {
     78         sendParts(response);
     79       };
     80     }
     81     partTimer.initWithCallback(callback, 1000, Ci.nsITimer.TYPE_ONE_SHOT);
     82   } else {
     83     sendClose(response);
     84   }
     85 }
     86 
     87 function sendClose(response) {
     88   response.write("--BOUNDARYOMG--\r\n");
     89   response.finish();
     90 }
     91 
     92 function getSendNextPart(response) {
     93   var part = bodyParts[bodyPartIndex];
     94   var nextPartHead = "Content-Type: " + part[1] + "\r\n\r\n";
     95   var inputStream = getFileAsInputStream(part[0]);
     96   return function () {
     97     response.bodyOutputStream.write(nextPartHead, nextPartHead.length);
     98     response.bodyOutputStream.writeFrom(inputStream, inputStream.available());
     99     inputStream.close();
    100     // Toss in the boundary, so the browser can know this part is complete
    101     response.write("--BOUNDARYOMG\r\n");
    102     sendParts(response);
    103   };
    104 }