jsonprc: make json_get_params() fail the command, for better error reporting.

We move it into jsonrpc where it belongs, and make it fail the command.
This means it can tell us exactly what was wrong.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-01-29 11:04:28 +10:30
committed by Christian Decker
parent 47577e5c4e
commit 91a22dc496
12 changed files with 138 additions and 154 deletions

View File

@@ -225,90 +225,6 @@ const jsmntok_t *json_delve(const char *buffer,
return tok;
}
bool json_get_params(const char *buffer, const jsmntok_t param[], ...)
{
va_list ap;
const char **names;
size_t num_names;
/* Uninitialized warnings on p and end */
const jsmntok_t **tokptr, *p = NULL, *end = NULL;
if (param->type == JSMN_ARRAY) {
if (param->size == 0)
p = NULL;
else
p = param + 1;
end = json_next(param);
} else if (param->type != JSMN_OBJECT)
return false;
num_names = 0;
names = tal_arr(NULL, const char *, num_names + 1);
va_start(ap, param);
while ((names[num_names] = va_arg(ap, const char *)) != NULL) {
tokptr = va_arg(ap, const jsmntok_t **);
bool compulsory = true;
if (names[num_names][0] == '?') {
names[num_names]++;
compulsory = false;
}
if (param->type == JSMN_ARRAY) {
*tokptr = p;
if (p) {
p = json_next(p);
if (p == end)
p = NULL;
}
} else {
*tokptr = json_get_member(buffer, param,
names[num_names]);
}
/* Convert 'null' to NULL */
if (*tokptr
&& (*tokptr)->type == JSMN_PRIMITIVE
&& buffer[(*tokptr)->start] == 'n') {
*tokptr = NULL;
}
if (compulsory && !*tokptr) {
va_end(ap);
tal_free(names);
return false;
}
num_names++;
tal_resize(&names, num_names + 1);
}
va_end(ap);
/* Now make sure there aren't any params which aren't valid */
if (param->type == JSMN_ARRAY) {
if (param->size > num_names) {
tal_free(names);
return false;
}
} else {
const jsmntok_t *t;
end = json_next(param);
/* Find each parameter among the valid names */
for (t = param + 1; t < end; t = json_next(t+1)) {
bool found = false;
for (size_t i = 0; i < num_names; i++) {
if (json_tok_streq(buffer, t, names[i]))
found = true;
}
if (!found) {
tal_free(names);
return false;
}
}
}
tal_free(names);
return true;
}
static bool strange_chars(const char *str, size_t len)
{
for (size_t i = 0; i < len; i++) {