Skip to content

Commit 4a9e410

Browse files
committed
Fix Async Empty Response Scenario
1 parent a6081e5 commit 4a9e410

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

src/webui.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,22 +4415,32 @@ void webui_interface_set_response_file_handler(size_t window, const void* respon
44154415
return;
44164416
_webui_window_t* win = _webui.wins[window];
44174417

4418-
if (length == 0)
4418+
// Check if the response is empty
4419+
if (length == 0) {
44194420
length = _webui_strlen(response);
4421+
if (length == 0) {
4422+
#ifdef WEBUI_LOG
4423+
_webui_log_info("[User] webui_interface_set_response_file_handler() -> Response is empty\n");
4424+
#endif
4425+
}
4426+
}
44204427

44214428
// If `response` is allocated using `webui_malloc()`, it will be
44224429
// available during execution and automatically freed by WebUI
44234430
// after sending the response. However, if `response` is allocated
44244431
// using other methods, or is simply a pointer to a static buffer,
44254432
// we may lose the data before sending the response. Therefore, we need
44264433
// to copy the data to a new buffer if it was not allocated by `webui_malloc()`.
4427-
void* response_copy = _webui_malloc_if_not_exist(response, length);
4428-
if (response_copy != response) {
4429-
#ifdef WEBUI_LOG
4430-
_webui_log_debug("[User] webui_interface_set_response_file_handler() -> "
4431-
"Response copied to a new buffer at %p\n", response_copy);
4432-
#endif
4433-
memcpy(response_copy, response, length);
4434+
void* response_copy = NULL;
4435+
if (length > 0) {
4436+
response_copy = _webui_malloc_if_not_exist(response, length);
4437+
if (response_copy != response) {
4438+
#ifdef WEBUI_LOG
4439+
_webui_log_debug("[User] webui_interface_set_response_file_handler() -> "
4440+
"Response copied to a new buffer at %p\n", response_copy);
4441+
#endif
4442+
memcpy(response_copy, response, length);
4443+
}
44344444
}
44354445

44364446
// Set the response
@@ -5450,6 +5460,14 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
54505460
// Call user callback for the requested path
54515461
const void* callback_resp = _webui_call_external_file_handler_cb(win, handler_url, &length);
54525462

5463+
// Content length
5464+
if (length == 0) {
5465+
length = _webui_strlen(callback_resp);
5466+
if (length == 0) {
5467+
callback_resp = NULL; // No content length, Treat as not found
5468+
}
5469+
}
5470+
54535471
if (callback_resp != NULL) {
54545472
// Keep canonical behavior consistent with local-folder mode:
54555473
// if we resolved a different target path (e.g. "/" -> "/custom.html"),
@@ -5464,11 +5482,6 @@ static int _webui_external_file_handler(_webui_window_t* win, struct mg_connecti
54645482
return http_status_code;
54655483
}
54665484

5467-
// File content found
5468-
5469-
if (length == 0)
5470-
length = _webui_strlen(callback_resp);
5471-
54725485
#ifdef WEBUI_LOG
54735486
_webui_log_debug("[Core]\t\t_webui_external_file_handler() -> Custom files handler found (%zu bytes)\n",
54745487
length

0 commit comments

Comments
 (0)