Kaynağa Gözat

Don't generate invalid JSON if an attachment has no description.

default 3 yıl önce
ebeveyn
işleme
885031bed9
3 değiştirilmiş dosya ile 22 ekleme ve 5 silme
  1. 7 3
      data.c
  2. 14 1
      mastoapi.c
  3. 1 1
      snac.h

+ 7 - 3
data.c

@@ -1971,7 +1971,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req,
 
 
 void srv_archive_error(const char *prefix, const xs_str *err,
-                       const xs_dict *req, const xs_dict *data)
+                       const xs_dict *req, const xs_val *data)
 /* archives an error */
 {
     xs *ntid = tid(0);
@@ -1993,8 +1993,12 @@ void srv_archive_error(const char *prefix, const xs_str *err,
         if (data) {
             fprintf(f, "Data:\n");
 
-            xs *j = xs_json_dumps_pp(data, 4);
-            fwrite(j, strlen(j), 1, f);
+            if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
+                xs *j = xs_json_dumps_pp(data, 4);
+                fwrite(j, strlen(j), 1, f);
+            }
+            else
+                fprintf(f, "%s", data);
 
             fprintf(f, "\n");
         }

+ 14 - 1
mastoapi.c

@@ -506,7 +506,12 @@ xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
             matte = xs_dict_append(matte, "url",         xs_dict_get(aobj, "url"));
             matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
             matte = xs_dict_append(matte, "remote_url",  xs_dict_get(aobj, "url"));
-            matte = xs_dict_append(matte, "description", xs_dict_get(aobj, "name"));
+
+            const char *name = xs_dict_get(aobj, "name");
+            if (xs_is_null(name))
+                name = "";
+
+            matte = xs_dict_append(matte, "description", name);
 
             matt = xs_list_append(matt, matte);
         }
@@ -730,6 +735,14 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
             *ctype = "application/json";
             status = 200;
 
+            {
+                xs *j = xs_json_loads(*body);
+                if (j == NULL) {
+                    srv_debug(0, xs_fmt("mastoapi timeline: bad JSON"));
+                    srv_archive_error("mastoapi_timeline", "bad JSON", req, *body);
+                }
+            }
+
             srv_debug(0, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
         }
         else {

+ 1 - 1
snac.h

@@ -56,7 +56,7 @@ void srv_archive(const char *direction, const char *url, xs_dict *req,
                  int status, xs_dict *headers,
                  const char *body, int b_size);
 void srv_archive_error(const char *prefix, const xs_str *err,
-                       const xs_dict *req, const xs_dict *data);
+                       const xs_dict *req, const xs_val *data);
 
 double mtime_nl(const char *fn, int *n_link);
 #define mtime(fn) mtime_nl(fn, NULL)