2022-09-21 19:09:16 +03:00
|
|
|
/* snac - A simple, minimalistic ActivityPub instance */
|
2024-01-04 11:22:03 +03:00
|
|
|
/* copyright (c) 2022 - 2024 grunfink et al. / 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_json.h"
|
|
|
|
#include "xs_socket.h"
|
|
|
|
#include "xs_httpd.h"
|
2022-10-16 20:00:17 +03:00
|
|
|
#include "xs_mime.h"
|
2023-05-08 10:34:27 +03:00
|
|
|
#include "xs_time.h"
|
2023-05-17 11:08:57 +03:00
|
|
|
#include "xs_openssl.h"
|
2023-10-17 21:02:08 +03:00
|
|
|
#include "xs_fcgi.h"
|
2023-11-24 02:05:45 +03:00
|
|
|
#include "xs_html.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>
|
2023-05-07 18:58:11 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2023-02-10 15:39:17 +03:00
|
|
|
#include <sys/resource.h> // for getrlimit()
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
#ifdef USE_POLL_FOR_SLEEP
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
/** server state **/
|
2024-01-08 10:21:22 +03:00
|
|
|
srv_state *p_state = NULL;
|
2023-12-26 12:41:55 +03:00
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
|
2023-12-26 20:28:43 +03:00
|
|
|
/** job control **/
|
|
|
|
|
|
|
|
/* mutex to access the lists of jobs */
|
|
|
|
static pthread_mutex_t job_mutex;
|
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
/* semaphore to trigger job processing */
|
2023-12-26 20:28:43 +03:00
|
|
|
static sem_t *job_sem;
|
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
typedef struct job_fifo_item {
|
|
|
|
struct job_fifo_item *next;
|
|
|
|
xs_val *job;
|
|
|
|
} job_fifo_item;
|
|
|
|
|
|
|
|
static job_fifo_item *job_fifo_first = NULL;
|
|
|
|
static job_fifo_item *job_fifo_last = NULL;
|
2023-12-26 20:28:43 +03:00
|
|
|
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
/** other global data **/
|
|
|
|
|
|
|
|
static jmp_buf on_break;
|
|
|
|
|
|
|
|
|
|
|
|
/** code **/
|
|
|
|
|
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\":{}}";
|
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
xs_str *nodeinfo_2_0(void)
|
2022-12-08 11:58:47 +03:00
|
|
|
/* builds a nodeinfo json object */
|
|
|
|
{
|
2023-08-21 23:49:46 +03:00
|
|
|
int n_utotal = 0;
|
|
|
|
int n_umonth = 0;
|
|
|
|
int n_uhyear = 0;
|
|
|
|
int n_posts = 0;
|
|
|
|
xs *users = user_list();
|
2023-12-29 12:58:21 +03:00
|
|
|
xs_list *p = users;
|
2024-05-23 11:01:37 +03:00
|
|
|
const char *v;
|
2023-12-29 12:58:21 +03:00
|
|
|
double now = (double)time(NULL);
|
2022-12-08 11:58:47 +03:00
|
|
|
|
2023-08-21 23:49:46 +03:00
|
|
|
while (xs_list_iter(&p, &v)) {
|
|
|
|
/* build the full path name to the last usage log */
|
|
|
|
xs *llfn = xs_fmt("%s/user/%s/lastlog.txt", srv_basedir, v);
|
2023-12-29 12:58:21 +03:00
|
|
|
double llsecs = now - mtime(llfn);
|
2023-08-21 23:49:46 +03:00
|
|
|
|
|
|
|
if (llsecs < 60 * 60 * 24 * 30 * 6) {
|
|
|
|
n_uhyear++;
|
|
|
|
|
|
|
|
if (llsecs < 60 * 60 * 24 * 30)
|
|
|
|
n_umonth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_utotal++;
|
|
|
|
|
|
|
|
/* build the file to each user public.idx */
|
2023-12-29 12:58:21 +03:00
|
|
|
xs *pidxfn = xs_fmt("%s/user/%s/public.idx", srv_basedir, v);
|
2023-08-21 23:49:46 +03:00
|
|
|
n_posts += index_len(pidxfn);
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs_fmt(nodeinfo_2_0_template, n_utotal, n_umonth, n_uhyear, n_posts);
|
2022-12-08 11:58:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
static xs_str *greeting_html(void)
|
|
|
|
/* processes and returns greeting.html */
|
2022-09-21 20:28:30 +03:00
|
|
|
{
|
2023-08-14 10:32:17 +03:00
|
|
|
/* try to open greeting.html */
|
|
|
|
xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
|
|
|
|
FILE *f;
|
|
|
|
xs_str *s = NULL;
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
if ((f = fopen(fn, "r")) != NULL) {
|
|
|
|
s = xs_readall(f);
|
|
|
|
fclose(f);
|
2023-05-04 10:28:36 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
/* replace %host% */
|
|
|
|
s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
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";
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
/* replace %admin_email */
|
|
|
|
s = xs_replace_i(s, "%admin_email%", adm_email);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
/* does it have a %userlist% mark? */
|
|
|
|
if (xs_str_in(s, "%userlist%") != -1) {
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *host = xs_dict_get(srv_config, "host");
|
2023-08-14 10:32:17 +03:00
|
|
|
xs *list = user_list();
|
2023-11-24 02:05:45 +03:00
|
|
|
xs_list *p = list;
|
2024-05-23 11:01:37 +03:00
|
|
|
const xs_str *uid;
|
2023-11-24 02:05:45 +03:00
|
|
|
|
|
|
|
xs_html *ul = xs_html_tag("ul",
|
|
|
|
xs_html_attr("class", "snac-user-list"));
|
2022-10-16 10:59:36 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
|
|
|
snac user;
|
2023-04-20 19:12:56 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
if (user_open(&user, uid)) {
|
2023-11-24 02:05:45 +03:00
|
|
|
xs_html_add(ul,
|
|
|
|
xs_html_tag("li",
|
|
|
|
xs_html_tag("a",
|
|
|
|
xs_html_attr("href", user.actor),
|
|
|
|
xs_html_text("@"),
|
|
|
|
xs_html_text(uid),
|
|
|
|
xs_html_text("@"),
|
|
|
|
xs_html_text(host),
|
|
|
|
xs_html_text(" ("),
|
|
|
|
xs_html_text(xs_dict_get(user.config, "name")),
|
|
|
|
xs_html_text(")"))));
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
user_free(&user);
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-11-24 02:05:45 +03:00
|
|
|
xs *s1 = xs_html_render(ul);
|
|
|
|
s = xs_replace_i(s, "%userlist%", s1);
|
2023-08-14 10:32:17 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
return s;
|
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
|
2023-08-14 10:32:17 +03:00
|
|
|
int server_get_handler(xs_dict *req, const char *q_path,
|
|
|
|
char **body, int *b_size, char **ctype)
|
|
|
|
/* basic server services */
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
/* is it the server root? */
|
|
|
|
if (*q_path == '\0') {
|
2024-05-21 15:12:15 +03:00
|
|
|
const xs_dict *q_vars = xs_dict_get(req, "q_vars");
|
|
|
|
const char *t = NULL;
|
2023-11-08 11:20:34 +03:00
|
|
|
|
|
|
|
if (xs_type(q_vars) == XSTYPE_DICT && (t = xs_dict_get(q_vars, "t"))) {
|
2024-02-21 10:09:57 +03:00
|
|
|
/** search by tag **/
|
2023-11-08 11:43:49 +03:00
|
|
|
int skip = 0;
|
|
|
|
int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *v;
|
2023-11-08 11:43:49 +03:00
|
|
|
|
|
|
|
if ((v = xs_dict_get(q_vars, "skip")) != NULL)
|
|
|
|
skip = atoi(v);
|
|
|
|
if ((v = xs_dict_get(q_vars, "show")) != NULL)
|
|
|
|
show = atoi(v);
|
|
|
|
|
|
|
|
xs *tl = tag_search(t, skip, show + 1);
|
|
|
|
int more = 0;
|
|
|
|
if (xs_list_len(tl) >= show + 1) {
|
|
|
|
/* drop the last one */
|
|
|
|
tl = xs_list_del(tl, -1);
|
|
|
|
more = 1;
|
|
|
|
}
|
2023-11-08 11:20:34 +03:00
|
|
|
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *accept = xs_dict_get(req, "accept");
|
2024-05-05 12:11:32 +03:00
|
|
|
if (!xs_is_null(accept) && strcmp(accept, "application/rss+xml") == 0) {
|
|
|
|
xs *link = xs_fmt("%s/?t=%s", srv_baseurl, t);
|
|
|
|
|
|
|
|
*body = timeline_to_rss(NULL, tl, link, link, link);
|
|
|
|
*ctype = "application/rss+xml; charset=utf-8";
|
|
|
|
}
|
2024-05-07 21:32:13 +03:00
|
|
|
else {
|
|
|
|
xs *page = xs_fmt("?t=%s", t);
|
2024-05-15 06:57:21 +03:00
|
|
|
xs *title = xs_fmt(L("Search results for tag #%s"), t);
|
2024-05-07 21:32:13 +03:00
|
|
|
*body = html_timeline(NULL, tl, 0, skip, show, more, title, page, 0);
|
|
|
|
}
|
2023-11-08 11:20:34 +03:00
|
|
|
}
|
|
|
|
else
|
2023-08-14 12:24:41 +03:00
|
|
|
if (xs_type(xs_dict_get(srv_config, "show_instance_timeline")) == XSTYPE_TRUE) {
|
2024-02-21 10:09:57 +03:00
|
|
|
/** instance timeline **/
|
2023-08-14 12:24:41 +03:00
|
|
|
xs *tl = timeline_instance_list(0, 30);
|
2024-05-07 21:32:13 +03:00
|
|
|
*body = html_timeline(NULL, tl, 0, 0, 0, 0,
|
|
|
|
L("Recent posts by users in this instance"), NULL, 0);
|
2023-08-14 12:24:41 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*body = greeting_html();
|
|
|
|
|
|
|
|
if (*body)
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
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 ) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
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) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-12-08 11:58:47 +03:00
|
|
|
*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
|
2024-01-26 19:42:20 +03:00
|
|
|
if (strcmp(q_path, "/.well-known/host-meta") == 0) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2024-01-26 19:42:20 +03:00
|
|
|
*ctype = "application/xrd+xml";
|
2024-01-30 10:09:43 +03:00
|
|
|
*body = xs_fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
|
|
|
|
"<XRD>"
|
|
|
|
"<Link rel=\"lrdd\" type=\"application/xrd+xml\" template=\"https://%s/.well-known/webfinger?resource={uri}\"/>"
|
|
|
|
"</XRD>", xs_dict_get(srv_config, "host"));
|
2024-01-26 19:42:20 +03:00
|
|
|
}
|
|
|
|
else
|
2022-12-08 11:58:47 +03:00
|
|
|
if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-12-08 11:58:47 +03:00
|
|
|
*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) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2022-12-13 00:11:07 +03:00
|
|
|
*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;
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *method;
|
2022-10-24 21:06:02 +03:00
|
|
|
int status = 0;
|
2023-08-14 10:32:17 +03:00
|
|
|
xs_str *body = NULL;
|
2022-10-24 21:06:02 +03:00
|
|
|
int b_size = 0;
|
|
|
|
char *ctype = NULL;
|
2023-08-12 22:46:54 +03:00
|
|
|
xs *headers = xs_dict_new();
|
2022-10-24 21:06:02 +03:00
|
|
|
xs *q_path = NULL;
|
|
|
|
xs *payload = NULL;
|
2023-07-02 12:11:01 +03:00
|
|
|
xs *etag = NULL;
|
2022-10-24 21:06:02 +03:00
|
|
|
int p_size = 0;
|
2024-05-21 15:12:15 +03:00
|
|
|
const char *p;
|
2023-10-17 21:02:08 +03:00
|
|
|
int fcgi_id;
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
if (p_state->use_fcgi)
|
2023-10-17 21:02:08 +03:00
|
|
|
req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id);
|
|
|
|
else
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-18 11:35:22 +03:00
|
|
|
if (!(method = xs_dict_get(req, "method")) || !(p = xs_dict_get(req, "path"))) {
|
|
|
|
/* missing needed headers; discard */
|
|
|
|
fclose(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
q_path = xs_dup(p);
|
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
|
|
|
|
2023-04-22 00:17:58 +03:00
|
|
|
#ifndef NO_MASTODON_API
|
2023-04-08 10:09:43 +03:00
|
|
|
if (status == 0)
|
|
|
|
status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
|
|
|
|
|
2023-04-09 21:34:05 +03:00
|
|
|
if (status == 0)
|
|
|
|
status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype);
|
2023-04-22 00:17:58 +03:00
|
|
|
#endif /* NO_MASTODON_API */
|
2023-04-09 21:34:05 +03:00
|
|
|
|
2022-09-28 06:22:08 +03:00
|
|
|
if (status == 0)
|
2023-07-02 12:11:01 +03:00
|
|
|
status = html_get_handler(req, q_path, &body, &b_size, &ctype, &etag);
|
2022-09-21 20:28:30 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp(method, "POST") == 0) {
|
2023-04-22 00:17:58 +03:00
|
|
|
|
|
|
|
#ifndef NO_MASTODON_API
|
2022-09-23 21:28:23 +03:00
|
|
|
if (status == 0)
|
2023-04-11 22:07:47 +03:00
|
|
|
status = oauth_post_handler(req, q_path,
|
2022-09-23 21:28:23 +03:00
|
|
|
payload, p_size, &body, &b_size, &ctype);
|
2022-09-28 06:22:08 +03:00
|
|
|
|
2023-04-08 08:04:40 +03:00
|
|
|
if (status == 0)
|
2023-04-11 22:07:47 +03:00
|
|
|
status = mastoapi_post_handler(req, q_path,
|
2023-04-08 08:04:40 +03:00
|
|
|
payload, p_size, &body, &b_size, &ctype);
|
2023-04-22 00:17:58 +03:00
|
|
|
#endif
|
2023-04-08 08:04:40 +03:00
|
|
|
|
2023-04-08 07:09:05 +03:00
|
|
|
if (status == 0)
|
2023-04-11 22:07:47 +03:00
|
|
|
status = activitypub_post_handler(req, q_path,
|
2023-04-08 07:09:05 +03:00
|
|
|
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
|
|
|
}
|
2023-04-22 02:21:09 +03:00
|
|
|
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
|
|
|
|
|
|
|
|
}
|
2023-08-12 22:46:54 +03:00
|
|
|
else
|
|
|
|
if (strcmp(method, "OPTIONS") == 0) {
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_OK;
|
2023-08-12 22:46:54 +03:00
|
|
|
}
|
2024-01-11 16:38:08 +03:00
|
|
|
else
|
|
|
|
if (strcmp(method, "DELETE") == 0) {
|
|
|
|
#ifndef NO_MASTODON_API
|
|
|
|
if (status == 0)
|
|
|
|
status = mastoapi_delete_handler(req, q_path,
|
2024-04-29 10:26:37 +03:00
|
|
|
payload, p_size, &body, &b_size, &ctype);
|
2024-01-11 16:38:08 +03:00
|
|
|
#endif
|
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
/* unattended? it's an error */
|
2022-09-28 06:22:08 +03:00
|
|
|
if (status == 0) {
|
2023-08-12 20:09:09 +03:00
|
|
|
srv_archive_error("unattended_method", "unattended method", req, payload);
|
2022-09-28 06:22:08 +03:00
|
|
|
srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
|
2024-05-26 22:45:41 +03:00
|
|
|
status = HTTP_STATUS_NOT_FOUND;
|
2022-09-28 06:22:08 +03:00
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2024-05-26 22:45:41 +03:00
|
|
|
if (status == HTTP_STATUS_FORBIDDEN)
|
2023-10-08 01:06:37 +03:00
|
|
|
body = xs_str_new("<h1>403 Forbidden</h1>");
|
|
|
|
|
2024-05-26 22:45:41 +03:00
|
|
|
if (status == HTTP_STATUS_NOT_FOUND)
|
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
|
|
|
|
2024-05-26 22:45:41 +03:00
|
|
|
if (status == HTTP_STATUS_BAD_REQUEST && body != NULL)
|
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
|
|
|
|
2024-05-26 22:45:41 +03:00
|
|
|
if (status == HTTP_STATUS_SEE_OTHER)
|
2022-09-21 20:28:30 +03:00
|
|
|
headers = xs_dict_append(headers, "location", body);
|
|
|
|
|
2024-05-26 22:45:41 +03:00
|
|
|
if (status == HTTP_STATUS_UNAUTHORIZED) {
|
2023-06-24 09:29:29 +03:00
|
|
|
xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"",
|
|
|
|
body, xs_dict_get(srv_config, "host"));
|
|
|
|
|
2023-06-18 20:34:35 +03:00
|
|
|
headers = xs_dict_append(headers, "WWW-Authenticate", www_auth);
|
2024-04-13 21:55:02 +03:00
|
|
|
headers = xs_dict_append(headers, "Cache-Control", "no-cache, must-revalidate, max-age=0");
|
2023-06-18 20:34:35 +03:00
|
|
|
}
|
2022-09-21 20:28:30 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-02 12:11:01 +03:00
|
|
|
if (!xs_is_null(etag))
|
|
|
|
headers = xs_dict_append(headers, "etag", etag);
|
|
|
|
|
2023-08-19 13:52:28 +03:00
|
|
|
/* if there are any additional headers, add them */
|
2024-05-21 15:12:15 +03:00
|
|
|
const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");
|
2023-08-19 13:52:28 +03:00
|
|
|
if (xs_type(more_headers) == XSTYPE_DICT) {
|
2024-05-23 11:01:37 +03:00
|
|
|
const char *k, *v;
|
2024-03-09 10:44:48 +03:00
|
|
|
int c = 0;
|
|
|
|
while (xs_dict_next(more_headers, &k, &v, &c))
|
2023-08-19 13:52:28 +03:00
|
|
|
headers = xs_dict_set(headers, k, v);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-12 22:46:54 +03:00
|
|
|
headers = xs_dict_append(headers, "access-control-allow-origin", "*");
|
|
|
|
headers = xs_dict_append(headers, "access-control-allow-headers", "*");
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
if (p_state->use_fcgi)
|
2023-10-17 21:02:08 +03:00
|
|
|
xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
|
|
|
|
else
|
|
|
|
xs_httpd_response(f, status, headers, body, b_size);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
fclose(f);
|
2022-09-21 20:28:30 +03:00
|
|
|
|
2023-03-02 19:13:17 +03:00
|
|
|
srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
|
2022-09-24 13:22:17 +03:00
|
|
|
|
2023-04-23 16:37:09 +03:00
|
|
|
/* JSON validation check */
|
2023-12-18 15:24:07 +03:00
|
|
|
if (!xs_is_null(body) && strcmp(ctype, "application/json") == 0) {
|
2023-04-23 16:37:09 +03:00
|
|
|
xs *j = xs_json_loads(body);
|
|
|
|
|
|
|
|
if (j == NULL) {
|
|
|
|
srv_log(xs_fmt("bad JSON"));
|
|
|
|
srv_archive_error("bad_json", "bad JSON", req, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 14:59:15 +03:00
|
|
|
xs_free(body);
|
2022-09-21 19:09:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-02 14:38:02 +03:00
|
|
|
void job_post(const xs_val *job, int urgent)
|
2023-02-06 20:43:27 +03:00
|
|
|
/* posts a job for the threads to process it */
|
2023-02-06 13:29:46 +03:00
|
|
|
{
|
2023-02-06 20:59:20 +03:00
|
|
|
if (job != NULL) {
|
|
|
|
/* lock the mutex */
|
|
|
|
pthread_mutex_lock(&job_mutex);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
job_fifo_item *i = xs_realloc(NULL, sizeof(job_fifo_item));
|
|
|
|
*i = (job_fifo_item){ NULL, xs_dup(job) };
|
2024-01-07 13:13:59 +03:00
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
if (job_fifo_first == NULL)
|
|
|
|
job_fifo_first = job_fifo_last = i;
|
|
|
|
else
|
|
|
|
if (urgent) {
|
|
|
|
/* prepend */
|
|
|
|
i->next = job_fifo_first;
|
|
|
|
job_fifo_first = i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* append */
|
|
|
|
job_fifo_last->next = i;
|
|
|
|
job_fifo_last = i;
|
2023-03-02 14:38:02 +03:00
|
|
|
}
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->job_fifo_size++;
|
2024-01-08 10:10:57 +03:00
|
|
|
|
2024-01-10 11:16:40 +03:00
|
|
|
if (p_state->job_fifo_size > p_state->peak_job_fifo_size)
|
|
|
|
p_state->peak_job_fifo_size = p_state->job_fifo_size;
|
2024-01-08 11:17:38 +03:00
|
|
|
|
2023-02-06 20:59:20 +03:00
|
|
|
/* unlock the mutex */
|
|
|
|
pthread_mutex_unlock(&job_mutex);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2024-01-07 13:13:59 +03:00
|
|
|
/* ask for someone to attend it */
|
|
|
|
sem_post(job_sem);
|
|
|
|
}
|
2023-02-06 13:29:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-05-07 14:18:30 +03:00
|
|
|
if (sem_wait(job_sem) == 0) {
|
2023-02-06 13:29:46 +03:00
|
|
|
/* lock the mutex */
|
|
|
|
pthread_mutex_lock(&job_mutex);
|
|
|
|
|
2023-02-06 20:43:27 +03:00
|
|
|
/* dequeue */
|
2024-01-08 10:10:57 +03:00
|
|
|
job_fifo_item *i = job_fifo_first;
|
|
|
|
|
|
|
|
if (i != NULL) {
|
|
|
|
job_fifo_first = i->next;
|
|
|
|
|
|
|
|
if (job_fifo_first == NULL)
|
|
|
|
job_fifo_last = NULL;
|
|
|
|
|
|
|
|
*job = i->job;
|
|
|
|
xs_free(i);
|
2023-02-06 13:29:46 +03:00
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->job_fifo_size--;
|
2024-01-03 13:01:25 +03:00
|
|
|
}
|
|
|
|
|
2023-02-06 13:29:46 +03:00
|
|
|
/* unlock the mutex */
|
|
|
|
pthread_mutex_unlock(&job_mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-06 20:59:20 +03:00
|
|
|
static void *job_thread(void *arg)
|
|
|
|
/* job thread */
|
|
|
|
{
|
2023-05-07 14:19:37 +03:00
|
|
|
int pid = (int)(uintptr_t)arg;
|
2023-02-06 21:23:35 +03:00
|
|
|
|
2023-02-10 11:07:54 +03:00
|
|
|
srv_debug(1, xs_fmt("job thread %d started", pid));
|
2023-02-06 20:59:20 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
xs *job = NULL;
|
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
p_state->th_state[pid] = THST_WAIT;
|
2023-02-06 20:59:20 +03:00
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
job_wait(&job);
|
2023-02-06 20:59:20 +03:00
|
|
|
|
2024-01-07 13:13:59 +03:00
|
|
|
if (job == NULL) /* corrupted message? */
|
|
|
|
continue;
|
2023-02-06 20:59:20 +03:00
|
|
|
|
2024-01-07 13:13:59 +03:00
|
|
|
if (xs_type(job) == XSTYPE_FALSE) /* special message: exit */
|
|
|
|
break;
|
|
|
|
else
|
2023-02-06 20:59:20 +03:00
|
|
|
if (xs_type(job) == XSTYPE_DATA) {
|
|
|
|
/* it's a socket */
|
2023-02-06 21:23:35 +03:00
|
|
|
FILE *f = NULL;
|
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
p_state->th_state[pid] = THST_IN;
|
|
|
|
|
2023-10-14 11:04:40 +03:00
|
|
|
xs_data_get(&f, job);
|
2023-02-06 21:23:35 +03:00
|
|
|
|
|
|
|
if (f != NULL)
|
|
|
|
httpd_connection(f);
|
2023-02-06 20:59:20 +03:00
|
|
|
}
|
2023-02-06 22:07:29 +03:00
|
|
|
else {
|
|
|
|
/* it's a q_item */
|
2024-01-08 10:50:40 +03:00
|
|
|
p_state->th_state[pid] = THST_QUEUE;
|
2024-01-08 10:38:25 +03:00
|
|
|
|
2023-02-06 22:07:29 +03:00
|
|
|
process_queue_item(job);
|
|
|
|
}
|
2023-02-06 20:59:20 +03:00
|
|
|
}
|
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
p_state->th_state[pid] = THST_STOP;
|
|
|
|
|
2023-02-10 11:07:54 +03:00
|
|
|
srv_debug(1, xs_fmt("job thread %d stopped", pid));
|
2023-02-06 20:59:20 +03:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-02-22 12:35:26 +03:00
|
|
|
/* background thread sleep control */
|
|
|
|
static pthread_mutex_t sleep_mutex;
|
|
|
|
static pthread_cond_t sleep_cond;
|
2023-02-06 20:59:20 +03:00
|
|
|
|
2023-02-06 22:07:29 +03:00
|
|
|
static void *background_thread(void *arg)
|
|
|
|
/* background thread (queue management and other things) */
|
|
|
|
{
|
|
|
|
time_t purge_time;
|
|
|
|
|
2023-05-04 10:28:36 +03:00
|
|
|
(void)arg;
|
|
|
|
|
2023-02-06 22:07:29 +03:00
|
|
|
/* first purge time */
|
|
|
|
purge_time = time(NULL) + 10 * 60;
|
|
|
|
|
|
|
|
srv_log(xs_fmt("background thread started"));
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
while (p_state->srv_running) {
|
2023-02-06 22:07:29 +03:00
|
|
|
time_t t;
|
2023-02-22 10:39:54 +03:00
|
|
|
int cnt = 0;
|
2023-02-06 22:07:29 +03:00
|
|
|
|
2024-01-08 10:50:40 +03:00
|
|
|
p_state->th_state[0] = THST_QUEUE;
|
2024-01-08 10:38:25 +03:00
|
|
|
|
2023-02-06 22:07:29 +03:00
|
|
|
{
|
|
|
|
xs *list = user_list();
|
2024-05-23 11:01:37 +03:00
|
|
|
char *p;
|
|
|
|
const char *uid;
|
2023-02-06 22:07:29 +03:00
|
|
|
|
|
|
|
/* process queues for all users */
|
|
|
|
p = list;
|
|
|
|
while (xs_list_iter(&p, &uid)) {
|
|
|
|
snac snac;
|
|
|
|
|
|
|
|
if (user_open(&snac, uid)) {
|
2023-02-22 10:39:54 +03:00
|
|
|
cnt += process_user_queue(&snac);
|
2023-02-06 22:07:29 +03:00
|
|
|
user_free(&snac);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* global queue */
|
2023-02-22 10:39:54 +03:00
|
|
|
cnt += process_queue();
|
2023-02-06 22:07:29 +03:00
|
|
|
|
|
|
|
/* time to purge? */
|
|
|
|
if ((t = time(NULL)) > purge_time) {
|
|
|
|
/* next purge time is tomorrow */
|
|
|
|
purge_time = t + 24 * 60 * 60;
|
|
|
|
|
|
|
|
xs *q_item = xs_dict_new();
|
|
|
|
q_item = xs_dict_append(q_item, "type", "purge");
|
2023-03-02 14:38:02 +03:00
|
|
|
job_post(q_item, 0);
|
2023-02-06 22:07:29 +03:00
|
|
|
}
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
if (cnt == 0) {
|
|
|
|
/* sleep 3 seconds */
|
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
p_state->th_state[0] = THST_WAIT;
|
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
#ifdef USE_POLL_FOR_SLEEP
|
|
|
|
poll(NULL, 0, 3 * 1000);
|
|
|
|
#else
|
|
|
|
struct timespec ts;
|
2023-02-06 22:07:29 +03:00
|
|
|
|
2023-02-22 10:39:54 +03:00
|
|
|
clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
ts.tv_sec += 3;
|
2023-02-06 22:07:29 +03:00
|
|
|
|
2023-02-22 12:35:26 +03:00
|
|
|
pthread_mutex_lock(&sleep_mutex);
|
|
|
|
while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
|
|
|
|
pthread_mutex_unlock(&sleep_mutex);
|
2023-02-22 10:39:54 +03:00
|
|
|
#endif
|
|
|
|
}
|
2023-02-06 22:07:29 +03:00
|
|
|
}
|
|
|
|
|
2024-01-08 10:38:25 +03:00
|
|
|
p_state->th_state[0] = THST_STOP;
|
|
|
|
|
2023-02-06 22:07:29 +03:00
|
|
|
srv_log(xs_fmt("background thread stopped"));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-08 10:10:57 +03:00
|
|
|
void term_handler(int s)
|
|
|
|
{
|
|
|
|
(void)s;
|
|
|
|
|
|
|
|
longjmp(on_break, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
srv_state *srv_state_op(xs_str **fname, int op)
|
|
|
|
/* opens or deletes the shared memory object */
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
srv_state *ss = NULL;
|
|
|
|
|
|
|
|
if (*fname == NULL)
|
|
|
|
*fname = xs_fmt("/%s_snac_state", xs_dict_get(srv_config, "host"));
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case 0: /* open for writing */
|
2024-05-21 20:08:33 +03:00
|
|
|
|
|
|
|
#ifdef WITHOUT_SHM
|
|
|
|
|
|
|
|
errno = ENOTSUP;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) {
|
|
|
|
ftruncate(fd, sizeof(*ss));
|
|
|
|
|
|
|
|
if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0)) == MAP_FAILED)
|
|
|
|
ss = NULL;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2024-05-21 20:08:33 +03:00
|
|
|
#endif
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
if (ss == NULL) {
|
|
|
|
/* shared memory error: just create a plain structure */
|
|
|
|
srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno)));
|
|
|
|
ss = malloc(sizeof(*ss));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init structure */
|
|
|
|
*ss = (srv_state){0};
|
|
|
|
ss->s_size = sizeof(*ss);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: /* open for reading */
|
2024-05-21 20:08:33 +03:00
|
|
|
|
|
|
|
#ifdef WITHOUT_SHM
|
|
|
|
|
|
|
|
errno = ENOTSUP;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) {
|
|
|
|
if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
|
|
|
|
ss = NULL;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2024-05-21 20:08:33 +03:00
|
|
|
#endif
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
if (ss == NULL) {
|
|
|
|
/* shared memory error */
|
|
|
|
srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (ss->s_size != sizeof(*ss)) {
|
|
|
|
srv_log(xs_fmt("error: struct size mismatch (%d != %d)",
|
|
|
|
ss->s_size, sizeof(*ss)));
|
|
|
|
|
|
|
|
munmap(ss, sizeof(*ss));
|
|
|
|
|
|
|
|
ss = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* unlink */
|
2024-05-21 20:08:33 +03:00
|
|
|
|
|
|
|
#ifndef WITHOUT_SHM
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
if (*fname)
|
|
|
|
shm_unlink(*fname);
|
|
|
|
|
2024-05-21 20:08:33 +03:00
|
|
|
#endif
|
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-21 19:09:16 +03:00
|
|
|
void httpd(void)
|
|
|
|
/* starts the server */
|
|
|
|
{
|
2023-09-23 23:02:52 +03:00
|
|
|
const char *address;
|
|
|
|
const char *port;
|
2022-09-21 19:09:16 +03:00
|
|
|
int rs;
|
2023-02-06 21:29:22 +03:00
|
|
|
pthread_t threads[MAX_THREADS] = {0};
|
2023-02-06 20:59:20 +03:00
|
|
|
int n;
|
2024-01-03 13:01:25 +03:00
|
|
|
xs *sem_name = NULL;
|
2024-01-10 21:37:40 +03:00
|
|
|
xs *shm_name = NULL;
|
2023-05-25 18:20:21 +03:00
|
|
|
sem_t anon_job_sem;
|
2022-09-21 19:09:16 +03:00
|
|
|
|
|
|
|
address = xs_dict_get(srv_config, "address");
|
2023-09-23 23:02:52 +03:00
|
|
|
port = xs_number_str(xs_dict_get(srv_config, "port"));
|
2022-09-21 19:09:16 +03:00
|
|
|
|
|
|
|
if ((rs = xs_socket_server(address, port)) == -1) {
|
2023-09-23 23:02:52 +03:00
|
|
|
srv_log(xs_fmt("cannot bind socket to %s:%s", address, port));
|
2022-09-21 19:09:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-21 08:14:51 +03:00
|
|
|
/* setup the server stat structure */
|
|
|
|
p_state = srv_state_op(&shm_name, 0);
|
|
|
|
|
|
|
|
p_state->srv_start_time = time(NULL);
|
|
|
|
|
|
|
|
p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->srv_running = 1;
|
2022-09-21 19:13:11 +03:00
|
|
|
|
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
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
srv_log(xs_fmt("httpd%s start %s:%s %s", p_state->use_fcgi ? " (FastCGI)" : "",
|
2023-10-17 21:02:08 +03:00
|
|
|
address, port, USER_AGENT));
|
2022-09-21 19:09:16 +03:00
|
|
|
|
2023-02-10 15:45:26 +03:00
|
|
|
/* show the number of usable file descriptors */
|
2023-02-10 15:39:17 +03:00
|
|
|
struct rlimit r;
|
|
|
|
getrlimit(RLIMIT_NOFILE, &r);
|
2023-02-10 15:53:12 +03:00
|
|
|
srv_debug(0, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
|
2023-02-10 15:39:17 +03:00
|
|
|
(int) r.rlim_cur, (int) r.rlim_max));
|
|
|
|
|
2023-02-06 13:29:46 +03:00
|
|
|
/* initialize the job control engine */
|
|
|
|
pthread_mutex_init(&job_mutex, NULL);
|
2024-01-03 13:01:25 +03:00
|
|
|
sem_name = xs_fmt("/job_%d", getpid());
|
2023-05-09 18:11:57 +03:00
|
|
|
job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
|
2023-05-25 18:20:21 +03:00
|
|
|
|
|
|
|
if (job_sem == NULL) {
|
|
|
|
/* error opening a named semaphore; try with an anonymous one */
|
|
|
|
if (sem_init(&anon_job_sem, 0, 0) != -1)
|
|
|
|
job_sem = &anon_job_sem;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (job_sem == NULL) {
|
|
|
|
srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-22 12:35:26 +03:00
|
|
|
/* initialize sleep control */
|
|
|
|
pthread_mutex_init(&sleep_mutex, NULL);
|
|
|
|
pthread_cond_init(&sleep_cond, NULL);
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
|
2023-02-09 10:09:55 +03:00
|
|
|
|
2023-02-06 12:53:29 +03:00
|
|
|
#ifdef _SC_NPROCESSORS_ONLN
|
2024-01-08 10:21:22 +03:00
|
|
|
if (p_state->n_threads == 0) {
|
2023-02-09 10:09:55 +03:00
|
|
|
/* get number of CPUs on the machine */
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
|
2023-02-09 10:09:55 +03:00
|
|
|
}
|
2023-02-06 12:53:29 +03:00
|
|
|
#endif
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
if (p_state->n_threads < 4)
|
|
|
|
p_state->n_threads = 4;
|
2023-02-06 12:53:29 +03:00
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
if (p_state->n_threads > MAX_THREADS)
|
|
|
|
p_state->n_threads = MAX_THREADS;
|
2023-02-06 12:53:29 +03:00
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
srv_debug(0, xs_fmt("using %d threads", p_state->n_threads));
|
2023-02-06 12:53:29 +03:00
|
|
|
|
|
|
|
/* thread #0 is the background thread */
|
|
|
|
pthread_create(&threads[0], NULL, background_thread, NULL);
|
2022-10-01 21:57:06 +03:00
|
|
|
|
2023-02-06 20:59:20 +03:00
|
|
|
/* the rest of threads are for job processing */
|
2023-02-10 11:07:54 +03:00
|
|
|
char *ptr = (char *) 0x1;
|
2024-01-08 10:21:22 +03:00
|
|
|
for (n = 1; n < p_state->n_threads; n++)
|
2023-02-10 11:07:54 +03:00
|
|
|
pthread_create(&threads[n], NULL, job_thread, ptr++);
|
2023-02-06 20:59:20 +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-22 12:02:24 +03:00
|
|
|
if (f != NULL) {
|
|
|
|
xs *job = xs_data_new(&f, sizeof(FILE *));
|
2023-03-02 14:38:02 +03:00
|
|
|
job_post(job, 1);
|
2023-02-22 12:02:24 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2022-09-29 13:50:50 +03:00
|
|
|
}
|
2022-09-21 19:09:16 +03:00
|
|
|
}
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->srv_running = 0;
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2024-01-07 13:13:59 +03:00
|
|
|
/* send as many exit jobs as working threads */
|
2024-01-08 10:21:22 +03:00
|
|
|
for (n = 1; n < p_state->n_threads; n++)
|
2024-03-15 06:10:11 +03:00
|
|
|
job_post(xs_stock(XSTYPE_FALSE), 0);
|
2023-02-06 20:59:20 +03:00
|
|
|
|
|
|
|
/* wait for all the threads to exit */
|
2024-01-08 10:21:22 +03:00
|
|
|
for (n = 0; n < p_state->n_threads; n++)
|
2023-02-06 20:59:20 +03:00
|
|
|
pthread_join(threads[n], NULL);
|
2022-09-29 13:50:50 +03:00
|
|
|
|
2023-05-08 10:17:00 +03:00
|
|
|
sem_close(job_sem);
|
2023-05-09 18:11:57 +03:00
|
|
|
sem_unlink(sem_name);
|
2023-05-08 10:17:00 +03:00
|
|
|
|
2024-01-10 21:37:40 +03:00
|
|
|
srv_state_op(&shm_name, 2);
|
|
|
|
|
2024-01-08 10:21:22 +03:00
|
|
|
xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
|
2023-05-08 10:34:27 +03:00
|
|
|
|
2024-01-03 13:01:25 +03:00
|
|
|
srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)",
|
2024-01-08 10:21:22 +03:00
|
|
|
p_state->use_fcgi ? " (FastCGI)" : "",
|
2023-10-17 21:02:08 +03:00
|
|
|
address, port, uptime));
|
2022-09-21 19:09:16 +03:00
|
|
|
}
|