snac2/httpd.c

295 lines
7.1 KiB
C
Raw Permalink Normal View History

2022-09-21 16:09:16 +00:00
/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 grunfink - MIT license */
#include "xs.h"
2022-09-21 17:28:30 +00:00
#include "xs_io.h"
2022-09-21 16:09:16 +00:00
#include "xs_encdec.h"
#include "xs_json.h"
#include "xs_socket.h"
#include "xs_httpd.h"
#include "snac.h"
2022-09-29 10:50:50 +00:00
#include <setjmp.h>
2022-10-01 18:57:06 +00:00
#include <pthread.h>
2022-09-29 10:50:50 +00:00
2022-09-22 15:55:59 +00:00
/* susie.png */
const char *susie =
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
"CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
"vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
"eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
"AYTtEsDU9F34AAAAAElFTkSuQmCC";
2022-09-21 16:09:16 +00:00
int server_get_handler(d_char *req, char *q_path,
char **body, int *b_size, char **ctype)
2022-09-21 17:28:30 +00:00
/* basic server services */
{
int status = 0;
char *acpt = xs_dict_get(req, "accept");
2022-09-21 17:28:30 +00:00
if (acpt == NULL)
return 400;
2022-09-21 17:28:30 +00:00
/* is it the server root? */
2022-09-21 17:46:02 +00:00
if (*q_path == '\0') {
2022-09-21 17:28:30 +00: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);
status = 200;
2022-09-21 17:28:30 +00: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 08:20:33 +00:00
s = xs_replace_i(s, "%userlist%", ul);
2022-09-21 17:28:30 +00:00
}
*body = s;
}
}
2022-09-22 15:55:59 +00:00
else
if (strcmp(q_path, "/susie.png") == 0) {
status = 200;
*body = xs_base64_dec(susie, b_size);
*ctype = "image/png";
2022-09-22 15:55:59 +00:00
}
2022-09-27 08:51:50 +00:00
if (status != 0)
2022-09-27 17:00:24 +00:00
srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
2022-09-27 08:51:50 +00:00
return status;
2022-09-21 17:28:30 +00:00
}
2022-10-10 17:33:39 +00:00
void httpd_connection(FILE *f)
/* the connection processor */
2022-09-21 16:09:16 +00:00
{
xs *req;
2022-09-21 17:28:30 +00:00
char *method;
int status = 0;
char *body = NULL;
int b_size = 0;
char *ctype = NULL;
xs *headers = NULL;
2022-09-21 17:46:02 +00:00
xs *q_path = NULL;
xs *payload = NULL;
int p_size = 0;
2022-09-21 17:46:02 +00:00
char *p;
2022-09-21 16:09:16 +00:00
req = xs_httpd_request(f, &payload, &p_size);
2022-09-21 16:09:16 +00:00
2022-09-28 14:27:53 +00:00
if (req == NULL) {
/* probably because a timeout */
fclose(f);
return;
}
method = xs_dict_get(req, "method");
q_path = xs_dup(xs_dict_get(req, "path"));
2022-09-21 17:46:02 +00:00
/* crop the q_path from leading / and the prefix */
if (xs_endswith(q_path, "/"))
q_path = xs_crop(q_path, 0, -1);
p = xs_dict_get(srv_config, "prefix");
if (xs_startswith(q_path, p))
q_path = xs_crop(q_path, strlen(p), 0);
2022-09-21 17:28:30 +00:00
if (strcmp(method, "GET") == 0) {
/* cascade through */
if (status == 0)
status = server_get_handler(req, q_path, &body, &b_size, &ctype);
2022-09-21 19:12:49 +00:00
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);
2022-09-28 03:22:08 +00:00
if (status == 0)
status = html_get_handler(req, q_path, &body, &b_size, &ctype);
2022-09-21 17:28:30 +00:00
}
else
if (strcmp(method, "POST") == 0) {
if (status == 0)
status = activitypub_post_handler(req, q_path,
payload, p_size, &body, &b_size, &ctype);
2022-09-28 03:22:08 +00:00
if (status == 0)
status = html_post_handler(req, q_path,
payload, p_size, &body, &b_size, &ctype);
2022-09-21 17:28:30 +00:00
}
/* let's go */
headers = xs_dict_new();
/* unattended? it's an error */
2022-09-28 03:22:08 +00:00
if (status == 0) {
srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
2022-09-21 17:28:30 +00:00
status = 404;
2022-09-28 03:22:08 +00:00
}
2022-09-21 17:28:30 +00:00
if (status == 404)
2022-09-21 17:46:02 +00:00
body = xs_str_new("<h1>404 Not Found</h1>");
2022-09-21 17:28:30 +00:00
if (status == 400)
2022-09-21 17:46:02 +00:00
body = xs_str_new("<h1>400 Bad Request</h1>");
2022-09-21 17:28:30 +00: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 16:01:51 +00:00
headers = xs_dict_append(headers, "x-creator", USER_AGENT);
2022-09-21 17:28:30 +00:00
if (b_size == 0 && body != NULL)
b_size = strlen(body);
xs_httpd_response(f, status, headers, body, b_size);
2022-09-21 16:09:16 +00:00
fclose(f);
2022-09-21 17:28:30 +00:00
2022-09-25 05:28:42 +00:00
srv_archive("RECV", req, payload, p_size, status, headers, body, b_size);
2022-09-24 10:22:17 +00:00
2022-09-21 17:28:30 +00:00
free(body);
2022-09-21 16:09:16 +00:00
}
2022-09-29 10:50:50 +00:00
static jmp_buf on_break;
void term_handler(int s)
{
longjmp(on_break, 1);
}
static void *queue_thread(void *arg)
/* queue thread (queue management) */
2022-10-01 18:57:06 +00:00
{
pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t dummy_cond = PTHREAD_COND_INITIALIZER;
srv_log(xs_fmt("queue thread start"));
2022-10-01 18:57:06 +00:00
while (srv_running) {
xs *list = user_list();
char *p, *uid;
p = list;
while (xs_list_iter(&p, &uid)) {
snac snac;
if (user_open(&snac, uid)) {
process_queue(&snac);
user_free(&snac);
}
}
/* sleep 3 seconds */
struct timespec ts;
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 18:57:06 +00:00
}
srv_log(xs_fmt("queue thread stop"));
2022-10-01 18:57:06 +00:00
return NULL;
}
static void *connection_thread(void *arg)
/* connection thread */
{
httpd_connection((FILE *)arg);
return NULL;
}
int threaded_connections = 1;
2022-09-21 16:09:16 +00:00
void httpd(void)
/* starts the server */
{
char *address;
int port;
int rs;
2022-10-01 18:57:06 +00:00
pthread_t htid;
2022-09-21 16:09:16 +00: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 16:13:11 +00:00
srv_running = 1;
2022-09-29 10:50:50 +00:00
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));
2022-09-21 16:09:16 +00:00
pthread_create(&htid, NULL, queue_thread, NULL);
2022-10-01 18:57:06 +00:00
2022-09-29 10:50:50 +00:00
if (setjmp(on_break) == 0) {
for (;;) {
2022-10-10 17:33:39 +00:00
FILE *f = xs_socket_accept(rs);
if (threaded_connections) {
pthread_t cth;
pthread_create(&cth, NULL, connection_thread, f);
}
else
httpd_connection(f);
2022-09-29 10:50:50 +00:00
}
2022-09-21 16:09:16 +00:00
}
2022-09-29 10:50:50 +00:00
srv_running = 0;
/* wait for the helper thread to end */
2022-10-01 18:57:06 +00:00
pthread_join(htid, NULL);
2022-09-29 10:50:50 +00:00
2022-09-21 16:09:16 +00:00
srv_log(xs_fmt("httpd stop %s:%d", address, port));
}