#include "gebs/gebs.h" #include "mongoose/mongoose.h" #include "cJSON/cJSON.h" #if MY_DEBUG #include "md5-c/md5.h" #endif #include "routes.h" #include "baked.h" #include "commit.h" #include "timer.h" extern char *clonestr_alloc(Allocator *alloc, char *s); #define INTERNAL_SERVER_ERROR_MSG "Internal server error ;(. Try again later..." #define META_TAGS \ "" \ "" \ "" \ "" void make_application_json(Allocator *alloc, Route_Result *result, int code, cJSON *root) { size_t buf_size = 512; char *buf = gebs_malloc(alloc, buf_size); cJSON_PrintPreallocated(root, buf, buf_size, false); result->status_code = code; result->type = ROUTE_RESULT_DYNAMIC; list_append(&result->headers, clonestr_alloc(alloc, "Content-Type: application/json")); sb_append_nstr_alloc(alloc, &result->body, buf); sb_finish_alloc(alloc, &result->body); } void make_text(Allocator *alloc, Route_Result *result, char *subtype, int code, char *in) { result->status_code = code; result->type = ROUTE_RESULT_DYNAMIC; list_append_alloc(alloc, &result->headers, clonestr_alloc(alloc, fmt("Content-Type: text/%s", subtype))); sb_append_nstr_alloc(alloc, &result->body, in); sb_finish(&result->body); } void make_internal_server_error(Allocator *alloc, Route_Result *result) { make_text(alloc, result, "plain", 500, INTERNAL_SERVER_ERROR_MSG); } bool gpp_run(Allocator *alloc, char *path, NString_List *env, String_Builder *out) { Cmd cmd = {0}; char gpp1[PATH_MAX]; if (!get_baked_resource_path("gpp1", gpp1, sizeof(gpp1))) { return false; } cmd_append_alloc(alloc, &cmd, gpp1); cmd_append_alloc(alloc, &cmd, "-H"); cmd_append_alloc(alloc, &cmd, "-x"); cmd_append_alloc(alloc, &cmd, "--nostdinc"); cmd_append_alloc(alloc, &cmd, path); for (size_t i = 0; i < env->count; i++) { cmd_append_alloc(alloc, &cmd, env->items[i]); } return cmd_run_collect_alloc(alloc, &cmd, out) == 0; } #if MY_DEBUG void route_build_id(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data) { // TODO: Somehow use our arena in here. This will require a slight rework of the way cJSON works. cJSON *root = cJSON_CreateObject(); defer { cJSON_Delete(root); } char *time = __TIME__; uchar md5_buf[16]; md5String(time, md5_buf); String_Builder sb = {0}; for (size_t i = 0; i < 16; i++) { sb_append_nstr_alloc(alloc, &sb, clonestr_alloc(alloc, fmt("%02x", md5_buf[i]))); } sb_finish_alloc(alloc, &sb); cJSON_AddItemToObject(root, "build_id", cJSON_CreateString(sb.items)); make_application_json(alloc, result, 200, root); } #endif void route_page_not_found(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data) { lock_baked_resources(); defer { unlock_baked_resources(); } NString_List env = {0}; list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf))); String_Builder out = {0}; char path[PATH_MAX] = {0}; if (!get_baked_resource_path("page-missing.html", path, sizeof(path))) { make_internal_server_error(alloc, result); return; } list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING))); char timer[100]; get_timer_string(timer, sizeof(timer)); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer))); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS))); #if MY_DEBUG list_append_alloc(alloc, &env, "-DHOTRELOAD"); #endif bool ok = gpp_run(alloc, path, &env, &out); sb_finish_alloc(alloc, &out); if (!ok) { make_internal_server_error(alloc, result); } else { make_text(alloc, result, "html", 200, out.items); } } void route_home(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data) { lock_baked_resources(); defer { unlock_baked_resources(); } NString_List env = {0}; String_Builder out = {0}; char path[PATH_MAX] = {0}; if (!get_baked_resource_path("home.html", path, sizeof(path))) { make_internal_server_error(alloc, result); return; } list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING))); char timer[100]; get_timer_string(timer, sizeof(timer)); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer))); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS))); #if MY_DEBUG list_append_alloc(alloc, &env, "-DHOTRELOAD"); #endif bool ok = gpp_run(alloc, path, &env, &out); sb_finish_alloc(alloc, &out); if (!ok) { make_internal_server_error(alloc, result); } else { make_text(alloc, result, "html", 200, out.items); } } void route_generic_blog(Allocator *alloc, struct mg_http_message *msg, Route_Result *result, void *context_data) { lock_baked_resources(); defer { unlock_baked_resources(); } NString_List env = {0}; String_Builder out = {0}; Baked_Resource *resource = (Baked_Resource *)context_data; char md_path[PATH_MAX] = {0}; if (!get_baked_resource_path(resource->key, md_path, sizeof(md_path))) { make_internal_server_error(alloc, result); return; } list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING))); char timer[100]; get_timer_string(timer, sizeof(timer)); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer))); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS))); #if MY_DEBUG list_append_alloc(alloc, &env, "-DHOTRELOAD"); #endif String_Builder md = {0}; if (!sb_read_file(&md, md_path)) { make_internal_server_error(alloc, result); return; } String_Builder md_prepared = {0}; for (size_t i = 0; i < md.count; i++) { char c = md.items[i]; switch (c) { case '\"': sb_append_nstr_alloc(alloc, &md_prepared, "\\\""); break; case '\'': sb_append_nstr_alloc(alloc, &md_prepared, "\\\'"); break; case '\\': sb_append_nstr_alloc(alloc, &md_prepared, "\\\\"); break; case '\a': sb_append_nstr_alloc(alloc, &md_prepared, "\\a"); break; case '\b': sb_append_nstr_alloc(alloc, &md_prepared, "\\b"); break; case '\r': sb_append_nstr_alloc(alloc, &md_prepared, "\\r"); break; case '\n': sb_append_nstr_alloc(alloc, &md_prepared, "\\n"); break; case '\t': sb_append_nstr_alloc(alloc, &md_prepared, "\\t"); break; default: if (iscntrl(md.items[i])) { sb_append_nstr_alloc(alloc, &md_prepared, clonestr_alloc(alloc, fmt("\\%03o", c))); } else { sb_append_char_alloc(alloc, &md_prepared, c); } break; } } sb_finish_alloc(alloc, &md_prepared); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DBLOG_POST_TITLE=%s", resource->key))); list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DBLOG_POST_MARKDOWN=%s", md_prepared.items))); char tmpl_file[PATH_MAX] = {0}; if (!get_baked_resource_path("template-blog.html", tmpl_file, sizeof(tmpl_file))) { make_internal_server_error(alloc, result); return; } bool ok = gpp_run(alloc, tmpl_file, &env, &out); sb_finish_alloc(alloc, &out); if (!ok) { make_internal_server_error(alloc, result); } else { make_text(alloc, result, "html", 200, out.items); } } void collect_blogs(Baked_Resource *resource, void *udata) { struct { Allocator *alloc; String_Builder *sb; } *ud = udata; if ((strlen(resource->key) >= strlen("blog-")) && strncmp(resource->key, "blog-", strlen("blog-")) == 0) { sb_append_nstr_alloc(ud->alloc, ud->sb, clonestr_alloc(ud->alloc, fmt("