.well-known => C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4837 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2024-02-10 16:10:58 +00:00
parent 5a978bb30d
commit 51a327c52d
2 changed files with 20 additions and 24 deletions

View File

@ -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) { function guessTypeFromName(path) {
let extension = path.split('.').pop(); let extension = path.split('.').pop();
return k_mime_types[extension]; return k_mime_types[extension];
@ -940,8 +929,6 @@ loadSettings().then(function() {
return blobHandler(request, response, match[1], match[2]); return blobHandler(request, response, match[1], match[2]);
} else if (match = /^(.*)(\/(?:save|delete)?)$/.exec(request.uri)) { } else if (match = /^(.*)(\/(?:save|delete)?)$/.exec(request.uri)) {
return blobHandler(request, response, match[1], match[2]); 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 { } else {
let data = "File not found."; let data = "File not found.";
response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()}); response.writeHead(404, {"Content-Type": "text/plain; charset=utf-8", "Content-Length": data.length.toString()});

View File

@ -2,7 +2,6 @@
#include "file.js.h" #include "file.js.h"
#include "http.h" #include "http.h"
#include "log.h"
#include "mem.h" #include "mem.h"
#include "task.h" #include "task.h"
#include "tlscontext.js.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 else
{ {
tf_printf("404 %s\n", path);
const char* k_payload = tf_http_status_text(404); const char* k_payload = tf_http_status_text(404);
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload)); 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; tf_http_request_t* request = user_data;
const char* match = tf_http_request_get_header(request, "if-none-match"); const char* match = tf_http_request_get_header(request, "if-none-match");
char etag[512]; if (result != 0)
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); 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); tf_http_request_unref(request);
} }
else else
{ {
http_file_t* file = tf_malloc(sizeof(http_file_t)); char etag[512];
*file = (http_file_t) { .request = request }; snprintf(etag, sizeof(etag), "\"%f_%zd\"", _time_spec_to_double(&stat->st_mtim), (size_t)stat->st_size);
static_assert(sizeof(file->etag) == sizeof(etag), "Size mismatch"); if (match && strcmp(match, etag) == 0)
memcpy(file->etag, etag, sizeof(etag)); {
tf_file_read(task, path, _httpd_endpoint_static_read, file); 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/" }, { "/lit/", "deps/lit/" },
{ "/codemirror/", "deps/codemirror/" }, { "/codemirror/", "deps/codemirror/" },
{ "/speedscope/", "deps/speedscope/" }, { "/speedscope/", "deps/speedscope/" },
{ "/.well-known/", "data/global/.well-known/" },
}; };
bool is_core = false; 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, "/lit/", _httpd_endpoint_static, NULL, task);
tf_http_add_handler(http, "/speedscope/", _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, "/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, "/robots.txt", _httpd_endpoint_robots_txt, NULL, NULL);
tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, NULL, task); tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, NULL, task);