fix: inject <base href> as first child of <head>, before any asset links

This commit is contained in:
2026-06-09 01:52:36 +02:00
parent f82c415f07
commit 3c1be9420d
+10 -11
View File
@@ -77,18 +77,17 @@ int parse_request(int fd, Req *req) {
void send_page(int fd, const char *title, Buf *body, const char *script_src) { void send_page(int fd, const char *title, Buf *body, const char *script_src) {
Buf page; buf_init(&page); Buf page; buf_init(&page);
buf_str(&page, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n"); buf_str(&page, "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\n\r\n");
/* inject <base href> so relative assets resolve correctly at any sub-path */ /* inject <base href> immediately after <head> so it precedes all asset links */
const char *ph = strstr(g_header, "{{TITLE}}"); const char *ph = strstr(g_header, "{{TITLE}}");
const char *head_end = strstr(g_header, "</head>"); const char *head_tag = strstr(g_header, "<head>");
if (ph && head_end && head_end > ph) { if (ph && head_tag && g_cfg.base_path[0]) {
/* write up to </head>, inject base tag, then rest of header */ const char *after_head = head_tag + 6; /* skip "<head>" */
buf_append(&page, g_header, ph - g_header); buf_append(&page, g_header, after_head - g_header);
buf_fmt(&page, "<base href=\"%s/\">", g_cfg.base_path);
/* now write from after <head> up to {{TITLE}}, substitute title, then rest */
buf_append(&page, after_head, ph - after_head);
buf_str(&page, title); buf_str(&page, title);
const char *after_title = ph + 9; buf_str(&page, ph + 9);
buf_append(&page, after_title, head_end - after_title);
if (g_cfg.base_path[0])
buf_fmt(&page, "<base href=\"%s/\">", g_cfg.base_path);
buf_str(&page, head_end);
} else if (ph) { } else if (ph) {
buf_append(&page, g_header, ph - g_header); buf_append(&page, g_header, ph - g_header);
buf_str(&page, title); buf_str(&page, title);