| 1538 | } |
| 1539 | |
| 1540 | struct jsonrpc_request *jsonrpc_request_start_( |
| 1541 | const tal_t *ctx, const char *method, |
| 1542 | const char *id_prefix, struct logger *log, |
| 1543 | bool add_header, |
| 1544 | void (*notify_cb)(const char *buffer, |
| 1545 | const jsmntok_t *methodtok, |
| 1546 | const jsmntok_t *paramtoks, |
| 1547 | const jsmntok_t *idtok, |
| 1548 | void *), |
| 1549 | void (*response_cb)(const char *buffer, const jsmntok_t *toks, |
| 1550 | const jsmntok_t *idtok, void *), |
| 1551 | void *response_cb_arg) |
| 1552 | { |
| 1553 | struct jsonrpc_request *r = tal(ctx, struct jsonrpc_request); |
| 1554 | static u64 next_request_id = 0; |
| 1555 | |
| 1556 | r->id_is_string = true; |
| 1557 | if (id_prefix) { |
| 1558 | /* Strip "" and otherwise sanity-check */ |
| 1559 | if (strstarts(id_prefix, "\"") |
| 1560 | && strlen(id_prefix) > 1 |
| 1561 | && strends(id_prefix, "\"")) { |
| 1562 | id_prefix = tal_strndup(tmpctx, id_prefix + 1, |
| 1563 | strlen(id_prefix) - 2); |
| 1564 | } |
| 1565 | /* We could try escaping, but TBH they're |
| 1566 | * messing with us at this point! */ |
| 1567 | if (json_escape_needed(id_prefix, strlen(id_prefix))) |
| 1568 | id_prefix = "weird-id"; |
| 1569 | |
| 1570 | r->id = tal_fmt(r, "\"%s/cln:%s#%"PRIu64"\"", |
| 1571 | id_prefix, method, next_request_id); |
| 1572 | } else { |
| 1573 | r->id = tal_fmt(r, "\"cln:%s#%"PRIu64"\"", method, next_request_id); |
| 1574 | } |
| 1575 | tal_free_if_taken(id_prefix); |
| 1576 | next_request_id++; |
| 1577 | r->notify_cb = notify_cb; |
| 1578 | r->response_cb = response_cb; |
| 1579 | r->response_cb_arg = response_cb_arg; |
| 1580 | r->method = tal_strdup(r, method); |
| 1581 | r->stream = new_json_stream(r, NULL, log); |
| 1582 | |
| 1583 | /* Disabling this serves as an escape hatch for plugin code to |
| 1584 | * get a raw request to paste into, but get a valid request-id |
| 1585 | * assigned. */ |
| 1586 | if (add_header) { |
| 1587 | json_object_start(r->stream, NULL); |
| 1588 | json_add_string(r->stream, "jsonrpc", "2.0"); |
| 1589 | json_add_id(r->stream, r->id); |
| 1590 | json_add_string(r->stream, "method", method); |
| 1591 | json_object_start(r->stream, "params"); |
| 1592 | } |
| 1593 | if (log) |
| 1594 | log_io(log, LOG_IO_OUT, NULL, r->id, NULL, 0); |
| 1595 | |
| 1596 | return r; |
| 1597 | } |
nothing calls this directly
no test coverage detected