summary refs log tree commit diff
path: root/ui-shared.c
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-11-21 03:16:11 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2018-11-25 06:01:34 +0100
commit898b9e19e0eacd67456ddcc91ff173055e1c0e99 (patch)
tree3f056db3a9d329b43a0fa7c567ad54672ace5bfb /ui-shared.c
parenta22855747e97e55a7b7a2622fe671b8ca9af0981 (diff)
auth-filter: pass url with query string attached
Otherwise redirections come out wrong.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui-shared.c')
-rw-r--r--ui-shared.c37
1 files changed, 35 insertions, 2 deletions
diff --git a/ui-shared.c b/ui-shared.c
index b53c56d..7a4c726 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -68,15 +68,48 @@ char *cgit_hosturl(void)
 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)
 {
 	if (ctx.cfg.virtual_root)