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"
50 #include "got_compat.h"
56 static const struct querystring_keys querystring_keys[] = {
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
67 static const struct action_keys action_keys[] = {
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 static const struct got_error *gotweb_load_got_path(struct request *c,
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_diff(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tag(struct request *);
100 static const struct got_error *gotweb_render_tags(struct request *);
101 static const struct got_error *gotweb_render_tree(struct request *);
102 static const struct got_error *gotweb_render_branches(struct request *);
104 static void gotweb_free_querystring(struct querystring *);
105 static void gotweb_free_repo_dir(struct repo_dir *);
107 struct server *gotweb_get_server(uint8_t *, uint8_t *);
110 gotweb_process_request(struct request *c)
112 const struct got_error *error = NULL, *error2 = NULL;
113 struct server *srv = NULL;
114 struct querystring *qs = NULL;
115 struct repo_dir *repo_dir = NULL;
116 uint8_t err[] = "gotwebd experienced an error: ";
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
122 log_warnx("%s: %s", __func__, error->msg);
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
131 log_warnx("%s: error server is NULL", __func__);
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
138 log_warnx("%s: %s", __func__, error->msg);
142 error = gotweb_parse_querystring(&qs, c->querystring);
144 log_warnx("%s: %s", __func__, error->msg);
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
154 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == DIFF)) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
160 if (qs->action != INDEX) {
161 error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 error = gotweb_load_got_path(c, repo_dir);
165 c->t->repo_dir = repo_dir;
166 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
170 if (qs->action == BLOB) {
171 error = got_get_repo_commits(c, 1);
174 error = got_output_file_blob(c);
176 log_warnx("%s: %s", __func__, error->msg);
182 if (qs->action == RSS) {
183 error = gotweb_render_content_type_file(c,
184 "application/rss+xml;charset=utf-8",
185 repo_dir->name, ".rss");
187 log_warnx("%s: %s", __func__, error->msg);
191 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
193 log_warnx("%s: %s", __func__, error->msg);
196 if (gotweb_render_rss(c->tp) == -1)
202 error = gotweb_render_content_type(c, "text/html");
204 log_warnx("%s: %s", __func__, error->msg);
209 if (gotweb_render_header(c->tp) == -1)
219 error = gotweb_render_blame(c);
221 log_warnx("%s: %s", __func__, error->msg);
226 if (gotweb_render_briefs(c->tp) == -1)
230 error = got_get_repo_commits(c, srv->max_commits_display);
232 log_warnx("%s: %s", __func__, error->msg);
235 if (gotweb_render_commits(c->tp) == -1)
239 error = gotweb_render_diff(c);
241 log_warnx("%s: %s", __func__, error->msg);
246 error = gotweb_render_index(c);
248 log_warnx("%s: %s", __func__, error->msg);
253 error = gotweb_render_summary(c);
255 log_warnx("%s: %s", __func__, error->msg);
260 error = gotweb_render_tag(c);
262 log_warnx("%s: %s", __func__, error->msg);
267 error = gotweb_render_tags(c);
269 log_warnx("%s: %s", __func__, error->msg);
274 error = gotweb_render_tree(c);
276 log_warnx("%s: %s", __func__, error->msg);
282 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
283 "Erorr: Bad Querystring");
291 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
293 if (fcgi_printf(c, "\n%s", err) == -1)
296 if (fcgi_printf(c, "%s", error->msg) == -1)
299 if (fcgi_printf(c, "see daemon logs for details") == -1)
302 if (html && fcgi_printf(c, "</div>\n") == -1)
305 if (html && srv != NULL)
306 gotweb_render_footer(c->tp);
310 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
312 struct server *srv = NULL;
314 /* check against the server name first */
315 if (strlen(server_name) > 0)
316 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
317 if (strcmp(srv->name, server_name) == 0)
320 /* check against subdomain second */
321 if (strlen(subdomain) > 0)
322 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
323 if (strcmp(srv->name, subdomain) == 0)
326 /* if those fail, send first server */
327 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
334 const struct got_error *
335 gotweb_init_transport(struct transport **t)
337 const struct got_error *error = NULL;
339 *t = calloc(1, sizeof(**t));
341 return got_error_from_errno2("%s: calloc", __func__);
343 TAILQ_INIT(&(*t)->repo_commits);
344 TAILQ_INIT(&(*t)->repo_tags);
347 (*t)->repo_dir = NULL;
349 (*t)->next_id = NULL;
350 (*t)->prev_id = NULL;
357 static const struct got_error *
358 gotweb_init_querystring(struct querystring **qs)
360 const struct got_error *error = NULL;
362 *qs = calloc(1, sizeof(**qs));
364 return got_error_from_errno2("%s: calloc", __func__);
366 (*qs)->headref = strdup("HEAD");
367 if ((*qs)->headref == NULL) {
370 return got_error_from_errno2("%s: strdup", __func__);
373 (*qs)->action = INDEX;
374 (*qs)->commit = NULL;
376 (*qs)->folder = NULL;
377 (*qs)->index_page = 0;
383 static const struct got_error *
384 gotweb_parse_querystring(struct querystring **qs, char *qst)
386 const struct got_error *error = NULL;
387 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
388 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
395 return got_error_from_errno2("%s: strdup", __func__);
400 while (tok1_pair != NULL) {
401 strsep(&tok1_end, "&");
403 tok2 = strdup(tok1_pair);
406 return got_error_from_errno2("%s: strdup", __func__);
412 while (tok2_pair != NULL) {
413 strsep(&tok2_end, "=");
415 error = gotweb_assign_querystring(qs, tok2_pair,
420 tok2_pair = tok2_end;
423 tok1_pair = tok1_end;
434 * Adapted from usr.sbin/httpd/httpd.c url_decode.
436 static const struct got_error *
437 gotweb_urldecode(char *url)
449 /* Encoding character is followed by two hex chars */
450 if (!isxdigit((unsigned char)p[1]) ||
451 !isxdigit((unsigned char)p[2]) ||
452 (p[1] == '0' && p[2] == '0'))
453 return got_error(GOT_ERR_BAD_QUERYSTRING);
459 * We don't have to validate "hex" because it is
460 * guaranteed to include two hex chars followed by nul.
462 x = strtoul(hex, NULL, 16);
478 static const struct got_error *
479 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
481 const struct got_error *error = NULL;
485 error = gotweb_urldecode(value);
489 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
490 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
493 switch (querystring_keys[el_cnt].element) {
495 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
496 if (strcmp(value, action_keys[a_cnt].name) != 0)
498 else if (strcmp(value,
499 action_keys[a_cnt].name) == 0){
501 action_keys[a_cnt].action;
509 (*qs)->commit = strdup(value);
510 if ((*qs)->commit == NULL) {
511 error = got_error_from_errno2("%s: strdup",
517 (*qs)->file = strdup(value);
518 if ((*qs)->file == NULL) {
519 error = got_error_from_errno2("%s: strdup",
525 (*qs)->folder = strdup(value);
526 if ((*qs)->folder == NULL) {
527 error = got_error_from_errno2("%s: strdup",
533 free((*qs)->headref);
534 (*qs)->headref = strdup(value);
535 if ((*qs)->headref == NULL) {
536 error = got_error_from_errno2("%s: strdup",
542 if (strlen(value) == 0)
544 (*qs)->index_page = strtonum(value, INT64_MIN,
547 error = got_error_from_errno3("%s: strtonum %s",
551 if ((*qs)->index_page < 0)
552 (*qs)->index_page = 0;
555 (*qs)->path = strdup(value);
556 if ((*qs)->path == NULL) {
557 error = got_error_from_errno2("%s: strdup",
563 if (strlen(value) == 0)
565 (*qs)->page = strtonum(value, INT64_MIN,
568 error = got_error_from_errno3("%s: strtonum %s",
584 gotweb_free_repo_tag(struct repo_tag *rt)
589 free(rt->tag_commit);
590 free(rt->commit_msg);
597 gotweb_free_repo_commit(struct repo_commit *rc)
607 free(rc->commit_msg);
613 gotweb_free_querystring(struct querystring *qs)
626 gotweb_free_repo_dir(struct repo_dir *repo_dir)
628 if (repo_dir != NULL) {
629 free(repo_dir->name);
630 free(repo_dir->owner);
631 free(repo_dir->description);
634 free(repo_dir->path);
640 gotweb_free_transport(struct transport *t)
642 struct repo_commit *rc = NULL, *trc = NULL;
643 struct repo_tag *rt = NULL, *trt = NULL;
645 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
646 TAILQ_REMOVE(&t->repo_commits, rc, entry);
647 gotweb_free_repo_commit(rc);
649 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
650 TAILQ_REMOVE(&t->repo_tags, rt, entry);
651 gotweb_free_repo_tag(rt);
653 gotweb_free_repo_dir(t->repo_dir);
654 gotweb_free_querystring(t->qs);
660 const struct got_error *
661 gotweb_render_content_type(struct request *c, const char *type)
663 const char *csp = "default-src 'self'; script-src 'none'; "
664 "object-src 'none';";
667 "Content-Security-Policy: %s\r\n"
668 "Content-Type: %s\r\n\r\n",
673 const struct got_error *
674 gotweb_render_content_type_file(struct request *c, const char *type,
675 const char *file, const char *suffix)
677 fcgi_printf(c, "Content-type: %s\r\n"
678 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
679 type, file, suffix ? suffix : "");
684 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
685 struct gotweb_url *next, int *have_next)
687 struct transport *t = c->t;
688 struct querystring *qs = t->qs;
689 struct server *srv = c->srv;
691 *have_prev = *have_next = 0;
695 if (qs->index_page > 0) {
697 *prev = (struct gotweb_url){
699 .index_page = qs->index_page - 1,
703 if (t->next_disp == srv->max_repos_display &&
704 t->repos_total != (qs->index_page + 1) *
705 srv->max_repos_display) {
707 *next = (struct gotweb_url){
709 .index_page = qs->index_page + 1,
715 if (t->prev_id && qs->commit != NULL &&
716 strcmp(qs->commit, t->prev_id) != 0) {
718 *prev = (struct gotweb_url){
721 .page = qs->page - 1,
723 .commit = t->prev_id,
724 .headref = qs->headref,
729 *next = (struct gotweb_url){
732 .page = qs->page + 1,
734 .commit = t->next_id,
735 .headref = qs->headref,
740 if (t->prev_id && qs->commit != NULL &&
741 strcmp(qs->commit, t->prev_id) != 0) {
743 *prev = (struct gotweb_url){
746 .page = qs->page - 1,
748 .commit = t->prev_id,
749 .headref = qs->headref,
750 .folder = qs->folder,
756 *next = (struct gotweb_url){
759 .page = qs->page + 1,
761 .commit = t->next_id,
762 .headref = qs->headref,
763 .folder = qs->folder,
769 if (t->prev_id && qs->commit != NULL &&
770 strcmp(qs->commit, t->prev_id) != 0) {
772 *prev = (struct gotweb_url){
775 .page = qs->page - 1,
777 .commit = t->prev_id,
778 .headref = qs->headref,
783 *next = (struct gotweb_url){
786 .page = qs->page + 1,
788 .commit = t->next_id,
789 .headref = qs->headref,
796 static const struct got_error *
797 gotweb_render_index(struct request *c)
799 const struct got_error *error = NULL;
800 struct server *srv = c->srv;
801 struct transport *t = c->t;
802 struct querystring *qs = t->qs;
803 struct repo_dir *repo_dir = NULL;
805 struct dirent **sd_dent = NULL;
806 unsigned int d_cnt, d_i, d_disp = 0;
807 unsigned int d_skipped = 0;
810 d = opendir(srv->repos_path);
812 error = got_error_from_errno2("opendir", srv->repos_path);
816 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
819 error = got_error_from_errno2("scandir", srv->repos_path);
823 if (gotweb_render_repo_table_hdr(c->tp) == -1)
826 for (d_i = 0; d_i < d_cnt; d_i++) {
827 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
830 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
831 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
836 error = got_path_dirent_type(&type, srv->repos_path,
840 if (type != DT_DIR) {
845 if (qs->index_page > 0 && (qs->index_page *
846 srv->max_repos_display) > t->prev_disp) {
851 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
855 error = gotweb_load_got_path(c, repo_dir);
856 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
858 gotweb_free_repo_dir(repo_dir);
863 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
869 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
872 gotweb_free_repo_dir(repo_dir);
875 if (d_disp == srv->max_repos_display)
878 t->repos_total = d_cnt - d_skipped;
880 if (srv->max_repos_display == 0)
882 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
884 if (t->repos_total <= srv->max_repos ||
885 t->repos_total <= srv->max_repos_display)
888 if (gotweb_render_navs(c->tp) == -1)
892 for (d_i = 0; d_i < d_cnt; d_i++)
896 if (d != NULL && closedir(d) == EOF && error == NULL)
897 error = got_error_from_errno("closedir");
901 static const struct got_error *
902 gotweb_render_blame(struct request *c)
904 const struct got_error *error = NULL;
905 struct transport *t = c->t;
906 struct repo_commit *rc = NULL;
907 char *age = NULL, *msg = NULL;
910 error = got_get_repo_commits(c, 1);
914 rc = TAILQ_FIRST(&t->repo_commits);
916 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
919 error = gotweb_escape_html(&msg, rc->commit_msg);
923 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
924 "<div id='blame_title'>Blame</div>\n"
925 "</div>\n" /* #blame_title_wrapper */
926 "<div id='blame_content'>\n"
927 "<div id='blame_header_wrapper'>\n"
928 "<div id='blame_header'>\n"
929 "<div class='header_age_title'>Date:</div>\n"
930 "<div class='header_age'>%s</div>\n"
931 "<div id='header_commit_msg_title'>Message:</div>\n"
932 "<div id='header_commit_msg'>%s</div>\n"
933 "</div>\n" /* #blame_header */
934 "</div>\n" /* #blame_header_wrapper */
935 "<div class='dotted_line'></div>\n"
936 "<div id='blame'>\n",
942 error = got_output_file_blame(c);
946 fcgi_printf(c, "</div>\n" /* #blame */
947 "</div>\n"); /* #blame_content */
954 static const struct got_error *
955 gotweb_render_branches(struct request *c)
957 const struct got_error *error = NULL;
958 struct got_reflist_head refs;
959 struct got_reflist_entry *re;
960 struct transport *t = c->t;
961 struct querystring *qs = t->qs;
962 struct got_repository *repo = t->repo;
963 char *escaped_refname = NULL;
969 error = got_ref_list(&refs, repo, "refs/heads",
970 got_ref_cmp_by_name, NULL);
974 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
975 "<div id='branches_title'>Branches</div>\n"
976 "</div>\n" /* #branches_title_wrapper */
977 "<div id='branches_content'>\n");
981 TAILQ_FOREACH(re, &refs, entry) {
982 const char *refname = NULL;
984 if (got_ref_is_symbolic(re->ref))
987 refname = got_ref_get_name(re->ref);
988 if (refname == NULL) {
989 error = got_error_from_errno("strdup");
992 if (strncmp(refname, "refs/heads/", 11) != 0)
995 error = got_get_repo_age(&age, c, refname, TM_DIFF);
999 if (strncmp(refname, "refs/heads/", 11) == 0)
1001 error = gotweb_escape_html(&escaped_refname, refname);
1005 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1006 "<div class='branches_age'>%s</div>\n"
1007 "<div class='branches_space'> </div>\n"
1008 "<div class='branch'>", age);
1012 r = gotweb_link(c, &(struct gotweb_url){
1018 }, "%s", escaped_refname);
1022 if (fcgi_printf(c, "</div>\n" /* .branch */
1023 "<div class='navs_wrapper'>\n"
1024 "<div class='navs'>") == -1)
1027 r = gotweb_link(c, &(struct gotweb_url){
1037 if (fcgi_printf(c, " | ") == -1)
1040 r = gotweb_link(c, &(struct gotweb_url){
1046 }, "commit briefs");
1050 if (fcgi_printf(c, " | ") == -1)
1053 r = gotweb_link(c, &(struct gotweb_url){
1063 r = fcgi_printf(c, "</div>\n" /* .navs */
1064 "</div>\n" /* .navs_wrapper */
1065 "<div class='dotted_line'></div>\n"
1066 "</div>\n"); /* .branches_wrapper */
1072 free(escaped_refname);
1073 escaped_refname = NULL;
1075 fcgi_printf(c, "</div>\n"); /* #branches_content */
1078 free(escaped_refname);
1079 got_ref_list_free(&refs);
1083 static const struct got_error *
1084 gotweb_render_tree(struct request *c)
1086 const struct got_error *error = NULL;
1087 struct transport *t = c->t;
1088 struct repo_commit *rc = NULL;
1089 char *age = NULL, *msg = NULL;
1092 error = got_get_repo_commits(c, 1);
1096 rc = TAILQ_FIRST(&t->repo_commits);
1098 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1102 error = gotweb_escape_html(&msg, rc->commit_msg);
1106 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1107 "<div id='tree_title'>Tree</div>\n"
1108 "</div>\n" /* #tree_title_wrapper */
1109 "<div id='tree_content'>\n"
1110 "<div id='tree_header_wrapper'>\n"
1111 "<div id='tree_header'>\n"
1112 "<div id='header_tree_title'>Tree:</div>\n"
1113 "<div id='header_tree'>%s</div>\n"
1114 "<div class='header_age_title'>Date:</div>\n"
1115 "<div class='header_age'>%s</div>\n"
1116 "<div id='header_commit_msg_title'>Message:</div>\n"
1117 "<div id='header_commit_msg'>%s</div>\n"
1118 "</div>\n" /* #tree_header */
1119 "</div>\n" /* #tree_header_wrapper */
1120 "<div class='dotted_line'></div>\n"
1121 "<div id='tree'>\n",
1128 error = got_output_repo_tree(c);
1132 fcgi_printf(c, "</div>\n"); /* #tree */
1133 fcgi_printf(c, "</div>\n"); /* #tree_content */
1140 static const struct got_error *
1141 gotweb_render_diff(struct request *c)
1143 const struct got_error *error = NULL;
1144 struct transport *t = c->t;
1145 struct repo_commit *rc = NULL;
1146 char *age = NULL, *author = NULL, *msg = NULL;
1149 error = got_get_repo_commits(c, 1);
1153 rc = TAILQ_FIRST(&t->repo_commits);
1155 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1158 error = gotweb_escape_html(&author, rc->author);
1161 error = gotweb_escape_html(&msg, rc->commit_msg);
1165 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1166 "<div id='diff_title'>Commit Diff</div>\n"
1167 "</div>\n" /* #diff_title_wrapper */
1168 "<div id='diff_content'>\n"
1169 "<div id='diff_header_wrapper'>\n"
1170 "<div id='diff_header'>\n"
1171 "<div id='header_diff_title'>Diff:</div>\n"
1172 "<div id='header_diff'>%s<br />%s</div>\n"
1173 "<div class='header_commit_title'>Commit:</div>\n"
1174 "<div class='header_commit'>%s</div>\n"
1175 "<div id='header_tree_title'>Tree:</div>\n"
1176 "<div id='header_tree'>%s</div>\n"
1177 "<div class='header_author_title'>Author:</div>\n"
1178 "<div class='header_author'>%s</div>\n"
1179 "<div class='header_age_title'>Date:</div>\n"
1180 "<div class='header_age'>%s</div>\n"
1181 "<div id='header_commit_msg_title'>Message:</div>\n"
1182 "<div id='header_commit_msg'>%s</div>\n"
1183 "</div>\n" /* #diff_header */
1184 "</div>\n" /* #diff_header_wrapper */
1185 "<div class='dotted_line'></div>\n"
1186 "<div id='diff'>\n",
1187 rc->parent_id, rc->commit_id,
1196 error = got_output_repo_diff(c);
1200 fcgi_printf(c, "</div>\n"); /* #diff */
1201 fcgi_printf(c, "</div>\n"); /* #diff_content */
1209 static const struct got_error *
1210 gotweb_render_summary(struct request *c)
1212 const struct got_error *error = NULL;
1213 struct transport *t = c->t;
1214 struct server *srv = c->srv;
1217 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1220 if (srv->show_repo_description) {
1222 "<div id='description_title'>Description:</div>\n"
1223 "<div id='description'>%s</div>\n",
1224 t->repo_dir->description ? t->repo_dir->description : "");
1229 if (srv->show_repo_owner) {
1231 "<div id='repo_owner_title'>Owner:</div>\n"
1232 "<div id='repo_owner'>%s</div>\n",
1233 t->repo_dir->owner ? t->repo_dir->owner : "");
1238 if (srv->show_repo_age) {
1240 "<div id='last_change_title'>Last Change:</div>\n"
1241 "<div id='last_change'>%s</div>\n",
1247 if (srv->show_repo_cloneurl) {
1249 "<div id='cloneurl_title'>Clone URL:</div>\n"
1250 "<div id='cloneurl'>%s</div>\n",
1251 t->repo_dir->url ? t->repo_dir->url : "");
1256 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1260 if (gotweb_render_briefs(c->tp) == -1)
1263 error = gotweb_render_tags(c);
1265 log_warnx("%s: %s", __func__, error->msg);
1269 error = gotweb_render_branches(c);
1271 log_warnx("%s: %s", __func__, error->msg);
1276 static const struct got_error *
1277 gotweb_render_tag(struct request *c)
1279 const struct got_error *error = NULL;
1280 struct repo_tag *rt = NULL;
1281 struct transport *t = c->t;
1282 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1284 error = got_get_repo_tags(c, 1);
1288 if (t->tag_count == 0) {
1289 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1294 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1296 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1299 error = gotweb_escape_html(&author, rt->tagger);
1302 error = gotweb_escape_html(&msg, rt->commit_msg);
1306 tagname = rt->tag_name;
1307 if (strncmp(tagname, "refs/", 5) == 0)
1309 error = gotweb_escape_html(&tagname, tagname);
1313 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1314 "<div id='tags_title'>Tag</div>\n"
1315 "</div>\n" /* #tags_title_wrapper */
1316 "<div id='tags_content'>\n"
1317 "<div id='tag_header_wrapper'>\n"
1318 "<div id='tag_header'>\n"
1319 "<div class='header_commit_title'>Commit:</div>\n"
1320 "<div class='header_commit'>%s"
1321 " <span class='refs_str'>(%s)</span></div>\n"
1322 "<div class='header_author_title'>Tagger:</div>\n"
1323 "<div class='header_author'>%s</div>\n"
1324 "<div class='header_age_title'>Date:</div>\n"
1325 "<div class='header_age'>%s</div>\n"
1326 "<div id='header_commit_msg_title'>Message:</div>\n"
1327 "<div id='header_commit_msg'>%s</div>\n"
1328 "</div>\n" /* #tag_header */
1329 "<div class='dotted_line'></div>\n"
1330 "<div id='tag_commit'>\n%s</div>"
1331 "</div>" /* #tag_header_wrapper */
1332 "</div>", /* #tags_content */
1347 static const struct got_error *
1348 gotweb_render_tags(struct request *c)
1350 const struct got_error *error = NULL;
1351 struct repo_tag *rt = NULL;
1352 struct server *srv = c->srv;
1353 struct transport *t = c->t;
1354 struct querystring *qs = t->qs;
1355 struct repo_dir *repo_dir = t->repo_dir;
1356 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1357 int r, commit_found = 0;
1359 if (qs->action == BRIEFS) {
1361 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1363 error = got_get_repo_tags(c, srv->max_commits_display);
1367 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1368 "<div id='tags_title'>Tags</div>\n"
1369 "</div>\n" /* #tags_title_wrapper */
1370 "<div id='tags_content'>\n");
1374 if (t->tag_count == 0) {
1375 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1376 "This repository contains no tags");
1381 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1382 if (commit_found == 0 && qs->commit != NULL) {
1383 if (strcmp(qs->commit, rt->commit_id) != 0)
1388 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1392 tagname = rt->tag_name;
1393 if (strncmp(tagname, "refs/tags/", 10) == 0)
1395 error = gotweb_escape_html(&tagname, tagname);
1399 if (rt->tag_commit != NULL) {
1400 newline = strchr(rt->tag_commit, '\n');
1403 error = gotweb_escape_html(&msg, rt->tag_commit);
1408 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1409 "<div class='tag'>%s</div>\n"
1410 "<div class='tag_log'>", age, tagname) == -1)
1413 r = gotweb_link(c, &(struct gotweb_url){
1417 .path = repo_dir->name,
1418 .commit = rt->commit_id,
1419 }, "%s", msg ? msg : "");
1423 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1424 "<div class='navs_wrapper'>\n"
1425 "<div class='navs'>") == -1)
1428 r = gotweb_link(c, &(struct gotweb_url){
1432 .path = repo_dir->name,
1433 .commit = rt->commit_id,
1438 if (fcgi_printf(c, " | ") == -1)
1441 r = gotweb_link(c, &(struct gotweb_url){
1445 .path = repo_dir->name,
1446 .commit = rt->commit_id,
1447 }, "commit briefs");
1451 if (fcgi_printf(c, " | ") == -1)
1454 r = gotweb_link(c, &(struct gotweb_url){
1458 .path = repo_dir->name,
1459 .commit = rt->commit_id,
1465 "</div>\n" /* .navs */
1466 "</div>\n" /* .navs_wrapper */
1467 "<div class='dotted_line'></div>\n");
1478 if (t->next_id || t->prev_id) {
1479 if (gotweb_render_navs(c->tp) == -1)
1482 fcgi_printf(c, "</div>\n"); /* #tags_content */
1490 const struct got_error *
1491 gotweb_escape_html(char **escaped_html, const char *orig_html)
1493 const struct got_error *error = NULL;
1494 struct escape_pair {
1505 size_t orig_len, len;
1508 orig_len = strlen(orig_html);
1510 for (i = 0; i < orig_len; i++) {
1511 for (j = 0; j < nitems(esc); j++) {
1512 if (orig_html[i] != esc[j].c)
1514 len += strlen(esc[j].s) - 1 /* escaped char */;
1518 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1519 if (*escaped_html == NULL)
1520 return got_error_from_errno("calloc");
1523 for (i = 0; i < orig_len; i++) {
1525 for (j = 0; j < nitems(esc); j++) {
1526 if (orig_html[i] != esc[j].c)
1529 if (strlcat(*escaped_html, esc[j].s, len + 1)
1531 error = got_error(GOT_ERR_NO_SPACE);
1534 x += strlen(esc[j].s);
1539 (*escaped_html)[x] = orig_html[i];
1545 free(*escaped_html);
1546 *escaped_html = NULL;
1548 (*escaped_html)[x] = '\0';
1555 should_urlencode(int c)
1557 if (c <= ' ' || c >= 127)
1588 gotweb_urlencode(const char *str)
1596 for (s = str; *s; ++s) {
1598 if (should_urlencode(*s))
1602 escaped = calloc(1, len + 1);
1603 if (escaped == NULL)
1607 for (s = str; *s; ++s) {
1608 if (should_urlencode(*s)) {
1609 a = (*s & 0xF0) >> 4;
1613 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1614 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1623 gotweb_action_name(int action)
1656 gotweb_render_url(struct request *c, struct gotweb_url *url)
1658 const char *sep = "?", *action;
1662 action = gotweb_action_name(url->action);
1663 if (action != NULL) {
1664 if (fcgi_printf(c, "?action=%s", action) == -1)
1670 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1676 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1682 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1688 tmp = gotweb_urlencode(url->file);
1691 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1699 tmp = gotweb_urlencode(url->folder);
1702 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1710 tmp = gotweb_urlencode(url->headref);
1713 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1720 if (url->index_page != -1) {
1721 if (fcgi_printf(c, "%sindex_page=%d", sep,
1722 url->index_page) == -1)
1728 tmp = gotweb_urlencode(url->path);
1731 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1738 if (url->page != -1) {
1739 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1748 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1750 struct template *tp = c->tp;
1751 const char *proto = c->https ? "https" : "http";
1753 if (fcgi_puts(tp, proto) == -1 ||
1754 fcgi_puts(tp, "://") == -1 ||
1755 tp_htmlescape(tp, c->server_name) == -1 ||
1756 tp_htmlescape(tp, c->document_uri) == -1)
1759 return gotweb_render_url(c, url);
1763 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1768 if (fcgi_printf(c, "<a href='") == -1)
1771 if (gotweb_render_url(c, url) == -1)
1774 if (fcgi_printf(c, "'>") == -1)
1778 r = fcgi_vprintf(c, fmt, ap);
1783 if (fcgi_printf(c, "</a>"))
1788 static struct got_repository *
1789 find_cached_repo(struct server *srv, const char *path)
1793 for (i = 0; i < srv->ncached_repos; i++) {
1794 if (strcmp(srv->cached_repos[i].path, path) == 0)
1795 return srv->cached_repos[i].repo;
1801 static const struct got_error *
1802 cache_repo(struct got_repository **new, struct server *srv,
1803 struct repo_dir *repo_dir, struct socket *sock)
1805 const struct got_error *error = NULL;
1806 struct got_repository *repo;
1807 struct cached_repo *cr;
1810 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1811 cr = &srv->cached_repos[srv->ncached_repos - 1];
1812 error = got_repo_close(cr->repo);
1813 memset(cr, 0, sizeof(*cr));
1814 srv->ncached_repos--;
1817 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1818 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1819 cr = &srv->cached_repos[0];
1822 cr = &srv->cached_repos[srv->ncached_repos];
1825 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1828 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1829 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1834 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1835 >= sizeof(cr->path)) {
1837 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1838 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1840 return got_error(GOT_ERR_NO_SPACE);
1844 srv->ncached_repos++;
1849 static const struct got_error *
1850 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1852 const struct got_error *error = NULL;
1853 struct socket *sock = c->sock;
1854 struct server *srv = c->srv;
1855 struct transport *t = c->t;
1856 struct got_repository *repo = NULL;
1860 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1861 GOTWEB_GIT_DIR) == -1)
1862 return got_error_from_errno("asprintf");
1864 dt = opendir(dir_test);
1868 repo_dir->path = dir_test;
1873 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1874 repo_dir->name) == -1)
1875 return got_error_from_errno("asprintf");
1877 dt = opendir(dir_test);
1879 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1882 repo_dir->path = dir_test;
1887 if (srv->respect_exportok &&
1888 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1889 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1893 repo = find_cached_repo(srv, repo_dir->path);
1895 error = cache_repo(&repo, srv, repo_dir, sock);
1900 error = gotweb_get_repo_description(&repo_dir->description, srv,
1901 repo_dir->path, dirfd(dt));
1904 error = got_get_repo_owner(&repo_dir->owner, c);
1907 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1910 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1914 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1915 error = got_error_from_errno("closedir");
1919 static const struct got_error *
1920 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1922 const struct got_error *error;
1924 *repo_dir = calloc(1, sizeof(**repo_dir));
1925 if (*repo_dir == NULL)
1926 return got_error_from_errno("calloc");
1928 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1929 error = got_error_from_errno("asprintf");
1934 (*repo_dir)->owner = NULL;
1935 (*repo_dir)->description = NULL;
1936 (*repo_dir)->url = NULL;
1937 (*repo_dir)->age = NULL;
1938 (*repo_dir)->path = NULL;
1943 static const struct got_error *
1944 gotweb_get_repo_description(char **description, struct server *srv,
1945 const char *dirpath, int dir)
1947 const struct got_error *error = NULL;
1952 *description = NULL;
1953 if (srv->show_repo_description == 0)
1956 fd = openat(dir, "description", O_RDONLY);
1958 if (errno != ENOENT && errno != EACCES) {
1959 error = got_error_from_errno_fmt("openat %s/%s",
1960 dirpath, "description");
1965 if (fstat(fd, &sb) == -1) {
1966 error = got_error_from_errno_fmt("fstat %s/%s",
1967 dirpath, "description");
1972 if (len > GOTWEBD_MAXDESCRSZ - 1)
1973 len = GOTWEBD_MAXDESCRSZ - 1;
1975 *description = calloc(len + 1, sizeof(**description));
1976 if (*description == NULL) {
1977 error = got_error_from_errno("calloc");
1981 if (read(fd, *description, len) == -1)
1982 error = got_error_from_errno("read");
1984 if (fd != -1 && close(fd) == -1 && error == NULL)
1985 error = got_error_from_errno("close");
1989 static const struct got_error *
1990 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1993 const struct got_error *error = NULL;
1999 if (srv->show_repo_cloneurl == 0)
2002 fd = openat(dir, "cloneurl", O_RDONLY);
2004 if (errno != ENOENT && errno != EACCES) {
2005 error = got_error_from_errno_fmt("openat %s/%s",
2006 dirpath, "cloneurl");
2011 if (fstat(fd, &sb) == -1) {
2012 error = got_error_from_errno_fmt("fstat %s/%s",
2013 dirpath, "cloneurl");
2018 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2019 len = GOTWEBD_MAXCLONEURLSZ - 1;
2021 *url = calloc(len + 1, sizeof(**url));
2023 error = got_error_from_errno("calloc");
2027 if (read(fd, *url, len) == -1)
2028 error = got_error_from_errno("read");
2030 if (fd != -1 && close(fd) == -1 && error == NULL)
2031 error = got_error_from_errno("close");
2035 const struct got_error *
2036 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2039 long long diff_time;
2040 const char *years = "years ago", *months = "months ago";
2041 const char *weeks = "weeks ago", *days = "days ago";
2042 const char *hours = "hours ago", *minutes = "minutes ago";
2043 const char *seconds = "seconds ago", *now = "right now";
2052 diff_time = time(NULL) - committer_time;
2053 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2054 if (asprintf(repo_age, "%lld %s",
2055 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2056 return got_error_from_errno("asprintf");
2057 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2058 if (asprintf(repo_age, "%lld %s",
2059 (diff_time / 60 / 60 / 24 / (365 / 12)),
2061 return got_error_from_errno("asprintf");
2062 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2063 if (asprintf(repo_age, "%lld %s",
2064 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2065 return got_error_from_errno("asprintf");
2066 } else if (diff_time > 60 * 60 * 24 * 2) {
2067 if (asprintf(repo_age, "%lld %s",
2068 (diff_time / 60 / 60 / 24), days) == -1)
2069 return got_error_from_errno("asprintf");
2070 } else if (diff_time > 60 * 60 * 2) {
2071 if (asprintf(repo_age, "%lld %s",
2072 (diff_time / 60 / 60), hours) == -1)
2073 return got_error_from_errno("asprintf");
2074 } else if (diff_time > 60 * 2) {
2075 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2077 return got_error_from_errno("asprintf");
2078 } else if (diff_time > 2) {
2079 if (asprintf(repo_age, "%lld %s", diff_time,
2081 return got_error_from_errno("asprintf");
2083 if (asprintf(repo_age, "%s", now) == -1)
2084 return got_error_from_errno("asprintf");
2088 if (gmtime_r(&committer_time, &tm) == NULL)
2089 return got_error_from_errno("gmtime_r");
2091 s = asctime_r(&tm, datebuf);
2093 return got_error_from_errno("asctime_r");
2095 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2096 return got_error_from_errno("asprintf");
2099 if (gmtime_r(&committer_time, &tm) == NULL)
2100 return got_error_from_errno("gmtime_r");
2102 r = strftime(datebuf, sizeof(datebuf),
2103 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2105 return got_error(GOT_ERR_NO_SPACE);
2107 *repo_age = strdup(datebuf);
2108 if (*repo_age == NULL)
2109 return got_error_from_errno("asprintf");