diff options
| author | 2022-09-23 18:15:59 +0200 | |
|---|---|---|
| committer | 2022-09-23 18:15:59 +0200 | |
| commit | a0bcc4e6c022fc55956920fd49303abc6ca9b4db (patch) | |
| tree | bbf2da67a9632f0515d73577337dc4c3091c47fb | |
| parent | server_get_handler() returns the status. (diff) | |
| download | snac2-a0bcc4e6c022fc55956920fd49303abc6ca9b4db.tar.gz snac2-a0bcc4e6c022fc55956920fd49303abc6ca9b4db.tar.xz snac2-a0bcc4e6c022fc55956920fd49303abc6ca9b4db.zip | |
Improved activitypub_request().
| -rw-r--r-- | activitypub.c | 49 | ||||
| -rw-r--r-- | main.c | 17 |
2 files changed, 47 insertions, 19 deletions
diff --git a/activitypub.c b/activitypub.c index de99b75..7cbf4db 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -15,8 +15,9 @@ int activitypub_request(snac *snac, char *url, d_char **data) | |||
| 15 | { | 15 | { |
| 16 | int status; | 16 | int status; |
| 17 | xs *response = NULL; | 17 | xs *response = NULL; |
| 18 | xs *payload; | 18 | xs *payload = NULL; |
| 19 | int p_size; | 19 | int p_size; |
| 20 | char *ctype; | ||
| 20 | 21 | ||
| 21 | /* check if it's an url for this same site */ | 22 | /* check if it's an url for this same site */ |
| 22 | /* ... */ | 23 | /* ... */ |
| @@ -25,41 +26,51 @@ int activitypub_request(snac *snac, char *url, d_char **data) | |||
| 25 | response = http_signed_request(snac, "GET", url, | 26 | response = http_signed_request(snac, "GET", url, |
| 26 | NULL, NULL, 0, &status, &payload, &p_size); | 27 | NULL, NULL, 0, &status, &payload, &p_size); |
| 27 | 28 | ||
| 28 | { | ||
| 29 | xs *j = xs_json_loads(response); | ||
| 30 | printf("%s\n", j); | ||
| 31 | } | ||
| 32 | |||
| 33 | if (valid_status(status)) { | 29 | if (valid_status(status)) { |
| 34 | *data = xs_json_loads(payload); | 30 | if (dbglevel >= 3) { |
| 31 | xs *j = xs_json_dumps_pp(response, 4); | ||
| 32 | fprintf(stderr, "%s\n", j); | ||
| 33 | } | ||
| 34 | |||
| 35 | /* ensure it's ActivityPub data */ | ||
| 36 | ctype = xs_dict_get(response, "content-type"); | ||
| 37 | |||
| 38 | if (xs_str_in(ctype, "application/activity+json") != -1) | ||
| 39 | *data = xs_json_loads(payload); | ||
| 40 | else | ||
| 41 | status = 500; | ||
| 35 | } | 42 | } |
| 36 | 43 | ||
| 44 | if (!valid_status(status)) | ||
| 45 | *data = NULL; | ||
| 46 | |||
| 37 | return status; | 47 | return status; |
| 38 | } | 48 | } |
| 39 | 49 | ||
| 40 | 50 | ||
| 41 | #if 0 | ||
| 42 | int actor_request(snac *snac, char *actor, d_char **data) | 51 | int actor_request(snac *snac, char *actor, d_char **data) |
| 43 | /* request an actor */ | 52 | /* request an actor */ |
| 44 | { | 53 | { |
| 45 | int status; | 54 | int status, status2; |
| 46 | xs *response = NULL; | 55 | xs *payload = NULL; |
| 47 | xs *payload; | ||
| 48 | int p_size; | ||
| 49 | 56 | ||
| 50 | /* get from disk first */ | 57 | /* get from disk first */ |
| 51 | status = actor_get(snac, actor, data); | 58 | status = actor_get(snac, actor, data); |
| 52 | 59 | ||
| 53 | if (status == 200) | 60 | if (status == 200) |
| 54 | return; | 61 | return status; |
| 55 | 62 | ||
| 56 | /* get from the net */ | 63 | /* actor data non-existent or stale: get from the net */ |
| 57 | response = http_signed_request(snac, "GET", actor, | 64 | status2 = activitypub_request(snac, actor, &payload); |
| 58 | NULL, NULL, 0, &status, &payload, &p_size); | ||
| 59 | 65 | ||
| 60 | // response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral", | 66 | if (valid_status(status2)) { |
| 61 | // headers, NULL, 0, &status, &payload, &p_size); | 67 | /* renew data */ |
| 68 | xs *j = xs_json_dumps_pp(payload, 4); | ||
| 69 | status = actor_add(snac, actor, j); | ||
| 70 | |||
| 71 | *data = payload; | ||
| 72 | payload = NULL; | ||
| 73 | } | ||
| 62 | 74 | ||
| 63 | return status; | 75 | return status; |
| 64 | } | 76 | } |
| 65 | #endif | ||
| @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) | |||
| 21 | char *user; | 21 | char *user; |
| 22 | char *url; | 22 | char *url; |
| 23 | int argi = 1; | 23 | int argi = 1; |
| 24 | snac snac; | ||
| 24 | 25 | ||
| 25 | argc--; | 26 | argc--; |
| 26 | if (argc < argi) | 27 | if (argc < argi) |
| @@ -73,7 +74,23 @@ int main(int argc, char *argv[]) | |||
| 73 | 74 | ||
| 74 | url = argv[argi++]; | 75 | url = argv[argi++]; |
| 75 | 76 | ||
| 77 | if (!user_open(&snac, user)) { | ||
| 78 | printf("error in user '%s'\n", user); | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | |||
| 76 | if (strcmp(cmd, "request") == 0) { | 82 | if (strcmp(cmd, "request") == 0) { |
| 83 | int status; | ||
| 84 | xs *data = NULL; | ||
| 85 | |||
| 86 | status = activitypub_request(&snac, url, &data); | ||
| 87 | |||
| 88 | printf("status: %d\n", status); | ||
| 89 | if (valid_status(status)) { | ||
| 90 | |||
| 91 | xs *j = xs_json_dumps_pp(data, 4); | ||
| 92 | printf("%s\n", j); | ||
| 93 | } | ||
| 77 | } | 94 | } |
| 78 | 95 | ||
| 79 | return 0; | 96 | return 0; |