2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
53 static const struct querystring_keys querystring_keys[] = {
58 { "headref", HEADREF },
59 { "index_page", INDEX_PAGE },
64 static const struct action_keys action_keys[] = {
68 { "commits", COMMITS },
72 { "summary", SUMMARY },
78 static const struct got_error *gotweb_init_querystring(struct querystring **);
79 static const struct got_error *gotweb_parse_querystring(struct querystring **,
81 static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 static const struct got_error *gotweb_render_index(struct request *);
84 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 static const struct got_error *gotweb_load_got_path(struct request *c,
88 static const struct got_error *gotweb_get_repo_description(char **,
89 struct server *, const char *, int);
90 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 static const struct got_error *gotweb_render_blame(struct request *);
93 static const struct got_error *gotweb_render_diff(struct request *);
94 static const struct got_error *gotweb_render_summary(struct request *);
95 static const struct got_error *gotweb_render_tag(struct request *);
96 static const struct got_error *gotweb_render_tags(struct request *);
97 static const struct got_error *gotweb_render_tree(struct request *);
98 static const struct got_error *gotweb_render_branches(struct request *);
100 static void gotweb_free_querystring(struct querystring *);
101 static void gotweb_free_repo_dir(struct repo_dir *);
103 struct server *gotweb_get_server(uint8_t *, uint8_t *);
106 gotweb_process_request(struct request *c)
108 const struct got_error *error = NULL, *error2 = NULL;
109 struct server *srv = NULL;
110 struct querystring *qs = NULL;
111 struct repo_dir *repo_dir = NULL;
112 uint8_t err[] = "gotwebd experienced an error: ";
115 /* init the transport */
116 error = gotweb_init_transport(&c->t);
118 log_warnx("%s: %s", __func__, error->msg);
121 /* don't process any further if client disconnected */
122 if (c->sock->client_status == CLIENT_DISCONNECT)
124 /* get the gotwebd server */
125 srv = gotweb_get_server(c->server_name, c->http_host);
127 log_warnx("%s: error server is NULL", __func__);
131 /* parse our querystring */
132 error = gotweb_init_querystring(&qs);
134 log_warnx("%s: %s", __func__, error->msg);
138 error = gotweb_parse_querystring(&qs, c->querystring);
140 log_warnx("%s: %s", __func__, error->msg);
145 * certain actions require a commit id in the querystring. this stops
146 * bad actors from exploiting this by manually manipulating the
150 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
151 qs->action == DIFF)) {
152 error2 = got_error(GOT_ERR_QUERYSTRING);
156 if (qs->action != INDEX) {
157 error = gotweb_init_repo_dir(&repo_dir, qs->path);
160 error = gotweb_load_got_path(c, repo_dir);
161 c->t->repo_dir = repo_dir;
162 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
166 /* render top of page */
167 if (qs != NULL && qs->action == BLOB) {
168 error = got_get_repo_commits(c, 1);
171 error = got_output_file_blob(c);
173 log_warnx("%s: %s", __func__, error->msg);
180 error = gotweb_render_content_type(c, "text/html");
182 log_warnx("%s: %s", __func__, error->msg);
187 if (gotweb_render_header(c->tp) == -1)
197 error = gotweb_render_blame(c);
199 log_warnx("%s: %s", __func__, error->msg);
204 if (gotweb_render_briefs(c->tp) == -1)
208 error = got_get_repo_commits(c, srv->max_commits_display);
210 log_warnx("%s: %s", __func__, error->msg);
213 if (gotweb_render_commits(c->tp) == -1)
217 error = gotweb_render_diff(c);
219 log_warnx("%s: %s", __func__, error->msg);
224 error = gotweb_render_index(c);
226 log_warnx("%s: %s", __func__, error->msg);
231 error = gotweb_render_summary(c);
233 log_warnx("%s: %s", __func__, error->msg);
238 error = gotweb_render_tag(c);
240 log_warnx("%s: %s", __func__, error->msg);
245 error = gotweb_render_tags(c);
247 log_warnx("%s: %s", __func__, error->msg);
252 error = gotweb_render_tree(c);
254 log_warnx("%s: %s", __func__, error->msg);
260 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
261 "Erorr: Bad Querystring");
269 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
271 if (fcgi_printf(c, "\n%s", err) == -1)
274 if (fcgi_printf(c, "%s", error->msg) == -1)
277 if (fcgi_printf(c, "see daemon logs for details") == -1)
280 if (html && fcgi_printf(c, "</div>\n") == -1)
283 if (html && srv != NULL)
284 gotweb_render_footer(c->tp);
288 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
290 struct server *srv = NULL;
292 /* check against the server name first */
293 if (strlen(server_name) > 0)
294 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
295 if (strcmp(srv->name, server_name) == 0)
298 /* check against subdomain second */
299 if (strlen(subdomain) > 0)
300 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
301 if (strcmp(srv->name, subdomain) == 0)
304 /* if those fail, send first server */
305 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
312 const struct got_error *
313 gotweb_init_transport(struct transport **t)
315 const struct got_error *error = NULL;
317 *t = calloc(1, sizeof(**t));
319 return got_error_from_errno2("%s: calloc", __func__);
321 TAILQ_INIT(&(*t)->repo_commits);
322 TAILQ_INIT(&(*t)->repo_tags);
325 (*t)->repo_dir = NULL;
327 (*t)->next_id = NULL;
328 (*t)->prev_id = NULL;
335 static const struct got_error *
336 gotweb_init_querystring(struct querystring **qs)
338 const struct got_error *error = NULL;
340 *qs = calloc(1, sizeof(**qs));
342 return got_error_from_errno2("%s: calloc", __func__);
344 (*qs)->headref = strdup("HEAD");
345 if ((*qs)->headref == NULL) {
348 return got_error_from_errno2("%s: strdup", __func__);
351 (*qs)->action = INDEX;
352 (*qs)->commit = NULL;
354 (*qs)->folder = NULL;
355 (*qs)->index_page = 0;
361 static const struct got_error *
362 gotweb_parse_querystring(struct querystring **qs, char *qst)
364 const struct got_error *error = NULL;
365 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
366 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
373 return got_error_from_errno2("%s: strdup", __func__);
378 while (tok1_pair != NULL) {
379 strsep(&tok1_end, "&");
381 tok2 = strdup(tok1_pair);
384 return got_error_from_errno2("%s: strdup", __func__);
390 while (tok2_pair != NULL) {
391 strsep(&tok2_end, "=");
393 error = gotweb_assign_querystring(qs, tok2_pair,
398 tok2_pair = tok2_end;
401 tok1_pair = tok1_end;
412 * Adapted from usr.sbin/httpd/httpd.c url_decode.
414 static const struct got_error *
415 gotweb_urldecode(char *url)
427 /* Encoding character is followed by two hex chars */
428 if (!isxdigit((unsigned char)p[1]) ||
429 !isxdigit((unsigned char)p[2]) ||
430 (p[1] == '0' && p[2] == '0'))
431 return got_error(GOT_ERR_BAD_QUERYSTRING);
437 * We don't have to validate "hex" because it is
438 * guaranteed to include two hex chars followed by nul.
440 x = strtoul(hex, NULL, 16);
456 static const struct got_error *
457 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
459 const struct got_error *error = NULL;
463 error = gotweb_urldecode(value);
467 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
468 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
471 switch (querystring_keys[el_cnt].element) {
473 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
474 if (strcmp(value, action_keys[a_cnt].name) != 0)
476 else if (strcmp(value,
477 action_keys[a_cnt].name) == 0){
479 action_keys[a_cnt].action;
487 (*qs)->commit = strdup(value);
488 if ((*qs)->commit == NULL) {
489 error = got_error_from_errno2("%s: strdup",
495 (*qs)->file = strdup(value);
496 if ((*qs)->file == NULL) {
497 error = got_error_from_errno2("%s: strdup",
503 (*qs)->folder = strdup(value);
504 if ((*qs)->folder == NULL) {
505 error = got_error_from_errno2("%s: strdup",
511 free((*qs)->headref);
512 (*qs)->headref = strdup(value);
513 if ((*qs)->headref == NULL) {
514 error = got_error_from_errno2("%s: strdup",
520 if (strlen(value) == 0)
522 (*qs)->index_page = strtonum(value, INT64_MIN,
525 error = got_error_from_errno3("%s: strtonum %s",
529 if ((*qs)->index_page < 0)
530 (*qs)->index_page = 0;
533 (*qs)->path = strdup(value);
534 if ((*qs)->path == NULL) {
535 error = got_error_from_errno2("%s: strdup",
541 if (strlen(value) == 0)
543 (*qs)->page = strtonum(value, INT64_MIN,
546 error = got_error_from_errno3("%s: strtonum %s",
562 gotweb_free_repo_tag(struct repo_tag *rt)
567 free(rt->tag_commit);
568 free(rt->commit_msg);
575 gotweb_free_repo_commit(struct repo_commit *rc)
585 free(rc->commit_msg);
591 gotweb_free_querystring(struct querystring *qs)
604 gotweb_free_repo_dir(struct repo_dir *repo_dir)
606 if (repo_dir != NULL) {
607 free(repo_dir->name);
608 free(repo_dir->owner);
609 free(repo_dir->description);
612 free(repo_dir->path);
618 gotweb_free_transport(struct transport *t)
620 struct repo_commit *rc = NULL, *trc = NULL;
621 struct repo_tag *rt = NULL, *trt = NULL;
623 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
624 TAILQ_REMOVE(&t->repo_commits, rc, entry);
625 gotweb_free_repo_commit(rc);
627 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
628 TAILQ_REMOVE(&t->repo_tags, rt, entry);
629 gotweb_free_repo_tag(rt);
631 gotweb_free_repo_dir(t->repo_dir);
632 gotweb_free_querystring(t->qs);
638 const struct got_error *
639 gotweb_render_content_type(struct request *c, const uint8_t *type)
641 const char *csp = "default-src 'self'; script-src 'none'; "
642 "object-src 'none';";
645 "Content-Security-Policy: %s\r\n"
646 "Content-Type: %s\r\n\r\n",
651 const struct got_error *
652 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
655 fcgi_printf(c, "Content-type: %s\r\n"
656 "Content-disposition: attachment; filename=%s\r\n\r\n",
662 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
663 struct gotweb_url *next, int *have_next)
665 struct transport *t = c->t;
666 struct querystring *qs = t->qs;
667 struct server *srv = c->srv;
669 *have_prev = *have_next = 0;
673 if (qs->index_page > 0) {
675 *prev = (struct gotweb_url){
677 .index_page = qs->index_page - 1,
681 if (t->next_disp == srv->max_repos_display &&
682 t->repos_total != (qs->index_page + 1) *
683 srv->max_repos_display) {
685 *next = (struct gotweb_url){
687 .index_page = qs->index_page + 1,
693 if (t->prev_id && qs->commit != NULL &&
694 strcmp(qs->commit, t->prev_id) != 0) {
696 *prev = (struct gotweb_url){
699 .page = qs->page - 1,
701 .commit = t->prev_id,
702 .headref = qs->headref,
707 *next = (struct gotweb_url){
710 .page = qs->page + 1,
712 .commit = t->next_id,
713 .headref = qs->headref,
718 if (t->prev_id && qs->commit != NULL &&
719 strcmp(qs->commit, t->prev_id) != 0) {
721 *prev = (struct gotweb_url){
724 .page = qs->page - 1,
726 .commit = t->prev_id,
727 .headref = qs->headref,
728 .folder = qs->folder,
734 *next = (struct gotweb_url){
737 .page = qs->page + 1,
739 .commit = t->next_id,
740 .headref = qs->headref,
741 .folder = qs->folder,
747 if (t->prev_id && qs->commit != NULL &&
748 strcmp(qs->commit, t->prev_id) != 0) {
750 *prev = (struct gotweb_url){
753 .page = qs->page - 1,
755 .commit = t->prev_id,
756 .headref = qs->headref,
761 *next = (struct gotweb_url){
764 .page = qs->page + 1,
766 .commit = t->next_id,
767 .headref = qs->headref,
774 static const struct got_error *
775 gotweb_render_index(struct request *c)
777 const struct got_error *error = NULL;
778 struct server *srv = c->srv;
779 struct transport *t = c->t;
780 struct querystring *qs = t->qs;
781 struct repo_dir *repo_dir = NULL;
783 struct dirent **sd_dent = NULL;
784 unsigned int d_cnt, d_i, d_disp = 0;
785 unsigned int d_skipped = 0;
788 d = opendir(srv->repos_path);
790 error = got_error_from_errno2("opendir", srv->repos_path);
794 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
797 error = got_error_from_errno2("scandir", srv->repos_path);
801 if (gotweb_render_repo_table_hdr(c->tp) == -1)
804 for (d_i = 0; d_i < d_cnt; d_i++) {
805 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
808 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
809 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
814 error = got_path_dirent_type(&type, srv->repos_path,
818 if (type != DT_DIR) {
823 if (qs->index_page > 0 && (qs->index_page *
824 srv->max_repos_display) > t->prev_disp) {
829 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
833 error = gotweb_load_got_path(c, repo_dir);
834 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
836 gotweb_free_repo_dir(repo_dir);
841 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
847 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
850 gotweb_free_repo_dir(repo_dir);
853 if (d_disp == srv->max_repos_display)
856 t->repos_total = d_cnt - d_skipped;
858 if (srv->max_repos_display == 0)
860 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
862 if (t->repos_total <= srv->max_repos ||
863 t->repos_total <= srv->max_repos_display)
866 if (gotweb_render_navs(c->tp) == -1)
870 for (d_i = 0; d_i < d_cnt; d_i++)
874 if (d != NULL && closedir(d) == EOF && error == NULL)
875 error = got_error_from_errno("closedir");
879 static const struct got_error *
880 gotweb_render_blame(struct request *c)
882 const struct got_error *error = NULL;
883 struct transport *t = c->t;
884 struct repo_commit *rc = NULL;
885 char *age = NULL, *msg = NULL;
888 error = got_get_repo_commits(c, 1);
892 rc = TAILQ_FIRST(&t->repo_commits);
894 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
897 error = gotweb_escape_html(&msg, rc->commit_msg);
901 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
902 "<div id='blame_title'>Blame</div>\n"
903 "</div>\n" /* #blame_title_wrapper */
904 "<div id='blame_content'>\n"
905 "<div id='blame_header_wrapper'>\n"
906 "<div id='blame_header'>\n"
907 "<div class='header_age_title'>Date:</div>\n"
908 "<div class='header_age'>%s</div>\n"
909 "<div id='header_commit_msg_title'>Message:</div>\n"
910 "<div id='header_commit_msg'>%s</div>\n"
911 "</div>\n" /* #blame_header */
912 "</div>\n" /* #blame_header_wrapper */
913 "<div class='dotted_line'></div>\n"
914 "<div id='blame'>\n",
920 error = got_output_file_blame(c);
924 fcgi_printf(c, "</div>\n" /* #blame */
925 "</div>\n"); /* #blame_content */
932 static const struct got_error *
933 gotweb_render_branches(struct request *c)
935 const struct got_error *error = NULL;
936 struct got_reflist_head refs;
937 struct got_reflist_entry *re;
938 struct transport *t = c->t;
939 struct querystring *qs = t->qs;
940 struct got_repository *repo = t->repo;
941 char *escaped_refname = NULL;
947 error = got_ref_list(&refs, repo, "refs/heads",
948 got_ref_cmp_by_name, NULL);
952 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
953 "<div id='branches_title'>Branches</div>\n"
954 "</div>\n" /* #branches_title_wrapper */
955 "<div id='branches_content'>\n");
959 TAILQ_FOREACH(re, &refs, entry) {
960 const char *refname = NULL;
962 if (got_ref_is_symbolic(re->ref))
965 refname = got_ref_get_name(re->ref);
966 if (refname == NULL) {
967 error = got_error_from_errno("strdup");
970 if (strncmp(refname, "refs/heads/", 11) != 0)
973 error = got_get_repo_age(&age, c, refname, TM_DIFF);
977 if (strncmp(refname, "refs/heads/", 11) == 0)
979 error = gotweb_escape_html(&escaped_refname, refname);
983 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
984 "<div class='branches_age'>%s</div>\n"
985 "<div class='branches_space'> </div>\n"
986 "<div class='branch'>", age);
990 r = gotweb_link(c, &(struct gotweb_url){
996 }, "%s", escaped_refname);
1000 if (fcgi_printf(c, "</div>\n" /* .branch */
1001 "<div class='navs_wrapper'>\n"
1002 "<div class='navs'>") == -1)
1005 r = gotweb_link(c, &(struct gotweb_url){
1015 if (fcgi_printf(c, " | ") == -1)
1018 r = gotweb_link(c, &(struct gotweb_url){
1024 }, "commit briefs");
1028 if (fcgi_printf(c, " | ") == -1)
1031 r = gotweb_link(c, &(struct gotweb_url){
1041 r = fcgi_printf(c, "</div>\n" /* .navs */
1042 "</div>\n" /* .navs_wrapper */
1043 "<div class='dotted_line'></div>\n"
1044 "</div>\n"); /* .branches_wrapper */
1050 free(escaped_refname);
1051 escaped_refname = NULL;
1053 fcgi_printf(c, "</div>\n"); /* #branches_content */
1056 free(escaped_refname);
1057 got_ref_list_free(&refs);
1061 static const struct got_error *
1062 gotweb_render_tree(struct request *c)
1064 const struct got_error *error = NULL;
1065 struct transport *t = c->t;
1066 struct repo_commit *rc = NULL;
1067 char *age = NULL, *msg = NULL;
1070 error = got_get_repo_commits(c, 1);
1074 rc = TAILQ_FIRST(&t->repo_commits);
1076 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1080 error = gotweb_escape_html(&msg, rc->commit_msg);
1084 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1085 "<div id='tree_title'>Tree</div>\n"
1086 "</div>\n" /* #tree_title_wrapper */
1087 "<div id='tree_content'>\n"
1088 "<div id='tree_header_wrapper'>\n"
1089 "<div id='tree_header'>\n"
1090 "<div id='header_tree_title'>Tree:</div>\n"
1091 "<div id='header_tree'>%s</div>\n"
1092 "<div class='header_age_title'>Date:</div>\n"
1093 "<div class='header_age'>%s</div>\n"
1094 "<div id='header_commit_msg_title'>Message:</div>\n"
1095 "<div id='header_commit_msg'>%s</div>\n"
1096 "</div>\n" /* #tree_header */
1097 "</div>\n" /* #tree_header_wrapper */
1098 "<div class='dotted_line'></div>\n"
1099 "<div id='tree'>\n",
1106 error = got_output_repo_tree(c);
1110 fcgi_printf(c, "</div>\n"); /* #tree */
1111 fcgi_printf(c, "</div>\n"); /* #tree_content */
1118 static const struct got_error *
1119 gotweb_render_diff(struct request *c)
1121 const struct got_error *error = NULL;
1122 struct transport *t = c->t;
1123 struct repo_commit *rc = NULL;
1124 char *age = NULL, *author = NULL, *msg = NULL;
1127 error = got_get_repo_commits(c, 1);
1131 rc = TAILQ_FIRST(&t->repo_commits);
1133 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1136 error = gotweb_escape_html(&author, rc->author);
1139 error = gotweb_escape_html(&msg, rc->commit_msg);
1143 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1144 "<div id='diff_title'>Commit Diff</div>\n"
1145 "</div>\n" /* #diff_title_wrapper */
1146 "<div id='diff_content'>\n"
1147 "<div id='diff_header_wrapper'>\n"
1148 "<div id='diff_header'>\n"
1149 "<div id='header_diff_title'>Diff:</div>\n"
1150 "<div id='header_diff'>%s<br />%s</div>\n"
1151 "<div class='header_commit_title'>Commit:</div>\n"
1152 "<div class='header_commit'>%s</div>\n"
1153 "<div id='header_tree_title'>Tree:</div>\n"
1154 "<div id='header_tree'>%s</div>\n"
1155 "<div class='header_author_title'>Author:</div>\n"
1156 "<div class='header_author'>%s</div>\n"
1157 "<div class='header_age_title'>Date:</div>\n"
1158 "<div class='header_age'>%s</div>\n"
1159 "<div id='header_commit_msg_title'>Message:</div>\n"
1160 "<div id='header_commit_msg'>%s</div>\n"
1161 "</div>\n" /* #diff_header */
1162 "</div>\n" /* #diff_header_wrapper */
1163 "<div class='dotted_line'></div>\n"
1164 "<div id='diff'>\n",
1165 rc->parent_id, rc->commit_id,
1174 error = got_output_repo_diff(c);
1178 fcgi_printf(c, "</div>\n"); /* #diff */
1179 fcgi_printf(c, "</div>\n"); /* #diff_content */
1187 static const struct got_error *
1188 gotweb_render_summary(struct request *c)
1190 const struct got_error *error = NULL;
1191 struct transport *t = c->t;
1192 struct server *srv = c->srv;
1195 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1198 if (srv->show_repo_description) {
1200 "<div id='description_title'>Description:</div>\n"
1201 "<div id='description'>%s</div>\n",
1202 t->repo_dir->description ? t->repo_dir->description : "");
1207 if (srv->show_repo_owner) {
1209 "<div id='repo_owner_title'>Owner:</div>\n"
1210 "<div id='repo_owner'>%s</div>\n",
1211 t->repo_dir->owner ? t->repo_dir->owner : "");
1216 if (srv->show_repo_age) {
1218 "<div id='last_change_title'>Last Change:</div>\n"
1219 "<div id='last_change'>%s</div>\n",
1225 if (srv->show_repo_cloneurl) {
1227 "<div id='cloneurl_title'>Clone URL:</div>\n"
1228 "<div id='cloneurl'>%s</div>\n",
1229 t->repo_dir->url ? t->repo_dir->url : "");
1234 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1238 if (gotweb_render_briefs(c->tp) == -1)
1241 error = gotweb_render_tags(c);
1243 log_warnx("%s: %s", __func__, error->msg);
1247 error = gotweb_render_branches(c);
1249 log_warnx("%s: %s", __func__, error->msg);
1254 static const struct got_error *
1255 gotweb_render_tag(struct request *c)
1257 const struct got_error *error = NULL;
1258 struct repo_tag *rt = NULL;
1259 struct transport *t = c->t;
1260 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1262 error = got_get_repo_tags(c, 1);
1266 if (t->tag_count == 0) {
1267 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1272 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1274 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1277 error = gotweb_escape_html(&author, rt->tagger);
1280 error = gotweb_escape_html(&msg, rt->commit_msg);
1284 tagname = rt->tag_name;
1285 if (strncmp(tagname, "refs/", 5) == 0)
1287 error = gotweb_escape_html(&tagname, tagname);
1291 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1292 "<div id='tags_title'>Tag</div>\n"
1293 "</div>\n" /* #tags_title_wrapper */
1294 "<div id='tags_content'>\n"
1295 "<div id='tag_header_wrapper'>\n"
1296 "<div id='tag_header'>\n"
1297 "<div class='header_commit_title'>Commit:</div>\n"
1298 "<div class='header_commit'>%s"
1299 " <span class='refs_str'>(%s)</span></div>\n"
1300 "<div class='header_author_title'>Tagger:</div>\n"
1301 "<div class='header_author'>%s</div>\n"
1302 "<div class='header_age_title'>Date:</div>\n"
1303 "<div class='header_age'>%s</div>\n"
1304 "<div id='header_commit_msg_title'>Message:</div>\n"
1305 "<div id='header_commit_msg'>%s</div>\n"
1306 "</div>\n" /* #tag_header */
1307 "<div class='dotted_line'></div>\n"
1308 "<div id='tag_commit'>\n%s</div>"
1309 "</div>" /* #tag_header_wrapper */
1310 "</div>", /* #tags_content */
1325 static const struct got_error *
1326 gotweb_render_tags(struct request *c)
1328 const struct got_error *error = NULL;
1329 struct repo_tag *rt = NULL;
1330 struct server *srv = c->srv;
1331 struct transport *t = c->t;
1332 struct querystring *qs = t->qs;
1333 struct repo_dir *repo_dir = t->repo_dir;
1334 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1335 int r, commit_found = 0;
1337 if (qs->action == BRIEFS) {
1339 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1341 error = got_get_repo_tags(c, srv->max_commits_display);
1345 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1346 "<div id='tags_title'>Tags</div>\n"
1347 "</div>\n" /* #tags_title_wrapper */
1348 "<div id='tags_content'>\n");
1352 if (t->tag_count == 0) {
1353 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1354 "This repository contains no tags");
1359 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1360 if (commit_found == 0 && qs->commit != NULL) {
1361 if (strcmp(qs->commit, rt->commit_id) != 0)
1366 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1370 tagname = rt->tag_name;
1371 if (strncmp(tagname, "refs/tags/", 10) == 0)
1373 error = gotweb_escape_html(&tagname, tagname);
1377 if (rt->tag_commit != NULL) {
1378 newline = strchr(rt->tag_commit, '\n');
1381 error = gotweb_escape_html(&msg, rt->tag_commit);
1386 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1387 "<div class='tag'>%s</div>\n"
1388 "<div class='tag_log'>", age, tagname) == -1)
1391 r = gotweb_link(c, &(struct gotweb_url){
1395 .path = repo_dir->name,
1396 .commit = rt->commit_id,
1397 }, "%s", msg ? msg : "");
1401 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1402 "<div class='navs_wrapper'>\n"
1403 "<div class='navs'>") == -1)
1406 r = gotweb_link(c, &(struct gotweb_url){
1410 .path = repo_dir->name,
1411 .commit = rt->commit_id,
1416 if (fcgi_printf(c, " | ") == -1)
1419 r = gotweb_link(c, &(struct gotweb_url){
1423 .path = repo_dir->name,
1424 .commit = rt->commit_id,
1425 }, "commit briefs");
1429 if (fcgi_printf(c, " | ") == -1)
1432 r = gotweb_link(c, &(struct gotweb_url){
1436 .path = repo_dir->name,
1437 .commit = rt->commit_id,
1443 "</div>\n" /* .navs */
1444 "</div>\n" /* .navs_wrapper */
1445 "<div class='dotted_line'></div>\n");
1456 if (t->next_id || t->prev_id) {
1457 if (gotweb_render_navs(c->tp) == -1)
1460 fcgi_printf(c, "</div>\n"); /* #tags_content */
1468 const struct got_error *
1469 gotweb_escape_html(char **escaped_html, const char *orig_html)
1471 const struct got_error *error = NULL;
1472 struct escape_pair {
1483 size_t orig_len, len;
1486 orig_len = strlen(orig_html);
1488 for (i = 0; i < orig_len; i++) {
1489 for (j = 0; j < nitems(esc); j++) {
1490 if (orig_html[i] != esc[j].c)
1492 len += strlen(esc[j].s) - 1 /* escaped char */;
1496 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1497 if (*escaped_html == NULL)
1498 return got_error_from_errno("calloc");
1501 for (i = 0; i < orig_len; i++) {
1503 for (j = 0; j < nitems(esc); j++) {
1504 if (orig_html[i] != esc[j].c)
1507 if (strlcat(*escaped_html, esc[j].s, len + 1)
1509 error = got_error(GOT_ERR_NO_SPACE);
1512 x += strlen(esc[j].s);
1517 (*escaped_html)[x] = orig_html[i];
1523 free(*escaped_html);
1524 *escaped_html = NULL;
1526 (*escaped_html)[x] = '\0';
1533 should_urlencode(int c)
1535 if (c <= ' ' || c >= 127)
1566 gotweb_urlencode(const char *str)
1574 for (s = str; *s; ++s) {
1576 if (should_urlencode(*s))
1580 escaped = calloc(1, len + 1);
1581 if (escaped == NULL)
1585 for (s = str; *s; ++s) {
1586 if (should_urlencode(*s)) {
1587 a = (*s & 0xF0) >> 4;
1591 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1592 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1601 gotweb_action_name(int action)
1632 gotweb_render_url(struct request *c, struct gotweb_url *url)
1634 const char *sep = "?", *action;
1638 action = gotweb_action_name(url->action);
1639 if (action != NULL) {
1640 if (fcgi_printf(c, "?action=%s", action) == -1)
1646 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1652 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1658 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1664 tmp = gotweb_urlencode(url->file);
1667 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1675 tmp = gotweb_urlencode(url->folder);
1678 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1686 tmp = gotweb_urlencode(url->headref);
1689 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1696 if (url->index_page != -1) {
1697 if (fcgi_printf(c, "%sindex_page=%d", sep,
1698 url->index_page) == -1)
1704 tmp = gotweb_urlencode(url->path);
1707 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1714 if (url->page != -1) {
1715 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1724 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1729 if (fcgi_printf(c, "<a href='") == -1)
1732 if (gotweb_render_url(c, url) == -1)
1735 if (fcgi_printf(c, "'>") == -1)
1739 r = fcgi_vprintf(c, fmt, ap);
1744 if (fcgi_printf(c, "</a>"))
1749 static struct got_repository *
1750 find_cached_repo(struct server *srv, const char *path)
1754 for (i = 0; i < srv->ncached_repos; i++) {
1755 if (strcmp(srv->cached_repos[i].path, path) == 0)
1756 return srv->cached_repos[i].repo;
1762 static const struct got_error *
1763 cache_repo(struct got_repository **new, struct server *srv,
1764 struct repo_dir *repo_dir, struct socket *sock)
1766 const struct got_error *error = NULL;
1767 struct got_repository *repo;
1768 struct cached_repo *cr;
1771 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1772 cr = &srv->cached_repos[srv->ncached_repos - 1];
1773 error = got_repo_close(cr->repo);
1774 memset(cr, 0, sizeof(*cr));
1775 srv->ncached_repos--;
1778 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1779 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1780 cr = &srv->cached_repos[0];
1783 cr = &srv->cached_repos[srv->ncached_repos];
1786 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1789 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1790 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1795 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1796 >= sizeof(cr->path)) {
1798 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1799 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1801 return got_error(GOT_ERR_NO_SPACE);
1805 srv->ncached_repos++;
1810 static const struct got_error *
1811 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1813 const struct got_error *error = NULL;
1814 struct socket *sock = c->sock;
1815 struct server *srv = c->srv;
1816 struct transport *t = c->t;
1817 struct got_repository *repo = NULL;
1821 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1822 GOTWEB_GIT_DIR) == -1)
1823 return got_error_from_errno("asprintf");
1825 dt = opendir(dir_test);
1829 repo_dir->path = dir_test;
1834 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1835 repo_dir->name) == -1)
1836 return got_error_from_errno("asprintf");
1838 dt = opendir(dir_test);
1840 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1843 repo_dir->path = dir_test;
1848 if (srv->respect_exportok &&
1849 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1850 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1854 repo = find_cached_repo(srv, repo_dir->path);
1856 error = cache_repo(&repo, srv, repo_dir, sock);
1861 error = gotweb_get_repo_description(&repo_dir->description, srv,
1862 repo_dir->path, dirfd(dt));
1865 error = got_get_repo_owner(&repo_dir->owner, c);
1868 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1871 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1875 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1876 error = got_error_from_errno("closedir");
1880 static const struct got_error *
1881 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1883 const struct got_error *error;
1885 *repo_dir = calloc(1, sizeof(**repo_dir));
1886 if (*repo_dir == NULL)
1887 return got_error_from_errno("calloc");
1889 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1890 error = got_error_from_errno("asprintf");
1895 (*repo_dir)->owner = NULL;
1896 (*repo_dir)->description = NULL;
1897 (*repo_dir)->url = NULL;
1898 (*repo_dir)->age = NULL;
1899 (*repo_dir)->path = NULL;
1904 static const struct got_error *
1905 gotweb_get_repo_description(char **description, struct server *srv,
1906 const char *dirpath, int dir)
1908 const struct got_error *error = NULL;
1913 *description = NULL;
1914 if (srv->show_repo_description == 0)
1917 fd = openat(dir, "description", O_RDONLY);
1919 if (errno != ENOENT && errno != EACCES) {
1920 error = got_error_from_errno_fmt("openat %s/%s",
1921 dirpath, "description");
1926 if (fstat(fd, &sb) == -1) {
1927 error = got_error_from_errno_fmt("fstat %s/%s",
1928 dirpath, "description");
1933 if (len > GOTWEBD_MAXDESCRSZ - 1)
1934 len = GOTWEBD_MAXDESCRSZ - 1;
1936 *description = calloc(len + 1, sizeof(**description));
1937 if (*description == NULL) {
1938 error = got_error_from_errno("calloc");
1942 if (read(fd, *description, len) == -1)
1943 error = got_error_from_errno("read");
1945 if (fd != -1 && close(fd) == -1 && error == NULL)
1946 error = got_error_from_errno("close");
1950 static const struct got_error *
1951 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1954 const struct got_error *error = NULL;
1960 if (srv->show_repo_cloneurl == 0)
1963 fd = openat(dir, "cloneurl", O_RDONLY);
1965 if (errno != ENOENT && errno != EACCES) {
1966 error = got_error_from_errno_fmt("openat %s/%s",
1967 dirpath, "cloneurl");
1972 if (fstat(fd, &sb) == -1) {
1973 error = got_error_from_errno_fmt("fstat %s/%s",
1974 dirpath, "cloneurl");
1979 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1980 len = GOTWEBD_MAXCLONEURLSZ - 1;
1982 *url = calloc(len + 1, sizeof(**url));
1984 error = got_error_from_errno("calloc");
1988 if (read(fd, *url, len) == -1)
1989 error = got_error_from_errno("read");
1991 if (fd != -1 && close(fd) == -1 && error == NULL)
1992 error = got_error_from_errno("close");
1996 const struct got_error *
1997 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2000 long long diff_time;
2001 const char *years = "years ago", *months = "months ago";
2002 const char *weeks = "weeks ago", *days = "days ago";
2003 const char *hours = "hours ago", *minutes = "minutes ago";
2004 const char *seconds = "seconds ago", *now = "right now";
2012 diff_time = time(NULL) - committer_time;
2013 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2014 if (asprintf(repo_age, "%lld %s",
2015 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2016 return got_error_from_errno("asprintf");
2017 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2018 if (asprintf(repo_age, "%lld %s",
2019 (diff_time / 60 / 60 / 24 / (365 / 12)),
2021 return got_error_from_errno("asprintf");
2022 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2023 if (asprintf(repo_age, "%lld %s",
2024 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2025 return got_error_from_errno("asprintf");
2026 } else if (diff_time > 60 * 60 * 24 * 2) {
2027 if (asprintf(repo_age, "%lld %s",
2028 (diff_time / 60 / 60 / 24), days) == -1)
2029 return got_error_from_errno("asprintf");
2030 } else if (diff_time > 60 * 60 * 2) {
2031 if (asprintf(repo_age, "%lld %s",
2032 (diff_time / 60 / 60), hours) == -1)
2033 return got_error_from_errno("asprintf");
2034 } else if (diff_time > 60 * 2) {
2035 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2037 return got_error_from_errno("asprintf");
2038 } else if (diff_time > 2) {
2039 if (asprintf(repo_age, "%lld %s", diff_time,
2041 return got_error_from_errno("asprintf");
2043 if (asprintf(repo_age, "%s", now) == -1)
2044 return got_error_from_errno("asprintf");
2048 if (gmtime_r(&committer_time, &tm) == NULL)
2049 return got_error_from_errno("gmtime_r");
2051 s = asctime_r(&tm, datebuf);
2053 return got_error_from_errno("asctime_r");
2055 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2056 return got_error_from_errno("asprintf");