Commit f1200fe3a020b8b790beb340db7334e07faf9f5d

Tracey Emery 2020-02-13T22:20:46

fixup initial action querystring error and use the err.tmpl finally

diff --git a/gotweb/files/cgi-bin/gw_tmpl/err.tmpl b/gotweb/files/cgi-bin/gw_tmpl/err.tmpl
index 1281926..52b2cb6 100644
--- a/gotweb/files/cgi-bin/gw_tmpl/err.tmpl
+++ b/gotweb/files/cgi-bin/gw_tmpl/err.tmpl
@@ -14,7 +14,12 @@
 			@@search@@
 		</div>
 		<div id="content">
-			@@content@@
+			<div id='err_title_wrapper'>
+				<div id='err_title'>Error</div>
+			</div>
+			<div id='err_content'>
+				@@content@@
+			</div>
 		</div>
 		@@siteowner@@
 	</div>
diff --git a/gotweb/files/htdocs/gotweb/gotweb.css b/gotweb/files/htdocs/gotweb/gotweb.css
index dd8d35a..33a8c27 100644
--- a/gotweb/files/htdocs/gotweb/gotweb.css
+++ b/gotweb/files/htdocs/gotweb/gotweb.css
@@ -363,6 +363,29 @@ body {
 	padding-bottom: 5px;
 }
 
+/* err.tmpl */
+
+#err_title_wrapper {
+	clear: left;
+	float: left;
+	width: 100%;
+	background-color: LightSlateGray;
+	color: #ffffff;
+}
+#err_title {
+	padding-left: 10px;
+	padding-top: 5px;
+	padding-bottom: 5px;
+}
+#err_content {
+	clear: left;
+	float: left;
+	width: 100%;
+	padding-left: 20px;
+	padding-top: 20px;
+	padding-bottom: 20px;
+}
+
 /* briefs.tmpl */
 
 #briefs_title_wrapper {
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index 97d12fc..4db052d 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -60,6 +60,7 @@ struct gw_trans {
 	struct ktemplate	*gw_tmpl;
 	struct khtmlreq		*gw_html_req;
 	struct kreq		*gw_req;
+	const struct got_error	*error;
 	const char		*repo_name;
 	char			*repo_path;
 	char			*commit;
@@ -152,15 +153,14 @@ static const struct kvalid gw_keys[KEY__ZMAX] = {
 	{ kvalid_stringne,	"path" },
 };
 
-static const struct got_error	*gw_init_gw_dir(struct gw_dir **, const char *);
 static struct gw_header		*gw_init_header(void);
 
 static void			 gw_free_headers(struct gw_header *);
-static void			 gw_display_error(struct gw_trans *,
-				    const struct got_error *);
 
 static int			 gw_template(size_t, void *);
 
+static const struct got_error	*gw_error(struct gw_trans *);
+static const struct got_error	*gw_init_gw_dir(struct gw_dir **, const char *);
 static const struct got_error	*gw_get_repo_description(char **,
 				    struct gw_trans *, char *);
 static const struct got_error	*gw_get_repo_owner(char **, struct gw_trans *,
@@ -246,7 +246,7 @@ static struct gw_query_action gw_query_funcs[] = {
 	{ GW_BRIEFS,	"briefs",	gw_briefs,	"gw_tmpl/briefs.tmpl" },
 	{ GW_COMMITS,	"commits",	gw_commits,	"gw_tmpl/commit.tmpl" },
 	{ GW_DIFF,	"diff",		gw_diff,	"gw_tmpl/diff.tmpl" },
-	{ GW_ERR,	 NULL,		NULL,		"gw_tmpl/err.tmpl" },
+	{ GW_ERR,	"error",	gw_error,	"gw_tmpl/err.tmpl" },
 	{ GW_INDEX,	"index",	gw_index,	"gw_tmpl/index.tmpl" },
 	{ GW_SUMMARY,	"summary",	gw_summary,	"gw_tmpl/summry.tmpl" },
 	{ GW_TAG,	"tag",		gw_tag,		"gw_tmpl/tag.tmpl" },
@@ -1637,16 +1637,22 @@ gw_parse_querystring(struct gw_trans *gw_trans)
 			return got_error_from_errno("asprintf");
 
 		/* get action and set function */
-		if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
+		if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION])) {
 			for (i = 0; i < nitems(gw_query_funcs); i++) {
 				action = &gw_query_funcs[i];
-				if (action->func_name == NULL ||
-				    strcmp(action->func_name, p->parsed.s))
+				if (action->func_name == NULL)
 					continue;
-
-				gw_trans->action = i;
-				break;
+				if (strcmp(action->func_name,
+				    p->parsed.s) == 0) {
+					gw_trans->action = i;
+					break;
+				}
 			}
+			if (gw_trans->action == -1) {
+				gw_trans->action = GW_ERR;
+				gw_trans->error = got_error_from_errno("bad action");
+			}
+		}
 
  		if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID])) {
 			if (asprintf(&gw_trans->commit, "%s",
@@ -1669,10 +1675,6 @@ gw_parse_querystring(struct gw_trans *gw_trans)
 				return got_error_from_errno("asprintf");
 		}
 
-		if (action == NULL) {
-			error = got_error_from_errno("invalid action");
-			return error;
-		}
 		error = gw_init_gw_dir(&gw_trans->gw_dir, gw_trans->repo_name);
 		if (error)
 			return error;
@@ -1775,16 +1777,14 @@ gw_display_index(struct gw_trans *gw_trans)
 	return gw_kcgi_error(khtml_close(gw_trans->gw_html_req));
 }
 
-static void
-gw_display_error(struct gw_trans *gw_trans, const struct got_error *err)
+static const struct got_error *
+gw_error(struct gw_trans *gw_trans)
 {
-	if (gw_display_open(gw_trans, KHTTP_200, gw_trans->mime) != NULL)
-		return;
+	enum kcgi_err kerr;
+
+	kerr = khtml_puts(gw_trans->gw_html_req, gw_trans->error->msg);
 
-	if (khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0) != KCGI_OK)
-		return;
-	khtml_puts(gw_trans->gw_html_req, err->msg);
-	khtml_close(gw_trans->gw_html_req);
+	return gw_kcgi_error(kerr);
 }
 
 static int
@@ -3108,6 +3108,8 @@ gw_get_commit(struct gw_trans *gw_trans, struct gw_header *header)
 		free(s);
 	}
 
+	free(refs_str);
+
 	error = got_object_id_str(&header->commit_id, header->id);
 	if (error)
 		return error;
@@ -4246,6 +4248,7 @@ main(int argc, char *argv[])
 	TAILQ_INIT(&gw_trans->gw_dirs);
 	TAILQ_INIT(&gw_trans->gw_headers);
 
+	gw_trans->action = -1;
 	gw_trans->page = 0;
 	gw_trans->repos_total = 0;
 	gw_trans->repo_path = NULL;
@@ -4273,11 +4276,6 @@ main(int argc, char *argv[])
 	else
 		error = gw_display_index(gw_trans);
 done:
-	if (error) {
-		gw_trans->mime = KMIME_TEXT_PLAIN;
-		gw_trans->action = GW_ERR;
-		gw_display_error(gw_trans, error);
-	}
 	if (gw_malloc) {
 		free(gw_trans->gw_conf->got_repos_path);
 		free(gw_trans->gw_conf->got_site_name);