@@ -579,6 +579,7 @@ static bool _webui_client_cookies_get_id(_webui_window_t* win, const char* cooki
579579static size_t _webui_client_get_id (_webui_window_t * win , struct mg_connection * client );
580580static void _webui_generate_cookies (char * cookies , size_t length );
581581static int _webui_serve_file (_webui_window_t * win , struct mg_connection * client , size_t client_id );
582+ static int _webui_external_file_handler (_webui_window_t * win , struct mg_connection * client , size_t client_id );
582583static int _webui_interpret_file (_webui_window_t * win , struct mg_connection * client , char * index , size_t client_id );
583584// WebView
584585#ifdef _WIN32
@@ -2071,6 +2072,18 @@ size_t webui_get_parent_process_id(size_t window) {
20712072 return win -> process_id ;
20722073}
20732074
2075+ const char * webui_get_mime_type (const char * file ) {
2076+
2077+ #ifdef WEBUI_LOG
2078+ printf ("[User] webui_get_mime_type([%s])\n" , file );
2079+ #endif
2080+
2081+ // Initialization
2082+ _webui_init ();
2083+
2084+ return mg_get_builtin_mime_type (file );
2085+ }
2086+
20742087size_t webui_get_free_port (void ) {
20752088
20762089 #ifdef WEBUI_LOG
@@ -2083,6 +2096,23 @@ size_t webui_get_free_port(void) {
20832096 return _webui_get_free_port ();
20842097}
20852098
2099+ size_t webui_get_port (size_t window ) {
2100+
2101+ #ifdef WEBUI_LOG
2102+ printf ("[User] webui_get_port([%zu])\n" , window );
2103+ #endif
2104+
2105+ // Initialization
2106+ _webui_init ();
2107+
2108+ // Dereference
2109+ if (_webui_mutex_is_exit_now (WEBUI_MUTEX_NONE ) || _webui .wins [window ] == NULL )
2110+ return 0 ;
2111+ _webui_window_t * win = _webui .wins [window ];
2112+
2113+ return win -> server_port ;
2114+ }
2115+
20862116#ifdef WEBUI_TLS
20872117static bool _webui_check_certificate (const char * certificate_pem , const char * private_key_pem ) {
20882118
@@ -2291,23 +2321,6 @@ void webui_set_event_blocking(size_t window, bool status) {
22912321 win -> ws_block = status ;
22922322}
22932323
2294- size_t webui_get_port (size_t window ) {
2295-
2296- #ifdef WEBUI_LOG
2297- printf ("[User] webui_get_port([%zu])...\n" , window );
2298- #endif
2299-
2300- // Initialization
2301- _webui_init ();
2302-
2303- // Dereference
2304- if (_webui_mutex_is_exit_now (WEBUI_MUTEX_NONE ) || _webui .wins [window ] == NULL )
2305- return 0 ;
2306- _webui_window_t * win = _webui .wins [window ];
2307-
2308- return win -> server_port ;
2309- }
2310-
23112324bool webui_set_port (size_t window , size_t port ) {
23122325
23132326 #ifdef WEBUI_LOG
@@ -4155,16 +4168,13 @@ static void _webui_free_event_inf(_webui_window_t* win, size_t event_num) {
41554168 win -> events [event_num ] = NULL ;
41564169}
41574170
4158- static int _webui_serve_file (_webui_window_t * win , struct mg_connection * client , size_t client_id ) {
4171+ static int _webui_external_file_handler (_webui_window_t * win , struct mg_connection * client , size_t client_id ) {
41594172
41604173 #ifdef WEBUI_LOG
4161- printf ("[Core]\t\t_webui_serve_file ()\n" );
4174+ printf ("[Core]\t\t_webui_external_file_handler ()\n" );
41624175 #endif
41634176
4164- // Serve a normal text based file
4165-
41664177 int http_status_code = 0 ;
4167-
41684178 const struct mg_request_info * ri = mg_get_request_info (client );
41694179 const char * url = ri -> local_uri ;
41704180
@@ -4188,55 +4198,84 @@ static int _webui_serve_file(_webui_window_t* win, struct mg_connection* client,
41884198 // e.cookies = "";
41894199
41904200 // Files handler callback
4191- const void * callback_resp = win -> files_handler (url , (int * )& length );
4201+ #ifdef WEBUI_LOG
4202+ printf ("[Core]\t\t_webui_external_file_handler() -> Path [%s]\n" , url );
4203+ printf ("[Core]\t\t_webui_external_file_handler() -> Calling custom files handler callback\n" );
4204+ printf ("[Call]\n" );
4205+ #endif
4206+ const void * callback_resp = win -> files_handler (url , (int * )& length );
41924207
41934208 if (callback_resp != NULL ) {
41944209
4195- // File content found (200)
4210+ // File content found
41964211
41974212 if (length == 0 )
41984213 length = _webui_strlen (callback_resp );
41994214
4200- // Send user data (200)
4201- _webui_http_send (win , client , mg_get_builtin_mime_type (url ),
4202- callback_resp , length , false);
4215+ #ifdef WEBUI_LOG
4216+ printf ("[Core]\t\t_webui_external_file_handler() -> Custom files handler found (%zu bytes)\n" ,
4217+ length
4218+ );
4219+ #endif
4220+
4221+ // Send user data (Header + Body)
4222+ mg_write (client , callback_resp , length );
42034223
42044224 // Safely free resources if end-user allocated
42054225 // using `webui_malloc()`. Otherwise just do nothing.
4206- webui_free ((void * )callback_resp );
4226+ _webui_free_mem ((void * )callback_resp );
42074227
42084228 http_status_code = 200 ;
42094229 }
4230+ else {
4231+ #ifdef WEBUI_LOG
4232+ printf ("[Core]\t\t_webui_external_file_handler() -> Custom files handler failed\n" );
4233+ #endif
4234+ }
42104235
42114236 // If `data == NULL` thats mean the external handler
42124237 // did not find the requested file. So WebUI will try
42134238 // looking for the file locally.
42144239 }
42154240
4216- if (http_status_code != 200 ) {
4241+ return http_status_code ;
4242+ }
42174243
4218- // Using internal files handler
4244+ static int _webui_serve_file ( _webui_window_t * win , struct mg_connection * client , size_t client_id ) {
42194245
4220- // Get full path
4221- char * file = _webui_get_file_name_from_url ( url );
4222- char * full_path = _webui_get_full_path ( win , file );
4246+ #ifdef WEBUI_LOG
4247+ printf ( "[Core]\t\t_webui_serve_file()\n" );
4248+ #endif
42234249
4224- if ( _webui_file_exist ( full_path )) {
4250+ // Serve a normal text based file
42254251
4226- // 200 - File exist
4227- _webui_http_send_file (win , client , mg_get_builtin_mime_type (url ), full_path , false);
4228- http_status_code = 200 ;
4229- }
4230- else {
4252+ int http_status_code = 0 ;
4253+ const struct mg_request_info * ri = mg_get_request_info (client );
4254+ const char * url = ri -> local_uri ;
42314255
4232- // 404 - File not exist
4233- _webui_http_send_error (client , webui_html_res_not_available , 404 );
4234- http_status_code = 404 ;
4235- }
4256+ #ifdef WEBUI_LOG
4257+ printf ("[Core]\t\t_webui_serve_file() -> Looking for file locally\n" );
4258+ #endif
4259+
4260+ // Get full path
4261+ char * file = _webui_get_file_name_from_url (url );
4262+ char * full_path = _webui_get_full_path (win , file );
4263+
4264+ if (_webui_file_exist (full_path )) {
42364265
4237- _webui_free_mem ((void * )full_path );
4238- _webui_free_mem ((void * )file );
4266+ // 200 - File exist
4267+ _webui_http_send_file (win , client , mg_get_builtin_mime_type (url ), full_path , false);
4268+ http_status_code = 200 ;
42394269 }
4270+ else {
4271+
4272+ // 404 - File not exist
4273+ _webui_http_send_error (client , webui_html_res_not_available , 404 );
4274+ http_status_code = 404 ;
4275+ }
4276+
4277+ _webui_free_mem ((void * )full_path );
4278+ _webui_free_mem ((void * )file );
42404279
42414280 return http_status_code ;
42424281}
@@ -7646,14 +7685,14 @@ static void _webui_http_send_error(struct mg_connection* client, const char* bod
76467685 size_t buffer_len = (512 + body_len );
76477686 char * buffer = (char * )_webui_malloc (buffer_len );
76487687 to_send = WEBUI_SN_PRINTF_DYN (buffer , buffer_len ,
7649- "HTTP/1.1 200 OK\r\n"
7688+ "HTTP/1.1 %d OK\r\n"
76507689 "Content-Type: text/html; charset=utf-8\r\n"
76517690 "Cache-Control: no-cache, no-store, must-revalidate, private, max-age=0\r\n"
76527691 "Pragma: no-cache\r\n"
76537692 "Expires: 0\r\n"
76547693 "Content-Length: %zu\r\n"
76557694 "Connection: close\r\n\r\n%s" ,
7656- body_len , body
7695+ status , body_len , body
76577696 );
76587697
76597698 // Send
@@ -7892,6 +7931,14 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
78927931 _webui_http_send (win , client , "application/javascript" , "" , 0 , false);
78937932 }
78947933 }
7934+ else if ((win -> files_handler != NULL ) && (_webui_external_file_handler (win , client , client_id ) != 0 )) {
7935+
7936+ // File already handled by the custom external file handler
7937+ // nothing to do now.
7938+ #ifdef WEBUI_LOG
7939+ printf ("[Core]\t\t_webui_http_handler() -> Handled by custom external file handler\n" );
7940+ #endif
7941+ }
78957942 else if (strcmp (url , "/" ) == 0 ) {
78967943
78977944 // [/]
@@ -7976,7 +8023,7 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
79768023 char * folder_path = _webui_get_full_path (win , url );
79778024
79788025 if (_webui_file_exist (folder_path )) {
7979-
8026+
79808027 // [/file]
79818028
79828029 bool script = false;
@@ -8012,8 +8059,8 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
80128059 // Serve as a normal text-based file
80138060 http_status_code = _webui_serve_file (win , client , client_id );
80148061 }
8015- }
8016- else if (_webui_folder_exist (folder_path )) {
8062+ }
8063+ else if (_webui_folder_exist (folder_path )) {
80178064
80188065 // [/folder]
80198066
@@ -8050,15 +8097,16 @@ static int _webui_http_handler(struct mg_connection* client, void * _win) {
80508097 http_status_code = 404 ;
80518098 }
80528099 else {
8053-
8100+
80548101 // [invalid]
80558102
80568103 #ifdef WEBUI_LOG
80578104 printf ("[Core]\t\t_webui_http_handler() -> Not found\n" );
80588105 #endif
80598106
80608107 // No file or folder is found at this path
8061- http_status_code = _webui_serve_file (win , client , client_id );
8108+ _webui_http_send_error (client , webui_html_res_not_available , 404 );
8109+ _webui_free_mem ((void * )folder_path );
80628110 http_status_code = 404 ;
80638111 }
80648112
0 commit comments