blob: 385d43d99f8fca2d3b3b47bfbc0401754cd5fe03 (
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
|
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80 ft=javascript: */
"use strict";
const BinaryInputStream = Components.Constructor(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream"
);
Cu.importGlobalProperties(["URLSearchParams"]);
function readStream(inputStream) {
let available = 0;
let result = [];
while ((available = inputStream.available()) > 0) {
result.push(inputStream.readBytes(available));
}
return result.join("");
}
function handleRequest(request, response) {
let rv = (() => {
try {
if (request.method != "POST") {
return "ERROR: not a POST request";
}
let body = new URLSearchParams(
readStream(new BinaryInputStream(request.bodyInputStream))
);
return body.get("payload").length;
} catch (e) {
return "ERROR: Exception: " + e;
}
})();
response.setHeader("Content-Type", "text/html", false);
response.setHeader("Cache-Control", "no-cache", false);
response.write(`<!DOCTYPE HTML>
<script>
let rv = (${JSON.stringify(rv)});
opener.postMessage(rv, "*");
</script>
`);
}
|