New function static_get().

This commit is contained in:
default 2022-09-28 09:29:09 +02:00
parent 1d19464a48
commit a636cf8b25
5 changed files with 77 additions and 5 deletions

25
data.c
View File

@ -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 */
{

42
html.c
View File

@ -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, "<!DOCTYPE html>\n<html>\n<head>\n");
s = xs_str_cat(s, "<meta name=\"viewport\" "
"content=\"width=device-width, initial-scale=1\"/>\n");
s = xs_str_cat(s, "<meta name=\"generator\" "
"content=\"" USER_AGENT "\"/>\n");
/* add server CSS */
p = xs_dict_get(srv_config, "cssurls");
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\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("<style>%s</style>\n", css);
s = xs_str_cat(s, s1);
}
}
{
xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "</head>\n<body>\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, "<!DOCTYPE html>\n<html>\n");
s = html_user_header(snac, s);
s = xs_str_cat(s, "<h1>HI</h1>\n");

2
snac.h
View File

@ -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);

View File

@ -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? */

11
xs_io.h
View File

@ -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;
}