#include #include #include #include #include #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" static locked(Route *) route_hashtable = locked_init(nil); static locked(char *) baked_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("/bakedres/*"), 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(&baked_dump_path); snprintf(full_path, PATH_MAX, "%s/%s", baked_dump_path.value, file); unlockx(&baked_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 = malloc(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, clonestr("/"), &route_home); shput(route_hashtable.value, clonestr("/page-missing"), &route_page_not_found); shput(route_hashtable.value, clonestr("/blog"), &route_blog); #if MY_DEBUG shput(route_hashtable.value, clonestr("/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); for (size_t i = 0; i < shlen(route_hashtable.value); i++) { free(route_hashtable.value[i].key); } shfree(route_hashtable.value); unlockx(&route_hashtable); } char *init_baked_dump(void) { char template[] = "/tmp/aboba-bakedres.XXXXXX"; char *baked_dump1 = mkdtemp(template); char *baked_dump = malloc(strlen(baked_dump1)+1); strcpy(baked_dump, baked_dump1); if (baked_dump == nil) { LOGE("Could not create bakedres dump\n"); return nil; } LOGI("bakedres dump dir is %s\n", baked_dump); return baked_dump; } int baked_dump_rm_cb(const char *path, discard const struct stat *st, discard int type, discard struct FTW *ftw) { return remove(path); } bool free_baked_dump(char *baked_dump) { LOGI("Removing bakedres dump %s\n", baked_dump); bool ok = nftw(baked_dump, baked_dump_rm_cb, FOPEN_MAX, FTW_DEPTH | FTW_MOUNT | FTW_PHYS) != -1; if (!ok) { LOGE("Could not remove %s\n", baked_dump); } free(baked_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 copy_baked_resources_to_baked_dump(Baked_Resource *resource, void *udata) { char *baked_dump = (char *)udata; char path[PATH_MAX]; get_baked_resource_path(resource->key, path, sizeof(path)); char dest[PATH_MAX]; snprintf(dest, sizeof(dest), "%s/%s", baked_dump, resource->key); cp(path, dest); } void populate_baked_dump(char *baked_dump) { lock_baked_resources(); baked_resource_each(©_baked_resources_to_baked_dump, baked_dump); 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(&baked_dump_path); if ((baked_dump_path.value = init_baked_dump()) == nil) { return 1; } populate_baked_dump(baked_dump_path.value); unlockx(&baked_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(&baked_dump_path); free_baked_dump(baked_dump_path.value); unlockx(&baked_dump_path); free_baked_resources(); return 0; }