tor-browser

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

check-header.sjs (3024B)


      1 const BinaryOutputStream = Components.Constructor("@mozilla.org/binaryoutputstream;1", "nsIBinaryOutputStream", "setOutputStream");
      2 
      3 function isCatchall(v)
      4 {
      5   // bug 1249474 made this be */* exactly, but bug 1711622
      6   // reverted to previous spec-conforming behaviour.
      7   // "image/*" item exactly or with a quality factor
      8   return /^image\/\*(?:|;q=(?:1(?:\.0{0,3})?|0(?:\.\d{0,3})?))$/.test(v);
      9 }
     10 
     11 /*
     12 # Python used to generate the following byte array
     13 def toHex(n):
     14   if n < 16: return "0x" + hex(n)[2:].upper()
     15   return "0x" + hex(n)[2:].upper()
     16 
     17 def hexFile(name):
     18   f = open(name, "rb")
     19   try:
     20     while True:
     21       print toHex(ord(f.read(1))) + ", ",
     22   except:
     23     pass
     24 
     25 hexFile("image/test/reftest/generic/green.png")
     26 */
     27 
     28 const IMAGE_DATA =
     29   [
     30    0x89,  0x50,  0x4E,  0x47,  0x0D,  0x0A,  0x1A,  0x0A,  0x00,  0x00,  0x00,
     31    0x0D,  0x49,  0x48,  0x44,  0x52,  0x00,  0x00,  0x00,  0x64,  0x00,  0x00,
     32    0x00,  0x64,  0x08,  0x02,  0x00,  0x00,  0x00,  0xFF,  0x80,  0x02,  0x03,
     33    0x00,  0x00,  0x00,  0x01,  0x73,  0x52,  0x47,  0x42,  0x00,  0xAE,  0xCE,
     34    0x1C,  0xE9,  0x00,  0x00,  0x00,  0x9E,  0x49,  0x44,  0x41,  0x54,  0x78,
     35    0xDA,  0xED,  0xD0,  0x31,  0x01,  0x00,  0x00,  0x08,  0x03,  0xA0,  0x69,
     36    0xFF,  0xCE,  0x5A,  0xC1,  0xCF,  0x07,  0x22,  0x50,  0x99,  0x70,  0xD4,
     37    0x0A,  0x64,  0xC9,  0x92,  0x25,  0x4B,  0x96,  0x2C,  0x05,  0xB2,  0x64,
     38    0xC9,  0x92,  0x25,  0x4B,  0x96,  0x02,  0x59,  0xB2,  0x64,  0xC9,  0x92,
     39    0x25,  0x4B,  0x81,  0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0xA5,  0x40,
     40    0x96,  0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x52,  0x20,  0x4B,  0x96,  0x2C,
     41    0x59,  0xB2,  0x64,  0x29,  0x90,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0xB2,
     42    0x14,  0xC8,  0x92,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0x0A,  0x64,  0xC9,
     43    0x92,  0x25,  0x4B,  0x96,  0x2C,  0x05,  0xB2,  0x64,  0xC9,  0x92,  0x25,
     44    0x4B,  0x96,  0x02,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0x25,  0x4B,  0x81,
     45    0x2C,  0x59,  0xB2,  0x64,  0xC9,  0x92,  0xA5,  0x40,  0x96,  0x2C,  0x59,
     46    0xB2,  0x64,  0xC9,  0x52,  0x20,  0x4B,  0x96,  0x2C,  0x59,  0xB2,  0x64,
     47    0x29,  0x90,  0x25,  0x4B,  0x96,  0x2C,  0x59,  0xB2,  0x14,  0xC8,  0x92,
     48    0x25,  0x4B,  0x96,  0x2C,  0x59,  0x0A,  0x64,  0xC9,  0xFA,  0xB6,  0x89,
     49    0x5F,  0x01,  0xC7,  0x24,  0x83,  0xB2,  0x0C,  0x00,  0x00,  0x00,  0x00,
     50    0x49,  0x45,  0x4E,  0x44,  0xAE,  0x42,  0x60,  0x82,
     51   ];
     52 
     53 function handleRequest(request, response)
     54 {
     55   response.setHeader("Content-Type", "text/plain", false);
     56   response.setHeader("Cache-Control", "no-cache", false);
     57 
     58   var accept = request.hasHeader("Accept")
     59              ? request.getHeader("Accept")
     60              : "";
     61 
     62   if (accept.split(",").some(isCatchall))
     63   {
     64     response.setHeader("Content-Type", "image/png", false);
     65 
     66     var stream = new BinaryOutputStream(response.bodyOutputStream);
     67     stream.writeByteArray(IMAGE_DATA);
     68   }
     69   else
     70   {
     71     response.setStatusLine(request.httpVersion, 404, "Not found");
     72     response.write("Accept header contained: " + accept);
     73   }
     74 }