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;
363 static const struct got_error *
364 gotweb_parse_querystring(struct querystring **qs, char *qst)
366 const struct got_error *error = NULL;
367 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
368 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
375 return got_error_from_errno2("%s: strdup", __func__);
380 while (tok1_pair != NULL) {
381 strsep(&tok1_end, "&");
383 tok2 = strdup(tok1_pair);
386 return got_error_from_errno2("%s: strdup", __func__);
392 while (tok2_pair != NULL) {
393 strsep(&tok2_end, "=");
395 error = gotweb_assign_querystring(qs, tok2_pair,
400 tok2_pair = tok2_end;
403 tok1_pair = tok1_end;
414 * Adapted from usr.sbin/httpd/httpd.c url_decode.
416 static const struct got_error *
417 gotweb_urldecode(char *url)
429 /* Encoding character is followed by two hex chars */
430 if (!isxdigit((unsigned char)p[1]) ||
431 !isxdigit((unsigned char)p[2]) ||
432 (p[1] == '0' && p[2] == '0'))
433 return got_error(GOT_ERR_BAD_QUERYSTRING);
439 * We don't have to validate "hex" because it is
440 * guaranteed to include two hex chars followed by nul.
442 x = strtoul(hex, NULL, 16);
458 static const struct got_error *
459 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
461 const struct got_error *error = NULL;
465 error = gotweb_urldecode(value);
469 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
470 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
473 switch (querystring_keys[el_cnt].element) {
475 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
476 if (strcmp(value, action_keys[a_cnt].name) != 0)
478 else if (strcmp(value,
479 action_keys[a_cnt].name) == 0){
481 action_keys[a_cnt].action;
489 (*qs)->commit = strdup(value);
490 if ((*qs)->commit == NULL) {
491 error = got_error_from_errno2("%s: strdup",
497 (*qs)->file = strdup(value);
498 if ((*qs)->file == NULL) {
499 error = got_error_from_errno2("%s: strdup",
505 (*qs)->folder = strdup(value);
506 if ((*qs)->folder == NULL) {
507 error = got_error_from_errno2("%s: strdup",
513 free((*qs)->headref);
514 (*qs)->headref = strdup(value);
515 if ((*qs)->headref == NULL) {
516 error = got_error_from_errno2("%s: strdup",
522 if (strlen(value) == 0)
524 (*qs)->index_page = strtonum(value, INT64_MIN,
527 error = got_error_from_errno3("%s: strtonum %s",
531 if ((*qs)->index_page < 0)
532 (*qs)->index_page = 0;
535 (*qs)->path = strdup(value);
536 if ((*qs)->path == NULL) {
537 error = got_error_from_errno2("%s: strdup",
543 if (strlen(value) == 0)
545 (*qs)->page = strtonum(value, INT64_MIN,
548 error = got_error_from_errno3("%s: strtonum %s",
564 gotweb_free_repo_tag(struct repo_tag *rt)
569 free(rt->tag_commit);
570 free(rt->commit_msg);
577 gotweb_free_repo_commit(struct repo_commit *rc)
587 free(rc->commit_msg);
593 gotweb_free_querystring(struct querystring *qs)
606 gotweb_free_repo_dir(struct repo_dir *repo_dir)
608 if (repo_dir != NULL) {
609 free(repo_dir->name);
610 free(repo_dir->owner);
611 free(repo_dir->description);
614 free(repo_dir->path);
620 gotweb_free_transport(struct transport *t)
622 struct repo_commit *rc = NULL, *trc = NULL;
623 struct repo_tag *rt = NULL, *trt = NULL;
625 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
626 TAILQ_REMOVE(&t->repo_commits, rc, entry);
627 gotweb_free_repo_commit(rc);
629 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
630 TAILQ_REMOVE(&t->repo_tags, rt, entry);
631 gotweb_free_repo_tag(rt);
633 gotweb_free_repo_dir(t->repo_dir);
634 gotweb_free_querystring(t->qs);
640 const struct got_error *
641 gotweb_render_content_type(struct request *c, const uint8_t *type)
643 const char *csp = "default-src 'self'; script-src 'none'; "
644 "object-src 'none';";
647 "Content-Security-Policy: %s\r\n"
648 "Content-Type: %s\r\n\r\n",
653 const struct got_error *
654 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
657 fcgi_printf(c, "Content-type: %s\r\n"
658 "Content-disposition: attachment; filename=%s\r\n\r\n",
663 const struct got_error *
664 gotweb_render_navs(struct request *c)
666 const struct got_error *error = NULL;
667 struct transport *t = c->t;
668 struct querystring *qs = t->qs;
669 struct server *srv = c->srv;
672 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
678 if (qs->index_page > 0) {
679 struct gotweb_url url = {
681 .index_page = qs->index_page - 1,
685 r = gotweb_link(c, &url, "Previous");
689 if (t->prev_id && qs->commit != NULL &&
690 strcmp(qs->commit, t->prev_id) != 0) {
691 struct gotweb_url url = {
694 .page = qs->page - 1,
696 .commit = t->prev_id,
697 .headref = qs->headref,
700 r = gotweb_link(c, &url, "Previous");
704 if (t->prev_id && qs->commit != NULL &&
705 strcmp(qs->commit, t->prev_id) != 0) {
706 struct gotweb_url url = {
709 .page = qs->page - 1,
711 .commit = t->prev_id,
712 .headref = qs->headref,
713 .folder = qs->folder,
717 r = gotweb_link(c, &url, "Previous");
721 if (t->prev_id && qs->commit != NULL &&
722 strcmp(qs->commit, t->prev_id) != 0) {
723 struct gotweb_url url = {
726 .page = qs->page - 1,
728 .commit = t->prev_id,
729 .headref = qs->headref,
732 r = gotweb_link(c, &url, "Previous");
740 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
741 "<div id='nav_next'>");
747 if (t->next_disp == srv->max_repos_display &&
748 t->repos_total != (qs->index_page + 1) *
749 srv->max_repos_display) {
750 struct gotweb_url url = {
752 .index_page = qs->index_page + 1,
756 r = gotweb_link(c, &url, "Next");
761 struct gotweb_url url = {
764 .page = qs->page + 1,
766 .commit = t->next_id,
767 .headref = qs->headref,
770 r = gotweb_link(c, &url, "Next");
775 struct gotweb_url url = {
778 .page = qs->page + 1,
780 .commit = t->next_id,
781 .headref = qs->headref,
782 .folder = qs->folder,
786 r = gotweb_link(c, &url, "Next");
791 struct gotweb_url url = {
794 .page = qs->page + 1,
796 .commit = t->next_id,
797 .headref = qs->headref,
800 r = gotweb_link(c, &url, "Next");
807 fcgi_printf(c, "</div>\n"); /* #nav_next */
808 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
817 static const struct got_error *
818 gotweb_render_index(struct request *c)
820 const struct got_error *error = NULL;
821 struct server *srv = c->srv;
822 struct transport *t = c->t;
823 struct querystring *qs = t->qs;
824 struct repo_dir *repo_dir = NULL;
826 struct dirent **sd_dent = NULL;
827 unsigned int d_cnt, d_i, d_disp = 0;
828 unsigned int d_skipped = 0;
831 d = opendir(srv->repos_path);
833 error = got_error_from_errno2("opendir", srv->repos_path);
837 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
840 error = got_error_from_errno2("scandir", srv->repos_path);
844 if (gotweb_render_repo_table_hdr(c->tp) == -1)
847 for (d_i = 0; d_i < d_cnt; d_i++) {
848 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
851 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
852 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
857 error = got_path_dirent_type(&type, srv->repos_path,
861 if (type != DT_DIR) {
866 if (qs->index_page > 0 && (qs->index_page *
867 srv->max_repos_display) > t->prev_disp) {
872 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
876 error = gotweb_load_got_path(c, repo_dir);
877 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
879 gotweb_free_repo_dir(repo_dir);
884 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
890 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
893 gotweb_free_repo_dir(repo_dir);
896 if (d_disp == srv->max_repos_display)
899 t->repos_total = d_cnt - d_skipped;
901 if (srv->max_repos_display == 0)
903 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
905 if (t->repos_total <= srv->max_repos ||
906 t->repos_total <= srv->max_repos_display)
909 error = gotweb_render_navs(c);
914 for (d_i = 0; d_i < d_cnt; d_i++)
918 if (d != NULL && closedir(d) == EOF && error == NULL)
919 error = got_error_from_errno("closedir");
923 static const struct got_error *
924 gotweb_render_blame(struct request *c)
926 const struct got_error *error = NULL;
927 struct transport *t = c->t;
928 struct repo_commit *rc = NULL;
929 char *age = NULL, *msg = NULL;
932 error = got_get_repo_commits(c, 1);
936 rc = TAILQ_FIRST(&t->repo_commits);
938 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
941 error = gotweb_escape_html(&msg, rc->commit_msg);
945 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
946 "<div id='blame_title'>Blame</div>\n"
947 "</div>\n" /* #blame_title_wrapper */
948 "<div id='blame_content'>\n"
949 "<div id='blame_header_wrapper'>\n"
950 "<div id='blame_header'>\n"
951 "<div class='header_age_title'>Date:</div>\n"
952 "<div class='header_age'>%s</div>\n"
953 "<div id='header_commit_msg_title'>Message:</div>\n"
954 "<div id='header_commit_msg'>%s</div>\n"
955 "</div>\n" /* #blame_header */
956 "</div>\n" /* #blame_header_wrapper */
957 "<div class='dotted_line'></div>\n"
958 "<div id='blame'>\n",
964 error = got_output_file_blame(c);
968 fcgi_printf(c, "</div>\n" /* #blame */
969 "</div>\n"); /* #blame_content */
976 static const struct got_error *
977 gotweb_render_commits(struct request *c)
979 const struct got_error *error = NULL;
980 struct repo_commit *rc = NULL;
981 struct server *srv = c->srv;
982 struct transport *t = c->t;
983 struct repo_dir *repo_dir = t->repo_dir;
984 char *age = NULL, *author = NULL, *msg = NULL;
987 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
988 "<div class='commits_title'>Commits</div>\n"
989 "</div>\n" /* .commits_title_wrapper */
990 "<div class='commits_content'>\n");
994 error = got_get_repo_commits(c, srv->max_commits_display);
998 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
999 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1002 error = gotweb_escape_html(&author, rc->author);
1005 error = gotweb_escape_html(&msg, rc->commit_msg);
1009 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1010 "<div class='commits_header'>\n"
1011 "<div class='header_commit_title'>Commit:</div>\n"
1012 "<div class='header_commit'>%s</div>\n"
1013 "<div class='header_author_title'>Author:</div>\n"
1014 "<div class='header_author'>%s</div>\n"
1015 "<div class='header_age_title'>Date:</div>\n"
1016 "<div class='header_age'>%s</div>\n"
1017 "</div>\n" /* .commits_header */
1018 "</div>\n" /* .commits_header_wrapper */
1019 "<div class='dotted_line'></div>\n"
1020 "<div class='commit'>\n%s</div>\n",
1028 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1029 "<div class='navs'>") == -1)
1032 r = gotweb_link(c, &(struct gotweb_url){
1036 .path = repo_dir->name,
1037 .commit = rc->commit_id,
1042 if (fcgi_printf(c, " | ") == -1)
1045 r = gotweb_link(c, &(struct gotweb_url){
1049 .path = repo_dir->name,
1050 .commit = rc->commit_id,
1055 if (fcgi_printf(c, "</div>\n" /* .navs */
1056 "</div>\n" /* .navs_wrapper */
1057 "<div class='dotted_line'></div>\n") == -1)
1068 if (t->next_id || t->prev_id) {
1069 error = gotweb_render_navs(c);
1073 fcgi_printf(c, "</div>\n"); /* .commits_content */
1081 static const struct got_error *
1082 gotweb_render_branches(struct request *c)
1084 const struct got_error *error = NULL;
1085 struct got_reflist_head refs;
1086 struct got_reflist_entry *re;
1087 struct transport *t = c->t;
1088 struct querystring *qs = t->qs;
1089 struct got_repository *repo = t->repo;
1090 char *escaped_refname = NULL;
1096 error = got_ref_list(&refs, repo, "refs/heads",
1097 got_ref_cmp_by_name, NULL);
1101 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1102 "<div id='branches_title'>Branches</div>\n"
1103 "</div>\n" /* #branches_title_wrapper */
1104 "<div id='branches_content'>\n");
1108 TAILQ_FOREACH(re, &refs, entry) {
1109 const char *refname = NULL;
1111 if (got_ref_is_symbolic(re->ref))
1114 refname = got_ref_get_name(re->ref);
1115 if (refname == NULL) {
1116 error = got_error_from_errno("strdup");
1119 if (strncmp(refname, "refs/heads/", 11) != 0)
1122 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1126 if (strncmp(refname, "refs/heads/", 11) == 0)
1128 error = gotweb_escape_html(&escaped_refname, refname);
1132 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1133 "<div class='branches_age'>%s</div>\n"
1134 "<div class='branches_space'> </div>\n"
1135 "<div class='branch'>", age);
1139 r = gotweb_link(c, &(struct gotweb_url){
1145 }, "%s", escaped_refname);
1149 if (fcgi_printf(c, "</div>\n" /* .branch */
1150 "<div class='navs_wrapper'>\n"
1151 "<div class='navs'>") == -1)
1154 r = gotweb_link(c, &(struct gotweb_url){
1164 if (fcgi_printf(c, " | ") == -1)
1167 r = gotweb_link(c, &(struct gotweb_url){
1173 }, "commit briefs");
1177 if (fcgi_printf(c, " | ") == -1)
1180 r = gotweb_link(c, &(struct gotweb_url){
1190 r = fcgi_printf(c, "</div>\n" /* .navs */
1191 "</div>\n" /* .navs_wrapper */
1192 "<div class='dotted_line'></div>\n"
1193 "</div>\n"); /* .branches_wrapper */
1199 free(escaped_refname);
1200 escaped_refname = NULL;
1202 fcgi_printf(c, "</div>\n"); /* #branches_content */
1205 free(escaped_refname);
1206 got_ref_list_free(&refs);
1210 static const struct got_error *
1211 gotweb_render_tree(struct request *c)
1213 const struct got_error *error = NULL;
1214 struct transport *t = c->t;
1215 struct repo_commit *rc = NULL;
1216 char *age = NULL, *msg = NULL;
1219 error = got_get_repo_commits(c, 1);
1223 rc = TAILQ_FIRST(&t->repo_commits);
1225 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1229 error = gotweb_escape_html(&msg, rc->commit_msg);
1233 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1234 "<div id='tree_title'>Tree</div>\n"
1235 "</div>\n" /* #tree_title_wrapper */
1236 "<div id='tree_content'>\n"
1237 "<div id='tree_header_wrapper'>\n"
1238 "<div id='tree_header'>\n"
1239 "<div id='header_tree_title'>Tree:</div>\n"
1240 "<div id='header_tree'>%s</div>\n"
1241 "<div class='header_age_title'>Date:</div>\n"
1242 "<div class='header_age'>%s</div>\n"
1243 "<div id='header_commit_msg_title'>Message:</div>\n"
1244 "<div id='header_commit_msg'>%s</div>\n"
1245 "</div>\n" /* #tree_header */
1246 "</div>\n" /* #tree_header_wrapper */
1247 "<div class='dotted_line'></div>\n"
1248 "<div id='tree'>\n",
1255 error = got_output_repo_tree(c);
1259 fcgi_printf(c, "</div>\n"); /* #tree */
1260 fcgi_printf(c, "</div>\n"); /* #tree_content */
1267 static const struct got_error *
1268 gotweb_render_diff(struct request *c)
1270 const struct got_error *error = NULL;
1271 struct transport *t = c->t;
1272 struct repo_commit *rc = NULL;
1273 char *age = NULL, *author = NULL, *msg = NULL;
1276 error = got_get_repo_commits(c, 1);
1280 rc = TAILQ_FIRST(&t->repo_commits);
1282 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1285 error = gotweb_escape_html(&author, rc->author);
1288 error = gotweb_escape_html(&msg, rc->commit_msg);
1292 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1293 "<div id='diff_title'>Commit Diff</div>\n"
1294 "</div>\n" /* #diff_title_wrapper */
1295 "<div id='diff_content'>\n"
1296 "<div id='diff_header_wrapper'>\n"
1297 "<div id='diff_header'>\n"
1298 "<div id='header_diff_title'>Diff:</div>\n"
1299 "<div id='header_diff'>%s<br />%s</div>\n"
1300 "<div class='header_commit_title'>Commit:</div>\n"
1301 "<div class='header_commit'>%s</div>\n"
1302 "<div id='header_tree_title'>Tree:</div>\n"
1303 "<div id='header_tree'>%s</div>\n"
1304 "<div class='header_author_title'>Author:</div>\n"
1305 "<div class='header_author'>%s</div>\n"
1306 "<div class='header_age_title'>Date:</div>\n"
1307 "<div class='header_age'>%s</div>\n"
1308 "<div id='header_commit_msg_title'>Message:</div>\n"
1309 "<div id='header_commit_msg'>%s</div>\n"
1310 "</div>\n" /* #diff_header */
1311 "</div>\n" /* #diff_header_wrapper */
1312 "<div class='dotted_line'></div>\n"
1313 "<div id='diff'>\n",
1314 rc->parent_id, rc->commit_id,
1323 error = got_output_repo_diff(c);
1327 fcgi_printf(c, "</div>\n"); /* #diff */
1328 fcgi_printf(c, "</div>\n"); /* #diff_content */
1336 static const struct got_error *
1337 gotweb_render_summary(struct request *c)
1339 const struct got_error *error = NULL;
1340 struct transport *t = c->t;
1341 struct server *srv = c->srv;
1344 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1347 if (srv->show_repo_description) {
1349 "<div id='description_title'>Description:</div>\n"
1350 "<div id='description'>%s</div>\n",
1351 t->repo_dir->description ? t->repo_dir->description : "");
1356 if (srv->show_repo_owner) {
1358 "<div id='repo_owner_title'>Owner:</div>\n"
1359 "<div id='repo_owner'>%s</div>\n",
1360 t->repo_dir->owner ? t->repo_dir->owner : "");
1365 if (srv->show_repo_age) {
1367 "<div id='last_change_title'>Last Change:</div>\n"
1368 "<div id='last_change'>%s</div>\n",
1374 if (srv->show_repo_cloneurl) {
1376 "<div id='cloneurl_title'>Clone URL:</div>\n"
1377 "<div id='cloneurl'>%s</div>\n",
1378 t->repo_dir->url ? t->repo_dir->url : "");
1383 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1387 if (gotweb_render_briefs(c->tp) == -1)
1390 error = gotweb_render_tags(c);
1392 log_warnx("%s: %s", __func__, error->msg);
1396 error = gotweb_render_branches(c);
1398 log_warnx("%s: %s", __func__, error->msg);
1403 static const struct got_error *
1404 gotweb_render_tag(struct request *c)
1406 const struct got_error *error = NULL;
1407 struct repo_tag *rt = NULL;
1408 struct transport *t = c->t;
1409 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1411 error = got_get_repo_tags(c, 1);
1415 if (t->tag_count == 0) {
1416 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1421 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1423 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1426 error = gotweb_escape_html(&author, rt->tagger);
1429 error = gotweb_escape_html(&msg, rt->commit_msg);
1433 tagname = rt->tag_name;
1434 if (strncmp(tagname, "refs/", 5) == 0)
1436 error = gotweb_escape_html(&tagname, tagname);
1440 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1441 "<div id='tags_title'>Tag</div>\n"
1442 "</div>\n" /* #tags_title_wrapper */
1443 "<div id='tags_content'>\n"
1444 "<div id='tag_header_wrapper'>\n"
1445 "<div id='tag_header'>\n"
1446 "<div class='header_commit_title'>Commit:</div>\n"
1447 "<div class='header_commit'>%s"
1448 " <span class='refs_str'>(%s)</span></div>\n"
1449 "<div class='header_author_title'>Tagger:</div>\n"
1450 "<div class='header_author'>%s</div>\n"
1451 "<div class='header_age_title'>Date:</div>\n"
1452 "<div class='header_age'>%s</div>\n"
1453 "<div id='header_commit_msg_title'>Message:</div>\n"
1454 "<div id='header_commit_msg'>%s</div>\n"
1455 "</div>\n" /* #tag_header */
1456 "<div class='dotted_line'></div>\n"
1457 "<div id='tag_commit'>\n%s</div>"
1458 "</div>" /* #tag_header_wrapper */
1459 "</div>", /* #tags_content */
1474 static const struct got_error *
1475 gotweb_render_tags(struct request *c)
1477 const struct got_error *error = NULL;
1478 struct repo_tag *rt = NULL;
1479 struct server *srv = c->srv;
1480 struct transport *t = c->t;
1481 struct querystring *qs = t->qs;
1482 struct repo_dir *repo_dir = t->repo_dir;
1483 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1484 int r, commit_found = 0;
1486 if (qs->action == BRIEFS) {
1488 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1490 error = got_get_repo_tags(c, srv->max_commits_display);
1494 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1495 "<div id='tags_title'>Tags</div>\n"
1496 "</div>\n" /* #tags_title_wrapper */
1497 "<div id='tags_content'>\n");
1501 if (t->tag_count == 0) {
1502 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1503 "This repository contains no tags");
1508 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1509 if (commit_found == 0 && qs->commit != NULL) {
1510 if (strcmp(qs->commit, rt->commit_id) != 0)
1515 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1519 tagname = rt->tag_name;
1520 if (strncmp(tagname, "refs/tags/", 10) == 0)
1522 error = gotweb_escape_html(&tagname, tagname);
1526 if (rt->tag_commit != NULL) {
1527 newline = strchr(rt->tag_commit, '\n');
1530 error = gotweb_escape_html(&msg, rt->tag_commit);
1535 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1536 "<div class='tag'>%s</div>\n"
1537 "<div class='tag_log'>", age, tagname) == -1)
1540 r = gotweb_link(c, &(struct gotweb_url){
1544 .path = repo_dir->name,
1545 .commit = rt->commit_id,
1546 }, "%s", msg ? msg : "");
1550 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1551 "<div class='navs_wrapper'>\n"
1552 "<div class='navs'>") == -1)
1555 r = gotweb_link(c, &(struct gotweb_url){
1559 .path = repo_dir->name,
1560 .commit = rt->commit_id,
1565 if (fcgi_printf(c, " | ") == -1)
1568 r = gotweb_link(c, &(struct gotweb_url){
1572 .path = repo_dir->name,
1573 .commit = rt->commit_id,
1574 }, "commit briefs");
1578 if (fcgi_printf(c, " | ") == -1)
1581 r = gotweb_link(c, &(struct gotweb_url){
1585 .path = repo_dir->name,
1586 .commit = rt->commit_id,
1592 "</div>\n" /* .navs */
1593 "</div>\n" /* .navs_wrapper */
1594 "<div class='dotted_line'></div>\n");
1605 if (t->next_id || t->prev_id) {
1606 error = gotweb_render_navs(c);
1610 fcgi_printf(c, "</div>\n"); /* #tags_content */
1618 const struct got_error *
1619 gotweb_escape_html(char **escaped_html, const char *orig_html)
1621 const struct got_error *error = NULL;
1622 struct escape_pair {
1633 size_t orig_len, len;
1636 orig_len = strlen(orig_html);
1638 for (i = 0; i < orig_len; i++) {
1639 for (j = 0; j < nitems(esc); j++) {
1640 if (orig_html[i] != esc[j].c)
1642 len += strlen(esc[j].s) - 1 /* escaped char */;
1646 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1647 if (*escaped_html == NULL)
1648 return got_error_from_errno("calloc");
1651 for (i = 0; i < orig_len; i++) {
1653 for (j = 0; j < nitems(esc); j++) {
1654 if (orig_html[i] != esc[j].c)
1657 if (strlcat(*escaped_html, esc[j].s, len + 1)
1659 error = got_error(GOT_ERR_NO_SPACE);
1662 x += strlen(esc[j].s);
1667 (*escaped_html)[x] = orig_html[i];
1673 free(*escaped_html);
1674 *escaped_html = NULL;
1676 (*escaped_html)[x] = '\0';
1683 should_urlencode(int c)
1685 if (c <= ' ' || c >= 127)
1716 gotweb_urlencode(const char *str)
1724 for (s = str; *s; ++s) {
1726 if (should_urlencode(*s))
1730 escaped = calloc(1, len + 1);
1731 if (escaped == NULL)
1735 for (s = str; *s; ++s) {
1736 if (should_urlencode(*s)) {
1737 a = (*s & 0xF0) >> 4;
1741 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1742 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1751 gotweb_action_name(int action)
1782 gotweb_render_url(struct request *c, struct gotweb_url *url)
1784 const char *sep = "?", *action;
1788 action = gotweb_action_name(url->action);
1789 if (action != NULL) {
1790 if (fcgi_printf(c, "?action=%s", action) == -1)
1796 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1802 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1808 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1814 tmp = gotweb_urlencode(url->file);
1817 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1825 tmp = gotweb_urlencode(url->folder);
1828 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1836 tmp = gotweb_urlencode(url->headref);
1839 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1846 if (url->index_page != -1) {
1847 if (fcgi_printf(c, "%sindex_page=%d", sep,
1848 url->index_page) == -1)
1854 tmp = gotweb_urlencode(url->path);
1857 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1864 if (url->page != -1) {
1865 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1874 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1879 if (fcgi_printf(c, "<a href='") == -1)
1882 if (gotweb_render_url(c, url) == -1)
1885 if (fcgi_printf(c, "'>") == -1)
1889 r = fcgi_vprintf(c, fmt, ap);
1894 if (fcgi_printf(c, "</a>"))
1899 static struct got_repository *
1900 find_cached_repo(struct server *srv, const char *path)
1904 for (i = 0; i < srv->ncached_repos; i++) {
1905 if (strcmp(srv->cached_repos[i].path, path) == 0)
1906 return srv->cached_repos[i].repo;
1912 static const struct got_error *
1913 cache_repo(struct got_repository **new, struct server *srv,
1914 struct repo_dir *repo_dir, struct socket *sock)
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo;
1918 struct cached_repo *cr;
1921 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1922 cr = &srv->cached_repos[srv->ncached_repos - 1];
1923 error = got_repo_close(cr->repo);
1924 memset(cr, 0, sizeof(*cr));
1925 srv->ncached_repos--;
1928 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1929 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1930 cr = &srv->cached_repos[0];
1933 cr = &srv->cached_repos[srv->ncached_repos];
1936 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1939 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1940 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1945 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1946 >= sizeof(cr->path)) {
1948 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1949 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1951 return got_error(GOT_ERR_NO_SPACE);
1955 srv->ncached_repos++;
1960 static const struct got_error *
1961 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1963 const struct got_error *error = NULL;
1964 struct socket *sock = c->sock;
1965 struct server *srv = c->srv;
1966 struct transport *t = c->t;
1967 struct got_repository *repo = NULL;
1971 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1972 GOTWEB_GIT_DIR) == -1)
1973 return got_error_from_errno("asprintf");
1975 dt = opendir(dir_test);
1979 repo_dir->path = dir_test;
1984 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1985 repo_dir->name) == -1)
1986 return got_error_from_errno("asprintf");
1988 dt = opendir(dir_test);
1990 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1993 repo_dir->path = dir_test;
1998 if (srv->respect_exportok &&
1999 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2000 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2004 repo = find_cached_repo(srv, repo_dir->path);
2006 error = cache_repo(&repo, srv, repo_dir, sock);
2011 error = gotweb_get_repo_description(&repo_dir->description, srv,
2012 repo_dir->path, dirfd(dt));
2015 error = got_get_repo_owner(&repo_dir->owner, c);
2018 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2021 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2025 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2026 error = got_error_from_errno("closedir");
2030 static const struct got_error *
2031 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2033 const struct got_error *error;
2035 *repo_dir = calloc(1, sizeof(**repo_dir));
2036 if (*repo_dir == NULL)
2037 return got_error_from_errno("calloc");
2039 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2040 error = got_error_from_errno("asprintf");
2045 (*repo_dir)->owner = NULL;
2046 (*repo_dir)->description = NULL;
2047 (*repo_dir)->url = NULL;
2048 (*repo_dir)->age = NULL;
2049 (*repo_dir)->path = NULL;
2054 static const struct got_error *
2055 gotweb_get_repo_description(char **description, struct server *srv,
2056 const char *dirpath, int dir)
2058 const struct got_error *error = NULL;
2063 *description = NULL;
2064 if (srv->show_repo_description == 0)
2067 fd = openat(dir, "description", O_RDONLY);
2069 if (errno != ENOENT && errno != EACCES) {
2070 error = got_error_from_errno_fmt("openat %s/%s",
2071 dirpath, "description");
2076 if (fstat(fd, &sb) == -1) {
2077 error = got_error_from_errno_fmt("fstat %s/%s",
2078 dirpath, "description");
2083 if (len > GOTWEBD_MAXDESCRSZ - 1)
2084 len = GOTWEBD_MAXDESCRSZ - 1;
2086 *description = calloc(len + 1, sizeof(**description));
2087 if (*description == NULL) {
2088 error = got_error_from_errno("calloc");
2092 if (read(fd, *description, len) == -1)
2093 error = got_error_from_errno("read");
2095 if (fd != -1 && close(fd) == -1 && error == NULL)
2096 error = got_error_from_errno("close");
2100 static const struct got_error *
2101 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2104 const struct got_error *error = NULL;
2110 if (srv->show_repo_cloneurl == 0)
2113 fd = openat(dir, "cloneurl", O_RDONLY);
2115 if (errno != ENOENT && errno != EACCES) {
2116 error = got_error_from_errno_fmt("openat %s/%s",
2117 dirpath, "cloneurl");
2122 if (fstat(fd, &sb) == -1) {
2123 error = got_error_from_errno_fmt("fstat %s/%s",
2124 dirpath, "cloneurl");
2129 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2130 len = GOTWEBD_MAXCLONEURLSZ - 1;
2132 *url = calloc(len + 1, sizeof(**url));
2134 error = got_error_from_errno("calloc");
2138 if (read(fd, *url, len) == -1)
2139 error = got_error_from_errno("read");
2141 if (fd != -1 && close(fd) == -1 && error == NULL)
2142 error = got_error_from_errno("close");
2146 const struct got_error *
2147 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2150 long long diff_time;
2151 const char *years = "years ago", *months = "months ago";
2152 const char *weeks = "weeks ago", *days = "days ago";
2153 const char *hours = "hours ago", *minutes = "minutes ago";
2154 const char *seconds = "seconds ago", *now = "right now";
2162 diff_time = time(NULL) - committer_time;
2163 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2164 if (asprintf(repo_age, "%lld %s",
2165 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2166 return got_error_from_errno("asprintf");
2167 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2168 if (asprintf(repo_age, "%lld %s",
2169 (diff_time / 60 / 60 / 24 / (365 / 12)),
2171 return got_error_from_errno("asprintf");
2172 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2173 if (asprintf(repo_age, "%lld %s",
2174 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2175 return got_error_from_errno("asprintf");
2176 } else if (diff_time > 60 * 60 * 24 * 2) {
2177 if (asprintf(repo_age, "%lld %s",
2178 (diff_time / 60 / 60 / 24), days) == -1)
2179 return got_error_from_errno("asprintf");
2180 } else if (diff_time > 60 * 60 * 2) {
2181 if (asprintf(repo_age, "%lld %s",
2182 (diff_time / 60 / 60), hours) == -1)
2183 return got_error_from_errno("asprintf");
2184 } else if (diff_time > 60 * 2) {
2185 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2187 return got_error_from_errno("asprintf");
2188 } else if (diff_time > 2) {
2189 if (asprintf(repo_age, "%lld %s", diff_time,
2191 return got_error_from_errno("asprintf");
2193 if (asprintf(repo_age, "%s", now) == -1)
2194 return got_error_from_errno("asprintf");
2198 if (gmtime_r(&committer_time, &tm) == NULL)
2199 return got_error_from_errno("gmtime_r");
2201 s = asctime_r(&tm, datebuf);
2203 return got_error_from_errno("asctime_r");
2205 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2206 return got_error_from_errno("asprintf");