progressserver.sjs (1327B)
1 const CC = Components.Constructor; 2 const BinaryInputStream = CC( 3 "@mozilla.org/binaryinputstream;1", 4 "nsIBinaryInputStream", 5 "setInputStream" 6 ); 7 8 function setReq(req) { 9 setObjectState("dom/xhr/tests/progressserver", req); 10 } 11 12 function getReq() { 13 var req; 14 getObjectState("dom/xhr/tests/progressserver", function (v) { 15 req = v; 16 }); 17 return req; 18 } 19 20 function handleRequest(request, response) { 21 var pairs = request.queryString.split("&"); 22 var command = pairs.shift(); 23 dump("received '" + command + "' command\n"); 24 25 var bodyStream = new BinaryInputStream(request.bodyInputStream); 26 var body = ""; 27 var bodyAvail; 28 while ((bodyAvail = bodyStream.available()) > 0) { 29 body += String.fromCharCode.apply( 30 null, 31 bodyStream.readByteArray(bodyAvail) 32 ); 33 } 34 35 if (command == "open") { 36 response.processAsync(); 37 setReq(response); 38 39 response.setHeader("Cache-Control", "no-cache", false); 40 pairs.forEach(function (val) { 41 var [name, value] = val.split("="); 42 response.setHeader(name, unescape(value), false); 43 }); 44 response.write(body); 45 return; 46 } 47 48 if (command == "send") { 49 getReq().write(body); 50 } else if (command == "close") { 51 getReq().finish(); 52 setReq(null); 53 } 54 response.setHeader("Content-Type", "text/plain"); 55 response.write("ok"); 56 }