| 56 | } |
| 57 | |
| 58 | const char *jsonrpc_io_parse(const tal_t *ctx, |
| 59 | struct jsonrpc_io *json_in, |
| 60 | const jsmntok_t **toks, |
| 61 | const char **buf) |
| 62 | { |
| 63 | bool complete; |
| 64 | |
| 65 | /* If we're read any more, add that */ |
| 66 | add_newly_read(json_in); |
| 67 | *toks = NULL; |
| 68 | *buf = NULL; |
| 69 | |
| 70 | /* Our JSON parser is pretty good at incremental parsing, but |
| 71 | * `getrawblock` gives a giant 2MB token, which forces it to re-parse |
| 72 | * every time until we have all of it. However, we can't complete a |
| 73 | * JSON object without a '}', so we do a cheaper check here. |
| 74 | */ |
| 75 | if (!memchr(membuf_elems(&json_in->membuf), '}', |
| 76 | membuf_num_elems(&json_in->membuf))) |
| 77 | return NULL; |
| 78 | |
| 79 | if (!json_parse_input(&json_in->parser, &json_in->toks, |
| 80 | membuf_elems(&json_in->membuf), |
| 81 | membuf_num_elems(&json_in->membuf), |
| 82 | &complete)) { |
| 83 | return tal_fmt(ctx, "Failed to parse RPC JSON response '%.*s'", |
| 84 | (int)membuf_num_elems(&json_in->membuf), |
| 85 | membuf_elems(&json_in->membuf)); |
| 86 | } |
| 87 | |
| 88 | if (!complete) |
| 89 | return NULL; |
| 90 | |
| 91 | /* Must have jsonrpc to be valid! */ |
| 92 | if (!json_get_member(membuf_elems(&json_in->membuf), |
| 93 | json_in->toks, |
| 94 | "jsonrpc")) { |
| 95 | return tal_fmt(ctx, |
| 96 | "JSON-RPC message does not contain \"jsonrpc\" field: '%.*s'", |
| 97 | (int)membuf_num_elems(&json_in->membuf), |
| 98 | membuf_elems(&json_in->membuf)); |
| 99 | } |
| 100 | |
| 101 | *toks = json_in->toks; |
| 102 | *buf = membuf_elems(&json_in->membuf); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | void jsonrpc_io_parse_done(struct jsonrpc_io *json_in) |
| 107 | { |