summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar default2022-09-23 20:28:23 +0200
committerGravatar default2022-09-23 20:28:23 +0200
commit1d694a245a56bb4fd90fd917ad3648c2c5449746 (patch)
treecb96eb621ca84ebfdfba40cf859af477639f75d6
parentNew function process_queue(). (diff)
downloadsnac2-1d694a245a56bb4fd90fd917ad3648c2c5449746.tar.gz
snac2-1d694a245a56bb4fd90fd917ad3648c2c5449746.tar.xz
snac2-1d694a245a56bb4fd90fd917ad3648c2c5449746.zip
xs_httpd_request() also returns the payload.
-rw-r--r--activitypub.c35
-rw-r--r--httpd.c7
-rw-r--r--snac.h3
-rw-r--r--xs_httpd.h21
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)
154 } 154 }
155 } 155 }
156} 156}
157
158
159int activitypub_post_handler(d_char *req, char *q_path,
160 d_char *payload, int p_size,
161 char **body, int *b_size, char **ctype)
162/* processes an input message */
163{
164 int status = 200;
165 char *i_ctype = xs_dict_get(req, "content-type");
166 snac snac;
167
168 if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
169 xs_str_in(i_ctype, "application/ld+json") == -1)
170 return 0;
171
172 xs *l = xs_split_n(q_path, "/", 2);
173 char *uid;
174
175 if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
176 /* strange q_path */
177 srv_log(xs_fmt("activitypub_post_handler unsupported path %s", q_path));
178 return 404;
179 }
180
181 uid = xs_list_get(l, 1);
182 if (!user_open(&snac, uid)) {
183 /* invalid user */
184 srv_log(xs_fmt("activitypub_post_handler bad user %s", uid));
185 return 404;
186 }
187
188 user_free(&snac);
189
190 return status;
191}
diff --git a/httpd.c b/httpd.c
index 1ef766c..037f690 100644
--- a/httpd.c
+++ b/httpd.c
@@ -98,11 +98,13 @@ void httpd_connection(int rs)
98 char *ctype = NULL; 98 char *ctype = NULL;
99 xs *headers = NULL; 99 xs *headers = NULL;
100 xs *q_path = NULL; 100 xs *q_path = NULL;
101 xs *payload = NULL;
102 int p_size;
101 char *p; 103 char *p;
102 104
103 f = xs_socket_accept(rs); 105 f = xs_socket_accept(rs);
104 106
105 req = xs_httpd_request(f); 107 req = xs_httpd_request(f, &payload, &p_size);
106 108
107 { 109 {
108 xs *j = xs_json_dumps_pp(req, 4); 110 xs *j = xs_json_dumps_pp(req, 4);
@@ -132,6 +134,9 @@ void httpd_connection(int rs)
132 } 134 }
133 else 135 else
134 if (strcmp(method, "POST") == 0) { 136 if (strcmp(method, "POST") == 0) {
137 if (status == 0)
138 status = activitypub_post_handler(req, q_path,
139 payload, p_size, &body, &b_size, &ctype);
135 } 140 }
136 141
137 /* let's go */ 142 /* let's go */
diff --git a/snac.h b/snac.h
index 166806b..f876363 100644
--- a/snac.h
+++ b/snac.h
@@ -85,3 +85,6 @@ int actor_request(snac *snac, char *actor, d_char **data);
85int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size); 85int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size);
86int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size); 86int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size);
87void process_queue(snac *snac); 87void process_queue(snac *snac);
88int activitypub_post_handler(d_char *req, char *q_path,
89 char *payload, int p_size,
90 char **body, int *b_size, char **ctype);
diff --git a/xs_httpd.h b/xs_httpd.h
index d15a473..d56226f 100644
--- a/xs_httpd.h
+++ b/xs_httpd.h
@@ -6,7 +6,7 @@
6 6
7d_char *xs_url_dec(char *str); 7d_char *xs_url_dec(char *str);
8d_char *xs_url_vars(char *str); 8d_char *xs_url_vars(char *str);
9d_char *xs_httpd_request(FILE *f); 9d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size);
10void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size); 10void xs_httpd_response(FILE *f, int status, d_char *headers, char *body, int b_size);
11 11
12 12
@@ -69,7 +69,7 @@ d_char *xs_url_vars(char *str)
69} 69}
70 70
71 71
72d_char *xs_httpd_request(FILE *f) 72d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size)
73/* processes an httpd connection */ 73/* processes an httpd connection */
74{ 74{
75 xs *headers = NULL; 75 xs *headers = NULL;
@@ -127,19 +127,18 @@ d_char *xs_httpd_request(FILE *f)
127 127
128 xs_socket_timeout(fileno(f), 5.0, 0.0); 128 xs_socket_timeout(fileno(f), 5.0, 0.0);
129 129
130 if ((v = xs_dict_get(headers, "content-length")) != NULL) {
131 /* if it has a payload, load it */
132 *p_size = atoi(v);
133 *payload = xs_read(f, *p_size);
134 }
135
130 /* does it have a payload with form urlencoded variables? */ 136 /* does it have a payload with form urlencoded variables? */
131 v = xs_dict_get(headers, "content-type"); 137 v = xs_dict_get(headers, "content-type");
132 138
133 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) { 139 if (v && strcmp(v, "application/x-www-form-urlencoded") == 0) {
134 if ((v = xs_dict_get(headers, "content-length")) != NULL) { 140 xs *upl = xs_url_dec(*payload);
135 int cl = atoi(v); 141 p_vars = xs_url_vars(upl);
136 xs *payload;
137
138 if ((payload = xs_read(f, cl)) != NULL) {
139 xs *upl = xs_url_dec(payload);
140 p_vars = xs_url_vars(upl);
141 }
142 }
143 } 142 }
144 else 143 else
145 p_vars = xs_dict_new(); 144 p_vars = xs_dict_new();