Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions include/webui.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ WEBUI_EXPORT bool webui_set_default_root_folder(const char* path);
/**
* @brief Set a custom handler to serve files. This custom handler should
* return full HTTP header and body.
* This deactivates any previous handler set with `webui_set_file_handler_window`
*
* @param window The window number
* @param handler The handler function: `void myHandler(const char* filename,
Expand All @@ -433,6 +434,20 @@ WEBUI_EXPORT bool webui_set_default_root_folder(const char* path);
*/
WEBUI_EXPORT void webui_set_file_handler(size_t window, const void* (*handler)(const char* filename, int* length));

/**
* @brief Set a custom handler to serve files. This custom handler should
* return full HTTP header and body.
* This deactivates any previous handler set with `webui_set_file_handler`
*
* @param window The window number
* @param handler The handler function: `void myHandler(size_t window, const char* filename,
* int* length)`
*
* @example webui_set_file_handler_window(myWindow, myHandlerFunction);
*/
WEBUI_EXPORT void webui_set_file_handler_window(size_t window, const void* (*handler)(size_t window, const char* filename, int* length));


/**
* @brief Check if the specified window is still running.
*
Expand Down
7 changes: 7 additions & 0 deletions include/webui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,17 @@ namespace webui {
}

// Set a custom handler to serve files. This custom handler should return full HTTP header and body.
// Resets previous handler set with `set_file_handler_window`.
void set_file_handler(const void* (*handler)(const char* filename, int* length)) const {
webui_set_file_handler(webui_window, handler);
}

// Set a custom handler to serve files. This custom handler should return full HTTP header and body.
// Resets previous handler set with `set_file_handler`
void set_file_handler_window(const void* (*handler)(size_t window, const char* filename, int* length)) const {
webui_set_file_handler_window(webui_window, handler);
}

// Set the web browser profile to use. An empty `name` and `path` means the default user profile. Need
// to be called before `webui_show()`.
void set_profile(const std::string_view name = {""}, const std::string_view path = {""}) const {
Expand Down
33 changes: 29 additions & 4 deletions src/webui.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ typedef struct _webui_window_t {
bool position_set;
size_t process_id;
const void*(*files_handler)(const char* filename, int* length);
const void*(*files_handler_window)(size_t window, const char* filename, int* length);
webui_event_inf_t* events[WEBUI_MAX_IDS];
size_t events_count;
bool is_public;
Expand Down Expand Up @@ -735,7 +736,29 @@ void webui_set_file_handler(size_t window, const void*(*handler)(const char* fil
return;
_webui_window_t* win = _webui.wins[window];

// Set the new `files_handler`
win->files_handler = handler;
// And reset any previous `files_handler_window`
win->files_handler_window = NULL;
}

void webui_set_file_handler_window(size_t window, const void*(*handler)(size_t window, const char* filename, int* length)) {

if (handler == NULL)
return;

// Initialization
_webui_init();

// Dereference
if (_webui_mutex_is_exit_now(WEBUI_MUTEX_NONE) || _webui.wins[window] == NULL)
return;
_webui_window_t* win = _webui.wins[window];

// Reset any previous `files_handler`
win->files_handler = NULL;
// And set `files_handler_window`
win->files_handler_window = handler;
}

bool webui_script_client(webui_event_t* e, const char* script, size_t timeout,
Expand Down Expand Up @@ -4170,8 +4193,7 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
const struct mg_request_info * ri = mg_get_request_info(client);
const char* url = ri->local_uri;

if (win->files_handler != NULL) {

if (win->files_handler != NULL || win->files_handler_window != NULL) {
// Get file content from the external files handler
size_t length = 0;

Expand All @@ -4195,7 +4217,10 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
printf("[Core]\t\t_webui_external_file_handler() -> Calling custom files handler callback\n");
printf("[Call]\n");
#endif
const void* callback_resp = win->files_handler(url, (int*)&length);

// True if we pass the window num to the handler, false otherwise.
int is_file_handler_window = win->files_handler_window != NULL;
const void* callback_resp = is_file_handler_window ? win->files_handler_window(win->num, url, (int*)&length) : win->files_handler(url, (int*)&length);

if (callback_resp != NULL) {

Expand Down Expand Up @@ -7924,7 +7949,7 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
_webui_http_send(win, client, "application/javascript", "", 0, false);
}
}
else if ((win->files_handler != NULL) && (_webui_external_file_handler(win, client, client_id) != 0)) {
else if ((win->files_handler != NULL || (win->files_handler_window != NULL)) && (_webui_external_file_handler(win, client, client_id) != 0)) {

// File already handled by the custom external file handler
// nothing to do now.
Expand Down