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"
55 static const struct querystring_keys querystring_keys[] = {
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
66 static const struct action_keys action_keys[] = {
70 { "commits", COMMITS },
74 { "summary", SUMMARY },
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 static const struct got_error *gotweb_render_index(struct request *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 static const struct got_error *gotweb_load_got_path(struct request *c,
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 static const struct got_error *gotweb_render_blame(struct request *);
96 static const struct got_error *gotweb_render_diff(struct request *);
97 static const struct got_error *gotweb_render_summary(struct request *);
98 static const struct got_error *gotweb_render_tag(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_tree(struct request *);
101 static const struct got_error *gotweb_render_branches(struct request *);
103 static void gotweb_free_querystring(struct querystring *);
104 static void gotweb_free_repo_dir(struct repo_dir *);
106 struct server *gotweb_get_server(uint8_t *, uint8_t *);
109 gotweb_process_request(struct request *c)
111 const struct got_error *error = NULL, *error2 = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
121 log_warnx("%s: %s", __func__, error->msg);
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
130 log_warnx("%s: error server is NULL", __func__);
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
137 log_warnx("%s: %s", __func__, error->msg);
141 error = gotweb_parse_querystring(&qs, c->querystring);
143 log_warnx("%s: %s", __func__, error->msg);
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
153 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == DIFF)) {
155 error2 = got_error(GOT_ERR_QUERYSTRING);
159 if (qs->action != INDEX) {
160 error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 error = gotweb_load_got_path(c, repo_dir);
164 c->t->repo_dir = repo_dir;
165 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
169 if (qs->action == BLOB) {
170 error = got_get_repo_commits(c, 1);
173 error = got_output_file_blob(c);
175 log_warnx("%s: %s", __func__, error->msg);
181 if (qs->action == RSS) {
182 error = gotweb_render_content_type(c,
183 "application/rss+xml;charset=utf-8");
185 log_warnx("%s: %s", __func__, error->msg);
189 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
191 log_warnx("%s: %s", __func__, error->msg);
194 if (gotweb_render_rss(c->tp) == -1)
200 error = gotweb_render_content_type(c, "text/html");
202 log_warnx("%s: %s", __func__, error->msg);
207 if (gotweb_render_header(c->tp) == -1)
217 error = gotweb_render_blame(c);
219 log_warnx("%s: %s", __func__, error->msg);
224 if (gotweb_render_briefs(c->tp) == -1)
228 error = got_get_repo_commits(c, srv->max_commits_display);
230 log_warnx("%s: %s", __func__, error->msg);
233 if (gotweb_render_commits(c->tp) == -1)
237 error = gotweb_render_diff(c);
239 log_warnx("%s: %s", __func__, error->msg);
244 error = gotweb_render_index(c);
246 log_warnx("%s: %s", __func__, error->msg);
251 error = gotweb_render_summary(c);
253 log_warnx("%s: %s", __func__, error->msg);
258 error = gotweb_render_tag(c);
260 log_warnx("%s: %s", __func__, error->msg);
265 error = gotweb_render_tags(c);
267 log_warnx("%s: %s", __func__, error->msg);
272 error = gotweb_render_tree(c);
274 log_warnx("%s: %s", __func__, error->msg);
280 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
281 "Erorr: Bad Querystring");
289 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
291 if (fcgi_printf(c, "\n%s", err) == -1)
294 if (fcgi_printf(c, "%s", error->msg) == -1)
297 if (fcgi_printf(c, "see daemon logs for details") == -1)
300 if (html && fcgi_printf(c, "</div>\n") == -1)
303 if (html && srv != NULL)
304 gotweb_render_footer(c->tp);
308 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
310 struct server *srv = NULL;
312 /* check against the server name first */
313 if (strlen(server_name) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, server_name) == 0)
318 /* check against subdomain second */
319 if (strlen(subdomain) > 0)
320 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
321 if (strcmp(srv->name, subdomain) == 0)
324 /* if those fail, send first server */
325 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
332 const struct got_error *
333 gotweb_init_transport(struct transport **t)
335 const struct got_error *error = NULL;
337 *t = calloc(1, sizeof(**t));
339 return got_error_from_errno2("%s: calloc", __func__);
341 TAILQ_INIT(&(*t)->repo_commits);
342 TAILQ_INIT(&(*t)->repo_tags);
345 (*t)->repo_dir = NULL;
347 (*t)->next_id = NULL;
348 (*t)->prev_id = NULL;
355 static const struct got_error *
356 gotweb_init_querystring(struct querystring **qs)
358 const struct got_error *error = NULL;
360 *qs = calloc(1, sizeof(**qs));
362 return got_error_from_errno2("%s: calloc", __func__);
364 (*qs)->headref = strdup("HEAD");
365 if ((*qs)->headref == NULL) {
368 return got_error_from_errno2("%s: strdup", __func__);
371 (*qs)->action = INDEX;
372 (*qs)->commit = NULL;
374 (*qs)->folder = NULL;
375 (*qs)->index_page = 0;
381 static const struct got_error *
382 gotweb_parse_querystring(struct querystring **qs, char *qst)
384 const struct got_error *error = NULL;
385 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
386 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
393 return got_error_from_errno2("%s: strdup", __func__);
398 while (tok1_pair != NULL) {
399 strsep(&tok1_end, "&");
401 tok2 = strdup(tok1_pair);
404 return got_error_from_errno2("%s: strdup", __func__);
410 while (tok2_pair != NULL) {
411 strsep(&tok2_end, "=");
413 error = gotweb_assign_querystring(qs, tok2_pair,
418 tok2_pair = tok2_end;
421 tok1_pair = tok1_end;
432 * Adapted from usr.sbin/httpd/httpd.c url_decode.
434 static const struct got_error *
435 gotweb_urldecode(char *url)
447 /* Encoding character is followed by two hex chars */
448 if (!isxdigit((unsigned char)p[1]) ||
449 !isxdigit((unsigned char)p[2]) ||
450 (p[1] == '0' && p[2] == '0'))
451 return got_error(GOT_ERR_BAD_QUERYSTRING);
457 * We don't have to validate "hex" because it is
458 * guaranteed to include two hex chars followed by nul.
460 x = strtoul(hex, NULL, 16);
476 static const struct got_error *
477 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
479 const struct got_error *error = NULL;
483 error = gotweb_urldecode(value);
487 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
488 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
491 switch (querystring_keys[el_cnt].element) {
493 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
494 if (strcmp(value, action_keys[a_cnt].name) != 0)
496 else if (strcmp(value,
497 action_keys[a_cnt].name) == 0){
499 action_keys[a_cnt].action;
507 (*qs)->commit = strdup(value);
508 if ((*qs)->commit == NULL) {
509 error = got_error_from_errno2("%s: strdup",
515 (*qs)->file = strdup(value);
516 if ((*qs)->file == NULL) {
517 error = got_error_from_errno2("%s: strdup",
523 (*qs)->folder = strdup(value);
524 if ((*qs)->folder == NULL) {
525 error = got_error_from_errno2("%s: strdup",
531 free((*qs)->headref);
532 (*qs)->headref = strdup(value);
533 if ((*qs)->headref == NULL) {
534 error = got_error_from_errno2("%s: strdup",
540 if (strlen(value) == 0)
542 (*qs)->index_page = strtonum(value, INT64_MIN,
545 error = got_error_from_errno3("%s: strtonum %s",
549 if ((*qs)->index_page < 0)
550 (*qs)->index_page = 0;
553 (*qs)->path = strdup(value);
554 if ((*qs)->path == NULL) {
555 error = got_error_from_errno2("%s: strdup",
561 if (strlen(value) == 0)
563 (*qs)->page = strtonum(value, INT64_MIN,
566 error = got_error_from_errno3("%s: strtonum %s",
582 gotweb_free_repo_tag(struct repo_tag *rt)
587 free(rt->tag_commit);
588 free(rt->commit_msg);
595 gotweb_free_repo_commit(struct repo_commit *rc)
605 free(rc->commit_msg);
611 gotweb_free_querystring(struct querystring *qs)
624 gotweb_free_repo_dir(struct repo_dir *repo_dir)
626 if (repo_dir != NULL) {
627 free(repo_dir->name);
628 free(repo_dir->owner);
629 free(repo_dir->description);
632 free(repo_dir->path);
638 gotweb_free_transport(struct transport *t)
640 struct repo_commit *rc = NULL, *trc = NULL;
641 struct repo_tag *rt = NULL, *trt = NULL;
643 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
644 TAILQ_REMOVE(&t->repo_commits, rc, entry);
645 gotweb_free_repo_commit(rc);
647 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
648 TAILQ_REMOVE(&t->repo_tags, rt, entry);
649 gotweb_free_repo_tag(rt);
651 gotweb_free_repo_dir(t->repo_dir);
652 gotweb_free_querystring(t->qs);
658 const struct got_error *
659 gotweb_render_content_type(struct request *c, const uint8_t *type)
661 const char *csp = "default-src 'self'; script-src 'none'; "
662 "object-src 'none';";
665 "Content-Security-Policy: %s\r\n"
666 "Content-Type: %s\r\n\r\n",
671 const struct got_error *
672 gotweb_render_content_type_file(struct request *c, const char *type,
675 fcgi_printf(c, "Content-type: %s\r\n"
676 "Content-disposition: attachment; filename=%s\r\n\r\n",
682 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
683 struct gotweb_url *next, int *have_next)
685 struct transport *t = c->t;
686 struct querystring *qs = t->qs;
687 struct server *srv = c->srv;
689 *have_prev = *have_next = 0;
693 if (qs->index_page > 0) {
695 *prev = (struct gotweb_url){
697 .index_page = qs->index_page - 1,
701 if (t->next_disp == srv->max_repos_display &&
702 t->repos_total != (qs->index_page + 1) *
703 srv->max_repos_display) {
705 *next = (struct gotweb_url){
707 .index_page = qs->index_page + 1,
713 if (t->prev_id && qs->commit != NULL &&
714 strcmp(qs->commit, t->prev_id) != 0) {
716 *prev = (struct gotweb_url){
719 .page = qs->page - 1,
721 .commit = t->prev_id,
722 .headref = qs->headref,
727 *next = (struct gotweb_url){
730 .page = qs->page + 1,
732 .commit = t->next_id,
733 .headref = qs->headref,
738 if (t->prev_id && qs->commit != NULL &&
739 strcmp(qs->commit, t->prev_id) != 0) {
741 *prev = (struct gotweb_url){
744 .page = qs->page - 1,
746 .commit = t->prev_id,
747 .headref = qs->headref,
748 .folder = qs->folder,
754 *next = (struct gotweb_url){
757 .page = qs->page + 1,
759 .commit = t->next_id,
760 .headref = qs->headref,
761 .folder = qs->folder,
767 if (t->prev_id && qs->commit != NULL &&
768 strcmp(qs->commit, t->prev_id) != 0) {
770 *prev = (struct gotweb_url){
773 .page = qs->page - 1,
775 .commit = t->prev_id,
776 .headref = qs->headref,
781 *next = (struct gotweb_url){
784 .page = qs->page + 1,
786 .commit = t->next_id,
787 .headref = qs->headref,
794 static const struct got_error *
795 gotweb_render_index(struct request *c)
797 const struct got_error *error = NULL;
798 struct server *srv = c->srv;
799 struct transport *t = c->t;
800 struct querystring *qs = t->qs;
801 struct repo_dir *repo_dir = NULL;
803 struct dirent **sd_dent = NULL;
804 unsigned int d_cnt, d_i, d_disp = 0;
805 unsigned int d_skipped = 0;
808 d = opendir(srv->repos_path);
810 error = got_error_from_errno2("opendir", srv->repos_path);
814 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
817 error = got_error_from_errno2("scandir", srv->repos_path);
821 if (gotweb_render_repo_table_hdr(c->tp) == -1)
824 for (d_i = 0; d_i < d_cnt; d_i++) {
825 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
828 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
829 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
834 error = got_path_dirent_type(&type, srv->repos_path,
838 if (type != DT_DIR) {
843 if (qs->index_page > 0 && (qs->index_page *
844 srv->max_repos_display) > t->prev_disp) {
849 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
853 error = gotweb_load_got_path(c, repo_dir);
854 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
856 gotweb_free_repo_dir(repo_dir);
861 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
867 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
870 gotweb_free_repo_dir(repo_dir);
873 if (d_disp == srv->max_repos_display)
876 t->repos_total = d_cnt - d_skipped;
878 if (srv->max_repos_display == 0)
880 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
882 if (t->repos_total <= srv->max_repos ||
883 t->repos_total <= srv->max_repos_display)
886 if (gotweb_render_navs(c->tp) == -1)
890 for (d_i = 0; d_i < d_cnt; d_i++)
894 if (d != NULL && closedir(d) == EOF && error == NULL)
895 error = got_error_from_errno("closedir");
899 static const struct got_error *
900 gotweb_render_blame(struct request *c)
902 const struct got_error *error = NULL;
903 struct transport *t = c->t;
904 struct repo_commit *rc = NULL;
905 char *age = NULL, *msg = NULL;
908 error = got_get_repo_commits(c, 1);
912 rc = TAILQ_FIRST(&t->repo_commits);
914 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
917 error = gotweb_escape_html(&msg, rc->commit_msg);
921 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
922 "<div id='blame_title'>Blame</div>\n"
923 "</div>\n" /* #blame_title_wrapper */
924 "<div id='blame_content'>\n"
925 "<div id='blame_header_wrapper'>\n"
926 "<div id='blame_header'>\n"
927 "<div class='header_age_title'>Date:</div>\n"
928 "<div class='header_age'>%s</div>\n"
929 "<div id='header_commit_msg_title'>Message:</div>\n"
930 "<div id='header_commit_msg'>%s</div>\n"
931 "</div>\n" /* #blame_header */
932 "</div>\n" /* #blame_header_wrapper */
933 "<div class='dotted_line'></div>\n"
934 "<div id='blame'>\n",
940 error = got_output_file_blame(c);
944 fcgi_printf(c, "</div>\n" /* #blame */
945 "</div>\n"); /* #blame_content */
952 static const struct got_error *
953 gotweb_render_branches(struct request *c)
955 const struct got_error *error = NULL;
956 struct got_reflist_head refs;
957 struct got_reflist_entry *re;
958 struct transport *t = c->t;
959 struct querystring *qs = t->qs;
960 struct got_repository *repo = t->repo;
961 char *escaped_refname = NULL;
967 error = got_ref_list(&refs, repo, "refs/heads",
968 got_ref_cmp_by_name, NULL);
972 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
973 "<div id='branches_title'>Branches</div>\n"
974 "</div>\n" /* #branches_title_wrapper */
975 "<div id='branches_content'>\n");
979 TAILQ_FOREACH(re, &refs, entry) {
980 const char *refname = NULL;
982 if (got_ref_is_symbolic(re->ref))
985 refname = got_ref_get_name(re->ref);
986 if (refname == NULL) {
987 error = got_error_from_errno("strdup");
990 if (strncmp(refname, "refs/heads/", 11) != 0)
993 error = got_get_repo_age(&age, c, refname, TM_DIFF);
997 if (strncmp(refname, "refs/heads/", 11) == 0)
999 error = gotweb_escape_html(&escaped_refname, refname);
1003 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1004 "<div class='branches_age'>%s</div>\n"
1005 "<div class='branches_space'> </div>\n"
1006 "<div class='branch'>", age);
1010 r = gotweb_link(c, &(struct gotweb_url){
1016 }, "%s", escaped_refname);
1020 if (fcgi_printf(c, "</div>\n" /* .branch */
1021 "<div class='navs_wrapper'>\n"
1022 "<div class='navs'>") == -1)
1025 r = gotweb_link(c, &(struct gotweb_url){
1035 if (fcgi_printf(c, " | ") == -1)
1038 r = gotweb_link(c, &(struct gotweb_url){
1044 }, "commit briefs");
1048 if (fcgi_printf(c, " | ") == -1)
1051 r = gotweb_link(c, &(struct gotweb_url){
1061 r = fcgi_printf(c, "</div>\n" /* .navs */
1062 "</div>\n" /* .navs_wrapper */
1063 "<div class='dotted_line'></div>\n"
1064 "</div>\n"); /* .branches_wrapper */
1070 free(escaped_refname);
1071 escaped_refname = NULL;
1073 fcgi_printf(c, "</div>\n"); /* #branches_content */
1076 free(escaped_refname);
1077 got_ref_list_free(&refs);
1081 static const struct got_error *
1082 gotweb_render_tree(struct request *c)
1084 const struct got_error *error = NULL;
1085 struct transport *t = c->t;
1086 struct repo_commit *rc = NULL;
1087 char *age = NULL, *msg = NULL;
1090 error = got_get_repo_commits(c, 1);
1094 rc = TAILQ_FIRST(&t->repo_commits);
1096 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1100 error = gotweb_escape_html(&msg, rc->commit_msg);
1104 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1105 "<div id='tree_title'>Tree</div>\n"
1106 "</div>\n" /* #tree_title_wrapper */
1107 "<div id='tree_content'>\n"
1108 "<div id='tree_header_wrapper'>\n"
1109 "<div id='tree_header'>\n"
1110 "<div id='header_tree_title'>Tree:</div>\n"
1111 "<div id='header_tree'>%s</div>\n"
1112 "<div class='header_age_title'>Date:</div>\n"
1113 "<div class='header_age'>%s</div>\n"
1114 "<div id='header_commit_msg_title'>Message:</div>\n"
1115 "<div id='header_commit_msg'>%s</div>\n"
1116 "</div>\n" /* #tree_header */
1117 "</div>\n" /* #tree_header_wrapper */
1118 "<div class='dotted_line'></div>\n"
1119 "<div id='tree'>\n",
1126 error = got_output_repo_tree(c);
1130 fcgi_printf(c, "</div>\n"); /* #tree */
1131 fcgi_printf(c, "</div>\n"); /* #tree_content */
1138 static const struct got_error *
1139 gotweb_render_diff(struct request *c)
1141 const struct got_error *error = NULL;
1142 struct transport *t = c->t;
1143 struct repo_commit *rc = NULL;
1144 char *age = NULL, *author = NULL, *msg = NULL;
1147 error = got_get_repo_commits(c, 1);
1151 rc = TAILQ_FIRST(&t->repo_commits);
1153 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1156 error = gotweb_escape_html(&author, rc->author);
1159 error = gotweb_escape_html(&msg, rc->commit_msg);
1163 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1164 "<div id='diff_title'>Commit Diff</div>\n"
1165 "</div>\n" /* #diff_title_wrapper */
1166 "<div id='diff_content'>\n"
1167 "<div id='diff_header_wrapper'>\n"
1168 "<div id='diff_header'>\n"
1169 "<div id='header_diff_title'>Diff:</div>\n"
1170 "<div id='header_diff'>%s<br />%s</div>\n"
1171 "<div class='header_commit_title'>Commit:</div>\n"
1172 "<div class='header_commit'>%s</div>\n"
1173 "<div id='header_tree_title'>Tree:</div>\n"
1174 "<div id='header_tree'>%s</div>\n"
1175 "<div class='header_author_title'>Author:</div>\n"
1176 "<div class='header_author'>%s</div>\n"
1177 "<div class='header_age_title'>Date:</div>\n"
1178 "<div class='header_age'>%s</div>\n"
1179 "<div id='header_commit_msg_title'>Message:</div>\n"
1180 "<div id='header_commit_msg'>%s</div>\n"
1181 "</div>\n" /* #diff_header */
1182 "</div>\n" /* #diff_header_wrapper */
1183 "<div class='dotted_line'></div>\n"
1184 "<div id='diff'>\n",
1185 rc->parent_id, rc->commit_id,
1194 error = got_output_repo_diff(c);
1198 fcgi_printf(c, "</div>\n"); /* #diff */
1199 fcgi_printf(c, "</div>\n"); /* #diff_content */
1207 static const struct got_error *
1208 gotweb_render_summary(struct request *c)
1210 const struct got_error *error = NULL;
1211 struct transport *t = c->t;
1212 struct server *srv = c->srv;
1215 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1218 if (srv->show_repo_description) {
1220 "<div id='description_title'>Description:</div>\n"
1221 "<div id='description'>%s</div>\n",
1222 t->repo_dir->description ? t->repo_dir->description : "");
1227 if (srv->show_repo_owner) {
1229 "<div id='repo_owner_title'>Owner:</div>\n"
1230 "<div id='repo_owner'>%s</div>\n",
1231 t->repo_dir->owner ? t->repo_dir->owner : "");
1236 if (srv->show_repo_age) {
1238 "<div id='last_change_title'>Last Change:</div>\n"
1239 "<div id='last_change'>%s</div>\n",
1245 if (srv->show_repo_cloneurl) {
1247 "<div id='cloneurl_title'>Clone URL:</div>\n"
1248 "<div id='cloneurl'>%s</div>\n",
1249 t->repo_dir->url ? t->repo_dir->url : "");
1254 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1258 if (gotweb_render_briefs(c->tp) == -1)
1261 error = gotweb_render_tags(c);
1263 log_warnx("%s: %s", __func__, error->msg);
1267 error = gotweb_render_branches(c);
1269 log_warnx("%s: %s", __func__, error->msg);
1274 static const struct got_error *
1275 gotweb_render_tag(struct request *c)
1277 const struct got_error *error = NULL;
1278 struct repo_tag *rt = NULL;
1279 struct transport *t = c->t;
1280 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1282 error = got_get_repo_tags(c, 1);
1286 if (t->tag_count == 0) {
1287 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1292 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1294 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1297 error = gotweb_escape_html(&author, rt->tagger);
1300 error = gotweb_escape_html(&msg, rt->commit_msg);
1304 tagname = rt->tag_name;
1305 if (strncmp(tagname, "refs/", 5) == 0)
1307 error = gotweb_escape_html(&tagname, tagname);
1311 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1312 "<div id='tags_title'>Tag</div>\n"
1313 "</div>\n" /* #tags_title_wrapper */
1314 "<div id='tags_content'>\n"
1315 "<div id='tag_header_wrapper'>\n"
1316 "<div id='tag_header'>\n"
1317 "<div class='header_commit_title'>Commit:</div>\n"
1318 "<div class='header_commit'>%s"
1319 " <span class='refs_str'>(%s)</span></div>\n"
1320 "<div class='header_author_title'>Tagger:</div>\n"
1321 "<div class='header_author'>%s</div>\n"
1322 "<div class='header_age_title'>Date:</div>\n"
1323 "<div class='header_age'>%s</div>\n"
1324 "<div id='header_commit_msg_title'>Message:</div>\n"
1325 "<div id='header_commit_msg'>%s</div>\n"
1326 "</div>\n" /* #tag_header */
1327 "<div class='dotted_line'></div>\n"
1328 "<div id='tag_commit'>\n%s</div>"
1329 "</div>" /* #tag_header_wrapper */
1330 "</div>", /* #tags_content */
1345 static const struct got_error *
1346 gotweb_render_tags(struct request *c)
1348 const struct got_error *error = NULL;
1349 struct repo_tag *rt = NULL;
1350 struct server *srv = c->srv;
1351 struct transport *t = c->t;
1352 struct querystring *qs = t->qs;
1353 struct repo_dir *repo_dir = t->repo_dir;
1354 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1355 int r, commit_found = 0;
1357 if (qs->action == BRIEFS) {
1359 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1361 error = got_get_repo_tags(c, srv->max_commits_display);
1365 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1366 "<div id='tags_title'>Tags</div>\n"
1367 "</div>\n" /* #tags_title_wrapper */
1368 "<div id='tags_content'>\n");
1372 if (t->tag_count == 0) {
1373 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1374 "This repository contains no tags");
1379 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1380 if (commit_found == 0 && qs->commit != NULL) {
1381 if (strcmp(qs->commit, rt->commit_id) != 0)
1386 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1390 tagname = rt->tag_name;
1391 if (strncmp(tagname, "refs/tags/", 10) == 0)
1393 error = gotweb_escape_html(&tagname, tagname);
1397 if (rt->tag_commit != NULL) {
1398 newline = strchr(rt->tag_commit, '\n');
1401 error = gotweb_escape_html(&msg, rt->tag_commit);
1406 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1407 "<div class='tag'>%s</div>\n"
1408 "<div class='tag_log'>", age, tagname) == -1)
1411 r = gotweb_link(c, &(struct gotweb_url){
1415 .path = repo_dir->name,
1416 .commit = rt->commit_id,
1417 }, "%s", msg ? msg : "");
1421 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1422 "<div class='navs_wrapper'>\n"
1423 "<div class='navs'>") == -1)
1426 r = gotweb_link(c, &(struct gotweb_url){
1430 .path = repo_dir->name,
1431 .commit = rt->commit_id,
1436 if (fcgi_printf(c, " | ") == -1)
1439 r = gotweb_link(c, &(struct gotweb_url){
1443 .path = repo_dir->name,
1444 .commit = rt->commit_id,
1445 }, "commit briefs");
1449 if (fcgi_printf(c, " | ") == -1)
1452 r = gotweb_link(c, &(struct gotweb_url){
1456 .path = repo_dir->name,
1457 .commit = rt->commit_id,
1463 "</div>\n" /* .navs */
1464 "</div>\n" /* .navs_wrapper */
1465 "<div class='dotted_line'></div>\n");
1476 if (t->next_id || t->prev_id) {
1477 if (gotweb_render_navs(c->tp) == -1)
1480 fcgi_printf(c, "</div>\n"); /* #tags_content */
1488 const struct got_error *
1489 gotweb_escape_html(char **escaped_html, const char *orig_html)
1491 const struct got_error *error = NULL;
1492 struct escape_pair {
1503 size_t orig_len, len;
1506 orig_len = strlen(orig_html);
1508 for (i = 0; i < orig_len; i++) {
1509 for (j = 0; j < nitems(esc); j++) {
1510 if (orig_html[i] != esc[j].c)
1512 len += strlen(esc[j].s) - 1 /* escaped char */;
1516 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1517 if (*escaped_html == NULL)
1518 return got_error_from_errno("calloc");
1521 for (i = 0; i < orig_len; i++) {
1523 for (j = 0; j < nitems(esc); j++) {
1524 if (orig_html[i] != esc[j].c)
1527 if (strlcat(*escaped_html, esc[j].s, len + 1)
1529 error = got_error(GOT_ERR_NO_SPACE);
1532 x += strlen(esc[j].s);
1537 (*escaped_html)[x] = orig_html[i];
1543 free(*escaped_html);
1544 *escaped_html = NULL;
1546 (*escaped_html)[x] = '\0';
1553 should_urlencode(int c)
1555 if (c <= ' ' || c >= 127)
1586 gotweb_urlencode(const char *str)
1594 for (s = str; *s; ++s) {
1596 if (should_urlencode(*s))
1600 escaped = calloc(1, len + 1);
1601 if (escaped == NULL)
1605 for (s = str; *s; ++s) {
1606 if (should_urlencode(*s)) {
1607 a = (*s & 0xF0) >> 4;
1611 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1612 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1621 gotweb_action_name(int action)
1654 gotweb_render_url(struct request *c, struct gotweb_url *url)
1656 const char *sep = "?", *action;
1660 action = gotweb_action_name(url->action);
1661 if (action != NULL) {
1662 if (fcgi_printf(c, "?action=%s", action) == -1)
1668 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1674 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1680 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1686 tmp = gotweb_urlencode(url->file);
1689 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1697 tmp = gotweb_urlencode(url->folder);
1700 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1708 tmp = gotweb_urlencode(url->headref);
1711 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1718 if (url->index_page != -1) {
1719 if (fcgi_printf(c, "%sindex_page=%d", sep,
1720 url->index_page) == -1)
1726 tmp = gotweb_urlencode(url->path);
1729 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1736 if (url->page != -1) {
1737 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1746 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1748 struct template *tp = c->tp;
1749 const char *proto = c->https ? "https" : "http";
1751 if (fcgi_puts(tp, proto) == -1 ||
1752 fcgi_puts(tp, "://") == -1 ||
1753 tp_htmlescape(tp, c->server_name) == -1 ||
1754 tp_htmlescape(tp, c->document_uri) == -1)
1757 return gotweb_render_url(c, url);
1761 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1766 if (fcgi_printf(c, "<a href='") == -1)
1769 if (gotweb_render_url(c, url) == -1)
1772 if (fcgi_printf(c, "'>") == -1)
1776 r = fcgi_vprintf(c, fmt, ap);
1781 if (fcgi_printf(c, "</a>"))
1786 static struct got_repository *
1787 find_cached_repo(struct server *srv, const char *path)
1791 for (i = 0; i < srv->ncached_repos; i++) {
1792 if (strcmp(srv->cached_repos[i].path, path) == 0)
1793 return srv->cached_repos[i].repo;
1799 static const struct got_error *
1800 cache_repo(struct got_repository **new, struct server *srv,
1801 struct repo_dir *repo_dir, struct socket *sock)
1803 const struct got_error *error = NULL;
1804 struct got_repository *repo;
1805 struct cached_repo *cr;
1808 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1809 cr = &srv->cached_repos[srv->ncached_repos - 1];
1810 error = got_repo_close(cr->repo);
1811 memset(cr, 0, sizeof(*cr));
1812 srv->ncached_repos--;
1815 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1816 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1817 cr = &srv->cached_repos[0];
1820 cr = &srv->cached_repos[srv->ncached_repos];
1823 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1826 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1827 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1832 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1833 >= sizeof(cr->path)) {
1835 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1836 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1838 return got_error(GOT_ERR_NO_SPACE);
1842 srv->ncached_repos++;
1847 static const struct got_error *
1848 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1850 const struct got_error *error = NULL;
1851 struct socket *sock = c->sock;
1852 struct server *srv = c->srv;
1853 struct transport *t = c->t;
1854 struct got_repository *repo = NULL;
1858 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1859 GOTWEB_GIT_DIR) == -1)
1860 return got_error_from_errno("asprintf");
1862 dt = opendir(dir_test);
1866 repo_dir->path = dir_test;
1871 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1872 repo_dir->name) == -1)
1873 return got_error_from_errno("asprintf");
1875 dt = opendir(dir_test);
1877 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1880 repo_dir->path = dir_test;
1885 if (srv->respect_exportok &&
1886 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1887 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1891 repo = find_cached_repo(srv, repo_dir->path);
1893 error = cache_repo(&repo, srv, repo_dir, sock);
1898 error = gotweb_get_repo_description(&repo_dir->description, srv,
1899 repo_dir->path, dirfd(dt));
1902 error = got_get_repo_owner(&repo_dir->owner, c);
1905 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1908 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1912 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1913 error = got_error_from_errno("closedir");
1917 static const struct got_error *
1918 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1920 const struct got_error *error;
1922 *repo_dir = calloc(1, sizeof(**repo_dir));
1923 if (*repo_dir == NULL)
1924 return got_error_from_errno("calloc");
1926 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1927 error = got_error_from_errno("asprintf");
1932 (*repo_dir)->owner = NULL;
1933 (*repo_dir)->description = NULL;
1934 (*repo_dir)->url = NULL;
1935 (*repo_dir)->age = NULL;
1936 (*repo_dir)->path = NULL;
1941 static const struct got_error *
1942 gotweb_get_repo_description(char **description, struct server *srv,
1943 const char *dirpath, int dir)
1945 const struct got_error *error = NULL;
1950 *description = NULL;
1951 if (srv->show_repo_description == 0)
1954 fd = openat(dir, "description", O_RDONLY);
1956 if (errno != ENOENT && errno != EACCES) {
1957 error = got_error_from_errno_fmt("openat %s/%s",
1958 dirpath, "description");
1963 if (fstat(fd, &sb) == -1) {
1964 error = got_error_from_errno_fmt("fstat %s/%s",
1965 dirpath, "description");
1970 if (len > GOTWEBD_MAXDESCRSZ - 1)
1971 len = GOTWEBD_MAXDESCRSZ - 1;
1973 *description = calloc(len + 1, sizeof(**description));
1974 if (*description == NULL) {
1975 error = got_error_from_errno("calloc");
1979 if (read(fd, *description, len) == -1)
1980 error = got_error_from_errno("read");
1982 if (fd != -1 && close(fd) == -1 && error == NULL)
1983 error = got_error_from_errno("close");
1987 static const struct got_error *
1988 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1991 const struct got_error *error = NULL;
1997 if (srv->show_repo_cloneurl == 0)
2000 fd = openat(dir, "cloneurl", O_RDONLY);
2002 if (errno != ENOENT && errno != EACCES) {
2003 error = got_error_from_errno_fmt("openat %s/%s",
2004 dirpath, "cloneurl");
2009 if (fstat(fd, &sb) == -1) {
2010 error = got_error_from_errno_fmt("fstat %s/%s",
2011 dirpath, "cloneurl");
2016 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2017 len = GOTWEBD_MAXCLONEURLSZ - 1;
2019 *url = calloc(len + 1, sizeof(**url));
2021 error = got_error_from_errno("calloc");
2025 if (read(fd, *url, len) == -1)
2026 error = got_error_from_errno("read");
2028 if (fd != -1 && close(fd) == -1 && error == NULL)
2029 error = got_error_from_errno("close");
2033 const struct got_error *
2034 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2037 long long diff_time;
2038 const char *years = "years ago", *months = "months ago";
2039 const char *weeks = "weeks ago", *days = "days ago";
2040 const char *hours = "hours ago", *minutes = "minutes ago";
2041 const char *seconds = "seconds ago", *now = "right now";
2050 diff_time = time(NULL) - committer_time;
2051 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2052 if (asprintf(repo_age, "%lld %s",
2053 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2054 return got_error_from_errno("asprintf");
2055 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2056 if (asprintf(repo_age, "%lld %s",
2057 (diff_time / 60 / 60 / 24 / (365 / 12)),
2059 return got_error_from_errno("asprintf");
2060 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2061 if (asprintf(repo_age, "%lld %s",
2062 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2063 return got_error_from_errno("asprintf");
2064 } else if (diff_time > 60 * 60 * 24 * 2) {
2065 if (asprintf(repo_age, "%lld %s",
2066 (diff_time / 60 / 60 / 24), days) == -1)
2067 return got_error_from_errno("asprintf");
2068 } else if (diff_time > 60 * 60 * 2) {
2069 if (asprintf(repo_age, "%lld %s",
2070 (diff_time / 60 / 60), hours) == -1)
2071 return got_error_from_errno("asprintf");
2072 } else if (diff_time > 60 * 2) {
2073 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2075 return got_error_from_errno("asprintf");
2076 } else if (diff_time > 2) {
2077 if (asprintf(repo_age, "%lld %s", diff_time,
2079 return got_error_from_errno("asprintf");
2081 if (asprintf(repo_age, "%s", now) == -1)
2082 return got_error_from_errno("asprintf");
2086 if (gmtime_r(&committer_time, &tm) == NULL)
2087 return got_error_from_errno("gmtime_r");
2089 s = asctime_r(&tm, datebuf);
2091 return got_error_from_errno("asctime_r");
2093 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2094 return got_error_from_errno("asprintf");
2097 if (gmtime_r(&committer_time, &tm) == NULL)
2098 return got_error_from_errno("gmtime_r");
2100 r = strftime(datebuf, sizeof(datebuf),
2101 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2103 return got_error(GOT_ERR_NO_SPACE);
2105 *repo_age = strdup(datebuf);
2106 if (*repo_age == NULL)
2107 return got_error_from_errno("asprintf");