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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const CC = Components.Constructor;
const BinaryInputStream = CC("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream");
function DEBUG(str)
{
// dump("********** " + str + "\n");
}
function setOurState(data) {
x = { data: data, QueryInterface: function(iid) { return this } };
x.wrappedJSObject = x;
setObjectState("beacon-handler", x);
DEBUG("our state is " + data);
}
function getOurState() {
var data;
getObjectState("beacon-handler", function(x) {
// x can be null if no one has set any state yet
if (x) {
data = x.wrappedJSObject.data;
}
});
return data;
}
function handleRequest(request, response) {
DEBUG("Entered request handler");
response.setHeader("Cache-Control", "no-cache", false);
function finishControlResponse(response) {
DEBUG("********* sending out the control GET response");
var data = getState("beaconData");
var mimetype = getState("beaconMimetype");
DEBUG("GET was sending : " + data + "\n");
DEBUG("GET was sending : " + mimetype + "\n");
var result = {
"data": data,
"mimetype": mimetype,
};
response.write(JSON.stringify(result));
setOurState(null);
}
if (request.method == "GET") {
DEBUG(" ------------ GET --------------- ");
response.setHeader("Content-Type", "application/json", false);
switch (request.queryString) {
case "getLastBeacon":
var state = getOurState();
if (state === "unblocked") {
finishControlResponse(response);
} else {
DEBUG("GET has arrived, but POST has not, blocking response!");
setOurState(response);
response.processAsync();
}
break;
default:
response.setStatusLine(request.httpVersion, 400, "Bad Request");
break;
}
return;
}
if (request.method == "POST") {
DEBUG(" ------------ POST --------------- ");
var body = new BinaryInputStream(request.bodyInputStream);
var avail;
var bytes = [];
while ((avail = body.available()) > 0) {
Array.prototype.push.apply(bytes, body.readByteArray(avail));
}
var data = "";
for (var i=0; i < bytes.length; i++) {
// We are only passing strings at this point.
if (bytes[i] < 32) continue;
var charcode = String.fromCharCode(bytes[i]);
data += charcode;
}
var mimetype = request.getHeader("Content-Type");
// check to see if this is form data.
if (mimetype.indexOf("multipart/form-data") != -1) {
// trim the mime type to make testing easier.
mimetype = "multipart/form-data";
// Extract only the form-data name.
var pattern = /; name=\"(.+)\";/;
data = data.split(pattern)[1];
}
DEBUG("********** POST was sending : " + data + "\n");
DEBUG("********** POST was sending : " + mimetype + "\n");
setState("beaconData", data);
setState("beaconMimetype", mimetype);
response.setHeader("Content-Type", "text/plain", false);
response.write('ok');
var blockedResponse = getOurState();
if (typeof(blockedResponse) == "object" && blockedResponse) {
DEBUG("GET is already pending, finishing!");
finishControlResponse(blockedResponse);
blockedResponse.finish();
} else {
DEBUG("GET has not arrived, marking it as unblocked");
setOurState("unblocked");
}
return;
}
response.setStatusLine(request.httpVersion, 402, "Bad Request");
}
|