From a636cf8b25d72900892b18191e0d68de10779e90 Mon Sep 17 00:00:00 2001 From: default Date: Wed, 28 Sep 2022 09:29:09 +0200 Subject: [PATCH] New function static_get(). --- data.c | 25 +++++++++++++++++++++++++ html.c | 42 +++++++++++++++++++++++++++++++++++++++++- snac.h | 2 ++ xs_httpd.h | 2 +- xs_io.h | 11 ++++++++--- 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/data.c b/data.c index 13cef84..b20dc9e 100644 --- a/data.c +++ b/data.c @@ -801,6 +801,31 @@ int actor_get(snac *snac, char *actor, d_char **data) } +d_char *_static_fn(snac *snac, char *id) +/* gets the filename for a static file */ +{ + return xs_fmt("%s/static/%s", snac->basedir, id); +} + + +int static_get(snac *snac, char *id, d_char **data, int *size) +/* returns static content */ +{ + xs *fn = _static_fn(snac, id); + FILE *f; + int status = 404; + + *size = 0xfffffff; + + if ((f = fopen(fn, "rb")) != NULL) { + *data = xs_read(f, size); + status = 200; + } + + return status; +} + + void enqueue_input(snac *snac, char *msg, char *req) /* enqueues an input message */ { diff --git a/html.c b/html.c index 7f8e990..89df82a 100644 --- a/html.c +++ b/html.c @@ -207,12 +207,52 @@ d_char *html_msg_icon(snac *snac, d_char *s, char *msg) } +d_char *html_user_header(snac *snac, d_char *s) +/* creates the HTML header */ +{ + char *p, *v; + + s = xs_str_cat(s, "\n\n\n"); + s = xs_str_cat(s, "\n"); + s = xs_str_cat(s, "\n"); + + /* add server CSS */ + p = xs_dict_get(srv_config, "cssurls"); + while (xs_list_iter(&p, &v)) { + xs *s1 = xs_fmt("\n", v); + s = xs_str_cat(s, s1); + } + + /* add the user CSS */ + { + xs *css = NULL; + int size; + + if (valid_status(static_get(snac, "style.css", &css, &size))) { + xs *s1 = xs_fmt("\n", css); + s = xs_str_cat(s, s1); + } + } + + { + xs *s1 = xs_fmt("%s\n", xs_dict_get(snac->config, "name")); + s = xs_str_cat(s, s1); + } + + s = xs_str_cat(s, "\n\n"); + + return s; +} + + d_char *html_timeline(snac *snac, char *list, int local) /* returns the HTML for the timeline */ { d_char *s = xs_str_new(NULL); - s = xs_str_cat(s, "\n\n"); + s = html_user_header(snac, s); s = xs_str_cat(s, "

HI

\n"); diff --git a/snac.h b/snac.h index b5a5a76..7cd46c7 100644 --- a/snac.h +++ b/snac.h @@ -78,6 +78,8 @@ int is_muted(snac *snac, char *actor); int actor_add(snac *snac, char *actor, char *msg); int actor_get(snac *snac, char *actor, d_char **data); +int static_get(snac *snac, char *id, d_char **data, int *size); + void enqueue_input(snac *snac, char *msg, char *req); void enqueue_output(snac *snac, char *msg, char *actor, int retries); diff --git a/xs_httpd.h b/xs_httpd.h index 0842e15..68f60c4 100644 --- a/xs_httpd.h +++ b/xs_httpd.h @@ -128,7 +128,7 @@ d_char *xs_httpd_request(FILE *f, d_char **payload, int *p_size) if ((v = xs_dict_get(req, "content-length")) != NULL) { /* if it has a payload, load it */ *p_size = atoi(v); - *payload = xs_read(f, *p_size); + *payload = xs_read(f, p_size); } /* is the payload form urlencoded variables? */ diff --git a/xs_io.h b/xs_io.h index 523d207..62af82c 100644 --- a/xs_io.h +++ b/xs_io.h @@ -6,7 +6,7 @@ d_char *xs_readall(FILE *f); d_char *xs_readline(FILE *f); -d_char *xs_read(FILE *f, int size); +d_char *xs_read(FILE *f, int *size); #ifdef XS_IMPLEMENTATION @@ -56,16 +56,18 @@ d_char *xs_readline(FILE *f) } -d_char *xs_read(FILE *f, int size) +d_char *xs_read(FILE *f, int *sz) /* reads up to size bytes from f */ { d_char *s; + int size = *sz; + int rdsz = 0; errno = 0; s = xs_str_new(NULL); - while (size != 0 && !feof(f)) { + while (size > 0 && !feof(f)) { char tmp[2048]; int n, r; @@ -76,8 +78,11 @@ d_char *xs_read(FILE *f, int size) s = xs_append_m(s, tmp, r); size -= r; + rdsz += r; } + *sz = rdsz; + return s; }