diff --git a/src/main.c b/src/main.c index eda178f..3f0448e 100644 --- a/src/main.c +++ b/src/main.c @@ -226,6 +226,8 @@ int main(int argc, char **argv) { mympd_api_queue = tiny_queue_create(); web_server_queue = tiny_queue_create(); + t_user_data *user_data = (t_user_data*)malloc(sizeof(t_user_data)); + srand((unsigned int)time(NULL)); //mympd config defaults @@ -312,7 +314,12 @@ int main(int argc, char **argv) { //init webserver struct mg_mgr mgr; - if (!web_server_init(&mgr, config)) { + + user_data->config = config; + user_data->conn_id = 1; + user_data->global = true; + + if (!web_server_init(&mgr, config, user_data)) { goto cleanup; } else { @@ -427,5 +434,6 @@ int main(int argc, char **argv) { tiny_queue_free(mympd_api_queue); mympd_free_config(config); free(configfile); + free(user_data); return rc; } diff --git a/src/web_server.c b/src/web_server.c index 1df64a5..6dbbef5 100644 --- a/src/web_server.c +++ b/src/web_server.c @@ -42,13 +42,8 @@ static void send_ws_notify(struct mg_mgr *mgr, t_work_result *response); static void send_api_response(struct mg_mgr *mgr, t_work_result *response); static bool handle_api(long conn_id, const char *request, int request_len); -typedef struct t_user_data { - void *config; //pointer to mympd config - long conn_id; -} t_user_data; - //public functions -bool web_server_init(void *arg_mgr, t_config *config) { +bool web_server_init(void *arg_mgr, t_config *config, t_user_data *user_data) { struct mg_mgr *mgr = (struct mg_mgr *) arg_mgr; struct mg_connection *nc_https; struct mg_connection *nc_http; @@ -57,10 +52,6 @@ bool web_server_init(void *arg_mgr, t_config *config) { const char *err_https; const char *err_http; - t_user_data *user_data = (t_user_data*)malloc(sizeof(t_user_data)); - user_data->config = config; - user_data->conn_id = 1; - mg_mgr_init(mgr, NULL); //bind to webport @@ -170,6 +161,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { t_user_data *nc_user_data = (t_user_data*)malloc(sizeof(t_user_data)); nc_user_data->config = config; nc_user_data->conn_id = user_data->conn_id; + nc_user_data->global = false; nc->user_data = nc_user_data; LOG_DEBUG() fprintf(stderr, "DEBUG: New connection id %ld.\n", user_data->conn_id); break; @@ -211,8 +203,13 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { break; } case MG_EV_CLOSE: { - LOG_VERBOSE() fprintf(stderr, "HTTP connection %ld closed.\n", user_data->conn_id); - free(nc->user_data); + if (nc->user_data != NULL) { + if (user_data->global == false) { + LOG_VERBOSE() fprintf(stderr, "HTTP connection %ld closed.\n", user_data->conn_id); + free(nc->user_data); + nc->user_data = NULL; + } + } break; } default: { diff --git a/src/web_server.h b/src/web_server.h index 93bb650..e1b4dae 100644 --- a/src/web_server.h +++ b/src/web_server.h @@ -25,8 +25,14 @@ #ifndef __WEB_SERVER_H__ #define __WEB_SERVER_H__ +typedef struct t_user_data { + void *config; //pointer to mympd config + long conn_id; + bool global; +} t_user_data; + void *web_server_loop(void *arg_mgr); -bool web_server_init(void *arg_mgr, t_config *config); +bool web_server_init(void *arg_mgr, t_config *config, t_user_data *user_data); void web_server_free(void *arg_mgr); #endif