Fix #1712 - JSON parsing for keys with arrays

This commit is contained in:
cpq committed 2022-09-08 10:59:23 +01:00
1 parent c33116166f
commit a57c0f4700
3 files changed
+41 -9

No files matched your search

+14
View File
@@ -1901,7 +1901,19 @@ static void printdirentry(const char *name, void *userdata) {
} else {
mg_snprintf(sz, sizeof(sz), "%lld", (uint64_t) size);
}
#if defined(MG_HTTP_DIRLIST_TIME)
char time_str[30];
struct tm * time_info = localtime(&t);
strftime(time_str, sizeof time_str, "%Y/%m/%d %H:%M:%S", time_info);
mg_snprintf(mod, sizeof(mod), "%s", time_str);
#elif defined(MG_HTTP_DIRLIST_TIME_UTC)
char time_str[30];
struct tm * time_info = gmtime(&t);
strftime(time_str, sizeof time_str, "%Y/%m/%d %H:%M:%S", time_info);
mg_snprintf(mod, sizeof(mod), "%s", time_str);
#else
mg_snprintf(mod, sizeof(mod), "%ld", (unsigned long) t);
#endif
n = (int) mg_url_encode(name, strlen(name), path, sizeof(path));
mg_printf(d->c,
" <tr><td><a href=\"%.*s%s\">%s%s</a></td>"
@@ -2482,6 +2494,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
int pos = 1; // Current position in `path`
int ci = -1, ei = -1; // Current and expected index in array
if (toklen) *toklen = 0;
if (path[0] != '$') return MG_JSON_INVALID;
#define MG_CHECKRET(x) \
@@ -2560,6 +2573,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
if (n < 0) return n;
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
if (depth < ed) return MG_JSON_NOT_FOUND;
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
// printf("K %s [%.*s] [%.*s] %d %d %d\n", path, pos, path, n,
// &s[i + 1], n, depth, ed);
// NOTE(cpq): in the check sequence below is important.
+3 -1
View File
@@ -1,6 +1,6 @@
#include "json.h"
#include "base64.h"
#include "fmt.h"
#include "json.h"
static const char *escapeseq(int esc) {
return esc ? "\b\f\n\r\t\\\"" : "bfnrt\\\"";
@@ -86,6 +86,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
int pos = 1; // Current position in `path`
int ci = -1, ei = -1; // Current and expected index in array
if (toklen) *toklen = 0;
if (path[0] != '$') return MG_JSON_INVALID;
#define MG_CHECKRET(x) \
@@ -164,6 +165,7 @@ int mg_json_get(struct mg_str json, const char *path, int *toklen) {
if (n < 0) return n;
if (i + 1 + n >= len) return MG_JSON_NOT_FOUND;
if (depth < ed) return MG_JSON_NOT_FOUND;
if (depth == ed && path[pos - 1] != '.') return MG_JSON_NOT_FOUND;
// printf("K %s [%.*s] [%.*s] %d %d %d\n", path, pos, path, n,
// &s[i + 1], n, depth, ed);
// NOTE(cpq): in the check sequence below is important.
+24 -8
View File
@@ -2393,21 +2393,21 @@ static void test_json(void) {
ASSERT(mg_json_get(mg_str_n("[1,2]", 5), "$[3]", &n) == MG_JSON_NOT_FOUND);
json = mg_str("{\"a\":[]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 2);
json = mg_str("{\"a\":[1,2]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 5);
json = mg_str("{\"a\":[1,[1]]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 7);
json = mg_str("{\"a\":[[]]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 4);
json = mg_str("{\"a\":[[1,2]]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 7);
json = mg_str("{\"a\":{}}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 2);
json = mg_str("{\"a\":{\"a\":{}}}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 8);
json = mg_str("{\"a\":{\"a\":[]}}");
ASSERT(mg_json_get(json, "$.a", &n) == 5);
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 8);
json = mg_str("[[1,[2,3]],4]");
ASSERT(mg_json_get(json, "$", &n) == 0 && n == 13);
@@ -2521,6 +2521,22 @@ static void test_json(void) {
ASSERT(mg_json_get_long(json, "$.a[2]", -42) == 4);
ASSERT(mg_json_get_long(json, "$.a[3]", -42) == -42);
}
json = mg_str("{\"a\":[],\"b\":[1,2]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 2);
ASSERT(mg_json_get(json, "$.a[0]", &n) < 0 && n == 0);
json = mg_str("{\"a\":{},\"b\":[1,2]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 2);
ASSERT(mg_json_get(json, "$.a[0]", &n) < 0 && n == 0);
json = mg_str("{\"a\":true,\"b\":[1,2]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 4);
ASSERT(mg_json_get(json, "$.a[0]", &n) < 0 && n == 0);
json = mg_str("{\"a\":1,\"b\":[1,2]}");
ASSERT(mg_json_get(json, "$.a", &n) == 5 && n == 1);
ASSERT(mg_json_get(json, "$.a[0]", &n) < 0 && n == 0);
}
static void resp_rpc(struct mg_rpc_req *r) {