MT#61856 control_ng: add `prepare_resp_ctx()` for resp ctx

When preparing a response, especially for the case of error,
we already make sure to init the resp ctx. So just add
a helper for this, because same routine used.

Also optionally init parser as native (dict) if required.
Some of the cases like data is <= 0 length, or
there has been not dict or JSON format spotted,
then init as native.

Change-Id: I7d13669075525b754e1950e3212b79800b20f85a
mr26.1
Donat Zenichev 4 weeks ago
parent 95c59e1035
commit c6ef37035d

@ -688,6 +688,19 @@ ng_buffer *ng_buffer_new(struct obj *ref) {
return ngbuf;
}
/**
* Initialize resp context.
*/
static void prepare_resp_ctx(ng_command_ctx_t *command_ctx, const ng_parser_t *parser,
bool native)
{
/* init as dict by default, if requested */
if (native)
ng_parser_native.init(&command_ctx->parser_ctx, &command_ctx->ngbuf->buffer);
command_ctx->resp = parser->dict(&command_ctx->parser_ctx);
assert(command_ctx->resp.gen != NULL);
}
static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, const endpoint_t *sin, char *addr, struct obj *ref,
struct ng_buffer **ngbufp)
{
@ -704,10 +717,7 @@ static void control_ng_process_payload(ng_ctx *hctx, str *reply, str *data, cons
errstr = "Invalid data (no payload)";
if (data->len <= 0) {
ng_parser_native.init(&command_ctx.parser_ctx, &command_ctx.ngbuf->buffer);
command_ctx.resp = parser->dict(&command_ctx.parser_ctx); // init as dict by default
assert(command_ctx.resp.gen != NULL);
prepare_resp_ctx(&command_ctx, parser, true);
goto err_send;
}
@ -718,8 +728,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) {
command_ctx.resp = parser->dict(&command_ctx.parser_ctx);
assert(command_ctx.resp.gen != NULL);
prepare_resp_ctx(&command_ctx, parser, false);
goto err_send;
}
}
@ -730,24 +739,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)) {
command_ctx.resp = parser->dict(&command_ctx.parser_ctx);
assert(command_ctx.resp.gen != NULL);
prepare_resp_ctx(&command_ctx, parser, false);
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 || !ng_parser_json.is_dict(command_ctx.req)) {
command_ctx.resp = parser->dict(&command_ctx.parser_ctx);
assert(command_ctx.resp.gen != NULL);
prepare_resp_ctx(&command_ctx, parser, false);
goto err_send;
}
}
else {
ng_parser_native.init(&command_ctx.parser_ctx, &command_ctx.ngbuf->buffer);
command_ctx.resp = parser->dict(&command_ctx.parser_ctx); // init as dict by default
assert(command_ctx.resp.gen != NULL);
prepare_resp_ctx(&command_ctx, parser, true);
errstr = "Invalid NG data format";
goto err_send;
}

Loading…
Cancel
Save