summaryrefslogtreecommitdiffstats
path: root/dom/media/webspeech/recognition/test/http_requesthandler.sjs
blob: 3400df50ec5c4befbdfefde2ec2763faf9fc910d (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const CC = Components.Constructor;

// Context structure - we need to set this up properly to pass to setObjectState
const ctx = {
  QueryInterface(iid) {
    if (iid.equals(Components.interfaces.nsISupports)) {
      return this;
    }
    throw Components.Exception("", Components.results.NS_ERROR_NO_INTERFACE);
  },
};

function setRequest(request) {
  setObjectState(key, request);
}
function getRequest() {
  let request;
  getObjectState(v => {
    request = v;
  });
  return request;
}

function handleRequest(request, response) {
  response.processAsync();
  if (request.queryString == "save") {
    // Get the context structure and finish the old request
    getObjectState("context", function (obj) {
      savedCtx = obj.wrappedJSObject;
      request = savedCtx.request;

      response.setHeader("Content-Type", "application/octet-stream", false);
      response.setHeader("Access-Control-Allow-Origin", "*", false);
      response.setHeader("Cache-Control", "no-cache", false);
      response.setStatusLine(request.httpVersion, 200, "OK");

      const input = request.bodyInputStream;
      const output = response.bodyOutputStream;
      let bodyAvail;
      while ((bodyAvail = input.available()) > 0) {
        output.writeFrom(input, bodyAvail);
      }
      response.finish();
    });
    return;
  } else if (
    request.queryString == "malformedresult=1" ||
    request.queryString == "emptyresult=1"
  ) {
    jsonOK =
      request.queryString == "malformedresult=1"
        ? '{"status":"ok","dat'
        : '{"status":"ok","data":[]}';
    response.setHeader("Content-Length", String(jsonOK.length), false);
    response.setHeader("Content-Type", "application/json", false);
    response.setHeader("Access-Control-Allow-Origin", "*", false);
    response.setHeader("Cache-Control", "no-cache", false);
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.write(jsonOK, jsonOK.length);
    response.finish();
  } else if (request.queryString == "hangup=1") {
    response.finish();
  } else if (request.queryString == "return400=1") {
    jsonOK = "{'message':'Bad header:accept-language-stt'}";
    response.setHeader("Content-Length", String(jsonOK.length), false);
    response.setHeader("Content-Type", "application/json", false);
    response.setHeader("Access-Control-Allow-Origin", "*", false);
    response.setHeader("Cache-Control", "no-cache", false);
    response.setStatusLine(request.httpVersion, 400, "Bad Request");
    response.write(jsonOK, jsonOK.length);
    response.finish();
  } else {
    ctx.wrappedJSObject = ctx;
    ctx.request = request;
    setObjectState("context", ctx);
    jsonOK = '{"status":"ok","data":[{"confidence":0.9085610,"text":"hello"}]}';
    response.setHeader("Content-Length", String(jsonOK.length), false);
    response.setHeader("Content-Type", "application/json", false);
    response.setHeader("Access-Control-Allow-Origin", "*", false);
    response.setHeader("Cache-Control", "no-cache", false);
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.write(jsonOK, jsonOK.length);
    response.finish();
  }
}