commit 885031b

default  ·  2023-04-12 08:41:15 +0000 UTC
parent aa86357
Don't generate invalid JSON if an attachment has no description.
3 files changed,  +22, -5
M data.c
M snac.h
M data.c
+7, -3
 1@@ -1971,7 +1971,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req,
 2 
 3 
 4 void srv_archive_error(const char *prefix, const xs_str *err,
 5-                       const xs_dict *req, const xs_dict *data)
 6+                       const xs_dict *req, const xs_val *data)
 7 /* archives an error */
 8 {
 9     xs *ntid = tid(0);
10@@ -1993,8 +1993,12 @@ void srv_archive_error(const char *prefix, const xs_str *err,
11         if (data) {
12             fprintf(f, "Data:\n");
13 
14-            xs *j = xs_json_dumps_pp(data, 4);
15-            fwrite(j, strlen(j), 1, f);
16+            if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
17+                xs *j = xs_json_dumps_pp(data, 4);
18+                fwrite(j, strlen(j), 1, f);
19+            }
20+            else
21+                fprintf(f, "%s", data);
22 
23             fprintf(f, "\n");
24         }
+14, -1
 1@@ -506,7 +506,12 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
 2             matte = xs_dict_append(matte, "url",         xs_dict_get(aobj, "url"));
 3             matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
 4             matte = xs_dict_append(matte, "remote_url",  xs_dict_get(aobj, "url"));
 5-            matte = xs_dict_append(matte, "description", xs_dict_get(aobj, "name"));
 6+
 7+            const char *name = xs_dict_get(aobj, "name");
 8+            if (xs_is_null(name))
 9+                name = "";
10+
11+            matte = xs_dict_append(matte, "description", name);
12 
13             matt = xs_list_append(matt, matte);
14         }
15@@ -730,6 +735,14 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
16             *ctype = "application/json";
17             status = 200;
18 
19+            {
20+                xs *j = xs_json_loads(*body);
21+                if (j == NULL) {
22+                    srv_debug(0, xs_fmt("mastoapi timeline: bad JSON"));
23+                    srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
24+                }
25+            }
26+
27             srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
28         }
29         else {
M snac.h
+1, -1
1@@ -56,7 +56,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req,
2                  int status, xs_dict *headers,
3                  const char *body, int b_size);
4 void srv_archive_error(const char *prefix, const xs_str *err,
5-                       const xs_dict *req, const xs_dict *data);
6+                       const xs_dict *req, const xs_val *data);
7 
8 double mtime_nl(const char *fn, int *n_link);
9 #define mtime(fn) mtime_nl(fn, NULL)