* Try to parse a complete message from the plugin's buffer. * * Returns NULL if there was no error. * If it can parse a JSON message, sets *@complete, and returns any error * from the callback. * * If @destroyed was set, it means the plugin called plugin stop on itself. */
| 598 | * If @destroyed was set, it means the plugin called plugin stop on itself. |
| 599 | */ |
| 600 | static const char *plugin_read_json_one(struct plugin *plugin, |
| 601 | bool *complete, |
| 602 | bool *destroyed) |
| 603 | { |
| 604 | const jsmntok_t *jrtok, *idtok; |
| 605 | struct plugin_destroyed *pd; |
| 606 | const char *err; |
| 607 | |
| 608 | *destroyed = false; |
| 609 | /* Note that in the case of 'plugin stop' this can free request (since |
| 610 | * plugin is parent), so detect that case */ |
| 611 | |
| 612 | if (!json_parse_input(&plugin->parser, &plugin->toks, |
| 613 | plugin->buffer, plugin->used, |
| 614 | complete)) { |
| 615 | return tal_fmt(plugin, |
| 616 | "Failed to parse JSON response '%.*s'", |
| 617 | (int)plugin->used, plugin->buffer); |
| 618 | } |
| 619 | |
| 620 | if (!*complete) { |
| 621 | /* We need more. */ |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | /* Empty buffer? (eg. just whitespace). */ |
| 626 | if (tal_count(plugin->toks) == 1) { |
| 627 | plugin->used = 0; |
| 628 | jsmn_init(&plugin->parser); |
| 629 | toks_reset(plugin->toks); |
| 630 | /* We need more. */ |
| 631 | *complete = false; |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | if (plugin->toks->type != JSMN_OBJECT) |
| 636 | return tal_fmt( |
| 637 | plugin, |
| 638 | "JSON-RPC message is not a valid JSON object type"); |
| 639 | |
| 640 | jrtok = json_get_member(plugin->buffer, plugin->toks, "jsonrpc"); |
| 641 | idtok = json_get_member(plugin->buffer, plugin->toks, "id"); |
| 642 | |
| 643 | if (!jrtok) { |
| 644 | return tal_fmt( |
| 645 | plugin, |
| 646 | "JSON-RPC message does not contain \"jsonrpc\" field"); |
| 647 | } |
| 648 | |
| 649 | pd = plugin_detect_destruction(plugin); |
| 650 | if (!idtok) { |
| 651 | /* A Notification is a Request object without an "id" |
| 652 | * member. A Request object that is a Notification |
| 653 | * signifies the Client's lack of interest in the |
| 654 | * corresponding Response object, and as such no |
| 655 | * Response object needs to be returned to the |
| 656 | * client. The Server MUST NOT reply to a |
| 657 | * Notification, including those that are within a |
no test coverage detected