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>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
54 static const struct querystring_keys querystring_keys[] = {
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
65 static const struct action_keys action_keys[] = {
69 { "commits", COMMITS },
73 { "summary", SUMMARY },
79 static const struct got_error *gotweb_init_querystring(struct querystring **);
80 static const struct got_error *gotweb_parse_querystring(struct querystring **,
82 static const struct got_error *gotweb_assign_querystring(struct querystring **,
84 static const struct got_error *gotweb_render_index(struct request *);
85 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
87 static const struct got_error *gotweb_load_got_path(struct request *c,
89 static const struct got_error *gotweb_get_repo_description(char **,
90 struct server *, const char *, int);
91 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
93 static const struct got_error *gotweb_render_blame(struct request *);
94 static const struct got_error *gotweb_render_commits(struct request *);
95 static const struct got_error *gotweb_render_diff(struct request *);
96 static const struct got_error *gotweb_render_summary(struct request *);
97 static const struct got_error *gotweb_render_tag(struct request *);
98 static const struct got_error *gotweb_render_tags(struct request *);
99 static const struct got_error *gotweb_render_tree(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 const struct got_error *gotweb_render_navs(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 /* render top of page */
171 if (qs != NULL && qs->action == BLOB) {
172 error = got_get_repo_commits(c, 1);
175 error = got_output_file_blob(c);
177 log_warnx("%s: %s", __func__, error->msg);
184 error = gotweb_render_content_type(c, "text/html");
186 log_warnx("%s: %s", __func__, error->msg);
191 if (gotweb_render_header(c->tp) == -1)
201 error = gotweb_render_blame(c);
203 log_warnx("%s: %s", __func__, error->msg);
208 if (gotweb_render_briefs(c->tp) == -1)
212 error = gotweb_render_commits(c);
214 log_warnx("%s: %s", __func__, error->msg);
219 error = gotweb_render_diff(c);
221 log_warnx("%s: %s", __func__, error->msg);
226 error = gotweb_render_index(c);
228 log_warnx("%s: %s", __func__, error->msg);
233 error = gotweb_render_summary(c);
235 log_warnx("%s: %s", __func__, error->msg);
240 error = gotweb_render_tag(c);
242 log_warnx("%s: %s", __func__, error->msg);
247 error = gotweb_render_tags(c);
249 log_warnx("%s: %s", __func__, error->msg);
254 error = gotweb_render_tree(c);
256 log_warnx("%s: %s", __func__, error->msg);
262 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
263 "Erorr: Bad Querystring");
271 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
273 if (fcgi_printf(c, "\n%s", err) == -1)
276 if (fcgi_printf(c, "%s", error->msg) == -1)
279 if (fcgi_printf(c, "see daemon logs for details") == -1)
282 if (html && fcgi_printf(c, "</div>\n") == -1)
285 if (html && srv != NULL)
286 gotweb_render_footer(c->tp);
290 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
292 struct server *srv = NULL;
294 /* check against the server name first */
295 if (strlen(server_name) > 0)
296 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
297 if (strcmp(srv->name, server_name) == 0)
300 /* check against subdomain second */
301 if (strlen(subdomain) > 0)
302 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
303 if (strcmp(srv->name, subdomain) == 0)
306 /* if those fail, send first server */
307 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
314 const struct got_error *
315 gotweb_init_transport(struct transport **t)
317 const struct got_error *error = NULL;
319 *t = calloc(1, sizeof(**t));
321 return got_error_from_errno2("%s: calloc", __func__);
323 TAILQ_INIT(&(*t)->repo_commits);
324 TAILQ_INIT(&(*t)->repo_tags);
327 (*t)->repo_dir = NULL;
329 (*t)->next_id = NULL;
330 (*t)->prev_id = NULL;
337 static const struct got_error *
338 gotweb_init_querystring(struct querystring **qs)
340 const struct got_error *error = NULL;
342 *qs = calloc(1, sizeof(**qs));
344 return got_error_from_errno2("%s: calloc", __func__);
346 (*qs)->headref = strdup("HEAD");
347 if ((*qs)->headref == NULL) {
350 return got_error_from_errno2("%s: strdup", __func__);
353 (*qs)->action = INDEX;
354 (*qs)->commit = NULL;
356 (*qs)->folder = NULL;
357 (*qs)->index_page = 0;
358 (*qs)->index_page_str = NULL;
364 static const struct got_error *
365 gotweb_parse_querystring(struct querystring **qs, char *qst)
367 const struct got_error *error = NULL;
368 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
369 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
376 return got_error_from_errno2("%s: strdup", __func__);
381 while (tok1_pair != NULL) {
382 strsep(&tok1_end, "&");
384 tok2 = strdup(tok1_pair);
387 return got_error_from_errno2("%s: strdup", __func__);
393 while (tok2_pair != NULL) {
394 strsep(&tok2_end, "=");
396 error = gotweb_assign_querystring(qs, tok2_pair,
401 tok2_pair = tok2_end;
404 tok1_pair = tok1_end;
415 * Adapted from usr.sbin/httpd/httpd.c url_decode.
417 static const struct got_error *
418 gotweb_urldecode(char *url)
430 /* Encoding character is followed by two hex chars */
431 if (!isxdigit((unsigned char)p[1]) ||
432 !isxdigit((unsigned char)p[2]) ||
433 (p[1] == '0' && p[2] == '0'))
434 return got_error(GOT_ERR_BAD_QUERYSTRING);
440 * We don't have to validate "hex" because it is
441 * guaranteed to include two hex chars followed by nul.
443 x = strtoul(hex, NULL, 16);
459 static const struct got_error *
460 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
462 const struct got_error *error = NULL;
466 error = gotweb_urldecode(value);
470 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
471 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
474 switch (querystring_keys[el_cnt].element) {
476 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
477 if (strcmp(value, action_keys[a_cnt].name) != 0)
479 else if (strcmp(value,
480 action_keys[a_cnt].name) == 0){
482 action_keys[a_cnt].action;
490 (*qs)->commit = strdup(value);
491 if ((*qs)->commit == NULL) {
492 error = got_error_from_errno2("%s: strdup",
498 (*qs)->file = strdup(value);
499 if ((*qs)->file == NULL) {
500 error = got_error_from_errno2("%s: strdup",
506 (*qs)->folder = strdup(value);
507 if ((*qs)->folder == NULL) {
508 error = got_error_from_errno2("%s: strdup",
514 free((*qs)->headref);
515 (*qs)->headref = strdup(value);
516 if ((*qs)->headref == NULL) {
517 error = got_error_from_errno2("%s: strdup",
523 if (strlen(value) == 0)
525 (*qs)->index_page_str = strdup(value);
526 if ((*qs)->index_page_str == NULL) {
527 error = got_error_from_errno2("%s: strdup",
531 (*qs)->index_page = strtonum(value, INT64_MIN,
534 error = got_error_from_errno3("%s: strtonum %s",
538 if ((*qs)->index_page < 0) {
539 (*qs)->index_page = 0;
540 sprintf((*qs)->index_page_str, "%d", 0);
544 (*qs)->path = strdup(value);
545 if ((*qs)->path == NULL) {
546 error = got_error_from_errno2("%s: strdup",
552 if (strlen(value) == 0)
554 (*qs)->page_str = strdup(value);
555 if ((*qs)->page_str == NULL) {
556 error = got_error_from_errno2("%s: strdup",
560 (*qs)->page = strtonum(value, INT64_MIN,
563 error = got_error_from_errno3("%s: strtonum %s",
567 if ((*qs)->page < 0) {
569 sprintf((*qs)->page_str, "%d", 0);
581 gotweb_free_repo_tag(struct repo_tag *rt)
586 free(rt->tag_commit);
587 free(rt->commit_msg);
594 gotweb_free_repo_commit(struct repo_commit *rc)
604 free(rc->commit_msg);
610 gotweb_free_querystring(struct querystring *qs)
617 free(qs->index_page_str);
625 gotweb_free_repo_dir(struct repo_dir *repo_dir)
627 if (repo_dir != NULL) {
628 free(repo_dir->name);
629 free(repo_dir->owner);
630 free(repo_dir->description);
633 free(repo_dir->path);
639 gotweb_free_transport(struct transport *t)
641 struct repo_commit *rc = NULL, *trc = NULL;
642 struct repo_tag *rt = NULL, *trt = NULL;
644 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
645 TAILQ_REMOVE(&t->repo_commits, rc, entry);
646 gotweb_free_repo_commit(rc);
648 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
649 TAILQ_REMOVE(&t->repo_tags, rt, entry);
650 gotweb_free_repo_tag(rt);
652 gotweb_free_repo_dir(t->repo_dir);
653 gotweb_free_querystring(t->qs);
659 const struct got_error *
660 gotweb_render_content_type(struct request *c, const uint8_t *type)
662 const char *csp = "default-src 'self'; script-src 'none'; "
663 "object-src 'none';";
666 "Content-Security-Policy: %s\r\n"
667 "Content-Type: %s\r\n\r\n",
672 const struct got_error *
673 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
676 fcgi_printf(c, "Content-type: %s\r\n"
677 "Content-disposition: attachment; filename=%s\r\n\r\n",
682 const struct got_error *
683 gotweb_render_navs(struct request *c)
685 const struct got_error *error = NULL;
686 struct transport *t = c->t;
687 struct querystring *qs = t->qs;
688 struct server *srv = c->srv;
691 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
697 if (qs->index_page > 0) {
698 struct gotweb_url url = {
700 .index_page = qs->index_page - 1,
704 r = gotweb_link(c, &url, "Previous");
708 if (t->prev_id && qs->commit != NULL &&
709 strcmp(qs->commit, t->prev_id) != 0) {
710 struct gotweb_url url = {
713 .page = qs->page - 1,
715 .commit = t->prev_id,
716 .headref = qs->headref,
719 r = gotweb_link(c, &url, "Previous");
723 if (t->prev_id && qs->commit != NULL &&
724 strcmp(qs->commit, t->prev_id) != 0) {
725 struct gotweb_url url = {
728 .page = qs->page - 1,
730 .commit = t->prev_id,
731 .headref = qs->headref,
732 .folder = qs->folder,
736 r = gotweb_link(c, &url, "Previous");
740 if (t->prev_id && qs->commit != NULL &&
741 strcmp(qs->commit, t->prev_id) != 0) {
742 struct gotweb_url url = {
745 .page = qs->page - 1,
747 .commit = t->prev_id,
748 .headref = qs->headref,
751 r = gotweb_link(c, &url, "Previous");
759 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
760 "<div id='nav_next'>");
766 if (t->next_disp == srv->max_repos_display &&
767 t->repos_total != (qs->index_page + 1) *
768 srv->max_repos_display) {
769 struct gotweb_url url = {
771 .index_page = qs->index_page + 1,
775 r = gotweb_link(c, &url, "Next");
780 struct gotweb_url url = {
783 .page = qs->page + 1,
785 .commit = t->next_id,
786 .headref = qs->headref,
789 r = gotweb_link(c, &url, "Next");
794 struct gotweb_url url = {
797 .page = qs->page + 1,
799 .commit = t->next_id,
800 .headref = qs->headref,
801 .folder = qs->folder,
805 r = gotweb_link(c, &url, "Next");
810 struct gotweb_url url = {
813 .page = qs->page + 1,
815 .commit = t->next_id,
816 .headref = qs->headref,
819 r = gotweb_link(c, &url, "Next");
826 fcgi_printf(c, "</div>\n"); /* #nav_next */
827 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
836 static const struct got_error *
837 gotweb_render_index(struct request *c)
839 const struct got_error *error = NULL;
840 struct server *srv = c->srv;
841 struct transport *t = c->t;
842 struct querystring *qs = t->qs;
843 struct repo_dir *repo_dir = NULL;
845 struct dirent **sd_dent = NULL;
846 unsigned int d_cnt, d_i, d_disp = 0;
847 unsigned int d_skipped = 0;
850 d = opendir(srv->repos_path);
852 error = got_error_from_errno2("opendir", srv->repos_path);
856 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
859 error = got_error_from_errno2("scandir", srv->repos_path);
863 if (gotweb_render_repo_table_hdr(c->tp) == -1)
866 for (d_i = 0; d_i < d_cnt; d_i++) {
867 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
870 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
871 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
876 error = got_path_dirent_type(&type, srv->repos_path,
880 if (type != DT_DIR) {
885 if (qs->index_page > 0 && (qs->index_page *
886 srv->max_repos_display) > t->prev_disp) {
891 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
895 error = gotweb_load_got_path(c, repo_dir);
896 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
898 gotweb_free_repo_dir(repo_dir);
903 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
909 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
912 gotweb_free_repo_dir(repo_dir);
915 if (d_disp == srv->max_repos_display)
918 t->repos_total = d_cnt - d_skipped;
920 if (srv->max_repos_display == 0)
922 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
924 if (t->repos_total <= srv->max_repos ||
925 t->repos_total <= srv->max_repos_display)
928 error = gotweb_render_navs(c);
933 for (d_i = 0; d_i < d_cnt; d_i++)
937 if (d != NULL && closedir(d) == EOF && error == NULL)
938 error = got_error_from_errno("closedir");
942 static const struct got_error *
943 gotweb_render_blame(struct request *c)
945 const struct got_error *error = NULL;
946 struct transport *t = c->t;
947 struct repo_commit *rc = NULL;
948 char *age = NULL, *msg = NULL;
951 error = got_get_repo_commits(c, 1);
955 rc = TAILQ_FIRST(&t->repo_commits);
957 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
960 error = gotweb_escape_html(&msg, rc->commit_msg);
964 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
965 "<div id='blame_title'>Blame</div>\n"
966 "</div>\n" /* #blame_title_wrapper */
967 "<div id='blame_content'>\n"
968 "<div id='blame_header_wrapper'>\n"
969 "<div id='blame_header'>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "<div id='header_commit_msg_title'>Message:</div>\n"
973 "<div id='header_commit_msg'>%s</div>\n"
974 "</div>\n" /* #blame_header */
975 "</div>\n" /* #blame_header_wrapper */
976 "<div class='dotted_line'></div>\n"
977 "<div id='blame'>\n",
983 error = got_output_file_blame(c);
987 fcgi_printf(c, "</div>\n" /* #blame */
988 "</div>\n"); /* #blame_content */
995 static const struct got_error *
996 gotweb_render_commits(struct request *c)
998 const struct got_error *error = NULL;
999 struct repo_commit *rc = NULL;
1000 struct server *srv = c->srv;
1001 struct transport *t = c->t;
1002 struct repo_dir *repo_dir = t->repo_dir;
1003 char *age = NULL, *author = NULL, *msg = NULL;
1006 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1007 "<div class='commits_title'>Commits</div>\n"
1008 "</div>\n" /* .commits_title_wrapper */
1009 "<div class='commits_content'>\n");
1013 error = got_get_repo_commits(c, srv->max_commits_display);
1017 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1018 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1021 error = gotweb_escape_html(&author, rc->author);
1024 error = gotweb_escape_html(&msg, rc->commit_msg);
1028 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1029 "<div class='commits_header'>\n"
1030 "<div class='header_commit_title'>Commit:</div>\n"
1031 "<div class='header_commit'>%s</div>\n"
1032 "<div class='header_author_title'>Author:</div>\n"
1033 "<div class='header_author'>%s</div>\n"
1034 "<div class='header_age_title'>Date:</div>\n"
1035 "<div class='header_age'>%s</div>\n"
1036 "</div>\n" /* .commits_header */
1037 "</div>\n" /* .commits_header_wrapper */
1038 "<div class='dotted_line'></div>\n"
1039 "<div class='commit'>\n%s</div>\n",
1047 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1048 "<div class='navs'>") == -1)
1051 r = gotweb_link(c, &(struct gotweb_url){
1055 .path = repo_dir->name,
1056 .commit = rc->commit_id,
1061 if (fcgi_printf(c, " | ") == -1)
1064 r = gotweb_link(c, &(struct gotweb_url){
1068 .path = repo_dir->name,
1069 .commit = rc->commit_id,
1074 if (fcgi_printf(c, "</div>\n" /* .navs */
1075 "</div>\n" /* .navs_wrapper */
1076 "<div class='dotted_line'></div>\n") == -1)
1087 if (t->next_id || t->prev_id) {
1088 error = gotweb_render_navs(c);
1092 fcgi_printf(c, "</div>\n"); /* .commits_content */
1100 static const struct got_error *
1101 gotweb_render_branches(struct request *c)
1103 const struct got_error *error = NULL;
1104 struct got_reflist_head refs;
1105 struct got_reflist_entry *re;
1106 struct transport *t = c->t;
1107 struct querystring *qs = t->qs;
1108 struct got_repository *repo = t->repo;
1109 char *escaped_refname = NULL;
1115 error = got_ref_list(&refs, repo, "refs/heads",
1116 got_ref_cmp_by_name, NULL);
1120 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1121 "<div id='branches_title'>Branches</div>\n"
1122 "</div>\n" /* #branches_title_wrapper */
1123 "<div id='branches_content'>\n");
1127 TAILQ_FOREACH(re, &refs, entry) {
1128 const char *refname = NULL;
1130 if (got_ref_is_symbolic(re->ref))
1133 refname = got_ref_get_name(re->ref);
1134 if (refname == NULL) {
1135 error = got_error_from_errno("strdup");
1138 if (strncmp(refname, "refs/heads/", 11) != 0)
1141 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1145 if (strncmp(refname, "refs/heads/", 11) == 0)
1147 error = gotweb_escape_html(&escaped_refname, refname);
1151 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1152 "<div class='branches_age'>%s</div>\n"
1153 "<div class='branches_space'> </div>\n"
1154 "<div class='branch'>", age);
1158 r = gotweb_link(c, &(struct gotweb_url){
1164 }, "%s", escaped_refname);
1168 if (fcgi_printf(c, "</div>\n" /* .branch */
1169 "<div class='navs_wrapper'>\n"
1170 "<div class='navs'>") == -1)
1173 r = gotweb_link(c, &(struct gotweb_url){
1183 if (fcgi_printf(c, " | ") == -1)
1186 r = gotweb_link(c, &(struct gotweb_url){
1192 }, "commit briefs");
1196 if (fcgi_printf(c, " | ") == -1)
1199 r = gotweb_link(c, &(struct gotweb_url){
1209 r = fcgi_printf(c, "</div>\n" /* .navs */
1210 "</div>\n" /* .navs_wrapper */
1211 "<div class='dotted_line'></div>\n"
1212 "</div>\n"); /* .branches_wrapper */
1218 free(escaped_refname);
1219 escaped_refname = NULL;
1221 fcgi_printf(c, "</div>\n"); /* #branches_content */
1224 free(escaped_refname);
1225 got_ref_list_free(&refs);
1229 static const struct got_error *
1230 gotweb_render_tree(struct request *c)
1232 const struct got_error *error = NULL;
1233 struct transport *t = c->t;
1234 struct repo_commit *rc = NULL;
1235 char *age = NULL, *msg = NULL;
1238 error = got_get_repo_commits(c, 1);
1242 rc = TAILQ_FIRST(&t->repo_commits);
1244 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1248 error = gotweb_escape_html(&msg, rc->commit_msg);
1252 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1253 "<div id='tree_title'>Tree</div>\n"
1254 "</div>\n" /* #tree_title_wrapper */
1255 "<div id='tree_content'>\n"
1256 "<div id='tree_header_wrapper'>\n"
1257 "<div id='tree_header'>\n"
1258 "<div id='header_tree_title'>Tree:</div>\n"
1259 "<div id='header_tree'>%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" /* #tree_header */
1265 "</div>\n" /* #tree_header_wrapper */
1266 "<div class='dotted_line'></div>\n"
1267 "<div id='tree'>\n",
1274 error = got_output_repo_tree(c);
1278 fcgi_printf(c, "</div>\n"); /* #tree */
1279 fcgi_printf(c, "</div>\n"); /* #tree_content */
1286 static const struct got_error *
1287 gotweb_render_diff(struct request *c)
1289 const struct got_error *error = NULL;
1290 struct transport *t = c->t;
1291 struct repo_commit *rc = NULL;
1292 char *age = NULL, *author = NULL, *msg = NULL;
1295 error = got_get_repo_commits(c, 1);
1299 rc = TAILQ_FIRST(&t->repo_commits);
1301 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1304 error = gotweb_escape_html(&author, rc->author);
1307 error = gotweb_escape_html(&msg, rc->commit_msg);
1311 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1312 "<div id='diff_title'>Commit Diff</div>\n"
1313 "</div>\n" /* #diff_title_wrapper */
1314 "<div id='diff_content'>\n"
1315 "<div id='diff_header_wrapper'>\n"
1316 "<div id='diff_header'>\n"
1317 "<div id='header_diff_title'>Diff:</div>\n"
1318 "<div id='header_diff'>%s<br />%s</div>\n"
1319 "<div class='header_commit_title'>Commit:</div>\n"
1320 "<div class='header_commit'>%s</div>\n"
1321 "<div id='header_tree_title'>Tree:</div>\n"
1322 "<div id='header_tree'>%s</div>\n"
1323 "<div class='header_author_title'>Author:</div>\n"
1324 "<div class='header_author'>%s</div>\n"
1325 "<div class='header_age_title'>Date:</div>\n"
1326 "<div class='header_age'>%s</div>\n"
1327 "<div id='header_commit_msg_title'>Message:</div>\n"
1328 "<div id='header_commit_msg'>%s</div>\n"
1329 "</div>\n" /* #diff_header */
1330 "</div>\n" /* #diff_header_wrapper */
1331 "<div class='dotted_line'></div>\n"
1332 "<div id='diff'>\n",
1333 rc->parent_id, rc->commit_id,
1342 error = got_output_repo_diff(c);
1346 fcgi_printf(c, "</div>\n"); /* #diff */
1347 fcgi_printf(c, "</div>\n"); /* #diff_content */
1355 static const struct got_error *
1356 gotweb_render_summary(struct request *c)
1358 const struct got_error *error = NULL;
1359 struct transport *t = c->t;
1360 struct server *srv = c->srv;
1363 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1366 if (srv->show_repo_description) {
1368 "<div id='description_title'>Description:</div>\n"
1369 "<div id='description'>%s</div>\n",
1370 t->repo_dir->description ? t->repo_dir->description : "");
1375 if (srv->show_repo_owner) {
1377 "<div id='repo_owner_title'>Owner:</div>\n"
1378 "<div id='repo_owner'>%s</div>\n",
1379 t->repo_dir->owner ? t->repo_dir->owner : "");
1384 if (srv->show_repo_age) {
1386 "<div id='last_change_title'>Last Change:</div>\n"
1387 "<div id='last_change'>%s</div>\n",
1393 if (srv->show_repo_cloneurl) {
1395 "<div id='cloneurl_title'>Clone URL:</div>\n"
1396 "<div id='cloneurl'>%s</div>\n",
1397 t->repo_dir->url ? t->repo_dir->url : "");
1402 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1406 if (gotweb_render_briefs(c->tp) == -1)
1409 error = gotweb_render_tags(c);
1411 log_warnx("%s: %s", __func__, error->msg);
1415 error = gotweb_render_branches(c);
1417 log_warnx("%s: %s", __func__, error->msg);
1422 static const struct got_error *
1423 gotweb_render_tag(struct request *c)
1425 const struct got_error *error = NULL;
1426 struct repo_tag *rt = NULL;
1427 struct transport *t = c->t;
1428 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1430 error = got_get_repo_tags(c, 1);
1434 if (t->tag_count == 0) {
1435 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1440 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1442 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1445 error = gotweb_escape_html(&author, rt->tagger);
1448 error = gotweb_escape_html(&msg, rt->commit_msg);
1452 tagname = rt->tag_name;
1453 if (strncmp(tagname, "refs/", 5) == 0)
1455 error = gotweb_escape_html(&tagname, tagname);
1459 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1460 "<div id='tags_title'>Tag</div>\n"
1461 "</div>\n" /* #tags_title_wrapper */
1462 "<div id='tags_content'>\n"
1463 "<div id='tag_header_wrapper'>\n"
1464 "<div id='tag_header'>\n"
1465 "<div class='header_commit_title'>Commit:</div>\n"
1466 "<div class='header_commit'>%s"
1467 " <span class='refs_str'>(%s)</span></div>\n"
1468 "<div class='header_author_title'>Tagger:</div>\n"
1469 "<div class='header_author'>%s</div>\n"
1470 "<div class='header_age_title'>Date:</div>\n"
1471 "<div class='header_age'>%s</div>\n"
1472 "<div id='header_commit_msg_title'>Message:</div>\n"
1473 "<div id='header_commit_msg'>%s</div>\n"
1474 "</div>\n" /* #tag_header */
1475 "<div class='dotted_line'></div>\n"
1476 "<div id='tag_commit'>\n%s</div>"
1477 "</div>" /* #tag_header_wrapper */
1478 "</div>", /* #tags_content */
1493 static const struct got_error *
1494 gotweb_render_tags(struct request *c)
1496 const struct got_error *error = NULL;
1497 struct repo_tag *rt = NULL;
1498 struct server *srv = c->srv;
1499 struct transport *t = c->t;
1500 struct querystring *qs = t->qs;
1501 struct repo_dir *repo_dir = t->repo_dir;
1502 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1503 int r, commit_found = 0;
1505 if (qs->action == BRIEFS) {
1507 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1509 error = got_get_repo_tags(c, srv->max_commits_display);
1513 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1514 "<div id='tags_title'>Tags</div>\n"
1515 "</div>\n" /* #tags_title_wrapper */
1516 "<div id='tags_content'>\n");
1520 if (t->tag_count == 0) {
1521 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1522 "This repository contains no tags");
1527 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1528 if (commit_found == 0 && qs->commit != NULL) {
1529 if (strcmp(qs->commit, rt->commit_id) != 0)
1534 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1538 tagname = rt->tag_name;
1539 if (strncmp(tagname, "refs/tags/", 10) == 0)
1541 error = gotweb_escape_html(&tagname, tagname);
1545 if (rt->tag_commit != NULL) {
1546 newline = strchr(rt->tag_commit, '\n');
1549 error = gotweb_escape_html(&msg, rt->tag_commit);
1554 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1555 "<div class='tag'>%s</div>\n"
1556 "<div class='tag_log'>", age, tagname) == -1)
1559 r = gotweb_link(c, &(struct gotweb_url){
1563 .path = repo_dir->name,
1564 .commit = rt->commit_id,
1565 }, "%s", msg ? msg : "");
1569 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1570 "<div class='navs_wrapper'>\n"
1571 "<div class='navs'>") == -1)
1574 r = gotweb_link(c, &(struct gotweb_url){
1578 .path = repo_dir->name,
1579 .commit = rt->commit_id,
1584 if (fcgi_printf(c, " | ") == -1)
1587 r = gotweb_link(c, &(struct gotweb_url){
1591 .path = repo_dir->name,
1592 .commit = rt->commit_id,
1593 }, "commit briefs");
1597 if (fcgi_printf(c, " | ") == -1)
1600 r = gotweb_link(c, &(struct gotweb_url){
1604 .path = repo_dir->name,
1605 .commit = rt->commit_id,
1611 "</div>\n" /* .navs */
1612 "</div>\n" /* .navs_wrapper */
1613 "<div class='dotted_line'></div>\n");
1624 if (t->next_id || t->prev_id) {
1625 error = gotweb_render_navs(c);
1629 fcgi_printf(c, "</div>\n"); /* #tags_content */
1637 const struct got_error *
1638 gotweb_escape_html(char **escaped_html, const char *orig_html)
1640 const struct got_error *error = NULL;
1641 struct escape_pair {
1652 size_t orig_len, len;
1655 orig_len = strlen(orig_html);
1657 for (i = 0; i < orig_len; i++) {
1658 for (j = 0; j < nitems(esc); j++) {
1659 if (orig_html[i] != esc[j].c)
1661 len += strlen(esc[j].s) - 1 /* escaped char */;
1665 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1666 if (*escaped_html == NULL)
1667 return got_error_from_errno("calloc");
1670 for (i = 0; i < orig_len; i++) {
1672 for (j = 0; j < nitems(esc); j++) {
1673 if (orig_html[i] != esc[j].c)
1676 if (strlcat(*escaped_html, esc[j].s, len + 1)
1678 error = got_error(GOT_ERR_NO_SPACE);
1681 x += strlen(esc[j].s);
1686 (*escaped_html)[x] = orig_html[i];
1692 free(*escaped_html);
1693 *escaped_html = NULL;
1695 (*escaped_html)[x] = '\0';
1702 should_urlencode(int c)
1704 if (c <= ' ' || c >= 127)
1735 gotweb_urlencode(const char *str)
1743 for (s = str; *s; ++s) {
1745 if (should_urlencode(*s))
1749 escaped = calloc(1, len + 1);
1750 if (escaped == NULL)
1754 for (s = str; *s; ++s) {
1755 if (should_urlencode(*s)) {
1756 a = (*s & 0xF0) >> 4;
1760 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1761 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1770 gotweb_action_name(int action)
1801 gotweb_render_url(struct request *c, struct gotweb_url *url)
1803 const char *sep = "?", *action;
1807 action = gotweb_action_name(url->action);
1808 if (action != NULL) {
1809 if (fcgi_printf(c, "?action=%s", action) == -1)
1815 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1821 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1827 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1833 tmp = gotweb_urlencode(url->file);
1836 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1844 tmp = gotweb_urlencode(url->folder);
1847 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1855 tmp = gotweb_urlencode(url->headref);
1858 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1865 if (url->index_page != -1) {
1866 if (fcgi_printf(c, "%sindex_page=%d", sep,
1867 url->index_page) == -1)
1873 tmp = gotweb_urlencode(url->path);
1876 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1883 if (url->page != -1) {
1884 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1893 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1898 if (fcgi_printf(c, "<a href='") == -1)
1901 if (gotweb_render_url(c, url) == -1)
1904 if (fcgi_printf(c, "'>") == -1)
1908 r = fcgi_vprintf(c, fmt, ap);
1913 if (fcgi_printf(c, "</a>"))
1918 static struct got_repository *
1919 find_cached_repo(struct server *srv, const char *path)
1923 for (i = 0; i < srv->ncached_repos; i++) {
1924 if (strcmp(srv->cached_repos[i].path, path) == 0)
1925 return srv->cached_repos[i].repo;
1931 static const struct got_error *
1932 cache_repo(struct got_repository **new, struct server *srv,
1933 struct repo_dir *repo_dir, struct socket *sock)
1935 const struct got_error *error = NULL;
1936 struct got_repository *repo;
1937 struct cached_repo *cr;
1940 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1941 cr = &srv->cached_repos[srv->ncached_repos - 1];
1942 error = got_repo_close(cr->repo);
1943 memset(cr, 0, sizeof(*cr));
1944 srv->ncached_repos--;
1947 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1948 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1949 cr = &srv->cached_repos[0];
1952 cr = &srv->cached_repos[srv->ncached_repos];
1955 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1958 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1959 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1964 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1965 >= sizeof(cr->path)) {
1967 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1968 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1970 return got_error(GOT_ERR_NO_SPACE);
1974 srv->ncached_repos++;
1979 static const struct got_error *
1980 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1982 const struct got_error *error = NULL;
1983 struct socket *sock = c->sock;
1984 struct server *srv = c->srv;
1985 struct transport *t = c->t;
1986 struct got_repository *repo = NULL;
1990 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1991 GOTWEB_GIT_DIR) == -1)
1992 return got_error_from_errno("asprintf");
1994 dt = opendir(dir_test);
1998 repo_dir->path = dir_test;
2003 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2004 repo_dir->name) == -1)
2005 return got_error_from_errno("asprintf");
2007 dt = opendir(dir_test);
2009 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2012 repo_dir->path = dir_test;
2017 if (srv->respect_exportok &&
2018 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2019 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2023 repo = find_cached_repo(srv, repo_dir->path);
2025 error = cache_repo(&repo, srv, repo_dir, sock);
2030 error = gotweb_get_repo_description(&repo_dir->description, srv,
2031 repo_dir->path, dirfd(dt));
2034 error = got_get_repo_owner(&repo_dir->owner, c);
2037 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2040 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2044 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2045 error = got_error_from_errno("closedir");
2049 static const struct got_error *
2050 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2052 const struct got_error *error;
2054 *repo_dir = calloc(1, sizeof(**repo_dir));
2055 if (*repo_dir == NULL)
2056 return got_error_from_errno("calloc");
2058 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2059 error = got_error_from_errno("asprintf");
2064 (*repo_dir)->owner = NULL;
2065 (*repo_dir)->description = NULL;
2066 (*repo_dir)->url = NULL;
2067 (*repo_dir)->age = NULL;
2068 (*repo_dir)->path = NULL;
2073 static const struct got_error *
2074 gotweb_get_repo_description(char **description, struct server *srv,
2075 const char *dirpath, int dir)
2077 const struct got_error *error = NULL;
2082 *description = NULL;
2083 if (srv->show_repo_description == 0)
2086 fd = openat(dir, "description", O_RDONLY);
2088 if (errno != ENOENT && errno != EACCES) {
2089 error = got_error_from_errno_fmt("openat %s/%s",
2090 dirpath, "description");
2095 if (fstat(fd, &sb) == -1) {
2096 error = got_error_from_errno_fmt("fstat %s/%s",
2097 dirpath, "description");
2102 if (len > GOTWEBD_MAXDESCRSZ - 1)
2103 len = GOTWEBD_MAXDESCRSZ - 1;
2105 *description = calloc(len + 1, sizeof(**description));
2106 if (*description == NULL) {
2107 error = got_error_from_errno("calloc");
2111 if (read(fd, *description, len) == -1)
2112 error = got_error_from_errno("read");
2114 if (fd != -1 && close(fd) == -1 && error == NULL)
2115 error = got_error_from_errno("close");
2119 static const struct got_error *
2120 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2123 const struct got_error *error = NULL;
2129 if (srv->show_repo_cloneurl == 0)
2132 fd = openat(dir, "cloneurl", O_RDONLY);
2134 if (errno != ENOENT && errno != EACCES) {
2135 error = got_error_from_errno_fmt("openat %s/%s",
2136 dirpath, "cloneurl");
2141 if (fstat(fd, &sb) == -1) {
2142 error = got_error_from_errno_fmt("fstat %s/%s",
2143 dirpath, "cloneurl");
2148 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2149 len = GOTWEBD_MAXCLONEURLSZ - 1;
2151 *url = calloc(len + 1, sizeof(**url));
2153 error = got_error_from_errno("calloc");
2157 if (read(fd, *url, len) == -1)
2158 error = got_error_from_errno("read");
2160 if (fd != -1 && close(fd) == -1 && error == NULL)
2161 error = got_error_from_errno("close");
2165 const struct got_error *
2166 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2169 long long diff_time;
2170 const char *years = "years ago", *months = "months ago";
2171 const char *weeks = "weeks ago", *days = "days ago";
2172 const char *hours = "hours ago", *minutes = "minutes ago";
2173 const char *seconds = "seconds ago", *now = "right now";
2181 diff_time = time(NULL) - committer_time;
2182 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2183 if (asprintf(repo_age, "%lld %s",
2184 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2185 return got_error_from_errno("asprintf");
2186 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2187 if (asprintf(repo_age, "%lld %s",
2188 (diff_time / 60 / 60 / 24 / (365 / 12)),
2190 return got_error_from_errno("asprintf");
2191 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2192 if (asprintf(repo_age, "%lld %s",
2193 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2194 return got_error_from_errno("asprintf");
2195 } else if (diff_time > 60 * 60 * 24 * 2) {
2196 if (asprintf(repo_age, "%lld %s",
2197 (diff_time / 60 / 60 / 24), days) == -1)
2198 return got_error_from_errno("asprintf");
2199 } else if (diff_time > 60 * 60 * 2) {
2200 if (asprintf(repo_age, "%lld %s",
2201 (diff_time / 60 / 60), hours) == -1)
2202 return got_error_from_errno("asprintf");
2203 } else if (diff_time > 60 * 2) {
2204 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2206 return got_error_from_errno("asprintf");
2207 } else if (diff_time > 2) {
2208 if (asprintf(repo_age, "%lld %s", diff_time,
2210 return got_error_from_errno("asprintf");
2212 if (asprintf(repo_age, "%s", now) == -1)
2213 return got_error_from_errno("asprintf");
2217 if (gmtime_r(&committer_time, &tm) == NULL)
2218 return got_error_from_errno("gmtime_r");
2220 s = asctime_r(&tm, datebuf);
2222 return got_error_from_errno("asctime_r");
2224 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2225 return got_error_from_errno("asprintf");