From 18001c3f79751528a1b27dca14089e9b0fa540d1 Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 31 Oct 2026 13:15:01 -0700 Subject: Set the virtual root from SCRIPT_NAME up front The script name always holds a value, so the fallback after parsing fired whenever cgitrc left virtual-root unset. The root is now set with the other defaults, for cgitrc to replace. A die while cgitrc is read is the one change, and its page now links the root with the trailing slash every other page has. The environment's copy of the script name only fed this. --- source/cgit.c | 11 +++++------ source/cgit.h | 1 - 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'source') diff --git a/source/cgit.c b/source/cgit.c index eaf1d19..715db59 100644 --- a/source/cgit.c +++ b/source/cgit.c @@ -91,6 +91,8 @@ static NORETURN void die_routine(const char *msg, va_list params) static void prepare_context(void) { + const char *script_name = getenv("SCRIPT_NAME"); + memset(&ctx, 0, sizeof(ctx)); ctx.cfg.agefile = "info/web/last-modified"; ctx.cfg.cache_size = 0; @@ -155,7 +157,6 @@ static void prepare_context(void) ctx.env.path_info = getenv("PATH_INFO"); ctx.env.query_string = getenv("QUERY_STRING"); ctx.env.request_method = getenv("REQUEST_METHOD"); - ctx.env.script_name = getenv("SCRIPT_NAME"); ctx.env.server_name = getenv("SERVER_NAME"); ctx.env.server_port = getenv("SERVER_PORT"); ctx.env.http_cookie = getenv("HTTP_COOKIE"); @@ -168,8 +169,9 @@ static void prepare_context(void) ctx.page.filename = NULL; ctx.page.size = 0; string_list_init_dup(&ctx.cfg.mimetypes); - if (ctx.env.script_name) - ctx.cfg.script_name = xstrdup(ctx.env.script_name); + if (script_name) + ctx.cfg.script_name = xstrdup(script_name); + ctx.cfg.virtual_root = cgit_ensure_end(ctx.cfg.script_name, '/'); if (ctx.env.query_string) ctx.qry.raw = xstrdup(ctx.env.query_string); if (!ctx.env.cgit_config) @@ -1349,9 +1351,6 @@ int cmd_main(int argc, const char **argv) ctx.repo = NULL; http_parse_querystring(ctx.qry.raw, apply_query_param); - if (!ctx.cfg.virtual_root && ctx.cfg.script_name) - ctx.cfg.virtual_root = cgit_ensure_end(ctx.cfg.script_name, '/'); - // Falling back to PATH_INFO lets cgit serve virtual urls without a // rewrite rule in the web server, and folding it into the raw query // string keeps it part of the cache key. diff --git a/source/cgit.h b/source/cgit.h index 5e17a3e..a089633 100644 --- a/source/cgit.h +++ b/source/cgit.h @@ -303,7 +303,6 @@ struct cgit_environment { const char *path_info; const char *query_string; const char *request_method; - const char *script_name; const char *server_name; const char *server_port; const char *http_cookie; -- cgit