Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | // testString = "This is a slightly longer test\n"; |
michael@0 | 6 | const responseBody = [0x1f, 0x8b, 0x08, 0x08, 0xef, 0x70, 0xe6, 0x4c, 0x00, 0x03, 0x74, 0x65, 0x78, 0x74, 0x66, 0x69, |
michael@0 | 7 | 0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85, |
michael@0 | 8 | 0xe2, 0x9c, 0xcc, 0xf4, 0x8c, 0x92, 0x9c, 0x4a, 0x85, 0x9c, 0xfc, 0xbc, 0xf4, 0xd4, 0x22, 0x85, |
michael@0 | 9 | 0x92, 0xd4, 0xe2, 0x12, 0x2e, 0x2e, 0x00, 0x00, 0xe5, 0xe6, 0xf0, 0x20, 0x00, 0x00, 0x00]; |
michael@0 | 10 | |
michael@0 | 11 | function make_channel(url, callback, ctx) { |
michael@0 | 12 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 13 | getService(Ci.nsIIOService); |
michael@0 | 14 | return ios.newChannel(url, "", null); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | var doRangeResponse = false; |
michael@0 | 18 | |
michael@0 | 19 | function cachedHandler(metadata, response) { |
michael@0 | 20 | response.setHeader("Content-Type", "application/x-gzip", false); |
michael@0 | 21 | response.setHeader("Content-Encoding", "gzip", false); |
michael@0 | 22 | response.setHeader("ETag", "Just testing"); |
michael@0 | 23 | response.setHeader("Cache-Control", "max-age=3600000"); // avoid validation |
michael@0 | 24 | response.setHeader("Content-Length", "" + responseBody.length); |
michael@0 | 25 | |
michael@0 | 26 | var body = responseBody; |
michael@0 | 27 | |
michael@0 | 28 | if (doRangeResponse) { |
michael@0 | 29 | do_check_true(metadata.hasHeader("Range")); |
michael@0 | 30 | var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/); |
michael@0 | 31 | var from = (matches[1] === undefined) ? 0 : matches[1]; |
michael@0 | 32 | var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2]; |
michael@0 | 33 | if (from >= responseBody.length) { |
michael@0 | 34 | response.setStatusLine(metadata.httpVersion, 416, "Start pos too high"); |
michael@0 | 35 | response.setHeader("Content-Range", "*/" + responseBody.length, false); |
michael@0 | 36 | return; |
michael@0 | 37 | } |
michael@0 | 38 | body = body.slice(from, to + 1); |
michael@0 | 39 | // always respond to successful range requests with 206 |
michael@0 | 40 | response.setStatusLine(metadata.httpVersion, 206, "Partial Content"); |
michael@0 | 41 | response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false); |
michael@0 | 42 | } else { |
michael@0 | 43 | response.setHeader("Accept-Ranges", "bytes"); |
michael@0 | 44 | body = body.slice(0, 17); // slice off a piece to send first |
michael@0 | 45 | doRangeResponse = true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | var bos = Cc["@mozilla.org/binaryoutputstream;1"] |
michael@0 | 49 | .createInstance(Ci.nsIBinaryOutputStream); |
michael@0 | 50 | bos.setOutputStream(response.bodyOutputStream); |
michael@0 | 51 | |
michael@0 | 52 | response.processAsync(); |
michael@0 | 53 | bos.writeByteArray(body, body.length); |
michael@0 | 54 | response.finish(); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | function continue_test(request, data) { |
michael@0 | 58 | do_check_true(17 == data.length); |
michael@0 | 59 | var chan = make_channel("http://localhost:" + |
michael@0 | 60 | httpserver.identity.primaryPort + "/cached/test.gz"); |
michael@0 | 61 | chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_GZIP), null); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | function finish_test(request, data, ctx) { |
michael@0 | 65 | do_check_eq(request.status, 0); |
michael@0 | 66 | do_check_eq(data.length, responseBody.length); |
michael@0 | 67 | for (var i = 0; i < data.length; ++i) { |
michael@0 | 68 | do_check_eq(data.charCodeAt(i), responseBody[i]); |
michael@0 | 69 | } |
michael@0 | 70 | httpserver.stop(do_test_finished); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function run_test() { |
michael@0 | 74 | httpserver = new HttpServer(); |
michael@0 | 75 | httpserver.registerPathHandler("/cached/test.gz", cachedHandler); |
michael@0 | 76 | httpserver.start(-1); |
michael@0 | 77 | |
michael@0 | 78 | // wipe out cached content |
michael@0 | 79 | evict_cache_entries(); |
michael@0 | 80 | |
michael@0 | 81 | var chan = make_channel("http://localhost:" + |
michael@0 | 82 | httpserver.identity.primaryPort + "/cached/test.gz"); |
michael@0 | 83 | chan.asyncOpen(new ChannelListener(continue_test, null, CL_EXPECT_GZIP), null); |
michael@0 | 84 | do_test_pending(); |
michael@0 | 85 | } |