The CGit configuration variable virtual_root is normalized so that it
does not have a trailing '/' character, but it is allowed to be empty
(the empty string and NULL have different meanings here) and there is
code that is insufficiently cautious when checking if it ends in a '/':
if (virtual_root[strlen(virtual_root) - 1] != '/')
Clearly this check is redundant, but rather than simply removing it we
get a slight efficiency improvement by switching the normalization so
that the virtual_root variable always ends in '/'. Do this with a new
"ensure_end" helper.
Signed-off-by: John Keeping <john@keeping.me.uk>
else if (!strcmp(name, "strict-export"))
ctx.cfg.strict_export = xstrdup(value);
else if (!strcmp(name, "virtual-root")) {
else if (!strcmp(name, "strict-export"))
ctx.cfg.strict_export = xstrdup(value);
else if (!strcmp(name, "virtual-root")) {
- ctx.cfg.virtual_root = trim_end(value, '/');
- if (!ctx.cfg.virtual_root && (!strcmp(value, "/")))
- ctx.cfg.virtual_root = "";
+ ctx.cfg.virtual_root = ensure_end(value, '/');
} else if (!strcmp(name, "nocache"))
ctx.cfg.nocache = atoi(value);
else if (!strcmp(name, "noplainemail"))
} else if (!strcmp(name, "nocache"))
ctx.cfg.nocache = atoi(value);
else if (!strcmp(name, "noplainemail"))
* that virtual-root equals SCRIPT_NAME, minus any possibly
* trailing slashes.
*/
* that virtual-root equals SCRIPT_NAME, minus any possibly
* trailing slashes.
*/
- if (!ctx.cfg.virtual_root && ctx.cfg.script_name) {
- ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/');
- if (!ctx.cfg.virtual_root)
- ctx.cfg.virtual_root = "";
- }
+ if (!ctx.cfg.virtual_root && ctx.cfg.script_name)
+ ctx.cfg.virtual_root = ensure_end(ctx.cfg.script_name, '/');
/* If no url parameter is specified on the querystring, lets
* use PATH_INFO as url. This allows cgit to work with virtual
/* If no url parameter is specified on the querystring, lets
* use PATH_INFO as url. This allows cgit to work with virtual
char *script_name;
char *section;
char *repository_sort;
char *script_name;
char *section;
char *repository_sort;
+ char *virtual_root; /* Always ends with '/'. */
char *strict_export;
int cache_size;
int cache_dynamic_ttl;
char *strict_export;
int cache_size;
int cache_dynamic_ttl;
extern int chk_non_negative(int result, char *msg);
extern char *trim_end(const char *str, char c);
extern int chk_non_negative(int result, char *msg);
extern char *trim_end(const char *str, char c);
+extern char *ensure_end(const char *str, char c);
extern char *strlpart(char *txt, int maxlen);
extern char *strrpart(char *txt, int maxlen);
extern char *strlpart(char *txt, int maxlen);
extern char *strrpart(char *txt, int maxlen);
return xstrndup(str, len);
}
return xstrndup(str, len);
}
+char *ensure_end(const char *str, char c)
+{
+ size_t len = strlen(str);
+ char *result;
+
+ if (len && str[len - 1] == c)
+ return xstrndup(str, len);
+
+ result = xmalloc(len + 2);
+ memcpy(result, str, len);
+ result[len] = '/';
+ result[len + 1] = '\0';
+ return result;
+}
+
char *strlpart(char *txt, int maxlen)
{
char *result;
char *strlpart(char *txt, int maxlen)
{
char *result;
const char *cgit_rooturl()
{
if (ctx.cfg.virtual_root)
const char *cgit_rooturl()
{
if (ctx.cfg.virtual_root)
- return fmt("%s/", ctx.cfg.virtual_root);
+ return ctx.cfg.virtual_root;
else
return ctx.cfg.script_name;
}
else
return ctx.cfg.script_name;
}
char *cgit_repourl(const char *reponame)
{
if (ctx.cfg.virtual_root) {
char *cgit_repourl(const char *reponame)
{
if (ctx.cfg.virtual_root) {
- return fmt("%s/%s/", ctx.cfg.virtual_root, reponame);
+ return fmt("%s%s/", ctx.cfg.virtual_root, reponame);
} else {
return fmt("?r=%s", reponame);
}
} else {
return fmt("?r=%s", reponame);
}
char *delim;
if (ctx.cfg.virtual_root) {
char *delim;
if (ctx.cfg.virtual_root) {
- tmp = fmt("%s/%s/%s/%s", ctx.cfg.virtual_root, reponame,
+ tmp = fmt("%s%s/%s/%s", ctx.cfg.virtual_root, reponame,
pagename, (filename ? filename:""));
delim = "?";
} else {
pagename, (filename ? filename:""));
delim = "?";
} else {
- if (ctx.cfg.virtual_root) {
+ if (ctx.cfg.virtual_root)
html_attr(ctx.cfg.virtual_root);
html_attr(ctx.cfg.virtual_root);
- if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
- html("/");
- } else
html(ctx.cfg.script_name);
if (page) {
html(ctx.cfg.script_name);
if (page) {
html(" href='");
if (ctx.cfg.virtual_root) {
html_url_path(ctx.cfg.virtual_root);
html(" href='");
if (ctx.cfg.virtual_root) {
html_url_path(ctx.cfg.virtual_root);
- if (ctx.cfg.virtual_root[strlen(ctx.cfg.virtual_root) - 1] != '/')
- html("/");
html_url_path(ctx.repo->url);
if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
html("/");
html_url_path(ctx.repo->url);
if (ctx.repo->url[strlen(ctx.repo->url) - 1] != '/')
html("/");