From 478e96fc5f10b01aa9d598568922ee5efe46fe42 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 17 May 2023 18:35:58 +0000 Subject: [PATCH] Just moving HTTP code around. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4303 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/core.js | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/core/core.js b/core/core.js index a9359d01..a55f2c9b 100644 --- a/core/core.js +++ b/core/core.js @@ -811,6 +811,16 @@ function enableStats(process, enabled) { } } +function stringResponse(response, data) { + let bytes = utf8Encode(data); + response.writeHead(200, { + "Content-Type": "application/json; charset=utf-8", + "Content-Length": bytes.byteLength.toString(), + "Access-Control-Allow-Origin": "*", + }); + return response.end(bytes); +} + loadSettings().then(function() { httpd.all("/login", auth.handler); httpd.all("", function(request, response) { @@ -824,8 +834,6 @@ loadSettings().then(function() { return blobHandler(request, response, match[1], match[2]); } else if (match = /^\/static\/lit\/([\.\w-/]*)$/.exec(request.uri)) { return staticDirectoryHandler(request, response, 'deps/lit/', match[1]); - } else if (match = /^\/static(\/.*)/.exec(request.uri)) { - return staticFileHandler(request, response, null, match[1]); } else if (match = /^\/codemirror\/([\.\w-/]*)$/.exec(request.uri)) { return staticDirectoryHandler(request, response, 'deps/codemirror/', match[1]); } else if (match = /^\/speedscope\/([\.\w-/]*)$/.exec(request.uri)) { @@ -834,34 +842,22 @@ loadSettings().then(function() { return staticDirectoryHandler(request, response, 'deps/split/', match[1]); } else if (match = /^\/smoothie\/([\.\w-/]*)$/.exec(request.uri)) { return staticDirectoryHandler(request, response, 'deps/smoothie/', match[1]); + } else if (match = /^\/static(\/.*)/.exec(request.uri)) { + return staticFileHandler(request, response, null, match[1]); + } else if (request.uri == "/robots.txt") { + return staticFileHandler(request, response, null, request.uri); } else if (match = /^(.*)(\/(?:save|delete)?)$/.exec(request.uri)) { return blobHandler(request, response, match[1], match[2]); } else if (match = /^\/trace$/.exec(request.uri)) { - let data = trace(); - response.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()}); - return response.end(data); + return stringResponse(response, trace()); } else if (match = /^\/disconnections$/.exec(request.uri)) { - let data = utf8Encode(JSON.stringify(disconnectionsDebug(), null, 2)); - response.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.byteLength.toString()}); - return response.end(data); + return stringResponse(response, JSON.stringify(disconnectionsDebug(), null, 2)); } else if (match = /^\/debug$/.exec(request.uri)) { - let data = JSON.stringify(getDebug(), null, 2); - response.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()}); - return response.end(data); + return stringResponse(response, JSON.stringify(getDebug(), null, 2)); } else if (match = /^\/hitches$/.exec(request.uri)) { - let data = JSON.stringify(getHitches(), null, 2); - response.writeHead(200, {"Content-Type": "application/json; charset=utf-8", "Content-Length": data.length.toString()}); - return response.end(data); + return stringResponse(response, JSON.stringify(getHitches(), null, 2)); } else if (match = /^\/mem$/.exec(request.uri)) { - let data = JSON.stringify(getAllocations(), null, 2); - response.writeHead(200, { - "Content-Type": "application/json; charset=utf-8", - "Content-Length": data.length.toString(), - "Access-Control-Allow-Origin": "*", - }); - return response.end(data); - } else if (request.uri == "/robots.txt") { - return blobHandler(request, response, null, request.uri); + return stringResponse(response, JSON.stringify(getAllocations(), null, 2)); } else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) { return wellKnownHandler(request, response, match[1]); } else {