author | Lars Hjemli <hjemli@gmail.com> | 2008-08-09 19:11:41 (UTC) |
---|---|---|
committer | Lars Hjemli <hjemli@gmail.com> | 2008-09-15 21:00:30 (UTC) |
commit | e154edd8078020d6eba41b448afade0a68617f35 (patch) (side-by-side diff) | |
tree | 9fdde0c3944e327ed2694464a4c6808373ee09b7 | |
parent | a30453a5d3f6db4d6e055da2112343e054e7e7d5 (diff) | |
download | cgit-e154edd8078020d6eba41b448afade0a68617f35.zip cgit-e154edd8078020d6eba41b448afade0a68617f35.tar.gz cgit-e154edd8078020d6eba41b448afade0a68617f35.tar.bz2 |
Teach cgit how to use PATH_INFO
This commit makes cgit use the cgi variables SCRIPT_NAME and PATH_INFO
when virtual-root is unspecified in cgitrc and no url-parameter is
specified on the querystring. This has two nice effects:
* Virtual urls works out of the box, no more need for rewrite-rules in httpd.
* Virtual urls with special querystring characters are handled correctly.
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
-rw-r--r-- | cgit.c | 27 | ||||
-rw-r--r-- | cgit.h | 1 |
2 files changed, 28 insertions, 0 deletions
@@ -117,32 +117,33 @@ void config_cb(const char *name, const char *value) if (*value == '/') ctx.repo->readme = xstrdup(value); else ctx.repo->readme = xstrdup(fmt("%s/%s", ctx.repo->path, value)); } else if (!strcmp(name, "include")) parse_configfile(value, config_cb); } static void querystring_cb(const char *name, const char *value) { if (!strcmp(name,"r")) { ctx.qry.repo = xstrdup(value); ctx.repo = cgit_get_repoinfo(value); } else if (!strcmp(name, "p")) { ctx.qry.page = xstrdup(value); } else if (!strcmp(name, "url")) { + ctx.qry.url = xstrdup(value); cgit_parse_url(value); } else if (!strcmp(name, "qt")) { ctx.qry.grep = xstrdup(value); } else if (!strcmp(name, "q")) { ctx.qry.search = xstrdup(value); } else if (!strcmp(name, "h")) { ctx.qry.head = xstrdup(value); ctx.qry.has_symref = 1; } else if (!strcmp(name, "id")) { ctx.qry.sha1 = xstrdup(value); ctx.qry.has_sha1 = 1; } else if (!strcmp(name, "id2")) { ctx.qry.sha2 = xstrdup(value); ctx.qry.has_sha1 = 1; } else if (!strcmp(name, "ofs")) { ctx.qry.ofs = atoi(value); @@ -360,44 +361,70 @@ static int calc_ttl() if (!ctx.qry.page) return ctx.cfg.cache_repo_ttl; if (ctx.qry.has_symref) return ctx.cfg.cache_dynamic_ttl; if (ctx.qry.has_sha1) return ctx.cfg.cache_static_ttl; return ctx.cfg.cache_repo_ttl; } int main(int argc, const char **argv) { const char *cgit_config_env = getenv("CGIT_CONFIG"); + const char *path; + char *qry; int err, ttl; prepare_context(&ctx); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; parse_configfile(cgit_config_env ? cgit_config_env : CGIT_CONFIG, config_cb); ctx.repo = NULL; if (getenv("SCRIPT_NAME")) ctx.cfg.script_name = xstrdup(getenv("SCRIPT_NAME")); if (getenv("QUERY_STRING")) ctx.qry.raw = xstrdup(getenv("QUERY_STRING")); cgit_parse_args(argc, argv); http_parse_querystring(ctx.qry.raw, querystring_cb); + /* If virtual-root isn't specified in cgitrc and no url + * parameter is specified on the querystring, lets pretend + * that virtualroot equals SCRIPT_NAME and use PATH_INFO as + * url. This allows cgit to work with virtual urls without + * the need for rewriterules in the webserver (as long as + * PATH_INFO is included in the cache lookup key). + */ + if (!ctx.cfg.virtual_root && !ctx.qry.url) { + ctx.cfg.virtual_root = ctx.cfg.script_name; + path = getenv("PATH_INFO"); + if (path) { + if (path[0] == '/') + path++; + ctx.qry.url = xstrdup(path); + if (ctx.qry.raw) { + qry = ctx.qry.raw; + ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); + free(qry); + } else + ctx.qry.raw = ctx.qry.url; + cgit_parse_url(ctx.qry.url); + } + } + ttl = calc_ttl(); ctx.page.expires += ttl*60; if (ctx.cfg.nocache) ctx.cfg.cache_size = 0; err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, ctx.qry.raw, ttl, process_request, &ctx); if (err) cgit_print_error(fmt("Error processing page: %s (%d)", strerror(err), err)); return err; } @@ -105,32 +105,33 @@ struct reflist { }; struct cgit_query { int has_symref; int has_sha1; char *raw; char *repo; char *page; char *search; char *grep; char *head; char *sha1; char *sha2; char *path; char *name; char *mimetype; + char *url; int ofs; }; struct cgit_config { char *agefile; char *cache_root; char *clone_prefix; char *css; char *favicon; char *footer; char *index_header; char *index_info; char *logo; char *logo_link; char *module_link; char *repo_group; |