2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "got_compat.h"
56 static const struct querystring_keys querystring_keys[] = {
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
67 static const struct action_keys action_keys[] = {
70 { "blobraw", BLOBRAW },
72 { "commits", COMMITS },
76 { "summary", SUMMARY },
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
91 static const struct got_error *gotweb_load_got_path(struct request *c,
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
97 static const struct got_error *gotweb_render_blame(struct request *);
98 static const struct got_error *gotweb_render_diff(struct request *);
99 static const struct got_error *gotweb_render_summary(struct request *);
100 static const struct got_error *gotweb_render_tags(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 got_blob_object *blob = 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: ";
117 int r, html = 0, fd = -1;
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->action == BLAME || qs->action == BLOB ||
155 qs->action == BLOBRAW || qs->action == DIFF) {
156 if (qs->commit == NULL) {
157 error2 = got_error(GOT_ERR_QUERYSTRING);
162 if (qs->action != INDEX) {
163 error = gotweb_init_repo_dir(&repo_dir, qs->path);
166 error = gotweb_load_got_path(c, repo_dir);
167 c->t->repo_dir = repo_dir;
168 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
172 if (qs->action == BLOBRAW) {
173 error = got_get_repo_commits(c, 1);
176 error = got_output_file_blob(c);
178 log_warnx("%s: %s", __func__, error->msg);
184 if (qs->action == BLOB) {
186 struct gotweb_url url = {
191 .commit = qs->commit,
192 .folder = qs->folder,
196 error = got_get_repo_commits(c, 1);
200 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
204 fcgi_puts(c->tp, "Status: 302\r\n");
205 fcgi_puts(c->tp, "Location: ");
206 gotweb_render_url(c, &url);
207 fcgi_puts(c->tp, "\r\n\r\n");
212 if (qs->action == RSS) {
213 error = gotweb_render_content_type_file(c,
214 "application/rss+xml;charset=utf-8",
215 repo_dir->name, ".rss");
217 log_warnx("%s: %s", __func__, error->msg);
221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
223 log_warnx("%s: %s", __func__, error->msg);
226 if (gotweb_render_rss(c->tp) == -1)
232 error = gotweb_render_content_type(c, "text/html");
234 log_warnx("%s: %s", __func__, error->msg);
239 if (gotweb_render_header(c->tp) == -1)
249 error = gotweb_render_blame(c);
251 log_warnx("%s: %s", __func__, error->msg);
256 if (gotweb_render_blob(c->tp, blob) == -1)
260 if (gotweb_render_briefs(c->tp) == -1)
264 error = got_get_repo_commits(c, srv->max_commits_display);
266 log_warnx("%s: %s", __func__, error->msg);
269 if (gotweb_render_commits(c->tp) == -1)
273 error = gotweb_render_diff(c);
275 log_warnx("%s: %s", __func__, error->msg);
280 error = gotweb_render_index(c);
282 log_warnx("%s: %s", __func__, error->msg);
287 error = gotweb_render_summary(c);
289 log_warnx("%s: %s", __func__, error->msg);
294 error = got_get_repo_tags(c, 1);
296 log_warnx("%s: %s", __func__, error->msg);
299 if (c->t->tag_count == 0) {
300 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
304 if (gotweb_render_tag(c->tp) == -1)
308 error = gotweb_render_tags(c);
310 log_warnx("%s: %s", __func__, error->msg);
315 error = got_get_repo_commits(c, 1);
317 log_warnx("%s: %s", __func__, error->msg);
320 if (gotweb_render_tree(c->tp) == -1)
325 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
326 "Erorr: Bad Querystring");
334 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
336 if (fcgi_printf(c, "\n%s", err) == -1)
339 if (fcgi_printf(c, "%s", error->msg) == -1)
342 if (fcgi_printf(c, "see daemon logs for details") == -1)
345 if (html && fcgi_printf(c, "</div>\n") == -1)
349 got_object_blob_close(blob);
352 if (html && srv != NULL)
353 gotweb_render_footer(c->tp);
357 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
359 struct server *srv = NULL;
361 /* check against the server name first */
362 if (strlen(server_name) > 0)
363 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
364 if (strcmp(srv->name, server_name) == 0)
367 /* check against subdomain second */
368 if (strlen(subdomain) > 0)
369 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
370 if (strcmp(srv->name, subdomain) == 0)
373 /* if those fail, send first server */
374 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
381 const struct got_error *
382 gotweb_init_transport(struct transport **t)
384 const struct got_error *error = NULL;
386 *t = calloc(1, sizeof(**t));
388 return got_error_from_errno2("%s: calloc", __func__);
390 TAILQ_INIT(&(*t)->repo_commits);
391 TAILQ_INIT(&(*t)->repo_tags);
394 (*t)->repo_dir = NULL;
396 (*t)->next_id = NULL;
397 (*t)->prev_id = NULL;
404 static const struct got_error *
405 gotweb_init_querystring(struct querystring **qs)
407 const struct got_error *error = NULL;
409 *qs = calloc(1, sizeof(**qs));
411 return got_error_from_errno2("%s: calloc", __func__);
413 (*qs)->headref = strdup("HEAD");
414 if ((*qs)->headref == NULL) {
417 return got_error_from_errno2("%s: strdup", __func__);
420 (*qs)->action = INDEX;
421 (*qs)->commit = NULL;
423 (*qs)->folder = NULL;
424 (*qs)->index_page = 0;
430 static const struct got_error *
431 gotweb_parse_querystring(struct querystring **qs, char *qst)
433 const struct got_error *error = NULL;
434 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
435 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
442 return got_error_from_errno2("%s: strdup", __func__);
447 while (tok1_pair != NULL) {
448 strsep(&tok1_end, "&");
450 tok2 = strdup(tok1_pair);
453 return got_error_from_errno2("%s: strdup", __func__);
459 while (tok2_pair != NULL) {
460 strsep(&tok2_end, "=");
462 error = gotweb_assign_querystring(qs, tok2_pair,
467 tok2_pair = tok2_end;
470 tok1_pair = tok1_end;
481 * Adapted from usr.sbin/httpd/httpd.c url_decode.
483 static const struct got_error *
484 gotweb_urldecode(char *url)
496 /* Encoding character is followed by two hex chars */
497 if (!isxdigit((unsigned char)p[1]) ||
498 !isxdigit((unsigned char)p[2]) ||
499 (p[1] == '0' && p[2] == '0'))
500 return got_error(GOT_ERR_BAD_QUERYSTRING);
506 * We don't have to validate "hex" because it is
507 * guaranteed to include two hex chars followed by nul.
509 x = strtoul(hex, NULL, 16);
525 static const struct got_error *
526 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
528 const struct got_error *error = NULL;
532 error = gotweb_urldecode(value);
536 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
537 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
540 switch (querystring_keys[el_cnt].element) {
542 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
543 if (strcmp(value, action_keys[a_cnt].name) != 0)
545 else if (strcmp(value,
546 action_keys[a_cnt].name) == 0){
548 action_keys[a_cnt].action;
556 (*qs)->commit = strdup(value);
557 if ((*qs)->commit == NULL) {
558 error = got_error_from_errno2("%s: strdup",
564 (*qs)->file = strdup(value);
565 if ((*qs)->file == NULL) {
566 error = got_error_from_errno2("%s: strdup",
572 (*qs)->folder = strdup(value);
573 if ((*qs)->folder == NULL) {
574 error = got_error_from_errno2("%s: strdup",
580 free((*qs)->headref);
581 (*qs)->headref = strdup(value);
582 if ((*qs)->headref == NULL) {
583 error = got_error_from_errno2("%s: strdup",
589 if (strlen(value) == 0)
591 (*qs)->index_page = strtonum(value, INT64_MIN,
594 error = got_error_from_errno3("%s: strtonum %s",
598 if ((*qs)->index_page < 0)
599 (*qs)->index_page = 0;
602 (*qs)->path = strdup(value);
603 if ((*qs)->path == NULL) {
604 error = got_error_from_errno2("%s: strdup",
610 if (strlen(value) == 0)
612 (*qs)->page = strtonum(value, INT64_MIN,
615 error = got_error_from_errno3("%s: strtonum %s",
631 gotweb_free_repo_tag(struct repo_tag *rt)
636 free(rt->tag_commit);
637 free(rt->commit_msg);
644 gotweb_free_repo_commit(struct repo_commit *rc)
654 free(rc->commit_msg);
660 gotweb_free_querystring(struct querystring *qs)
673 gotweb_free_repo_dir(struct repo_dir *repo_dir)
675 if (repo_dir != NULL) {
676 free(repo_dir->name);
677 free(repo_dir->owner);
678 free(repo_dir->description);
681 free(repo_dir->path);
687 gotweb_free_transport(struct transport *t)
689 struct repo_commit *rc = NULL, *trc = NULL;
690 struct repo_tag *rt = NULL, *trt = NULL;
692 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
693 TAILQ_REMOVE(&t->repo_commits, rc, entry);
694 gotweb_free_repo_commit(rc);
696 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
697 TAILQ_REMOVE(&t->repo_tags, rt, entry);
698 gotweb_free_repo_tag(rt);
700 gotweb_free_repo_dir(t->repo_dir);
701 gotweb_free_querystring(t->qs);
707 const struct got_error *
708 gotweb_render_content_type(struct request *c, const char *type)
710 const char *csp = "default-src 'self'; script-src 'none'; "
711 "object-src 'none';";
714 "Content-Security-Policy: %s\r\n"
715 "Content-Type: %s\r\n\r\n",
720 const struct got_error *
721 gotweb_render_content_type_file(struct request *c, const char *type,
722 const char *file, const char *suffix)
724 fcgi_printf(c, "Content-type: %s\r\n"
725 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
726 type, file, suffix ? suffix : "");
731 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
732 struct gotweb_url *next, int *have_next)
734 struct transport *t = c->t;
735 struct querystring *qs = t->qs;
736 struct server *srv = c->srv;
738 *have_prev = *have_next = 0;
742 if (qs->index_page > 0) {
744 *prev = (struct gotweb_url){
746 .index_page = qs->index_page - 1,
750 if (t->next_disp == srv->max_repos_display &&
751 t->repos_total != (qs->index_page + 1) *
752 srv->max_repos_display) {
754 *next = (struct gotweb_url){
756 .index_page = qs->index_page + 1,
762 if (t->prev_id && qs->commit != NULL &&
763 strcmp(qs->commit, t->prev_id) != 0) {
765 *prev = (struct gotweb_url){
768 .page = qs->page - 1,
770 .commit = t->prev_id,
771 .headref = qs->headref,
776 *next = (struct gotweb_url){
779 .page = qs->page + 1,
781 .commit = t->next_id,
782 .headref = qs->headref,
787 if (t->prev_id && qs->commit != NULL &&
788 strcmp(qs->commit, t->prev_id) != 0) {
790 *prev = (struct gotweb_url){
793 .page = qs->page - 1,
795 .commit = t->prev_id,
796 .headref = qs->headref,
797 .folder = qs->folder,
803 *next = (struct gotweb_url){
806 .page = qs->page + 1,
808 .commit = t->next_id,
809 .headref = qs->headref,
810 .folder = qs->folder,
816 if (t->prev_id && qs->commit != NULL &&
817 strcmp(qs->commit, t->prev_id) != 0) {
819 *prev = (struct gotweb_url){
822 .page = qs->page - 1,
824 .commit = t->prev_id,
825 .headref = qs->headref,
830 *next = (struct gotweb_url){
833 .page = qs->page + 1,
835 .commit = t->next_id,
836 .headref = qs->headref,
843 static const struct got_error *
844 gotweb_render_index(struct request *c)
846 const struct got_error *error = NULL;
847 struct server *srv = c->srv;
848 struct transport *t = c->t;
849 struct querystring *qs = t->qs;
850 struct repo_dir *repo_dir = NULL;
852 struct dirent **sd_dent = NULL;
853 unsigned int d_cnt, d_i, d_disp = 0;
854 unsigned int d_skipped = 0;
857 d = opendir(srv->repos_path);
859 error = got_error_from_errno2("opendir", srv->repos_path);
863 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
866 error = got_error_from_errno2("scandir", srv->repos_path);
870 if (gotweb_render_repo_table_hdr(c->tp) == -1)
873 for (d_i = 0; d_i < d_cnt; d_i++) {
874 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
877 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
878 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
883 error = got_path_dirent_type(&type, srv->repos_path,
887 if (type != DT_DIR) {
892 if (qs->index_page > 0 && (qs->index_page *
893 srv->max_repos_display) > t->prev_disp) {
898 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
902 error = gotweb_load_got_path(c, repo_dir);
903 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
905 gotweb_free_repo_dir(repo_dir);
910 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
916 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
919 gotweb_free_repo_dir(repo_dir);
922 if (d_disp == srv->max_repos_display)
925 t->repos_total = d_cnt - d_skipped;
927 if (srv->max_repos_display == 0)
929 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
931 if (t->repos_total <= srv->max_repos ||
932 t->repos_total <= srv->max_repos_display)
935 if (gotweb_render_navs(c->tp) == -1)
939 for (d_i = 0; d_i < d_cnt; d_i++)
943 if (d != NULL && closedir(d) == EOF && error == NULL)
944 error = got_error_from_errno("closedir");
948 static const struct got_error *
949 gotweb_render_blame(struct request *c)
951 const struct got_error *error = NULL;
952 struct transport *t = c->t;
953 struct repo_commit *rc = NULL;
954 char *age = NULL, *msg = NULL;
957 error = got_get_repo_commits(c, 1);
961 rc = TAILQ_FIRST(&t->repo_commits);
963 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
966 error = gotweb_escape_html(&msg, rc->commit_msg);
970 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
971 "<div id='blame_title'>Blame</div>\n"
972 "</div>\n" /* #blame_title_wrapper */
973 "<div id='blame_content'>\n"
974 "<div id='blame_header_wrapper'>\n"
975 "<div id='blame_header'>\n"
976 "<div class='header_age_title'>Date:</div>\n"
977 "<div class='header_age'>%s</div>\n"
978 "<div id='header_commit_msg_title'>Message:</div>\n"
979 "<div id='header_commit_msg'>%s</div>\n"
980 "</div>\n" /* #blame_header */
981 "</div>\n" /* #blame_header_wrapper */
982 "<div class='dotted_line'></div>\n"
983 "<div id='blame'>\n",
989 error = got_output_file_blame(c);
993 fcgi_printf(c, "</div>\n" /* #blame */
994 "</div>\n"); /* #blame_content */
1001 static const struct got_error *
1002 gotweb_render_branches(struct request *c)
1004 const struct got_error *error = NULL;
1005 struct got_reflist_head refs;
1006 struct got_reflist_entry *re;
1007 struct transport *t = c->t;
1008 struct querystring *qs = t->qs;
1009 struct got_repository *repo = t->repo;
1010 char *escaped_refname = NULL;
1016 error = got_ref_list(&refs, repo, "refs/heads",
1017 got_ref_cmp_by_name, NULL);
1021 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1022 "<div id='branches_title'>Branches</div>\n"
1023 "</div>\n" /* #branches_title_wrapper */
1024 "<div id='branches_content'>\n");
1028 TAILQ_FOREACH(re, &refs, entry) {
1029 const char *refname = NULL;
1031 if (got_ref_is_symbolic(re->ref))
1034 refname = got_ref_get_name(re->ref);
1035 if (refname == NULL) {
1036 error = got_error_from_errno("strdup");
1039 if (strncmp(refname, "refs/heads/", 11) != 0)
1042 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1046 if (strncmp(refname, "refs/heads/", 11) == 0)
1048 error = gotweb_escape_html(&escaped_refname, refname);
1052 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1053 "<div class='branches_age'>%s</div>\n"
1054 "<div class='branches_space'> </div>\n"
1055 "<div class='branch'>", age);
1059 r = gotweb_link(c, &(struct gotweb_url){
1065 }, "%s", escaped_refname);
1069 if (fcgi_printf(c, "</div>\n" /* .branch */
1070 "<div class='navs_wrapper'>\n"
1071 "<div class='navs'>") == -1)
1074 r = gotweb_link(c, &(struct gotweb_url){
1084 if (fcgi_printf(c, " | ") == -1)
1087 r = gotweb_link(c, &(struct gotweb_url){
1093 }, "commit briefs");
1097 if (fcgi_printf(c, " | ") == -1)
1100 r = gotweb_link(c, &(struct gotweb_url){
1110 r = fcgi_printf(c, "</div>\n" /* .navs */
1111 "</div>\n" /* .navs_wrapper */
1112 "<div class='dotted_line'></div>\n"
1113 "</div>\n"); /* .branches_wrapper */
1119 free(escaped_refname);
1120 escaped_refname = NULL;
1122 fcgi_printf(c, "</div>\n"); /* #branches_content */
1125 free(escaped_refname);
1126 got_ref_list_free(&refs);
1130 static const struct got_error *
1131 gotweb_render_diff(struct request *c)
1133 const struct got_error *error = NULL;
1134 struct transport *t = c->t;
1135 struct repo_commit *rc = NULL;
1136 char *age = NULL, *author = NULL, *msg = NULL;
1139 error = got_get_repo_commits(c, 1);
1143 rc = TAILQ_FIRST(&t->repo_commits);
1145 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1148 error = gotweb_escape_html(&author, rc->author);
1151 error = gotweb_escape_html(&msg, rc->commit_msg);
1155 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1156 "<div id='diff_title'>Commit Diff</div>\n"
1157 "</div>\n" /* #diff_title_wrapper */
1158 "<div id='diff_content'>\n"
1159 "<div id='diff_header_wrapper'>\n"
1160 "<div id='diff_header'>\n"
1161 "<div id='header_diff_title'>Diff:</div>\n"
1162 "<div id='header_diff'>%s<br />%s</div>\n"
1163 "<div class='header_commit_title'>Commit:</div>\n"
1164 "<div class='header_commit'>%s</div>\n"
1165 "<div id='header_tree_title'>Tree:</div>\n"
1166 "<div id='header_tree'>%s</div>\n"
1167 "<div class='header_author_title'>Author:</div>\n"
1168 "<div class='header_author'>%s</div>\n"
1169 "<div class='header_age_title'>Date:</div>\n"
1170 "<div class='header_age'>%s</div>\n"
1171 "<div id='header_commit_msg_title'>Message:</div>\n"
1172 "<div id='header_commit_msg'>%s</div>\n"
1173 "</div>\n" /* #diff_header */
1174 "</div>\n" /* #diff_header_wrapper */
1175 "<div class='dotted_line'></div>\n"
1176 "<div id='diff'>\n",
1177 rc->parent_id, rc->commit_id,
1186 error = got_output_repo_diff(c);
1190 fcgi_printf(c, "</div>\n"); /* #diff */
1191 fcgi_printf(c, "</div>\n"); /* #diff_content */
1199 static const struct got_error *
1200 gotweb_render_summary(struct request *c)
1202 const struct got_error *error = NULL;
1203 struct transport *t = c->t;
1204 struct server *srv = c->srv;
1207 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1210 if (srv->show_repo_description) {
1212 "<div id='description_title'>Description:</div>\n"
1213 "<div id='description'>%s</div>\n",
1214 t->repo_dir->description ? t->repo_dir->description : "");
1219 if (srv->show_repo_owner) {
1221 "<div id='repo_owner_title'>Owner:</div>\n"
1222 "<div id='repo_owner'>%s</div>\n",
1223 t->repo_dir->owner ? t->repo_dir->owner : "");
1228 if (srv->show_repo_age) {
1230 "<div id='last_change_title'>Last Change:</div>\n"
1231 "<div id='last_change'>%s</div>\n",
1237 if (srv->show_repo_cloneurl) {
1239 "<div id='cloneurl_title'>Clone URL:</div>\n"
1240 "<div id='cloneurl'>%s</div>\n",
1241 t->repo_dir->url ? t->repo_dir->url : "");
1246 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1250 if (gotweb_render_briefs(c->tp) == -1)
1253 error = gotweb_render_tags(c);
1255 log_warnx("%s: %s", __func__, error->msg);
1259 error = gotweb_render_branches(c);
1261 log_warnx("%s: %s", __func__, error->msg);
1266 static const struct got_error *
1267 gotweb_render_tags(struct request *c)
1269 const struct got_error *error = NULL;
1270 struct server *srv = c->srv;
1271 struct transport *t = c->t;
1272 struct querystring *qs = t->qs;
1274 if (qs->action == BRIEFS) {
1276 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1278 error = got_get_repo_tags(c, srv->max_commits_display);
1282 if (gotweb_render_tags_tmpl(c->tp) == -1)
1289 const struct got_error *
1290 gotweb_escape_html(char **escaped_html, const char *orig_html)
1292 const struct got_error *error = NULL;
1293 struct escape_pair {
1304 size_t orig_len, len;
1307 orig_len = strlen(orig_html);
1309 for (i = 0; i < orig_len; i++) {
1310 for (j = 0; j < nitems(esc); j++) {
1311 if (orig_html[i] != esc[j].c)
1313 len += strlen(esc[j].s) - 1 /* escaped char */;
1317 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1318 if (*escaped_html == NULL)
1319 return got_error_from_errno("calloc");
1322 for (i = 0; i < orig_len; i++) {
1324 for (j = 0; j < nitems(esc); j++) {
1325 if (orig_html[i] != esc[j].c)
1328 if (strlcat(*escaped_html, esc[j].s, len + 1)
1330 error = got_error(GOT_ERR_NO_SPACE);
1333 x += strlen(esc[j].s);
1338 (*escaped_html)[x] = orig_html[i];
1344 free(*escaped_html);
1345 *escaped_html = NULL;
1347 (*escaped_html)[x] = '\0';
1354 should_urlencode(int c)
1356 if (c <= ' ' || c >= 127)
1380 /* needed because the URLs are embedded into the HTML */
1389 gotweb_urlencode(const char *str)
1397 for (s = str; *s; ++s) {
1399 if (should_urlencode(*s))
1403 escaped = calloc(1, len + 1);
1404 if (escaped == NULL)
1408 for (s = str; *s; ++s) {
1409 if (should_urlencode(*s)) {
1410 a = (*s & 0xF0) >> 4;
1414 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1415 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1424 gotweb_action_name(int action)
1459 gotweb_render_url(struct request *c, struct gotweb_url *url)
1461 const char *sep = "?", *action;
1465 action = gotweb_action_name(url->action);
1466 if (action != NULL) {
1467 if (fcgi_printf(c, "?action=%s", action) == -1)
1473 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1479 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1485 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1491 tmp = gotweb_urlencode(url->file);
1494 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1502 tmp = gotweb_urlencode(url->folder);
1505 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1513 tmp = gotweb_urlencode(url->headref);
1516 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1523 if (url->index_page != -1) {
1524 if (fcgi_printf(c, "%sindex_page=%d", sep,
1525 url->index_page) == -1)
1531 tmp = gotweb_urlencode(url->path);
1534 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1541 if (url->page != -1) {
1542 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1551 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1553 struct template *tp = c->tp;
1554 const char *proto = c->https ? "https" : "http";
1556 if (fcgi_puts(tp, proto) == -1 ||
1557 fcgi_puts(tp, "://") == -1 ||
1558 tp_htmlescape(tp, c->server_name) == -1 ||
1559 tp_htmlescape(tp, c->document_uri) == -1)
1562 return gotweb_render_url(c, url);
1566 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1571 if (fcgi_printf(c, "<a href='") == -1)
1574 if (gotweb_render_url(c, url) == -1)
1577 if (fcgi_printf(c, "'>") == -1)
1581 r = fcgi_vprintf(c, fmt, ap);
1586 if (fcgi_printf(c, "</a>"))
1591 static struct got_repository *
1592 find_cached_repo(struct server *srv, const char *path)
1596 for (i = 0; i < srv->ncached_repos; i++) {
1597 if (strcmp(srv->cached_repos[i].path, path) == 0)
1598 return srv->cached_repos[i].repo;
1604 static const struct got_error *
1605 cache_repo(struct got_repository **new, struct server *srv,
1606 struct repo_dir *repo_dir, struct socket *sock)
1608 const struct got_error *error = NULL;
1609 struct got_repository *repo;
1610 struct cached_repo *cr;
1613 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1614 cr = &srv->cached_repos[srv->ncached_repos - 1];
1615 error = got_repo_close(cr->repo);
1616 memset(cr, 0, sizeof(*cr));
1617 srv->ncached_repos--;
1620 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1621 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1622 cr = &srv->cached_repos[0];
1625 cr = &srv->cached_repos[srv->ncached_repos];
1628 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1631 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1632 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1637 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1638 >= sizeof(cr->path)) {
1640 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1641 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1643 return got_error(GOT_ERR_NO_SPACE);
1647 srv->ncached_repos++;
1652 static const struct got_error *
1653 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1655 const struct got_error *error = NULL;
1656 struct socket *sock = c->sock;
1657 struct server *srv = c->srv;
1658 struct transport *t = c->t;
1659 struct got_repository *repo = NULL;
1663 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1664 GOTWEB_GIT_DIR) == -1)
1665 return got_error_from_errno("asprintf");
1667 dt = opendir(dir_test);
1671 repo_dir->path = dir_test;
1676 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1677 repo_dir->name) == -1)
1678 return got_error_from_errno("asprintf");
1680 dt = opendir(dir_test);
1682 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1685 repo_dir->path = dir_test;
1690 if (srv->respect_exportok &&
1691 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1692 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1696 repo = find_cached_repo(srv, repo_dir->path);
1698 error = cache_repo(&repo, srv, repo_dir, sock);
1703 error = gotweb_get_repo_description(&repo_dir->description, srv,
1704 repo_dir->path, dirfd(dt));
1707 error = got_get_repo_owner(&repo_dir->owner, c);
1710 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1713 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1717 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1718 error = got_error_from_errno("closedir");
1722 static const struct got_error *
1723 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1725 const struct got_error *error;
1727 *repo_dir = calloc(1, sizeof(**repo_dir));
1728 if (*repo_dir == NULL)
1729 return got_error_from_errno("calloc");
1731 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1732 error = got_error_from_errno("asprintf");
1737 (*repo_dir)->owner = NULL;
1738 (*repo_dir)->description = NULL;
1739 (*repo_dir)->url = NULL;
1740 (*repo_dir)->age = NULL;
1741 (*repo_dir)->path = NULL;
1746 static const struct got_error *
1747 gotweb_get_repo_description(char **description, struct server *srv,
1748 const char *dirpath, int dir)
1750 const struct got_error *error = NULL;
1755 *description = NULL;
1756 if (srv->show_repo_description == 0)
1759 fd = openat(dir, "description", O_RDONLY);
1761 if (errno != ENOENT && errno != EACCES) {
1762 error = got_error_from_errno_fmt("openat %s/%s",
1763 dirpath, "description");
1768 if (fstat(fd, &sb) == -1) {
1769 error = got_error_from_errno_fmt("fstat %s/%s",
1770 dirpath, "description");
1775 if (len > GOTWEBD_MAXDESCRSZ - 1)
1776 len = GOTWEBD_MAXDESCRSZ - 1;
1778 *description = calloc(len + 1, sizeof(**description));
1779 if (*description == NULL) {
1780 error = got_error_from_errno("calloc");
1784 if (read(fd, *description, len) == -1)
1785 error = got_error_from_errno("read");
1787 if (fd != -1 && close(fd) == -1 && error == NULL)
1788 error = got_error_from_errno("close");
1792 static const struct got_error *
1793 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1796 const struct got_error *error = NULL;
1802 if (srv->show_repo_cloneurl == 0)
1805 fd = openat(dir, "cloneurl", O_RDONLY);
1807 if (errno != ENOENT && errno != EACCES) {
1808 error = got_error_from_errno_fmt("openat %s/%s",
1809 dirpath, "cloneurl");
1814 if (fstat(fd, &sb) == -1) {
1815 error = got_error_from_errno_fmt("fstat %s/%s",
1816 dirpath, "cloneurl");
1821 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1822 len = GOTWEBD_MAXCLONEURLSZ - 1;
1824 *url = calloc(len + 1, sizeof(**url));
1826 error = got_error_from_errno("calloc");
1830 if (read(fd, *url, len) == -1)
1831 error = got_error_from_errno("read");
1833 if (fd != -1 && close(fd) == -1 && error == NULL)
1834 error = got_error_from_errno("close");
1838 const struct got_error *
1839 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1842 long long diff_time;
1843 const char *years = "years ago", *months = "months ago";
1844 const char *weeks = "weeks ago", *days = "days ago";
1845 const char *hours = "hours ago", *minutes = "minutes ago";
1846 const char *seconds = "seconds ago", *now = "right now";
1855 diff_time = time(NULL) - committer_time;
1856 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1857 if (asprintf(repo_age, "%lld %s",
1858 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1859 return got_error_from_errno("asprintf");
1860 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1861 if (asprintf(repo_age, "%lld %s",
1862 (diff_time / 60 / 60 / 24 / (365 / 12)),
1864 return got_error_from_errno("asprintf");
1865 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1866 if (asprintf(repo_age, "%lld %s",
1867 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1868 return got_error_from_errno("asprintf");
1869 } else if (diff_time > 60 * 60 * 24 * 2) {
1870 if (asprintf(repo_age, "%lld %s",
1871 (diff_time / 60 / 60 / 24), days) == -1)
1872 return got_error_from_errno("asprintf");
1873 } else if (diff_time > 60 * 60 * 2) {
1874 if (asprintf(repo_age, "%lld %s",
1875 (diff_time / 60 / 60), hours) == -1)
1876 return got_error_from_errno("asprintf");
1877 } else if (diff_time > 60 * 2) {
1878 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1880 return got_error_from_errno("asprintf");
1881 } else if (diff_time > 2) {
1882 if (asprintf(repo_age, "%lld %s", diff_time,
1884 return got_error_from_errno("asprintf");
1886 if (asprintf(repo_age, "%s", now) == -1)
1887 return got_error_from_errno("asprintf");
1891 if (gmtime_r(&committer_time, &tm) == NULL)
1892 return got_error_from_errno("gmtime_r");
1894 s = asctime_r(&tm, datebuf);
1896 return got_error_from_errno("asctime_r");
1898 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1899 return got_error_from_errno("asprintf");
1902 if (gmtime_r(&committer_time, &tm) == NULL)
1903 return got_error_from_errno("gmtime_r");
1905 r = strftime(datebuf, sizeof(datebuf),
1906 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1908 return got_error(GOT_ERR_NO_SPACE);
1910 *repo_age = strdup(datebuf);
1911 if (*repo_age == NULL)
1912 return got_error_from_errno("asprintf");