summaryrefslogtreecommitdiffstats
path: root/dom/reporting/tests/delivering.sjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /dom/reporting/tests/delivering.sjs
parentInitial commit. (diff)
downloadthunderbird-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/reporting/tests/delivering.sjs')
-rw-r--r--dom/reporting/tests/delivering.sjs113
1 files changed, 113 insertions, 0 deletions
diff --git a/dom/reporting/tests/delivering.sjs b/dom/reporting/tests/delivering.sjs
new file mode 100644
index 0000000000..9d378a9c82
--- /dev/null
+++ b/dom/reporting/tests/delivering.sjs
@@ -0,0 +1,113 @@
+const CC = Components.Constructor;
+const BinaryInputStream = CC(
+ "@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream"
+);
+
+Cu.importGlobalProperties(["URLSearchParams"]);
+
+function handleRequest(aRequest, aResponse) {
+ var params = new URLSearchParams(aRequest.queryString);
+
+ // Report-to setter
+ if (aRequest.method == "GET" && params.get("task") == "header") {
+ let extraParams = [];
+
+ if (params.has("410")) {
+ extraParams.push("410=true");
+ }
+
+ if (params.has("worker")) {
+ extraParams.push("worker=true");
+ }
+
+ let body = {
+ max_age: 1,
+ endpoints: [
+ {
+ url:
+ "https://example.org/tests/dom/reporting/tests/delivering.sjs" +
+ (extraParams.length ? "?" + extraParams.join("&") : ""),
+ priority: 1,
+ weight: 1,
+ },
+ ],
+ };
+
+ aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
+ aResponse.setHeader("Report-to", JSON.stringify(body), false);
+ aResponse.write("OK");
+ return;
+ }
+
+ // Report check
+ if (aRequest.method == "GET" && params.get("task") == "check") {
+ aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
+
+ let reports = getState("report");
+ if (!reports) {
+ aResponse.write("");
+ return;
+ }
+
+ if (params.has("min")) {
+ let json = JSON.parse(reports);
+ if (json.length < params.get("min")) {
+ aResponse.write("");
+ return;
+ }
+ }
+
+ aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
+ aResponse.write(getState("report"));
+
+ setState("report", "");
+ return;
+ }
+
+ if (aRequest.method == "POST") {
+ var body = new BinaryInputStream(aRequest.bodyInputStream);
+
+ var avail;
+ var bytes = [];
+ while ((avail = body.available()) > 0) {
+ Array.prototype.push.apply(bytes, body.readByteArray(avail));
+ }
+
+ let reports = getState("report");
+ if (!reports) {
+ reports = [];
+ } else {
+ reports = JSON.parse(reports);
+ }
+
+ const incoming_reports = JSON.parse(String.fromCharCode.apply(null, bytes));
+ for (let report of incoming_reports) {
+ let data = {
+ contentType: aRequest.getHeader("content-type"),
+ origin: aRequest.getHeader("origin"),
+ body: report,
+ url:
+ aRequest.scheme +
+ "://" +
+ aRequest.host +
+ aRequest.path +
+ (aRequest.queryString ? "&" + aRequest.queryString : ""),
+ };
+ reports.push(data);
+ }
+
+ setState("report", JSON.stringify(reports));
+
+ if (params.has("410")) {
+ aResponse.setStatusLine(aRequest.httpVersion, 410, "Gone");
+ } else {
+ aResponse.setStatusLine(aRequest.httpVersion, 200, "OK");
+ }
+ return;
+ }
+
+ aResponse.setStatusLine(aRequest.httpVersion, 500, "Internal error");
+ aResponse.write("Invalid request");
+}