diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /dom/workers/test/server_fetch_synthetic.sjs | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/workers/test/server_fetch_synthetic.sjs')
-rw-r--r-- | dom/workers/test/server_fetch_synthetic.sjs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/dom/workers/test/server_fetch_synthetic.sjs b/dom/workers/test/server_fetch_synthetic.sjs new file mode 100644 index 0000000000..703b26d0d2 --- /dev/null +++ b/dom/workers/test/server_fetch_synthetic.sjs @@ -0,0 +1,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"); +} |