Skip to content

start_server() ignores user_index_file when using VFS file handler; setting vfs.py custom index feels like a workaround #679

@triuk

Description

@triuk

Hi, this issue is basically continuation of
Originally posted by @triuk in #673
which addressed the custom index file behavior, but I think the solution is not ideal. I’ll try to explain why in this new issue.
I’m really excited about this project — it’s the best (easy and universal) GUI solution I’ve found. I can build one UI (HTML/CSS/JS) and use it across languages without any changes. I hope my amateur feedback helps make it even better.

Summary

When using set_file_handler() (VFS) and calling start_server("test.html"), the server still serves / as index.html. The user_index_file set in start_server() is ignored for VFS requests to /. The current solution is passing a custom index into vfs.py argument, which feels redundant and hardcoded.

Expected behavior

start_server("test.html") should define the default index for / regardless of whether files are served from disk or via VFS. VFS should stay a generic filesystem, without hardcoded index behavior. The selection of index should happen in the server logic, not in the VFS generator.

Actual behavior

  • / resolves to index.* even when start_server("test.html") is used
  • user_index_file is ignored by the VFS handler
  • The only way to override / with VFS is to hardcode a custom index in the vfs.py argument

Why this seems wrong

start_server() already accepts the desired start file in the form of a string. In the current behavior, this argument is effectively ignored when VFS is used, and the current solution is to bake the custom index into vfs.h using the custom index as vfs.py argument. That makes VFS non-generic and duplicates responsibility.

Possible fix

Respect user_index_file in the HTTP handler / external file handler when URL is /. This makes start_server() consistent across backends.
Also, with this change, the VFS index_files[] entry for / becomes redundant, since you always specify the string in start_server() anyway.

After making this change in _webui_external_file_handler, the server correctly served test.html when accessing [IP]:[PORT].
The suggested solution (or something similar):

diff --git a/src/webui.c b/src/webui.c
index dce5b32..86f2362 100644
--- a/src/webui.c
+++ b/src/webui.c
@@ -5175,6 +5175,17 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
     int http_status_code = 0;
     const struct mg_request_info * ri = mg_get_request_info(client);
     const char* url = ri->local_uri;
+    const char* handler_url = url;
+    char user_index_url[WEBUI_MAX_PATH];
+    // Respect user-defined index file when using an external file handler.
+    if (strcmp(url, "/") == 0 && !_webui_is_empty(win->user_index_file)) {
+        if (win->user_index_file[0] == '/') {
+            handler_url = win->user_index_file;
+        } else {
+            WEBUI_SN_PRINTF_STATIC(user_index_url, WEBUI_MAX_PATH, "/%s", win->user_index_file);
+            handler_url = user_index_url;
+        }
+    }
 
     if (win->files_handler != NULL || win->files_handler_window != NULL) {
@@ -5200,7 +5211,7 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
         if(win->files_handler_window) {
             pt = win->files_handler_window;
         }        
-        _webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Path [%s]\n", url);
+        _webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Path [%s]\n", handler_url);
@@ -5215,9 +5226,9 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
         const void* callback_resp = NULL;
         if (win->files_handler_window != NULL) {
-            callback_resp = win->files_handler_window(win->num, url, (int*)&length);
+            callback_resp = win->files_handler_window(win->num, handler_url, (int*)&length);
         } else {
-            callback_resp = win->files_handler(url, (int*)&length);
+            callback_resp = win->files_handler(handler_url, (int*)&length);
         }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions