Compare commits

..

No commits in common. "5854ed1b32be470b2ca9c126681fa4b417b94d37" and "d489362a28395f47baff248fdd4170acb79c4cdb" have entirely different histories.

3 changed files with 50 additions and 65 deletions

View File

@ -76,9 +76,9 @@ Protocol
*-o* _ORIGIN_, *--origin*=_ORIGIN_:: *-o* _ORIGIN_, *--origin*=_ORIGIN_::
Set the HTTP Origin header to _ORIGIN_. Some servers may need this. Set the HTTP Origin header to _ORIGIN_. Some servers may need this.
*-O*[__PATH__], *--openrpc*[**=**__PATH__]:: *-O*, *--openrpc*::
Call "rpc.discover" upon start-up in order to pull in OpenRPC data for Call "rpc.discover" upon start-up in order to pull in OpenRPC data for
tab completion of method names. If a path is given, it is read from a file. tab completion of method names.
Program information Program information
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

View File

@ -1096,6 +1096,7 @@ static struct app_context
bool compact; ///< Whether to not pretty print bool compact; ///< Whether to not pretty print
bool verbose; ///< Print requests bool verbose; ///< Print requests
bool trust_all; ///< Don't verify peer certificates 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 bool null_as_id; ///< JSON null is used as an ID
int64_t next_id; ///< Next autogenerated ID int64_t next_id; ///< Next autogenerated ID
@ -3242,17 +3243,16 @@ 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 // Just optimistically punch through, I don't have time for this shit
json_error_t error; json_error_t error;
json_t *response = NULL, *methods = NULL, *value = NULL; json_t *response = NULL, *result = NULL, *value = NULL;
if (!(response = json_loadb (buf->str, buf->len, 0, &error))) if (!(response = json_loadb (buf->str, buf->len, 0, &error)))
error_set (e, "parse failure: %s", error.text); error_set (e, "parse failure: %s", error.text);
else if ((!(methods = json_object_get (response, "result")) else if (!(result = json_object_get (response, "result"))
|| !(methods = json_object_get (methods, "methods"))) || !(result = json_object_get (result, "methods")))
&& !(methods = json_object_get (response, "methods")))
error_set (e, "unsupported"); error_set (e, "unsupported");
else else
{ {
const char *name = NULL; const char *name = NULL;
for (size_t i = 0; (value = json_array_get (methods, i)); i++) for (size_t i = 0; (value = json_array_get (result, i)); i++)
if ((value = json_object_get (value, "name")) if ((value = json_object_get (value, "name"))
&& (name = json_string_value (value))) && (name = json_string_value (value)))
str_map_set (&ctx->methods, name, (void *) 1); str_map_set (&ctx->methods, name, (void *) 1);
@ -3261,26 +3261,17 @@ parse_rpc_discover (struct app_context *ctx, struct str *buf, struct error **e)
} }
static void static void
init_openrpc (struct app_context *ctx, const char *openrpc) init_openrpc (struct app_context *ctx)
{ {
// Three possibilities: NULL for not requested, "" for rpc.discover, if (!ctx->openrpc)
// and anything else has to be a path to a JSON file
if (!openrpc)
return; return;
json_t *id = json_integer (ctx->next_id++);
struct str buf = str_make (); struct str buf = str_make ();
struct error *error; struct error *error;
if (*openrpc) if (!(error = json_rpc_call_raw (ctx, "rpc.discover", id, NULL, &buf)))
// 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); parse_rpc_discover (ctx, &buf, &error);
json_decref (id);
if (error) if (error)
{ {
@ -3545,7 +3536,7 @@ init_watchers (struct app_context *ctx)
static void static void
parse_program_arguments (struct app_context *ctx, int argc, char **argv, parse_program_arguments (struct app_context *ctx, int argc, char **argv,
const char **origin, const char **endpoint, const char **openrpc) char **origin, char **endpoint)
{ {
static const struct opt opts[] = static const struct opt opts[] =
{ {
@ -3555,11 +3546,10 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
{ 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" }, { 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" },
{ 'o', "origin", "O", 0, "set the HTTP Origin header" }, { 'o', "origin", "O", 0, "set the HTTP Origin header" },
// So far you have to explicitly enable this rather than disable // So far you have to explicitly enable this rather than disable
{ 'O', "openrpc", "PATH", OPT_OPTIONAL_ARG, { 'O', "openrpc", NULL, 0, "method name completion using OpenRPC" },
"method name completion using OpenRPC" },
{ 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" }, { 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" },
{ 'v', "verbose", NULL, 0, "print raw requests and responses" }, { 'v', "verbose", NULL, 0, "print raw requests and responses" },
{ 'w', "write-default-cfg", "PATH", { 'w', "write-default-cfg", "FILENAME",
OPT_OPTIONAL_ARG | OPT_LONG_ONLY, OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
"write a default configuration file and exit" }, "write a default configuration file and exit" },
{ 'd', "debug", NULL, 0, "run in debug mode" }, { 'd', "debug", NULL, 0, "run in debug mode" },
@ -3585,9 +3575,8 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
printf (PROGRAM_NAME " " PROGRAM_VERSION "\n"); printf (PROGRAM_NAME " " PROGRAM_VERSION "\n");
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);
case 'o': *origin = optarg; break; case 'o': *origin = optarg; break;
case 'O': *openrpc = optarg ? optarg : ""; break; case 'O': ctx->openrpc = true; break;
case 'n': ctx->null_as_id = true; break; case 'n': ctx->null_as_id = true; break;
case 'c': ctx->compact = true; break; case 'c': ctx->compact = true; break;
case 't': ctx->trust_all = true; break; case 't': ctx->trust_all = true; break;
@ -3629,38 +3618,6 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
opt_handler_free (&oh); opt_handler_free (&oh);
} }
static void
init_backend (struct app_context *ctx, const char *origin, const char *endpoint)
{
struct http_parser_url url;
if (http_parser_parse_url (endpoint, strlen (endpoint), false, &url))
exit_fatal ("invalid endpoint address");
if (!(url.field_set & (1 << UF_SCHEMA)))
exit_fatal ("invalid endpoint address, must contain the schema");
char *url_schema = xstrndup (endpoint +
url.field_data[UF_SCHEMA].off,
url.field_data[UF_SCHEMA].len);
// TODO: try to avoid the need to pass the application context to backends
if (!strcasecmp_ascii (url_schema, "http")
|| !strcasecmp_ascii (url_schema, "https"))
ctx->backend = backend_curl_new (ctx, endpoint);
else if (!strcasecmp_ascii (url_schema, "ws")
|| !strcasecmp_ascii (url_schema, "wss"))
ctx->backend = backend_ws_new (ctx, endpoint, &url);
else
exit_fatal ("unsupported protocol");
free (url_schema);
if (origin)
{
char *header = xstrdup_printf ("Origin: %s", origin);
g_ctx.backend->vtable->add_header (g_ctx.backend, header);
free (header);
}
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -3668,8 +3625,9 @@ main (int argc, char *argv[])
register_config_modules (&g_ctx); register_config_modules (&g_ctx);
config_load (&g_ctx.config, config_item_object ()); config_load (&g_ctx.config, config_item_object ());
const char *origin = NULL, *endpoint = NULL, *openrpc = NULL; char *origin = NULL;
parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint, &openrpc); char *endpoint = NULL;
parse_program_arguments (&g_ctx, argc, argv, &origin, &endpoint);
g_ctx.input = input_new (); g_ctx.input = input_new ();
g_ctx.input->user_data = &g_ctx; g_ctx.input->user_data = &g_ctx;
@ -3680,7 +3638,34 @@ main (int argc, char *argv[])
g_ctx.methods = str_map_make (NULL); g_ctx.methods = str_map_make (NULL);
init_colors (&g_ctx); init_colors (&g_ctx);
load_configuration (&g_ctx); load_configuration (&g_ctx);
init_backend (&g_ctx, origin, endpoint);
struct http_parser_url url;
if (http_parser_parse_url (endpoint, strlen (endpoint), false, &url))
exit_fatal ("invalid endpoint address");
if (!(url.field_set & (1 << UF_SCHEMA)))
exit_fatal ("invalid endpoint address, must contain the schema");
char *url_schema = xstrndup (endpoint +
url.field_data[UF_SCHEMA].off,
url.field_data[UF_SCHEMA].len);
// TODO: try to avoid the need to pass application context to backends
if (!strcasecmp_ascii (url_schema, "http")
|| !strcasecmp_ascii (url_schema, "https"))
g_ctx.backend = backend_curl_new (&g_ctx, endpoint);
else if (!strcasecmp_ascii (url_schema, "ws")
|| !strcasecmp_ascii (url_schema, "wss"))
g_ctx.backend = backend_ws_new (&g_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);
}
free (origin);
// We only need to convert to and from the terminal encoding // We only need to convert to and from the terminal encoding
setlocale (LC_CTYPE, ""); setlocale (LC_CTYPE, "");
@ -3730,7 +3715,7 @@ main (int argc, char *argv[])
g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME); g_ctx.input->vtable->start (g_ctx.input, PROGRAM_NAME);
ev_set_userdata (EV_DEFAULT_ &g_ctx); ev_set_userdata (EV_DEFAULT_ &g_ctx);
init_openrpc (&g_ctx, openrpc); init_openrpc (&g_ctx);
ev_run (EV_DEFAULT_ 0); ev_run (EV_DEFAULT_ 0);
// User has terminated the program, let's save the history and clean up // User has terminated the program, let's save the history and clean up

@ -1 +1 @@
Subproject commit 69101eb1554ad2fca6de30cdbaccac076210d7e3 Subproject commit e029aae1d3d1884ca868c3694bdec0456b3e8267