summaryrefslogtreecommitdiff
path: root/activitypub.c
diff options
context:
space:
mode:
Diffstat (limited to 'activitypub.c')
-rw-r--r--activitypub.c56
1 files changed, 31 insertions, 25 deletions
diff --git a/activitypub.c b/activitypub.c
index 6e40a88..4247078 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -96,19 +96,19 @@ int activitypub_request(snac *user, const char *url, xs_dict **data)
96 ctype = xs_dict_get(response, "content-type"); 96 ctype = xs_dict_get(response, "content-type");
97 97
98 if (xs_is_null(ctype)) 98 if (xs_is_null(ctype))
99 status = 400; 99 status = HTTP_STATUS_BAD_REQUEST;
100 else 100 else
101 if (xs_str_in(ctype, "application/activity+json") != -1 || 101 if (xs_str_in(ctype, "application/activity+json") != -1 ||
102 xs_str_in(ctype, "application/ld+json") != -1) { 102 xs_str_in(ctype, "application/ld+json") != -1) {
103 103
104 /* if there is no payload, fail */ 104 /* if there is no payload, fail */
105 if (xs_is_null(payload)) 105 if (xs_is_null(payload))
106 status = 400; 106 status = HTTP_STATUS_BAD_REQUEST;
107 else 107 else
108 *data = xs_json_loads(payload); 108 *data = xs_json_loads(payload);
109 } 109 }
110 else 110 else
111 status = 500; 111 status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
112 } 112 }
113 113
114 return status; 114 return status;
@@ -443,7 +443,7 @@ int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
443 xs_val **payload, int *p_size, int timeout) 443 xs_val **payload, int *p_size, int timeout)
444/* sends a message to an actor */ 444/* sends a message to an actor */
445{ 445{
446 int status = 400; 446 int status = HTTP_STATUS_BAD_REQUEST;
447 xs *inbox = get_actor_inbox(actor); 447 xs *inbox = get_actor_inbox(actor);
448 448
449 if (!xs_is_null(inbox)) 449 if (!xs_is_null(inbox))
@@ -1762,7 +1762,9 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
1762 a_status = actor_request(snac, actor, &actor_o); 1762 a_status = actor_request(snac, actor, &actor_o);
1763 1763
1764 /* do not retry permanent failures */ 1764 /* do not retry permanent failures */
1765 if (a_status == 404 || a_status == 410 || a_status < 0) { 1765 if (a_status == HTTP_STATUS_NOT_FOUND
1766 || a_status == HTTP_STATUS_GONE
1767 || a_status < 0) {
1766 srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status)); 1768 srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
1767 return -1; 1769 return -1;
1768 } 1770 }
@@ -1905,7 +1907,7 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
1905 } 1907 }
1906 else 1908 else
1907 if (strcmp(utype, "Announce") == 0) { /** **/ 1909 if (strcmp(utype, "Announce") == 0) { /** **/
1908 int status = 200; 1910 int status = HTTP_STATUS_OK;
1909 1911
1910 /* commented out: if a followed user boosts something that 1912 /* commented out: if a followed user boosts something that
1911 is requested and then unboosts, the post remains here, 1913 is requested and then unboosts, the post remains here,
@@ -2015,7 +2017,7 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
2015 if (xs_type(object) == XSTYPE_DICT) 2017 if (xs_type(object) == XSTYPE_DICT)
2016 object = xs_dict_get(object, "id"); 2018 object = xs_dict_get(object, "id");
2017 2019
2018 if (timeline_admire(snac, object, actor, 1) == 201) 2020 if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
2019 snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object)); 2021 snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
2020 else 2022 else
2021 snac_log(snac, xs_fmt("repeated 'Like' from %s to %s", actor, object)); 2023 snac_log(snac, xs_fmt("repeated 'Like' from %s to %s", actor, object));
@@ -2046,7 +2048,7 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
2046 xs *who_o = NULL; 2048 xs *who_o = NULL;
2047 2049
2048 if (valid_status(actor_request(snac, who, &who_o))) { 2050 if (valid_status(actor_request(snac, who, &who_o))) {
2049 if (timeline_admire(snac, object, actor, 0) == 201) 2051 if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
2050 snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object)); 2052 snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
2051 else 2053 else
2052 snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s", 2054 snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
@@ -2383,11 +2385,15 @@ void process_queue_item(xs_dict *q_item)
2383 2385
2384 /* if it's not the first time it fails with a timeout, 2386 /* if it's not the first time it fails with a timeout,
2385 penalize the server by skipping one retry */ 2387 penalize the server by skipping one retry */
2386 if (p_status == status && status == 499) 2388 if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
2387 retries++; 2389 retries++;
2388 2390
2389 /* error sending; requeue? */ 2391 /* error sending; requeue? */
2390 if (status == 400 || status == 404 || status == 405 || status == 410 || status < 0) 2392 if (status == HTTP_STATUS_BAD_REQUEST
2393 || status == HTTP_STATUS_NOT_FOUND
2394 || status == HTTP_STATUS_METHOD_NOT_ALLOWED
2395 || status == HTTP_STATUS_GONE
2396 || status < 0)
2391 /* explicit error: discard */ 2397 /* explicit error: discard */
2392 srv_log(xs_fmt("output message: fatal error %s %d", inbox, status)); 2398 srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
2393 else 2399 else
@@ -2574,7 +2580,7 @@ int process_queue(void)
2574int activitypub_get_handler(const xs_dict *req, const char *q_path, 2580int activitypub_get_handler(const xs_dict *req, const char *q_path,
2575 char **body, int *b_size, char **ctype) 2581 char **body, int *b_size, char **ctype)
2576{ 2582{
2577 int status = 200; 2583 int status = HTTP_STATUS_OK;
2578 const char *accept = xs_dict_get(req, "accept"); 2584 const char *accept = xs_dict_get(req, "accept");
2579 snac snac; 2585 snac snac;
2580 xs *msg = NULL; 2586 xs *msg = NULL;
@@ -2594,7 +2600,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
2594 if (!user_open(&snac, uid)) { 2600 if (!user_open(&snac, uid)) {
2595 /* invalid user */ 2601 /* invalid user */
2596 srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid)); 2602 srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
2597 return 404; 2603 return HTTP_STATUS_NOT_FOUND;
2598 } 2604 }
2599 2605
2600 p_path = xs_list_get(l, 2); 2606 p_path = xs_list_get(l, 2);
@@ -2652,12 +2658,12 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
2652 2658
2653 /* don't return non-public objects */ 2659 /* don't return non-public objects */
2654 if (valid_status(status) && !is_msg_public(msg)) 2660 if (valid_status(status) && !is_msg_public(msg))
2655 status = 404; 2661 status = HTTP_STATUS_NOT_FOUND;
2656 } 2662 }
2657 else 2663 else
2658 status = 404; 2664 status = HTTP_STATUS_NOT_FOUND;
2659 2665
2660 if (status == 200 && msg != NULL) { 2666 if (status == HTTP_STATUS_OK && msg != NULL) {
2661 *body = xs_json_dumps(msg, 4); 2667 *body = xs_json_dumps(msg, 4);
2662 *b_size = strlen(*body); 2668 *b_size = strlen(*body);
2663 } 2669 }
@@ -2677,7 +2683,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2677{ 2683{
2678 (void)b_size; 2684 (void)b_size;
2679 2685
2680 int status = 202; /* accepted */ 2686 int status = HTTP_STATUS_ACCEPTED;
2681 const char *i_ctype = xs_dict_get(req, "content-type"); 2687 const char *i_ctype = xs_dict_get(req, "content-type");
2682 snac snac; 2688 snac snac;
2683 const char *v; 2689 const char *v;
@@ -2685,13 +2691,13 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2685 if (i_ctype == NULL) { 2691 if (i_ctype == NULL) {
2686 *body = xs_str_new("no content-type"); 2692 *body = xs_str_new("no content-type");
2687 *ctype = "text/plain"; 2693 *ctype = "text/plain";
2688 return 400; 2694 return HTTP_STATUS_BAD_REQUEST;
2689 } 2695 }
2690 2696
2691 if (xs_is_null(payload)) { 2697 if (xs_is_null(payload)) {
2692 *body = xs_str_new("no payload"); 2698 *body = xs_str_new("no payload");
2693 *ctype = "text/plain"; 2699 *ctype = "text/plain";
2694 return 400; 2700 return HTTP_STATUS_BAD_REQUEST;
2695 } 2701 }
2696 2702
2697 if (xs_str_in(i_ctype, "application/activity+json") == -1 && 2703 if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
@@ -2709,7 +2715,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2709 2715
2710 *body = xs_str_new("JSON error"); 2716 *body = xs_str_new("JSON error");
2711 *ctype = "text/plain"; 2717 *ctype = "text/plain";
2712 return 400; 2718 return HTTP_STATUS_BAD_REQUEST;
2713 } 2719 }
2714 2720
2715 if (id && is_instance_blocked(id)) { 2721 if (id && is_instance_blocked(id)) {
@@ -2717,7 +2723,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2717 2723
2718 *body = xs_str_new("blocked"); 2724 *body = xs_str_new("blocked");
2719 *ctype = "text/plain"; 2725 *ctype = "text/plain";
2720 return 403; 2726 return HTTP_STATUS_FORBIDDEN;
2721 } 2727 }
2722 2728
2723 /* get the user and path */ 2729 /* get the user and path */
@@ -2725,20 +2731,20 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2725 2731
2726 if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) { 2732 if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
2727 enqueue_shared_input(msg, req, 0); 2733 enqueue_shared_input(msg, req, 0);
2728 return 202; 2734 return HTTP_STATUS_ACCEPTED;
2729 } 2735 }
2730 2736
2731 if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) { 2737 if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
2732 /* strange q_path */ 2738 /* strange q_path */
2733 srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path)); 2739 srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
2734 return 404; 2740 return HTTP_STATUS_NOT_FOUND;
2735 } 2741 }
2736 2742
2737 const char *uid = xs_list_get(l, 1); 2743 const char *uid = xs_list_get(l, 1);
2738 if (!user_open(&snac, uid)) { 2744 if (!user_open(&snac, uid)) {
2739 /* invalid user */ 2745 /* invalid user */
2740 srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid)); 2746 srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
2741 return 404; 2747 return HTTP_STATUS_NOT_FOUND;
2742 } 2748 }
2743 2749
2744 /* if it has a digest, check it now, because 2750 /* if it has a digest, check it now, because
@@ -2752,7 +2758,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2752 2758
2753 *body = xs_str_new("bad digest"); 2759 *body = xs_str_new("bad digest");
2754 *ctype = "text/plain"; 2760 *ctype = "text/plain";
2755 status = 400; 2761 status = HTTP_STATUS_BAD_REQUEST;
2756 } 2762 }
2757 } 2763 }
2758 2764
@@ -2763,7 +2769,7 @@ int activitypub_post_handler(const xs_dict *req, const char *q_path,
2763 2769
2764 *body = xs_str_new("rejected"); 2770 *body = xs_str_new("rejected");
2765 *ctype = "text/plain"; 2771 *ctype = "text/plain";
2766 status = 403; 2772 status = HTTP_STATUS_FORBIDDEN;
2767 } 2773 }
2768 } 2774 }
2769 2775