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