Compare commits

...

5 Commits

Author SHA1 Message Date
5854ed1b32 Support reading OpenRPC documents from a file
Bump liberty, it generated incorrect help messages.
2020-10-13 21:48:24 +02:00
63c8a79479 Factor out init_backend()
The main() function is still way too long.
2020-10-13 21:07:40 +02:00
d489362a28 json-rpc-test-server: implement rpc.discover 2020-10-13 20:54:27 +02:00
c87869bef7 Cleanup
Prevent the last fuck-up from happening again.
2020-10-13 20:30:33 +02:00
fcf65f8377 Add libedit autocompletion back in
I've mistakenly removed it in the M-Enter change.
2020-10-13 20:19:19 +02:00
4 changed files with 111 additions and 56 deletions

View File

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

View File

@@ -663,6 +663,14 @@ input_el_install_prompt (struct input_el *self)
static unsigned char input_el_on_complete (EditLine *editline, int key);
static void
input_el_addbind (EditLine *editline, const char *name, const char *desc,
unsigned char (*function) (EditLine *, int), const char *binding)
{
el_set (editline, EL_ADDFN, name, desc, function);
el_set (editline, EL_BIND, binding, name, NULL);
}
static void
input_el_start (struct input *input, const char *program_name)
{
@@ -681,16 +689,15 @@ input_el_start (struct input *input, const char *program_name)
el_set (self->editline, EL_BIND, "^u", "vi-kill-line-prev", NULL);
// It's probably better to handle these ourselves
el_set (self->editline, EL_ADDFN,
"send-line", "Send line", input_el_on_return);
el_set (self->editline, EL_BIND, "\n", "send-line", NULL);
el_set (self->editline, EL_ADDFN,
"run-editor", "Run editor to edit line", input_el_on_run_editor);
el_set (self->editline, EL_BIND, "M-e", "run-editor", NULL);
input_el_addbind (self->editline, "send-line", "Send line",
input_el_on_return, "\n");
input_el_addbind (self->editline, "run-editor", "Run editor to edit line",
input_el_on_run_editor, "M-e");
el_set (self->editline, EL_ADDFN,
"newline-insert", "Insert a newline", input_el_on_newline_insert);
el_set (self->editline, EL_BIND, "M-\n", "newline-insert", NULL);
input_el_addbind (self->editline, "complete", "Complete word",
input_el_on_complete, "\t");
input_el_addbind (self->editline, "newline-insert", "Insert a newline",
input_el_on_newline_insert, "M-\n");
// Source the user's defaults file
el_source (self->editline, NULL);
@@ -1089,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
@@ -3236,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);
@@ -3254,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)))
parse_rpc_discover (ctx, &buf, &error);
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);
if (error)
{
@@ -3529,7 +3545,7 @@ init_watchers (struct app_context *ctx)
static void
parse_program_arguments (struct app_context *ctx, int argc, char **argv,
char **origin, char **endpoint)
const char **origin, const char **endpoint, const char **openrpc)
{
static const struct opt opts[] =
{
@@ -3539,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" },
@@ -3569,7 +3586,8 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
exit (EXIT_SUCCESS);
case 'o': *origin = optarg; break;
case 'O': ctx->openrpc = true; 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;
@@ -3611,27 +3629,9 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
opt_handler_free (&oh);
}
int
main (int argc, char *argv[])
static void
init_backend (struct app_context *ctx, const char *origin, const char *endpoint)
{
g_ctx.config = config_make ();
register_config_modules (&g_ctx);
config_load (&g_ctx.config, config_item_object ());
char *origin = NULL;
char *endpoint = NULL;
parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint);
g_ctx.input = input_new ();
g_ctx.input->user_data = &g_ctx;
g_ctx.input->on_input = process_input;
g_ctx.input->on_run_editor = run_editor;
g_ctx.input->complete_start_word = complete_method_name;
g_ctx.methods = str_map_make (NULL);
init_colors (&g_ctx);
load_configuration (&g_ctx);
struct http_parser_url url;
if (http_parser_parse_url (endpoint, strlen (endpoint), false, &url))
exit_fatal ("invalid endpoint address");
@@ -3642,23 +3642,45 @@ main (int argc, char *argv[])
url.field_data[UF_SCHEMA].off,
url.field_data[UF_SCHEMA].len);
// TODO: try to avoid the need to pass application context to backends
// TODO: try to avoid the need to pass the application context to backends
if (!strcasecmp_ascii (url_schema, "http")
|| !strcasecmp_ascii (url_schema, "https"))
g_ctx.backend = backend_curl_new (&g_ctx, endpoint);
ctx->backend = backend_curl_new (ctx, endpoint);
else if (!strcasecmp_ascii (url_schema, "ws")
|| !strcasecmp_ascii (url_schema, "wss"))
g_ctx.backend = backend_ws_new (&g_ctx, endpoint, &url);
ctx->backend = backend_ws_new (ctx, endpoint, &url);
else
exit_fatal ("unsupported protocol");
free (url_schema);
if (origin)
{
origin = xstrdup_printf ("Origin: %s", origin);
g_ctx.backend->vtable->add_header (g_ctx.backend, origin);
char *header = xstrdup_printf ("Origin: %s", origin);
g_ctx.backend->vtable->add_header (g_ctx.backend, header);
free (header);
}
free (origin);
}
int
main (int argc, char *argv[])
{
g_ctx.config = config_make ();
register_config_modules (&g_ctx);
config_load (&g_ctx.config, config_item_object ());
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;
g_ctx.input->on_input = process_input;
g_ctx.input->on_run_editor = run_editor;
g_ctx.input->complete_start_word = complete_method_name;
g_ctx.methods = str_map_make (NULL);
init_colors (&g_ctx);
load_configuration (&g_ctx);
init_backend (&g_ctx, origin, endpoint);
// We only need to convert to and from the terminal encoding
setlocale (LC_CTYPE, "");
@@ -3708,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

View File

@@ -1446,6 +1446,38 @@ json_rpc_handler_info_cmp (const void *first, const void *second)
((struct json_rpc_handler_info *) second)->method_name);
}
static json_t *
open_rpc_describe (const char *method, json_t *result)
{
return json_pack ("{sssoso}", "name", method, "params", json_pack ("[]"),
"result", json_pack ("{ssso}", "name", method, "schema", result));
}
// This server rarely sees changes and we can afford to hardcode the schema
static json_t *
json_rpc_discover (struct server_context *ctx, json_t *params)
{
(void) ctx;
(void) params;
json_t *info = json_pack ("{ssss}",
"title", PROGRAM_NAME, "version", PROGRAM_VERSION);
json_t *methods = json_pack ("[ooo]",
open_rpc_describe ("date", json_pack ("{ssso}", "type", "object",
"properties", json_pack ("{s{ss}s{ss}s{ss}s{ss}s{ss}s{ss}}",
"year", "type", "number",
"month", "type", "number",
"day", "type", "number",
"hours", "type", "number",
"minutes", "type", "number",
"seconds", "type", "number"))),
open_rpc_describe ("ping", json_pack ("{ss}", "type", "string")),
open_rpc_describe ("rpc.discover", json_pack ("{ss}", "$ref",
"https://github.com/open-rpc/meta-schema/raw/master/schema.json")));
return json_rpc_response (NULL, json_pack ("{sssoso}",
"openrpc", "1.2.6", "info", info, "methods", methods), NULL);
}
static json_t *
json_rpc_ping (struct server_context *ctx, json_t *params)
{
@@ -1489,6 +1521,7 @@ process_json_rpc_request (struct server_context *ctx, json_t *request)
{
{ "date", json_rpc_date },
{ "ping", json_rpc_ping },
{ "rpc.discover", json_rpc_discover },
};
if (!json_is_object (request))

Submodule liberty updated: e029aae1d3...69101eb155