1const CC = Components.Constructor;
2const BinaryInputStream = CC(
3  "@mozilla.org/binaryinputstream;1",
4  "nsIBinaryInputStream",
5  "setInputStream"
6);
7
8function handleRequest(request, response) {
9  response.setHeader("Content-Type", "text/plain", false);
10  if (request.method == "GET") {
11    response.write(request.queryString);
12  } else {
13    var body = new BinaryInputStream(request.bodyInputStream);
14
15    var avail;
16    var bytes = [];
17
18    while ((avail = body.available()) > 0) {
19      Array.prototype.push.apply(bytes, body.readByteArray(avail));
20    }
21
22    var data = String.fromCharCode.apply(null, bytes);
23    response.bodyOutputStream.write(data, data.length);
24  }
25}
26