From 88042277980edb34f5bc495dfc8b20a05cfe71c2 Mon Sep 17 00:00:00 2001 From: default Date: Sat, 22 Apr 2023 01:21:09 +0200 Subject: [PATCH] New function mastoapi_put_handler(). --- httpd.c | 10 ++++++++ mastoapi.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ snac.h | 3 +++ 3 files changed, 83 insertions(+) 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) status = html_post_handler(req, q_path, payload, p_size, &body, &b_size, &ctype); } + else + if (strcmp(method, "PUT") == 0) { + +#ifndef NO_MASTODON_API + if (status == 0) + status = mastoapi_put_handler(req, q_path, + payload, p_size, &body, &b_size, &ctype); +#endif + + } /* let's go */ 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, return status; } + +int mastoapi_put_handler(const xs_dict *req, const char *q_path, + const char *payload, int p_size, + char **body, int *b_size, char **ctype) +{ + if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/")) + return 0; + + srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); +/* { + xs *j = xs_json_dumps_pp(req, 4); + printf("mastoapi put:\n%s\n", j); + }*/ + + int status = 404; + xs *args = NULL; + char *i_ctype = xs_dict_get(req, "content-type"); + + if (i_ctype && xs_startswith(i_ctype, "application/json")) + args = xs_json_loads(payload); + else + args = xs_dup(xs_dict_get(req, "p_vars")); + + if (args == NULL) + return 400; + + xs *cmd = xs_replace(q_path, "/api", ""); + + snac snac = {0}; + int logged_in = process_auth_token(&snac, req); + + if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { + if (logged_in) { + xs *l = xs_split(cmd, "/"); + const char *stid = xs_list_get(l, 3); + + if (!xs_is_null(stid)) { + const char *desc = xs_dict_get(args, "description"); + + /* set the image metadata */ + static_put_meta(&snac, stid, desc); + + /* prepare a response */ + xs *rsp = xs_dict_new(); + xs *url = xs_fmt("%s/s/%s", snac.actor, stid); + + rsp = xs_dict_append(rsp, "id", stid); + rsp = xs_dict_append(rsp, "type", "image"); + rsp = xs_dict_append(rsp, "url", url); + rsp = xs_dict_append(rsp, "preview_url", url); + rsp = xs_dict_append(rsp, "remote_url", url); + rsp = xs_dict_append(rsp, "description", desc); + + *body = xs_json_dumps_pp(rsp, 4); + *ctype = "application/json"; + status = 200; + } + } + else + status = 401; + } + + /* user cleanup */ + if (logged_in) + user_free(&snac); + + return status; +} + + #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, int mastoapi_post_handler(const xs_dict *req, const char *q_path, const char *payload, int p_size, char **body, int *b_size, char **ctype); +int mastoapi_put_handler(const xs_dict *req, const char *q_path, + const char *payload, int p_size, + char **body, int *b_size, char **ctype);