commit 8804227

default  ·  2023-04-21 23:21:09 +0000 UTC
parent 66d5acc
New function mastoapi_put_handler().
3 files changed,  +83, -0
M snac.h
+10, -0
 1@@ -215,6 +215,16 @@ void httpd_connection(FILE *f)
 2             status = html_post_handler(req, q_path,
 3                         payload, p_size, &body, &b_size, &ctype);
 4     }
 5+    else
 6+    if (strcmp(method, "PUT") == 0) {
 7+
 8+#ifndef NO_MASTODON_API
 9+        if (status == 0)
10+            status = mastoapi_put_handler(req, q_path,
11+                        payload, p_size, &body, &b_size, &ctype);
12+#endif
13+
14+    }
15 
16     /* let's go */
17     headers = xs_dict_new();
+70, -0
 1@@ -1528,4 +1528,74 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 2     return status;
 3 }
 4 
 5+
 6+int mastoapi_put_handler(const xs_dict *req, const char *q_path,
 7+                          const char *payload, int p_size,
 8+                          char **body, int *b_size, char **ctype)
 9+{
10+    if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
11+        return 0;
12+
13+    srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
14+/*    {
15+        xs *j = xs_json_dumps_pp(req, 4);
16+        printf("mastoapi put:\n%s\n", j);
17+    }*/
18+
19+    int status    = 404;
20+    xs *args      = NULL;
21+    char *i_ctype = xs_dict_get(req, "content-type");
22+
23+    if (i_ctype && xs_startswith(i_ctype, "application/json"))
24+        args = xs_json_loads(payload);
25+    else
26+        args = xs_dup(xs_dict_get(req, "p_vars"));
27+
28+    if (args == NULL)
29+        return 400;
30+
31+    xs *cmd = xs_replace(q_path, "/api", "");
32+
33+    snac snac = {0};
34+    int logged_in = process_auth_token(&snac, req);
35+
36+    if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
37+        if (logged_in) {
38+            xs *l = xs_split(cmd, "/");
39+            const char *stid = xs_list_get(l, 3);
40+
41+            if (!xs_is_null(stid)) {
42+                const char *desc = xs_dict_get(args, "description");
43+
44+                /* set the image metadata */
45+                static_put_meta(&snac, stid, desc);
46+
47+                /* prepare a response */
48+                xs *rsp = xs_dict_new();
49+                xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
50+
51+                rsp = xs_dict_append(rsp, "id",          stid);
52+                rsp = xs_dict_append(rsp, "type",        "image");
53+                rsp = xs_dict_append(rsp, "url",         url);
54+                rsp = xs_dict_append(rsp, "preview_url", url);
55+                rsp = xs_dict_append(rsp, "remote_url",  url);
56+                rsp = xs_dict_append(rsp, "description", desc);
57+
58+                *body  = xs_json_dumps_pp(rsp, 4);
59+                *ctype = "application/json";
60+                status = 200;
61+            }
62+        }
63+        else
64+            status = 401;
65+    }
66+
67+    /* user cleanup */
68+    if (logged_in)
69+        user_free(&snac);
70+
71+    return status;
72+}
73+
74+
75 #endif /* #ifndef NO_MASTODON_API */
M snac.h
+3, -0
1@@ -248,3 +248,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
2 int mastoapi_post_handler(const xs_dict *req, const char *q_path,
3                           const char *payload, int p_size,
4                           char **body, int *b_size, char **ctype);
5+int mastoapi_put_handler(const xs_dict *req, const char *q_path,
6+                          const char *payload, int p_size,
7+                          char **body, int *b_size, char **ctype);