Skip to content

Commit a868e13

Browse files
committed
fix: make explicit server entry files strict on not found
1 parent f960f88 commit a868e13

5 files changed

Lines changed: 41 additions & 22 deletions

File tree

examples/C++/test_index_redirect/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
//
1313
// The expected server behavior is:
1414
// - start_server("custom.html") prefers custom.html in every directory
15-
// - if custom.html is missing, fallback probes index.html, index.htm,
16-
// index.ts, then index.js
17-
// - start_server() or start_server("") means "fallback mode"
15+
// - an explicit filename is strict; if it does not exist, the request fails
16+
// - start_server() or start_server("") means "fallback mode", which probes
17+
// index.html, index.htm, index.ts, then index.js
1818
//
1919
// Note: index.ts and index.js can still be valid fallback targets for
2020
// runtime/script-first scenarios, but they are not equivalent to an HTML GUI

examples/C/test_index_redirect/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//
1616
// The expected server behavior is:
1717
// - start_server("custom.html") prefers custom.html in every directory
18-
// - if custom.html is missing, fallback probes index.html, index.htm,
19-
// index.ts, then index.js
20-
// - webui_start_server(window, "") means "fallback mode"
18+
// - an explicit filename is strict; if it does not exist, the request fails
19+
// - webui_start_server(window, "") means "fallback mode", which probes
20+
// index.html, index.htm, index.ts, then index.js
2121
//
2222
// Note: index.ts and index.js can still be valid fallback targets for
2323
// runtime/script-first scenarios, but they are not equivalent to an HTML GUI

include/webui.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,13 @@ WEBUI_EXPORT bool webui_show_client(webui_event_t* e, const char* content);
361361
WEBUI_EXPORT bool webui_show_browser(size_t window, const char* content, size_t browser);
362362

363363
/**
364-
* @brief Same as `webui_show()`. But start only the web server and return the URL.
364+
* @brief Start only the local web server and return the URL.
365365
* No window will be shown.
366366
*
367367
* @param window The window number
368-
* @param content The HTML, Or a local file
368+
* @param content The local file or local folder to serve.
369+
* Pass `NULL` or an empty string to start in folder mode using the
370+
* current root folder and index fallback logic.
369371
*
370372
* @return Returns the url of this window server.
371373
*

include/webui.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ namespace webui {
421421
webui_set_center(webui_window);
422422
}
423423

424-
// Same as `webui_show()`. But start only the web server and return the URL. No window will be shown.
424+
// Start only the local web server and return the URL.
425+
// Empty content means: use current root folder + index fallback.
425426
std::string_view start_server(const std::string_view content = "") const {
426427
return std::string_view{webui_start_server(webui_window, content.data())};
427428
}

src/webui.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ typedef struct _webui_window_t {
336336
const char* html;
337337
const char* user_index_file;
338338
const char* user_index_file_encoded;
339+
bool allow_index_fallback;
339340
char* server_root_path;
340341
#ifdef _WIN32
341342
HANDLE server_thread;
@@ -5425,10 +5426,12 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
54255426
}
54265427
}
54275428

5428-
index_files[index_files_len++] = "index.html";
5429-
index_files[index_files_len++] = "index.htm";
5430-
index_files[index_files_len++] = "index.ts";
5431-
index_files[index_files_len++] = "index.js";
5429+
if (win->allow_index_fallback) {
5430+
index_files[index_files_len++] = "index.html";
5431+
index_files[index_files_len++] = "index.htm";
5432+
index_files[index_files_len++] = "index.ts";
5433+
index_files[index_files_len++] = "index.js";
5434+
}
54325435

54335436
char base_url[WEBUI_MAX_PATH];
54345437
if (strcmp(url, "/") == 0) {
@@ -8860,6 +8863,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
88608863
if (!keep_user_index_file) {
88618864
win->user_index_file = NULL;
88628865
win->user_index_file_encoded = NULL;
8866+
win->allow_index_fallback = false;
88638867
}
88648868

88658869
// Get network ports
@@ -8924,6 +8928,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
89248928

89258929
// Show a window using a local folder
89268930
win->is_embedded_html = false;
8931+
win->allow_index_fallback = true;
89278932

89288933
// Set window URL
89298934
window_url = win->url;
@@ -8934,6 +8939,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
89348939
win->is_embedded_html = false;
89358940
win->user_index_file = content;
89368941
win->user_index_file_encoded = _webui_url_encode(content);
8942+
win->allow_index_fallback = false;
89378943

89388944
// Generate the URL
89398945
size_t bf_len = (64 + _webui_strlen(win->user_index_file_encoded));
@@ -9944,15 +9950,23 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
99449950

99459951
// Looking for index file and redirect
99469952

9947-
const char* index_files[] = {
9948-
win->user_index_file, // User-defined index file
9949-
"index.html", "index.htm", "index.ts", "index.js"
9950-
};
9953+
const char* index_files[5];
9954+
size_t index_files_len = 0;
9955+
9956+
if (!_webui_is_empty(win->user_index_file)) {
9957+
index_files[index_files_len++] = win->user_index_file;
9958+
}
9959+
if (win->allow_index_fallback) {
9960+
index_files[index_files_len++] = "index.html";
9961+
index_files[index_files_len++] = "index.htm";
9962+
index_files[index_files_len++] = "index.ts";
9963+
index_files[index_files_len++] = "index.js";
9964+
}
99519965

99529966
// [Path][Sep][File Name]
99539967
size_t bf_len = (_webui_strlen(win->server_root_path) + 1 + 24);
99549968
char* index_path = (char*)_webui_malloc(bf_len);
9955-
for (size_t i = 0; i < (sizeof(index_files) / sizeof(index_files[0])); i++) {
9969+
for (size_t i = 0; i < index_files_len; i++) {
99569970
WEBUI_SN_PRINTF_DYN(index_path, bf_len, "%s%s%s",
99579971
win->server_root_path, os_sep, index_files[i]
99589972
);
@@ -10072,10 +10086,12 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
1007210086
}
1007310087
}
1007410088

10075-
index_files[index_files_len++] = "index.html";
10076-
index_files[index_files_len++] = "index.htm";
10077-
index_files[index_files_len++] = "index.ts";
10078-
index_files[index_files_len++] = "index.js";
10089+
if (win->allow_index_fallback) {
10090+
index_files[index_files_len++] = "index.html";
10091+
index_files[index_files_len++] = "index.htm";
10092+
index_files[index_files_len++] = "index.ts";
10093+
index_files[index_files_len++] = "index.js";
10094+
}
1007910095

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

0 commit comments

Comments
 (0)