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_summary(struct request *);
99 static const struct got_error *gotweb_render_tag(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;
117 uint8_t err[] = "gotwebd experienced an error: ";
118 int r, html = 0, fd = -1;
120 /* init the transport */
121 error = gotweb_init_transport(&c->t);
123 log_warnx("%s: %s", __func__, error->msg);
126 /* don't process any further if client disconnected */
127 if (c->sock->client_status == CLIENT_DISCONNECT)
129 /* get the gotwebd server */
130 srv = gotweb_get_server(c->server_name, c->http_host);
132 log_warnx("%s: error server is NULL", __func__);
136 /* parse our querystring */
137 error = gotweb_init_querystring(&qs);
139 log_warnx("%s: %s", __func__, error->msg);
143 error = gotweb_parse_querystring(&qs, c->querystring);
145 log_warnx("%s: %s", __func__, error->msg);
150 * certain actions require a commit id in the querystring. this stops
151 * bad actors from exploiting this by manually manipulating the
155 if (qs->action == BLAME || qs->action == BLOB ||
156 qs->action == BLOBRAW || qs->action == DIFF) {
157 if (qs->commit == NULL) {
158 error2 = got_error(GOT_ERR_QUERYSTRING);
163 if (qs->action != INDEX) {
164 error = gotweb_init_repo_dir(&repo_dir, qs->path);
167 error = gotweb_load_got_path(c, repo_dir);
168 c->t->repo_dir = repo_dir;
169 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
173 if (qs->action == BLOBRAW) {
174 error = got_get_repo_commits(c, 1);
177 error = got_output_file_blob(c);
179 log_warnx("%s: %s", __func__, error->msg);
185 if (qs->action == BLOB) {
187 struct gotweb_url url = {
192 .commit = qs->commit,
193 .folder = qs->folder,
197 error = got_get_repo_commits(c, 1);
201 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
205 fcgi_puts(c->tp, "Status: 302\r\n");
206 fcgi_puts(c->tp, "Location: ");
207 gotweb_render_url(c, &url);
208 fcgi_puts(c->tp, "\r\n\r\n");
213 if (qs->action == RSS) {
214 error = gotweb_render_content_type_file(c,
215 "application/rss+xml;charset=utf-8",
216 repo_dir->name, ".rss");
218 log_warnx("%s: %s", __func__, error->msg);
222 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
224 log_warnx("%s: %s", __func__, error->msg);
227 if (gotweb_render_rss(c->tp) == -1)
233 error = gotweb_render_content_type(c, "text/html");
235 log_warnx("%s: %s", __func__, error->msg);
240 if (gotweb_render_header(c->tp) == -1)
250 error = gotweb_render_blame(c);
252 log_warnx("%s: %s", __func__, error->msg);
257 if (gotweb_render_blob(c->tp, blob) == -1)
261 if (gotweb_render_briefs(c->tp) == -1)
265 error = got_get_repo_commits(c, srv->max_commits_display);
267 log_warnx("%s: %s", __func__, error->msg);
270 if (gotweb_render_commits(c->tp) == -1)
274 error = got_get_repo_commits(c, 1);
276 log_warnx("%s: %s", __func__, error->msg);
279 error = got_open_diff_for_output(&fp, &fd, c);
281 log_warnx("%s: %s", __func__, error->msg);
284 if (gotweb_render_diff(c->tp, fp) == -1)
288 error = gotweb_render_index(c);
290 log_warnx("%s: %s", __func__, error->msg);
295 error = gotweb_render_summary(c);
297 log_warnx("%s: %s", __func__, error->msg);
302 error = gotweb_render_tag(c);
304 log_warnx("%s: %s", __func__, error->msg);
309 error = gotweb_render_tags(c);
311 log_warnx("%s: %s", __func__, error->msg);
316 error = got_get_repo_commits(c, 1);
318 log_warnx("%s: %s", __func__, error->msg);
321 if (gotweb_render_tree(c->tp) == -1)
326 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
327 "Erorr: Bad Querystring");
335 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
337 if (fcgi_printf(c, "\n%s", err) == -1)
340 if (fcgi_printf(c, "%s", error->msg) == -1)
343 if (fcgi_printf(c, "see daemon logs for details") == -1)
346 if (html && fcgi_printf(c, "</div>\n") == -1)
350 got_object_blob_close(blob);
352 error = got_gotweb_flushfile(fp, fd);
354 log_warnx("%s: got_gotweb_flushfile failure: %s",
355 __func__, error->msg);
360 if (html && srv != NULL)
361 gotweb_render_footer(c->tp);
365 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
367 struct server *srv = NULL;
369 /* check against the server name first */
370 if (strlen(server_name) > 0)
371 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
372 if (strcmp(srv->name, server_name) == 0)
375 /* check against subdomain second */
376 if (strlen(subdomain) > 0)
377 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
378 if (strcmp(srv->name, subdomain) == 0)
381 /* if those fail, send first server */
382 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
389 const struct got_error *
390 gotweb_init_transport(struct transport **t)
392 const struct got_error *error = NULL;
394 *t = calloc(1, sizeof(**t));
396 return got_error_from_errno2("%s: calloc", __func__);
398 TAILQ_INIT(&(*t)->repo_commits);
399 TAILQ_INIT(&(*t)->repo_tags);
402 (*t)->repo_dir = NULL;
404 (*t)->next_id = NULL;
405 (*t)->prev_id = NULL;
412 static const struct got_error *
413 gotweb_init_querystring(struct querystring **qs)
415 const struct got_error *error = NULL;
417 *qs = calloc(1, sizeof(**qs));
419 return got_error_from_errno2("%s: calloc", __func__);
421 (*qs)->headref = strdup("HEAD");
422 if ((*qs)->headref == NULL) {
425 return got_error_from_errno2("%s: strdup", __func__);
428 (*qs)->action = INDEX;
429 (*qs)->commit = NULL;
431 (*qs)->folder = NULL;
432 (*qs)->index_page = 0;
438 static const struct got_error *
439 gotweb_parse_querystring(struct querystring **qs, char *qst)
441 const struct got_error *error = NULL;
442 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
443 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
450 return got_error_from_errno2("%s: strdup", __func__);
455 while (tok1_pair != NULL) {
456 strsep(&tok1_end, "&");
458 tok2 = strdup(tok1_pair);
461 return got_error_from_errno2("%s: strdup", __func__);
467 while (tok2_pair != NULL) {
468 strsep(&tok2_end, "=");
470 error = gotweb_assign_querystring(qs, tok2_pair,
475 tok2_pair = tok2_end;
478 tok1_pair = tok1_end;
489 * Adapted from usr.sbin/httpd/httpd.c url_decode.
491 static const struct got_error *
492 gotweb_urldecode(char *url)
504 /* Encoding character is followed by two hex chars */
505 if (!isxdigit((unsigned char)p[1]) ||
506 !isxdigit((unsigned char)p[2]) ||
507 (p[1] == '0' && p[2] == '0'))
508 return got_error(GOT_ERR_BAD_QUERYSTRING);
514 * We don't have to validate "hex" because it is
515 * guaranteed to include two hex chars followed by nul.
517 x = strtoul(hex, NULL, 16);
533 static const struct got_error *
534 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
536 const struct got_error *error = NULL;
540 error = gotweb_urldecode(value);
544 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
545 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
548 switch (querystring_keys[el_cnt].element) {
550 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
551 if (strcmp(value, action_keys[a_cnt].name) != 0)
553 else if (strcmp(value,
554 action_keys[a_cnt].name) == 0){
556 action_keys[a_cnt].action;
564 (*qs)->commit = strdup(value);
565 if ((*qs)->commit == NULL) {
566 error = got_error_from_errno2("%s: strdup",
572 (*qs)->file = strdup(value);
573 if ((*qs)->file == NULL) {
574 error = got_error_from_errno2("%s: strdup",
580 (*qs)->folder = strdup(value);
581 if ((*qs)->folder == NULL) {
582 error = got_error_from_errno2("%s: strdup",
588 free((*qs)->headref);
589 (*qs)->headref = strdup(value);
590 if ((*qs)->headref == NULL) {
591 error = got_error_from_errno2("%s: strdup",
597 if (strlen(value) == 0)
599 (*qs)->index_page = strtonum(value, INT64_MIN,
602 error = got_error_from_errno3("%s: strtonum %s",
606 if ((*qs)->index_page < 0)
607 (*qs)->index_page = 0;
610 (*qs)->path = strdup(value);
611 if ((*qs)->path == NULL) {
612 error = got_error_from_errno2("%s: strdup",
618 if (strlen(value) == 0)
620 (*qs)->page = strtonum(value, INT64_MIN,
623 error = got_error_from_errno3("%s: strtonum %s",
639 gotweb_free_repo_tag(struct repo_tag *rt)
644 free(rt->tag_commit);
645 free(rt->commit_msg);
652 gotweb_free_repo_commit(struct repo_commit *rc)
662 free(rc->commit_msg);
668 gotweb_free_querystring(struct querystring *qs)
681 gotweb_free_repo_dir(struct repo_dir *repo_dir)
683 if (repo_dir != NULL) {
684 free(repo_dir->name);
685 free(repo_dir->owner);
686 free(repo_dir->description);
689 free(repo_dir->path);
695 gotweb_free_transport(struct transport *t)
697 struct repo_commit *rc = NULL, *trc = NULL;
698 struct repo_tag *rt = NULL, *trt = NULL;
700 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
701 TAILQ_REMOVE(&t->repo_commits, rc, entry);
702 gotweb_free_repo_commit(rc);
704 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
705 TAILQ_REMOVE(&t->repo_tags, rt, entry);
706 gotweb_free_repo_tag(rt);
708 gotweb_free_repo_dir(t->repo_dir);
709 gotweb_free_querystring(t->qs);
715 const struct got_error *
716 gotweb_render_content_type(struct request *c, const char *type)
718 const char *csp = "default-src 'self'; script-src 'none'; "
719 "object-src 'none';";
722 "Content-Security-Policy: %s\r\n"
723 "Content-Type: %s\r\n\r\n",
728 const struct got_error *
729 gotweb_render_content_type_file(struct request *c, const char *type,
730 const char *file, const char *suffix)
732 fcgi_printf(c, "Content-type: %s\r\n"
733 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
734 type, file, suffix ? suffix : "");
739 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
740 struct gotweb_url *next, int *have_next)
742 struct transport *t = c->t;
743 struct querystring *qs = t->qs;
744 struct server *srv = c->srv;
746 *have_prev = *have_next = 0;
750 if (qs->index_page > 0) {
752 *prev = (struct gotweb_url){
754 .index_page = qs->index_page - 1,
758 if (t->next_disp == srv->max_repos_display &&
759 t->repos_total != (qs->index_page + 1) *
760 srv->max_repos_display) {
762 *next = (struct gotweb_url){
764 .index_page = qs->index_page + 1,
770 if (t->prev_id && qs->commit != NULL &&
771 strcmp(qs->commit, t->prev_id) != 0) {
773 *prev = (struct gotweb_url){
776 .page = qs->page - 1,
778 .commit = t->prev_id,
779 .headref = qs->headref,
784 *next = (struct gotweb_url){
787 .page = qs->page + 1,
789 .commit = t->next_id,
790 .headref = qs->headref,
795 if (t->prev_id && qs->commit != NULL &&
796 strcmp(qs->commit, t->prev_id) != 0) {
798 *prev = (struct gotweb_url){
801 .page = qs->page - 1,
803 .commit = t->prev_id,
804 .headref = qs->headref,
805 .folder = qs->folder,
811 *next = (struct gotweb_url){
814 .page = qs->page + 1,
816 .commit = t->next_id,
817 .headref = qs->headref,
818 .folder = qs->folder,
824 if (t->prev_id && qs->commit != NULL &&
825 strcmp(qs->commit, t->prev_id) != 0) {
827 *prev = (struct gotweb_url){
830 .page = qs->page - 1,
832 .commit = t->prev_id,
833 .headref = qs->headref,
838 *next = (struct gotweb_url){
841 .page = qs->page + 1,
843 .commit = t->next_id,
844 .headref = qs->headref,
851 static const struct got_error *
852 gotweb_render_index(struct request *c)
854 const struct got_error *error = NULL;
855 struct server *srv = c->srv;
856 struct transport *t = c->t;
857 struct querystring *qs = t->qs;
858 struct repo_dir *repo_dir = NULL;
860 struct dirent **sd_dent = NULL;
861 unsigned int d_cnt, d_i, d_disp = 0;
862 unsigned int d_skipped = 0;
865 d = opendir(srv->repos_path);
867 error = got_error_from_errno2("opendir", srv->repos_path);
871 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
874 error = got_error_from_errno2("scandir", srv->repos_path);
878 if (gotweb_render_repo_table_hdr(c->tp) == -1)
881 for (d_i = 0; d_i < d_cnt; d_i++) {
882 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
885 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
886 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
891 error = got_path_dirent_type(&type, srv->repos_path,
895 if (type != DT_DIR) {
900 if (qs->index_page > 0 && (qs->index_page *
901 srv->max_repos_display) > t->prev_disp) {
906 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
910 error = gotweb_load_got_path(c, repo_dir);
911 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
913 gotweb_free_repo_dir(repo_dir);
918 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
924 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
927 gotweb_free_repo_dir(repo_dir);
930 if (d_disp == srv->max_repos_display)
933 t->repos_total = d_cnt - d_skipped;
935 if (srv->max_repos_display == 0)
937 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
939 if (t->repos_total <= srv->max_repos ||
940 t->repos_total <= srv->max_repos_display)
943 if (gotweb_render_navs(c->tp) == -1)
947 for (d_i = 0; d_i < d_cnt; d_i++)
951 if (d != NULL && closedir(d) == EOF && error == NULL)
952 error = got_error_from_errno("closedir");
956 static const struct got_error *
957 gotweb_render_blame(struct request *c)
959 const struct got_error *error = NULL;
960 struct transport *t = c->t;
961 struct repo_commit *rc = NULL;
962 char *age = NULL, *msg = NULL;
965 error = got_get_repo_commits(c, 1);
969 rc = TAILQ_FIRST(&t->repo_commits);
971 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
974 error = gotweb_escape_html(&msg, rc->commit_msg);
978 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
979 "<div id='blame_title'>Blame</div>\n"
980 "</div>\n" /* #blame_title_wrapper */
981 "<div id='blame_content'>\n"
982 "<div id='blame_header_wrapper'>\n"
983 "<div id='blame_header'>\n"
984 "<div class='header_age_title'>Date:</div>\n"
985 "<div class='header_age'>%s</div>\n"
986 "<div id='header_commit_msg_title'>Message:</div>\n"
987 "<div id='header_commit_msg'>%s</div>\n"
988 "</div>\n" /* #blame_header */
989 "</div>\n" /* #blame_header_wrapper */
990 "<div class='dotted_line'></div>\n"
991 "<div id='blame'>\n",
997 error = got_output_file_blame(c);
1001 fcgi_printf(c, "</div>\n" /* #blame */
1002 "</div>\n"); /* #blame_content */
1009 static const struct got_error *
1010 gotweb_render_branches(struct request *c)
1012 const struct got_error *error = NULL;
1013 struct got_reflist_head refs;
1014 struct got_reflist_entry *re;
1015 struct transport *t = c->t;
1016 struct querystring *qs = t->qs;
1017 struct got_repository *repo = t->repo;
1018 char *escaped_refname = NULL;
1024 error = got_ref_list(&refs, repo, "refs/heads",
1025 got_ref_cmp_by_name, NULL);
1029 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1030 "<div id='branches_title'>Branches</div>\n"
1031 "</div>\n" /* #branches_title_wrapper */
1032 "<div id='branches_content'>\n");
1036 TAILQ_FOREACH(re, &refs, entry) {
1037 const char *refname = NULL;
1039 if (got_ref_is_symbolic(re->ref))
1042 refname = got_ref_get_name(re->ref);
1043 if (refname == NULL) {
1044 error = got_error_from_errno("strdup");
1047 if (strncmp(refname, "refs/heads/", 11) != 0)
1050 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1054 if (strncmp(refname, "refs/heads/", 11) == 0)
1056 error = gotweb_escape_html(&escaped_refname, refname);
1060 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1061 "<div class='branches_age'>%s</div>\n"
1062 "<div class='branches_space'> </div>\n"
1063 "<div class='branch'>", age);
1067 r = gotweb_link(c, &(struct gotweb_url){
1073 }, "%s", escaped_refname);
1077 if (fcgi_printf(c, "</div>\n" /* .branch */
1078 "<div class='navs_wrapper'>\n"
1079 "<div class='navs'>") == -1)
1082 r = gotweb_link(c, &(struct gotweb_url){
1092 if (fcgi_printf(c, " | ") == -1)
1095 r = gotweb_link(c, &(struct gotweb_url){
1101 }, "commit briefs");
1105 if (fcgi_printf(c, " | ") == -1)
1108 r = gotweb_link(c, &(struct gotweb_url){
1118 r = fcgi_printf(c, "</div>\n" /* .navs */
1119 "</div>\n" /* .navs_wrapper */
1120 "<div class='dotted_line'></div>\n"
1121 "</div>\n"); /* .branches_wrapper */
1127 free(escaped_refname);
1128 escaped_refname = NULL;
1130 fcgi_printf(c, "</div>\n"); /* #branches_content */
1133 free(escaped_refname);
1134 got_ref_list_free(&refs);
1138 static const struct got_error *
1139 gotweb_render_summary(struct request *c)
1141 const struct got_error *error = NULL;
1142 struct transport *t = c->t;
1143 struct server *srv = c->srv;
1146 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1149 if (srv->show_repo_description) {
1151 "<div id='description_title'>Description:</div>\n"
1152 "<div id='description'>%s</div>\n",
1153 t->repo_dir->description ? t->repo_dir->description : "");
1158 if (srv->show_repo_owner) {
1160 "<div id='repo_owner_title'>Owner:</div>\n"
1161 "<div id='repo_owner'>%s</div>\n",
1162 t->repo_dir->owner ? t->repo_dir->owner : "");
1167 if (srv->show_repo_age) {
1169 "<div id='last_change_title'>Last Change:</div>\n"
1170 "<div id='last_change'>%s</div>\n",
1176 if (srv->show_repo_cloneurl) {
1178 "<div id='cloneurl_title'>Clone URL:</div>\n"
1179 "<div id='cloneurl'>%s</div>\n",
1180 t->repo_dir->url ? t->repo_dir->url : "");
1185 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1189 if (gotweb_render_briefs(c->tp) == -1)
1192 error = gotweb_render_tags(c);
1194 log_warnx("%s: %s", __func__, error->msg);
1198 error = gotweb_render_branches(c);
1200 log_warnx("%s: %s", __func__, error->msg);
1205 static const struct got_error *
1206 gotweb_render_tag(struct request *c)
1208 const struct got_error *error = NULL;
1209 struct repo_tag *rt = NULL;
1210 struct transport *t = c->t;
1211 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1213 error = got_get_repo_tags(c, 1);
1217 if (t->tag_count == 0) {
1218 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1223 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1225 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1228 error = gotweb_escape_html(&author, rt->tagger);
1231 error = gotweb_escape_html(&msg, rt->commit_msg);
1235 tagname = rt->tag_name;
1236 if (strncmp(tagname, "refs/", 5) == 0)
1238 error = gotweb_escape_html(&tagname, tagname);
1242 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1243 "<div id='tags_title'>Tag</div>\n"
1244 "</div>\n" /* #tags_title_wrapper */
1245 "<div id='tags_content'>\n"
1246 "<div id='tag_header_wrapper'>\n"
1247 "<div id='tag_header'>\n"
1248 "<div class='header_commit_title'>Commit:</div>\n"
1249 "<div class='header_commit'>%s"
1250 " <span class='refs_str'>(%s)</span></div>\n"
1251 "<div class='header_author_title'>Tagger:</div>\n"
1252 "<div class='header_author'>%s</div>\n"
1253 "<div class='header_age_title'>Date:</div>\n"
1254 "<div class='header_age'>%s</div>\n"
1255 "<div id='header_commit_msg_title'>Message:</div>\n"
1256 "<div id='header_commit_msg'>%s</div>\n"
1257 "</div>\n" /* #tag_header */
1258 "<div class='dotted_line'></div>\n"
1259 "<div id='tag_commit'>\n%s</div>"
1260 "</div>" /* #tag_header_wrapper */
1261 "</div>", /* #tags_content */
1276 static const struct got_error *
1277 gotweb_render_tags(struct request *c)
1279 const struct got_error *error = NULL;
1280 struct repo_tag *rt = NULL;
1281 struct server *srv = c->srv;
1282 struct transport *t = c->t;
1283 struct querystring *qs = t->qs;
1284 struct repo_dir *repo_dir = t->repo_dir;
1285 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1286 int r, commit_found = 0;
1288 if (qs->action == BRIEFS) {
1290 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1292 error = got_get_repo_tags(c, srv->max_commits_display);
1296 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1297 "<div id='tags_title'>Tags</div>\n"
1298 "</div>\n" /* #tags_title_wrapper */
1299 "<div id='tags_content'>\n");
1303 if (t->tag_count == 0) {
1304 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1305 "This repository contains no tags");
1310 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1311 if (commit_found == 0 && qs->commit != NULL) {
1312 if (strcmp(qs->commit, rt->commit_id) != 0)
1317 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1321 tagname = rt->tag_name;
1322 if (strncmp(tagname, "refs/tags/", 10) == 0)
1324 error = gotweb_escape_html(&tagname, tagname);
1328 if (rt->tag_commit != NULL) {
1329 newline = strchr(rt->tag_commit, '\n');
1332 error = gotweb_escape_html(&msg, rt->tag_commit);
1337 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1338 "<div class='tag'>%s</div>\n"
1339 "<div class='tag_log'>", age, tagname) == -1)
1342 r = gotweb_link(c, &(struct gotweb_url){
1346 .path = repo_dir->name,
1347 .commit = rt->commit_id,
1348 }, "%s", msg ? msg : "");
1352 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1353 "<div class='navs_wrapper'>\n"
1354 "<div class='navs'>") == -1)
1357 r = gotweb_link(c, &(struct gotweb_url){
1361 .path = repo_dir->name,
1362 .commit = rt->commit_id,
1367 if (fcgi_printf(c, " | ") == -1)
1370 r = gotweb_link(c, &(struct gotweb_url){
1374 .path = repo_dir->name,
1375 .commit = rt->commit_id,
1376 }, "commit briefs");
1380 if (fcgi_printf(c, " | ") == -1)
1383 r = gotweb_link(c, &(struct gotweb_url){
1387 .path = repo_dir->name,
1388 .commit = rt->commit_id,
1394 "</div>\n" /* .navs */
1395 "</div>\n" /* .navs_wrapper */
1396 "<div class='dotted_line'></div>\n");
1407 if (t->next_id || t->prev_id) {
1408 if (gotweb_render_navs(c->tp) == -1)
1411 fcgi_printf(c, "</div>\n"); /* #tags_content */
1419 const struct got_error *
1420 gotweb_escape_html(char **escaped_html, const char *orig_html)
1422 const struct got_error *error = NULL;
1423 struct escape_pair {
1434 size_t orig_len, len;
1437 orig_len = strlen(orig_html);
1439 for (i = 0; i < orig_len; i++) {
1440 for (j = 0; j < nitems(esc); j++) {
1441 if (orig_html[i] != esc[j].c)
1443 len += strlen(esc[j].s) - 1 /* escaped char */;
1447 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1448 if (*escaped_html == NULL)
1449 return got_error_from_errno("calloc");
1452 for (i = 0; i < orig_len; i++) {
1454 for (j = 0; j < nitems(esc); j++) {
1455 if (orig_html[i] != esc[j].c)
1458 if (strlcat(*escaped_html, esc[j].s, len + 1)
1460 error = got_error(GOT_ERR_NO_SPACE);
1463 x += strlen(esc[j].s);
1468 (*escaped_html)[x] = orig_html[i];
1474 free(*escaped_html);
1475 *escaped_html = NULL;
1477 (*escaped_html)[x] = '\0';
1484 should_urlencode(int c)
1486 if (c <= ' ' || c >= 127)
1510 /* needed because the URLs are embedded into the HTML */
1519 gotweb_urlencode(const char *str)
1527 for (s = str; *s; ++s) {
1529 if (should_urlencode(*s))
1533 escaped = calloc(1, len + 1);
1534 if (escaped == NULL)
1538 for (s = str; *s; ++s) {
1539 if (should_urlencode(*s)) {
1540 a = (*s & 0xF0) >> 4;
1544 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1545 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1554 gotweb_action_name(int action)
1589 gotweb_render_url(struct request *c, struct gotweb_url *url)
1591 const char *sep = "?", *action;
1595 action = gotweb_action_name(url->action);
1596 if (action != NULL) {
1597 if (fcgi_printf(c, "?action=%s", action) == -1)
1603 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1609 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1615 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1621 tmp = gotweb_urlencode(url->file);
1624 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1632 tmp = gotweb_urlencode(url->folder);
1635 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1643 tmp = gotweb_urlencode(url->headref);
1646 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1653 if (url->index_page != -1) {
1654 if (fcgi_printf(c, "%sindex_page=%d", sep,
1655 url->index_page) == -1)
1661 tmp = gotweb_urlencode(url->path);
1664 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1671 if (url->page != -1) {
1672 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1681 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1683 struct template *tp = c->tp;
1684 const char *proto = c->https ? "https" : "http";
1686 if (fcgi_puts(tp, proto) == -1 ||
1687 fcgi_puts(tp, "://") == -1 ||
1688 tp_htmlescape(tp, c->server_name) == -1 ||
1689 tp_htmlescape(tp, c->document_uri) == -1)
1692 return gotweb_render_url(c, url);
1696 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1701 if (fcgi_printf(c, "<a href='") == -1)
1704 if (gotweb_render_url(c, url) == -1)
1707 if (fcgi_printf(c, "'>") == -1)
1711 r = fcgi_vprintf(c, fmt, ap);
1716 if (fcgi_printf(c, "</a>"))
1721 static struct got_repository *
1722 find_cached_repo(struct server *srv, const char *path)
1726 for (i = 0; i < srv->ncached_repos; i++) {
1727 if (strcmp(srv->cached_repos[i].path, path) == 0)
1728 return srv->cached_repos[i].repo;
1734 static const struct got_error *
1735 cache_repo(struct got_repository **new, struct server *srv,
1736 struct repo_dir *repo_dir, struct socket *sock)
1738 const struct got_error *error = NULL;
1739 struct got_repository *repo;
1740 struct cached_repo *cr;
1743 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1744 cr = &srv->cached_repos[srv->ncached_repos - 1];
1745 error = got_repo_close(cr->repo);
1746 memset(cr, 0, sizeof(*cr));
1747 srv->ncached_repos--;
1750 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1751 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1752 cr = &srv->cached_repos[0];
1755 cr = &srv->cached_repos[srv->ncached_repos];
1758 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1761 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1762 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1767 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1768 >= sizeof(cr->path)) {
1770 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1771 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1773 return got_error(GOT_ERR_NO_SPACE);
1777 srv->ncached_repos++;
1782 static const struct got_error *
1783 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1785 const struct got_error *error = NULL;
1786 struct socket *sock = c->sock;
1787 struct server *srv = c->srv;
1788 struct transport *t = c->t;
1789 struct got_repository *repo = NULL;
1793 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1794 GOTWEB_GIT_DIR) == -1)
1795 return got_error_from_errno("asprintf");
1797 dt = opendir(dir_test);
1801 repo_dir->path = dir_test;
1806 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1807 repo_dir->name) == -1)
1808 return got_error_from_errno("asprintf");
1810 dt = opendir(dir_test);
1812 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1815 repo_dir->path = dir_test;
1820 if (srv->respect_exportok &&
1821 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1822 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1826 repo = find_cached_repo(srv, repo_dir->path);
1828 error = cache_repo(&repo, srv, repo_dir, sock);
1833 error = gotweb_get_repo_description(&repo_dir->description, srv,
1834 repo_dir->path, dirfd(dt));
1837 error = got_get_repo_owner(&repo_dir->owner, c);
1840 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1843 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1847 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1848 error = got_error_from_errno("closedir");
1852 static const struct got_error *
1853 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1855 const struct got_error *error;
1857 *repo_dir = calloc(1, sizeof(**repo_dir));
1858 if (*repo_dir == NULL)
1859 return got_error_from_errno("calloc");
1861 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1862 error = got_error_from_errno("asprintf");
1867 (*repo_dir)->owner = NULL;
1868 (*repo_dir)->description = NULL;
1869 (*repo_dir)->url = NULL;
1870 (*repo_dir)->age = NULL;
1871 (*repo_dir)->path = NULL;
1876 static const struct got_error *
1877 gotweb_get_repo_description(char **description, struct server *srv,
1878 const char *dirpath, int dir)
1880 const struct got_error *error = NULL;
1885 *description = NULL;
1886 if (srv->show_repo_description == 0)
1889 fd = openat(dir, "description", O_RDONLY);
1891 if (errno != ENOENT && errno != EACCES) {
1892 error = got_error_from_errno_fmt("openat %s/%s",
1893 dirpath, "description");
1898 if (fstat(fd, &sb) == -1) {
1899 error = got_error_from_errno_fmt("fstat %s/%s",
1900 dirpath, "description");
1905 if (len > GOTWEBD_MAXDESCRSZ - 1)
1906 len = GOTWEBD_MAXDESCRSZ - 1;
1908 *description = calloc(len + 1, sizeof(**description));
1909 if (*description == NULL) {
1910 error = got_error_from_errno("calloc");
1914 if (read(fd, *description, len) == -1)
1915 error = got_error_from_errno("read");
1917 if (fd != -1 && close(fd) == -1 && error == NULL)
1918 error = got_error_from_errno("close");
1922 static const struct got_error *
1923 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1926 const struct got_error *error = NULL;
1932 if (srv->show_repo_cloneurl == 0)
1935 fd = openat(dir, "cloneurl", O_RDONLY);
1937 if (errno != ENOENT && errno != EACCES) {
1938 error = got_error_from_errno_fmt("openat %s/%s",
1939 dirpath, "cloneurl");
1944 if (fstat(fd, &sb) == -1) {
1945 error = got_error_from_errno_fmt("fstat %s/%s",
1946 dirpath, "cloneurl");
1951 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1952 len = GOTWEBD_MAXCLONEURLSZ - 1;
1954 *url = calloc(len + 1, sizeof(**url));
1956 error = got_error_from_errno("calloc");
1960 if (read(fd, *url, len) == -1)
1961 error = got_error_from_errno("read");
1963 if (fd != -1 && close(fd) == -1 && error == NULL)
1964 error = got_error_from_errno("close");
1968 const struct got_error *
1969 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1972 long long diff_time;
1973 const char *years = "years ago", *months = "months ago";
1974 const char *weeks = "weeks ago", *days = "days ago";
1975 const char *hours = "hours ago", *minutes = "minutes ago";
1976 const char *seconds = "seconds ago", *now = "right now";
1985 diff_time = time(NULL) - committer_time;
1986 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1987 if (asprintf(repo_age, "%lld %s",
1988 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1989 return got_error_from_errno("asprintf");
1990 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1991 if (asprintf(repo_age, "%lld %s",
1992 (diff_time / 60 / 60 / 24 / (365 / 12)),
1994 return got_error_from_errno("asprintf");
1995 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1996 if (asprintf(repo_age, "%lld %s",
1997 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1998 return got_error_from_errno("asprintf");
1999 } else if (diff_time > 60 * 60 * 24 * 2) {
2000 if (asprintf(repo_age, "%lld %s",
2001 (diff_time / 60 / 60 / 24), days) == -1)
2002 return got_error_from_errno("asprintf");
2003 } else if (diff_time > 60 * 60 * 2) {
2004 if (asprintf(repo_age, "%lld %s",
2005 (diff_time / 60 / 60), hours) == -1)
2006 return got_error_from_errno("asprintf");
2007 } else if (diff_time > 60 * 2) {
2008 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2010 return got_error_from_errno("asprintf");
2011 } else if (diff_time > 2) {
2012 if (asprintf(repo_age, "%lld %s", diff_time,
2014 return got_error_from_errno("asprintf");
2016 if (asprintf(repo_age, "%s", now) == -1)
2017 return got_error_from_errno("asprintf");
2021 if (gmtime_r(&committer_time, &tm) == NULL)
2022 return got_error_from_errno("gmtime_r");
2024 s = asctime_r(&tm, datebuf);
2026 return got_error_from_errno("asctime_r");
2028 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2029 return got_error_from_errno("asprintf");
2032 if (gmtime_r(&committer_time, &tm) == NULL)
2033 return got_error_from_errno("gmtime_r");
2035 r = strftime(datebuf, sizeof(datebuf),
2036 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2038 return got_error(GOT_ERR_NO_SPACE);
2040 *repo_age = strdup(datebuf);
2041 if (*repo_age == NULL)
2042 return got_error_from_errno("asprintf");