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 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | |
michael@0 | 3 | var httpserver = null; |
michael@0 | 4 | |
michael@0 | 5 | function make_channel(url, callback, ctx) { |
michael@0 | 6 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 7 | getService(Ci.nsIIOService); |
michael@0 | 8 | return ios.newChannel(url, "", null); |
michael@0 | 9 | } |
michael@0 | 10 | |
michael@0 | 11 | const responseBody = "response body"; |
michael@0 | 12 | |
michael@0 | 13 | function cachedHandler(metadata, response) { |
michael@0 | 14 | var body = responseBody; |
michael@0 | 15 | if (metadata.hasHeader("Range")) { |
michael@0 | 16 | var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); |
michael@0 | 17 | var from = (matches[1] === undefined) ? 0 : matches[1]; |
michael@0 | 18 | var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2]; |
michael@0 | 19 | if (from >= responseBody.length) { |
michael@0 | 20 | response.setStatusLine(metadata.httpVersion, 416, "Start pos too high"); |
michael@0 | 21 | response.setHeader("Content-Range", "*/" + responseBody.length, false); |
michael@0 | 22 | return; |
michael@0 | 23 | } |
michael@0 | 24 | body = responseBody.slice(from, to + 1); |
michael@0 | 25 | // always respond to successful range requests with 206 |
michael@0 | 26 | response.setStatusLine(metadata.httpVersion, 206, "Partial Content"); |
michael@0 | 27 | response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 31 | response.setHeader("ETag", "Just testing"); |
michael@0 | 32 | response.setHeader("Accept-Ranges", "bytes"); |
michael@0 | 33 | |
michael@0 | 34 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | function Canceler(continueFn) { |
michael@0 | 38 | this.continueFn = continueFn; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | Canceler.prototype = { |
michael@0 | 42 | QueryInterface: function(iid) { |
michael@0 | 43 | if (iid.equals(Ci.nsIStreamListener) || |
michael@0 | 44 | iid.equals(Ci.nsIRequestObserver) || |
michael@0 | 45 | iid.equals(Ci.nsISupports)) |
michael@0 | 46 | return this; |
michael@0 | 47 | throw Components.results.NS_ERROR_NO_INTERFACE; |
michael@0 | 48 | }, |
michael@0 | 49 | |
michael@0 | 50 | onStartRequest: function(request, context) { |
michael@0 | 51 | }, |
michael@0 | 52 | |
michael@0 | 53 | onDataAvailable: function(request, context, stream, offset, count) { |
michael@0 | 54 | request.QueryInterface(Ci.nsIChannel) |
michael@0 | 55 | .cancel(Components.results.NS_BINDING_ABORTED); |
michael@0 | 56 | }, |
michael@0 | 57 | |
michael@0 | 58 | onStopRequest: function(request, context, status) { |
michael@0 | 59 | do_check_eq(status, Components.results.NS_BINDING_ABORTED); |
michael@0 | 60 | this.continueFn(); |
michael@0 | 61 | } |
michael@0 | 62 | }; |
michael@0 | 63 | |
michael@0 | 64 | function finish_test() { |
michael@0 | 65 | httpserver.stop(do_test_finished); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | function start_cache_read() { |
michael@0 | 69 | var chan = make_channel("http://localhost:" + |
michael@0 | 70 | httpserver.identity.primaryPort + "/cached/test.gz"); |
michael@0 | 71 | chan.asyncOpen(new ChannelListener(finish_test, null), null); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | function start_canceler() { |
michael@0 | 75 | var chan = make_channel("http://localhost:" + |
michael@0 | 76 | httpserver.identity.primaryPort + "/cached/test.gz"); |
michael@0 | 77 | chan.asyncOpen(new Canceler(start_cache_read), null); |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | function run_test() { |
michael@0 | 81 | httpserver = new HttpServer(); |
michael@0 | 82 | httpserver.registerPathHandler("/cached/test.gz", cachedHandler); |
michael@0 | 83 | httpserver.start(-1); |
michael@0 | 84 | |
michael@0 | 85 | var chan = make_channel("http://localhost:" + |
michael@0 | 86 | httpserver.identity.primaryPort + "/cached/test.gz"); |
michael@0 | 87 | chan.asyncOpen(new ChannelListener(start_canceler, null), null); |
michael@0 | 88 | do_test_pending(); |
michael@0 | 89 | } |