main
httpd.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_io.h"
6#include "xs_json.h"
7#include "xs_socket.h"
8#include "xs_unix_socket.h"
9#include "xs_http.h"
10#include "xs_httpd.h"
11#include "xs_mime.h"
12#include "xs_time.h"
13#include "xs_bearssl.h"
14#include "xs_fcgi.h"
15#include "xs_html.h"
16#include "xs_webmention.h"
17
18#include "snac.h"
19
20#include <setjmp.h>
21#include <pthread.h>
22#include <semaphore.h>
23#include <fcntl.h>
24#include <stdint.h>
25
26#include <sys/resource.h> // for getrlimit()
27
28#include <sys/mman.h>
29
30#ifdef USE_POLL_FOR_SLEEP
31#include <poll.h>
32#endif
33
34/** server state **/
35srv_state *p_state = NULL;
36
37
38/** job control **/
39
40/* mutex to access the lists of jobs */
41static pthread_mutex_t job_mutex;
42
43/* semaphore to trigger job processing */
44static sem_t *job_sem;
45
46typedef struct job_fifo_item {
47 struct job_fifo_item *next;
48 xs_val *job;
49} job_fifo_item;
50
51static job_fifo_item *job_fifo_first = NULL;
52static job_fifo_item *job_fifo_last = NULL;
53
54
55/** other global data **/
56
57static jmp_buf on_break;
58
59
60/** code **/
61
62/* nodeinfo 2.0 template */
63const char * const nodeinfo_2_0_template = ""
64 "{\"version\":\"2.0\","
65 "\"software\":{\"name\":\"snac\",\"version\":\"" VERSION "\"},"
66 "\"protocols\":[\"activitypub\"],"
67 "\"services\":{\"outbound\":[],\"inbound\":[]},"
68 "\"usage\":{\"users\":{\"total\":%d,\"activeMonth\":%d,\"activeHalfyear\":%d},"
69 "\"localPosts\":%d},"
70 "\"openRegistrations\":false,\"metadata\":{"
71 "\"nodeDescription\":\"%s\",\"nodeName\":\"%s\","
72 "\"baseURL\":\"%s\""
73 "}}";
74
75xs_str *nodeinfo_2_0(void)
76/* builds a nodeinfo json object */
77{
78 int n_utotal = 0;
79 int n_umonth = 0;
80 int n_uhyear = 0;
81 int n_posts = 0;
82 xs *users = user_list();
83 xs_list *p = users;
84 const char *v;
85 double now = (double)time(NULL);
86
87 while (xs_list_iter(&p, &v)) {
88 /* build the full path name to the last usage log */
89 xs *llfn = xs_fmt("%s/user/%s/lastlog.txt", srv_basedir, v);
90 double llsecs = now - mtime(llfn);
91
92 if (llsecs < 60 * 60 * 24 * 30 * 6) {
93 n_uhyear++;
94
95 if (llsecs < 60 * 60 * 24 * 30)
96 n_umonth++;
97 }
98
99 n_utotal++;
100
101 /* build the file to each user public.idx */
102 xs *pidxfn = xs_fmt("%s/user/%s/public.idx", srv_basedir, v);
103 n_posts += index_len(pidxfn);
104 }
105
106 const char *name = xs_dict_get_def(srv_config, "title", "");
107 const char *desc = xs_dict_get_def(srv_config, "short_description", "");
108
109 return xs_fmt(nodeinfo_2_0_template, n_utotal, n_umonth, n_uhyear, n_posts, desc, name, srv_baseurl);
110}
111
112
113static xs_str *greeting_html(void)
114/* processes and returns greeting.html */
115{
116 /* try to open greeting.html */
117 xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
118 FILE *f;
119 xs_str *s = NULL;
120
121 if ((f = fopen(fn, "r")) != NULL) {
122 s = xs_readall(f);
123 fclose(f);
124
125 /* replace %host% */
126 s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
127
128 const char *adm_email = xs_dict_get(srv_config, "admin_email");
129 if (xs_is_null(adm_email) || *adm_email == '\0')
130 adm_email = "the administrator of this instance";
131
132 /* replace %admin_email */
133 s = xs_replace_i(s, "%admin_email%", adm_email);
134
135 /* does it have a %userlist% mark? */
136 if (xs_str_in(s, "%userlist%") != -1) {
137 const char *host = xs_dict_get(srv_config, "host");
138 xs *list = user_list();
139 xs_list *p = list;
140 const xs_str *uid;
141
142 xs_html *ul = xs_html_tag("ul",
143 xs_html_attr("class", "snac-user-list"));
144
145 p = list;
146 while (xs_list_iter(&p, &uid)) {
147 snac user;
148
149 if (strcmp(uid, "relay") && user_open(&user, uid)) {
150 xs *formatted_name = format_text_with_emoji(NULL, xs_dict_get(user.config, "name"), 1, NULL);
151
152 xs_html_add(ul,
153 xs_html_tag("li",
154 xs_html_tag("a",
155 xs_html_attr("href", user.actor),
156 xs_html_text("@"),
157 xs_html_text(uid),
158 xs_html_text("@"),
159 xs_html_text(host),
160 xs_html_text(" ("),
161 xs_html_raw(formatted_name),
162 xs_html_text(")"))));
163
164 user_free(&user);
165 }
166 }
167
168 xs *s1 = xs_html_render(ul);
169 s = xs_replace_i(s, "%userlist%", s1);
170 }
171 }
172
173 return s;
174}
175
176
177const char * const share_page = ""
178"<!DOCTYPE html>\n"
179"<html>\n"
180"<head>\n"
181"<title>%s - snac</title>\n"
182"<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">\n"
183"<link rel=\"stylesheet\" type=\"text/css\" href=\"%s/style.css\"/>\n"
184"<style>:root {color-scheme: light dark}</style>\n"
185"</head>\n"
186"<body><h1>%s link share</h1>\n"
187"<form method=\"get\" action=\"%s/share-bridge\">\n"
188"<textarea name=\"content\" rows=\"6\" wrap=\"virtual\" required=\"required\" style=\"width: 50em\">%s</textarea>\n"
189"<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\" required=\"required\"></p>\n"
190"<input type=\"submit\" value=\"OK\">\n"
191"</form><p>%s</p></body></html>\n"
192"";
193
194
195const char * const authorize_interaction_page = ""
196"<!DOCTYPE html>\n"
197"<html>\n"
198"<head>\n"
199"<title>%s - snac</title>\n"
200"<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">\n"
201"<link rel=\"stylesheet\" type=\"text/css\" href=\"%s/style.css\"/>\n"
202"<style>:root {color-scheme: light dark}</style>\n"
203"</head>\n"
204"<body><h1>%s authorize interaction</h1>\n"
205"<form method=\"get\" action=\"%s/auth-int-bridge\">\n"
206"<select name=\"action\">\n"
207"<option value=\"Follow\">Follow</option>\n"
208"<option value=\"Boost\">Boost</option>\n"
209"<option value=\"Like\">Like</option>\n"
210"</select> %s\n"
211"<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
212"<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\" required=\"required\"></p>\n"
213"<input type=\"submit\" value=\"OK\">\n"
214"</form><p>%s</p></body></html>\n"
215"";
216
217
218int server_get_handler(xs_dict *req, const char *q_path,
219 char **body, int *b_size, char **ctype)
220/* basic server services */
221{
222 int status = 0;
223
224 const snac *user = NULL;
225
226 /* is it the server root? */
227 if (*q_path == '\0' || strcmp(q_path, "/") == 0) {
228 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
229 const char *t = NULL;
230
231 if (xs_type(q_vars) == XSTYPE_DICT && xs_is_string(t = xs_dict_get(q_vars, "t"))) {
232 /** search by tag **/
233 int skip = 0;
234 int show = xs_number_get(xs_dict_get_def(srv_config, "def_timeline_entries",
235 xs_dict_get_def(srv_config, "max_timeline_entries", "50")));
236 const char *v;
237
238 if ((v = xs_dict_get(q_vars, "skip")) != NULL)
239 skip = atoi(v);
240 if ((v = xs_dict_get(q_vars, "show")) != NULL)
241 show = atoi(v);
242
243 xs *tl = tag_search(t, skip, show + 1);
244 int more = 0;
245 if (xs_list_len(tl) >= show + 1) {
246 /* drop the last one */
247 tl = xs_list_del(tl, -1);
248 more = 1;
249 }
250
251 const char *accept = xs_dict_get(req, "accept");
252 if (!xs_is_null(accept) && strcmp(accept, "application/rss+xml") == 0) {
253 xs *link = xs_fmt("%s/?t=%s", srv_baseurl, t);
254
255 *body = rss_from_timeline(NULL, tl, link, link, link);
256 *ctype = "application/rss+xml; charset=utf-8";
257 }
258 else {
259 xs *page = xs_fmt("?t=%s", t);
260 xs *title = xs_fmt(L("Search results for tag #%s"), t);
261 *body = html_timeline(NULL, tl, 0, skip, show, more, title, page, 0, NULL, 0, 0);
262 }
263 }
264 else
265 if (xs_type(xs_dict_get(srv_config, "show_instance_timeline")) == XSTYPE_TRUE) {
266 /** instance timeline **/
267 xs *tl = timeline_instance_list(0, 30);
268 *body = html_timeline(NULL, tl, 0, 0, 0, 0,
269 L("Recent posts by users in this instance"), NULL, 0, NULL, 0, 0);
270 }
271 else
272 *body = greeting_html();
273
274 if (*body)
275 status = HTTP_STATUS_OK;
276 }
277 else
278 if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
279 status = HTTP_STATUS_OK;
280 *body = xs_base64_dec(default_avatar_base64(), b_size);
281 *ctype = "image/png";
282 }
283 else
284 if (strcmp(q_path, "/.well-known/nodeinfo") == 0) {
285 status = HTTP_STATUS_OK;
286 *ctype = "application/json; charset=utf-8";
287 *body = xs_fmt("{\"links\":["
288 "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.1\",\"href\":\"%s/nodeinfo_2_1\"},"
289 "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\",\"href\":\"%s/nodeinfo_2_0\"}"
290 "]}",
291 srv_baseurl, srv_baseurl);
292 }
293 else
294 if (strcmp(q_path, "/.well-known/host-meta") == 0) {
295 status = HTTP_STATUS_OK;
296 *ctype = "application/xrd+xml";
297 *body = xs_fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
298 "<XRD>"
299 "<Link rel=\"lrdd\" type=\"application/xrd+xml\" template=\"https://%s/.well-known/webfinger?resource={uri}\"/>"
300 "</XRD>", xs_dict_get(srv_config, "host"));
301 }
302 else
303 if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
304 status = HTTP_STATUS_OK;
305 *ctype = "application/json; charset=utf-8";
306 *body = nodeinfo_2_0();
307 }
308 else
309 if (strcmp(q_path, "/nodeinfo_2_1") == 0) {
310 xs *s = nodeinfo_2_0();
311 xs *j = xs_json_loads(s);
312
313 j = xs_dict_set(j, "version", "2.1");
314 j = xs_dict_set_path(j, "software.repository", WHAT_IS_SNAC_URL);
315 j = xs_dict_set_path(j, "software.homepage", SNAC_DOC_URL);
316
317 status = HTTP_STATUS_OK;
318 *ctype = "application/json; charset=utf-8";
319 *body = xs_json_dumps(j, 4);
320 }
321 else
322 if (strcmp(q_path, "/robots.txt") == 0) {
323 status = HTTP_STATUS_OK;
324 *ctype = "text/plain";
325 *body = xs_str_new("User-agent: *\n"
326 "Disallow: /\n");
327 }
328 else
329 if (strcmp(q_path, "/style.css") == 0) {
330 FILE *f;
331 xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
332
333 if ((f = fopen(css_fn, "r")) != NULL) {
334 *body = xs_readall(f);
335 fclose(f);
336
337 status = HTTP_STATUS_OK;
338 *ctype = "text/css";
339 }
340 }
341 else
342 if (strcmp(q_path, "/share") == 0) {
343 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
344 const char *url = xs_dict_get(q_vars, "url");
345 const char *text = xs_dict_get(q_vars, "text");
346 xs *s = NULL;
347
348 if (xs_type(text) == XSTYPE_STRING) {
349 if (xs_type(url) == XSTYPE_STRING)
350 s = xs_fmt("%s:\n\n%s\n", text, url);
351 else
352 s = xs_fmt("%s\n", text);
353 }
354 else
355 if (xs_type(url) == XSTYPE_STRING)
356 s = xs_fmt("%s\n", url);
357 else
358 s = xs_str_new(NULL);
359
360 status = HTTP_STATUS_OK;
361 *ctype = "text/html; charset=utf-8";
362 *body = xs_fmt(share_page,
363 xs_dict_get(srv_config, "host"),
364 srv_baseurl,
365 xs_dict_get(srv_config, "host"),
366 srv_baseurl,
367 s,
368 USER_AGENT
369 );
370 }
371 else
372 if (strcmp(q_path, "/authorize_interaction") == 0) {
373 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
374 const char *uri = xs_dict_get(q_vars, "uri");
375
376 if (xs_is_string(uri)) {
377 status = HTTP_STATUS_OK;
378 *ctype = "text/html; charset=utf-8";
379 *body = xs_fmt(authorize_interaction_page,
380 xs_dict_get(srv_config, "host"),
381 srv_baseurl,
382 xs_dict_get(srv_config, "host"),
383 srv_baseurl,
384 uri,
385 uri,
386 USER_AGENT
387 );
388 }
389 }
390
391 if (status != 0)
392 srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
393
394 return status;
395}
396
397
398int server_post_handler(const xs_dict *req, const char *q_path,
399 char *payload, int p_size,
400 char **body, int *b_size, char **ctype)
401{
402 int status = 0;
403
404 (void)payload;
405 (void)p_size;
406 (void)body;
407 (void)b_size;
408 (void)ctype;
409
410 if (strcmp(q_path, "/webmention-hook") == 0) {
411 status = HTTP_STATUS_BAD_REQUEST;
412
413 const xs_dict *p_vars = xs_dict_get(req, "p_vars");
414
415 if (!xs_is_dict(p_vars))
416 return status;
417
418 const char *source = xs_dict_get(p_vars, "source");
419 const char *target = xs_dict_get(p_vars, "target");
420
421 if (!xs_is_string(source) || !xs_is_string(target)) {
422 srv_debug(1, xs_fmt("webmention-hook bad source or target"));
423 return status;
424 }
425
426 if (!xs_startswith(target, srv_baseurl)) {
427 srv_debug(1, xs_fmt("webmention-hook unknown target %s", target));
428 return status;
429 }
430
431 /* get the user */
432 xs *s1 = xs_replace(target, srv_baseurl, "");
433
434 xs *l1 = xs_split(s1, "/");
435 const char *uid = xs_list_get(l1, 1);
436 snac user;
437
438 if (!xs_is_string(uid) || !user_open(&user, uid)) {
439 srv_debug(1, xs_fmt("webmention-hook cannot find user for %s", target));
440 return status;
441 }
442
443 int r = xs_webmention_hook(source, target, USER_AGENT);
444
445 if (r > 0) {
446 notify_add(&user, "Webmention", NULL, source, target, xs_stock(XSTYPE_DICT));
447 timeline_touch(&user);
448 }
449
450 srv_log(xs_fmt("webmention-hook source=%s target=%s %d", source, target, r));
451
452 user_free(&user);
453 status = HTTP_STATUS_OK;
454 }
455
456 return status;
457}
458
459
460void httpd_connection(FILE *f)
461/* the connection processor */
462{
463 xs *req;
464 const char *method;
465 int status = 0;
466 xs_str *body = NULL;
467 int b_size = 0;
468 char *ctype = NULL;
469 xs *headers = xs_dict_new();
470 xs *q_path = NULL;
471 xs *payload = NULL;
472 xs *etag = NULL;
473 xs *last_modified = NULL;
474 xs *link = NULL;
475 int p_size = 0;
476 const char *p;
477 int fcgi_id;
478
479 if (p_state->use_fcgi)
480 req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id);
481 else
482 req = xs_httpd_request(f, &payload, &p_size);
483
484 fflush(f);
485
486 if (req == NULL) {
487 /* probably because a timeout */
488 fclose(f);
489 return;
490 }
491
492 if (!(method = xs_dict_get(req, "method")) || !(p = xs_dict_get(req, "path"))) {
493 /* missing needed headers; discard */
494 fclose(f);
495 return;
496 }
497
498 q_path = xs_dup(p);
499
500 /* crop the q_path from leading / and the prefix */
501 if (xs_endswith(q_path, "/"))
502 q_path = xs_crop_i(q_path, 0, -1);
503
504 p = xs_dict_get(srv_config, "prefix");
505 if (xs_startswith(q_path, p))
506 q_path = xs_crop_i(q_path, strlen(p), 0);
507
508 /* add users endpoint redirection mimic Mastodon behaviour */
509 const char *users_endpoint = "/users/";
510 if (xs_startswith(q_path, users_endpoint)) {
511 q_path = xs_crop_i(q_path, strlen(users_endpoint) - 1, 0);
512 status = HTTP_STATUS_FOUND;
513 }
514
515 if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
516 /* cascade through */
517 if (status == 0)
518 status = server_get_handler(req, q_path, &body, &b_size, &ctype);
519
520 if (status == 0)
521 status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
522
523 if (status == 0)
524 status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
525
526#ifndef NO_MASTODON_API
527 if (status == 0)
528 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
529
530 if (status == 0)
531 status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype, &link);
532#endif /* NO_MASTODON_API */
533
534 if (status == 0)
535 status = html_get_handler(req, q_path, &body, &b_size, &ctype, &etag, &last_modified);
536 }
537 else
538 if (strcmp(method, "POST") == 0) {
539
540 if (status == 0)
541 status = server_post_handler(req, q_path,
542 payload, p_size, &body, &b_size, &ctype);
543
544#ifndef NO_MASTODON_API
545 if (status == 0)
546 status = oauth_post_handler(req, q_path,
547 payload, p_size, &body, &b_size, &ctype);
548
549 if (status == 0)
550 status = mastoapi_post_handler(req, q_path,
551 payload, p_size, &body, &b_size, &ctype);
552#endif
553
554 if (status == 0)
555 status = activitypub_post_handler(req, q_path,
556 payload, p_size, &body, &b_size, &ctype);
557
558 if (status == 0)
559 status = html_post_handler(req, q_path,
560 payload, p_size, &body, &b_size, &ctype);
561 }
562 else
563 if (strcmp(method, "PUT") == 0) {
564
565#ifndef NO_MASTODON_API
566 if (status == 0)
567 status = mastoapi_put_handler(req, q_path,
568 payload, p_size, &body, &b_size, &ctype);
569#endif
570
571 }
572 else
573 if (strcmp(method, "PATCH") == 0) {
574
575#ifndef NO_MASTODON_API
576 if (status == 0)
577 status = mastoapi_patch_handler(req, q_path,
578 payload, p_size, &body, &b_size, &ctype);
579#endif
580
581 }
582 else
583 if (strcmp(method, "OPTIONS") == 0) {
584 const char *methods = "OPTIONS, GET, HEAD, POST, PUT, DELETE";
585 headers = xs_dict_append(headers, "allow", methods);
586 headers = xs_dict_append(headers, "access-control-allow-methods", methods);
587 status = HTTP_STATUS_OK;
588 }
589 else
590 if (strcmp(method, "DELETE") == 0) {
591#ifndef NO_MASTODON_API
592 if (status == 0)
593 status = mastoapi_delete_handler(req, q_path,
594 payload, p_size, &body, &b_size, &ctype);
595#endif
596 }
597
598 /* unattended? it's an error */
599 if (status == 0) {
600 srv_archive_error("unattended_method", "unattended method", req, payload);
601 srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
602 status = HTTP_STATUS_NOT_FOUND;
603 }
604
605 if (body == NULL) {
606 if (status == HTTP_STATUS_FORBIDDEN)
607 body = xs_str_new("<h1>403 Forbidden (" USER_AGENT ")</h1>");
608
609 if (status == HTTP_STATUS_NOT_FOUND)
610 body = xs_str_new("<h1>404 Not Found (" USER_AGENT ")</h1>");
611
612 if (status == HTTP_STATUS_GONE)
613 body = xs_str_new("<h1>410 Gone (" USER_AGENT ")</h1>");
614
615 if (status == HTTP_STATUS_BAD_REQUEST)
616 body = xs_str_new("<h1>400 Bad Request (" USER_AGENT ")</h1>");
617 }
618
619 if (status == HTTP_STATUS_SEE_OTHER)
620 headers = xs_dict_append(headers, "location", body);
621
622 if (status == HTTP_STATUS_FOUND)
623 headers = xs_dict_append(headers, "location", xs_fmt("%s%s", p, q_path));
624
625 if (status == HTTP_STATUS_UNAUTHORIZED && body) {
626 xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"",
627 body, xs_dict_get(srv_config, "host"));
628
629 headers = xs_dict_append(headers, "WWW-Authenticate", www_auth);
630 headers = xs_dict_append(headers, "Cache-Control", "no-cache, must-revalidate, max-age=0");
631 }
632
633 if (ctype == NULL)
634 ctype = "text/html; charset=utf-8";
635
636 headers = xs_dict_append(headers, "content-type", ctype);
637 headers = xs_dict_append(headers, "x-creator", USER_AGENT);
638
639 if (!xs_is_null(etag))
640 headers = xs_dict_append(headers, "etag", etag);
641 if (!xs_is_null(last_modified))
642 headers = xs_dict_append(headers, "last-modified", last_modified);
643 if (!xs_is_null(link))
644 headers = xs_dict_append(headers, "Link", link);
645
646 /* if there are any additional headers, add them */
647 const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");
648 if (xs_type(more_headers) == XSTYPE_DICT) {
649 const char *k, *v;
650 int c = 0;
651 while (xs_dict_next(more_headers, &k, &v, &c))
652 headers = xs_dict_set(headers, k, v);
653 }
654
655 if (b_size == 0 && body != NULL)
656 b_size = strlen(body);
657
658 /* if it was a HEAD, no body will be sent */
659 if (strcmp(method, "HEAD") == 0)
660 body = xs_free(body);
661
662 headers = xs_dict_append(headers, "access-control-allow-origin", "*");
663 headers = xs_dict_append(headers, "access-control-allow-headers", "*");
664 headers = xs_dict_append(headers, "access-control-expose-headers", "Link");
665
666 /* disable any form of fucking JavaScript */
667 headers = xs_dict_append(headers, "Content-Security-Policy", "script-src ;");
668
669 if (p_state->use_fcgi)
670 xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
671 else
672 xs_httpd_response(f, status, xs_http_status_text(status), headers, body, b_size);
673
674 fclose(f);
675
676 srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
677
678 /* JSON validation check */
679 if (!xs_is_null(body) && strcmp(ctype, "application/json") == 0) {
680 xs *j = xs_json_loads(body);
681
682 if (j == NULL) {
683 srv_log(xs_fmt("bad JSON"));
684 srv_archive_error("bad_json", "bad JSON", req, body);
685 }
686 }
687
688 xs_free(body);
689}
690
691
692void job_post(const xs_val *job, int urgent)
693/* posts a job for the threads to process it */
694{
695 if (job != NULL) {
696 /* lock the mutex */
697 pthread_mutex_lock(&job_mutex);
698
699 job_fifo_item *i = xs_realloc(NULL, sizeof(job_fifo_item));
700 *i = (job_fifo_item){ NULL, xs_dup(job) };
701
702 if (job_fifo_first == NULL)
703 job_fifo_first = job_fifo_last = i;
704 else
705 if (urgent) {
706 /* prepend */
707 i->next = job_fifo_first;
708 job_fifo_first = i;
709 }
710 else {
711 /* append */
712 job_fifo_last->next = i;
713 job_fifo_last = i;
714 }
715
716 p_state->job_fifo_size++;
717
718 if (p_state->job_fifo_size > p_state->peak_job_fifo_size)
719 p_state->peak_job_fifo_size = p_state->job_fifo_size;
720
721 /* unlock the mutex */
722 pthread_mutex_unlock(&job_mutex);
723
724 /* ask for someone to attend it */
725 sem_post(job_sem);
726 }
727}
728
729
730void job_wait(xs_val **job)
731/* waits for an available job */
732{
733 *job = NULL;
734
735 if (sem_wait(job_sem) == 0) {
736 /* lock the mutex */
737 pthread_mutex_lock(&job_mutex);
738
739 /* dequeue */
740 job_fifo_item *i = job_fifo_first;
741
742 if (i != NULL) {
743 job_fifo_first = i->next;
744
745 if (job_fifo_first == NULL)
746 job_fifo_last = NULL;
747
748 *job = i->job;
749 xs_free(i);
750
751 p_state->job_fifo_size--;
752 }
753
754 /* unlock the mutex */
755 pthread_mutex_unlock(&job_mutex);
756 }
757}
758
759
760static void *job_thread(void *arg)
761/* job thread */
762{
763 int pid = (int)(uintptr_t)arg;
764
765 srv_debug(1, xs_fmt("job thread %d started", pid));
766
767 for (;;) {
768 xs *job = NULL;
769
770 p_state->th_state[pid] = THST_WAIT;
771
772 job_wait(&job);
773
774 if (job == NULL) /* corrupted message? */
775 continue;
776
777 if (xs_type(job) == XSTYPE_FALSE) /* special message: exit */
778 break;
779 else
780 if (xs_type(job) == XSTYPE_DATA) {
781 /* it's a socket */
782 FILE *f = NULL;
783
784 p_state->th_state[pid] = THST_IN;
785
786 xs_data_get(&f, job);
787
788 if (f != NULL)
789 httpd_connection(f);
790 }
791 else {
792 /* it's a q_item */
793 p_state->th_state[pid] = THST_QUEUE;
794
795 process_queue_item(job);
796 }
797 }
798
799 p_state->th_state[pid] = THST_STOP;
800
801 srv_debug(1, xs_fmt("job thread %d stopped", pid));
802
803 return NULL;
804}
805
806/* background thread sleep control */
807static pthread_mutex_t sleep_mutex;
808static pthread_cond_t sleep_cond;
809
810static void *background_thread(void *arg)
811/* background thread (queue management and other things) */
812{
813 time_t t, purge_time, rss_time;
814
815 (void)arg;
816
817 t = time(NULL);
818
819 /* first purge time */
820 purge_time = t + 10 * 60;
821
822 /* first RSS polling time */
823 rss_time = t + 15 * 60;
824
825 srv_log(xs_fmt("background thread started"));
826
827 enqueue_fsck();
828
829 while (p_state->srv_running) {
830 int cnt = 0;
831
832 p_state->th_state[0] = THST_QUEUE;
833
834 {
835 xs *list = user_list();
836 const char *uid;
837
838 /* process queues for all users */
839 xs_list_foreach(list, uid) {
840 snac user;
841
842 if (user_open(&user, uid)) {
843 cnt += process_user_queue(&user);
844 user_free(&user);
845 }
846 }
847 }
848
849 /* global queue */
850 cnt += process_queue();
851
852 t = time(NULL);
853
854 /* time to purge? */
855 if (t > purge_time) {
856 /* next purge time is tomorrow */
857 purge_time = t + 24 * 60 * 60;
858
859 xs *q_item = xs_dict_new();
860 q_item = xs_dict_append(q_item, "type", "purge");
861 job_post(q_item, 0);
862 }
863
864 /* time to poll the RSS? */
865 if (t > rss_time) {
866 /* next RSS poll time */
867 int hours = xs_number_get(xs_dict_get_def(srv_config, "rss_hashtag_poll_hours", "4"));
868
869 /* don't hammer servers too much */
870 if (hours < 1)
871 hours = 1;
872
873 rss_time = t + 60 * 60 * hours;
874
875 xs *q_item = xs_dict_new();
876 q_item = xs_dict_append(q_item, "type", "rss_hashtag_poll");
877 job_post(q_item, 0);
878 }
879
880 if (cnt == 0) {
881 /* sleep 3 seconds */
882
883 p_state->th_state[0] = THST_WAIT;
884
885#ifdef USE_POLL_FOR_SLEEP
886 poll(NULL, 0, 3 * 1000);
887#else
888 struct timespec ts;
889
890 clock_gettime(CLOCK_REALTIME, &ts);
891 ts.tv_sec += 3;
892
893 pthread_mutex_lock(&sleep_mutex);
894 while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
895 pthread_mutex_unlock(&sleep_mutex);
896#endif
897 }
898 }
899
900 p_state->th_state[0] = THST_STOP;
901
902 srv_log(xs_fmt("background thread stopped"));
903
904 return NULL;
905}
906
907
908void term_handler(int s)
909{
910 (void)s;
911
912 longjmp(on_break, 1);
913}
914
915
916srv_state *srv_state_op(xs_str **fname, int op)
917/* opens or deletes the shared memory object */
918{
919 int fd;
920 srv_state *ss = NULL;
921
922 if (*fname == NULL)
923 *fname = xs_fmt("/%s_snac_state", xs_dict_get(srv_config, "host"));
924
925 switch (op) {
926 case 0: /* open for writing */
927
928#ifdef WITHOUT_SHM
929
930 errno = ENOTSUP;
931
932#else
933
934 if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) {
935 ftruncate(fd, sizeof(*ss));
936
937 if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE,
938 MAP_SHARED, fd, 0)) == MAP_FAILED)
939 ss = NULL;
940
941 close(fd);
942 }
943
944#endif
945
946 if (ss == NULL) {
947 /* shared memory error: just create a plain structure */
948 srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno)));
949 ss = malloc(sizeof(*ss));
950 }
951
952 /* init structure */
953 *ss = (srv_state){0};
954 ss->s_size = sizeof(*ss);
955
956 break;
957
958 case 1: /* open for reading */
959
960#ifdef WITHOUT_SHM
961
962 errno = ENOTSUP;
963
964#else
965
966 if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) {
967 if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
968 ss = NULL;
969
970 close(fd);
971 }
972
973#endif
974
975 if (ss == NULL) {
976 /* shared memory error */
977 srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno)));
978 }
979 else
980 if (ss->s_size != sizeof(*ss)) {
981 srv_log(xs_fmt("error: struct size mismatch (%d != %d)",
982 ss->s_size, sizeof(*ss)));
983
984 munmap(ss, sizeof(*ss));
985
986 ss = NULL;
987 }
988
989 break;
990
991 case 2: /* unlink */
992
993#ifndef WITHOUT_SHM
994
995 if (*fname)
996 shm_unlink(*fname);
997
998#endif
999
1000 break;
1001 }
1002
1003 return ss;
1004}
1005
1006
1007void httpd(void)
1008/* starts the server */
1009{
1010 const char *address = NULL;
1011 const char *port = NULL;
1012 xs *full_address = NULL;
1013 int rs;
1014 pthread_t threads[MAX_THREADS] = {0};
1015 int n;
1016 xs *sem_name = NULL;
1017 xs *shm_name = NULL;
1018 sem_t anon_job_sem;
1019 xs *pidfile = xs_fmt("%s/server.pid", srv_basedir);
1020 int pidfd;
1021
1022 {
1023 /* do some pidfile locking acrobatics */
1024 if ((pidfd = open(pidfile, O_RDWR | O_CREAT, 0660)) == -1) {
1025 srv_log(xs_fmt("Cannot create pidfile %s -- cannot continue", pidfile));
1026 return;
1027 }
1028
1029 if (lockf(pidfd, F_TLOCK, 1) == -1) {
1030 srv_log(xs_fmt("Cannot lock pidfile %s -- server already running?", pidfile));
1031 close(pidfd);
1032 return;
1033 }
1034
1035 ftruncate(pidfd, 0);
1036
1037 xs *s = xs_fmt("%d\n", (int)getpid());
1038 write(pidfd, s, strlen(s));
1039 }
1040
1041 address = xs_dict_get(srv_config, "address");
1042
1043 if (*address == '/') {
1044 rs = xs_unix_socket_server(address, NULL);
1045 full_address = xs_fmt("unix:%s", address);
1046 }
1047 else {
1048 port = xs_number_str(xs_dict_get(srv_config, "port"));
1049 full_address = xs_fmt("%s:%s", address, port);
1050
1051 rs = xs_socket_server(address, port);
1052 }
1053
1054 if (rs == -1) {
1055 srv_log(xs_fmt("cannot bind socket to %s", full_address));
1056 return;
1057 }
1058
1059 /* setup the server stat structure */
1060 p_state = srv_state_op(&shm_name, 0);
1061
1062 p_state->srv_start_time = time(NULL);
1063
1064 p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
1065
1066 p_state->srv_running = 1;
1067
1068 signal(SIGPIPE, SIG_IGN);
1069 signal(SIGTERM, term_handler);
1070 signal(SIGINT, term_handler);
1071
1072 srv_log(xs_fmt(USER_AGENT " httpd%s start %s", p_state->use_fcgi ? " (FastCGI)" : "",
1073 full_address));
1074
1075 /* show the number of usable file descriptors */
1076 struct rlimit r;
1077 getrlimit(RLIMIT_NOFILE, &r);
1078 srv_debug(1, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
1079 (int) r.rlim_cur, (int) r.rlim_max));
1080
1081 /* initialize the job control engine */
1082 pthread_mutex_init(&job_mutex, NULL);
1083 sem_name = xs_fmt("/job_%d", getpid());
1084 job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
1085
1086 if (job_sem == NULL) {
1087 /* error opening a named semaphore; try with an anonymous one */
1088 if (sem_init(&anon_job_sem, 0, 0) != -1)
1089 job_sem = &anon_job_sem;
1090 }
1091
1092 if (job_sem == NULL) {
1093 srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
1094 return;
1095 }
1096
1097 /* initialize sleep control */
1098 pthread_mutex_init(&sleep_mutex, NULL);
1099 pthread_cond_init(&sleep_cond, NULL);
1100
1101 p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
1102
1103#ifdef _SC_NPROCESSORS_ONLN
1104 if (p_state->n_threads == 0) {
1105 /* get number of CPUs on the machine */
1106 p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
1107 }
1108#endif
1109
1110 if (p_state->n_threads < 4)
1111 p_state->n_threads = 4;
1112
1113 if (p_state->n_threads > MAX_THREADS)
1114 p_state->n_threads = MAX_THREADS;
1115
1116 srv_debug(0, xs_fmt("using %d threads", p_state->n_threads));
1117
1118 /* thread #0 is the background thread */
1119 pthread_create(&threads[0], NULL, background_thread, NULL);
1120
1121 /* the rest of threads are for job processing */
1122 char *ptr = (char *) 0x1;
1123 for (n = 1; n < p_state->n_threads; n++)
1124 pthread_create(&threads[n], NULL, job_thread, ptr++);
1125
1126 if (setjmp(on_break) == 0) {
1127 for (;;) {
1128 int cs = xs_socket_accept(rs);
1129
1130 if (cs != -1) {
1131 FILE *f = fdopen(cs, "r+");
1132 xs *job = xs_data_new(&f, sizeof(FILE *));
1133 job_post(job, 1);
1134 } else {
1135 srv_log(xs_fmt("error: xs_socket_accept failed: %s", strerror(errno)));
1136 break;
1137 }
1138 }
1139 }
1140
1141 p_state->srv_running = 0;
1142
1143 /* send as many exit jobs as working threads */
1144 for (n = 1; n < p_state->n_threads; n++)
1145 job_post(xs_stock(XSTYPE_FALSE), 0);
1146
1147 /* wait for all the threads to exit */
1148 for (n = 0; n < p_state->n_threads; n++)
1149 pthread_join(threads[n], NULL);
1150
1151 sem_close(job_sem);
1152 sem_unlink(sem_name);
1153
1154 srv_state_op(&shm_name, 2);
1155
1156 xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
1157
1158 srv_log(xs_fmt("httpd%s stop %s (run time: %s)",
1159 p_state->use_fcgi ? " (FastCGI)" : "",
1160 full_address, uptime));
1161
1162 unlink(pidfile);
1163}