From fb635210427b1a5559e653f6091a48f5452894bc Mon Sep 17 00:00:00 2001 From: Hassan DRAGA Date: Thu, 11 Jul 2024 13:32:31 -0400 Subject: [PATCH] Fix serve_a_folder (Now should be Header + Body) --- examples/C/serve_a_folder/main.c | 194 +++++++++++++++++-------------- 1 file changed, 107 insertions(+), 87 deletions(-) diff --git a/examples/C/serve_a_folder/main.c b/examples/C/serve_a_folder/main.c index b906c616..01e7f088 100644 --- a/examples/C/serve_a_folder/main.c +++ b/examples/C/serve_a_folder/main.c @@ -10,135 +10,155 @@ void exit_app(webui_event_t* e) { - // Close all opened windows - webui_exit(); + // Close all opened windows + webui_exit(); } void events(webui_event_t* e) { - // This function gets called every time - // there is an event + // This function gets called every time + // there is an event - if (e->event_type == WEBUI_EVENT_CONNECTED) - printf("Connected. \n"); - else if (e->event_type == WEBUI_EVENT_DISCONNECTED) - printf("Disconnected. \n"); - else if (e->event_type == WEBUI_EVENT_MOUSE_CLICK) - printf("Click. \n"); - else if (e->event_type == WEBUI_EVENT_NAVIGATION) { - const char* url = webui_get_string(e); - printf("Starting navigation to: %s \n", url); + if (e->event_type == WEBUI_EVENT_CONNECTED) + printf("Connected. \n"); + else if (e->event_type == WEBUI_EVENT_DISCONNECTED) + printf("Disconnected. \n"); + else if (e->event_type == WEBUI_EVENT_MOUSE_CLICK) + printf("Click. \n"); + else if (e->event_type == WEBUI_EVENT_NAVIGATION) { + const char* url = webui_get_string(e); + printf("Starting navigation to: %s \n", url); - // Because we used `webui_bind(MyWindow, "", events);` - // WebUI will block all `href` link clicks and sent here instead. - // We can then control the behaviour of links as needed. - webui_navigate(e->window, url); - } + // Because we used `webui_bind(MyWindow, "", events);` + // WebUI will block all `href` link clicks and sent here instead. + // We can then control the behaviour of links as needed. + webui_navigate(e->window, url); + } } void switch_to_second_page(webui_event_t* e) { - // This function gets called every - // time the user clicks on "SwitchToSecondPage" + // This function gets called every + // time the user clicks on "SwitchToSecondPage" - // Switch to `/second.html` in the same opened window. - webui_show(e->window, "second.html"); + // Switch to `/second.html` in the same opened window. + webui_show(e->window, "second.html"); } void show_second_window(webui_event_t* e) { - // This function gets called every - // time the user clicks on "OpenNewWindow" + // This function gets called every + // time the user clicks on "OpenNewWindow" - // Show a new window, and navigate to `/second.html` - // if it's already open, then switch in the same window - webui_show(MySecondWindow, "second.html"); + // Show a new window, and navigate to `/second.html` + // if it's already open, then switch in the same window + webui_show(MySecondWindow, "second.html"); } const void* my_files_handler(const char* filename, int* length) { - printf("File: %s \n", filename); + printf("File: %s \n", filename); - if (!strcmp(filename, "/test.txt")) { + if (!strcmp(filename, "/test.txt")) { - // Const static file example - // Note: The connection will drop if the content - // does not have `` - return "This is a embedded file content example."; - } else if (!strcmp(filename, "/dynamic.html")) { + // Const static file example + return "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 99\r\n\r\n" + "" + " This is a static embedded file content example." + " " // To keep connection with WebUI + ""; + } + else if (!strcmp(filename, "/dynamic.html")) { - // Dynamic file example + // Dynamic file example - // Allocate memory - char* dynamic_content = webui_malloc(1024); + // Allocate memory + char* body = webui_malloc(1024); + char* header_and_body = webui_malloc(1024); - // Generate content - static int count = 0; - sprintf( - dynamic_content, - "" - " This is a dynamic file content example.
" - " Count: %d [Refresh]
" - " " // To keep connection with WebUI - "", - ++count - ); + // Generate body + static int count = 0; + sprintf( + body, + "" + " This is a dynamic file content example.
" + " Count: %d [Refresh]
" + " " // To keep connection with WebUI + "", + ++count + ); - // Set len (optional) - *length = strlen(dynamic_content); + // Generate header + body + int body_size = strlen(body); + sprintf( + header_and_body, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %d\r\n\r\n" + "%s", + body_size, body + ); + + // Free body buffer + webui_free(body); - // By allocating resources using webui_malloc() - // WebUI will automaticaly free the resources. - return dynamic_content; - } + // Set len (optional) + *length = strlen(header_and_body); - // Other files: - // A NULL return will make WebUI - // looks for the file locally. - return NULL; + // By allocating resources using webui_malloc() + // WebUI will automaticaly free the resources. + return header_and_body; + } + + // Other files: + // A NULL return will make WebUI + // looks for the file locally. + return NULL; } int main() { - // Create new windows - webui_new_window_id(MyWindow); - webui_new_window_id(MySecondWindow); + // Create new windows + webui_new_window_id(MyWindow); + webui_new_window_id(MySecondWindow); - // Bind HTML element IDs with a C functions - webui_bind(MyWindow, "SwitchToSecondPage", switch_to_second_page); - webui_bind(MyWindow, "OpenNewWindow", show_second_window); - webui_bind(MyWindow, "Exit", exit_app); - webui_bind(MySecondWindow, "Exit", exit_app); + // Bind HTML element IDs with a C functions + webui_bind(MyWindow, "SwitchToSecondPage", switch_to_second_page); + webui_bind(MyWindow, "OpenNewWindow", show_second_window); + webui_bind(MyWindow, "Exit", exit_app); + webui_bind(MySecondWindow, "Exit", exit_app); - // Bind events - webui_bind(MyWindow, "", events); + // Bind events + webui_bind(MyWindow, "", events); - // Set the `.ts` and `.js` runtime - // webui_set_runtime(MyWindow, NodeJS); - // webui_set_runtime(MyWindow, Bun); - webui_set_runtime(MyWindow, Deno); + // Set the `.ts` and `.js` runtime + // webui_set_runtime(MyWindow, NodeJS); + // webui_set_runtime(MyWindow, Bun); + webui_set_runtime(MyWindow, Deno); - // Set a custom files handler - webui_set_file_handler(MyWindow, my_files_handler); + // Set a custom files handler + webui_set_file_handler(MyWindow, my_files_handler); - // Set window size - webui_set_size(MyWindow, 800, 800); + // Set window size + webui_set_size(MyWindow, 800, 800); - // Set window position - webui_set_position(MyWindow, 200, 200); + // Set window position + webui_set_position(MyWindow, 200, 200); - // Show a new window - // webui_set_root_folder(MyWindow, "_MY_PATH_HERE_"); - // webui_show_browser(MyWindow, "index.html", Chrome); - webui_show(MyWindow, "index.html"); + // Show a new window + // webui_set_root_folder(MyWindow, "_MY_PATH_HERE_"); + // webui_show_browser(MyWindow, "index.html", Chrome); + webui_show(MyWindow, "index.html"); - // Wait until all windows get closed - webui_wait(); + // Wait until all windows get closed + webui_wait(); - // Free all memory resources (Optional) - webui_clean(); + // Free all memory resources (Optional) + webui_clean(); - return 0; + return 0; } #if defined(_MSC_VER)