add: schedule tags endpoint

fix: memory leak for string args
This commit is contained in:
Tobias Reisinger 2020-05-06 10:53:42 +02:00
parent a127a68e31
commit f040cd8b21
6 changed files with 71 additions and 2 deletions

View file

@ -195,7 +195,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, stru
{
(void)hm;
char *target_uid_str = args[0].value.v_str;
const char *target_uid_str = args[0].value.v_str;
uuid_t target_uid;
if(schedule_uid_parse(target_uid_str, target_uid))

View file

@ -0,0 +1,57 @@
#include <cJSON.h>
#include <constants.h>
#include <endpoints/api_v1_schedules.h>
#include <logger.h>
#include <models/junction_tag.h>
#include <models/schedule.h>
#include <models/tag.h>
void
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
{
(void)hm;
int tag_id = tag_get_id(args[0].value.v_str);
int *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
if(schedules_ids == NULL)
{
LOG_ERROR("failed to print schedules json\n");
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "[]");
return;
}
cJSON *json = cJSON_CreateArray();
for(int i = 0; schedules_ids[i] != 0; ++i)
{
schedule_t* schedule = schedule_get_by_id(schedules_ids[i]);
if(!schedule)
{
continue;
}
cJSON *json_schedule = schedule_to_json(schedule);
cJSON_AddItemToArray(json, json_schedule);
schedule_free(schedule);
}
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print schedules json\n");
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "[]");
}
else
{
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "%s", json_str);
free(json_str);
}
cJSON_Delete(json);
free(schedules_ids);
}

View file

@ -16,6 +16,14 @@ handler_connection(struct mg_connection *c, int ev, void *p)
if(endpoint && endpoint->func)
{
endpoint->func(c, endpoint->args, p);
for(int i = 0; i < endpoint->args_count; ++i)
{
if(endpoint->args[i].type == ENDPOINT_ARG_TYPE_STR)
{
free((char*)endpoint->args[i].value.v_str);
}
}
}
else
{

View file

@ -18,4 +18,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
void
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
void
api_v1_schedules_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
#endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */

View file

@ -17,7 +17,7 @@ typedef struct
union
{
int v_int;
char *v_str;
const char *v_str;
} value;
} endpoint_args_t;

View file

@ -59,6 +59,7 @@ router_init()
router_register_endpoint("/api/v1/schedules/{str}/", HTTP_METHOD_GET, api_v1_schedules_STR_GET);
router_register_endpoint("/api/v1/schedules/{str}/", HTTP_METHOD_PUT, api_v1_schedules_STR_PUT);
router_register_endpoint("/api/v1/schedules/{str}/", HTTP_METHOD_DELETE, api_v1_schedules_STR_DELETE);
router_register_endpoint("/api/v1/schedules/tag/{str}/", HTTP_METHOD_GET, api_v1_schedules_tag_STR_GET);
router_register_endpoint("/api/v1/controllers/discover/", HTTP_METHOD_POST, api_v1_controllers_discover_POST);
router_register_endpoint("/api/v1/controllers/", HTTP_METHOD_GET, api_v1_controllers_GET);