From 961a286a2742a0a321c6447d6bbe2fa174c8fc53 Mon Sep 17 00:00:00 2001 From: Pawel Wodkowski Date: Fri, 12 Oct 2018 20:17:26 +0200 Subject: [PATCH] jsonrpc: simplify parsing client response Capture json version by reference so we don't need to call free() in parse_single_response. Change-Id: Ia97fa484ca9ff8692a15160878b625b57d7f4b9e Signed-off-by: Pawel Wodkowski Reviewed-on: https://review.gerrithub.io/429261 Chandler-Test-Pool: SPDK Automated Test System Tested-by: SPDK CI Jenkins Reviewed-by: Ben Walker Reviewed-by: Shuhei Matsumoto Reviewed-by: Jim Harris --- lib/jsonrpc/jsonrpc_client.c | 53 +++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/jsonrpc/jsonrpc_client.c b/lib/jsonrpc/jsonrpc_client.c index d2cf22068..2426f4e3b 100644 --- a/lib/jsonrpc/jsonrpc_client.c +++ b/lib/jsonrpc/jsonrpc_client.c @@ -35,13 +35,39 @@ #include "jsonrpc_internal.h" struct jsonrpc_response { - char *version; + const struct spdk_json_val *version; const struct spdk_json_val *id; const struct spdk_json_val *result; }; static int -capture_val(const struct spdk_json_val *val, void *out) +capture_string(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + if (spdk_json_strequal(val, "2.0") != true) { + return SPDK_JSON_PARSE_INVALID; + } + + *vptr = val; + return 0; +} + +static int +capture_id(const struct spdk_json_val *val, void *out) +{ + const struct spdk_json_val **vptr = out; + + if (val->type != SPDK_JSON_VAL_STRING && val->type != SPDK_JSON_VAL_NUMBER) { + return SPDK_JSON_PARSE_INVALID; + } + + *vptr = val; + return 0; +} + +static int +capture_any(const struct spdk_json_val *val, void *out) { const struct spdk_json_val **vptr = out; @@ -50,9 +76,9 @@ capture_val(const struct spdk_json_val *val, void *out) } static const struct spdk_json_object_decoder jsonrpc_response_decoders[] = { - {"jsonrpc", offsetof(struct jsonrpc_response, version), spdk_json_decode_string}, - {"id", offsetof(struct jsonrpc_response, id), capture_val}, - {"result", offsetof(struct jsonrpc_response, result), capture_val}, + {"jsonrpc", offsetof(struct jsonrpc_response, version), capture_string}, + {"id", offsetof(struct jsonrpc_response, id), capture_id}, + {"result", offsetof(struct jsonrpc_response, result), capture_any}, }; static int @@ -61,27 +87,10 @@ parse_single_response(struct spdk_json_val *values, void *parser_ctx) { struct jsonrpc_response resp = {}; - int rc = 0; if (spdk_json_decode_object(values, jsonrpc_response_decoders, SPDK_COUNTOF(jsonrpc_response_decoders), &resp)) { - rc = SPDK_JSON_PARSE_INVALID; - goto done; - } - - if (strcmp(resp.version, "2.0")) { - rc = SPDK_JSON_PARSE_INVALID; - goto done; - } - - if (resp.id->type != SPDK_JSON_VAL_STRING && resp.id->type != SPDK_JSON_VAL_NUMBER) { - rc = SPDK_JSON_PARSE_INVALID; - } - -done: - free(resp.version); - if (rc) { return SPDK_JSON_PARSE_INVALID; }