aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-09 01:20:18 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2023-03-09 01:20:18 +0100
commit8016f537ca5a4bbde6fee2566ff82cd222122915 (patch)
tree4c39011cff809c091cb6f55fef58d851030a7e36
parenta4c12e7ccc11f5f15e0e81db0be2a908eab5b141 (diff)
downloadslcl-8016f537ca5a4bbde6fee2566ff82cd222122915.tar.gz
page.c: Implement function for common <head> nodes
This will be used by future commits.
-rw-r--r--page.c89
1 files changed, 55 insertions, 34 deletions
diff --git a/page.c b/page.c
index 1c6023d..03671d6 100644
--- a/page.c
+++ b/page.c
@@ -14,16 +14,17 @@
#include <string.h>
#include <time.h>
-#define PROJECT_TITLE "<title>slcl, a suckless cloud</title>"
-#define DOCTYPE_TAG "<!DOCTYPE html>\n"
#define PROJECT_NAME "slcl"
+#define PROJECT_TITLE PROJECT_NAME ", a suckless cloud"
+#define PROJECT_TAG "<title>" PROJECT_TITLE "</title>"
+#define DOCTYPE_TAG "<!DOCTYPE html>\n"
#define PROJECT_URL "https://gitea.privatedns.org/Xavi92/" PROJECT_NAME
#define COMMON_HEAD \
" <meta charset=\"UTF-8\">\n" \
" <meta name=\"viewport\"\n" \
" content=\"width=device-width, initial-scale=1,\n" \
" maximum-scale=1\">\n" \
- PROJECT_TITLE "\n"
+ PROJECT_TAG "\n"
#define STYLE_A "<link href=\"/style.css\" rel=\"stylesheet\">"
#define LOGIN_BODY \
"<header>\n" \
@@ -590,37 +591,15 @@ end:
return ret;
}
-static struct html_node *resource_layout(const char *const dir,
- const struct page_quota *const q, struct html_node **const table)
+static int common_head(struct html_node *const head, const char *const tl)
{
- const char *const fdir = dir + strlen("/user");
- struct html_node *const html = html_node_alloc("html"),
- *ret = NULL, *head, *title, *body, *charset, *viewport;
+ int ret = -1;
+ struct html_node *charset, *title, *viewport;
struct dynstr t;
dynstr_init(&t);
- if (!html)
- {
- fprintf(stderr, "%s: html_node_alloc_failed\n", __func__);
- goto end;
- }
- else if (!(head = html_node_add_child(html, "head")))
- {
- fprintf(stderr, "%s: html_node_add_child head failed\n", __func__);
- goto end;
- }
- else if (!(body = html_node_add_child(html, "body")))
- {
- fprintf(stderr, "%s: html_node_add_child body failed\n", __func__);
- goto end;
- }
- else if (!(*table = html_node_add_child(body, "table")))
- {
- fprintf(stderr, "%s: html_node_add_child table failed\n", __func__);
- goto end;
- }
- else if (!(title = html_node_add_child(head, "title")))
+ if (!(title = html_node_add_child(head, "title")))
{
fprintf(stderr, "%s: html_node_add_child title failed\n", __func__);
goto end;
@@ -635,12 +614,15 @@ static struct html_node *resource_layout(const char *const dir,
fprintf(stderr, "%s: html_node_add_attr charset failed\n", __func__);
goto end;
}
- else if (dynstr_append(&t, PROJECT_NAME " - %s", fdir))
+ else if (tl && dynstr_append(&t, PROJECT_NAME " - %s", tl))
{
fprintf(stderr, "%s: dynstr_append title failed\n", __func__);
goto end;
}
- else if (html_node_set_value(title, t.str))
+
+ const char *const value = tl ? t.str : PROJECT_TITLE;
+
+ if (html_node_set_value(title, value))
{
fprintf(stderr, "%s: html_node_set_value title failed\n", __func__);
goto end;
@@ -663,6 +645,46 @@ static struct html_node *resource_layout(const char *const dir,
__func__);
goto end;
}
+
+ ret = 0;
+
+end:
+ dynstr_free(&t);
+ return ret;
+}
+
+static struct html_node *resource_layout(const char *const dir,
+ const struct page_quota *const q, struct html_node **const table)
+{
+ const char *const fdir = dir + strlen("/user");
+ struct html_node *const html = html_node_alloc("html"),
+ *ret = NULL, *head, *body;
+
+ if (!html)
+ {
+ fprintf(stderr, "%s: html_node_alloc_failed\n", __func__);
+ goto end;
+ }
+ else if (!(head = html_node_add_child(html, "head")))
+ {
+ fprintf(stderr, "%s: html_node_add_child head failed\n", __func__);
+ goto end;
+ }
+ else if (!(body = html_node_add_child(html, "body")))
+ {
+ fprintf(stderr, "%s: html_node_add_child body failed\n", __func__);
+ goto end;
+ }
+ else if (!(*table = html_node_add_child(body, "table")))
+ {
+ fprintf(stderr, "%s: html_node_add_child table failed\n", __func__);
+ goto end;
+ }
+ else if (common_head(head, fdir))
+ {
+ fprintf(stderr, "%s: common_head failed\n", __func__);
+ goto end;
+ }
else if (prepare_upload_form(body, fdir))
{
fprintf(stderr, "%s: prepare_upload_form failed\n", __func__);
@@ -692,7 +714,6 @@ static struct html_node *resource_layout(const char *const dir,
ret = html;
end:
- dynstr_free(&t);
if (!ret)
html_node_free(html);
@@ -914,7 +935,7 @@ int page_forbidden(struct http_response *const r)
DOCTYPE_TAG
"<html>\n"
" <head>\n"
- " " PROJECT_TITLE "\n"
+ " " PROJECT_TAG "\n"
" " COMMON_HEAD "\n"
" </head>\n"
"Forbidden\n"
@@ -942,7 +963,7 @@ int page_bad_request(struct http_response *const r)
DOCTYPE_TAG
"<html>\n"
" <head>"
- " " PROJECT_TITLE "\n"
+ " " PROJECT_TAG "\n"
" </head>"
"Invalid request\n"
"</html>";