Skip to content

Commit e15707b

Browse files
authored
Merge pull request #681 from triuk/main
Deterministic entry file handling for VFS and filesystem roots
2 parents accfaae + 98b28aa commit e15707b

2 files changed

Lines changed: 30 additions & 59 deletions

File tree

examples/C++/virtual_file_system/vfs.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,8 @@
1212
import os
1313
import sys
1414

15-
def generate_vfs_header(directory, output_header, custom_index=None):
15+
def generate_vfs_header(directory, output_header):
1616
files = []
17-
index_files = {}
18-
19-
# Handle custom index file
20-
custom_index_path = None
21-
if custom_index:
22-
custom_index_rel = custom_index.lstrip('/').replace('\\', '/')
23-
custom_index_abs = os.path.join(directory, custom_index_rel)
24-
if os.path.isfile(custom_index_abs):
25-
custom_index_path = '/' + custom_index_rel
2617

2718
# Walk through the directory and collect files
2819
for root, _, filenames in os.walk(directory):
@@ -33,16 +24,6 @@ def generate_vfs_header(directory, output_header, custom_index=None):
3324
relative_path = '/' + relative_path.replace('\\', '/')
3425
files.append((relative_path, filepath))
3526

36-
# Check for index files
37-
if filename.startswith("index."):
38-
dir_path = os.path.dirname(relative_path)
39-
if dir_path not in index_files:
40-
index_files[dir_path] = relative_path
41-
42-
# If a custom index file is provided, override the root index
43-
if custom_index_path is not None:
44-
index_files["/"] = custom_index_path
45-
4627
# Generate the C header file
4728
with open(output_header, 'w') as header:
4829
header.write('#ifndef VIRTUAL_FILE_SYSTEM_H\n')
@@ -75,15 +56,6 @@ def generate_vfs_header(directory, output_header, custom_index=None):
7556

7657
header.write('static const int virtual_files_count = sizeof(virtual_files) / sizeof(virtual_files[0]);\n\n')
7758

78-
header.write('static const char* index_files[] = {\n')
79-
for dir_path, index_path in index_files.items():
80-
if dir_path == "/":
81-
header.write(f' "/", "{index_path}",\n')
82-
else:
83-
header.write(f' "{dir_path}/", "{index_path}",\n')
84-
header.write(' NULL\n')
85-
header.write('};\n\n')
86-
8759
header.write('bool virtual_file_system(const char* path, const unsigned char** file, int* length) {\n')
8860
header.write(' for (int i = 0; i < virtual_files_count; ++i) {\n')
8961
header.write(' if (strcmp(virtual_files[i].path, path) == 0) {\n')
@@ -110,41 +82,18 @@ def generate_vfs_header(directory, output_header, custom_index=None):
11082
header.write(' snprintf((char*) response, header_length + 1, http_header_template, content_type, file_length);\n')
11183
header.write(' memcpy(response + header_length, file_data, file_length);\n')
11284
header.write(' return response;\n')
113-
header.write(' } else {\n')
114-
header.write(' // Check for index file redirection\n')
115-
header.write(' char redirect_path[1024];\n')
116-
header.write(' snprintf(redirect_path, sizeof(redirect_path), "%s", path);\n')
117-
header.write(' size_t len = strlen(redirect_path);\n')
118-
header.write(' if (redirect_path[len - 1] != \'/\') {\n')
119-
header.write(' redirect_path[len] = \'/\';\n')
120-
header.write(' redirect_path[len + 1] = \'\\0\';\n')
121-
header.write(' }\n')
122-
header.write(' for (int i = 0; index_files[i] != NULL; i += 2) {\n')
123-
header.write(' if (strcmp(index_files[i], redirect_path) == 0) {\n')
124-
header.write(' const char* location_header = "HTTP/1.1 302 Found\\r\\n"\n')
125-
header.write(' "Location: %s\\r\\n"\n')
126-
header.write(' "Cache-Control: no-cache\\r\\n\\r\\n";\n')
127-
header.write(' int header_length = snprintf(NULL, 0, location_header, index_files[i + 1]);\n')
128-
header.write(' *length = header_length;\n')
129-
header.write(' unsigned char* response = (unsigned char*) webui_malloc(*length);\n')
130-
header.write(' snprintf((char*) response, header_length + 1, location_header, index_files[i + 1]);\n')
131-
header.write(' return response;\n')
132-
header.write(' }\n')
133-
header.write(' }\n')
134-
header.write(' return NULL;\n')
13585
header.write(' }\n')
86+
header.write(' return NULL;\n')
13687
header.write('}\n\n')
13788

