2022-09-21 19:09:16 +03:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2023-01-17 11:50:16 +03:00
|
|
|
/* copyright (c) 2022 - 2023 grunfink / MIT license */
|
2022-09-21 19:09:16 +03:00
|
|
|
|
|
|
|
#include "xs.h"
|
2022-09-21 20:28:30 +03:00
|
|
|
#include "xs_io.h"
|
2022-09-21 19:09:16 +03:00
|
|
|
#include "xs_encdec.h"
|
|
|
|
#include "xs_json.h"
|
|
|
|
#include "xs_socket.h"
|
|
|
|
#include "xs_httpd.h"
|
2022-10-16 20:00:17 +03:00
|
|
|
#include "xs_mime.h"
|
2022-09-21 19:09:16 +03:00
|
|
|
|
|
|
|
#include "snac.h"
|
|
|
|
|
2022-09-29 13:50:50 +03:00
|
|
|
#include <setjmp.h>
|
2022-10-01 21:57:06 +03:00
|
|
|
#include <pthread.h>
|
2023-02-06 13:29:46 +03:00
|
|
|
#include <semaphore.h>
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2022-12-08 11:58:47 +03:00
|
|
|
/* nodeinfo 2.0 template */
|
|
|
|
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)
|
|
|
|
/* builds a nodeinfo json object */
|
|
|
|
{
|
|
|
|
xs *users = user_list();
|
|
|
|
int n_users = xs_list_len(users);
|
|
|
|
int n_posts = 0; /* to be implemented someday */
|
|
|
|
|
|
|
|
return xs_fmt(nodeinfo_2_0_template, n_users, n_users, n_users, n_posts);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-23 18:44:02 +03:00
|
|
|
int server_get_handler(d_char *req, char *q_path,
|
|
|
|
char **body, int *b_size, char **ctype)
|
2022-09-21 20:28:30 +03:00
|
|
|
/* basic server services */
|
|
|
|
{
|
2022-09-23 18:44:02 +03:00
|
|
|
int status = 0;
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
/* is it the server root? */
|
2022-09-21 20:46:02 +03:00
|
|
|
if (*q_path == '\0') {
|
2022-09-21 20:28:30 +03:00
|
|
|
/* try to open greeting.html */
|
|
|
|
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);
|
|
|
|
|
2022-09-23 18:44:02 +03:00
|
|
|
status = 200;
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2022-10-16 10:59:36 +03:00
|
|
|
/* replace %host% */
|
|
|
|
s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
|
|
|
|
|
2022-09-21 20:28:30 +03:00
|
|
|
/* does it have a %userlist% mark? */
|
|
|
|
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");
|
|
|
|
|
2022-09-27 11:20:33 +03:00
|
|
|
s = xs_replace_i(s, "%userlist%", ul);
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
*body = s;
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 18:55:59 +03:00
|
|
|
else
|
2022-12-04 22:16:40 +03:00
|
|
|
if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
|
2022-09-23 18:44:02 +03:00
|
|
|
status = 200;
|
2023-01-27 20:17:11 +03:00
|
|
|
*body = xs_base64_dec(default_avatar_base64(), b_size);
|
2022-09-23 18:44:02 +03:00
|
|
|
*ctype = "image/png";
|
2022-09-22 18:55:59 +03:00
|
|
|
}
|
2022-12-08 11:58:47 +03:00
|
|
|
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();
|
|
|
|
}
|
2022-12-13 00:11:07 +03:00
|
|
|
else
|
|
|
|
if (strcmp(q_path, "/robots.txt") == 0) {
|
|
|
|
status = 200;
|
|
|
|
*ctype = "text/plain";
|
2022-12-13 17:40:59 +03:00
|
|
|
*body = xs_str_new("User-agent: *\n"
|
|
|
|
"Disallow: /\n");
|
2022-12-13 00:11:07 +03:00
|
|
|
}
|
2022-09-23 18:44:02 +03:00
|
|
|
|
2022-09-27 11:51:50 +03:00
|
|
|
if (status != 0)
|
2022-09-27 20:00:24 +03:00
|
|
|
srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
|
2022-09-27 11:51:50 +03:00
|
|
|
|
2022-09-23 18:44:02 +03:00
|
|
|
return status;
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 18:44:02 +03:00
|
|
|
|
2022-10-10 20:33:39 +03:00
|
|
|
void httpd_connection(FILE *f)
|
|
|
|
/* the connection processor */
|
2022-09-21 19:09:16 +03:00
|
|
|
{
|
|
|
|
xs *req;
|
2022-09-21 20:28:30 +03:00
|
|
|
char *method;
|
2022-10-24 21:06:02 +03:00
|
|
|
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;
|
2022-09-21 20:46:02 +03:00
|
|
|
char *p;
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2022-09-23 21:28:23 +03:00
|
|
|
req = xs_httpd_request(f, &payload, &p_size);
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2022-09-28 17:27:53 +03:00
|
|
|
if (req == NULL) {
|
|
|
|
/* probably because a timeout */
|
|
|
|
fclose(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-25 08:42:57 +03:00
|
|
|
method = xs_dict_get(req, "method");
|
|
|
|
q_path = xs_dup(xs_dict_get(req, "path"));
|
2022-09-21 20:46:02 +03:00
|
|
|
|
|
|
|
/* crop the q_path from leading / and the prefix */
|
|
|
|
if (xs_endswith(q_path, "/"))
|
2023-01-12 11:28:02 +03:00
|
|
|
q_path = xs_crop_i(q_path, 0, -1);
|
2022-09-21 20:46:02 +03:00
|
|
|
|
|
|
|
p = xs_dict_get(srv_config, "prefix");
|
|
|
|
if (xs_startswith(q_path, p))
|
2023-01-12 11:28:02 +03:00
|
|
|
q_path = xs_crop_i(q_path, strlen(p), 0);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2022-10-16 20:00:17 +03:00
|
|
|
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
|
2022-09-21 20:28:30 +03:00
|
|
|
/* cascade through */
|
|
|
|
if (status == 0)
|
2022-09-23 18:44:02 +03:00
|
|
|
status = server_get_handler(req, q_path, &body, &b_size, &ctype);
|
2022-09-21 22:12:49 +03:00
|
|
|
|
|
|
|
if (status == 0)
|
2022-09-23 18:40:59 +03:00
|
|
|
status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
|
2022-09-23 21:59:19 +03:00
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
|
2022-09-28 06:22:08 +03:00
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
status = html_get_handler(req, q_path, &body, &b_size, &ctype);
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(method, "POST") == 0) {
|
2022-09-23 21:28:23 +03:00
|
|
|
if (status == 0)
|
|
|
|
status = activitypub_post_handler(req, q_path,
|
|
|
|
payload, p_size, &body, &b_size, &ctype);
|
2022-09-28 06:22:08 +03:00
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
status = html_post_handler(req, q_path,
|
|
|
|
payload, p_size, &body, &b_size, &ctype);
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* let's go */
|
|
|
|
headers = xs_dict_new();
|
|
|
|
|
|
|
|
/* unattended? it's an error */
|
2022-09-28 06:22:08 +03:00
|
|
|
if (status == 0) {
|
|
|
|
srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
|
2022-09-21 20:28:30 +03:00
|
|
|
status = 404;
|
2022-09-28 06:22:08 +03:00
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
if (status == 404)
|
2022-09-21 20:46:02 +03:00
|
|
|
body = xs_str_new("<h1>404 Not Found</h1>");
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
if (status == 400)
|
2022-09-21 20:46:02 +03:00
|
|
|
body = xs_str_new("<h1>400 Bad Request</h1>");
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
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);
|
2022-09-27 19:01:51 +03:00
|
|
|
headers = xs_dict_append(headers, "x-creator", USER_AGENT);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
if (b_size == 0 && body != NULL)
|
|
|
|
b_size = strlen(body);
|
|
|
|
|
2022-10-16 20:00:17 +03:00
|
|
|
/* if it was a HEAD, no body will be sent */
|
2022-10-25 14:59:15 +03:00
|
|
|
if (strcmp(method, "HEAD") == 0)
|
|
|
|
body = xs_free(body);
|
2022-10-16 20:00:17 +03:00
|
|
|
|
2022-09-21 20:28:30 +03:00
|
|
|
xs_httpd_response(f, status, headers, body, b_size);
|
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
fclose(f);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2022-09-25 08:28:42 +03:00
|
|
|
srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
|
2022-09-24 13:22:17 +03:00
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
xs_free(body);
|
2022-09-21 19:09:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-29 13:50:50 +03:00
|
|
|
static jmp_buf on_break;
|
|
|
|
|
|
|
|
|
|
|
|
void term_handler(int s)
|
|
|
|
{
|
|
|
|
longjmp(on_break, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-17 12:18:09 +03:00
|
|
|
static void *purge_thread(void *arg)
|
|
|
|
/* spawned purge */
|
|
|
|
{
|
|
|
|
srv_log(xs_dup("purge start"));
|
|
|
|
|
|
|
|
purge_all();
|
|
|
|
|
|
|
|
srv_log(xs_dup("purge end"));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 10:44:00 +03:00
|
|
|
static void *background_thread(void *arg)
|
|
|
|
/* background thread (queue management and other things) */
|
2022-10-01 21:57:06 +03:00
|
|
|
{
|
2022-10-17 12:18:09 +03:00
|
|
|
time_t purge_time;
|
|
|
|
|
|
|
|
/* first purge time */
|
2022-11-26 07:05:57 +03:00
|
|
|
purge_time = time(NULL) + 10 * 60;
|
2022-10-13 16:56:19 +03:00
|
|
|
|
2023-02-06 10:44:00 +03:00
|
|
|
srv_log(xs_fmt("background thread started"));
|
2022-10-01 21:57:06 +03:00
|
|
|
|
|
|
|
while (srv_running) {
|
2022-10-17 12:18:09 +03:00
|
|
|
time_t t;
|
2022-10-01 21:57:06 +03:00
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
{
|
|
|
|
xs *list = user_list();
|
|
|
|
char *p, *uid;
|
|
|
|
|
|
|
|
/* process queues for all users */
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
|
|
|
snac snac;
|
2022-10-01 21:57:06 +03:00
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
if (user_open(&snac, uid)) {
|
2023-02-02 06:50:51 +03:00
|
|
|
process_user_queue(&snac);
|
2022-10-25 14:59:15 +03:00
|
|
|
user_free(&snac);
|
|
|
|
}
|
2022-10-01 21:57:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:21:16 +03:00
|
|
|
/* global queue */
|
|
|
|
process_queue();
|
|
|
|
|
2022-10-17 12:18:09 +03:00
|
|
|
/* time to purge? */
|
|
|
|
if ((t = time(NULL)) > purge_time) {
|
|
|
|
pthread_t pth;
|
|
|
|
|
|
|
|
pthread_create(&pth, NULL, purge_thread, NULL);
|
2022-12-10 19:15:26 +03:00
|
|
|
pthread_detach(pth);
|
2022-10-17 12:18:09 +03:00
|
|
|
|
|
|
|
/* next purge time is tomorrow */
|
|
|
|
purge_time = t + 24 * 60 * 60;
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:56:19 +03:00
|
|
|
/* sleep 3 seconds */
|
2023-02-06 12:53:29 +03:00
|
|
|
pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
|
2022-10-13 16:56:19 +03:00
|
|
|
struct timespec ts;
|
2023-02-06 12:53:29 +03:00
|
|
|
|
2022-10-13 16:56:19 +03:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
ts.tv_sec += 3;
|
|
|
|
|
|
|
|
pthread_mutex_lock(&dummy_mutex);
|
|
|
|
while (pthread_cond_timedwait(&dummy_cond, &dummy_mutex, &ts) == 0);
|
|
|
|
pthread_mutex_unlock(&dummy_mutex);
|
2022-10-01 21:57:06 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 10:44:00 +03:00
|
|
|
srv_log(xs_fmt("background thread stopped"));
|
2022-10-01 21:57:06 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 20:50:37 +03:00
|
|
|
static void *connection_thread(void *arg)
|
|
|
|
/* connection thread */
|
|
|
|
{
|
|
|
|
httpd_connection((FILE *)arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 13:29:46 +03:00
|
|
|
/** job control **/
|
|
|
|
|
|
|
|
/* mutex to access the lists of jobs */
|
|
|
|
static pthread_mutex_t job_mutex;
|
|
|
|
|
|
|
|
/* semaphre to trigger job processing */
|
|
|
|
static sem_t job_sem;
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
/* fifo of jobs */
|
|
|
|
xs_list *job_fifo = NULL;
|
2023-02-06 13:29:46 +03:00
|
|
|
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
void job_post(const xs_val *job)
|
|
|
|
/* posts a job for the threads to process it */
|
2023-02-06 13:29:46 +03:00
|
|
|
{
|
|
|
|
/* lock the mutex */
|
|
|
|
pthread_mutex_lock(&job_mutex);
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
/* add to the fifo */
|
|
|
|
job_fifo = xs_list_append(job_fifo, job);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
|
|
|
/* unlock the mutex */
|
|
|
|
pthread_mutex_unlock(&job_mutex);
|
|
|
|
|
|
|
|
/* ask for someone to attend it */
|
|
|
|
sem_post(&job_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
void job_wait(xs_val **job)
|
|
|
|
/* waits for an available job */
|
2023-02-06 13:29:46 +03:00
|
|
|
{
|
2023-02-06 20:43:27 +03:00
|
|
|
*job = NULL;
|
2023-02-06 13:29:46 +03:00
|
|
|
|
|
|
|
if (sem_wait(&job_sem) == 0) {
|
|
|
|
/* lock the mutex */
|
|
|
|
pthread_mutex_lock(&job_mutex);
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
/* dequeue */
|
|
|
|
job_fifo = xs_list_shift(job_fifo, job);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
|
|
|
/* unlock the mutex */
|
|
|
|
pthread_mutex_unlock(&job_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 12:53:29 +03:00
|
|
|
#ifndef MAX_THREADS
|
|
|
|
#define MAX_THREADS 256
|
|
|
|
#endif
|
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
void httpd(void)
|
|
|
|
/* starts the server */
|
|
|
|
{
|
|
|
|
char *address;
|
|
|
|
int port;
|
|
|
|
int rs;
|
2023-02-06 12:53:29 +03:00
|
|
|
pthread_t threads[MAX_THREADS];
|
|
|
|
int n_threads = 0;
|
2022-09-21 19:09:16 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:13:11 +03:00
|
|
|
srv_running = 1;
|
|
|
|
|
2022-09-29 13:50:50 +03:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGTERM, term_handler);
|
2023-02-06 12:53:29 +03:00
|
|
|
signal(SIGINT, term_handler);
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2022-10-14 09:47:52 +03:00
|
|
|
srv_log(xs_fmt("httpd start %s:%d %s", address, port, USER_AGENT));
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2023-02-06 13:29:46 +03:00
|
|
|
/* initialize the job control engine */
|
|
|
|
pthread_mutex_init(&job_mutex, NULL);
|
|
|
|
sem_init(&job_sem, 0, 0);
|
2023-02-06 20:43:27 +03:00
|
|
|
job_fifo = xs_list_new();
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2023-02-06 12:53:29 +03:00
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
2023-02-06 13:29:46 +03:00
|
|
|
/* get number of CPUs on the machine */
|
2023-02-06 12:53:29 +03:00
|
|
|
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));
|
|
|
|
|
|
|
|
/* thread #0 is the background thread */
|
|
|
|
pthread_create(&threads[0], NULL, background_thread, NULL);
|
2022-10-01 21:57:06 +03:00
|
|
|
|
2022-09-29 13:50:50 +03:00
|
|
|
if (setjmp(on_break) == 0) {
|
|
|
|
for (;;) {
|
2022-10-10 20:33:39 +03:00
|
|
|
FILE *f = xs_socket_accept(rs);
|
|
|
|
|
2023-02-06 10:44:00 +03:00
|
|
|
pthread_t cth;
|
2022-10-10 20:50:37 +03:00
|
|
|
|
2023-02-06 10:44:00 +03:00
|
|
|
pthread_create(&cth, NULL, connection_thread, f);
|
|
|
|
pthread_detach(cth);
|
2022-09-29 13:50:50 +03:00
|
|
|
}
|
2022-09-21 19:09:16 +03:00
|
|
|
}
|
|
|
|
|
2022-09-29 13:50:50 +03:00
|
|
|
srv_running = 0;
|
|
|
|
|
2023-02-06 12:53:29 +03:00
|
|
|
/* wait for the background thread to end */
|
|
|
|
pthread_join(threads[0], NULL);
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
job_fifo = xs_free(job_fifo);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
srv_log(xs_fmt("httpd stop %s:%d", address, port));
|
|
|
|
}
|