From cc43740ce0b6d626d34342f6a20482c410fb7044 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 26 Jul 2024 11:50:20 -0400 Subject: [PATCH] MT#55283 add abstract dict() method Change-Id: I305f1fc3e4cab7961ca4d38c35fb8359c3a63856 --- daemon/control_ng.c | 11 ++++++++--- daemon/statistics.c | 2 +- include/control_ng.h | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/daemon/control_ng.c b/daemon/control_ng.c index f3eb99b70..891b79f15 100644 --- a/daemon/control_ng.c +++ b/daemon/control_ng.c @@ -127,6 +127,9 @@ static void bencode_list_iter(ng_parser_ctx_t *ctx, bencode_item_t *list, static long long bencode_get_int(bencode_item_t *arg) { return arg->value; } +static bencode_item_t *__bencode_dict(ng_parser_ctx_t *ctx) { + return bencode_dictionary(&ctx->ngbuf->buffer); +} const ng_parser_t ng_parser_native = { .collapse = bencode_collapse_str, @@ -137,6 +140,7 @@ const ng_parser_t ng_parser_native = { .get_int_str = bencode_get_integer_str, .is_int = bencode_is_int, .get_int = bencode_get_int, + .dict = __bencode_dict, }; const ng_parser_t ng_parser_json = { .collapse = bencode_collapse_str_json, @@ -147,6 +151,7 @@ const ng_parser_t ng_parser_json = { .get_int_str = bencode_get_integer_str, .is_int = bencode_is_int, .get_int = bencode_get_int, + .dict = __bencode_dict, }; @@ -299,9 +304,6 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons parser_ctx.ngbuf = *ngbufp = ng_buffer_new(ref); - parser_ctx.resp = bencode_dictionary(&parser_ctx.ngbuf->buffer); - assert(parser_ctx.resp != NULL); - errstr = "Invalid data (no payload)"; if (data->len <= 0) goto err_send; @@ -333,6 +335,9 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons goto err_send; } + parser_ctx.resp = parser_ctx.parser->dict(&parser_ctx); + assert(parser_ctx.resp != NULL); + bencode_dictionary_get_str(parser_ctx.req, "command", &cmd); errstr = "Dictionary contains no key \"command\""; if (!cmd.s) diff --git a/daemon/statistics.c b/daemon/statistics.c index e48d432c6..2a72b8873 100644 --- a/daemon/statistics.c +++ b/daemon/statistics.c @@ -996,7 +996,7 @@ const char *statistics_ng(ng_parser_ctx_t *ctx) { // open bracket of some sort - new sub-entry follows bencode_item_t *sub = NULL; if (m->is_brace) - sub = bencode_dictionary(buf); + sub = ctx->parser->dict(ctx); else sub = bencode_list(buf); diff --git a/include/control_ng.h b/include/control_ng.h index 490504263..cecdf1906 100644 --- a/include/control_ng.h +++ b/include/control_ng.h @@ -121,6 +121,7 @@ struct ng_parser { long long (*get_int_str)(bencode_item_t *, long long def); bool (*is_int)(bencode_item_t *); long long (*get_int)(bencode_item_t *); + bencode_item_t *(*dict)(ng_parser_ctx_t *); }; struct ng_parser_ctx { const ng_parser_t *parser;