Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / media / webspeech / recognition / test / http_requesthandler.sjs
blobe79662b850b45b6883b1c4a6ec2af5e108568299
1 const CC = Components.Constructor;
3 // Context structure - we need to set this up properly to pass to setObjectState
4 const ctx = {
5   QueryInterface(iid) {
6     if (iid.equals(Ci.nsISupports)) {
7       return this;
8     }
9     throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
10   },
13 function setRequest(request) {
14   setObjectState(key, request);
16 function getRequest() {
17   let request;
18   getObjectState(v => {
19     request = v;
20   });
21   return request;
24 function handleRequest(request, response) {
25   response.processAsync();
26   if (request.queryString == "save") {
27     // Get the context structure and finish the old request
28     getObjectState("context", function (obj) {
29       savedCtx = obj.wrappedJSObject;
30       request = savedCtx.request;
32       response.setHeader("Content-Type", "application/octet-stream", false);
33       response.setHeader("Access-Control-Allow-Origin", "*", false);
34       response.setHeader("Cache-Control", "no-cache", false);
35       response.setStatusLine(request.httpVersion, 200, "OK");
37       const input = request.bodyInputStream;
38       const output = response.bodyOutputStream;
39       let bodyAvail;
40       while ((bodyAvail = input.available()) > 0) {
41         output.writeFrom(input, bodyAvail);
42       }
43       response.finish();
44     });
45     return;
46   }
48   if (
49     request.queryString == "malformedresult=1" ||
50     request.queryString == "emptyresult=1"
51   ) {
52     jsonOK =
53       request.queryString == "malformedresult=1"
54         ? '{"status":"ok","dat'
55         : '{"status":"ok","data":[]}';
56     response.setHeader("Content-Length", String(jsonOK.length), false);
57     response.setHeader("Content-Type", "application/json", false);
58     response.setHeader("Access-Control-Allow-Origin", "*", false);
59     response.setHeader("Cache-Control", "no-cache", false);
60     response.setStatusLine(request.httpVersion, 200, "OK");
61     response.write(jsonOK, jsonOK.length);
62     response.finish();
63   } else if (request.queryString == "hangup=1") {
64     response.finish();
65   } else if (request.queryString == "return400=1") {
66     jsonOK = "{'message':'Bad header:accept-language-stt'}";
67     response.setHeader("Content-Length", String(jsonOK.length), false);
68     response.setHeader("Content-Type", "application/json", false);
69     response.setHeader("Access-Control-Allow-Origin", "*", false);
70     response.setHeader("Cache-Control", "no-cache", false);
71     response.setStatusLine(request.httpVersion, 400, "Bad Request");
72     response.write(jsonOK, jsonOK.length);
73     response.finish();
74   } else {
75     ctx.wrappedJSObject = ctx;
76     ctx.request = request;
77     setObjectState("context", ctx);
78     jsonOK = '{"status":"ok","data":[{"confidence":0.9085610,"text":"hello"}]}';
79     response.setHeader("Content-Length", String(jsonOK.length), false);
80     response.setHeader("Content-Type", "application/json", false);
81     response.setHeader("Access-Control-Allow-Origin", "*", false);
82     response.setHeader("Cache-Control", "no-cache", false);
83     response.setStatusLine(request.httpVersion, 200, "OK");
84     response.write(jsonOK, jsonOK.length);
85     response.finish();
86   }