staticDirectoryHandler => C.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4835 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2024-02-10 03:14:02 +00:00
parent 14de3dd9e5
commit 6801758cb3
2 changed files with 44 additions and 49 deletions

View File

@ -517,7 +517,7 @@ static const char* _after(const char* text, const char* prefix)
{
return text + prefix_length;
}
return text;
return NULL;
}
static double _time_spec_to_double(const uv_timespec_t* time_spec)
@ -628,28 +628,55 @@ static void _httpd_endpoint_static(tf_http_request_t* request)
"w3.css",
};
tf_task_t* task = request->user_data;
const char* after = _after(request->path, "/static/");
bool found = false;
for (int i = 0; i < tf_countof(k_static_files); i++)
const char* k_map[][2] =
{
if (strcmp(after, k_static_files[i]) == 0)
{
found = true;
break;
}
{ "/static/", "core/" },
{ "/lit/", "deps/lit/" },
{ "/codemirror/", "deps/codemirror/" },
{ "/speedscope/", "deps/speedscope/" },
};
bool is_core = false;
const char* after = NULL;
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);
}
if (!found)
if (!after || strstr(after, ".."))
{
const char* k_payload = tf_http_status_text(404);
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
return;
}
size_t size = strlen("core/") + strlen(after) + 1;
if (is_core)
{
bool found = false;
for (int i = 0; i < tf_countof(k_static_files); i++)
{
if (strcmp(after, k_static_files[i]) == 0)
{
found = true;
break;
}
}
if (!found)
{
const char* k_payload = tf_http_status_text(404);
tf_http_respond(request, 404, NULL, 0, k_payload, strlen(k_payload));
return;
}
}
tf_task_t* task = request->user_data;
size_t size = strlen(file_path) + strlen(after) + 1;
char* path = alloca(size);
snprintf(path, size, "core/%s", after);
snprintf(path, size, "%s%s", file_path, after);
tf_http_request_ref(request);
tf_file_stat(task, path, _httpd_endpoint_static_stat, request);
}
@ -717,7 +744,11 @@ void tf_httpd_register(JSContext* context)
tf_http_set_trace(http, tf_task_get_trace(task));
JS_SetOpaque(httpd, http);
tf_http_add_handler(http, "/codemirror/", _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, "/static/", _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);
tf_http_add_handler(http, "/disconnections", _httpd_endpoint_disconnections, NULL, task);