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