Improved activitypub_request().

This commit is contained in:
default 2022-09-23 18:15:59 +02:00
parent 02b0df78c6
commit a0bcc4e6c0
2 changed files with 47 additions and 19 deletions

View file

@ -15,8 +15,9 @@ int activitypub_request(snac *snac, char *url, d_char **data)
{ {
int status; int status;
xs *response = NULL; xs *response = NULL;
xs *payload; xs *payload = NULL;
int p_size; int p_size;
char *ctype;
/* check if it's an url for this same site */ /* check if it's an url for this same site */
/* ... */ /* ... */
@ -25,41 +26,51 @@ int activitypub_request(snac *snac, char *url, d_char **data)
response = http_signed_request(snac, "GET", url, response = http_signed_request(snac, "GET", url,
NULL, NULL, 0, &status, &payload, &p_size); NULL, NULL, 0, &status, &payload, &p_size);
{ if (valid_status(status)) {
xs *j = xs_json_loads(response); if (dbglevel >= 3) {
printf("%s\n", j); xs *j = xs_json_dumps_pp(response, 4);
fprintf(stderr, "%s\n", j);
}
/* ensure it's ActivityPub data */
ctype = xs_dict_get(response, "content-type");
if (xs_str_in(ctype, "application/activity+json") != -1)
*data = xs_json_loads(payload);
else
status = 500;
} }
if (valid_status(status)) { if (!valid_status(status))
*data = xs_json_loads(payload); *data = NULL;
}
return status; return status;
} }
#if 0
int actor_request(snac *snac, char *actor, d_char **data) int actor_request(snac *snac, char *actor, d_char **data)
/* request an actor */ /* request an actor */
{ {
int status; int status, status2;
xs *response = NULL; xs *payload = NULL;
xs *payload;
int p_size;
/* get from disk first */ /* get from disk first */
status = actor_get(snac, actor, data); status = actor_get(snac, actor, data);
if (status == 200) if (status == 200)
return; return status;
/* get from the net */ /* actor data non-existent or stale: get from the net */
response = http_signed_request(snac, "GET", actor, status2 = activitypub_request(snac, actor, &payload);
NULL, NULL, 0, &status, &payload, &p_size);
// response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral", if (valid_status(status2)) {
// headers, NULL, 0, &status, &payload, &p_size); /* renew data */
xs *j = xs_json_dumps_pp(payload, 4);
status = actor_add(snac, actor, j);
*data = payload;
payload = NULL;
}
return status; return status;
} }
#endif

17
main.c
View file

@ -21,6 +21,7 @@ int main(int argc, char *argv[])
char *user; char *user;
char *url; char *url;
int argi = 1; int argi = 1;
snac snac;
argc--; argc--;
if (argc < argi) if (argc < argi)
@ -73,7 +74,23 @@ int main(int argc, char *argv[])
url = argv[argi++]; url = argv[argi++];
if (!user_open(&snac, user)) {
printf("error in user '%s'\n", user);
return 1;
}
if (strcmp(cmd, "request") == 0) { if (strcmp(cmd, "request") == 0) {
int status;
xs *data = NULL;
status = activitypub_request(&snac, url, &data);
printf("status: %d\n", status);
if (valid_status(status)) {
xs *j = xs_json_dumps_pp(data, 4);
printf("%s\n", j);
}
} }
return 0; return 0;