1 do_import_script("netwerk/test/httpserver/httpd.js");
5 const responseBody = [0x1f, 0x8b, 0x08, 0x00, 0x16, 0x5a, 0x8a, 0x48, 0x02,
6 0x03, 0x2b, 0x49, 0x2d, 0x2e, 0xe1, 0x02, 0x00, 0xc6,
7 0x35, 0xb9, 0x3b, 0x05, 0x00, 0x00, 0x00];
9 function make_channel(url, callback, ctx) {
10 var ios = Cc["@mozilla.org/network/io-service;1"].
11 getService(Ci.nsIIOService);
12 return ios.newChannel(url, "", null);
15 var doRangeResponse = false;
17 function cachedHandler(metadata, response) {
18 response.setHeader("Content-Type", "application/x-gzip", false);
19 response.setHeader("Content-Encoding", "gzip", false);
20 response.setHeader("ETag", "Just testing");
22 var body = responseBody;
24 if (doRangeResponse) {
25 do_check_true(metadata.hasHeader("Range"));
26 var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
27 var from = (matches[1] === undefined) ? 0 : matches[1];
28 var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2];
29 if (from >= responseBody.length) {
30 response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
31 response.setHeader("Content-Range", "*/" + responseBody.length, false);
34 body = body.slice(from, to + 1);
35 // always respond to successful range requests with 206
36 response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
37 response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
39 response.setHeader("Accept-Ranges", "bytes");
40 doRangeResponse = true;
43 var bos = Cc["@mozilla.org/binaryoutputstream;1"]
44 .createInstance(Ci.nsIBinaryOutputStream);
45 bos.setOutputStream(response.bodyOutputStream);
47 bos.writeByteArray(body, body.length);
53 Canceler.prototype = {
54 QueryInterface: function(iid) {
55 if (iid.equals(Ci.nsIStreamListener) ||
56 iid.equals(Ci.nsIRequestObserver) ||
57 iid.equals(Ci.nsISupports))
59 throw Components.results.NS_ERROR_NO_INTERFACE;
62 onStartRequest: function(request, context) {
65 onDataAvailable: function(request, context, stream, offset, count) {
66 request.QueryInterface(Ci.nsIChannel)
67 .cancel(Components.results.NS_BINDING_ABORTED);
70 onStopRequest: function(request, context, status) {
71 do_check_eq(status, Components.results.NS_BINDING_ABORTED);
76 function continue_test() {
77 var chan = make_channel("http://localhost:4444/cached/test.gz");
78 chan.asyncOpen(new ChannelListener(finish_test, null), null);
81 function finish_test(request, data, ctx) {
83 do_check_eq(request.status, 0);
84 do_check_eq(data.length, responseBody.length);
85 for (var i = 0; i < data.length; ++i) {
86 do_check_eq(data.charCodeAt(i), responseBody[i]);
92 httpserver = new nsHttpServer();
93 httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
94 httpserver.start(4444);
96 var chan = make_channel("http://localhost:4444/cached/test.gz");
97 chan.asyncOpen(new Canceler(), null);