From 24cec21465c19db192a08fca1aea98e40b495f90 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 24 Jul 2024 12:06:24 -0400 Subject: [PATCH] Move last remnant of static file handling from core to C. --- core/core.js | 32 -------------------------------- src/httpd.js.c | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/core/core.js b/core/core.js index 78e19096..410117cd 100644 --- a/core/core.js +++ b/core/core.js @@ -8,10 +8,6 @@ let gStatsTimer = false; const k_content_security_policy = 'sandbox allow-downloads allow-top-navigation-by-user-activation'; -let k_static_files = [ - {uri: '/', path: 'index.html', type: 'text/html; charset=UTF-8'}, -]; - const k_global_settings = { index: { type: 'string', @@ -918,34 +914,6 @@ async function useAppHandler( * @returns */ async function blobHandler(request, response, blobId, uri) { - // TODO(tasiaiso): break this down ? - for (let i in k_static_files) { - if (uri === k_static_files[i].uri && k_static_files[i].path) { - let stat = await File.stat('core/' + k_static_files[i].path); - let id = `${stat.mtime}_${stat.size}`; - - if (request.headers['if-none-match'] === '"' + id + '"') { - response.writeHead(304, {'Content-Length': '0'}); - response.end(); - } else { - let data = await File.readFile('core/' + k_static_files[i].path); - response.writeHead( - 200, - Object.assign( - { - 'Content-Type': k_static_files[i].type, - 'Content-Length': data.byteLength, - etag: '"' + id + '"', - }, - k_static_files[i].headers || {} - ) - ); - response.end(data); - } - return; - } - } - if (!uri) { response.writeHead(303, { Location: diff --git a/src/httpd.js.c b/src/httpd.js.c index d29bc122..c34366de 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -905,14 +905,23 @@ static void _httpd_endpoint_static(tf_http_request_t* request) const char* file_path = NULL; for (int i = 0; i < tf_countof(k_map) && !after; i++) { - after = _after(request->path, k_map[i][0]); - file_path = k_map[i][1]; - is_core = is_core || (after && i == 0); + const char* next_after = _after(request->path, k_map[i][0]); + if (next_after) + { + after = next_after; + file_path = k_map[i][1]; + is_core = after && i == 0; + } } - if (strcmp(request->path, "/speedscope/") == 0) + if ((!after || !*after) && request->path[strlen(request->path) - 1] == '/') { after = "index.html"; + if (!file_path) + { + file_path = "core/"; + is_core = true; + } } if (!after || strstr(after, "..")) @@ -1668,6 +1677,7 @@ void tf_httpd_register(JSContext* context) tf_http_add_handler(http, "/speedscope/*", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/static/*", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/.well-known/*", _httpd_endpoint_static, NULL, task); + tf_http_add_handler(http, "/~*/*/", _httpd_endpoint_static, NULL, task); tf_http_add_handler(http, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL); tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, NULL, task);