Commit 9f6f394340870ce92e48a6d248d41d3d35d3fbf2

Tracey Emery 2020-09-24T22:44:11

restore code removed in b5f0780096f443734de8fc503ca92bf737e2a7b2 and complete code so sub directories work in gotweb This was pointed out by uwerler in IRC. ok stsp

diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index e0c4b29..6ea20ef 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -2192,7 +2192,31 @@ gw_template(size_t key, void *arg)
 	const struct got_error *error = NULL;
 	enum kcgi_err kerr = KCGI_OK;
 	struct gw_trans *gw_trans = arg;
-	char *img_src = NULL;
+	char *ati = NULL, *fic32 = NULL, *fic16 = NULL;
+	char *swm = NULL, *spt = NULL, *css = NULL, *logo = NULL;
+
+	if (asprintf(&ati, "%s%s", gw_trans->gw_conf->got_www_path,
+	    "/apple-touch-icon.png") == -1)
+		goto err;
+	if (asprintf(&fic32, "%s%s", gw_trans->gw_conf->got_www_path,
+	     "/favicon-32x32.png") == -1)
+		goto err;
+	if (asprintf(&fic16, "%s%s", gw_trans->gw_conf->got_www_path,
+	    "/favicon-16x16.png") == -1)
+		goto err;
+	if (asprintf(&swm, "%s%s", gw_trans->gw_conf->got_www_path,
+	    "/site.webmanifest") == -1)
+		goto err;
+	if (asprintf(&spt, "%s%s", gw_trans->gw_conf->got_www_path,
+	    "/safari-pinned-tab.svg") == -1)
+		goto err;
+	if (asprintf(&css, "%s%s", gw_trans->gw_conf->got_www_path,
+	    "/gotweb.css") == -1)
+		goto err;
+	if (asprintf(&logo, "%s%s%s", gw_trans->gw_conf->got_www_path,
+	    gw_trans->gw_conf->got_www_path ? "/" : "",
+	    gw_trans->gw_conf->got_logo) == -1)
+		goto err;
 
 	switch (key) {
 	case (TEMPL_HEAD):
@@ -2231,7 +2255,7 @@ gw_template(size_t key, void *arg)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
 		    KATTR_REL, "apple-touch-icon", KATTR_SIZES, "180x180",
-		    KATTR_HREF, "/apple-touch-icon.png", KATTR__MAX);
+		    KATTR_HREF, ati, KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
@@ -2239,7 +2263,7 @@ gw_template(size_t key, void *arg)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
 		    KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
-		    "32x32", KATTR_HREF, "/favicon-32x32.png", KATTR__MAX);
+		    "32x32", KATTR_HREF, fic32, KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
@@ -2247,14 +2271,14 @@ gw_template(size_t key, void *arg)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
 		    KATTR_REL, "icon", KATTR_TYPE, "image/png", KATTR_SIZES,
-		    "16x16", KATTR_HREF, "/favicon-16x16.png", KATTR__MAX);
+		    "16x16", KATTR_HREF, fic16, KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
-		    KATTR_REL, "manifest", KATTR_HREF, "/site.webmanifest",
+		    KATTR_REL, "manifest", KATTR_HREF, swm,
 		    KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
@@ -2263,7 +2287,7 @@ gw_template(size_t key, void *arg)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
 		    KATTR_REL, "mask-icon", KATTR_HREF,
-		    "/safari-pinned-tab.svg", KATTR__MAX);
+		    spt, KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
@@ -2271,7 +2295,7 @@ gw_template(size_t key, void *arg)
 			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_LINK,
 		    KATTR_REL, "stylesheet", KATTR_TYPE, "text/css",
-		    KATTR_HREF, "/gotweb.css", KATTR__MAX);
+		    KATTR_HREF, css, KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 1);
@@ -2288,20 +2312,13 @@ gw_template(size_t key, void *arg)
 		    KATTR_TARGET, "_sotd", KATTR__MAX);
 		if (kerr != KCGI_OK)
 			return 0;
-		if (asprintf(&img_src, "/%s",
-		    gw_trans->gw_conf->got_logo) == -1)
-			return 0;
 		kerr = khtml_attr(gw_trans->gw_html_req, KELEM_IMG,
-		    KATTR_SRC, img_src, KATTR__MAX);
-		if (kerr != KCGI_OK) {
-			free(img_src);
+		    KATTR_SRC, logo, KATTR__MAX);
+		if (kerr != KCGI_OK)
 			return 0;
-		}
 		kerr = khtml_closeelem(gw_trans->gw_html_req, 3);
