mirror of https://github.com/SuperBFG7/ympd
Fix: free mongoose user_data
This commit is contained in:
parent
ccf25a578d
commit
35ea7a2bb6
10
src/main.c
10
src/main.c
|
@ -226,6 +226,8 @@ int main(int argc, char **argv) {
|
||||||
mympd_api_queue = tiny_queue_create();
|
mympd_api_queue = tiny_queue_create();
|
||||||
web_server_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));
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
//mympd config defaults
|
//mympd config defaults
|
||||||
|
@ -312,7 +314,12 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
//init webserver
|
//init webserver
|
||||||
struct mg_mgr mgr;
|
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;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -427,5 +434,6 @@ int main(int argc, char **argv) {
|
||||||
tiny_queue_free(mympd_api_queue);
|
tiny_queue_free(mympd_api_queue);
|
||||||
mympd_free_config(config);
|
mympd_free_config(config);
|
||||||
free(configfile);
|
free(configfile);
|
||||||
|
free(user_data);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 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);
|
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
|
//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_mgr *mgr = (struct mg_mgr *) arg_mgr;
|
||||||
struct mg_connection *nc_https;
|
struct mg_connection *nc_https;
|
||||||
struct mg_connection *nc_http;
|
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_https;
|
||||||
const char *err_http;
|
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);
|
mg_mgr_init(mgr, NULL);
|
||||||
|
|
||||||
//bind to webport
|
//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));
|
t_user_data *nc_user_data = (t_user_data*)malloc(sizeof(t_user_data));
|
||||||
nc_user_data->config = config;
|
nc_user_data->config = config;
|
||||||
nc_user_data->conn_id = user_data->conn_id;
|
nc_user_data->conn_id = user_data->conn_id;
|
||||||
|
nc_user_data->global = false;
|
||||||
nc->user_data = nc_user_data;
|
nc->user_data = nc_user_data;
|
||||||
LOG_DEBUG() fprintf(stderr, "DEBUG: New connection id %ld.\n", user_data->conn_id);
|
LOG_DEBUG() fprintf(stderr, "DEBUG: New connection id %ld.\n", user_data->conn_id);
|
||||||
break;
|
break;
|
||||||
|
@ -211,8 +203,13 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MG_EV_CLOSE: {
|
case MG_EV_CLOSE: {
|
||||||
LOG_VERBOSE() fprintf(stderr, "HTTP connection %ld closed.\n", user_data->conn_id);
|
if (nc->user_data != NULL) {
|
||||||
free(nc->user_data);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
|
|
@ -25,8 +25,14 @@
|
||||||
#ifndef __WEB_SERVER_H__
|
#ifndef __WEB_SERVER_H__
|
||||||
#define __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);
|
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);
|
void web_server_free(void *arg_mgr);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue