1 do_import_script("netwerk/test/httpserver/httpd.js");
5 function make_channel(url, callback, ctx) {
6 var ios = Cc["@mozilla.org/network/io-service;1"].
7 getService(Ci.nsIIOService);
8 return ios.newChannel(url, "", null);
13 const responseBody = "response body";
15 function cachedHandler(metadata, response) {
16 var body = responseBody;
17 if (metadata.hasHeader("Range")) {
18 var matches = metadata.getHeader("Range").match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
19 var from = (matches[1] === undefined) ? 0 : matches[1];
20 var to = (matches[2] === undefined) ? responseBody.length - 1 : matches[2];
21 if (from >= responseBody.length) {
22 response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
23 response.setHeader("Content-Range", "*/" + responseBody.length, false);
26 body = responseBody.slice(from, to + 1);
27 // always respond to successful range requests with 206
28 response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
29 response.setHeader("Content-Range", from + "-" + to + "/" + responseBody.length, false);
31 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
35 response.setHeader("Content-Type", "text/plain", false);
36 response.setHeader("ETag", "Just testing");
37 response.setHeader("Accept-Ranges", "bytes");
39 response.bodyOutputStream.write(body, body.length);
42 function Canceler(continueFn) {
43 this.continueFn = continueFn;
46 Canceler.prototype = {
47 QueryInterface: function(iid) {
48 if (iid.equals(Ci.nsIStreamListener) ||
49 iid.equals(Ci.nsIRequestObserver) ||
50 iid.equals(Ci.nsISupports))
52 throw Components.results.NS_ERROR_NO_INTERFACE;
55 onStartRequest: function(request, context) {
58 onDataAvailable: function(request, context, stream, offset, count) {
59 request.QueryInterface(Ci.nsIChannel)
60 .cancel(Components.results.NS_BINDING_ABORTED);
63 onStopRequest: function(request, context, status) {
64 do_check_eq(status, Components.results.NS_BINDING_ABORTED);
69 function finish_test() {
74 function start_cache_read() {
76 var chan = make_channel("http://localhost:4444/cached/test.gz");
77 chan.asyncOpen(new ChannelListener(finish_test, null), null);
80 function start_canceler() {
82 var chan = make_channel("http://localhost:4444/cached/test.gz");
83 chan.asyncOpen(new Canceler(start_cache_read), null);
87 httpserver = new nsHttpServer();
88 httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
89 httpserver.start(4444);
92 var chan = make_channel("http://localhost:4444/cached/test.gz");
93 chan.asyncOpen(new ChannelListener(start_canceler, null), null);