Compare commits
100 Commits
2a071d319b
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
710f5f197f
|
|||
|
ba68585d14
|
|||
|
984e5b4e7f
|
|||
|
d57a8bd3c7
|
|||
|
2962a644da
|
|||
|
6f5ef30293
|
|||
|
b7b1198be7
|
|||
|
633f7007d1
|
|||
|
f4d178b3f6
|
|||
|
ee5317f865
|
|||
|
20558ecd2b
|
|||
|
f6225ac6cc
|
|||
|
16ec8261dc
|
|||
|
e49ff84b74
|
|||
|
b7c9bfd9f5
|
|||
|
f6165164ee
|
|||
|
3a445c2db2
|
|||
|
45d023147a
|
|||
|
90b5364b29
|
|||
|
1840ac795e
|
|||
|
df38bcf775
|
|||
|
1c52f9e37e
|
|||
|
6e152ae37c
|
|||
|
8d66435568
|
|||
|
d883f4cc71
|
|||
|
62945cceb3
|
|||
|
580f0a0c59
|
|||
|
253e35e1e4
|
|||
|
cf56921c4e
|
|||
|
a3ec0942f8
|
|||
|
efd500ca3c
|
|||
|
13892fcd0e
|
|||
|
1d638c9170
|
|||
|
83363e6383
|
|||
|
272145ace2
|
|||
|
4c54bc42b9
|
|||
|
7d922352ea
|
|||
|
7f6db9d39f
|
|||
|
7cefdd496f
|
|||
|
e9530f450e
|
|||
|
dda22c2cd5
|
|||
|
7aff9c3475
|
|||
|
a14edb72e9
|
|||
|
267a9a561b
|
|||
|
441c89f654
|
|||
|
14ded260a0
|
|||
|
8b334e9c91
|
|||
|
fd17b4e504
|
|||
|
d182bcef3b
|
|||
|
329fc9b88f
|
|||
|
711d73f481
|
|||
|
df340c13ed
|
|||
|
8e986a6040
|
|||
|
131debe985
|
|||
|
b312c022ae
|
|||
|
4078c8845c
|
|||
|
333efdc70f
|
|||
|
5e88608286
|
|||
|
ca90e9df83
|
|||
|
419147beec
|
|||
|
b85d1d74a4
|
|||
|
d35e733c6e
|
|||
|
e6f9e53229
|
|||
|
2986f6cda0
|
|||
|
c4ebf2ccd5
|
|||
|
a785dc9670
|
|||
|
2b7d455471
|
|||
|
0ec0685714
|
|||
|
733451cf2a
|
|||
|
369f94f5ab
|
|||
| 7b94a03e8c | |||
| 8b66a3f074 | |||
| f273151447 | |||
| a95867dbee | |||
| 5298d802bb | |||
| af3cb3aaba | |||
| 0f62ef26f5 | |||
| 3c7b57bba9 | |||
| 98bbea72d2 | |||
| df5b7ad71a | |||
| 6785d3a9ed | |||
| 1944f9f17d | |||
| 4dbdc849d9 | |||
| db6dff4216 | |||
| 8aa232d32e | |||
| 6e9109df4c | |||
| 987eae5661 | |||
| 9b7dd630e3 | |||
| c87d684154 | |||
| 23eb4cca38 | |||
| 4337038819 | |||
| 012a57b357 | |||
| 3c0e48a429 | |||
| 5885d1aa69 | |||
| 931fc441f6 | |||
| 9e0c9dd6d8 | |||
| 2733ead30f | |||
| 0b0d64124b | |||
| a54230bddb | |||
| 8a3241d5c4 |
@@ -13,8 +13,8 @@ if ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
endif ("${CMAKE_C_COMPILER_ID}" MATCHES "GNU" OR CMAKE_COMPILER_IS_GNUCC)
|
||||
|
||||
# Version
|
||||
set (project_VERSION_MAJOR "0")
|
||||
set (project_VERSION_MINOR "1")
|
||||
set (project_VERSION_MAJOR "1")
|
||||
set (project_VERSION_MINOR "0")
|
||||
set (project_VERSION_PATCH "0")
|
||||
|
||||
set (project_VERSION "${project_VERSION_MAJOR}")
|
||||
@@ -70,6 +70,16 @@ include_directories (${PROJECT_BINARY_DIR})
|
||||
add_executable (${PROJECT_NAME} ${PROJECT_NAME}.c http-parser/http_parser.c)
|
||||
target_link_libraries (${PROJECT_NAME} ${project_libraries})
|
||||
|
||||
# Development tools
|
||||
find_package (LibMagic)
|
||||
if (LIBMAGIC_FOUND)
|
||||
include_directories (${LIBMAGIC_INCLUDE_DIRS})
|
||||
add_executable (json-rpc-test-server
|
||||
json-rpc-test-server.c http-parser/http_parser.c)
|
||||
target_link_libraries (json-rpc-test-server
|
||||
${project_libraries} ${LIBMAGIC_LIBRARIES})
|
||||
endif ()
|
||||
|
||||
# The files to be installed
|
||||
include (GNUInstallDirs)
|
||||
install (TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
@@ -77,18 +87,20 @@ install (PROGRAMS json-format.pl DESTINATION ${CMAKE_INSTALL_BINDIR})
|
||||
install (FILES LICENSE DESTINATION ${CMAKE_INSTALL_DOCDIR})
|
||||
|
||||
# Generate documentation from program help
|
||||
find_program (HELP2MAN_EXECUTABLE help2man)
|
||||
if (NOT HELP2MAN_EXECUTABLE)
|
||||
message (FATAL_ERROR "help2man not found")
|
||||
endif (NOT HELP2MAN_EXECUTABLE)
|
||||
find_program (ASCIIDOCTOR_EXECUTABLE asciidoctor)
|
||||
if (NOT ASCIIDOCTOR_EXECUTABLE)
|
||||
message (FATAL_ERROR "asciidoctor not found")
|
||||
endif (NOT ASCIIDOCTOR_EXECUTABLE)
|
||||
|
||||
foreach (page ${PROJECT_NAME})
|
||||
set (page_output "${PROJECT_BINARY_DIR}/${page}.1")
|
||||
list (APPEND project_MAN_PAGES "${page_output}")
|
||||
add_custom_command (OUTPUT ${page_output}
|
||||
COMMAND ${HELP2MAN_EXECUTABLE} -N
|
||||
"${PROJECT_BINARY_DIR}/${page}" -o ${page_output}
|
||||
DEPENDS ${page}
|
||||
COMMAND ${ASCIIDOCTOR_EXECUTABLE} -b manpage
|
||||
-a release-version=${project_VERSION}
|
||||
"${PROJECT_SOURCE_DIR}/${page}.adoc"
|
||||
-o "${page_output}"
|
||||
DEPENDS ${page}.adoc
|
||||
COMMENT "Generating man page for ${page}" VERBATIM)
|
||||
endforeach (page)
|
||||
|
||||
@@ -101,9 +113,10 @@ foreach (page ${project_MAN_PAGES})
|
||||
endforeach (page)
|
||||
|
||||
# CPack
|
||||
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY "Shell for running JSON-RPC 2.0 queries")
|
||||
set (CPACK_PACKAGE_VENDOR "Premysl Janouch")
|
||||
set (CPACK_PACKAGE_CONTACT "Přemysl Janouch <p@janouch.name>")
|
||||
set (CPACK_PACKAGE_DESCRIPTION_SUMMARY
|
||||
"A shell for running JSON-RPC 2.0 queries")
|
||||
set (CPACK_PACKAGE_VENDOR "Premysl Eric Janouch")
|
||||
set (CPACK_PACKAGE_CONTACT "Přemysl Eric Janouch <p@janouch.name>")
|
||||
set (CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
|
||||
set (CPACK_PACKAGE_VERSION_MAJOR ${project_VERSION_MAJOR})
|
||||
set (CPACK_PACKAGE_VERSION_MINOR ${project_VERSION_MINOR})
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014 - 2016, Přemysl Janouch <p@janouch.name>
|
||||
Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted.
|
||||
|
||||
38
README.adoc
38
README.adoc
@@ -17,36 +17,29 @@ you get the following niceties:
|
||||
- ability to pipe output through a shell command, so that you can view the
|
||||
results in your favourite editor or redirect them to a file
|
||||
- ability to edit the input line in your favourite editor as well with Alt+E
|
||||
- WebSockets (RFC 6455) can also be used as a transport rather than HTTP
|
||||
|
||||
Supported transports
|
||||
--------------------
|
||||
- HTTP
|
||||
- HTTPS
|
||||
- WebSocket
|
||||
- WebSocket over TLS
|
||||
|
||||
WebSockets
|
||||
~~~~~~~~~~
|
||||
The JSON-RPC 2.0 spec doesn't say almost anything about underlying transports.
|
||||
The way it's implemented here is that every request is sent as a single text
|
||||
message. If it has an "id" field, i.e. it's not just a notification, the
|
||||
client waits for a message from the server in response.
|
||||
|
||||
There's no support so far for any protocol extensions, nor for specifying
|
||||
the higher-level protocol (the "Sec-Ws-Protocol" HTTP field).
|
||||
Documentation
|
||||
-------------
|
||||
See the link:json-rpc-shell.adoc[man page] for information about usage.
|
||||
The rest of this README will concern itself with externalities.
|
||||
|
||||
Packages
|
||||
--------
|
||||
Regular releases are sporadic. git master should be stable enough. You can get
|
||||
a package with the latest development version from Archlinux's AUR.
|
||||
|
||||
Building and Usage
|
||||
------------------
|
||||
Build dependencies: CMake, pkg-config, help2man,
|
||||
Building
|
||||
--------
|
||||
Build dependencies: CMake, pkg-config, asciidoctor,
|
||||
liberty (included), http-parser (included) +
|
||||
Runtime dependencies: libev, Jansson, cURL, openssl,
|
||||
readline or libedit >= 2013-07-12,
|
||||
|
||||
Avoid libedit if you can, in general it works but at the moment history is
|
||||
acting up and I have no clue about fixing it. Multiline editing is also
|
||||
misbehaving there.
|
||||
|
||||
$ git clone --recursive https://git.janouch.name/p/json-rpc-shell.git
|
||||
$ mkdir json-rpc-shell/build
|
||||
$ cd json-rpc-shell/build
|
||||
@@ -65,7 +58,12 @@ Or you can try telling CMake to make a package for you. For Debian it is:
|
||||
Note that for versions of CMake before 2.8.9, you need to prefix `cpack` with
|
||||
`fakeroot` or file ownership will end up wrong.
|
||||
|
||||
Run the program with `--help` to obtain usage information.
|
||||
Test server
|
||||
-----------
|
||||
If you install development packages for libmagic, an included test server will
|
||||
be built but not installed which provides a trivial JSON-RPC 2.0 service with
|
||||
FastCGI, SCGI, and WebSocket interfaces. It responds to `ping` and `date`
|
||||
methods and it can serve static files.
|
||||
|
||||
Contributing and Support
|
||||
------------------------
|
||||
|
||||
10
cmake/FindLibMagic.cmake
Normal file
10
cmake/FindLibMagic.cmake
Normal file
@@ -0,0 +1,10 @@
|
||||
# Public Domain
|
||||
|
||||
find_library (LIBMAGIC_LIBRARIES magic)
|
||||
find_path (LIBMAGIC_INCLUDE_DIRS magic.h)
|
||||
|
||||
include (FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args (LibMagic DEFAULT_MSG
|
||||
LIBMAGIC_LIBRARIES LIBMAGIC_INCLUDE_DIRS)
|
||||
|
||||
mark_as_advanced (LIBMAGIC_LIBRARIES LIBMAGIC_INCLUDE_DIRS)
|
||||
180
json-rpc-shell.adoc
Normal file
180
json-rpc-shell.adoc
Normal file
@@ -0,0 +1,180 @@
|
||||
json-rpc-shell(1)
|
||||
=================
|
||||
:doctype: manpage
|
||||
:manmanual: json-rpc-shell Manual
|
||||
:mansource: json-rpc-shell {release-version}
|
||||
|
||||
Name
|
||||
----
|
||||
json-rpc-shell - a simple JSON-RPC 2.0 shell
|
||||
|
||||
Synopsis
|
||||
--------
|
||||
*json-rpc-shell* [_OPTION_]... _ENDPOINT_
|
||||
|
||||
Description
|
||||
-----------
|
||||
The _ENDPOINT_ must be either an HTTP or a WebSocket URL, with or without TLS
|
||||
(i.e. one of the _+++http+++://_, _+++https+++://_, _ws://_, _wss://_ schemas).
|
||||
|
||||
*json-rpc-shell* will use it to send any JSON-RPC 2.0 requests you enter on its
|
||||
command line. The server's response will be parsed and validated, stripping it
|
||||
of the protocol's noisy envelope. At your option, it can then also be
|
||||
pretty-printed, rendered with adjustable syntax highlighting, or even piped
|
||||
through another program such as the *less*(1) pager or the *jq*(1) JSON
|
||||
processor.
|
||||
|
||||
Usage
|
||||
~~~~~
|
||||
Three things may appear on the internal command line, in a sequence. The first
|
||||
one is always the name of the JSON-RPC method to call, as a bare word, separated
|
||||
from the rest by white space. Following that, you may enter three kinds of JSON
|
||||
values. If it is an object or an array, it constitutes the method parameters.
|
||||
If it is a string or a number, it is taken as the "id" to use for the request,
|
||||
which would be chosen for you automatically if left unspecified. Finally,
|
||||
a null value indicates that the request should be sent as a notification,
|
||||
lacking the ID completely. Booleans cannot be used for anything.
|
||||
|
||||
The response to the method call may be piped through external commands, the same
|
||||
way you would do it in a Unix shell.
|
||||
|
||||
Exit the program by pressing C-c or C-d. No special keywords are reserved for
|
||||
this action as they might conflict with method names.
|
||||
|
||||
Options
|
||||
-------
|
||||
Controlling output
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
*-c*, *--compact-output*::
|
||||
Do not pretty-print responses. Normally, spaces and newlines are added
|
||||
where appropriate to improve readability.
|
||||
|
||||
*--color*=_WHEN_::
|
||||
By default, when the output of the program is a terminal, JSON responses
|
||||
are syntax-highlighted. This corresponds to the _auto_ setting. You may
|
||||
also set this to _always_ or _never_. In either case, color is never
|
||||
applied when piping to another program.
|
||||
|
||||
*-v*, *--verbose*::
|
||||
Print raw requests and responses, including the JSON-RPC 2.0 envelope.
|
||||
|
||||
*-d*, *--debug*::
|
||||
Print even more information to help debug various issues.
|
||||
|
||||
Protocol
|
||||
~~~~~~~~
|
||||
*-n*, *--null-as-id*::
|
||||
Normally, entering a null JSON value on the command line causes
|
||||
a notification to be sent. With this option, it is sent as the "id"
|
||||
field of a normal request, which is discouraged by the specification.
|
||||
|
||||
*-t*, *--trust-all*::
|
||||
Trust all SSL/TLS certificates. Useful in case that the certificate is
|
||||
self-signed, or when the CA isn't in your CA store. Beware that this option
|
||||
is about as good as using plain unencrypted HTTP.
|
||||
|
||||
*-o* _ORIGIN_, *--origin*=_ORIGIN_::
|
||||
Set the HTTP Origin header to _ORIGIN_. Some servers may need this.
|
||||
|
||||
Program information
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
*-h*, *--help*::
|
||||
Display a help message and exit.
|
||||
|
||||
*-V*, *--version*::
|
||||
Output version information and exit.
|
||||
|
||||
*--write-default-cfg*[**=**__PATH__]::
|
||||
Write a default configuration file, show its path and exit.
|
||||
|
||||
Files
|
||||
-----
|
||||
_~/.config/json-rpc-shell/json-rpc-shell.conf_::
|
||||
The configuration file, in which you can configure color output and
|
||||
CA certificate paths. Use the *--write-default-cfg* option to create
|
||||
a new one for editing.
|
||||
|
||||
_~/.local/share/json-rpc-shell/history_::
|
||||
All your past method invocations are stored here upon exit and loaded back
|
||||
on start-up.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Editing
|
||||
~~~~~~~
|
||||
While single-line editing on the command line may be satisfactory for simple
|
||||
requests, it is often convenient or even necessary to run a full text editor
|
||||
in order to construct complex objects or arrays, and may even be used to import
|
||||
data from elsewhere. You can launch an editor for the current request using
|
||||
the M-e key combination. Both *readline*(3) and *editline*(7) also support
|
||||
multiline editing natively, though you need to press C-v C-j in order to insert
|
||||
newlines.
|
||||
|
||||
WebSockets
|
||||
~~~~~~~~~~
|
||||
The JSON-RPC 2.0 specification doesn't say almost anything about underlying
|
||||
transports. As far as the author is aware, he is the only person combining it
|
||||
with WebSockets. The way it's implemented here is that every request is sent as
|
||||
a single text message. If it has an "id" field, i.e., it's not just
|
||||
a notification, the client waits for a message from the server in response.
|
||||
Should any message arrive unexpectedly, you will receive a warning.
|
||||
|
||||
There is no support so far for any protocol extensions, nor for specifying
|
||||
the higher-level protocol (the "Sec-Ws-Protocol" HTTP field).
|
||||
|
||||
Bugs
|
||||
----
|
||||
The editline (libedit) frontend is more of a proof of concept that mostly seems
|
||||
to work but exhibits bugs that are not our fault.
|
||||
|
||||
Examples
|
||||
--------
|
||||
Running some queries against json-rpc-test-server, included in the source
|
||||
distribution of this program (public services are hard to find):
|
||||
|
||||
Methods without parameters
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
$ json-rpc-shell ws://localhost:1234
|
||||
json-rpc> ping
|
||||
"pong"
|
||||
json-rpc> date
|
||||
{
|
||||
"year": 2020,
|
||||
"month": 9,
|
||||
"day": 5,
|
||||
"hours": 2,
|
||||
"minutes": 23,
|
||||
"seconds": 51
|
||||
}
|
||||
|
||||
Notification with a parameter
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Notifications never produce a response, not even when the method is not known
|
||||
to the server:
|
||||
|
||||
$ json-rpc-shell ws://localhost:1234
|
||||
json-rpc> notify {"events": ["conquest", "war", "famine", "death"]} null
|
||||
[Notification]
|
||||
|
||||
Piping in and out
|
||||
~~~~~~~~~~~~~~~~~
|
||||
GNU Readline always repeats the prompt, which makes this a bit less useful
|
||||
for invoking from other programs:
|
||||
|
||||
$ echo 'ping | jq ascii_upcase' | json-rpc-shell ws://localhost:1234
|
||||
json-rpc> ping | jq ascii_upcase
|
||||
"PONG"
|
||||
|
||||
Reporting bugs
|
||||
--------------
|
||||
Use https://git.janouch.name/p/json-rpc-shell to report bugs, request features,
|
||||
or submit pull requests.
|
||||
|
||||
See also
|
||||
--------
|
||||
*jq*(1), *readline*(3) or *editline*(7)
|
||||
|
||||
Specifications
|
||||
~~~~~~~~~~~~~~
|
||||
https://www.jsonrpc.org/specification +
|
||||
https://www.json.org
|
||||
131
json-rpc-shell.c
131
json-rpc-shell.c
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* json-rpc-shell.c: simple JSON-RPC 2.0 shell
|
||||
*
|
||||
* Copyright (c) 2014 - 2016, Přemysl Janouch <p@janouch.name>
|
||||
* Copyright (c) 2014 - 2020, Přemysl Eric Janouch <p@janouch.name>
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
@@ -531,7 +531,8 @@ input_el_redisplay (struct input_el *self)
|
||||
el_push (self->editline, x);
|
||||
|
||||
// We have to do this or it gets stuck and nothing is done
|
||||
(void) el_gets (self->editline, NULL);
|
||||
int count = 0;
|
||||
(void) el_wgets (self->editline, &count);
|
||||
}
|
||||
|
||||
static char *
|
||||
@@ -618,7 +619,6 @@ input_el_on_run_editor (EditLine *editline, int key)
|
||||
static void
|
||||
input_el_install_prompt (struct input_el *self)
|
||||
{
|
||||
// XXX: the ignore doesn't quite work, see https://gnats.netbsd.org/47539
|
||||
el_set (self->editline, EL_PROMPT_ESC,
|
||||
input_el_make_prompt, INPUT_START_IGNORE);
|
||||
}
|
||||
@@ -633,7 +633,7 @@ input_el_start (struct input *input, const char *program_name)
|
||||
el_set (self->editline, EL_CLIENTDATA, self);
|
||||
input_el_install_prompt (self);
|
||||
el_set (self->editline, EL_SIGNAL, false);
|
||||
el_set (self->editline, EL_UNBUFFERED, true);
|
||||
el_set (self->editline, EL_UNBUFFERED, isatty (fileno (stdin)));
|
||||
el_set (self->editline, EL_EDITOR, "emacs");
|
||||
el_wset (self->editline, EL_HIST, history_w, self->history);
|
||||
|
||||
@@ -833,6 +833,17 @@ input_el_on_tty_readable (struct input *input)
|
||||
int count = 0;
|
||||
const wchar_t *buf = el_wgets (self->editline, &count);
|
||||
|
||||
// Editline works in a funny NO_TTY mode when the input is not a tty,
|
||||
// we cannot use EL_UNBUFFERED and expect sane results then
|
||||
int unbuffered = 0;
|
||||
if (!el_get (self->editline, EL_UNBUFFERED, &unbuffered) && !unbuffered)
|
||||
{
|
||||
char *entered_line = buf ? input_el_wcstombs (buf) : NULL;
|
||||
self->super.on_input (entered_line, self->super.user_data);
|
||||
free (entered_line);
|
||||
return;
|
||||
}
|
||||
|
||||
// Process data from our newline handler (async-friendly handling)
|
||||
if (self->entered_line)
|
||||
{
|
||||
@@ -921,11 +932,11 @@ static struct app_context
|
||||
|
||||
struct config config; ///< Program configuration
|
||||
enum color_mode color_mode; ///< Colour output mode
|
||||
bool pretty_print; ///< Whether to pretty print
|
||||
bool compact; ///< Whether to not pretty print
|
||||
bool verbose; ///< Print requests
|
||||
bool trust_all; ///< Don't verify peer certificates
|
||||
|
||||
bool auto_id; ///< Use automatically generated ID's
|
||||
bool null_as_id; ///< JSON null is used as an ID
|
||||
int64_t next_id; ///< Next autogenerated ID
|
||||
|
||||
iconv_t term_to_utf8; ///< Terminal encoding to UTF-8
|
||||
@@ -1672,7 +1683,7 @@ backend_ws_establish_connection (struct ws_context *self,
|
||||
else
|
||||
real_host = buf;
|
||||
|
||||
if (self->ctx->verbose)
|
||||
if (g_debug_mode)
|
||||
{
|
||||
char *address = format_host_port_pair (real_host, port);
|
||||
print_status ("connecting to %s...", address);
|
||||
@@ -2738,8 +2749,19 @@ display_via_pipeline (struct app_context *ctx,
|
||||
}
|
||||
|
||||
static bool
|
||||
parse_response (struct app_context *ctx, struct str *buf, const char *pipeline)
|
||||
process_response (struct app_context *ctx, const json_t *id, struct str *buf,
|
||||
const char *pipeline)
|
||||
{
|
||||
if (!id)
|
||||
{
|
||||
printf ("[Notification]\n");
|
||||
if (!buf->len)
|
||||
return true;
|
||||
|
||||
print_warning ("we have been sent data back for a notification");
|
||||
return false;
|
||||
}
|
||||
|
||||
json_error_t e;
|
||||
json_t *response;
|
||||
if (!(response = json_loadb (buf->str, buf->len, JSON_DECODE_ANY, &e)))
|
||||
@@ -2758,9 +2780,15 @@ parse_response (struct app_context *ctx, struct str *buf, const char *pipeline)
|
||||
else if (!json_is_string (v) || strcmp (json_string_value (v), "2.0"))
|
||||
print_warning ("invalid `%s' field in response", "jsonrpc");
|
||||
|
||||
json_t *result = json_object_get (response, "result");
|
||||
json_t *error = json_object_get (response, "error");
|
||||
json_t *data = NULL;
|
||||
json_t *returned_id = json_object_get (response, "id");
|
||||
json_t *result = json_object_get (response, "result");
|
||||
json_t *error = json_object_get (response, "error");
|
||||
json_t *data = NULL;
|
||||
|
||||
if (!returned_id)
|
||||
print_warning ("`%s' field not present in response", "id");
|
||||
if (!json_equal (id, returned_id))
|
||||
print_warning ("mismatching `%s' field in response", "id");
|
||||
|
||||
if (!result && !error)
|
||||
PARSE_FAIL ("neither `result' nor `error' present in response");
|
||||
@@ -2817,7 +2845,7 @@ parse_response (struct app_context *ctx, struct str *buf, const char *pipeline)
|
||||
if (result)
|
||||
{
|
||||
int flags = JSON_ENCODE_ANY;
|
||||
if (ctx->pretty_print)
|
||||
if (!ctx->compact)
|
||||
flags |= JSON_INDENT (2);
|
||||
|
||||
char *utf8 = json_dumps (result, flags);
|
||||
@@ -2891,26 +2919,28 @@ make_json_rpc_call (struct app_context *ctx,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
if (id)
|
||||
success = parse_response (ctx, &buf, pipeline);
|
||||
else
|
||||
if (ctx->verbose)
|
||||
{
|
||||
printf ("[Notification]\n");
|
||||
if (buf.len)
|
||||
print_warning ("we have been sent data back for a notification");
|
||||
char *buf_term =
|
||||
iconv_xstrdup (ctx->term_from_utf8, buf.str, buf.len, NULL);
|
||||
if (!buf_term)
|
||||
print_error ("%s: %s", "verbose", "character conversion failed");
|
||||
else
|
||||
success = true;
|
||||
{
|
||||
print_attributed (ctx, stdout, ATTR_INCOMING, "%s", buf_term);
|
||||
fputs ("\n", stdout);
|
||||
}
|
||||
free (buf_term);
|
||||
}
|
||||
|
||||
if (!success)
|
||||
if (!process_response (ctx, id, &buf, pipeline))
|
||||
{
|
||||
char *s = iconv_xstrdup (ctx->term_from_utf8,
|
||||
buf.str, buf.len + 1 /* null byte */, NULL);
|
||||
if (!s)
|
||||
print_error ("character conversion failed for `%s'",
|
||||
"raw response data");
|
||||
else
|
||||
else if (!ctx->verbose /* already printed */)
|
||||
printf ("%s: %s\n", "raw response data", s);
|
||||
free (s);
|
||||
}
|
||||
@@ -3011,9 +3041,16 @@ process_input (char *user_input, void *user_data)
|
||||
*target = json_incref (args[i]);
|
||||
}
|
||||
|
||||
if (!id && ctx->auto_id)
|
||||
if (!id)
|
||||
id = json_integer (ctx->next_id++);
|
||||
|
||||
// Use nulls to send notifications, unless a special switch is used
|
||||
if (!ctx->null_as_id && json_is_null (id))
|
||||
{
|
||||
json_decref (id);
|
||||
id = NULL;
|
||||
}
|
||||
|
||||
make_json_rpc_call (ctx, method, id, params, pipeline);
|
||||
|
||||
fail_parse:
|
||||
@@ -3176,16 +3213,21 @@ process_edited_input (struct app_context *ctx)
|
||||
print_error ("%s: %s", "input editing failed", e->message);
|
||||
error_free (e);
|
||||
}
|
||||
else if (!ctx->input->vtable->replace_line (ctx->input, input.str))
|
||||
print_error ("%s: %s", "input editing failed",
|
||||
"could not re-insert modified text");
|
||||
else
|
||||
{
|
||||
// Strip trailing newlines, added automatically by editors
|
||||
while (input.len && strchr ("\r\n", input.str[input.len - 1]))
|
||||
input.str[--input.len] = 0;
|
||||
|
||||
if (!ctx->input->vtable->replace_line (ctx->input, input.str))
|
||||
print_error ("%s: %s", "input editing failed",
|
||||
"could not re-insert modified text");
|
||||
}
|
||||
|
||||
if (unlink (ctx->editor_filename))
|
||||
print_error ("could not unlink `%s': %s",
|
||||
ctx->editor_filename, strerror (errno));
|
||||
|
||||
free (ctx->editor_filename);
|
||||
ctx->editor_filename = NULL;
|
||||
str_free (&input);
|
||||
}
|
||||
|
||||
@@ -3203,7 +3245,7 @@ on_child (EV_P_ ev_child *handle, int revents)
|
||||
kill (-handle->rpid, SIGKILL);
|
||||
return;
|
||||
}
|
||||
// I don't recognize this child (we should also check PID)
|
||||
// I don't recognize this child (we should also check its PID)
|
||||
if (!ctx->editor_filename)
|
||||
return;
|
||||
|
||||
@@ -3216,6 +3258,9 @@ on_child (EV_P_ ev_child *handle, int revents)
|
||||
print_error ("editor returned status %d", WEXITSTATUS (status));
|
||||
else
|
||||
process_edited_input (ctx);
|
||||
|
||||
free (ctx->editor_filename);
|
||||
ctx->editor_filename = NULL;
|
||||
}
|
||||
|
||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
@@ -3250,8 +3295,14 @@ on_tty_readable (EV_P_ ev_io *handle, int revents)
|
||||
{
|
||||
// rl_callback_read_char() is not reentrant, may happen on EOF
|
||||
ev_io_stop (EV_DEFAULT_ &ctx->tty_watcher);
|
||||
|
||||
ctx->input->vtable->on_tty_readable (ctx->input);
|
||||
ev_io_start (EV_DEFAULT_ &ctx->tty_watcher);
|
||||
|
||||
// Don't make ourselves receive a SIGTTIN. Ideally we'd prevent
|
||||
// reentrancy without inciting conflicts with
|
||||
// {suspend,resume}_terminal() but I can't figure anything out.
|
||||
if (!ctx->editor_filename)
|
||||
ev_io_start (EV_DEFAULT_ &ctx->tty_watcher);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3295,14 +3346,14 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
|
||||
static const struct opt opts[] =
|
||||
{
|
||||
{ 'd', "debug", NULL, 0, "run in debug mode" },
|
||||
{ 'h', "help", NULL, 0, "display this help and exit" },
|
||||
{ 'h', "help", NULL, 0, "display this help message and exit" },
|
||||
{ 'V', "version", NULL, 0, "output version information and exit" },
|
||||
{ 'a', "auto-id", NULL, 0, "automatic `id' fields" },
|
||||
{ 'n', "null-as-id", NULL, 0, "JSON null is used as an `id'" },
|
||||
{ 'o', "origin", "O", 0, "set the HTTP Origin header" },
|
||||
{ 'p', "pretty", NULL, 0, "pretty-print the responses" },
|
||||
{ 'c', "compact-output", NULL, 0, "do not pretty-print responses" },
|
||||
{ 't', "trust-all", NULL, 0, "don't care about SSL/TLS certificates" },
|
||||
{ 'v', "verbose", NULL, 0, "print the request before sending" },
|
||||
{ 'c', "color", "WHEN", OPT_LONG_ONLY,
|
||||
{ 'v', "verbose", NULL, 0, "print raw requests and responses" },
|
||||
{ 'C', "color", "WHEN", OPT_LONG_ONLY,
|
||||
"colorize output: never, always, or auto" },
|
||||
{ 'w', "write-default-cfg", "FILENAME",
|
||||
OPT_OPTIONAL_ARG | OPT_LONG_ONLY,
|
||||
@@ -3311,7 +3362,7 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
|
||||
};
|
||||
|
||||
struct opt_handler oh = opt_handler_make (argc, argv, opts,
|
||||
"ENDPOINT", "Simple JSON-RPC shell.");
|
||||
"ENDPOINT", "A simple JSON-RPC 2.0 shell.");
|
||||
|
||||
int c;
|
||||
while ((c = opt_handler_get (&oh)) != -1)
|
||||
@@ -3328,12 +3379,12 @@ parse_program_arguments (struct app_context *ctx, int argc, char **argv,
|
||||
exit (EXIT_SUCCESS);
|
||||
|
||||
case 'o': *origin = optarg; break;
|
||||
case 'a': ctx->auto_id = true; break;
|
||||
case 'p': ctx->pretty_print = true; break;
|
||||
case 'n': ctx->null_as_id = true; break;
|
||||
case 'c': ctx->compact = true; break;
|
||||
case 't': ctx->trust_all = true; break;
|
||||
case 'v': ctx->verbose = true; break;
|
||||
|
||||
case 'c':
|
||||
case 'C':
|
||||
if (!strcasecmp (optarg, "never"))
|
||||
ctx->color_mode = COLOR_NEVER;
|
||||
else if (!strcasecmp (optarg, "always"))
|
||||
@@ -3453,7 +3504,7 @@ main (int argc, char *argv[])
|
||||
{
|
||||
// XXX: to be completely correct, we should use tputs, but we cannot
|
||||
g_ctx.input->vtable->set_prompt (g_ctx.input,
|
||||
xstrdup_printf ("%c%s%cjson-rpc> %c%s%c",
|
||||
xstrdup_printf ("%c%s%cjson-rpc>%c%s%c ",
|
||||
INPUT_START_IGNORE, g_ctx.attrs[ATTR_PROMPT],
|
||||
INPUT_END_IGNORE,
|
||||
INPUT_START_IGNORE, g_ctx.attrs[ATTR_RESET],
|
||||
|
||||
2979
json-rpc-test-server.c
Normal file
2979
json-rpc-test-server.c
Normal file
File diff suppressed because it is too large
Load Diff
2
liberty
2
liberty
Submodule liberty updated: bb30c7d86e...1a76b2032e
Reference in New Issue
Block a user