blob: 703b26d0d2ebbe5086bb0796f4e77e3920773bae (
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
|
const CC = Components.Constructor;
const BinaryInputStream = CC(
"@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream",
"setInputStream"
);
function log(str) {
//dump(`SJS LOG: ${str}\n`);
}
/**
* Given a multipart/form-data encoded string that we know to have only a single
* part, return the contents of the part. (MIME multipart encoding is too
* exciting to delve into.)
*/
function extractBlobFromMultipartFormData(text) {
const lines = text.split(/\r\n/g);
const firstBlank = lines.indexOf("");
const foo = lines.slice(firstBlank + 1, -2).join("\n");
return foo;
}
async function handleRequest(request, response) {
let blobContents = "";
if (request.method !== "POST") {
} else {
var body = new BinaryInputStream(request.bodyInputStream);
var avail;
var bytes = [];
while ((avail = body.available()) > 0) {
Array.prototype.push.apply(bytes, body.readByteArray(avail));
}
let requestBodyContents = String.fromCharCode.apply(null, bytes);
log(requestBodyContents);
blobContents = extractBlobFromMultipartFormData(requestBodyContents);
}
log("Setting Headers");
response.setHeader("Content-Type", "text/html", false);
response.setStatusLine(request.httpVersion, "200", "OK");
response.write(`<!DOCTYPE HTML><head><meta charset="utf-8"/></head><body>
<h1 id="url">${request.scheme}${request.host}${request.port}${request.path}</h1>
<div id="source">ServerJS</div>
<div id="blob">${blobContents}</div>
</body>`);
log("Done");
}
|