diff options
author | default <nobody@localhost> | 2023-04-22 01:21:09 +0200 |
---|---|---|
committer | default <nobody@localhost> | 2023-04-22 01:21:09 +0200 |
commit | 88042277980edb34f5bc495dfc8b20a05cfe71c2 (patch) | |
tree | 670cbaebc367a6bc669c866f3e68ab4cc1cb7f2b | |
parent | 66d5acc8226ee9a2b2c047ad62b96a865894ce96 (diff) |
New function mastoapi_put_handler().
-rw-r--r-- | httpd.c | 10 | ||||
-rw-r--r-- | mastoapi.c | 70 | ||||
-rw-r--r-- | snac.h | 3 |
3 files changed, 83 insertions, 0 deletions
@@ -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(); @@ -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 */ @@ -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); |