Browse Source

MT#55283 add abstract dict() method

Change-Id: I305f1fc3e4cab7961ca4d38c35fb8359c3a63856
pull/1848/head
Richard Fuchs 1 year ago
parent
commit
cc43740ce0
3 changed files with 10 additions and 4 deletions
  1. +8
    -3
      daemon/control_ng.c
  2. +1
    -1
      daemon/statistics.c
  3. +1
    -0
      include/control_ng.h

+ 8
- 3
daemon/control_ng.c View File

@ -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)


+ 1
- 1
daemon/statistics.c View File

@ -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);


+ 1
- 0
include/control_ng.h View File

@ -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;


Loading…
Cancel
Save