From ef3bad6e14eeded9832f3d24ae60bc6303ff015e Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Sun, 1 Dec 2024 17:15:23 +0100 Subject: rest: consolidate rest API handlers add struct og_rest_ctx and use it to consolidate rest API handlers and to ease future extensibility. --- src/rest.c | 577 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 292 insertions(+), 285 deletions(-) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 68b24a0..2f3acc0 100644 --- a/src/rest.c +++ b/src/rest.c @@ -434,11 +434,16 @@ static int og_json_client_wol_append(json_t *array, return 0; } -static int og_cmd_get_clients(json_t *element, struct og_msg_params *params, - char *buffer_reply) +struct og_rest_ctx { + json_t *json; + struct og_msg_params params; + char buf_reply[OG_MSG_RESPONSE_MAXLEN]; +}; + +static int og_cmd_get_clients(struct og_rest_ctx *ctx) { struct og_buffer og_buffer = { - .data = buffer_reply, + .data = ctx->buf_reply, }; struct og_client_wol *cli_wol; struct og_client *client; @@ -513,9 +518,10 @@ struct og_client_wol *og_client_wol_find(const struct in_addr *addr) return NULL; } -static int og_cmd_wol(json_t *element, struct og_msg_params *params) +static int og_cmd_wol(struct og_rest_ctx *ctx) { char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {}; + struct og_msg_params *params = &ctx->params; struct og_msg_params new_params = {}; struct og_client_wol *cli_wol; struct in_addr addr, netmask; @@ -529,10 +535,10 @@ static int og_cmd_wol(json_t *element, struct og_msg_params *params) json_t *value; int sd; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "type")) { @@ -663,17 +669,18 @@ static int og_json_parse_run(json_t *element, struct og_msg_params *params) return 0; } -static int og_cmd_run_post(json_t *element, struct og_msg_params *params) +static int og_cmd_run_post(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *clients; const char *key; bool inline_cmd; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else if (!strcmp(key, "run")) @@ -692,27 +699,27 @@ static int og_cmd_run_post(json_t *element, struct og_msg_params *params) OG_REST_PARAM_INLINE)) return -1; - clients = json_copy(element); + clients = json_copy(ctx->json); json_object_del(clients, "clients"); return og_send_request(OG_METHOD_POST, OG_CMD_SHELL_RUN, params, clients, NULL); } -static int og_cmd_run_get(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_run_get(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_buffer og_buffer = { - .data = buffer_reply, + .data = ctx->buf_reply, }; json_t *root, *value, *array; const char *key; unsigned int i; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -841,10 +848,10 @@ static int og_get_client_scripts(struct list_head *script_list) return 0; } -static int og_cmd_shell_list(char *buffer_reply) +static int og_cmd_shell_list(struct og_rest_ctx *ctx) { struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; struct script_entry *entry; json_t *root, *script_arr; @@ -883,16 +890,17 @@ static int og_cmd_shell_list(char *buffer_reply) return 0; } -static int og_cmd_session(json_t *element, struct og_msg_params *params) +static int og_cmd_session(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *clients, *value; const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "disk")) { @@ -912,7 +920,7 @@ static int og_cmd_session(json_t *element, struct og_msg_params *params) OG_REST_PARAM_PARTITION)) return -1; - clients = json_copy(element); + clients = json_copy(ctx->json); json_object_del(clients, "clients"); return og_send_request(OG_METHOD_POST, OG_CMD_SESSION, params, clients, NULL); @@ -962,19 +970,19 @@ static int og_json_os_array_get(struct og_dbi *dbi, json_t *array, const char *i return 0; } -static int og_cmd_get_session(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_session(struct og_rest_ctx *ctx) { json_t *value, *root, *array, *client_data, *partitions; + struct og_msg_params *params = &ctx->params; struct og_dbi *dbi; const char *key; int err = 0, i; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "client")) /* alias for backward compatibility */ err = og_json_parse_clients(value, params); else if (!strcmp(key, "clients")) @@ -1049,16 +1057,17 @@ static int og_cmd_get_session(json_t *element, struct og_msg_params *params, return 0; } -static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) +static int og_cmd_poweroff(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -1072,16 +1081,17 @@ static int og_cmd_poweroff(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_POST, OG_CMD_POWEROFF, params, NULL, NULL); } -static int og_cmd_refresh(json_t *element, struct og_msg_params *params) +static int og_cmd_refresh(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -1095,16 +1105,17 @@ static int og_cmd_refresh(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_GET, OG_CMD_REFRESH, params, NULL, NULL); } -static int og_cmd_reboot(json_t *element, struct og_msg_params *params) +static int og_cmd_reboot(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -1183,11 +1194,10 @@ static int og_get_boot_modes(struct list_head *boot_mode_list) return 0; } -static int og_cmd_get_modes(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_modes(struct og_rest_ctx *ctx) { struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; struct og_boot_mode *mode; LIST_HEAD(boot_mode_list); @@ -1718,8 +1728,9 @@ static int og_set_client_mode(struct og_dbi *dbi, const char *client_ip, return 0; } -static int og_cmd_post_modes(json_t *element, struct og_msg_params *params) +static int og_cmd_post_modes(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *mode_str, *mac; const char *msglog; struct og_dbi *dbi; @@ -1730,7 +1741,7 @@ static int og_cmd_post_modes(json_t *element, struct og_msg_params *params) int err = 0; int i; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "mode")) { @@ -1793,11 +1804,10 @@ static int og_cmd_post_modes(json_t *element, struct og_msg_params *params) return 0; } -static int og_cmd_get_client_setup(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_client_setup(struct og_rest_ctx *ctx) { json_t *value, *root, *partitions_array, *partition_json; + struct og_msg_params *params = &ctx->params; const char *key, *msglog; unsigned int len_part; struct og_dbi *dbi; @@ -1805,7 +1815,7 @@ static int og_cmd_get_client_setup(json_t *element, int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; struct { @@ -1822,7 +1832,7 @@ static int og_cmd_get_client_setup(json_t *element, int software; } partition; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "client")) { err = og_json_parse_clients(value, params); } @@ -1952,11 +1962,10 @@ static int og_dbi_update_client_repo(struct og_dbi *dbi, const char *mac, return 0; } -static int og_cmd_post_client_repo(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_client_repo(struct og_rest_ctx *ctx) { char ips_str[(OG_DB_IP_MAXLEN + 1) * OG_CLIENTS_MAX + 1] = {}; + struct og_msg_params *params = &ctx->params; const char *key, *msglog, *mac; int ips_str_len = 0; struct og_dbi *dbi; @@ -1965,7 +1974,7 @@ static int og_cmd_post_client_repo(json_t *element, int err = 0, i; json_t *value; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "id")) { @@ -2021,10 +2030,9 @@ static int og_cmd_post_client_repo(json_t *element, return 0; } -static int og_cmd_get_center_info(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_center_info(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_center center = {}; json_t *value, *root; struct og_dbi *dbi; @@ -2032,10 +2040,10 @@ static int og_cmd_get_center_info(json_t *element, int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, ¢er.id); params->flags |= OG_REST_PARAM_CENTER; @@ -2079,10 +2087,9 @@ static int og_cmd_get_center_info(json_t *element, return 0; } -static int og_cmd_get_client_info(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_client_info(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_computer computer = {}; json_t *value, *root; struct in_addr addr; @@ -2091,10 +2098,10 @@ static int og_cmd_get_client_info(json_t *element, int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "client")) { err = og_json_parse_clients(value, params); } @@ -2155,9 +2162,9 @@ static int og_cmd_get_client_info(json_t *element, return 0; } -static int og_cmd_post_center_update(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_center_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; uint32_t center_id; struct og_dbi *dbi; @@ -2165,7 +2172,7 @@ static int og_cmd_post_center_update(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string(value, ¶ms->name); params->flags |= OG_REST_PARAM_NAME; @@ -2250,10 +2257,9 @@ static int og_cmd_post_center_update(json_t *element, return 0; } -static int og_cmd_post_client_update(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_client_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog, *client_ip; struct og_computer computer = {}; struct og_dbi *dbi; @@ -2261,7 +2267,7 @@ static int og_cmd_post_client_update(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "ip")) { err = og_json_parse_string_copy(value, computer.ip, @@ -2614,16 +2620,16 @@ static int og_room_folder_update(struct og_dbi *dbi, struct og_folder *folder) #define OG_COMPUTER_FOLDER_MARKER 0x00010000 -static int og_cmd_post_folder_update(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_folder_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_folder folder = {}; struct og_dbi *dbi; const char *key; json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { params->flags |= OG_REST_PARAM_FOLDER; err = og_json_parse_uint(value, &folder.id); @@ -2657,16 +2663,16 @@ static int og_cmd_post_folder_update(json_t *element, return err; } -static int og_cmd_post_folder_add(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_folder_add(struct og_rest_ctx *ctx) { - const char *key; - json_t *value; + struct og_msg_params *params = &ctx->params; struct og_folder folder = {}; struct og_dbi *dbi; + const char *key; + json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "center")) { params->flags |= OG_REST_PARAM_CENTER; err = og_json_parse_uint(value, &folder.center); @@ -2789,16 +2795,16 @@ static int og_delete_room_folder(struct og_dbi *dbi, uint32_t folder_id) return 0; } -static int og_cmd_post_folder_delete(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_folder_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_dbi *dbi; uint32_t folder_id; const char *key; json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, &folder_id); params->flags |= OG_REST_PARAM_ID; @@ -2827,9 +2833,7 @@ static int og_cmd_post_folder_delete(json_t *element, return err; } -static int og_cmd_post_client_add(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_client_add(struct og_rest_ctx *ctx) { const char *room_name, *computer_name, *computer_ip, *hwaddr; struct og_computer computer = {}; @@ -2839,7 +2843,7 @@ static int og_cmd_post_client_add(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "serial_number")) { err = og_json_parse_string_copy(value, computer.serial_number, @@ -3002,9 +3006,9 @@ static int og_cmd_post_client_add(json_t *element, return 0; } -static int og_cmd_post_client_delete(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_client_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; @@ -3012,7 +3016,7 @@ static int og_cmd_post_client_delete(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -3059,9 +3063,9 @@ static int og_cmd_post_client_delete(json_t *element, return 0; } -static int og_cmd_post_client_move(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_client_move(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_computer computer = {}; const char *key, *msglog; struct og_dbi *dbi; @@ -3070,7 +3074,7 @@ static int og_cmd_post_client_move(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "room")) { @@ -3124,10 +3128,9 @@ static int og_cmd_post_client_move(json_t *element, return 0; } -static int og_cmd_get_room_info(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_room_info(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_room room = {}; json_t *value, *root; struct og_dbi *dbi; @@ -3136,10 +3139,10 @@ static int og_cmd_get_room_info(json_t *element, int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, &room_id); params->flags |= OG_REST_PARAM_ID; @@ -3185,16 +3188,17 @@ static int og_cmd_get_room_info(json_t *element, } -static int og_cmd_stop(json_t *element, struct og_msg_params *params) +static int og_cmd_stop(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -3208,16 +3212,17 @@ static int og_cmd_stop(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_POST, OG_CMD_STOP, params, NULL, NULL); } -static int og_cmd_hardware(json_t *element, struct og_msg_params *params) +static int og_cmd_hardware(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); @@ -3231,20 +3236,20 @@ static int og_cmd_hardware(json_t *element, struct og_msg_params *params) return og_send_request(OG_METHOD_GET, OG_CMD_HARDWARE, params, NULL, NULL); } -static int og_cmd_get_hardware(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_hardware(struct og_rest_ctx *ctx) { const char *key, *msglog, *hw_item, *hw_type; + struct og_msg_params *params = &ctx->params; json_t *value, *root, *array, *item; struct og_dbi *dbi; dbi_result result; int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "client")) err = og_json_parse_clients(value, params); @@ -3326,16 +3331,17 @@ static int og_cmd_get_hardware(json_t *element, struct og_msg_params *params, return 0; } -static int og_cmd_software(json_t *element, struct og_msg_params *params) +static int og_cmd_software(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *clients, *value; const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else if (!strcmp(key, "disk")) { @@ -3356,15 +3362,15 @@ static int og_cmd_software(json_t *element, struct og_msg_params *params) OG_REST_PARAM_PARTITION)) return -1; - clients = json_copy(element); + clients = json_copy(ctx->json); json_object_del(clients, "clients"); return og_send_request(OG_METHOD_GET, OG_CMD_SOFTWARE, params, clients, NULL); } -static int og_cmd_get_software(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_get_software(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *software, *root; const char *key, *msglog, *name; uint64_t disk, partition; @@ -3374,13 +3380,13 @@ static int og_cmd_get_software(json_t *element, struct og_msg_params *params, int err = 0; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "client")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "disk")) { @@ -3457,11 +3463,11 @@ static int og_cmd_get_software(json_t *element, struct og_msg_params *params, return 0; } -static const int og_cmd_get_repositories(char *buffer_reply) +static const int og_cmd_get_repositories(struct og_rest_ctx *ctx) { json_t *root, *repository, *repositories, *ips; struct og_buffer og_buffer = { - .data = buffer_reply, + .data = ctx->buf_reply, }; LIST_HEAD(repo_list); struct og_repo *repo; @@ -3586,11 +3592,11 @@ static json_t *og_json_image_alloc(struct og_image *image) return image_json; } -static int og_cmd_images(char *buffer_reply) +static int og_cmd_images(struct og_rest_ctx *ctx) { json_t *root, *images, *image_json, *disk_json; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; struct og_image image; struct og_dbi *dbi; @@ -3680,16 +3686,17 @@ static int og_cmd_images(char *buffer_reply) return 0; } -static int og_json_parse_create_image(json_t *element, struct og_msg_params *params) +static int og_json_parse_create_image(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "disk")) { err = og_json_parse_string(value, ¶ms->disk); params->flags |= OG_REST_PARAM_DISK; @@ -3735,18 +3742,18 @@ static int og_json_parse_create_image(json_t *element, struct og_msg_params *par return 0; } -static int og_cmd_add_image(json_t *element, struct og_msg_params *params, - bool update) +static int og_cmd_add_image(struct og_rest_ctx *ctx, bool update) { + struct og_msg_params *params = &ctx->params; char repository_ip[OG_DB_IP_MAXLEN + 1]; char new_image_id[OG_DB_INT_MAXLEN + 1]; - struct og_cmd_ctx ctx = {}; + struct og_cmd_ctx cmd_ctx = {}; struct og_dbi *dbi; json_t *body; int err = 0; bool found; - err = og_json_parse_create_image(element, params); + err = og_json_parse_create_image(ctx); if (err < 0) return err; @@ -3796,7 +3803,7 @@ static int og_cmd_add_image(json_t *element, struct og_msg_params *params, snprintf(new_image_id, sizeof(new_image_id), "%lu", params->image.id); params->id = new_image_id; } - json_object_set_new(element, "id", json_string(params->id)); + json_object_set_new(ctx->json, "id", json_string(params->id)); err = og_dbi_get_repository_ip(dbi, params->image.repo_id, params->ips_array[0], repository_ip); @@ -3804,37 +3811,38 @@ static int og_cmd_add_image(json_t *element, struct og_msg_params *params, if (err < 0) return err; - body = json_copy(element); + body = json_copy(ctx->json); json_object_del(body, "clients"); json_object_set_new(body, "repository", json_string(repository_ip)); json_object_set_new(body, "backup", json_boolean(params->backup)); - ctx.image.id = params->image.id; + cmd_ctx.image.id = params->image.id; return og_send_request(OG_METHOD_POST, OG_CMD_IMAGE_CREATE, params, - body, &ctx); + body, &cmd_ctx); } -static int og_cmd_create_image(json_t *element, struct og_msg_params *params) +static int og_cmd_create_image(struct og_rest_ctx *ctx) { - return og_cmd_add_image(element, params, false); + return og_cmd_add_image(ctx, false); } -static int og_cmd_update_image(json_t *element, struct og_msg_params *params) +static int og_cmd_update_image(struct og_rest_ctx *ctx) { - return og_cmd_add_image(element, params, true); + return og_cmd_add_image(ctx, true); } -static int og_json_parse_restore_image(json_t *element, struct og_msg_params *params) +static int og_json_parse_restore_image(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "disk")) { err = og_json_parse_string(value, ¶ms->disk); params->flags |= OG_REST_PARAM_DISK; @@ -3867,14 +3875,15 @@ static int og_json_parse_restore_image(json_t *element, struct og_msg_params *pa return 0; } -static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) +static int og_cmd_restore_image(struct og_rest_ctx *ctx) { char repository_ip[OG_DB_IP_MAXLEN + 1] = {}; + struct og_msg_params *params = &ctx->params; struct og_dbi *dbi; json_t *body; int err; - err = og_json_parse_restore_image(element, params); + err = og_json_parse_restore_image(ctx); if (err < 0) return err; @@ -3902,7 +3911,7 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) if (err < 0) return err; - body = json_copy(element); + body = json_copy(ctx->json); json_object_del(body, "clients"); json_object_set_new(body, "repository", json_string(repository_ip)); @@ -3910,18 +3919,19 @@ static int og_cmd_restore_image(json_t *element, struct og_msg_params *params) body, NULL); } -static int og_cmd_delete_image(json_t *element, struct og_msg_params *params) +static int og_cmd_delete_image(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_dbi *dbi; uint64_t image_id; const char *key; json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "image")) { err = og_json_parse_string(value, ¶ms->id); params->flags |= OG_REST_PARAM_ID; @@ -4007,8 +4017,9 @@ enum { OG_ATTR_IMAGE_SCOPE_ID = (1 << 0), }; -static int og_cmd_image_scope_update(json_t *element, struct og_msg_params *params) +static int og_cmd_image_scope_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_scope_image *scope; LIST_HEAD(scope_list); struct og_dbi *dbi; @@ -4019,10 +4030,10 @@ static int og_cmd_image_scope_update(json_t *element, struct og_msg_params *para json_t *value; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "image")) { err = og_json_parse_uint(value, &image_id); params->flags |= OG_ATTR_IMAGE_ID; @@ -4129,12 +4140,11 @@ static json_t *og_json_image_scope(struct og_dbi *dbi, uint32_t image_id) return scope_image; } -static int og_cmd_image_scope_list(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_image_scope_list(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; json_t *value, *scope_image; struct og_dbi *dbi; @@ -4142,10 +4152,10 @@ static int og_cmd_image_scope_list(json_t *element, const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "image")) { err = og_json_parse_uint(value, &image_id); params->flags |= OG_ATTR_IMAGE_ID; @@ -4266,22 +4276,22 @@ static int og_dbi_client_cache_get(struct og_dbi *dbi, json_t *clients, const ch return 0; } -static int og_cmd_cache_list(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_cache_list(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *root, *clients; struct og_dbi *dbi; const char *key; int err = 0, i; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else @@ -4403,22 +4413,22 @@ static int og_dbi_client_efi_get(struct og_dbi *dbi, json_t *clients, const char return 0; } -static int og_cmd_efi_list(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_efi_list(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *root, *clients; struct og_dbi *dbi; const char *key; int err = 0, i; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) err = og_json_parse_clients(value, params); else @@ -4471,17 +4481,17 @@ static int og_cmd_efi_list(json_t *element, struct og_msg_params *params, return 0; } -static int og_cmd_cache_delete(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_cache_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *body, *image_arr = NULL; const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "images")) { @@ -4501,26 +4511,26 @@ static int og_cmd_cache_delete(json_t *element, struct og_msg_params *params, if (!image_arr) return -1; - body = json_copy(element); + body = json_copy(ctx->json); json_object_del(body, "clients"); return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_DELETE, params, body, NULL); } -static int og_cmd_cache_fetch(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_cache_fetch(struct og_rest_ctx *ctx) { char repository_ip[OG_DB_IP_MAXLEN + 1] = {}; + struct og_msg_params *params = &ctx->params; json_t *value, *body; struct og_dbi *dbi; const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "image")) { @@ -4554,23 +4564,24 @@ static int og_cmd_cache_fetch(json_t *element, struct og_msg_params *params, if (err < 0) return err; - body = json_copy(element); + body = json_copy(ctx->json); json_object_del(body, "clients"); json_object_set_new(body, "repository", json_string(repository_ip)); return og_send_request(OG_METHOD_POST, OG_CMD_CACHE_FETCH, params, body, NULL); } -static int og_cmd_setup(json_t *element, struct og_msg_params *params) +static int og_cmd_setup(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; json_t *value, *clients; const char *key; int err = 0; - if (json_typeof(element) != JSON_OBJECT) + if (json_typeof(ctx->json) != JSON_OBJECT) return -1; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "type")) { @@ -4604,17 +4615,16 @@ static int og_cmd_setup(json_t *element, struct og_msg_params *params) OG_REST_PARAM_PART_3)) return -1; - clients = json_copy(element); + clients = json_copy(ctx->json); json_object_del(clients, "clients"); return og_send_request(OG_METHOD_POST, OG_CMD_SETUP, params, clients, NULL); } -static int og_cmd_scope_get(json_t *element, struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_scope_get(struct og_rest_ctx *ctx) { struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; json_t *root, *array; struct og_dbi *dbi; @@ -4658,10 +4668,10 @@ static int og_cmd_scope_get(json_t *element, struct og_msg_params *params, #define OG_LIVE_JSON_FILE_PATH "/opt/opengnsys/etc/ogliveinfo.json" -static int og_cmd_oglive_list(char *buffer_reply) +static int og_cmd_oglive_list(struct og_rest_ctx *ctx) { struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; const char *msglog, *live_name, *live_datetime; @@ -4739,17 +4749,16 @@ static int og_cmd_oglive_list(char *buffer_reply) return 0; } -static int og_cmd_post_center_add(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_center_add(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string(value, ¶ms->name); params->flags |= OG_REST_PARAM_NAME; @@ -4815,16 +4824,16 @@ static int og_cmd_post_center_add(json_t *element, return 0; } -static int og_cmd_post_center_delete(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_center_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_string(value, ¶ms->id); params->flags |= OG_REST_PARAM_ID; @@ -4900,10 +4909,9 @@ static int og_repo_list_find_ips(const char *ips_array[], uint32_t ips_array_len return 0; } -static int og_cmd_post_repository_update(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_repository_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; char name[OG_DB_REPO_NAME_MAXLEN]; const char *key, *msglog; unsigned int repo_id; @@ -4912,7 +4920,7 @@ static int og_cmd_post_repository_update(json_t *element, int err = 0, i; json_t *value; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, &repo_id); params->flags |= OG_REST_PARAM_ID; @@ -4992,10 +5000,9 @@ static int og_cmd_post_repository_update(json_t *element, return 0; } -static int og_cmd_post_repository_add(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_repository_add(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; char name[OG_DB_REPO_NAME_MAXLEN]; const char *key, *msglog; struct og_dbi *dbi; @@ -5004,7 +5011,7 @@ static int og_cmd_post_repository_add(json_t *element, json_t *value; uint32_t id; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string_copy(value, name, sizeof(name)); @@ -5073,9 +5080,9 @@ static int og_cmd_post_repository_add(json_t *element, return 0; } -static int og_cmd_post_repository_delete(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_repository_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; @@ -5083,7 +5090,7 @@ static int og_cmd_post_repository_delete(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, &repo_id); params->flags |= OG_REST_PARAM_ID; @@ -5129,9 +5136,9 @@ static int og_cmd_post_repository_delete(json_t *element, return 0; } -static int og_cmd_post_room_update(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_room_update(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_room room = {}; const char *key, *msglog; struct og_dbi *dbi; @@ -5139,7 +5146,7 @@ static int og_cmd_post_room_update(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string_copy(value, room.name, sizeof(room.name)); @@ -5236,9 +5243,9 @@ static int og_cmd_post_room_update(json_t *element, return 0; } -static int og_cmd_post_room_add(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_room_add(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_room room = {}; const char *key, *msglog; struct og_dbi *dbi; @@ -5246,7 +5253,7 @@ static int og_cmd_post_room_add(json_t *element, json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string_copy(value, room.name, sizeof(room.name)); @@ -5345,16 +5352,16 @@ static int og_cmd_post_room_add(json_t *element, return 0; } -static int og_cmd_post_room_delete(json_t *element, - struct og_msg_params *params) +static int og_cmd_post_room_delete(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_string(value, ¶ms->id); params->flags |= OG_REST_PARAM_ID; @@ -5398,11 +5405,11 @@ static int og_cmd_post_room_delete(json_t *element, return 0; } -static int og_cmd_get_servers(char *buffer_reply) +static int og_cmd_get_servers(struct og_rest_ctx *ctx) { json_t *root, *servers, *address; struct og_buffer og_buffer = { - .data = buffer_reply, + .data = ctx->buf_reply, }; char ipaddr[50] = {}; struct og_dbi *dbi; @@ -5455,12 +5462,11 @@ static int og_cmd_get_servers(char *buffer_reply) return ret; } -static int og_cmd_post_server(json_t *element, - struct og_msg_params *params, - char *buffer_reply) +static int og_cmd_post_server(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; struct og_buffer og_buffer = { - .data = buffer_reply, + .data = ctx->buf_reply, }; const char *key, *msglog; json_t *value, *root; @@ -5469,7 +5475,7 @@ static int og_cmd_post_server(json_t *element, struct og_dbi *dbi; dbi_result result; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "address")) { err = og_json_parse_string(value, ¶ms->name); params->flags |= OG_REST_PARAM_NAME; @@ -5550,9 +5556,9 @@ static int og_cmd_post_server(json_t *element, return ret; } -static int og_cmd_delete_server(json_t *element, - struct og_msg_params *params) +static int og_cmd_delete_server(struct og_rest_ctx *ctx) { + struct og_msg_params *params = &ctx->params; const char *key, *msglog; struct og_dbi *dbi; dbi_result result; @@ -5560,7 +5566,7 @@ static int og_cmd_delete_server(json_t *element, uint32_t id; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "id")) { err = og_json_parse_uint(value, &id); params->flags |= OG_REST_PARAM_ID; @@ -5626,9 +5632,10 @@ static int og_dbi_update_oglive(struct og_dbi *dbi, const char *mac, return 0; } -static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) +static int og_cmd_oglive_set(struct og_rest_ctx *ctx) { const char legacy_default_oglive_str[] = "ogLive"; + struct og_msg_params *params = &ctx->params; const char *oglive_str, *mac, *mode_str; const char *msglog; struct og_dbi *dbi; @@ -5639,7 +5646,7 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) int err = 0; int i; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "clients")) { err = og_json_parse_clients(value, params); } else if (!strcmp(key, "name")) { @@ -5712,7 +5719,7 @@ static int og_cmd_oglive_set(json_t *element, struct og_msg_params *params) return 0; } -static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params) +static int og_cmd_oglive_add(struct og_rest_ctx *ctx) { const char *oglive_str; bool is_update = false; @@ -5725,7 +5732,7 @@ static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params) json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string(value, &oglive_str); flags |= OG_REST_PARAM_NAME; @@ -5793,7 +5800,7 @@ static int og_cmd_oglive_add(json_t *element, struct og_msg_params *params) return 0; } -static int og_cmd_oglive_delete(json_t *element, struct og_msg_params *params) +static int og_cmd_oglive_delete(struct og_rest_ctx *ctx) { const char *oglive_str; const char *msglog; @@ -5804,7 +5811,7 @@ static int og_cmd_oglive_delete(json_t *element, struct og_msg_params *params) json_t *value; int err = 0; - json_object_foreach(element, key, value) { + json_object_foreach(ctx->json, key, value) { if (!strcmp(key, "name")) { err = og_json_parse_string(value, &oglive_str); flags |= OG_REST_PARAM_NAME; @@ -5866,11 +5873,11 @@ static int og_cmd_oglive_delete(json_t *element, struct og_msg_params *params) return 0; } -static int og_cmd_get_server_stats(char *buffer_reply) +static int og_cmd_get_server_stats(struct og_rest_ctx *ctx) { json_t *root, *time_obj, *memory, *swap; struct og_buffer og_buffer = { - .data = buffer_reply + .data = ctx->buf_reply }; struct sysinfo stats; time_t now; @@ -6132,8 +6139,7 @@ static void og_rest_log(const struct og_client *cli, enum og_rest_method method, int og_client_state_process_payload_rest(struct og_client *cli) { - char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {}; - struct og_msg_params params = {}; + struct og_rest_ctx ctx = {}; enum og_rest_method method; const char *cmd, *body; json_error_t json_err; @@ -6167,6 +6173,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) json_err.line, json_err.text); return og_client_not_found(cli); } + ctx.json = root; } uri = og_str_to_uri(cmd); @@ -6179,7 +6186,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) switch (method) { case OG_METHOD_GET: - err = og_cmd_get_clients(root, ¶ms, buf_reply); + err = og_cmd_get_clients(&ctx); break; default: err = og_client_method_not_found(cli); @@ -6193,7 +6200,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_client_repo(root, ¶ms, buf_reply); + err = og_cmd_post_client_repo(&ctx); break; default: err = og_client_method_not_found(cli); @@ -6213,7 +6220,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_client_setup(root, ¶ms, buf_reply); + err = og_cmd_get_client_setup(&ctx); } else if (!strncmp(cmd, "client/info", strlen("client/info"))) { if (method != OG_METHOD_GET) { @@ -6227,7 +6234,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_client_info(root, ¶ms, buf_reply); + err = og_cmd_get_client_info(&ctx); } else if (!strncmp(cmd, "client/add", strlen("client/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6241,7 +6248,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_client_add(root, ¶ms, buf_reply); + err = og_cmd_post_client_add(&ctx); } else if (!strncmp(cmd, "client/update", strlen("client/update"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6255,7 +6262,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_client_update(root, ¶ms, buf_reply); + err = og_cmd_post_client_update(&ctx); } else if (!strncmp(cmd, "client/delete", strlen("client/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6269,7 +6276,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_client_delete(root, ¶ms); + err = og_cmd_post_client_delete(&ctx); } else if (!strncmp(cmd, "client/move", strlen("client/move"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6283,7 +6290,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_client_move(root, ¶ms); + err = og_cmd_post_client_move(&ctx); } else if (!strncmp(cmd, "folder/add", strlen("folder/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6297,7 +6304,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_folder_add(root, ¶ms); + err = og_cmd_post_folder_add(&ctx); } else if (!strncmp(cmd, "folder/update", strlen("folder/update"))) { if (method != OG_METHOD_POST) { @@ -6312,7 +6319,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_folder_update(root, ¶ms); + err = og_cmd_post_folder_update(&ctx); } else if (!strncmp(cmd, "folder/delete", strlen("folder/delete"))) { if (method != OG_METHOD_POST) { @@ -6327,7 +6334,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_folder_delete(root, ¶ms); + err = og_cmd_post_folder_delete(&ctx); } else if (!strncmp(cmd, "wol", strlen("wol"))) { if (method != OG_METHOD_POST) { @@ -6340,7 +6347,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_wol(root, ¶ms); + err = og_cmd_wol(&ctx); } else if (!strncmp(cmd, "shell/run", strlen("shell/run"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6352,7 +6359,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_run_post(root, ¶ms); + err = og_cmd_run_post(&ctx); } else if (!strncmp(cmd, "shell/output", strlen("shell/output"))) { if (method != OG_METHOD_POST && method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6365,14 +6372,14 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_run_get(root, ¶ms, buf_reply); + err = og_cmd_run_get(&ctx); } else if (!strncmp(cmd, "shell/list", strlen("shell/list"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); goto err_process_rest_payload; } - err = og_cmd_shell_list(buf_reply); + err = og_cmd_shell_list(&ctx); } else if (!strncmp(cmd, "session", strlen("session"))) { if (method != OG_METHOD_POST && method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6386,9 +6393,9 @@ int og_client_state_process_payload_rest(struct og_client *cli) } if (method == OG_METHOD_POST) - err = og_cmd_session(root, ¶ms); + err = og_cmd_session(&ctx); else - err = og_cmd_get_session(root, ¶ms, buf_reply); + err = og_cmd_get_session(&ctx); } else if (!strncmp(cmd, "scopes", strlen("scopes"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6401,7 +6408,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_scope_get(root, ¶ms, buf_reply); + err = og_cmd_scope_get(&ctx); } else if (!strncmp(cmd, "poweroff", strlen("poweroff"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6413,7 +6420,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_poweroff(root, ¶ms); + err = og_cmd_poweroff(&ctx); } else if (!strncmp(cmd, "reboot", strlen("reboot"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6425,7 +6432,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_reboot(root, ¶ms); + err = og_cmd_reboot(&ctx); } else if (!strncmp(cmd, "mode", strlen("mode"))) { if (method != OG_METHOD_GET && method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6439,9 +6446,9 @@ int og_client_state_process_payload_rest(struct og_client *cli) } if (method == OG_METHOD_GET) - err = og_cmd_get_modes(root, ¶ms, buf_reply); + err = og_cmd_get_modes(&ctx); else if (method == OG_METHOD_POST) - err = og_cmd_post_modes(root, ¶ms); + err = og_cmd_post_modes(&ctx); } else if (!strncmp(cmd, "stop", strlen("stop"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6453,7 +6460,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_stop(root, ¶ms); + err = og_cmd_stop(&ctx); } else if (!strncmp(cmd, "refresh", strlen("refresh"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6465,7 +6472,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_refresh(root, ¶ms); + err = og_cmd_refresh(&ctx); } else if (!strncmp(cmd, "hardware", strlen("hardware"))) { if (method != OG_METHOD_GET && method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6479,9 +6486,9 @@ int og_client_state_process_payload_rest(struct og_client *cli) } if (method == OG_METHOD_GET) - err = og_cmd_get_hardware(root, ¶ms, buf_reply); + err = og_cmd_get_hardware(&ctx); else if (method == OG_METHOD_POST) - err = og_cmd_hardware(root, ¶ms); + err = og_cmd_hardware(&ctx); } else if (!strncmp(cmd, "software", strlen("software"))) { if (method != OG_METHOD_POST && method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6495,9 +6502,9 @@ int og_client_state_process_payload_rest(struct og_client *cli) } if (method == OG_METHOD_POST) - err = og_cmd_software(root, ¶ms); + err = og_cmd_software(&ctx); else - err = og_cmd_get_software(root, ¶ms, buf_reply); + err = og_cmd_get_software(&ctx); } else if (!strncmp(cmd, "repositories", strlen("repositories"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6509,7 +6516,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_repositories(buf_reply); + err = og_cmd_get_repositories(&ctx); } else if (!strncmp(cmd, "repository/add", strlen("repository/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6522,7 +6529,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_repository_add(root, ¶ms, buf_reply); + err = og_cmd_post_repository_add(&ctx); } else if (!strncmp(cmd, "repository/update", strlen("repository/update"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6535,7 +6542,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_repository_update(root, ¶ms, buf_reply); + err = og_cmd_post_repository_update(&ctx); } else if (!strncmp(cmd, "repository/delete", strlen("repository/delete"))) { if (method != OG_METHOD_POST) { @@ -6549,7 +6556,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_repository_delete(root, ¶ms); + err = og_cmd_post_repository_delete(&ctx); } else if (!strncmp(cmd, "images", strlen("images"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6561,7 +6568,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_images(buf_reply); + err = og_cmd_images(&ctx); } else if (!strncmp(cmd, "image/create", strlen("image/create"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6573,7 +6580,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_create_image(root, ¶ms); + err = og_cmd_create_image(&ctx); } else if (!strncmp(cmd, "image/update", strlen("image/update"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6585,7 +6592,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_update_image(root, ¶ms); + err = og_cmd_update_image(&ctx); } else if (!strncmp(cmd, "image/restore", strlen("image/restore"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6597,7 +6604,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_restore_image(root, ¶ms); + err = og_cmd_restore_image(&ctx); } else if (!strncmp(cmd, "image/delete", strlen("image/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6610,7 +6617,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_delete_image(root, ¶ms); + err = og_cmd_delete_image(&ctx); } else if (!strncmp(cmd, "image/restrict", strlen("image/restrict"))) { if (!root) { err = og_client_bad_request(cli); @@ -6619,10 +6626,10 @@ int og_client_state_process_payload_rest(struct og_client *cli) switch (method) { case OG_METHOD_GET: - err = og_cmd_image_scope_list(root, ¶ms, buf_reply); + err = og_cmd_image_scope_list(&ctx); break; case OG_METHOD_POST: - err = og_cmd_image_scope_update(root, ¶ms); + err = og_cmd_image_scope_update(&ctx); break; default: err = og_client_method_not_found(cli); @@ -6641,7 +6648,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_cache_list(root, ¶ms, buf_reply); + err = og_cmd_cache_list(&ctx); } else if (!strncmp(cmd, "efi", strlen("efi"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6654,7 +6661,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_efi_list(root, ¶ms, buf_reply); + err = og_cmd_efi_list(&ctx); } else if (!strncmp(cmd, "cache/delete", strlen("cache/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6667,7 +6674,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_cache_delete(root, ¶ms, buf_reply); + err = og_cmd_cache_delete(&ctx); } else if (!strncmp(cmd, "cache/fetch", strlen("cache/fetch"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6680,7 +6687,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_cache_fetch(root, ¶ms, buf_reply); + err = og_cmd_cache_fetch(&ctx); } else if (!strncmp(cmd, "setup", strlen("setup"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6692,7 +6699,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_setup(root, ¶ms); + err = og_cmd_setup(&ctx); } else if (!strncmp(cmd, "oglive/list", strlen("oglive/list"))) { if (method != OG_METHOD_GET) { @@ -6700,7 +6707,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_oglive_list(buf_reply); + err = og_cmd_oglive_list(&ctx); } else if (!strncmp(cmd, "oglive/add", strlen("oglive/add"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6713,7 +6720,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_oglive_add(root, ¶ms); + err = og_cmd_oglive_add(&ctx); } else if (!strncmp(cmd, "oglive/delete", strlen("oglive/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6726,7 +6733,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_oglive_delete(root, ¶ms); + err = og_cmd_oglive_delete(&ctx); } else if (!strncmp(cmd, "oglive/set", strlen("oglive/set"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6739,7 +6746,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_oglive_set(root, ¶ms); + err = og_cmd_oglive_set(&ctx); } else if (!strncmp(cmd, "center/add", strlen("center/add"))) { if (method != OG_METHOD_POST) { @@ -6747,7 +6754,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_post_center_add(root, ¶ms, buf_reply); + err = og_cmd_post_center_add(&ctx); } else if (!strncmp(cmd, "center/delete", strlen("center/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6760,7 +6767,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_center_delete(root, ¶ms); + err = og_cmd_post_center_delete(&ctx); } else if (!strncmp(cmd, "center/info", strlen("center/info"))) { if (method != OG_METHOD_GET) { @@ -6774,7 +6781,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_center_info(root, ¶ms, buf_reply); + err = og_cmd_get_center_info(&ctx); } else if (!strncmp(cmd, "center/update", strlen("center/update"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6787,7 +6794,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_center_update(root, ¶ms); + err = og_cmd_post_center_update(&ctx); } else if (!strncmp(cmd, "room/add", strlen("room/add"))) { if (method != OG_METHOD_POST) { @@ -6800,7 +6807,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_room_add(root, ¶ms); + err = og_cmd_post_room_add(&ctx); } else if (!strncmp(cmd, "room/update", strlen("room/update"))) { if (method != OG_METHOD_POST) { @@ -6813,7 +6820,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_room_update(root, ¶ms); + err = og_cmd_post_room_update(&ctx); } else if (!strncmp(cmd, "room/delete", strlen("room/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); @@ -6826,7 +6833,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_room_delete(root, ¶ms); + err = og_cmd_post_room_delete(&ctx); } else if (!strncmp(cmd, "room/info", strlen("room/info"))) { if (method != OG_METHOD_GET) { err = og_client_method_not_found(cli); @@ -6839,14 +6846,14 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_room_info(root, ¶ms, buf_reply); + err = og_cmd_get_room_info(&ctx); } else if (!strncmp(cmd, "server", strlen("server"))) { switch (method) { case OG_METHOD_GET: - err = og_cmd_get_servers(buf_reply); + err = og_cmd_get_servers(&ctx); break; case OG_METHOD_DELETE: - err = og_cmd_delete_server(root, ¶ms); + err = og_cmd_delete_server(&ctx); break; case OG_METHOD_POST: if (!root) { @@ -6854,7 +6861,7 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_bad_request(cli); goto err_process_rest_payload; } - err = og_cmd_post_server(root, ¶ms, buf_reply); + err = og_cmd_post_server(&ctx); break; default: err = og_client_method_not_found(cli); @@ -6866,20 +6873,20 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } - err = og_cmd_get_server_stats(buf_reply); + err = og_cmd_get_server_stats(&ctx); } else { syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd); err = og_client_not_found(cli); } - og_rest_log(cli, method, uri, ¶ms); + og_rest_log(cli, method, uri, &ctx.params); json_decref(root); if (err < 0) return og_client_bad_request(cli); - return og_client_ok(cli, buf_reply); + return og_client_ok(cli, ctx.buf_reply); err_process_rest_payload: syslog(LOG_ERR, "%s:%hu %.32s ...\n", -- cgit v1.2.3-18-g5258