xs_httpd_request() also returns the payload.

This commit is contained in:
default 2022-09-23 20:28:23 +02:00
parent bbf5471039
commit 1d694a245a
4 changed files with 54 additions and 12 deletions

View file

@ -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;
}

View file

@ -98,11 +98,13 @@ void httpd_connection(int rs)
char *ctype = NULL; char *ctype = NULL;
xs *headers = NULL; xs *headers = NULL;
xs *q_path = NULL; xs *q_path = NULL;
xs *payload = NULL;
int p_size;
char *p; char *p;
f = xs_socket_accept(rs); 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); xs *j = xs_json_dumps_pp(req, 4);
@ -132,6 +134,9 @@ void httpd_connection(int rs)
} }
else else
if (strcmp(method, "POST") == 0) { 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 */ /* let's go */

3
snac.h
View file

@ -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_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); int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size);
void process_queue(snac *snac); 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);

View file

@ -6,7 +6,7 @@
d_char *xs_url_dec(char *str); d_char *xs_url_dec(char *str);
d_char *xs_url_vars(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); 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 */ /* processes an httpd connection */
{ {
xs *headers = NULL; xs *headers = NULL;
@ -127,19 +127,18 @@ d_char *xs_httpd_request(FILE *f)
xs_socket_timeout(fileno(f), 5.0, 0.0); 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? */ /* does it have a payload with form urlencoded variables? */
v = xs_dict_get(headers, "content-type"); v = xs_dict_get(headers, "content-type");
if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
if ((v = xs_dict_get(headers, "content-length")) != NULL) { xs *upl = xs_url_dec(*payload);
int cl = atoi(v); p_vars = xs_url_vars(upl);
xs *payload;
if ((payload = xs_read(f, cl)) != NULL) {
xs *upl = xs_url_dec(payload);
p_vars = xs_url_vars(upl);
}
}
} }
else else
p_vars = xs_dict_new(); p_vars = xs_dict_new();