summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2023-04-22 01:21:09 +0200
committerGravatar default2023-04-22 01:21:09 +0200
commit88042277980edb34f5bc495dfc8b20a05cfe71c2 (patch)
tree670cbaebc367a6bc669c866f3e68ab4cc1cb7f2b
parentUse static_get_meta() when preparing the attachment. (diff)
downloadsnac2-88042277980edb34f5bc495dfc8b20a05cfe71c2.tar.gz
snac2-88042277980edb34f5bc495dfc8b20a05cfe71c2.tar.xz
snac2-88042277980edb34f5bc495dfc8b20a05cfe71c2.zip
New function mastoapi_put_handler().
-rw-r--r--httpd.c10
-rw-r--r--mastoapi.c70
-rw-r--r--snac.h3
3 files changed, 83 insertions, 0 deletions
diff --git a/httpd.c b/httpd.c
index 3168323..79d489f 100644
--- a/httpd.c
+++ b/httpd.c
@@ -215,6 +215,16 @@ void httpd_connection(FILE *f)
215 status = html_post_handler(req, q_path, 215 status = html_post_handler(req, q_path,
216 payload, p_size, &body, &b_size, &ctype); 216 payload, p_size, &body, &b_size, &ctype);
217 } 217 }
218 else
219 if (strcmp(method, "PUT") == 0) {
220
221#ifndef NO_MASTODON_API
222 if (status == 0)
223 status = mastoapi_put_handler(req, q_path,
224 payload, p_size, &body, &b_size, &ctype);
225#endif
226
227 }
218 228
219 /* let's go */ 229 /* let's go */
220 headers = xs_dict_new(); 230 headers = xs_dict_new();
diff --git a/mastoapi.c b/mastoapi.c
index b8362b2..f924c2b 100644
--- a/mastoapi.c
+++ b/mastoapi.c
@@ -1528,4 +1528,74 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
1528 return status; 1528 return status;
1529} 1529}
1530 1530
1531
1532int mastoapi_put_handler(const xs_dict *req, const char *q_path,
1533 const char *payload, int p_size,
1534 char **body, int *b_size, char **ctype)
1535{
1536 if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
1537 return 0;
1538
1539 srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
1540/* {
1541 xs *j = xs_json_dumps_pp(req, 4);
1542 printf("mastoapi put:\n%s\n", j);
1543 }*/
1544
1545 int status = 404;
1546 xs *args = NULL;
1547 char *i_ctype = xs_dict_get(req, "content-type");
1548
1549 if (i_ctype && xs_startswith(i_ctype, "application/json"))
1550 args = xs_json_loads(payload);
1551 else
1552 args = xs_dup(xs_dict_get(req, "p_vars"));
1553
1554 if (args == NULL)
1555 return 400;
1556
1557 xs *cmd = xs_replace(q_path, "/api", "");
1558
1559 snac snac = {0};
1560 int logged_in = process_auth_token(&snac, req);
1561
1562 if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
1563 if (logged_in) {
1564 xs *l = xs_split(cmd, "/");
1565 const char *stid = xs_list_get(l, 3);
1566
1567 if (!xs_is_null(stid)) {
1568 const char *desc = xs_dict_get(args, "description");
1569
1570 /* set the image metadata */
1571 static_put_meta(&snac, stid, desc);
1572
1573 /* prepare a response */
1574 xs *rsp = xs_dict_new();
1575 xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
1576
1577 rsp = xs_dict_append(rsp, "id", stid);
1578 rsp = xs_dict_append(rsp, "type", "image");
1579 rsp = xs_dict_append(rsp, "url", url);
1580 rsp = xs_dict_append(rsp, "preview_url", url);
1581 rsp = xs_dict_append(rsp, "remote_url", url);
1582 rsp = xs_dict_append(rsp, "description", desc);
1583
1584 *body = xs_json_dumps_pp(rsp, 4);
1585 *ctype = "application/json";
1586 status = 200;
1587 }
1588 }
1589 else
1590 status = 401;
1591 }
1592
1593 /* user cleanup */
1594 if (logged_in)
1595 user_free(&snac);
1596
1597 return status;
1598}
1599
1600
1531#endif /* #ifndef NO_MASTODON_API */ 1601#endif /* #ifndef NO_MASTODON_API */
diff --git a/snac.h b/snac.h
index 090809a..e4689a1 100644
--- a/snac.h
+++ b/snac.h
@@ -248,3 +248,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
248int mastoapi_post_handler(const xs_dict *req, const char *q_path, 248int mastoapi_post_handler(const xs_dict *req, const char *q_path,
249 const char *payload, int p_size, 249 const char *payload, int p_size,
250 char **body, int *b_size, char **ctype); 250 char **body, int *b_size, char **ctype);
251int mastoapi_put_handler(const xs_dict *req, const char *q_path,
252 const char *payload, int p_size,
253 char **body, int *b_size, char **ctype);