Skip to content

Commit 7fa3ef1

Browse files
committed
Fix custom index file name
1 parent 8f0f2ce commit 7fa3ef1

1 file changed

Lines changed: 24 additions & 10 deletions

File tree

src/webui.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ typedef struct _webui_window_t {
333333
char* url;
334334
char* public_url;
335335
const char* html;
336+
const char* user_index_file;
337+
const char* user_index_file_encoded;
336338
char* server_root_path;
337339
#ifdef _WIN32
338340
HANDLE server_thread;
@@ -8314,6 +8316,14 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
83148316
_webui_free_mem((void*)win->html);
83158317
if (win->url != NULL)
83168318
_webui_free_mem((void*)win->url);
8319+
if (win->user_index_file != NULL)
8320+
_webui_free_mem((void*)win->user_index_file);
8321+
if (win->user_index_file_encoded != NULL)
8322+
_webui_free_mem((void*)win->user_index_file_encoded);
8323+
win->html = NULL;
8324+
win->url = NULL;
8325+
win->user_index_file = NULL;
8326+
win->user_index_file_encoded = NULL;
83178327

83188328
// Get network ports
83198329
if (win->custom_server_port > 0) win->server_port = win->custom_server_port;
@@ -8358,26 +8368,22 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
83588368

83598369
// Show a window using a local folder
83608370
win->is_embedded_html = false;
8361-
win->html = NULL;
83628371

83638372
// Set window URL
83648373
window_url = win->url;
83658374
}
83668375
else {
8367-
const char* user_file = content;
83688376

83698377
// Show a window using a local file
83708378
win->is_embedded_html = false;
8371-
win->html = NULL;
8379+
win->user_index_file = content;
8380+
win->user_index_file_encoded = _webui_url_encode(content);
83728381

83738382
// Generate the URL
8374-
const char* file_url_encoded = _webui_url_encode(user_file);
8375-
size_t bf_len = (64 + _webui_strlen(file_url_encoded));
8383+
size_t bf_len = (64 + _webui_strlen(win->user_index_file_encoded));
83768384
char* url_encoded = (char*)_webui_malloc(bf_len); // [http][domain][port] [file_encoded]
83778385
WEBUI_SN_PRINTF_DYN(url_encoded, bf_len, WEBUI_HTTP_PROTOCOL "localhost:%zu/%s",
8378-
win->server_port, file_url_encoded);
8379-
_webui_free_mem((void*)file_url_encoded);
8380-
_webui_free_mem((void*)user_file);
8386+
win->server_port, win->user_index_file_encoded);
83818387

83828388
// Set window URL
83838389
window_url = url_encoded;
@@ -8471,6 +8477,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
84718477
_webui_mutex_win_is_exit_now(win, WEBUI_MUTEX_SET_TRUE);
84728478
_webui_free_mem((void*)win->html);
84738479
_webui_free_mem((void*)win->url);
8480+
_webui_free_mem((void*)win->user_index_file);
8481+
_webui_free_mem((void*)win->user_index_file_encoded);
84748482
_webui_free_port(win->server_port);
84758483
win->server_port = 0;
84768484
return false;
@@ -9358,7 +9366,10 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
93589366

93599367
// Looking for index file and redirect
93609368

9361-
const char* index_files[] = {"index.ts", "index.js", "index.html", "index.htm"};
9369+
const char* index_files[] = {
9370+
win->user_index_file, // User-defined index file
9371+
"index.ts", "index.js", "index.html", "index.htm"
9372+
};
93629373

93639374
// [Path][Sep][File Name]
93649375
size_t bf_len = (_webui_strlen(win->server_root_path) + 1 + 24);
@@ -9466,7 +9477,10 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
94669477

94679478
// Looking for index file and redirect
94689479

9469-
const char* index_files[] = {"index.ts", "index.js", "index.html", "index.htm"};
9480+
const char* index_files[] = {
9481+
// win->user_index_file, TODO: Do we need to disable user index file for subfolders?
9482+
"index.ts", "index.js", "index.html", "index.htm"
9483+
};
94709484

94719485
// [Path][Sep][File Name]
94729486
size_t bf_len = (_webui_strlen(folder_path) + 1 + 24);

0 commit comments

Comments
 (0)