Rework memory management, use per-thread arenas
This commit is contained in:
206
routes.c
206
routes.c
@ -7,10 +7,11 @@
|
||||
|
||||
#include "routes.h"
|
||||
#include "baked.h"
|
||||
#include "clonestr.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 \
|
||||
@ -19,65 +20,61 @@
|
||||
"<meta name=\"author\" content=\"kamkow1\">" \
|
||||
"<meta name=\"application-name\" content=\"aboba\">"
|
||||
|
||||
void make_internal_server_error(Route_Result *result)
|
||||
void make_application_json(Allocator *alloc, Route_Result *result, int code, cJSON *root)
|
||||
{
|
||||
result->status_code = 500;
|
||||
result->type = ROUTE_RESULT_DYNAMIC;
|
||||
list_append(&result->headers, "Content-Type: text/plain");
|
||||
sb_append_nstr(&result->body, INTERNAL_SERVER_ERROR_MSG);
|
||||
sb_finish(&result->body);
|
||||
}
|
||||
size_t buf_size = 512;
|
||||
char *buf = gebs_malloc(alloc, buf_size);
|
||||
|
||||
void make_application_json(Route_Result *result, int code, cJSON *root)
|
||||
{
|
||||
char *root_text = cJSON_PrintUnformatted(root);
|
||||
defer { free(root_text); }
|
||||
cJSON_PrintPreallocated(root, buf, buf_size, false);
|
||||
|
||||
result->status_code = code;
|
||||
result->type = ROUTE_RESULT_DYNAMIC;
|
||||
list_append(&result->headers, clonestr("Content-Type: application/json"));
|
||||
sb_append_nstr(&result->body, root_text);
|
||||
sb_finish(&result->body);
|
||||
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(Route_Result *result, char *subtype, int code, char *in)
|
||||
void make_text(Allocator *alloc, Route_Result *result, char *subtype, int code, char *in)
|
||||
{
|
||||
char type[100];
|
||||
snprintf(type, sizeof(type), "Content-Type: text/%s", subtype);
|
||||
|
||||
result->status_code = code;
|
||||
result->type = ROUTE_RESULT_DYNAMIC;
|
||||
list_append(&result->headers, clonestr(type));
|
||||
sb_append_nstr(&result->body, in);
|
||||
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);
|
||||
}
|
||||
|
||||
bool gpp_run(char *path, NString_List *env, String_Builder *out)
|
||||
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};
|
||||
defer { cmd_free(&cmd); }
|
||||
|
||||
char gpp1[PATH_MAX];
|
||||
if (!get_baked_resource_path("gpp1", gpp1, sizeof(gpp1))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cmd_append(&cmd, gpp1);
|
||||
cmd_append(&cmd, "-H");
|
||||
cmd_append(&cmd, "-x");
|
||||
cmd_append(&cmd, "--nostdinc");
|
||||
cmd_append(&cmd, path);
|
||||
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(&cmd, env->items[i]);
|
||||
cmd_append_alloc(alloc, &cmd, env->items[i]);
|
||||
}
|
||||
|
||||
return cmd_run_collect(&cmd, out) == 0;
|
||||
return cmd_run_collect_alloc(alloc, &cmd, out) == 0;
|
||||
}
|
||||
|
||||
#if MY_DEBUG
|
||||
void route_build_id(struct mg_http_message *msg, Route_Result *result, void *context_data)
|
||||
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); }
|
||||
|
||||
@ -85,226 +82,215 @@ void route_build_id(struct mg_http_message *msg, Route_Result *result, void *con
|
||||
uchar md5_buf[16];
|
||||
md5String(time, md5_buf);
|
||||
String_Builder sb = {0};
|
||||
defer { sb_free(&sb); }
|
||||
for (size_t i = 0; i < 16; i++) {
|
||||
sb_append_nstr(&sb, fmt("%02x", md5_buf[i]));
|
||||
sb_append_nstr_alloc(alloc, &sb, clonestr_alloc(alloc, fmt("%02x", md5_buf[i])));
|
||||
}
|
||||
sb_finish(&sb);
|
||||
sb_finish_alloc(alloc, &sb);
|
||||
|
||||
cJSON_AddItemToObject(root, "build_id", cJSON_CreateString(sb.items));
|
||||
|
||||
make_application_json(result, 200, root);
|
||||
make_application_json(alloc, result, 200, root);
|
||||
}
|
||||
#endif
|
||||
|
||||
void route_page_not_found(struct mg_http_message *msg, Route_Result *result, void *context_data)
|
||||
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};
|
||||
defer { list_free(&env); }
|
||||
|
||||
list_append(&env, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DURL=%.*s", msg->uri.len, msg->uri.buf)));
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("page-missing.html", path, sizeof(path))) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
list_append(&env, fmt("-DCOMMIT=%s", COMMIT_STRING));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING)));
|
||||
char timer[100];
|
||||
get_timer_string(timer, sizeof(timer));
|
||||
list_append(&env, fmt("-DRUNNING_SINCE=%s", timer));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer)));
|
||||
|
||||
list_append(&env, fmt("-DMETA_TAGS=%s", META_TAGS));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS)));
|
||||
|
||||
#if MY_DEBUG
|
||||
list_append(&env, "-DHOTRELOAD");
|
||||
list_append_alloc(alloc, &env, "-DHOTRELOAD");
|
||||
#endif
|
||||
|
||||
bool ok = gpp_run(path, &env, &out);
|
||||
sb_finish(&out);
|
||||
bool ok = gpp_run(alloc, path, &env, &out);
|
||||
sb_finish_alloc(alloc, &out);
|
||||
|
||||
if (!ok) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
} else {
|
||||
make_text(result, "html", 200, out.items);
|
||||
make_text(alloc, result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
||||
void route_home(struct mg_http_message *msg, Route_Result *result, void *context_data)
|
||||
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};
|
||||
defer { list_free(&env); }
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("home.html", path, sizeof(path))) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
list_append(&env, fmt("-DCOMMIT=%s", COMMIT_STRING));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING)));
|
||||
char timer[100];
|
||||
get_timer_string(timer, sizeof(timer));
|
||||
list_append(&env, fmt("-DRUNNING_SINCE=%s", timer));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer)));
|
||||
|
||||
list_append(&env, fmt("-DMETA_TAGS=%s", META_TAGS));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS)));
|
||||
|
||||
#if MY_DEBUG
|
||||
list_append(&env, "-DHOTRELOAD");
|
||||
list_append_alloc(alloc, &env, "-DHOTRELOAD");
|
||||
#endif
|
||||
|
||||
bool ok = gpp_run(path, &env, &out);
|
||||
sb_finish(&out);
|
||||
bool ok = gpp_run(alloc, path, &env, &out);
|
||||
sb_finish_alloc(alloc, &out);
|
||||
|
||||
if (!ok) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
} else {
|
||||
make_text(result, "html", 200, out.items);
|
||||
make_text(alloc, result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
||||
void route_generic_blog(struct mg_http_message *msg, Route_Result *result, void *context_data)
|
||||
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};
|
||||
defer { list_free(&env); }
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
|
||||
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(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
list_append(&env, fmt("-DCOMMIT=%s", COMMIT_STRING));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING)));
|
||||
char timer[100];
|
||||
get_timer_string(timer, sizeof(timer));
|
||||
list_append(&env, fmt("-DRUNNING_SINCE=%s", timer));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer)));
|
||||
|
||||
list_append(&env, fmt("-DMETA_TAGS=%s", META_TAGS));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS)));
|
||||
|
||||
#if MY_DEBUG
|
||||
list_append(&env, "-DHOTRELOAD");
|
||||
list_append_alloc(alloc, &env, "-DHOTRELOAD");
|
||||
#endif
|
||||
|
||||
String_Builder md = {0};
|
||||
defer { sb_free(&md); }
|
||||
if (!sb_read_file(&md, md_path)) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
String_Builder md_prepared = {0};
|
||||
defer { sb_free(&md_prepared); }
|
||||
for (size_t i = 0; i < md.count; i++) {
|
||||
char c = md.items[i];
|
||||
switch (c) {
|
||||
case '\"': sb_append_nstr(&md_prepared, "\\\""); break;
|
||||
case '\'': sb_append_nstr(&md_prepared, "\\\'"); break;
|
||||
case '\\': sb_append_nstr(&md_prepared, "\\\\"); break;
|
||||
case '\a': sb_append_nstr(&md_prepared, "\\a"); break;
|
||||
case '\b': sb_append_nstr(&md_prepared, "\\b"); break;
|
||||
case '\r': sb_append_nstr(&md_prepared, "\\r"); break;
|
||||
case '\n': sb_append_nstr(&md_prepared, "\\n"); break;
|
||||
case '\t': sb_append_nstr(&md_prepared, "\\t"); break;
|
||||
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(&md_prepared, fmt("\\%03o", c));
|
||||
sb_append_nstr_alloc(alloc, &md_prepared, clonestr_alloc(alloc, fmt("\\%03o", c)));
|
||||
} else {
|
||||
sb_append_char(&md_prepared, c);
|
||||
sb_append_char_alloc(alloc, &md_prepared, c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
sb_finish(&md_prepared);
|
||||
sb_finish_alloc(alloc, &md_prepared);
|
||||
|
||||
list_append(&env, fmt("-DBLOG_POST_TITLE=%s", resource->key));
|
||||
list_append(&env, fmt("-DBLOG_POST_MARKDOWN=%s", md_prepared.items));
|
||||
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(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
bool ok = gpp_run(tmpl_file, &env, &out);
|
||||
sb_finish(&out);
|
||||
bool ok = gpp_run(alloc, tmpl_file, &env, &out);
|
||||
sb_finish_alloc(alloc, &out);
|
||||
|
||||
if (!ok) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
} else {
|
||||
make_text(result, "html", 200, out.items);
|
||||
make_text(alloc, result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
||||
void collect_blogs(Baked_Resource *resource, void *udata)
|
||||
{
|
||||
struct { Arena *tmp; String_Builder *sb; } *ud = 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->tmp, ud->sb, fmt("<li><a href=\"/%s\">%s</a></li>", resource->key, resource->key));
|
||||
sb_append_nstr_alloc(ud->alloc, ud->sb,
|
||||
clonestr_alloc(ud->alloc, fmt("<li><a href=\"/%s\">%s</a></li>", resource->key, resource->key)));
|
||||
}
|
||||
}
|
||||
|
||||
void route_blog(struct mg_http_message *msg, Route_Result *result, void *context_data)
|
||||
void route_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};
|
||||
defer { list_free(&env); }
|
||||
|
||||
String_Builder out = {0};
|
||||
defer { sb_free(&out); }
|
||||
|
||||
Arena tmp = arena_get();
|
||||
defer { arena_destroy(&tmp); }
|
||||
|
||||
String_Builder sb = {0};
|
||||
struct { Arena *tmp; String_Builder *sb; } collect_blogs_data = {
|
||||
.tmp = &tmp,
|
||||
struct { Allocator *alloc; String_Builder *sb; } collect_blogs_data = {
|
||||
.alloc = alloc,
|
||||
.sb = &sb,
|
||||
};
|
||||
baked_resource_each(&collect_blogs, &collect_blogs_data);
|
||||
sb_finish_alloc(&tmp, &sb);
|
||||
list_append(&env, fmt("-DBLOG_POSTS=%s", sb.items));
|
||||
sb_finish_alloc(alloc, &sb);
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DBLOG_POSTS=%s", sb.items)));
|
||||
|
||||
char path[PATH_MAX] = {0};
|
||||
if (!get_baked_resource_path("blog.html", path, sizeof(path))) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
return;
|
||||
}
|
||||
|
||||
list_append(&env, fmt("-DCOMMIT=%s", COMMIT_STRING));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DCOMMIT=%s", COMMIT_STRING)));
|
||||
char timer[100];
|
||||
get_timer_string(timer, sizeof(timer));
|
||||
list_append(&env, fmt("-DRUNNING_SINCE=%s", timer));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DRUNNING_SINCE=%s", timer)));
|
||||
|
||||
list_append(&env, fmt("-DMETA_TAGS=%s", META_TAGS));
|
||||
list_append_alloc(alloc, &env, clonestr_alloc(alloc, fmt("-DMETA_TAGS=%s", META_TAGS)));
|
||||
|
||||
#if MY_DEBUG
|
||||
list_append(&env, "-DHOTRELOAD");
|
||||
list_append_alloc(alloc, &env, "-DHOTRELOAD");
|
||||
#endif
|
||||
|
||||
bool ok = gpp_run(path, &env, &out);
|
||||
sb_finish(&out);
|
||||
bool ok = gpp_run(alloc, path, &env, &out);
|
||||
sb_finish_alloc(alloc, &out);
|
||||
|
||||
if (!ok) {
|
||||
make_internal_server_error(result);
|
||||
make_internal_server_error(alloc, result);
|
||||
} else {
|
||||
make_text(result, "html", 200, out.items);
|
||||
make_text(alloc, result, "html", 200, out.items);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user