michael@0: const CC = Components.Constructor; michael@0: const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1", michael@0: "nsIBinaryInputStream", michael@0: "setInputStream"); michael@0: michael@0: function setReq(req) { michael@0: setObjectState("content/base/test/progressserver", req); michael@0: } michael@0: michael@0: function getReq() { michael@0: var req; michael@0: getObjectState("content/base/test/progressserver", function(v) { michael@0: req = v; michael@0: }); michael@0: return req; michael@0: } michael@0: michael@0: function handleRequest(request, response) michael@0: { michael@0: var pairs = request.queryString.split('&'); michael@0: var command = pairs.shift(); michael@0: dump("received '" + command + "' command\n"); michael@0: michael@0: var bodyStream = new BinaryInputStream(request.bodyInputStream); michael@0: var body = ""; michael@0: var bodyAvail; michael@0: while ((bodyAvail = bodyStream.available()) > 0) michael@0: body += String.fromCharCode.apply(null, bodyStream.readByteArray(bodyAvail)); michael@0: michael@0: if (command == "open") { michael@0: response.processAsync(); michael@0: setReq(response); michael@0: michael@0: response.setHeader("Cache-Control", "no-cache", false); michael@0: pairs.forEach(function (val) { michael@0: var [name, value] = val.split('='); michael@0: response.setHeader(name, unescape(value), false); michael@0: }); michael@0: response.write(body); michael@0: return; michael@0: } michael@0: michael@0: if (command == "send") { michael@0: getReq().write(body); michael@0: } michael@0: else if (command == "close") { michael@0: getReq().finish(); michael@0: setReq(null); michael@0: } michael@0: response.setHeader("Content-Type", "text/plain"); michael@0: response.write("ok"); michael@0: }