From 51a327c52d3ced2eeaa32182d1567ff6f2966529 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 10 Feb 2024 16:10:58 +0000 Subject: [PATCH] .well-known => C. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4837 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/core.js | 13 ------------- src/httpd.js.c | 31 ++++++++++++++++++++----------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/core/core.js b/core/core.js index b867f867..10f80d58 100644 --- a/core/core.js +++ b/core/core.js @@ -556,17 +556,6 @@ function startsWithBytes(data, bytes) { } } -async function wellKnownHandler(request, response, path) { - let data = await File.readFile("data/global/.well-known/" + path); - if (data) { - response.writeHead(200, {"Content-Type": "text/plain", "Content-Length": data.length}); - response.end(data); - } else { - response.writeHead(404, {"Content-Type": "text/plain", "Content-Length": "File not found".length}); - response.end("File not found"); - } -} - function guessTypeFromName(path) { let extension = path.split('.').pop(); return k_mime_types[extension]; @@ -940,8 +929,6 @@ loadSettings().then(function() { return blobHandler(request, response, match[1], match[2]); } else if (match = /^(.*)(\/(?:save|delete)?)$/.exec(request.uri)) { return blobHandler(request, response, match[1], match[2]); - } else if ((match = /^\/.well-known\/(.*)/.exec(request.uri)) && request.uri.indexOf("..") == -1) { - return wellKnownHandler(request, response, match[1]); } else { let data = "File not found."; response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()}); diff --git a/src/httpd.js.c b/src/httpd.js.c index 6e5b134e..4dc87dfd 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -2,7 +2,6 @@ #include "file.js.h" #include "http.h" -#include "log.h" #include "mem.h" #include "task.h" #include "tlscontext.js.h" @@ -581,7 +580,6 @@ static void _httpd_endpoint_static_read(tf_task_t* task, const char* path, int r } else { - tf_printf("404 %s\n", path); const char* k_payload = tf_http_status_text(404); tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); } @@ -593,20 +591,29 @@ static void _httpd_endpoint_static_stat(tf_task_t* task, const char* path, int r { tf_http_request_t* request = user_data; const char* match = tf_http_request_get_header(request, "if-none-match"); - char etag[512]; - snprintf(etag, sizeof(etag), "\"%f_%zd\"", _time_spec_to_double(&stat->st_mtim), (size_t)stat->st_size); - if (match && strcmp(match, etag) == 0) + if (result != 0) { - tf_http_respond(request, 304, NULL, 0, NULL, 0); + const char* k_payload = tf_http_status_text(404); + tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); tf_http_request_unref(request); } else { - http_file_t* file = tf_malloc(sizeof(http_file_t)); - *file = (http_file_t) { .request = request }; - static_assert(sizeof(file->etag) == sizeof(etag), "Size mismatch"); - memcpy(file->etag, etag, sizeof(etag)); - tf_file_read(task, path, _httpd_endpoint_static_read, file); + char etag[512]; + snprintf(etag, sizeof(etag), "\"%f_%zd\"", _time_spec_to_double(&stat->st_mtim), (size_t)stat->st_size); + if (match && strcmp(match, etag) == 0) + { + tf_http_respond(request, 304, NULL, 0, NULL, 0); + tf_http_request_unref(request); + } + else + { + http_file_t* file = tf_malloc(sizeof(http_file_t)); + *file = (http_file_t) { .request = request }; + static_assert(sizeof(file->etag) == sizeof(etag), "Size mismatch"); + memcpy(file->etag, etag, sizeof(etag)); + tf_file_read(task, path, _httpd_endpoint_static_read, file); + } } } @@ -634,6 +641,7 @@ static void _httpd_endpoint_static(tf_http_request_t* request) { "/lit/", "deps/lit/" }, { "/codemirror/", "deps/codemirror/" }, { "/speedscope/", "deps/speedscope/" }, + { "/.well-known/", "data/global/.well-known/" }, }; bool is_core = false; @@ -748,6 +756,7 @@ void tf_httpd_register(JSContext* context) tf_http_add_handler(http, "/lit/", _httpd_endpoint_static, NULL, task); 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, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL); tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, NULL, task);