Files
aboba/main.c
2025-06-22 15:32:13 +02:00

292 lines
8.1 KiB
C

#include <libgen.h>
#include <pthread.h>
#include <ftw.h>
#include <signal.h>
#include <sys/sendfile.h>
#define GEBS_IMPLEMENTATION
#include "gebs/gebs.h"
#include "mongoose/mongoose.h"
#define STB_DS_IMPLEMENTATION
#include "stb/stb_ds.h"
#include "routes.h"
#include "baked.h"
#include "timer.h"
#include "CONFIG.h"
#include "clonestr.h"
#include "locked.h"
locked(Route *) route_hashtable = locked_init(nil);
locked(char *) etc_dump_path = locked_init(nil);
void run_in_thread(void *(*f)(void *), void *p)
{
pthread_t tid = 0;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, f, p);
pthread_attr_destroy(&attr);
}
void *route_thread_function(void *param)
{
Route_Thread_Data *data = (Route_Thread_Data *)param;
struct mg_http_message http_msg = {0};
int r = mg_http_parse(data->message.buf, data->message.len, &http_msg);
if (r <= 0) {
Route_Result result = {0};
result.status_code = 400;
result.type = ROUTE_RESULT_DYNAMIC;
/* list_append(&result.headers, clonestr("Content-Type: text/plain")); */
sb_append_nstr(&result.body, "Could not parse HTTP request");
sb_finish(&result.body);
mg_wakeup(data->mgr, data->conn_id, &result, sizeof(result));
free(data->message.buf);
free(data);
return nil;
}
if (mg_match(http_msg.uri, mg_str("/etc/*"), nil)) {
Route_Result result = {0};
result.type = ROUTE_RESULT_STATIC;
char *p = malloc(http_msg.uri.len+1);
strncpy(p, http_msg.uri.buf, http_msg.uri.len);
p[http_msg.uri.len] = 0;
char *file = basename(p);
char *full_path = malloc(PATH_MAX);
lockx(&etc_dump_path);
snprintf(full_path, PATH_MAX, "%s/%s", etc_dump_path.value, file);
unlockx(&etc_dump_path);
sb_append_nstr(&result.body, full_path);
sb_finish(&result.body);
free(full_path);
free(p);
mg_wakeup(data->mgr, data->conn_id, &result, sizeof(result));
free(data->message.buf);
free(data);
return nil;
}
char key[MG_PATH_MAX] = {0};
strncpy(key, http_msg.uri.buf, http_msg.uri.len);
lockx(&route_hashtable);
ssize_t idx = shgeti(route_hashtable.value, key);
Route_Result result = {0};
route_hashtable.value[idx].value(&http_msg, &result, route_hashtable.value[idx].context_data);
unlockx(&route_hashtable);
mg_wakeup(data->mgr, data->conn_id, &result, sizeof(result));
free(data->message.buf);
free(data);
return nil;
}
void event_handler(struct mg_connection *conn, int ev, void *ev_data)
{
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *msg = (struct mg_http_message *)ev_data;
Route_Thread_Data *data = calloc(1, sizeof(*data));
data->message = mg_strdup(msg->message);
data->conn_id = conn->id;
data->mgr = conn->mgr;
run_in_thread(&route_thread_function, data);
} else if (ev == MG_EV_WAKEUP) {
struct mg_str *data = (struct mg_str *)ev_data;
Route_Result *result = (Route_Result *)data->buf;
defer {
for (size_t i = 0; i < result->headers.count; i++) {
free(result->headers.items[i]);
}
list_free(&result->headers);
sb_free(&result->body);
}
Gebs_String_Builder sb = {0};
defer { sb_free(&sb); }
nsl_join(&result->headers, &sb, "\r\n");
if (result->headers.count > 0) {
sb_append_nstr(&sb, "\r\n");
}
sb_finish(&sb);
if (result->type == ROUTE_RESULT_DYNAMIC) {
mg_http_reply(conn, result->status_code, sb.items, "%s", result->body.items);
} else if (result->type == ROUTE_RESULT_STATIC) {
char *path = result->body.items;
mg_http_serve_file(conn, ev_data, path, &(struct mg_http_serve_opts){0});
}
}
}
void route_hashtable_put_blogs(Baked_Resource *resource, void *udata)
{
if ((strlen(resource->key) >= strlen("blog-"))
&& strncmp(resource->key, "blog-", strlen("blog-")) == 0) {
char path[MG_PATH_MAX];
snprintf(path, MG_PATH_MAX, "/%s", resource->key);
shput(route_hashtable.value, path, &route_generic_blog);
route_hashtable.value[shgeti(route_hashtable.value, path)].context_data = (void *)resource;
}
}
void init_route_hashtable(void)
{
lockx(&route_hashtable);
shdefault(route_hashtable.value, &route_page_not_found);
shput(route_hashtable.value, "/", &route_home);
shput(route_hashtable.value, "/page-missing", &route_page_not_found);
shput(route_hashtable.value, "/blog", &route_blog);
#if MY_DEBUG
shput(route_hashtable.value, "/build-id", &route_build_id);
#endif
baked_resource_each(&route_hashtable_put_blogs, nil);
unlockx(&route_hashtable);
}
void free_route_hashtable(void)
{
lockx(&route_hashtable);
shfree(route_hashtable.value);
unlockx(&route_hashtable);
}
char *init_etc_dump(void)
{
char template[] = "/tmp/aboba-etc.XXXXXX";
char *etc_dump1 = mkdtemp(template);
char *etc_dump = malloc(strlen(etc_dump1)+1);
strcpy(etc_dump, etc_dump1);
if (etc_dump == nil) {
LOGE("Could not create etc dump\n");
return nil;
}
LOGI("etc dump dir is %s\n", etc_dump);
return etc_dump;
}
int etc_dump_rm_cb(const char *path,
discard const struct stat *st,
discard int type,
discard struct FTW *ftw)
{
return remove(path);
}
bool free_etc_dump(char *etc_dump)
{
LOGI("Removing etc dump %s\n", etc_dump);
bool ok = nftw(etc_dump, etc_dump_rm_cb, FOPEN_MAX, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) != -1;
if (!ok) {
LOGE("Could not remove %s\n", etc_dump);
}
free(etc_dump);
return ok;
}
// https://stackoverflow.com/questions/2180079/how-can-i-copy-a-file-on-unix-using-c
int cp(const char* source, const char* destination)
{
int result = 0;
int input, output;
if ((input = open(source, O_RDONLY)) == -1)
{
return -1;
}
if ((output = creat(destination, 0660)) == -1)
{
close(input);
return -1;
}
// sendfile will work with non-socket output (i.e. regular file) under
// Linux 2.6.33+ and some other unixy systems.
struct stat file_stat = {0};
result = fstat(input, &file_stat);
off_t copied = 0;
while (result == 0 && copied < file_stat.st_size) {
ssize_t written = sendfile(output, input, &copied, SSIZE_MAX);
copied += written;
if (written == -1) {
result = -1;
}
}
close(input);
close(output);
return result;
}
void populate_etc_dump(char *etc_dump)
{
static char *files[] = {
"favicon.ico", "hotreload.js",
"simple.css", "me.jpg",
};
lock_baked_resources();
for (size_t i = 0; i < sizeof(files)/sizeof(files[0]); i++) {
char path[PATH_MAX];
get_baked_resource_path(files[i], path, sizeof(path));
char dest[PATH_MAX];
snprintf(dest, sizeof(dest), "%s/%s", etc_dump, files[i]);
cp(path, dest);
}
unlock_baked_resources();
}
volatile bool alive = true;
void graceful_shutdown(int no) { alive = false; }
int main(int argc, char ** argv)
{
signal(SIGINT, &graceful_shutdown);
start_timer();
init_baked_resources();
lockx(&etc_dump_path);
if ((etc_dump_path.value = init_etc_dump()) == nil) {
return 1;
}
populate_etc_dump(etc_dump_path.value);
unlockx(&etc_dump_path);
mg_log_set(MG_LL_DEBUG);
struct mg_mgr mgr;
mg_mgr_init(&mgr);
init_route_hashtable();
defer { free_route_hashtable(); }
mg_wakeup_init(&mgr);
mg_http_listen(&mgr, CONFIG_LISTEN_URL, &event_handler, NULL);
while (alive) {
mg_mgr_poll(&mgr, 1000);
scratch_arena_reset();
}
mg_mgr_free(&mgr);
lockx(&etc_dump_path);
free_etc_dump(etc_dump_path.value);
unlockx(&etc_dump_path);
free_baked_resources();
return 0;
}