-		if (kerr != KCGI_OK) {
-			free(img_src);
+		if (kerr != KCGI_OK)
 			return 0;
-		}
 		break;
 	case (TEMPL_SITEPATH):
 		error = gw_output_site_link(gw_trans);
@@ -2379,7 +2396,23 @@ gw_template(size_t key, void *arg)
 	default:
 		return 0;
 	}
+	free(ati);
+	free(fic32);
+	free(fic16);
+	free(swm);
+	free(spt);
+	free(css);
+	free(logo);
 	return 1;
+err:
+	free(ati);
+	free(fic32);
+	free(fic16);
+	free(swm);
+	free(spt);
+	free(css);
+	free(logo);
+	return 0;
 }
 
 static const struct got_error *
@@ -4701,6 +4734,7 @@ main(int argc, char *argv[])
 done:
 	if (gw_malloc) {
 		free(gw_trans->gw_conf->got_repos_path);
+		free(gw_trans->gw_conf->got_www_path);
 		free(gw_trans->gw_conf->got_site_name);
 		free(gw_trans->gw_conf->got_site_owner);
 		free(gw_trans->gw_conf->got_site_link);
diff --git a/gotweb/gotweb.conf.5 b/gotweb/gotweb.conf.5
index 62d2057..d1056ee 100644
--- a/gotweb/gotweb.conf.5
+++ b/gotweb/gotweb.conf.5
@@ -88,6 +88,8 @@ Set the displayed site name title.
 Set the displayed site owner.
 .It Ic got_show_site_owner Ar on | off
 Toggle display of the site owner.
+.It Ic got_www_path Ar string
+Set the public gotweb httpd path.
 .El
 .Sh EXAMPLES
 These are the currently configurable items for
@@ -101,6 +103,7 @@ with their default values.
 #
 
 got_repos_path			"/got/public"
+got_www_path			"/gotweb"
 
 #got_max_repos			100
 #got_max_repos_display		25
diff --git a/gotweb/gotweb.h b/gotweb/gotweb.h
index c2c8f70..0f7fca0 100644
--- a/gotweb/gotweb.h
+++ b/gotweb/gotweb.h
@@ -27,6 +27,7 @@
 #define GOTWEB_GOT_DIR	 ".got"
 #define GOTWEB_GIT_DIR	 ".git"
 
+#define D_GOTWWW	 ""
 #define D_GOTPATH	 "/got/public"
 #define D_SITENAME	 "Gotweb"
 #define D_SITEOWNER	 "Got Owner"
@@ -48,6 +49,7 @@
 
 struct gotweb_config {
 	char		*got_repos_path;
+	char		*got_www_path;
 	char		*got_site_name;
 	char		*got_site_owner;
 	char		*got_site_link;
diff --git a/gotweb/parse.y b/gotweb/parse.y
index 93c6e03..c662466 100644
--- a/gotweb/parse.y
+++ b/gotweb/parse.y
@@ -130,7 +130,16 @@ boolean		: STRING {
 		;
 main		: GOT_REPOS_PATH STRING {
 			gw_conf->got_repos_path = strdup($2);
-			if (gw_conf->got_repos_path== NULL) {
+			if (gw_conf->got_repos_path == NULL) {
+				free($2);
+				yyerror("strdup");
+				YYERROR;
+			}
+			free($2);
+		}
+		| GOT_WWW_PATH STRING {
+			gw_conf->got_www_path = strdup($2);
+			if (gw_conf->got_www_path == NULL) {
 				free($2);
 				yyerror("strdup");
 				YYERROR;
@@ -265,6 +274,7 @@ lookup(char *s)
 		{ "got_site_link",		GOT_SITE_LINK },
 		{ "got_site_name",		GOT_SITE_NAME },
 		{ "got_site_owner",		GOT_SITE_OWNER },
+		{ "got_www_path",		GOT_WWW_PATH },
 	};
 	const struct keywords	*p;
 
@@ -597,6 +607,11 @@ parse_gotweb_config(struct gotweb_config **gconf, const char *filename)
 		gerror = got_error_from_errno("strdup");
 		goto done;
 	}
+	gw_conf->got_www_path = strdup(D_GOTWWW);
+	if (gw_conf->got_www_path == NULL) {
+		gerror = got_error_from_errno("strdup");
+		goto done;
+	}
 	gw_conf->got_site_name = strdup(D_SITENAME);
 	if (gw_conf->got_site_name == NULL) {
 		gerror = got_error_from_errno("strdup");