bunkum

an old and silly c99 web server with some fun features
Log | Files | Refs | README

commit b1958060de24825479d759637e48b944c491d6dc
parent d1a5f8e0b9375add30c53d8732ffe708906dbd1e
Author: quantumish <freifeld.david@gmail.com>
Date:   Fri, 28 Apr 2023 22:52:33 -0700

Begin generalizing HTML utils

Diffstat:
MMakefile | 2+-
Mhttp.c | 33+++++++++++++++------------------
Mutils/html.c | 8+++++---
Mutils/html.h | 3++-
4 files changed, 23 insertions(+), 23 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,4 @@ -CFLAGS?= -g +CFLAGS?= -g -std=c99 LIBS:=-lz -lpthread -lunwind -lunwind-ptrace -lunwind-generic CLIBS = $(wildcard utils/*.c) $(wildcard http/*.c) OBJ = $(wildcard build/*.o) diff --git a/http.c b/http.c @@ -46,10 +46,8 @@ response_t serve_error(enum StatusCode c) { sprintf(errt, "Error %d", c); html_t html = html_new(); - html_fc_t err = html_h1_new(errt); - html_fc_t desc = html_p_new("Hey! Don't do that."); - html_body_add(&html.body, &err); - html_body_add(&html.body, &desc); + html_body_add(&html.body, html_h1_new(errt)); + html_body_add(&html.body, html_p_new("Hey! Don't do that.")); char* msg = html_render(&html); resp_add_content(&r, msg, strlen(msg)); @@ -144,13 +142,13 @@ response_t make_response (request_t* req, channel_t* chan) { return serve_error(BadRequest); } - /* if (hashmap_get(&req->headers, "Profile") != 0x0) { */ - /* int profile = true; */ - /* channel_push(chan, &profile); */ - /* ptrace(PTRACE_TRACEME, NULL); */ - /* usleep(10000); */ - /* } */ - + if (hashmap_get(&req->headers, "Profile") != 0x0) { + int profile = true; + channel_push(chan, &profile); + ptrace(PTRACE_TRACEME, NULL); + /* usleep(10000); */ + } + log_info("Got request %s %s", method_name(req->method), req->path); char* mapped_path = hashmap_get(&path_redirs, req->path); @@ -252,7 +250,7 @@ int profile(pid_t tid) { errno = 0; if (ptrace(PTRACE_ATTACH, tid) < 0) { log_error("ptrace() fail, errno %d", errno); - } + } void* ui = _UPT_create(tid); if (!ui) return -1; unw_cursor_t c; @@ -311,7 +309,7 @@ int main() { int* ns; pid_t* tid; - while (true) { + while (true) { if ((ns = channel_try_recv(&listen_chan))) { channel_t chan = channel_new(sizeof(pid_t)); shitvec_push(&channels, &chan); @@ -323,11 +321,10 @@ int main() { for (size_t i = 0; i < channels.vec_sz; i++) { struct thread_comm* comm = shitvec_get(&channels, i); if ((tid = channel_try_recv(&comm->chan))) { - /* for (int j = 0; j < 10; j++) { */ - /* profile(*tid); */ - /* usleep(10); */ - /* } */ - /* } else comm->tid = *tid; */ + for (int j = 0; j < 10; j++) { + profile(*tid); + usleep(10); + } /* else comm->tid = *tid; */ } } } diff --git a/utils/html.c b/utils/html.c @@ -23,9 +23,9 @@ html_fc_t html_h1_new(char* content) { out.value.text = content; return out; } - -void html_body_add(html_body_t* body, html_fc_t* fc) { - shitvec_push(&body->content, fc); + +void html_body_add(html_body_t* body, html_fc_t fc) { + shitvec_push(&body->content, &fc); } void c_sv_pushs(shitvec_t* sv, char* str) { @@ -40,6 +40,8 @@ char* html_render(html_t* html) { for (int i = 0; i < html->body.content.vec_sz; i++) { html_fc_t* elem = shitvec_get(&html->body.content, i); switch (elem->type) { + case HTML_TEXT: + case HTML_P: c_sv_pushs(&html->_buf, "<p>"); c_sv_pushs(&html->_buf, elem->value.text); diff --git a/utils/html.h b/utils/html.h @@ -4,6 +4,7 @@ #include "shitvec.h" enum html_fc_type { + HTML_TEXT, HTML_A, HTML_P, HTML_BR, @@ -43,7 +44,7 @@ typedef struct html { html_t html_new(); html_fc_t html_p_new(char* content); html_fc_t html_h1_new(char* content); -void html_body_add(html_body_t* body, html_fc_t* fc); +void html_body_add(html_body_t* body, html_fc_t fc); char* html_render(html_t* html); #endif