2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
53 static const struct querystring_keys querystring_keys[] = {
58 { "headref", HEADREF },
59 { "index_page", INDEX_PAGE },
64 static const struct action_keys action_keys[] = {
68 { "commits", COMMITS },
72 { "summary", SUMMARY },
78 static const struct got_error *gotweb_init_querystring(struct querystring **);
79 static const struct got_error *gotweb_parse_querystring(struct querystring **,
81 static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 static const struct got_error *gotweb_render_index(struct request *);
84 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 static const struct got_error *gotweb_load_got_path(struct request *c,
88 static const struct got_error *gotweb_get_repo_description(char **,
89 struct server *, const char *, int);
90 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 static const struct got_error *gotweb_render_blame(struct request *);
93 static const struct got_error *gotweb_render_commits(struct request *);
94 static const struct got_error *gotweb_render_diff(struct request *);
95 static const struct got_error *gotweb_render_summary(struct request *);
96 static const struct got_error *gotweb_render_tag(struct request *);
97 static const struct got_error *gotweb_render_tags(struct request *);
98 static const struct got_error *gotweb_render_tree(struct request *);
99 static const struct got_error *gotweb_render_branches(struct request *);
101 static void gotweb_free_querystring(struct querystring *);
102 static void gotweb_free_repo_dir(struct repo_dir *);
104 struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 gotweb_process_request(struct request *c)
109 const struct got_error *error = NULL, *error2 = NULL;
110 struct server *srv = NULL;
111 struct querystring *qs = NULL;
112 struct repo_dir *repo_dir = NULL;
113 uint8_t err[] = "gotwebd experienced an error: ";
116 /* init the transport */
117 error = gotweb_init_transport(&c->t);
119 log_warnx("%s: %s", __func__, error->msg);
122 /* don't process any further if client disconnected */
123 if (c->sock->client_status == CLIENT_DISCONNECT)
125 /* get the gotwebd server */
126 srv = gotweb_get_server(c->server_name, c->http_host);
128 log_warnx("%s: error server is NULL", __func__);
132 /* parse our querystring */
133 error = gotweb_init_querystring(&qs);
135 log_warnx("%s: %s", __func__, error->msg);
139 error = gotweb_parse_querystring(&qs, c->querystring);
141 log_warnx("%s: %s", __func__, error->msg);
146 * certain actions require a commit id in the querystring. this stops
147 * bad actors from exploiting this by manually manipulating the
151 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
152 qs->action == DIFF)) {
153 error2 = got_error(GOT_ERR_QUERYSTRING);
157 if (qs->action != INDEX) {
158 error = gotweb_init_repo_dir(&repo_dir, qs->path);
161 error = gotweb_load_got_path(c, repo_dir);
162 c->t->repo_dir = repo_dir;
163 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 /* render top of page */
168 if (qs != NULL && qs->action == BLOB) {
169 error = got_get_repo_commits(c, 1);
172 error = got_output_file_blob(c);
174 log_warnx("%s: %s", __func__, error->msg);
181 error = gotweb_render_content_type(c, "text/html");
183 log_warnx("%s: %s", __func__, error->msg);
188 if (gotweb_render_header(c->tp) == -1)
198 error = gotweb_render_blame(c);
200 log_warnx("%s: %s", __func__, error->msg);
205 if (gotweb_render_briefs(c->tp) == -1)
209 error = gotweb_render_commits(c);
211 log_warnx("%s: %s", __func__, error->msg);
216 error = gotweb_render_diff(c);
218 log_warnx("%s: %s", __func__, error->msg);
223 error = gotweb_render_index(c);
225 log_warnx("%s: %s", __func__, error->msg);
230 error = gotweb_render_summary(c);
232 log_warnx("%s: %s", __func__, error->msg);
237 error = gotweb_render_tag(c);
239 log_warnx("%s: %s", __func__, error->msg);
244 error = gotweb_render_tags(c);
246 log_warnx("%s: %s", __func__, error->msg);
251 error = gotweb_render_tree(c);
253 log_warnx("%s: %s", __func__, error->msg);
259 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
260 "Erorr: Bad Querystring");
268 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
270 if (fcgi_printf(c, "\n%s", err) == -1)
273 if (fcgi_printf(c, "%s", error->msg) == -1)
276 if (fcgi_printf(c, "see daemon logs for details") == -1)
279 if (html && fcgi_printf(c, "</div>\n") == -1)
282 if (html && srv != NULL)
283 gotweb_render_footer(c->tp);
287 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
289 struct server *srv = NULL;
291 /* check against the server name first */
292 if (strlen(server_name) > 0)
293 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
294 if (strcmp(srv->name, server_name) == 0)
297 /* check against subdomain second */
298 if (strlen(subdomain) > 0)
299 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
300 if (strcmp(srv->name, subdomain) == 0)
303 /* if those fail, send first server */
304 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
311 const struct got_error *
312 gotweb_init_transport(struct transport **t)
314 const struct got_error *error = NULL;
316 *t = calloc(1, sizeof(**t));
318 return got_error_from_errno2("%s: calloc", __func__);
320 TAILQ_INIT(&(*t)->repo_commits);
321 TAILQ_INIT(&(*t)->repo_tags);
324 (*t)->repo_dir = NULL;
326 (*t)->next_id = NULL;
327 (*t)->prev_id = NULL;
334 static const struct got_error *
335 gotweb_init_querystring(struct querystring **qs)
337 const struct got_error *error = NULL;
339 *qs = calloc(1, sizeof(**qs));
341 return got_error_from_errno2("%s: calloc", __func__);
343 (*qs)->headref = strdup("HEAD");
344 if ((*qs)->headref == NULL) {
347 return got_error_from_errno2("%s: strdup", __func__);
350 (*qs)->action = INDEX;
351 (*qs)->commit = NULL;
353 (*qs)->folder = NULL;
354 (*qs)->index_page = 0;
360 static const struct got_error *
361 gotweb_parse_querystring(struct querystring **qs, char *qst)
363 const struct got_error *error = NULL;
364 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
365 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
372 return got_error_from_errno2("%s: strdup", __func__);
377 while (tok1_pair != NULL) {
378 strsep(&tok1_end, "&");
380 tok2 = strdup(tok1_pair);
383 return got_error_from_errno2("%s: strdup", __func__);
389 while (tok2_pair != NULL) {
390 strsep(&tok2_end, "=");
392 error = gotweb_assign_querystring(qs, tok2_pair,
397 tok2_pair = tok2_end;
400 tok1_pair = tok1_end;
411 * Adapted from usr.sbin/httpd/httpd.c url_decode.
413 static const struct got_error *
414 gotweb_urldecode(char *url)
426 /* Encoding character is followed by two hex chars */
427 if (!isxdigit((unsigned char)p[1]) ||
428 !isxdigit((unsigned char)p[2]) ||
429 (p[1] == '0' && p[2] == '0'))
430 return got_error(GOT_ERR_BAD_QUERYSTRING);
436 * We don't have to validate "hex" because it is
437 * guaranteed to include two hex chars followed by nul.
439 x = strtoul(hex, NULL, 16);
455 static const struct got_error *
456 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
458 const struct got_error *error = NULL;
462 error = gotweb_urldecode(value);
466 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
467 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
470 switch (querystring_keys[el_cnt].element) {
472 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
473 if (strcmp(value, action_keys[a_cnt].name) != 0)
475 else if (strcmp(value,
476 action_keys[a_cnt].name) == 0){
478 action_keys[a_cnt].action;
486 (*qs)->commit = strdup(value);
487 if ((*qs)->commit == NULL) {
488 error = got_error_from_errno2("%s: strdup",
494 (*qs)->file = strdup(value);
495 if ((*qs)->file == NULL) {
496 error = got_error_from_errno2("%s: strdup",
502 (*qs)->folder = strdup(value);
503 if ((*qs)->folder == NULL) {
504 error = got_error_from_errno2("%s: strdup",
510 free((*qs)->headref);
511 (*qs)->headref = strdup(value);
512 if ((*qs)->headref == NULL) {
513 error = got_error_from_errno2("%s: strdup",
519 if (strlen(value) == 0)
521 (*qs)->index_page = strtonum(value, INT64_MIN,
524 error = got_error_from_errno3("%s: strtonum %s",
528 if ((*qs)->index_page < 0)
529 (*qs)->index_page = 0;
532 (*qs)->path = strdup(value);
533 if ((*qs)->path == NULL) {
534 error = got_error_from_errno2("%s: strdup",
540 if (strlen(value) == 0)
542 (*qs)->page = strtonum(value, INT64_MIN,
545 error = got_error_from_errno3("%s: strtonum %s",
561 gotweb_free_repo_tag(struct repo_tag *rt)
566 free(rt->tag_commit);
567 free(rt->commit_msg);
574 gotweb_free_repo_commit(struct repo_commit *rc)
584 free(rc->commit_msg);
590 gotweb_free_querystring(struct querystring *qs)
603 gotweb_free_repo_dir(struct repo_dir *repo_dir)
605 if (repo_dir != NULL) {
606 free(repo_dir->name);
607 free(repo_dir->owner);
608 free(repo_dir->description);
611 free(repo_dir->path);
617 gotweb_free_transport(struct transport *t)
619 struct repo_commit *rc = NULL, *trc = NULL;
620 struct repo_tag *rt = NULL, *trt = NULL;
622 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
623 TAILQ_REMOVE(&t->repo_commits, rc, entry);
624 gotweb_free_repo_commit(rc);
626 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
627 TAILQ_REMOVE(&t->repo_tags, rt, entry);
628 gotweb_free_repo_tag(rt);
630 gotweb_free_repo_dir(t->repo_dir);
631 gotweb_free_querystring(t->qs);
637 const struct got_error *
638 gotweb_render_content_type(struct request *c, const uint8_t *type)
640 const char *csp = "default-src 'self'; script-src 'none'; "
641 "object-src 'none';";
644 "Content-Security-Policy: %s\r\n"
645 "Content-Type: %s\r\n\r\n",
650 const struct got_error *
651 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
654 fcgi_printf(c, "Content-type: %s\r\n"
655 "Content-disposition: attachment; filename=%s\r\n\r\n",
661 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
662 struct gotweb_url *next, int *have_next)
664 struct transport *t = c->t;
665 struct querystring *qs = t->qs;
666 struct server *srv = c->srv;
668 *have_prev = *have_next = 0;
672 if (qs->index_page > 0) {
674 *prev = (struct gotweb_url){
676 .index_page = qs->index_page - 1,
680 if (t->next_disp == srv->max_repos_display &&
681 t->repos_total != (qs->index_page + 1) *
682 srv->max_repos_display) {
684 *next = (struct gotweb_url){
686 .index_page = qs->index_page + 1,
692 if (t->prev_id && qs->commit != NULL &&
693 strcmp(qs->commit, t->prev_id) != 0) {
695 *prev = (struct gotweb_url){
698 .page = qs->page - 1,
700 .commit = t->prev_id,
701 .headref = qs->headref,
706 *next = (struct gotweb_url){
709 .page = qs->page + 1,
711 .commit = t->next_id,
712 .headref = qs->headref,
717 if (t->prev_id && qs->commit != NULL &&
718 strcmp(qs->commit, t->prev_id) != 0) {
720 *prev = (struct gotweb_url){
723 .page = qs->page - 1,
725 .commit = t->prev_id,
726 .headref = qs->headref,
727 .folder = qs->folder,
733 *next = (struct gotweb_url){
736 .page = qs->page + 1,
738 .commit = t->next_id,
739 .headref = qs->headref,
740 .folder = qs->folder,
746 if (t->prev_id && qs->commit != NULL &&
747 strcmp(qs->commit, t->prev_id) != 0) {
749 *prev = (struct gotweb_url){
752 .page = qs->page - 1,
754 .commit = t->prev_id,
755 .headref = qs->headref,
760 *next = (struct gotweb_url){
763 .page = qs->page + 1,
765 .commit = t->next_id,
766 .headref = qs->headref,
773 static const struct got_error *
774 gotweb_render_index(struct request *c)
776 const struct got_error *error = NULL;
777 struct server *srv = c->srv;
778 struct transport *t = c->t;
779 struct querystring *qs = t->qs;
780 struct repo_dir *repo_dir = NULL;
782 struct dirent **sd_dent = NULL;
783 unsigned int d_cnt, d_i, d_disp = 0;
784 unsigned int d_skipped = 0;
787 d = opendir(srv->repos_path);
789 error = got_error_from_errno2("opendir", srv->repos_path);
793 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
796 error = got_error_from_errno2("scandir", srv->repos_path);
800 if (gotweb_render_repo_table_hdr(c->tp) == -1)
803 for (d_i = 0; d_i < d_cnt; d_i++) {
804 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
807 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
808 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
813 error = got_path_dirent_type(&type, srv->repos_path,
817 if (type != DT_DIR) {
822 if (qs->index_page > 0 && (qs->index_page *
823 srv->max_repos_display) > t->prev_disp) {
828 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
832 error = gotweb_load_got_path(c, repo_dir);
833 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
835 gotweb_free_repo_dir(repo_dir);
840 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
846 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
849 gotweb_free_repo_dir(repo_dir);
852 if (d_disp == srv->max_repos_display)
855 t->repos_total = d_cnt - d_skipped;
857 if (srv->max_repos_display == 0)
859 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
861 if (t->repos_total <= srv->max_repos ||
862 t->repos_total <= srv->max_repos_display)
865 if (gotweb_render_navs(c->tp) == -1)
869 for (d_i = 0; d_i < d_cnt; d_i++)
873 if (d != NULL && closedir(d) == EOF && error == NULL)
874 error = got_error_from_errno("closedir");
878 static const struct got_error *
879 gotweb_render_blame(struct request *c)
881 const struct got_error *error = NULL;
882 struct transport *t = c->t;
883 struct repo_commit *rc = NULL;
884 char *age = NULL, *msg = NULL;
887 error = got_get_repo_commits(c, 1);
891 rc = TAILQ_FIRST(&t->repo_commits);
893 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
896 error = gotweb_escape_html(&msg, rc->commit_msg);
900 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
901 "<div id='blame_title'>Blame</div>\n"
902 "</div>\n" /* #blame_title_wrapper */
903 "<div id='blame_content'>\n"
904 "<div id='blame_header_wrapper'>\n"
905 "<div id='blame_header'>\n"
906 "<div class='header_age_title'>Date:</div>\n"
907 "<div class='header_age'>%s</div>\n"
908 "<div id='header_commit_msg_title'>Message:</div>\n"
909 "<div id='header_commit_msg'>%s</div>\n"
910 "</div>\n" /* #blame_header */
911 "</div>\n" /* #blame_header_wrapper */
912 "<div class='dotted_line'></div>\n"
913 "<div id='blame'>\n",
919 error = got_output_file_blame(c);
923 fcgi_printf(c, "</div>\n" /* #blame */
924 "</div>\n"); /* #blame_content */
931 static const struct got_error *
932 gotweb_render_commits(struct request *c)
934 const struct got_error *error = NULL;
935 struct repo_commit *rc = NULL;
936 struct server *srv = c->srv;
937 struct transport *t = c->t;
938 struct repo_dir *repo_dir = t->repo_dir;
939 char *age = NULL, *author = NULL, *msg = NULL;
942 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
943 "<div class='commits_title'>Commits</div>\n"
944 "</div>\n" /* .commits_title_wrapper */
945 "<div class='commits_content'>\n");
949 error = got_get_repo_commits(c, srv->max_commits_display);
953 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
954 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
957 error = gotweb_escape_html(&author, rc->author);
960 error = gotweb_escape_html(&msg, rc->commit_msg);
964 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
965 "<div class='commits_header'>\n"
966 "<div class='header_commit_title'>Commit:</div>\n"
967 "<div class='header_commit'>%s</div>\n"
968 "<div class='header_author_title'>Author:</div>\n"
969 "<div class='header_author'>%s</div>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "</div>\n" /* .commits_header */
973 "</div>\n" /* .commits_header_wrapper */
974 "<div class='dotted_line'></div>\n"
975 "<div class='commit'>\n%s</div>\n",
983 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
984 "<div class='navs'>") == -1)
987 r = gotweb_link(c, &(struct gotweb_url){
991 .path = repo_dir->name,
992 .commit = rc->commit_id,
997 if (fcgi_printf(c, " | ") == -1)
1000 r = gotweb_link(c, &(struct gotweb_url){
1004 .path = repo_dir->name,
1005 .commit = rc->commit_id,
1010 if (fcgi_printf(c, "</div>\n" /* .navs */
1011 "</div>\n" /* .navs_wrapper */
1012 "<div class='dotted_line'></div>\n") == -1)
1023 if (t->next_id || t->prev_id) {
1024 if (gotweb_render_navs(c->tp) == -1)
1027 fcgi_printf(c, "</div>\n"); /* .commits_content */
1035 static const struct got_error *
1036 gotweb_render_branches(struct request *c)
1038 const struct got_error *error = NULL;
1039 struct got_reflist_head refs;
1040 struct got_reflist_entry *re;
1041 struct transport *t = c->t;
1042 struct querystring *qs = t->qs;
1043 struct got_repository *repo = t->repo;
1044 char *escaped_refname = NULL;
1050 error = got_ref_list(&refs, repo, "refs/heads",
1051 got_ref_cmp_by_name, NULL);
1055 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1056 "<div id='branches_title'>Branches</div>\n"
1057 "</div>\n" /* #branches_title_wrapper */
1058 "<div id='branches_content'>\n");
1062 TAILQ_FOREACH(re, &refs, entry) {
1063 const char *refname = NULL;
1065 if (got_ref_is_symbolic(re->ref))
1068 refname = got_ref_get_name(re->ref);
1069 if (refname == NULL) {
1070 error = got_error_from_errno("strdup");
1073 if (strncmp(refname, "refs/heads/", 11) != 0)
1076 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1080 if (strncmp(refname, "refs/heads/", 11) == 0)
1082 error = gotweb_escape_html(&escaped_refname, refname);
1086 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1087 "<div class='branches_age'>%s</div>\n"
1088 "<div class='branches_space'> </div>\n"
1089 "<div class='branch'>", age);
1093 r = gotweb_link(c, &(struct gotweb_url){
1099 }, "%s", escaped_refname);
1103 if (fcgi_printf(c, "</div>\n" /* .branch */
1104 "<div class='navs_wrapper'>\n"
1105 "<div class='navs'>") == -1)
1108 r = gotweb_link(c, &(struct gotweb_url){
1118 if (fcgi_printf(c, " | ") == -1)
1121 r = gotweb_link(c, &(struct gotweb_url){
1127 }, "commit briefs");
1131 if (fcgi_printf(c, " | ") == -1)
1134 r = gotweb_link(c, &(struct gotweb_url){
1144 r = fcgi_printf(c, "</div>\n" /* .navs */
1145 "</div>\n" /* .navs_wrapper */
1146 "<div class='dotted_line'></div>\n"
1147 "</div>\n"); /* .branches_wrapper */
1153 free(escaped_refname);
1154 escaped_refname = NULL;
1156 fcgi_printf(c, "</div>\n"); /* #branches_content */
1159 free(escaped_refname);
1160 got_ref_list_free(&refs);
1164 static const struct got_error *
1165 gotweb_render_tree(struct request *c)
1167 const struct got_error *error = NULL;
1168 struct transport *t = c->t;
1169 struct repo_commit *rc = NULL;
1170 char *age = NULL, *msg = NULL;
1173 error = got_get_repo_commits(c, 1);
1177 rc = TAILQ_FIRST(&t->repo_commits);
1179 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1183 error = gotweb_escape_html(&msg, rc->commit_msg);
1187 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1188 "<div id='tree_title'>Tree</div>\n"
1189 "</div>\n" /* #tree_title_wrapper */
1190 "<div id='tree_content'>\n"
1191 "<div id='tree_header_wrapper'>\n"
1192 "<div id='tree_header'>\n"
1193 "<div id='header_tree_title'>Tree:</div>\n"
1194 "<div id='header_tree'>%s</div>\n"
1195 "<div class='header_age_title'>Date:</div>\n"
1196 "<div class='header_age'>%s</div>\n"
1197 "<div id='header_commit_msg_title'>Message:</div>\n"
1198 "<div id='header_commit_msg'>%s</div>\n"
1199 "</div>\n" /* #tree_header */
1200 "</div>\n" /* #tree_header_wrapper */
1201 "<div class='dotted_line'></div>\n"
1202 "<div id='tree'>\n",
1209 error = got_output_repo_tree(c);
1213 fcgi_printf(c, "</div>\n"); /* #tree */
1214 fcgi_printf(c, "</div>\n"); /* #tree_content */
1221 static const struct got_error *
1222 gotweb_render_diff(struct request *c)
1224 const struct got_error *error = NULL;
1225 struct transport *t = c->t;
1226 struct repo_commit *rc = NULL;
1227 char *age = NULL, *author = NULL, *msg = NULL;
1230 error = got_get_repo_commits(c, 1);
1234 rc = TAILQ_FIRST(&t->repo_commits);
1236 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1239 error = gotweb_escape_html(&author, rc->author);
1242 error = gotweb_escape_html(&msg, rc->commit_msg);
1246 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1247 "<div id='diff_title'>Commit Diff</div>\n"
1248 "</div>\n" /* #diff_title_wrapper */
1249 "<div id='diff_content'>\n"
1250 "<div id='diff_header_wrapper'>\n"
1251 "<div id='diff_header'>\n"
1252 "<div id='header_diff_title'>Diff:</div>\n"
1253 "<div id='header_diff'>%s<br />%s</div>\n"
1254 "<div class='header_commit_title'>Commit:</div>\n"
1255 "<div class='header_commit'>%s</div>\n"
1256 "<div id='header_tree_title'>Tree:</div>\n"
1257 "<div id='header_tree'>%s</div>\n"
1258 "<div class='header_author_title'>Author:</div>\n"
1259 "<div class='header_author'>%s</div>\n"
1260 "<div class='header_age_title'>Date:</div>\n"
1261 "<div class='header_age'>%s</div>\n"
1262 "<div id='header_commit_msg_title'>Message:</div>\n"
1263 "<div id='header_commit_msg'>%s</div>\n"
1264 "</div>\n" /* #diff_header */
1265 "</div>\n" /* #diff_header_wrapper */
1266 "<div class='dotted_line'></div>\n"
1267 "<div id='diff'>\n",
1268 rc->parent_id, rc->commit_id,
1277 error = got_output_repo_diff(c);
1281 fcgi_printf(c, "</div>\n"); /* #diff */
1282 fcgi_printf(c, "</div>\n"); /* #diff_content */
1290 static const struct got_error *
1291 gotweb_render_summary(struct request *c)
1293 const struct got_error *error = NULL;
1294 struct transport *t = c->t;
1295 struct server *srv = c->srv;
1298 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1301 if (srv->show_repo_description) {
1303 "<div id='description_title'>Description:</div>\n"
1304 "<div id='description'>%s</div>\n",
1305 t->repo_dir->description ? t->repo_dir->description : "");
1310 if (srv->show_repo_owner) {
1312 "<div id='repo_owner_title'>Owner:</div>\n"
1313 "<div id='repo_owner'>%s</div>\n",
1314 t->repo_dir->owner ? t->repo_dir->owner : "");
1319 if (srv->show_repo_age) {
1321 "<div id='last_change_title'>Last Change:</div>\n"
1322 "<div id='last_change'>%s</div>\n",
1328 if (srv->show_repo_cloneurl) {
1330 "<div id='cloneurl_title'>Clone URL:</div>\n"
1331 "<div id='cloneurl'>%s</div>\n",
1332 t->repo_dir->url ? t->repo_dir->url : "");
1337 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1341 if (gotweb_render_briefs(c->tp) == -1)
1344 error = gotweb_render_tags(c);
1346 log_warnx("%s: %s", __func__, error->msg);
1350 error = gotweb_render_branches(c);
1352 log_warnx("%s: %s", __func__, error->msg);
1357 static const struct got_error *
1358 gotweb_render_tag(struct request *c)
1360 const struct got_error *error = NULL;
1361 struct repo_tag *rt = NULL;
1362 struct transport *t = c->t;
1363 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1365 error = got_get_repo_tags(c, 1);
1369 if (t->tag_count == 0) {
1370 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1375 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1377 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1380 error = gotweb_escape_html(&author, rt->tagger);
1383 error = gotweb_escape_html(&msg, rt->commit_msg);
1387 tagname = rt->tag_name;
1388 if (strncmp(tagname, "refs/", 5) == 0)
1390 error = gotweb_escape_html(&tagname, tagname);
1394 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1395 "<div id='tags_title'>Tag</div>\n"
1396 "</div>\n" /* #tags_title_wrapper */
1397 "<div id='tags_content'>\n"
1398 "<div id='tag_header_wrapper'>\n"
1399 "<div id='tag_header'>\n"
1400 "<div class='header_commit_title'>Commit:</div>\n"
1401 "<div class='header_commit'>%s"
1402 " <span class='refs_str'>(%s)</span></div>\n"
1403 "<div class='header_author_title'>Tagger:</div>\n"
1404 "<div class='header_author'>%s</div>\n"
1405 "<div class='header_age_title'>Date:</div>\n"
1406 "<div class='header_age'>%s</div>\n"
1407 "<div id='header_commit_msg_title'>Message:</div>\n"
1408 "<div id='header_commit_msg'>%s</div>\n"
1409 "</div>\n" /* #tag_header */
1410 "<div class='dotted_line'></div>\n"
1411 "<div id='tag_commit'>\n%s</div>"
1412 "</div>" /* #tag_header_wrapper */
1413 "</div>", /* #tags_content */
1428 static const struct got_error *
1429 gotweb_render_tags(struct request *c)
1431 const struct got_error *error = NULL;
1432 struct repo_tag *rt = NULL;
1433 struct server *srv = c->srv;
1434 struct transport *t = c->t;
1435 struct querystring *qs = t->qs;
1436 struct repo_dir *repo_dir = t->repo_dir;
1437 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1438 int r, commit_found = 0;
1440 if (qs->action == BRIEFS) {
1442 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1444 error = got_get_repo_tags(c, srv->max_commits_display);
1448 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1449 "<div id='tags_title'>Tags</div>\n"
1450 "</div>\n" /* #tags_title_wrapper */
1451 "<div id='tags_content'>\n");
1455 if (t->tag_count == 0) {
1456 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1457 "This repository contains no tags");
1462 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1463 if (commit_found == 0 && qs->commit != NULL) {
1464 if (strcmp(qs->commit, rt->commit_id) != 0)
1469 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1473 tagname = rt->tag_name;
1474 if (strncmp(tagname, "refs/tags/", 10) == 0)
1476 error = gotweb_escape_html(&tagname, tagname);
1480 if (rt->tag_commit != NULL) {
1481 newline = strchr(rt->tag_commit, '\n');
1484 error = gotweb_escape_html(&msg, rt->tag_commit);
1489 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1490 "<div class='tag'>%s</div>\n"
1491 "<div class='tag_log'>", age, tagname) == -1)
1494 r = gotweb_link(c, &(struct gotweb_url){
1498 .path = repo_dir->name,
1499 .commit = rt->commit_id,
1500 }, "%s", msg ? msg : "");
1504 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1505 "<div class='navs_wrapper'>\n"
1506 "<div class='navs'>") == -1)
1509 r = gotweb_link(c, &(struct gotweb_url){
1513 .path = repo_dir->name,
1514 .commit = rt->commit_id,
1519 if (fcgi_printf(c, " | ") == -1)
1522 r = gotweb_link(c, &(struct gotweb_url){
1526 .path = repo_dir->name,
1527 .commit = rt->commit_id,
1528 }, "commit briefs");
1532 if (fcgi_printf(c, " | ") == -1)
1535 r = gotweb_link(c, &(struct gotweb_url){
1539 .path = repo_dir->name,
1540 .commit = rt->commit_id,
1546 "</div>\n" /* .navs */
1547 "</div>\n" /* .navs_wrapper */
1548 "<div class='dotted_line'></div>\n");
1559 if (t->next_id || t->prev_id) {
1560 if (gotweb_render_navs(c->tp) == -1)
1563 fcgi_printf(c, "</div>\n"); /* #tags_content */
1571 const struct got_error *
1572 gotweb_escape_html(char **escaped_html, const char *orig_html)
1574 const struct got_error *error = NULL;
1575 struct escape_pair {
1586 size_t orig_len, len;
1589 orig_len = strlen(orig_html);
1591 for (i = 0; i < orig_len; i++) {
1592 for (j = 0; j < nitems(esc); j++) {
1593 if (orig_html[i] != esc[j].c)
1595 len += strlen(esc[j].s) - 1 /* escaped char */;
1599 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1600 if (*escaped_html == NULL)
1601 return got_error_from_errno("calloc");
1604 for (i = 0; i < orig_len; i++) {
1606 for (j = 0; j < nitems(esc); j++) {
1607 if (orig_html[i] != esc[j].c)
1610 if (strlcat(*escaped_html, esc[j].s, len + 1)
1612 error = got_error(GOT_ERR_NO_SPACE);
1615 x += strlen(esc[j].s);
1620 (*escaped_html)[x] = orig_html[i];
1626 free(*escaped_html);
1627 *escaped_html = NULL;
1629 (*escaped_html)[x] = '\0';
1636 should_urlencode(int c)
1638 if (c <= ' ' || c >= 127)
1669 gotweb_urlencode(const char *str)
1677 for (s = str; *s; ++s) {
1679 if (should_urlencode(*s))
1683 escaped = calloc(1, len + 1);
1684 if (escaped == NULL)
1688 for (s = str; *s; ++s) {
1689 if (should_urlencode(*s)) {
1690 a = (*s & 0xF0) >> 4;
1694 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1695 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1704 gotweb_action_name(int action)
1735 gotweb_render_url(struct request *c, struct gotweb_url *url)
1737 const char *sep = "?", *action;
1741 action = gotweb_action_name(url->action);
1742 if (action != NULL) {
1743 if (fcgi_printf(c, "?action=%s", action) == -1)
1749 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1755 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1761 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1767 tmp = gotweb_urlencode(url->file);
1770 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1778 tmp = gotweb_urlencode(url->folder);
1781 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1789 tmp = gotweb_urlencode(url->headref);
1792 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1799 if (url->index_page != -1) {
1800 if (fcgi_printf(c, "%sindex_page=%d", sep,
1801 url->index_page) == -1)
1807 tmp = gotweb_urlencode(url->path);
1810 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1817 if (url->page != -1) {
1818 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1827 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1832 if (fcgi_printf(c, "<a href='") == -1)
1835 if (gotweb_render_url(c, url) == -1)
1838 if (fcgi_printf(c, "'>") == -1)
1842 r = fcgi_vprintf(c, fmt, ap);
1847 if (fcgi_printf(c, "</a>"))
1852 static struct got_repository *
1853 find_cached_repo(struct server *srv, const char *path)
1857 for (i = 0; i < srv->ncached_repos; i++) {
1858 if (strcmp(srv->cached_repos[i].path, path) == 0)
1859 return srv->cached_repos[i].repo;
1865 static const struct got_error *
1866 cache_repo(struct got_repository **new, struct server *srv,
1867 struct repo_dir *repo_dir, struct socket *sock)
1869 const struct got_error *error = NULL;
1870 struct got_repository *repo;
1871 struct cached_repo *cr;
1874 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1875 cr = &srv->cached_repos[srv->ncached_repos - 1];
1876 error = got_repo_close(cr->repo);
1877 memset(cr, 0, sizeof(*cr));
1878 srv->ncached_repos--;
1881 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1882 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1883 cr = &srv->cached_repos[0];
1886 cr = &srv->cached_repos[srv->ncached_repos];
1889 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1892 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1893 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1898 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1899 >= sizeof(cr->path)) {
1901 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1902 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1904 return got_error(GOT_ERR_NO_SPACE);
1908 srv->ncached_repos++;
1913 static const struct got_error *
1914 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1916 const struct got_error *error = NULL;
1917 struct socket *sock = c->sock;
1918 struct server *srv = c->srv;
1919 struct transport *t = c->t;
1920 struct got_repository *repo = NULL;
1924 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1925 GOTWEB_GIT_DIR) == -1)
1926 return got_error_from_errno("asprintf");
1928 dt = opendir(dir_test);
1932 repo_dir->path = dir_test;
1937 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1938 repo_dir->name) == -1)
1939 return got_error_from_errno("asprintf");
1941 dt = opendir(dir_test);
1943 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1946 repo_dir->path = dir_test;
1951 if (srv->respect_exportok &&
1952 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1953 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1957 repo = find_cached_repo(srv, repo_dir->path);
1959 error = cache_repo(&repo, srv, repo_dir, sock);
1964 error = gotweb_get_repo_description(&repo_dir->description, srv,
1965 repo_dir->path, dirfd(dt));
1968 error = got_get_repo_owner(&repo_dir->owner, c);
1971 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1974 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1978 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1979 error = got_error_from_errno("closedir");
1983 static const struct got_error *
1984 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1986 const struct got_error *error;
1988 *repo_dir = calloc(1, sizeof(**repo_dir));
1989 if (*repo_dir == NULL)
1990 return got_error_from_errno("calloc");
1992 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1993 error = got_error_from_errno("asprintf");
1998 (*repo_dir)->owner = NULL;
1999 (*repo_dir)->description = NULL;
2000 (*repo_dir)->url = NULL;
2001 (*repo_dir)->age = NULL;
2002 (*repo_dir)->path = NULL;
2007 static const struct got_error *
2008 gotweb_get_repo_description(char **description, struct server *srv,
2009 const char *dirpath, int dir)
2011 const struct got_error *error = NULL;
2016 *description = NULL;
2017 if (srv->show_repo_description == 0)
2020 fd = openat(dir, "description", O_RDONLY);
2022 if (errno != ENOENT && errno != EACCES) {
2023 error = got_error_from_errno_fmt("openat %s/%s",
2024 dirpath, "description");
2029 if (fstat(fd, &sb) == -1) {
2030 error = got_error_from_errno_fmt("fstat %s/%s",
2031 dirpath, "description");
2036 if (len > GOTWEBD_MAXDESCRSZ - 1)
2037 len = GOTWEBD_MAXDESCRSZ - 1;
2039 *description = calloc(len + 1, sizeof(**description));
2040 if (*description == NULL) {
2041 error = got_error_from_errno("calloc");
2045 if (read(fd, *description, len) == -1)
2046 error = got_error_from_errno("read");
2048 if (fd != -1 && close(fd) == -1 && error == NULL)
2049 error = got_error_from_errno("close");
2053 static const struct got_error *
2054 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2057 const struct got_error *error = NULL;
2063 if (srv->show_repo_cloneurl == 0)
2066 fd = openat(dir, "cloneurl", O_RDONLY);
2068 if (errno != ENOENT && errno != EACCES) {
2069 error = got_error_from_errno_fmt("openat %s/%s",
2070 dirpath, "cloneurl");
2075 if (fstat(fd, &sb) == -1) {
2076 error = got_error_from_errno_fmt("fstat %s/%s",
2077 dirpath, "cloneurl");
2082 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2083 len = GOTWEBD_MAXCLONEURLSZ - 1;
2085 *url = calloc(len + 1, sizeof(**url));
2087 error = got_error_from_errno("calloc");
2091 if (read(fd, *url, len) == -1)
2092 error = got_error_from_errno("read");
2094 if (fd != -1 && close(fd) == -1 && error == NULL)
2095 error = got_error_from_errno("close");
2099 const struct got_error *
2100 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2103 long long diff_time;
2104 const char *years = "years ago", *months = "months ago";
2105 const char *weeks = "weeks ago", *days = "days ago";
2106 const char *hours = "hours ago", *minutes = "minutes ago";
2107 const char *seconds = "seconds ago", *now = "right now";
2115 diff_time = time(NULL) - committer_time;
2116 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2117 if (asprintf(repo_age, "%lld %s",
2118 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2119 return got_error_from_errno("asprintf");
2120 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2121 if (asprintf(repo_age, "%lld %s",
2122 (diff_time / 60 / 60 / 24 / (365 / 12)),
2124 return got_error_from_errno("asprintf");
2125 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2126 if (asprintf(repo_age, "%lld %s",
2127 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2128 return got_error_from_errno("asprintf");
2129 } else if (diff_time > 60 * 60 * 24 * 2) {
2130 if (asprintf(repo_age, "%lld %s",
2131 (diff_time / 60 / 60 / 24), days) == -1)
2132 return got_error_from_errno("asprintf");
2133 } else if (diff_time > 60 * 60 * 2) {
2134 if (asprintf(repo_age, "%lld %s",
2135 (diff_time / 60 / 60), hours) == -1)
2136 return got_error_from_errno("asprintf");
2137 } else if (diff_time > 60 * 2) {
2138 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2140 return got_error_from_errno("asprintf");
2141 } else if (diff_time > 2) {
2142 if (asprintf(repo_age, "%lld %s", diff_time,
2144 return got_error_from_errno("asprintf");
2146 if (asprintf(repo_age, "%s", now) == -1)
2147 return got_error_from_errno("asprintf");
2151 if (gmtime_r(&committer_time, &tm) == NULL)
2152 return got_error_from_errno("gmtime_r");
2154 s = asctime_r(&tm, datebuf);
2156 return got_error_from_errno("asctime_r");
2158 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2159 return got_error_from_errno("asprintf");