We return struct command_result so command_fail return value has a natural * sink; we don't actually use the result. */
| 861 | /* We return struct command_result so command_fail return value has a natural |
| 862 | * sink; we don't actually use the result. */ |
| 863 | static struct command_result * |
| 864 | parse_request(struct json_connection *jcon, const jsmntok_t tok[]) |
| 865 | { |
| 866 | const jsmntok_t *method, *id, *params, *jsonrpc; |
| 867 | struct command *c; |
| 868 | struct rpc_command_hook_payload *rpc_hook; |
| 869 | bool completed; |
| 870 | |
| 871 | if (tok[0].type != JSMN_OBJECT) { |
| 872 | json_command_malformed(jcon, "null", |
| 873 | "Expected {} for json command"); |
| 874 | return NULL; |
| 875 | } |
| 876 | |
| 877 | method = json_get_member(jcon->buffer, tok, "method"); |
| 878 | params = json_get_member(jcon->buffer, tok, "params"); |
| 879 | id = json_get_member(jcon->buffer, tok, "id"); |
| 880 | |
| 881 | if (!id) { |
| 882 | json_command_malformed(jcon, "null", "No id"); |
| 883 | return NULL; |
| 884 | } |
| 885 | |
| 886 | if (id->type != JSMN_STRING && id->type != JSMN_PRIMITIVE) { |
| 887 | json_command_malformed(jcon, "null", |
| 888 | "Expected string/primitive for id"); |
| 889 | return NULL; |
| 890 | } |
| 891 | |
| 892 | // Adding a deprecated phase to make sure that all the Core Lightning wrapper |
| 893 | // can migrate all the frameworks |
| 894 | jsonrpc = json_get_member(jcon->buffer, tok, "jsonrpc"); |
| 895 | |
| 896 | if (!jsonrpc || jsonrpc->type != JSMN_STRING || !json_tok_streq(jcon->buffer, jsonrpc, "2.0")) { |
| 897 | json_command_malformed(jcon, "null", "jsonrpc: \"2.0\" must be specified in the request"); |
| 898 | return NULL; |
| 899 | } |
| 900 | |
| 901 | /* Allocate the command off of the `jsonrpc` object and not |
| 902 | * the connection since the command may outlive `conn`. */ |
| 903 | c = tal(jcon->ld->jsonrpc, struct command); |
| 904 | c->jcon = jcon; |
| 905 | c->send_notifications = jcon->notifications_enabled; |
| 906 | c->ld = jcon->ld; |
| 907 | c->pending = false; |
| 908 | c->json_stream = NULL; |
| 909 | c->id_is_string = (id->type == JSMN_STRING); |
| 910 | c->id = json_strdup(c, jcon->buffer, id); |
| 911 | c->mode = CMD_NORMAL; |
| 912 | list_add_tail(&jcon->commands, &c->list); |
| 913 | tal_add_destructor(c, destroy_command); |
| 914 | |
| 915 | if (!method || !params) { |
| 916 | return command_fail(c, JSONRPC2_INVALID_REQUEST, |
| 917 | method ? "No params" : "No method"); |
| 918 | } |
| 919 | |
| 920 | if (method->type != JSMN_STRING) { |
no test coverage detected