From 5854ed1b32be470b2ca9c126681fa4b417b94d37 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Tue, 13 Oct 2020 21:15:56 +0200 Subject: Support reading OpenRPC documents from a file Bump liberty, it generated incorrect help messages. --- json-rpc-shell.adoc | 4 ++-- json-rpc-shell.c | 47 +++++++++++++++++++++++++++++------------------ liberty | 2 +- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/json-rpc-shell.adoc b/json-rpc-shell.adoc index 595dd68..31f78b7 100644 --- a/json-rpc-shell.adoc +++ b/json-rpc-shell.adoc @@ -76,9 +76,9 @@ Protocol *-o* _ORIGIN_, *--origin*=_ORIGIN_:: Set the HTTP Origin header to _ORIGIN_. Some servers may need this. -*-O*, *--openrpc*:: +*-O*[__PATH__], *--openrpc*[**=**__PATH__]:: Call "rpc.discover" upon start-up in order to pull in OpenRPC data for - tab completion of method names. + tab completion of method names. If a path is given, it is read from a file. Program information ~~~~~~~~~~~~~~~~~~~ diff --git a/json-rpc-shell.c b/json-rpc-shell.c index 44e2598..ea4fba7 100644 --- a/json-rpc-shell.c +++ b/json-rpc-shell.c @@ -1096,7 +1096,6 @@ static struct app_context bool compact; ///< Whether to not pretty print bool verbose; ///< Print requests bool trust_all; ///< Don't verify peer certificates - bool openrpc; ///< OpenRPC method name completion bool null_as_id; ///< JSON null is used as an ID int64_t next_id; ///< Next autogenerated ID @@ -3243,16 +3242,17 @@ parse_rpc_discover (struct app_context *ctx, struct str *buf, struct error **e) { // Just optimistically punch through, I don't have time for this shit json_error_t error; - json_t *response = NULL, *result = NULL, *value = NULL; + json_t *response = NULL, *methods = NULL, *value = NULL; if (!(response = json_loadb (buf->str, buf->len, 0, &error))) error_set (e, "parse failure: %s", error.text); - else if (!(result = json_object_get (response, "result")) - || !(result = json_object_get (result, "methods"))) + else if ((!(methods = json_object_get (response, "result")) + || !(methods = json_object_get (methods, "methods"))) + && !(methods = json_object_get (response, "methods"))) error_set (e, "unsupported"); else { const char *name = NULL; - for (size_t i = 0; (value = json_array_get (result, i)); i++) + for (size_t i = 0; (value = json_array_get (methods, i)); i++) if ((value = json_object_get (value, "name")) && (name = json_string_value (value))) str_map_set (&ctx->methods, name, (void *) 1); @@ -3261,17 +3261,26 @@ parse_rpc_discover (struct app_context *ctx, struct str *buf, struct error **e) } static void -init_openrpc (struct app_context *ctx) +init_openrpc (struct app_context *ctx, const char *openrpc) { - if (!ctx->openrpc) + // Three possibilities: NULL for not requested, "" for rpc.discover, + // and anything else has to be a path to a JSON file + if (!openrpc) return; - json_t *id = json_integer (ctx->next_id++); struct str buf = str_make (); struct error *error; - if (!(error = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf))) + if (*openrpc) + // This may or may not have a protocol envelope + read_file (openrpc, &buf, &error); + else + { + json_t *id = json_integer (ctx->next_id++); + error = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf); + json_decref (id); + } + if (!error) parse_rpc_discover (ctx, &buf, &error); - json_decref (id); if (error) { @@ -3536,7 +3545,7 @@ init_watchers (struct app_context *ctx) static void parse_program_arguments (struct app_context *ctx, int argc, char **argv, - const char **origin, const char **endpoint) + const char **origin, const char **endpoint, const char **openrpc) { static const struct opt opts[] = { @@ -3546,10 +3555,11 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv, { 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" }, { 'o', "origin", "O", 0, "set the HTTP Origin header" }, // So far you have to explicitly enable this rather than disable - { 'O', "openrpc", NULL, 0, "method name completion using OpenRPC" }, + { 'O', "openrpc", "PATH", OPT_OPTIONAL_ARG, + "method name completion using OpenRPC" }, { 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" }, { 'v', "verbose", NULL, 0, "print raw requests and responses" }, - { 'w', "write-default-cfg", "FILENAME", + { 'w', "write-default-cfg", "PATH", OPT_OPTIONAL_ARG | OPT_LONG_ONLY, "write a default configuration file and exit" }, { 'd', "debug", NULL, 0, "run in debug mode" }, @@ -3575,8 +3585,9 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv, printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); exit (EXIT_SUCCESS); - case 'o': *origin = optarg; break; - case 'O': ctx->openrpc = true; break; + case 'o': *origin = optarg; break; + case 'O': *openrpc = optarg ? optarg : ""; break; + case 'n': ctx->null_as_id = true; break; case 'c': ctx->compact = true; break; case 't': ctx->trust_all = true; break; @@ -3657,8 +3668,8 @@ main (int argc, char *argv[]) register_config_modules (&g_ctx); config_load (&g_ctx.config, config_item_object ()); - const char *origin = NULL, *endpoint = NULL; - parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint); + const char *origin = NULL, *endpoint = NULL, *openrpc = NULL; + parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint, &openrpc); g_ctx.input = input_new (); g_ctx.input->user_data = &g_ctx; @@ -3719,7 +3730,7 @@ main (int argc, char *argv[]) g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME); ev_set_userdata (EV_DEFAULT_ &g_ctx); - init_openrpc (&g_ctx); + init_openrpc (&g_ctx, openrpc); ev_run (EV_DEFAULT_ 0); // User has terminated the program, let's save the history and clean up diff --git a/liberty b/liberty index e029aae..69101eb 160000 --- a/liberty +++ b/liberty @@ -1 +1 @@ -Subproject commit e029aae1d3d1884ca868c3694bdec0456b3e8267 +Subproject commit 69101eb1554ad2fca6de30cdbaccac076210d7e3 -- cgit v1.2.3-70-g09d2