diff options
author | default <nobody@localhost> | 2022-09-23 20:28:23 +0200 |
---|---|---|
committer | default <nobody@localhost> | 2022-09-23 20:28:23 +0200 |
commit | 1d694a245a56bb4fd90fd917ad3648c2c5449746 (patch) | |
tree | cb96eb621ca84ebfdfba40cf859af477639f75d6 | |
parent | bbf5471039a973fed918441150ef76ff0db7682a (diff) |
xs_httpd_request() also returns the payload.
-rw-r--r-- | activitypub.c | 35 | ||||
-rw-r--r-- | httpd.c | 7 | ||||
-rw-r--r-- | snac.h | 3 | ||||
-rw-r--r-- | xs_httpd.h | 21 |
4 files changed, 54 insertions, 12 deletions
diff --git a/activitypub.c b/activitypub.c index b654beb..d470e10 100644 --- a/activitypub.c +++ b/activitypub.c @@ -154,3 +154,38 @@ void process_queue(snac *snac) } } } + + +int activitypub_post_handler(d_char *req, char *q_path, + d_char *payload, int p_size, + char **body, int *b_size, char **ctype) +/* processes an input message */ +{ + int status = 200; + char *i_ctype = xs_dict_get(req, "content-type"); + snac snac; + + if (xs_str_in(i_ctype, "application/activity+json") == -1 && + xs_str_in(i_ctype, "application/ld+json") == -1) + return 0; + + xs *l = xs_split_n(q_path, "/", 2); + char *uid; + + if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) { + /* strange q_path */ + srv_log(xs_fmt("activitypub_post_handler unsupported path %s", q_path)); + return 404; + } + + uid = xs_list_get(l, 1); + if (!user_open(&snac, uid)) { + /* invalid user */ + srv_log(xs_fmt("activitypub_post_handler bad user %s", uid)); + return 404; + } + + user_free(&snac); + + return status; +} @@ -98,11 +98,13 @@ void httpd_connection(int rs) char *ctype = NULL; xs *headers = NULL; xs *q_path = NULL; + xs *payload = NULL; + int p_size; char *p; f = xs_socket_accept(rs); - req = xs_httpd_request(f); + req = xs_httpd_request(f, &payload, &p_size); { xs *j = xs_json_dumps_pp(req, 4); @@ -132,6 +134,9 @@ void httpd_connection(int rs) } else if (strcmp(method, "POST") == 0) { + if (status == 0) + status = activitypub_post_handler(req, q_path, + payload, p_size, &body, &b_size, &ctype); } /* let's go */ @@ -85,3 +85,6 @@ int actor_request(snac *snac, char *actor, d_char **data); int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size); int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size); void process_queue(snac *snac); +int activitypub_post_handler(d_char *req, char *q_path, + char *payload, int p_size, + char **body, int *b_size, char **ctype); @@ -6,7 +6,7 @@ d_char *xs_url_dec(char *str); d_char *xs_url_vars(char *str); -d_char *xs_httpd_request(FILE *f); +d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size); void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size); @@ -69,7 +69,7 @@ d_char *xs_url_vars(char *str) } -d_char *xs_httpd_request(FILE *f) +d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) /* processes an httpd connection */ { xs *headers = NULL; @@ -127,19 +127,18 @@ d_char *xs_httpd_request(FILE *f) xs_socket_timeout(fileno(f), 5.0, 0.0); + if ((v = xs_dict_get(headers, "content-length")) != NULL) { + /* if it has a payload, load it */ + *p_size = atoi(v); + *payload = xs_read(f, *p_size); + } + /* does it have a payload with form urlencoded variables? */ v = xs_dict_get(headers, "content-type"); if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { - if ((v = xs_dict_get(headers, "content-length")) != NULL) { - int cl = atoi(v); - xs *payload; - - if ((payload = xs_read(f, cl)) != NULL) { - xs *upl = xs_url_dec(payload); - p_vars = xs_url_vars(upl); - } - } + xs *upl = xs_url_dec(*payload); + p_vars = xs_url_vars(upl); } else p_vars = xs_dict_new(); |