2012-08-14 14:06:04 +00:00
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
2014-10-24 20:19:40 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2015-12-22 00:29:26 +00:00
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
|
|
|
|
2008-08-01 00:55:14 +00:00
|
|
|
|
|
|
|
var httpserver = null;
|
|
|
|
|
2011-02-04 19:49:05 +00:00
|
|
|
// testString = "This is a slightly longer test\n";
|
|
|
|
const responseBody = [0x1f, 0x8b, 0x08, 0x08, 0xef, 0x70, 0xe6, 0x4c, 0x00, 0x03, 0x74, 0x65, 0x78, 0x74, 0x66, 0x69,
|
|
|
|
0x6c, 0x65, 0x2e, 0x74, 0x78, 0x74, 0x00, 0x0b, 0xc9, 0xc8, 0x2c, 0x56, 0x00, 0xa2, 0x44, 0x85,
|
|
|
|
0xe2, 0x9c, 0xcc, 0xf4, 0x8c, 0x92, 0x9c, 0x4a, 0x85, 0x9c, 0xfc, 0xbc, 0xf4, 0xd4, 0x22, 0x85,
|
|
|
|
0x92, 0xd4, 0xe2, 0x12, 0x2e, 0x2e, 0x00, 0x00, 0xe5, 0xe6, 0xf0, 0x20, 0x00, 0x00, 0x00];
|
|
|
|
|
2008-08-01 00:55:14 +00:00
|
|
|
function make_channel(url, callback, ctx) {
|
2015-12-22 00:29:26 +00:00
|
|
|
return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true});
|
2008-08-01 00:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var doRangeResponse = false;
|
|
|
|
|
|
|
|
function cachedHandler(metadata, response) {
|
|
|
|
response.setHeader("Content-Type", "application/x-gzip", false);
|
|
|
|
response.setHeader("Content-Encoding", "gzip", false);
|
|
|
|
response.setHeader("ETag", "Just testing");
|
2011-02-04 19:49:05 +00:00
|
|
|
response.setHeader("Cache-Control", "max-age=3600000"); // avoid validation
|
2008-08-01 00:55:14 +00:00
|
|
|
|
|
|
|
var body = responseBody;
|
|
|
|
|
|
|
|
if (doRangeResponse) {
|
|
|
|
do_check_true(metadata.hasHeader("Range"));
|
|
|
|
var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
|
|
|
|
var from = (matches[1] === undefined) ? 0 : matches[1];
|
|
|
|
var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2];
|
|
|
|
if (from >= responseBody.length) {
|
|
|
|
response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
|
|
|
|
response.setHeader("Content-Range", "*/" + responseBody.length, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
body = body.slice(from, to + 1);
|
2014-06-08 22:15:00 +00:00
|
|
|
response.setHeader("Content-Length", "" + (to + 1 - from));
|
2008-08-01 00:55:14 +00:00
|
|
|
// always respond to successful range requests with 206
|
|
|
|
response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
|
|
|
|
response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
|
|
|
|
} else {
|
2014-06-08 22:15:00 +00:00
|
|
|
// This response will get cut off prematurely
|
|
|
|
response.setHeader("Content-Length", "" + responseBody.length);
|
2008-08-01 00:55:14 +00:00
|
|
|
response.setHeader("Accept-Ranges", "bytes");
|
2011-02-04 19:49:05 +00:00
|
|
|
body = body.slice(0, 17); // slice off a piece to send first
|
2008-08-01 00:55:14 +00:00
|
|
|
doRangeResponse = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bos = Cc["@mozilla.org/binaryoutputstream;1"]
|
|
|
|
.createInstance(Ci.nsIBinaryOutputStream);
|
|
|
|
bos.setOutputStream(response.bodyOutputStream);
|
|
|
|
|
2011-02-04 19:49:05 +00:00
|
|
|
response.processAsync();
|
2008-08-01 00:55:14 +00:00
|
|
|
bos.writeByteArray(body, body.length);
|
2011-02-04 19:49:05 +00:00
|
|
|
response.finish();
|
2008-08-01 00:55:14 +00:00
|
|
|
}
|
|
|
|
|
2011-02-04 19:49:05 +00:00
|
|
|
function continue_test(request, data) {
|
2014-06-08 22:15:00 +00:00
|
|
|
do_check_eq(17, data.length);
|
2013-07-19 17:19:28 +00:00
|
|
|
var chan = make_channel("http://localhost:" +
|
|
|
|
httpserver.identity.primaryPort + "/cached/test.gz");
|
2015-12-22 00:29:26 +00:00
|
|
|
chan.asyncOpen2(new ChannelListener(finish_test, null, CL_EXPECT_GZIP));
|
2008-08-01 00:55:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 20:19:40 +00:00
|
|
|
var enforcePref;
|
|
|
|
|
2008-08-01 00:55:14 +00:00
|
|
|
function finish_test(request, data, ctx) {
|
|
|
|
do_check_eq(request.status, 0);
|
|
|
|
do_check_eq(data.length, responseBody.length);
|
|
|
|
for (var i = 0; i < data.length; ++i) {
|
|
|
|
do_check_eq(data.charCodeAt(i), responseBody[i]);
|
|
|
|
}
|
2014-10-24 20:19:40 +00:00
|
|
|
Services.prefs.setBoolPref("network.http.enforce-framing.http1", enforcePref);
|
2011-02-04 19:49:05 +00:00
|
|
|
httpserver.stop(do_test_finished);
|
2008-08-01 00:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function run_test() {
|
2014-10-24 20:19:40 +00:00
|
|
|
enforcePref = Services.prefs.getBoolPref("network.http.enforce-framing.http1");
|
|
|
|
Services.prefs.setBoolPref("network.http.enforce-framing.http1", false);
|
|
|
|
|
2012-08-14 14:06:04 +00:00
|
|
|
httpserver = new HttpServer();
|
2008-08-01 00:55:14 +00:00
|
|
|
httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
|
2013-07-19 17:19:28 +00:00
|
|
|
httpserver.start(-1);
|
2008-08-01 00:55:14 +00:00
|
|
|
|
2011-02-04 19:49:05 +00:00
|
|
|
// wipe out cached content
|
2012-06-12 21:23:32 +00:00
|
|
|
evict_cache_entries();
|
2011-02-04 19:49:05 +00:00
|
|
|
|
2013-07-19 17:19:28 +00:00
|
|
|
var chan = make_channel("http://localhost:" +
|
|
|
|
httpserver.identity.primaryPort + "/cached/test.gz");
|
2015-12-22 00:29:26 +00:00
|
|
|
chan.asyncOpen2(new ChannelListener(continue_test, null, CL_EXPECT_GZIP | CL_IGNORE_CL));
|
2008-08-01 00:55:14 +00:00
|
|
|
do_test_pending();
|
|
|
|
}
|