summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/progressserver.sjs
blob: 945f79557544030977971049bc7ce9edd918bdd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
                             "nsIBinaryInputStream",
                             "setInputStream");

function setReq(req) {
  setObjectState("dom/xhr/tests/progressserver", req);
}

function getReq() {
  var req;
  getObjectState("dom/xhr/tests/progressserver", function(v) {
    req = v;
  });
  return req;
}

function handleRequest(request, response)
{
  var pairs = request.queryString.split('&');
  var command = pairs.shift();
  dump("received '" + command + "' command\n");

  var bodyStream = new BinaryInputStream(request.bodyInputStream);
  var body = "";
  var bodyAvail;
  while ((bodyAvail = bodyStream.available()) > 0)
    body += String.fromCharCode.apply(null, bodyStream.readByteArray(bodyAvail));

  if (command == "open") {
    response.processAsync();
    setReq(response);

    response.setHeader("Cache-Control", "no-cache", false);
    pairs.forEach(function (val) {
      var [name, value] = val.split('=');
      response.setHeader(name, unescape(value), false);
    });
    response.write(body);
    return;
  }

  if (command == "send") {
    getReq().write(body);
  }
  else if (command == "close") {
    getReq().finish();
    setReq(null);
  }
  response.setHeader("Content-Type", "text/plain");
  response.write("ok");
}