Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const CC = Components.Constructor;
8 const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
9 "nsIBinaryInputStream",
10 "setInputStream");
12 function DEBUG(str)
13 {
14 // dump("********** " + str + "\n");
15 }
17 function setOurState(data) {
18 x = { data: data, QueryInterface: function(iid) { return this } };
19 x.wrappedJSObject = x;
20 setObjectState("beacon-handler", x);
21 DEBUG("our state is " + data);
22 }
24 function getOurState() {
25 var data;
26 getObjectState("beacon-handler", function(x) {
27 // x can be null if no one has set any state yet
28 if (x) {
29 data = x.wrappedJSObject.data;
30 }
31 });
32 return data;
33 }
35 function handleRequest(request, response) {
36 DEBUG("Entered request handler");
37 response.setHeader("Cache-Control", "no-cache", false);
39 function finishControlResponse(response) {
40 DEBUG("********* sending out the control GET response");
41 var data = getState("beaconData");
42 var mimetype = getState("beaconMimetype");
43 DEBUG("GET was sending : " + data + "\n");
44 DEBUG("GET was sending : " + mimetype + "\n");
45 var result = {
46 "data": data,
47 "mimetype": mimetype,
48 };
49 response.write(JSON.stringify(result));
50 setOurState(null);
51 }
53 if (request.method == "GET") {
54 DEBUG(" ------------ GET --------------- ");
55 response.setHeader("Content-Type", "application/json", false);
56 switch (request.queryString) {
57 case "getLastBeacon":
58 var state = getOurState();
59 if (state === "unblocked") {
60 finishControlResponse(response);
61 } else {
62 DEBUG("GET has arrived, but POST has not, blocking response!");
63 setOurState(response);
64 response.processAsync();
65 }
66 break;
67 default:
68 response.setStatusLine(request.httpVersion, 400, "Bad Request");
69 break;
70 }
71 return;
72 }
74 if (request.method == "POST") {
75 DEBUG(" ------------ POST --------------- ");
76 var body = new BinaryInputStream(request.bodyInputStream);
77 var avail;
78 var bytes = [];
80 while ((avail = body.available()) > 0) {
81 Array.prototype.push.apply(bytes, body.readByteArray(avail));
82 }
84 var data = "";
85 for (var i=0; i < bytes.length; i++) {
86 // We are only passing strings at this point.
87 if (bytes[i] < 32) continue;
88 var charcode = String.fromCharCode(bytes[i]);
89 data += charcode;
90 }
92 var mimetype = request.getHeader("Content-Type");
94 // check to see if this is form data.
95 if (mimetype.indexOf("multipart/form-data") != -1) {
97 // trim the mime type to make testing easier.
98 mimetype = "multipart/form-data";
99 // Extract only the form-data name.
101 var pattern = /; name=\"(.+)\";/;
102 data = data.split(pattern)[1];
103 }
105 DEBUG("********** POST was sending : " + data + "\n");
106 DEBUG("********** POST was sending : " + mimetype + "\n");
107 setState("beaconData", data);
108 setState("beaconMimetype", mimetype);
110 response.setHeader("Content-Type", "text/plain", false);
111 response.write('ok');
113 var blockedResponse = getOurState();
114 if (typeof(blockedResponse) == "object" && blockedResponse) {
115 DEBUG("GET is already pending, finishing!");
116 finishControlResponse(blockedResponse);
117 blockedResponse.finish();
118 } else {
119 DEBUG("GET has not arrived, marking it as unblocked");
120 setOurState("unblocked");
121 }
123 return;
124 }
126 response.setStatusLine(request.httpVersion, 402, "Bad Request");
127 }