Commit 961a286a authored by Pawel Wodkowski's avatar Pawel Wodkowski Committed by Jim Harris
Browse files

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: default avatarPawel Wodkowski <pawelx.wodkowski@intel.com>
Reviewed-on: https://review.gerrithub.io/429261


Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Reviewed-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 8397285b
Loading
Loading
Loading
Loading
+31 −22
Original line number Diff line number Diff line
@@ -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;
	}