Author: Jason A. Donenfeld <Jason@zx2c4.com>
auth-filter: pass url with query string attached Otherwise redirections come out wrong. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
cgit.c | 2 +- ui-shared.c | 37 +++++++++++++++++++++++++++++++++++-- ui-shared.h | 1 +
diff --git a/cgit.c b/cgit.c index 6301b87c8ad3d29826e0fc585d6e494bcc9990bd..2f07e6d5c88ddb9a9647da519d5044513f39a5e4 100644 --- a/cgit.c +++ b/cgit.c @@ -645,7 +645,7 @@ ctx.env.http_host ? ctx.env.http_host : "", ctx.env.https ? ctx.env.https : "", ctx.qry.repo ? ctx.qry.repo : "", ctx.qry.page ? ctx.qry.page : "", - ctx.qry.url ? ctx.qry.url : "", + cgit_currentfullurl(), cgit_loginurl()); } diff --git a/ui-shared.c b/ui-shared.c index b53c56dd126fd76c15a51a588355343282f9695e..7a4c7265962fb93ad5261849360f1ef2b541eeb0 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -68,13 +68,46 @@ char *cgit_currenturl(void) { const char *root = cgit_rooturl(); - size_t len = strlen(root); if (!ctx.qry.url) return xstrdup(root); - if (len && root[len - 1] == '/') + if (root[0] && root[strlen(root) - 1] == '/') return fmtalloc("%s%s", root, ctx.qry.url); return fmtalloc("%s/%s", root, ctx.qry.url); +} + +char *cgit_currentfullurl(void) +{ + const char *root = cgit_rooturl(); + const char *orig_query = ctx.env.query_string ? ctx.env.query_string : ""; + size_t len = strlen(orig_query); + char *query = xmalloc(len + 2), *start_url, *ret; + + /* Remove all url=... parts from query string */ + memcpy(query + 1, orig_query, len + 1); + query[0] = '?'; + start_url = query; + while ((start_url = strstr(start_url, "url=")) != NULL) { + if (start_url[-1] == '?' || start_url[-1] == '&') { + const char *end_url = strchr(start_url, '&'); + if (end_url) + memmove(start_url, end_url + 1, strlen(end_url)); + else + start_url[0] = '\0'; + } else + ++start_url; + } + if (!query[1]) + query[0] = '\0'; + + if (!ctx.qry.url) + ret = fmtalloc("%s%s", root, query); + else if (root[0] && root[strlen(root) - 1] == '/') + ret = fmtalloc("%s%s%s", root, ctx.qry.url, query); + else + ret = fmtalloc("%s/%s%s", root, ctx.qry.url, query); + free(query); + return ret; } const char *cgit_rooturl(void) diff --git a/ui-shared.h b/ui-shared.h index 4d5978bb6f79b643ee296b69b061baf4955dc4cc..6964873a63942e2222d3981d73d4178c52c51508 100644 --- a/ui-shared.h +++ b/ui-shared.h @@ -5,6 +5,7 @@ extern const char *cgit_httpscheme(void); extern char *cgit_hosturl(void); extern const char *cgit_rooturl(void); extern char *cgit_currenturl(void); +extern char *cgit_currentfullurl(void); extern const char *cgit_loginurl(void); extern char *cgit_repourl(const char *reponame); extern char *cgit_fileurl(const char *reponame, const char *pagename,