Skip to content

Commit bbcdb9c

Browse files
committed
Modify _webui_http_handler() to handle a file before a path
1 parent de95086 commit bbcdb9c

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

src/webui.c

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7958,7 +7958,45 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
79587958
// [Path][Sep][folder]
79597959
char* folder_path = _webui_get_full_path(win, url);
79607960

7961-
if (_webui_folder_exist(folder_path)) {
7961+
if (_webui_file_exist(folder_path)) {
7962+
7963+
// [/file]
7964+
7965+
bool script = false;
7966+
if (win->runtime != None) {
7967+
const char* extension = _webui_get_extension(url);
7968+
const char* index_extensions[] = {
7969+
"js", "ts"
7970+
};
7971+
for (size_t i = 0; i < (sizeof(index_extensions) / sizeof(index_extensions[0])); i++) {
7972+
if (strcmp(extension, index_extensions[i]) == 0) {
7973+
script = true;
7974+
break;
7975+
}
7976+
}
7977+
}
7978+
7979+
if (script) {
7980+
7981+
#ifdef WEBUI_LOG
7982+
printf("[Core]\t\t_webui_http_handler() -> Interpret local script file\n");
7983+
#endif
7984+
7985+
// Serve as a script file to be interpreted by
7986+
// an external interpreter (Deno, Bun, Nodejs)
7987+
http_status_code = _webui_interpret_file(win, client, NULL, client_id);
7988+
}
7989+
else {
7990+
7991+
#ifdef WEBUI_LOG
7992+
printf("[Core]\t\t_webui_http_handler() -> Local file\n");
7993+
#endif
7994+
7995+
// Serve as a normal text-based file
7996+
http_status_code = _webui_serve_file(win, client, client_id);
7997+
}
7998+
}
7999+
else if (_webui_folder_exist(folder_path)) {
79628000

79638001
// [/folder]
79648002

@@ -7995,42 +8033,16 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
79958033
http_status_code = 404;
79968034
}
79978035
else {
7998-
7999-
// [/file]
8000-
8001-
bool script = false;
8002-
if (win->runtime != None) {
8003-
const char* extension = _webui_get_extension(url);
8004-
const char* index_extensions[] = {
8005-
"js", "ts"
8006-
};
8007-
for (size_t i = 0; i < (sizeof(index_extensions) / sizeof(index_extensions[0])); i++) {
8008-
if (strcmp(extension, index_extensions[i]) == 0) {
8009-
script = true;
8010-
break;
8011-
}
8012-
}
8013-
}
8014-
8015-
if (script) {
8016-
8017-
#ifdef WEBUI_LOG
8018-
printf("[Core]\t\t_webui_http_handler() -> Interpret local script file\n");
8019-
#endif
8020-
8021-
// Serve as a script file to be interpreted by
8022-
// an external interpreter (Deno, Bun, Nodejs)
8023-
http_status_code = _webui_interpret_file(win, client, NULL, client_id);
8024-
}
8025-
else {
8026-
8027-
#ifdef WEBUI_LOG
8028-
printf("[Core]\t\t_webui_http_handler() -> Local file\n");
8029-
#endif
8030-
8031-
// Serve as a normal text-based file
8032-
http_status_code = _webui_serve_file(win, client, client_id);
8033-
}
8036+
8037+
// [invalid]
8038+
8039+
#ifdef WEBUI_LOG
8040+
printf("[Core]\t\t_webui_http_handler() -> Not found\n");
8041+
#endif
8042+
8043+
// No file or folder is found at this path
8044+
http_status_code = _webui_serve_file(win, client, client_id);
8045+
http_status_code = 404;
80348046
}
80358047

80368048
// Clear

0 commit comments

Comments
 (0)