param: upgraded json_tok_escaped_string

Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
Mark Beckwith 2018-08-29 15:44:04 -05:00 committed by Rusty Russell
parent 8590dbedfb
commit aa60057134
6 changed files with 34 additions and 52 deletions

View File

@ -13,9 +13,9 @@ struct json_escaped *json_escaped_string_(const tal_t *ctx,
return esc;
}
struct json_escaped *json_tok_escaped_string(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
struct json_escaped *json_to_escaped_string(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
{
if (tok->type != JSMN_STRING)
return NULL;

View File

@ -17,9 +17,9 @@ struct json_escaped *json_partial_escape(const tal_t *ctx,
const char *str TAKES);
/* Extract a JSON-escaped string. */
struct json_escaped *json_tok_escaped_string(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok);
struct json_escaped *json_to_escaped_string(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok);
/* Are two escaped json strings identical? */
bool json_escaped_eq(const struct json_escaped *a,

View File

@ -131,10 +131,10 @@ static void json_invoice(struct command *cmd,
{
struct invoice invoice;
const struct invoice_details *details;
const jsmntok_t *desctok, *fallbacks;
const jsmntok_t *fallbacks;
const jsmntok_t *preimagetok;
u64 *msatoshi_val;
struct json_escaped *label_val, *desc;
struct json_escaped *label_val;
const char *desc_val;
struct json_result *response = new_json_result(cmd);
struct wallet *wallet = cmd->ld->wallet;
@ -147,7 +147,7 @@ static void json_invoice(struct command *cmd,
if (!param(cmd, buffer, params,
p_req("msatoshi", json_tok_msat, &msatoshi_val),
p_req("label", json_tok_label, &label_val),
p_req("description", json_tok_tok, &desctok),
p_req("description", json_tok_escaped_string, &desc_val),
p_opt_def("expiry", json_tok_u64, &expiry, 3600),
p_opt("fallbacks", json_tok_tok, &fallbacks),
p_opt("preimage", json_tok_tok, &preimagetok),
@ -166,23 +166,6 @@ static void json_invoice(struct command *cmd,
return;
}
desc = json_tok_escaped_string(cmd, buffer, desctok);
if (!desc) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"description '%.*s' not a string",
desctok->end - desctok->start,
buffer + desctok->start);
return;
}
desc_val = json_escaped_unescape(cmd, desc);
if (!desc_val) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"description '%s' is invalid"
" (note: we don't allow \\u)",
desc->s);
return;
}
/* description */
if (strlen(desc_val) >= BOLT11_FIELD_BYTE_LIMIT) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Descriptions greater than %d bytes "

View File

@ -129,11 +129,28 @@ bool json_tok_double(struct command *cmd, const char *name,
return false;
}
bool json_tok_escaped_string(struct command *cmd, const char *name,
const char * buffer, const jsmntok_t *tok,
const char **str)
{
struct json_escaped *esc = json_to_escaped_string(cmd, buffer, tok);
if (esc)
if ((*str = json_escaped_unescape(cmd, esc)))
return true;
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be a string, not '%.*s'"
" (note, we don't allow \\u)",
name,
tok->end - tok->start, buffer + tok->start);
return false;
}
bool json_tok_label(struct command *cmd, const char *name,
const char * buffer, const jsmntok_t *tok,
struct json_escaped **label)
{
if ((*label = json_tok_escaped_string(cmd, buffer, tok)))
if ((*label = json_to_escaped_string(cmd, buffer, tok)))
return true;
/* Allow literal numbers */

View File

@ -54,6 +54,11 @@ bool json_tok_double(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
double **num);
/* Extract an escaped string (and unescape it) */
bool json_tok_escaped_string(struct command *cmd, const char *name,
const char * buffer, const jsmntok_t *tok,
const char **str);
/* Extract a label. It is either an escaped string or a number. */
bool json_tok_label(struct command *cmd, const char *name,
const char * buffer, const jsmntok_t *tok,

View File

@ -945,19 +945,18 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r,
static void json_sendpay(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
const jsmntok_t *routetok, *desctok;
const jsmntok_t *routetok;
const jsmntok_t *t, *end;
size_t n_hops;
struct sha256 *rhash;
struct route_hop *route;
u64 *msatoshi;
const struct json_escaped *desc;
const char *description;
if (!param(cmd, buffer, params,
p_req("route", json_tok_tok, &routetok),
p_req("payment_hash", json_tok_sha256, &rhash),
p_opt("description", json_tok_tok, &desctok),
p_opt("description", json_tok_escaped_string, &description),
p_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
@ -1017,28 +1016,6 @@ static void json_sendpay(struct command *cmd,
}
}
if (desctok) {
desc = json_tok_escaped_string(cmd, buffer, desctok);
if (!desc) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"description '%.*s' not a string",
desctok->end - desctok->start,
buffer + desctok->start);
return;
}
description = json_escaped_unescape(cmd, desc);
if (description == NULL) {
command_fail(
cmd, JSONRPC2_INVALID_PARAMS,
"description '%.*s' not a valid escaped string",
desctok->end - desctok->start,
buffer + desctok->start);
return;
}
} else {
description = NULL;
}
if (send_payment(cmd, cmd->ld, rhash, route,
msatoshi ? *msatoshi : route[n_hops-1].amount,
description,