123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566 |
- #include "xs.h"
- #include "xs_io.h"
- #include "xs_encdec.h"
- #include "xs_json.h"
- #include "xs_socket.h"
- #include "xs_httpd.h"
- #include "xs_mime.h"
- #include "snac.h"
- #include <setjmp.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <sys/resource.h> // for getrlimit()
- #ifdef USE_POLL_FOR_SLEEP
- #include <poll.h>
- #endif
- const char *nodeinfo_2_0_template = ""
- "{\"version\":\"2.0\","
- "\"software\":{\"name\":\"snac\",\"version\":\"" VERSION "\"},"
- "\"protocols\":[\"activitypub\"],"
- "\"services\":{\"outbound\":[],\"inbound\":[]},"
- "\"usage\":{\"users\":{\"total\":%d,\"activeMonth\":%d,\"activeHalfyear\":%d},"
- "\"localPosts\":%d},"
- "\"openRegistrations\":false,\"metadata\":{}}";
- d_char *nodeinfo_2_0(void)
- {
- xs *users = user_list();
- int n_users = xs_list_len(users);
- int n_posts = 0;
- return xs_fmt(nodeinfo_2_0_template, n_users, n_users, n_users, n_posts);
- }
- int server_get_handler(xs_dict *req, char *q_path,
- char **body, int *b_size, char **ctype)
- {
- int status = 0;
- (void)req;
-
- if (*q_path == '\0') {
-
- xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
- FILE *f;
- if ((f = fopen(fn, "r")) != NULL) {
- d_char *s = xs_readall(f);
- fclose(f);
- status = 200;
-
- s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
- const char *adm_email = xs_dict_get(srv_config, "admin_email");
- if (xs_is_null(adm_email) || *adm_email == '\0')
- adm_email = "the administrator of this instance";
-
- s = xs_replace_i(s, "%admin_email%", adm_email);
-
- if (xs_str_in(s, "%userlist%") != -1) {
- char *host = xs_dict_get(srv_config, "host");
- xs *list = user_list();
- char *p, *uid;
- xs *ul = xs_str_new("<ul class=\"snac-user-list\">\n");
- p = list;
- while (xs_list_iter(&p, &uid)) {
- snac snac;
- if (user_open(&snac, uid)) {
- xs *u = xs_fmt(
- "<li><a href=\"%s\">@%s@%s (%s)</a></li>\n",
- snac.actor, uid, host,
- xs_dict_get(snac.config, "name"));
- ul = xs_str_cat(ul, u);
- user_free(&snac);
- }
- }
- ul = xs_str_cat(ul, "</ul>\n");
- s = xs_replace_i(s, "%userlist%", ul);
- }
- *body = s;
- }
- }
- else
- if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
- status = 200;
- *body = xs_base64_dec(default_avatar_base64(), b_size);
- *ctype = "image/png";
- }
- else
- if (strcmp(q_path, "/.well-known/nodeinfo") == 0) {
- status = 200;
- *ctype = "application/json; charset=utf-8";
- *body = xs_fmt("{\"links\":["
- "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\","
- "\"href\":\"%s/nodeinfo_2_0\"}]}",
- srv_baseurl);
- }
- else
- if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
- status = 200;
- *ctype = "application/json; charset=utf-8";
- *body = nodeinfo_2_0();
- }
- else
- if (strcmp(q_path, "/robots.txt") == 0) {
- status = 200;
- *ctype = "text/plain";
- *body = xs_str_new("User-agent: *\n"
- "Disallow: /\n");
- }
- if (status != 0)
- srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
- return status;
- }
- void httpd_connection(FILE *f)
- {
- xs *req;
- char *method;
- int status = 0;
- d_char *body = NULL;
- int b_size = 0;
- char *ctype = NULL;
- xs *headers = NULL;
- xs *q_path = NULL;
- xs *payload = NULL;
- int p_size = 0;
- char *p;
- req = xs_httpd_request(f, &payload, &p_size);
- if (req == NULL) {
-
- fclose(f);
- return;
- }
- method = xs_dict_get(req, "method");
- q_path = xs_dup(xs_dict_get(req, "path"));
-
- if (xs_endswith(q_path, "/"))
- q_path = xs_crop_i(q_path, 0, -1);
- p = xs_dict_get(srv_config, "prefix");
- if (xs_startswith(q_path, p))
- q_path = xs_crop_i(q_path, strlen(p), 0);
- if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
-
- if (status == 0)
- status = server_get_handler(req, q_path, &body, &b_size, &ctype);
- if (status == 0)
- status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
- if (status == 0)
- status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
- #ifndef NO_MASTODON_API
- if (status == 0)
- status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
- if (status == 0)
- status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
- #endif
- if (status == 0)
- status = html_get_handler(req, q_path, &body, &b_size, &ctype);
- }
- else
- if (strcmp(method, "POST") == 0) {
- #ifndef NO_MASTODON_API
- if (status == 0)
- status = oauth_post_handler(req, q_path,
- payload, p_size, &body, &b_size, &ctype);
- if (status == 0)
- status = mastoapi_post_handler(req, q_path,
- payload, p_size, &body, &b_size, &ctype);
- #endif
- if (status == 0)
- status = activitypub_post_handler(req, q_path,
- payload, p_size, &body, &b_size, &ctype);
- if (status == 0)
- status = html_post_handler(req, q_path,
- payload, p_size, &body, &b_size, &ctype);
- }
- else
- if (strcmp(method, "PUT") == 0) {
- #ifndef NO_MASTODON_API
- if (status == 0)
- status = mastoapi_put_handler(req, q_path,
- payload, p_size, &body, &b_size, &ctype);
- #endif
- }
-
- headers = xs_dict_new();
-
- if (status == 0) {
- srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
- status = 404;
- }
- if (status == 404)
- body = xs_str_new("<h1>404 Not Found</h1>");
- if (status == 400 && body != NULL)
- body = xs_str_new("<h1>400 Bad Request</h1>");
- if (status == 303)
- headers = xs_dict_append(headers, "location", body);
- if (status == 401)
- headers = xs_dict_append(headers, "WWW-Authenticate", "Basic realm=\"IDENTIFY\"");
- if (ctype == NULL)
- ctype = "text/html; charset=utf-8";
- headers = xs_dict_append(headers, "content-type", ctype);
- headers = xs_dict_append(headers, "x-creator", USER_AGENT);
- if (b_size == 0 && body != NULL)
- b_size = strlen(body);
-
- if (strcmp(method, "HEAD") == 0)
- body = xs_free(body);
- xs_httpd_response(f, status, headers, body, b_size);
- fclose(f);
- srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
-
- if (strcmp(ctype, "application/json") == 0) {
- xs *j = xs_json_loads(body);
- if (j == NULL) {
- srv_log(xs_fmt("bad JSON"));
- srv_archive_error("bad_json", "bad JSON", req, body);
- }
- }
- xs_free(body);
- }
- static jmp_buf on_break;
- void term_handler(int s)
- {
- (void)s;
- longjmp(on_break, 1);
- }
- static pthread_mutex_t job_mutex;
- static sem_t job_sem;
- xs_list *job_fifo = NULL;
- int job_fifo_ready(void)
- {
- return job_fifo != NULL;
- }
- void job_post(const xs_val *job, int urgent)
- {
- if (job != NULL) {
-
- pthread_mutex_lock(&job_mutex);
-
- if (job_fifo != NULL) {
- if (urgent)
- job_fifo = xs_list_insert(job_fifo, 0, job);
- else
- job_fifo = xs_list_append(job_fifo, job);
- }
-
- pthread_mutex_unlock(&job_mutex);
- }
-
- sem_post(&job_sem);
- }
- void job_wait(xs_val **job)
- {
- *job = NULL;
- if (sem_wait(&job_sem) == 0) {
-
- pthread_mutex_lock(&job_mutex);
-
- if (job_fifo != NULL)
- job_fifo = xs_list_shift(job_fifo, job);
-
- pthread_mutex_unlock(&job_mutex);
- }
- }
- #ifndef MAX_THREADS
- #define MAX_THREADS 256
- #endif
- static void *job_thread(void *arg)
- {
- int pid = (char *) arg - (char *) 0x0;
- srv_debug(1, xs_fmt("job thread %d started", pid));
- for (;;) {
- xs *job = NULL;
- job_wait(&job);
- srv_debug(2, xs_fmt("job thread %d wake up", pid));
- if (job == NULL)
- break;
- if (xs_type(job) == XSTYPE_DATA) {
-
- FILE *f = NULL;
- xs_data_get(job, &f);
- if (f != NULL)
- httpd_connection(f);
- }
- else {
-
- process_queue_item(job);
- }
- }
- srv_debug(1, xs_fmt("job thread %d stopped", pid));
- return NULL;
- }
- static pthread_mutex_t sleep_mutex;
- static pthread_cond_t sleep_cond;
- static void *background_thread(void *arg)
- {
- time_t purge_time;
- (void)arg;
-
- purge_time = time(NULL) + 10 * 60;
- srv_log(xs_fmt("background thread started"));
- while (srv_running) {
- time_t t;
- int cnt = 0;
- {
- xs *list = user_list();
- char *p, *uid;
-
- p = list;
- while (xs_list_iter(&p, &uid)) {
- snac snac;
- if (user_open(&snac, uid)) {
- cnt += process_user_queue(&snac);
- user_free(&snac);
- }
- }
- }
-
- cnt += process_queue();
-
- if ((t = time(NULL)) > purge_time) {
-
- purge_time = t + 24 * 60 * 60;
- xs *q_item = xs_dict_new();
- q_item = xs_dict_append(q_item, "type", "purge");
- job_post(q_item, 0);
- }
- if (cnt == 0) {
-
- #ifdef USE_POLL_FOR_SLEEP
- poll(NULL, 0, 3 * 1000);
- #else
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_sec += 3;
- pthread_mutex_lock(&sleep_mutex);
- while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
- pthread_mutex_unlock(&sleep_mutex);
- #endif
- }
- }
- srv_log(xs_fmt("background thread stopped"));
- return NULL;
- }
- void httpd(void)
- {
- char *address;
- int port;
- int rs;
- pthread_t threads[MAX_THREADS] = {0};
- int n_threads = 0;
- int n;
- address = xs_dict_get(srv_config, "address");
- port = xs_number_get(xs_dict_get(srv_config, "port"));
- if ((rs = xs_socket_server(address, port)) == -1) {
- srv_log(xs_fmt("cannot bind socket to %s:%d", address, port));
- return;
- }
- srv_running = 1;
- signal(SIGPIPE, SIG_IGN);
- signal(SIGTERM, term_handler);
- signal(SIGINT, term_handler);
- srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
-
- struct rlimit r;
- getrlimit(RLIMIT_NOFILE, &r);
- srv_debug(0, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
- (int) r.rlim_cur, (int) r.rlim_max));
-
- pthread_mutex_init(&job_mutex, NULL);
- sem_init(&job_sem, 0, 0);
- job_fifo = xs_list_new();
-
- pthread_mutex_init(&sleep_mutex, NULL);
- pthread_cond_init(&sleep_cond, NULL);
- n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
- #ifdef _SC_NPROCESSORS_ONLN
- if (n_threads == 0) {
-
- n_threads = sysconf(_SC_NPROCESSORS_ONLN);
- }
- #endif
- if (n_threads < 4)
- n_threads = 4;
- if (n_threads > MAX_THREADS)
- n_threads = MAX_THREADS;
- srv_debug(0, xs_fmt("using %d threads", n_threads));
-
- pthread_create(&threads[0], NULL, background_thread, NULL);
-
- char *ptr = (char *) 0x1;
- for (n = 1; n < n_threads; n++)
- pthread_create(&threads[n], NULL, job_thread, ptr++);
- if (setjmp(on_break) == 0) {
- for (;;) {
- FILE *f = xs_socket_accept(rs);
- if (f != NULL) {
- xs *job = xs_data_new(&f, sizeof(FILE *));
- job_post(job, 1);
- }
- else
- break;
- }
- }
- srv_running = 0;
-
- for (n = 1; n < n_threads; n++)
- job_post(NULL, 0);
-
- for (n = 0; n < n_threads; n++)
- pthread_join(threads[n], NULL);
- pthread_mutex_lock(&job_mutex);
- job_fifo = xs_free(job_fifo);
- pthread_mutex_unlock(&job_mutex);
- srv_log(xs_fmt("httpd stop %s:%d", address, port));
- }
|