commit 4d89b560595076c8994896195fbcd196460987dc
parent a8e69abfa2b38ef12bad79516bf02da7701d549c
Author: Christoph Lohmann <20h@r-36.net>
Date:   Thu, 30 Aug 2018 21:27:57 +0200
Add preliminary relative path support for gph files.
Diffstat:
3 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/handlr.c b/handlr.c
@@ -83,14 +83,13 @@ handlegph(int sock, char *file, char *port, char *base, char *args,
 	Indexs *act;
 	int i, ret = 0;
 
-	USED(base);
 	USED(args);
 	USED(sear);
 
 	act = scanfile(file);
 	if (act != NULL) {
 		for (i = 0; i < act->num && ret >= 0; i++) {
-			ret = printelem(sock, act->n[i], ohost, port);
+			ret = printelem(sock, act->n[i], file, base, ohost, port);
 			freeelem(act->n[i]);
 			act->n[i] = NULL;
 		}
@@ -185,8 +184,6 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
 	int outpipe[2], ret = 0;
 	Elems *el;
 
-	USED(base);
-
 	if (pipe(outpipe) < 0)
 		return;
 
@@ -246,7 +243,7 @@ handledcgi(int sock, char *file, char *port, char *base, char *args,
 			if (el == NULL)
 				continue;
 
-			ret = printelem(sock, el, ohost, port);
+			ret = printelem(sock, el, file, base, ohost, port);
 			freeelem(el);
 		}
 		if (ferror(fp))
diff --git a/ind.c b/ind.c
@@ -17,6 +17,7 @@
 #include <netinet/tcp.h>
 #include <arpa/inet.h>
 #include <sys/ioctl.h>
+#include <limits.h>
 
 /* for sendfile(2) */
 #ifdef __linux__
@@ -413,8 +414,11 @@ scanfile(char *fname)
 }
 
 int
-printelem(int fd, Elems *el, char *addr, char *port)
+printelem(int fd, Elems *el, char *file, char *base, char *addr, char *port)
 {
+	char *path, *p, buf[PATH_MAX+1];
+	int len;
+
 	if (!strcmp(el->e[3], "server")) {
 		free(el->e[3]);
 		el->e[3] = xstrdup(addr);
@@ -423,6 +427,24 @@ printelem(int fd, Elems *el, char *addr, char *port)
 		free(el->e[4]);
 		el->e[4] = xstrdup(port);
 	}
+	if (el->e[2][0] != '/' && !strncmp(el->e[2], "URL:", 4)) {
+		path = file + strlen(base);
+		if ((p = strrchr(path, '/')))
+			len = p - path;
+		else
+			len = strlen(path);
+		snprintf(buf, sizeof(buf), "%s%.*s/%s", base, len, path, el->e[2]);
+
+		if ((path = realpath(buf, NULL)) &&
+				!strncmp(base, path, strlen(base))) {
+			p = path + strlen(base);
+			free(el->e[2]);
+			el->e[2] = xstrdup(p[0]? p : "/");
+		}
+		if (path != NULL)
+			free(path);
+	}
+
 	if (dprintf(fd, "%.1s%s\t%s\t%s\t%s\r\n", el->e[0], el->e[1], el->e[2],
 			el->e[3], el->e[4]) < 0) {
 		perror("printelem: dprintf");
diff --git a/ind.h b/ind.h
@@ -40,7 +40,7 @@ Indexs *scanfile(char *fname);
 Elems *getadv(char *str);
 int pendingbytes(int sock);
 void waitforpendingbytes(int sock);
-int printelem(int fd, Elems *el, char *addr, char *port);
+int printelem(int fd, Elems *el, char *file, char *base, char *addr, char *port);
 void addindexs(Indexs *idx, Elems *el);
 void addelem(Elems *e, char *s);
 void freeindex(Indexs *i);