13889
header.write('#endif // VIRTUAL_FILE_SYSTEM_H\n')
13990

14091
if __name__ == '__main__':
141-
if len(sys.argv) not in (3, 4):
142-
print(f'Usage: {sys.argv[0]} <directory> <output_header> [custom_index_filename]')
92+
if len(sys.argv) != 3:
93+
print(f'Usage: {sys.argv[0]} <directory> <output_header>')
14394
sys.exit(1)
14495

14596
directory = sys.argv[1]
14697
output_header = sys.argv[2]
147-
custom_index = sys.argv[3] if len(sys.argv) == 4 else None
148-
149-
generate_vfs_header(directory, output_header, custom_index)
98+
generate_vfs_header(directory, output_header)
15099
print(f'Generated {output_header} from {directory}')

src/webui.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,6 +4864,17 @@ static bool _webui_file_exist_mg(_webui_window_t* win, struct mg_connection* cli
48644864

48654865
const struct mg_request_info * ri = mg_get_request_info(client);
48664866
const char* url = ri->local_uri;
4867+
const char* handler_url = url;
4868+
char user_index_url[WEBUI_MAX_PATH];
4869+
// Respect user-defined index file when using an external file handler.
4870+
if (strcmp(url, "/") == 0 && !_webui_is_empty(win->user_index_file)) {
4871+
if (win->user_index_file[0] == '/') {
4872+
handler_url = win->user_index_file;
4873+
} else {
4874+
WEBUI_SN_PRINTF_STATIC(user_index_url, WEBUI_MAX_PATH, "/%s", win->user_index_file);
4875+
handler_url = user_index_url;
4876+
}
4877+
}
48674878
size_t url_len = _webui_strlen(url);
48684879

48694880
// Get file name
@@ -5192,6 +5203,17 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
51925203
int http_status_code = 0;
51935204
const struct mg_request_info * ri = mg_get_request_info(client);
51945205
const char* url = ri->local_uri;
5206+
const char* handler_url = url;
5207+
char user_index_url[WEBUI_MAX_PATH];
5208+
// Respect user-defined index file when using an external file handler.
5209+
if (strcmp(url, "/") == 0 && !_webui_is_empty(win->user_index_file)) {
5210+
if (win->user_index_file[0] == '/') {
5211+
handler_url = win->user_index_file;
5212+
} else {
5213+
WEBUI_SN_PRINTF_STATIC(user_index_url, WEBUI_MAX_PATH, "/%s", win->user_index_file);
5214+
handler_url = user_index_url;
5215+
}
5216+
}
51955217

51965218
if (win->files_handler != NULL || win->files_handler_window != NULL) {
51975219
// Get file content from the external files handler
@@ -5217,7 +5239,7 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
52175239
if(win->files_handler_window) {
52185240
pt = win->files_handler_window;
52195241
}
5220-
_webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Path [%s]\n", url);
5242+
_webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Path [%s]\n", handler_url);
52215243
_webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Calling custom files handler callback at address 0x%p\n", pt);
52225244
_webui_log_debug("[Call]\n");
52235245
#endif
@@ -5232,9 +5254,9 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
52325254
// Call user callback
52335255
const void* callback_resp = NULL;
52345256
if (win->files_handler_window != NULL) {
5235-
callback_resp = win->files_handler_window(win->num, url, (int*)&length);
5257+
callback_resp = win->files_handler_window(win->num, handler_url, (int*)&length);
52365258
} else {
5237-
callback_resp = win->files_handler(url, (int*)&length);
5259+
callback_resp = win->files_handler(handler_url, (int*)&length);
52385260
}
52395261

52405262
// Async response wait

0 commit comments

Comments
 (0)