From bdb8dc412d3af837909183296546b81c00965c15 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Thu, 21 May 2026 14:25:25 +0200 Subject: [PATCH] MT#61856 control_ng: `prepare_resp_ctx()` check command ctx parser In order to understand whether is must be initialized. And use a given parser pointer to do so. This can be native or json parser. Remove previously introduced bool param, which was used for differentiation. Change-Id: I6d0bb083fe4dea5625d40175c8978fffaca6d34f --- daemon/control_ng.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/daemon/control_ng.c b/daemon/control_ng.c index db4c150eb..85207f655 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -691,15 +691,14 @@ ng_buffer *ng_buffer_new(struct obj *ref) { /** * Initialize resp context. */ -static void prepare_resp_ctx(ng_command_ctx_t *command_ctx, const ng_parser_t *parser, - bool native) +static void prepare_resp_ctx(ng_command_ctx_t *command_ctx, const ng_parser_t *parser) { - /* init as dict by default, if requested */ - if (native) + if (!command_ctx->parser_ctx.parser) parser->init(&command_ctx->parser_ctx, &command_ctx->ngbuf->buffer); + /* TODO: JSON-like structured data probably needs to have own `parser_arg` * because otherwise resp is always added as dictionary */ - command_ctx->resp = parser->dict(&command_ctx->parser_ctx); + command_ctx->resp = command_ctx->parser_ctx.parser->dict(&command_ctx->parser_ctx); assert(command_ctx->resp.gen != NULL); } @@ -720,7 +719,7 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons errstr = "Invalid data (no payload)"; if (data->len <= 0) { - prepare_resp_ctx(&command_ctx, parser, true); + prepare_resp_ctx(&command_ctx, parser); goto err_send; } @@ -731,7 +730,7 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons command_ctx.req.benc = bencode_decode_expect_str(&command_ctx.ngbuf->buffer, data, BENCODE_DICTIONARY); errstr = "Could not decode bencode dictionary"; if (!command_ctx.req.benc) { - prepare_resp_ctx(&command_ctx, parser, false); + prepare_resp_ctx(&command_ctx, parser); goto err_send; } } @@ -742,19 +741,19 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons command_ctx.ngbuf->json = json_parser_new(); errstr = "Failed to parse JSON document"; if (!json_parser_load_from_data(command_ctx.ngbuf->json, data->s, data->len, NULL)) { - prepare_resp_ctx(&command_ctx, json_parser, false); + prepare_resp_ctx(&command_ctx, json_parser); goto err_send; } command_ctx.req.json = json_parser_get_root(command_ctx.ngbuf->json); errstr = "Could not decode JSON dictionary"; if (!command_ctx.req.json || !json_parser->is_dict(command_ctx.req)) { - prepare_resp_ctx(&command_ctx, json_parser, false); + prepare_resp_ctx(&command_ctx, json_parser); goto err_send; } } else { - prepare_resp_ctx(&command_ctx, parser, true); + prepare_resp_ctx(&command_ctx, parser); errstr = "Invalid NG data format"; goto err_send; }