2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
55 static const struct querystring_keys querystring_keys[] = {
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
66 static const struct action_keys action_keys[] = {
69 { "blobraw", BLOBRAW },
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 static const struct got_error *gotweb_load_got_path(struct request *c,
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_summary(struct request *);
98 static const struct got_error *gotweb_render_tag(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 static void gotweb_free_querystring(struct querystring *);
103 static void gotweb_free_repo_dir(struct repo_dir *);
105 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 gotweb_process_request(struct request *c)
110 const struct got_error *error = NULL, *error2 = NULL;
111 struct got_blob_object *blob = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 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 = got_get_repo_commits(c, 1);
275 log_warnx("%s: %s", __func__, error->msg);
278 error = got_open_diff_for_output(&fp, &fd, c);
280 log_warnx("%s: %s", __func__, error->msg);
283 if (gotweb_render_diff(c->tp, fp) == -1)
287 error = gotweb_render_index(c);
289 log_warnx("%s: %s", __func__, error->msg);
294 error = gotweb_render_summary(c);
296 log_warnx("%s: %s", __func__, error->msg);
301 error = gotweb_render_tag(c);
303 log_warnx("%s: %s", __func__, error->msg);
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);
351 error = got_gotweb_flushfile(fp, fd);
353 log_warnx("%s: got_gotweb_flushfile failure: %s",
354 __func__, error->msg);
359 if (html && srv != NULL)
360 gotweb_render_footer(c->tp);
364 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
366 struct server *srv = NULL;
368 /* check against the server name first */
369 if (strlen(server_name) > 0)
370 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
371 if (strcmp(srv->name, server_name) == 0)
374 /* check against subdomain second */
375 if (strlen(subdomain) > 0)
376 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
377 if (strcmp(srv->name, subdomain) == 0)
380 /* if those fail, send first server */
381 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
388 const struct got_error *
389 gotweb_init_transport(struct transport **t)
391 const struct got_error *error = NULL;
393 *t = calloc(1, sizeof(**t));
395 return got_error_from_errno2("%s: calloc", __func__);
397 TAILQ_INIT(&(*t)->repo_commits);
398 TAILQ_INIT(&(*t)->repo_tags);
401 (*t)->repo_dir = NULL;
403 (*t)->next_id = NULL;
404 (*t)->prev_id = NULL;
411 static const struct got_error *
412 gotweb_init_querystring(struct querystring **qs)
414 const struct got_error *error = NULL;
416 *qs = calloc(1, sizeof(**qs));
418 return got_error_from_errno2("%s: calloc", __func__);
420 (*qs)->headref = strdup("HEAD");
421 if ((*qs)->headref == NULL) {
424 return got_error_from_errno2("%s: strdup", __func__);
427 (*qs)->action = INDEX;
428 (*qs)->commit = NULL;
430 (*qs)->folder = NULL;
431 (*qs)->index_page = 0;
437 static const struct got_error *
438 gotweb_parse_querystring(struct querystring **qs, char *qst)
440 const struct got_error *error = NULL;
441 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
442 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
449 return got_error_from_errno2("%s: strdup", __func__);
454 while (tok1_pair != NULL) {
455 strsep(&tok1_end, "&");
457 tok2 = strdup(tok1_pair);
460 return got_error_from_errno2("%s: strdup", __func__);
466 while (tok2_pair != NULL) {
467 strsep(&tok2_end, "=");
469 error = gotweb_assign_querystring(qs, tok2_pair,
474 tok2_pair = tok2_end;
477 tok1_pair = tok1_end;
488 * Adapted from usr.sbin/httpd/httpd.c url_decode.
490 static const struct got_error *
491 gotweb_urldecode(char *url)
503 /* Encoding character is followed by two hex chars */
504 if (!isxdigit((unsigned char)p[1]) ||
505 !isxdigit((unsigned char)p[2]) ||
506 (p[1] == '0' && p[2] == '0'))
507 return got_error(GOT_ERR_BAD_QUERYSTRING);
513 * We don't have to validate "hex" because it is
514 * guaranteed to include two hex chars followed by nul.
516 x = strtoul(hex, NULL, 16);
532 static const struct got_error *
533 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
535 const struct got_error *error = NULL;
539 error = gotweb_urldecode(value);
543 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
544 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
547 switch (querystring_keys[el_cnt].element) {
549 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
550 if (strcmp(value, action_keys[a_cnt].name) != 0)
552 else if (strcmp(value,
553 action_keys[a_cnt].name) == 0){
555 action_keys[a_cnt].action;
563 (*qs)->commit = strdup(value);
564 if ((*qs)->commit == NULL) {
565 error = got_error_from_errno2("%s: strdup",
571 (*qs)->file = strdup(value);
572 if ((*qs)->file == NULL) {
573 error = got_error_from_errno2("%s: strdup",
579 (*qs)->folder = strdup(value);
580 if ((*qs)->folder == NULL) {
581 error = got_error_from_errno2("%s: strdup",
587 free((*qs)->headref);
588 (*qs)->headref = strdup(value);
589 if ((*qs)->headref == NULL) {
590 error = got_error_from_errno2("%s: strdup",
596 if (strlen(value) == 0)
598 (*qs)->index_page = strtonum(value, INT64_MIN,
601 error = got_error_from_errno3("%s: strtonum %s",
605 if ((*qs)->index_page < 0)
606 (*qs)->index_page = 0;
609 (*qs)->path = strdup(value);
610 if ((*qs)->path == NULL) {
611 error = got_error_from_errno2("%s: strdup",
617 if (strlen(value) == 0)
619 (*qs)->page = strtonum(value, INT64_MIN,
622 error = got_error_from_errno3("%s: strtonum %s",
638 gotweb_free_repo_tag(struct repo_tag *rt)
643 free(rt->tag_commit);
644 free(rt->commit_msg);
651 gotweb_free_repo_commit(struct repo_commit *rc)
661 free(rc->commit_msg);
667 gotweb_free_querystring(struct querystring *qs)
680 gotweb_free_repo_dir(struct repo_dir *repo_dir)
682 if (repo_dir != NULL) {
683 free(repo_dir->name);
684 free(repo_dir->owner);
685 free(repo_dir->description);
688 free(repo_dir->path);
694 gotweb_free_transport(struct transport *t)
696 struct repo_commit *rc = NULL, *trc = NULL;
697 struct repo_tag *rt = NULL, *trt = NULL;
699 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
700 TAILQ_REMOVE(&t->repo_commits, rc, entry);
701 gotweb_free_repo_commit(rc);
703 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
704 TAILQ_REMOVE(&t->repo_tags, rt, entry);
705 gotweb_free_repo_tag(rt);
707 gotweb_free_repo_dir(t->repo_dir);
708 gotweb_free_querystring(t->qs);
714 const struct got_error *
715 gotweb_render_content_type(struct request *c, const char *type)
717 const char *csp = "default-src 'self'; script-src 'none'; "
718 "object-src 'none';";
721 "Content-Security-Policy: %s\r\n"
722 "Content-Type: %s\r\n\r\n",
727 const struct got_error *
728 gotweb_render_content_type_file(struct request *c, const char *type,
729 const char *file, const char *suffix)
731 fcgi_printf(c, "Content-type: %s\r\n"
732 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
733 type, file, suffix ? suffix : "");
738 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
739 struct gotweb_url *next, int *have_next)
741 struct transport *t = c->t;
742 struct querystring *qs = t->qs;
743 struct server *srv = c->srv;
745 *have_prev = *have_next = 0;
749 if (qs->index_page > 0) {
751 *prev = (struct gotweb_url){
753 .index_page = qs->index_page - 1,
757 if (t->next_disp == srv->max_repos_display &&
758 t->repos_total != (qs->index_page + 1) *
759 srv->max_repos_display) {
761 *next = (struct gotweb_url){
763 .index_page = qs->index_page + 1,
769 if (t->prev_id && qs->commit != NULL &&
770 strcmp(qs->commit, t->prev_id) != 0) {
772 *prev = (struct gotweb_url){
775 .page = qs->page - 1,
777 .commit = t->prev_id,
778 .headref = qs->headref,
783 *next = (struct gotweb_url){
786 .page = qs->page + 1,
788 .commit = t->next_id,
789 .headref = qs->headref,
794 if (t->prev_id && qs->commit != NULL &&
795 strcmp(qs->commit, t->prev_id) != 0) {
797 *prev = (struct gotweb_url){
800 .page = qs->page - 1,
802 .commit = t->prev_id,
803 .headref = qs->headref,
804 .folder = qs->folder,
810 *next = (struct gotweb_url){
813 .page = qs->page + 1,
815 .commit = t->next_id,
816 .headref = qs->headref,
817 .folder = qs->folder,
823 if (t->prev_id && qs->commit != NULL &&
824 strcmp(qs->commit, t->prev_id) != 0) {
826 *prev = (struct gotweb_url){
829 .page = qs->page - 1,
831 .commit = t->prev_id,
832 .headref = qs->headref,
837 *next = (struct gotweb_url){
840 .page = qs->page + 1,
842 .commit = t->next_id,
843 .headref = qs->headref,
850 static const struct got_error *
851 gotweb_render_index(struct request *c)
853 const struct got_error *error = NULL;
854 struct server *srv = c->srv;
855 struct transport *t = c->t;
856 struct querystring *qs = t->qs;
857 struct repo_dir *repo_dir = NULL;
859 struct dirent **sd_dent = NULL;
860 unsigned int d_cnt, d_i, d_disp = 0;
861 unsigned int d_skipped = 0;
864 d = opendir(srv->repos_path);
866 error = got_error_from_errno2("opendir", srv->repos_path);
870 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
873 error = got_error_from_errno2("scandir", srv->repos_path);
877 if (gotweb_render_repo_table_hdr(c->tp) == -1)
880 for (d_i = 0; d_i < d_cnt; d_i++) {
881 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
884 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
885 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
890 error = got_path_dirent_type(&type, srv->repos_path,
894 if (type != DT_DIR) {
899 if (qs->index_page > 0 && (qs->index_page *
900 srv->max_repos_display) > t->prev_disp) {
905 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
909 error = gotweb_load_got_path(c, repo_dir);
910 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
912 gotweb_free_repo_dir(repo_dir);
917 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
923 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
926 gotweb_free_repo_dir(repo_dir);
929 if (d_disp == srv->max_repos_display)
932 t->repos_total = d_cnt - d_skipped;
934 if (srv->max_repos_display == 0)
936 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
938 if (t->repos_total <= srv->max_repos ||
939 t->repos_total <= srv->max_repos_display)
942 if (gotweb_render_navs(c->tp) == -1)
946 for (d_i = 0; d_i < d_cnt; d_i++)
950 if (d != NULL && closedir(d) == EOF && error == NULL)
951 error = got_error_from_errno("closedir");
955 static const struct got_error *
956 gotweb_render_blame(struct request *c)
958 const struct got_error *error = NULL;
959 struct transport *t = c->t;
960 struct repo_commit *rc = NULL;
961 char *age = NULL, *msg = NULL;
964 error = got_get_repo_commits(c, 1);
968 rc = TAILQ_FIRST(&t->repo_commits);
970 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
973 error = gotweb_escape_html(&msg, rc->commit_msg);
977 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
978 "<div id='blame_title'>Blame</div>\n"
979 "</div>\n" /* #blame_title_wrapper */
980 "<div id='blame_content'>\n"
981 "<div id='blame_header_wrapper'>\n"
982 "<div id='blame_header'>\n"
983 "<div class='header_age_title'>Date:</div>\n"
984 "<div class='header_age'>%s</div>\n"
985 "<div id='header_commit_msg_title'>Message:</div>\n"
986 "<div id='header_commit_msg'>%s</div>\n"
987 "</div>\n" /* #blame_header */
988 "</div>\n" /* #blame_header_wrapper */
989 "<div class='dotted_line'></div>\n"
990 "<div id='blame'>\n",
996 error = got_output_file_blame(c);
1000 fcgi_printf(c, "</div>\n" /* #blame */
1001 "</div>\n"); /* #blame_content */
1008 static const struct got_error *
1009 gotweb_render_branches(struct request *c)
1011 const struct got_error *error = NULL;
1012 struct got_reflist_head refs;
1013 struct got_reflist_entry *re;
1014 struct transport *t = c->t;
1015 struct querystring *qs = t->qs;
1016 struct got_repository *repo = t->repo;
1017 char *escaped_refname = NULL;
1023 error = got_ref_list(&refs, repo, "refs/heads",
1024 got_ref_cmp_by_name, NULL);
1028 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1029 "<div id='branches_title'>Branches</div>\n"
1030 "</div>\n" /* #branches_title_wrapper */
1031 "<div id='branches_content'>\n");
1035 TAILQ_FOREACH(re, &refs, entry) {
1036 const char *refname = NULL;
1038 if (got_ref_is_symbolic(re->ref))
1041 refname = got_ref_get_name(re->ref);
1042 if (refname == NULL) {
1043 error = got_error_from_errno("strdup");
1046 if (strncmp(refname, "refs/heads/", 11) != 0)
1049 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1053 if (strncmp(refname, "refs/heads/", 11) == 0)
1055 error = gotweb_escape_html(&escaped_refname, refname);
1059 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1060 "<div class='branches_age'>%s</div>\n"
1061 "<div class='branches_space'> </div>\n"
1062 "<div class='branch'>", age);
1066 r = gotweb_link(c, &(struct gotweb_url){
1072 }, "%s", escaped_refname);
1076 if (fcgi_printf(c, "</div>\n" /* .branch */
1077 "<div class='navs_wrapper'>\n"
1078 "<div class='navs'>") == -1)
1081 r = gotweb_link(c, &(struct gotweb_url){
1091 if (fcgi_printf(c, " | ") == -1)
1094 r = gotweb_link(c, &(struct gotweb_url){
1100 }, "commit briefs");
1104 if (fcgi_printf(c, " | ") == -1)
1107 r = gotweb_link(c, &(struct gotweb_url){
1117 r = fcgi_printf(c, "</div>\n" /* .navs */
1118 "</div>\n" /* .navs_wrapper */
1119 "<div class='dotted_line'></div>\n"
1120 "</div>\n"); /* .branches_wrapper */
1126 free(escaped_refname);
1127 escaped_refname = NULL;
1129 fcgi_printf(c, "</div>\n"); /* #branches_content */
1132 free(escaped_refname);
1133 got_ref_list_free(&refs);
1137 static const struct got_error *
1138 gotweb_render_summary(struct request *c)
1140 const struct got_error *error = NULL;
1141 struct transport *t = c->t;
1142 struct server *srv = c->srv;
1145 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1148 if (srv->show_repo_description) {
1150 "<div id='description_title'>Description:</div>\n"
1151 "<div id='description'>%s</div>\n",
1152 t->repo_dir->description ? t->repo_dir->description : "");
1157 if (srv->show_repo_owner) {
1159 "<div id='repo_owner_title'>Owner:</div>\n"
1160 "<div id='repo_owner'>%s</div>\n",
1161 t->repo_dir->owner ? t->repo_dir->owner : "");
1166 if (srv->show_repo_age) {
1168 "<div id='last_change_title'>Last Change:</div>\n"
1169 "<div id='last_change'>%s</div>\n",
1175 if (srv->show_repo_cloneurl) {
1177 "<div id='cloneurl_title'>Clone URL:</div>\n"
1178 "<div id='cloneurl'>%s</div>\n",
1179 t->repo_dir->url ? t->repo_dir->url : "");
1184 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1188 if (gotweb_render_briefs(c->tp) == -1)
1191 error = gotweb_render_tags(c);
1193 log_warnx("%s: %s", __func__, error->msg);
1197 error = gotweb_render_branches(c);
1199 log_warnx("%s: %s", __func__, error->msg);
1204 static const struct got_error *
1205 gotweb_render_tag(struct request *c)
1207 const struct got_error *error = NULL;
1208 struct repo_tag *rt = NULL;
1209 struct transport *t = c->t;
1210 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1212 error = got_get_repo_tags(c, 1);
1216 if (t->tag_count == 0) {
1217 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1222 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1224 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1227 error = gotweb_escape_html(&author, rt->tagger);
1230 error = gotweb_escape_html(&msg, rt->commit_msg);
1234 tagname = rt->tag_name;
1235 if (strncmp(tagname, "refs/", 5) == 0)
1237 error = gotweb_escape_html(&tagname, tagname);
1241 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1242 "<div id='tags_title'>Tag</div>\n"
1243 "</div>\n" /* #tags_title_wrapper */
1244 "<div id='tags_content'>\n"
1245 "<div id='tag_header_wrapper'>\n"
1246 "<div id='tag_header'>\n"
1247 "<div class='header_commit_title'>Commit:</div>\n"
1248 "<div class='header_commit'>%s"
1249 " <span class='refs_str'>(%s)</span></div>\n"
1250 "<div class='header_author_title'>Tagger:</div>\n"
1251 "<div class='header_author'>%s</div>\n"
1252 "<div class='header_age_title'>Date:</div>\n"
1253 "<div class='header_age'>%s</div>\n"
1254 "<div id='header_commit_msg_title'>Message:</div>\n"
1255 "<div id='header_commit_msg'>%s</div>\n"
1256 "</div>\n" /* #tag_header */
1257 "<div class='dotted_line'></div>\n"
1258 "<div id='tag_commit'>\n%s</div>"
1259 "</div>" /* #tag_header_wrapper */
1260 "</div>", /* #tags_content */
1275 static const struct got_error *
1276 gotweb_render_tags(struct request *c)
1278 const struct got_error *error = NULL;
1279 struct repo_tag *rt = NULL;
1280 struct server *srv = c->srv;
1281 struct transport *t = c->t;
1282 struct querystring *qs = t->qs;
1283 struct repo_dir *repo_dir = t->repo_dir;
1284 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1285 int r, commit_found = 0;
1287 if (qs->action == BRIEFS) {
1289 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1291 error = got_get_repo_tags(c, srv->max_commits_display);
1295 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1296 "<div id='tags_title'>Tags</div>\n"
1297 "</div>\n" /* #tags_title_wrapper */
1298 "<div id='tags_content'>\n");
1302 if (t->tag_count == 0) {
1303 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1304 "This repository contains no tags");
1309 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1310 if (commit_found == 0 && qs->commit != NULL) {
1311 if (strcmp(qs->commit, rt->commit_id) != 0)
1316 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1320 tagname = rt->tag_name;
1321 if (strncmp(tagname, "refs/tags/", 10) == 0)
1323 error = gotweb_escape_html(&tagname, tagname);
1327 if (rt->tag_commit != NULL) {
1328 newline = strchr(rt->tag_commit, '\n');
1331 error = gotweb_escape_html(&msg, rt->tag_commit);
1336 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1337 "<div class='tag'>%s</div>\n"
1338 "<div class='tag_log'>", age, tagname) == -1)
1341 r = gotweb_link(c, &(struct gotweb_url){
1345 .path = repo_dir->name,
1346 .commit = rt->commit_id,
1347 }, "%s", msg ? msg : "");
1351 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1352 "<div class='navs_wrapper'>\n"
1353 "<div class='navs'>") == -1)
1356 r = gotweb_link(c, &(struct gotweb_url){
1360 .path = repo_dir->name,
1361 .commit = rt->commit_id,
1366 if (fcgi_printf(c, " | ") == -1)
1369 r = gotweb_link(c, &(struct gotweb_url){
1373 .path = repo_dir->name,
1374 .commit = rt->commit_id,
1375 }, "commit briefs");
1379 if (fcgi_printf(c, " | ") == -1)
1382 r = gotweb_link(c, &(struct gotweb_url){
1386 .path = repo_dir->name,
1387 .commit = rt->commit_id,
1393 "</div>\n" /* .navs */
1394 "</div>\n" /* .navs_wrapper */
1395 "<div class='dotted_line'></div>\n");
1406 if (t->next_id || t->prev_id) {
1407 if (gotweb_render_navs(c->tp) == -1)
1410 fcgi_printf(c, "</div>\n"); /* #tags_content */
1418 const struct got_error *
1419 gotweb_escape_html(char **escaped_html, const char *orig_html)
1421 const struct got_error *error = NULL;
1422 struct escape_pair {
1433 size_t orig_len, len;
1436 orig_len = strlen(orig_html);
1438 for (i = 0; i < orig_len; i++) {
1439 for (j = 0; j < nitems(esc); j++) {
1440 if (orig_html[i] != esc[j].c)
1442 len += strlen(esc[j].s) - 1 /* escaped char */;
1446 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1447 if (*escaped_html == NULL)
1448 return got_error_from_errno("calloc");
1451 for (i = 0; i < orig_len; i++) {
1453 for (j = 0; j < nitems(esc); j++) {
1454 if (orig_html[i] != esc[j].c)
1457 if (strlcat(*escaped_html, esc[j].s, len + 1)
1459 error = got_error(GOT_ERR_NO_SPACE);
1462 x += strlen(esc[j].s);
1467 (*escaped_html)[x] = orig_html[i];
1473 free(*escaped_html);
1474 *escaped_html = NULL;
1476 (*escaped_html)[x] = '\0';
1483 should_urlencode(int c)
1485 if (c <= ' ' || c >= 127)
1509 /* needed because the URLs are embedded into the HTML */
1518 gotweb_urlencode(const char *str)
1526 for (s = str; *s; ++s) {
1528 if (should_urlencode(*s))
1532 escaped = calloc(1, len + 1);
1533 if (escaped == NULL)
1537 for (s = str; *s; ++s) {
1538 if (should_urlencode(*s)) {
1539 a = (*s & 0xF0) >> 4;
1543 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1544 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1553 gotweb_action_name(int action)
1588 gotweb_render_url(struct request *c, struct gotweb_url *url)
1590 const char *sep = "?", *action;
1594 action = gotweb_action_name(url->action);
1595 if (action != NULL) {
1596 if (fcgi_printf(c, "?action=%s", action) == -1)
1602 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1608 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1614 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1620 tmp = gotweb_urlencode(url->file);
1623 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1631 tmp = gotweb_urlencode(url->folder);
1634 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1642 tmp = gotweb_urlencode(url->headref);
1645 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1652 if (url->index_page != -1) {
1653 if (fcgi_printf(c, "%sindex_page=%d", sep,
1654 url->index_page) == -1)
1660 tmp = gotweb_urlencode(url->path);
1663 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1670 if (url->page != -1) {
1671 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1680 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1682 struct template *tp = c->tp;
1683 const char *proto = c->https ? "https" : "http";
1685 if (fcgi_puts(tp, proto) == -1 ||
1686 fcgi_puts(tp, "://") == -1 ||
1687 tp_htmlescape(tp, c->server_name) == -1 ||
1688 tp_htmlescape(tp, c->document_uri) == -1)
1691 return gotweb_render_url(c, url);
1695 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1700 if (fcgi_printf(c, "<a href='") == -1)
1703 if (gotweb_render_url(c, url) == -1)
1706 if (fcgi_printf(c, "'>") == -1)
1710 r = fcgi_vprintf(c, fmt, ap);
1715 if (fcgi_printf(c, "</a>"))
1720 static struct got_repository *
1721 find_cached_repo(struct server *srv, const char *path)
1725 for (i = 0; i < srv->ncached_repos; i++) {
1726 if (strcmp(srv->cached_repos[i].path, path) == 0)
1727 return srv->cached_repos[i].repo;
1733 static const struct got_error *
1734 cache_repo(struct got_repository **new, struct server *srv,
1735 struct repo_dir *repo_dir, struct socket *sock)
1737 const struct got_error *error = NULL;
1738 struct got_repository *repo;
1739 struct cached_repo *cr;
1742 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1743 cr = &srv->cached_repos[srv->ncached_repos - 1];
1744 error = got_repo_close(cr->repo);
1745 memset(cr, 0, sizeof(*cr));
1746 srv->ncached_repos--;
1749 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1750 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1751 cr = &srv->cached_repos[0];
1754 cr = &srv->cached_repos[srv->ncached_repos];
1757 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1760 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1761 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1766 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1767 >= sizeof(cr->path)) {
1769 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1770 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1772 return got_error(GOT_ERR_NO_SPACE);
1776 srv->ncached_repos++;
1781 static const struct got_error *
1782 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1784 const struct got_error *error = NULL;
1785 struct socket *sock = c->sock;
1786 struct server *srv = c->srv;
1787 struct transport *t = c->t;
1788 struct got_repository *repo = NULL;
1792 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1793 GOTWEB_GIT_DIR) == -1)
1794 return got_error_from_errno("asprintf");
1796 dt = opendir(dir_test);
1800 repo_dir->path = dir_test;
1805 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1806 repo_dir->name) == -1)
1807 return got_error_from_errno("asprintf");
1809 dt = opendir(dir_test);
1811 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1814 repo_dir->path = dir_test;
1819 if (srv->respect_exportok &&
1820 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1821 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1825 repo = find_cached_repo(srv, repo_dir->path);
1827 error = cache_repo(&repo, srv, repo_dir, sock);
1832 error = gotweb_get_repo_description(&repo_dir->description, srv,
1833 repo_dir->path, dirfd(dt));
1836 error = got_get_repo_owner(&repo_dir->owner, c);
1839 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1842 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1846 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1847 error = got_error_from_errno("closedir");
1851 static const struct got_error *
1852 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1854 const struct got_error *error;
1856 *repo_dir = calloc(1, sizeof(**repo_dir));
1857 if (*repo_dir == NULL)
1858 return got_error_from_errno("calloc");
1860 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1861 error = got_error_from_errno("asprintf");
1866 (*repo_dir)->owner = NULL;
1867 (*repo_dir)->description = NULL;
1868 (*repo_dir)->url = NULL;
1869 (*repo_dir)->age = NULL;
1870 (*repo_dir)->path = NULL;
1875 static const struct got_error *
1876 gotweb_get_repo_description(char **description, struct server *srv,
1877 const char *dirpath, int dir)
1879 const struct got_error *error = NULL;
1884 *description = NULL;
1885 if (srv->show_repo_description == 0)
1888 fd = openat(dir, "description", O_RDONLY);
1890 if (errno != ENOENT && errno != EACCES) {
1891 error = got_error_from_errno_fmt("openat %s/%s",
1892 dirpath, "description");
1897 if (fstat(fd, &sb) == -1) {
1898 error = got_error_from_errno_fmt("fstat %s/%s",
1899 dirpath, "description");
1904 if (len > GOTWEBD_MAXDESCRSZ - 1)
1905 len = GOTWEBD_MAXDESCRSZ - 1;
1907 *description = calloc(len + 1, sizeof(**description));
1908 if (*description == NULL) {
1909 error = got_error_from_errno("calloc");
1913 if (read(fd, *description, len) == -1)
1914 error = got_error_from_errno("read");
1916 if (fd != -1 && close(fd) == -1 && error == NULL)
1917 error = got_error_from_errno("close");
1921 static const struct got_error *
1922 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1925 const struct got_error *error = NULL;
1931 if (srv->show_repo_cloneurl == 0)
1934 fd = openat(dir, "cloneurl", O_RDONLY);
1936 if (errno != ENOENT && errno != EACCES) {
1937 error = got_error_from_errno_fmt("openat %s/%s",
1938 dirpath, "cloneurl");
1943 if (fstat(fd, &sb) == -1) {
1944 error = got_error_from_errno_fmt("fstat %s/%s",
1945 dirpath, "cloneurl");
1950 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1951 len = GOTWEBD_MAXCLONEURLSZ - 1;
1953 *url = calloc(len + 1, sizeof(**url));
1955 error = got_error_from_errno("calloc");
1959 if (read(fd, *url, len) == -1)
1960 error = got_error_from_errno("read");
1962 if (fd != -1 && close(fd) == -1 && error == NULL)
1963 error = got_error_from_errno("close");
1967 const struct got_error *
1968 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1971 long long diff_time;
1972 const char *years = "years ago", *months = "months ago";
1973 const char *weeks = "weeks ago", *days = "days ago";
1974 const char *hours = "hours ago", *minutes = "minutes ago";
1975 const char *seconds = "seconds ago", *now = "right now";
1984 diff_time = time(NULL) - committer_time;
1985 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1986 if (asprintf(repo_age, "%lld %s",
1987 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1988 return got_error_from_errno("asprintf");
1989 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1990 if (asprintf(repo_age, "%lld %s",
1991 (diff_time / 60 / 60 / 24 / (365 / 12)),
1993 return got_error_from_errno("asprintf");
1994 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1995 if (asprintf(repo_age, "%lld %s",
1996 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1997 return got_error_from_errno("asprintf");
1998 } else if (diff_time > 60 * 60 * 24 * 2) {
1999 if (asprintf(repo_age, "%lld %s",
2000 (diff_time / 60 / 60 / 24), days) == -1)
2001 return got_error_from_errno("asprintf");
2002 } else if (diff_time > 60 * 60 * 2) {
2003 if (asprintf(repo_age, "%lld %s",
2004 (diff_time / 60 / 60), hours) == -1)
2005 return got_error_from_errno("asprintf");
2006 } else if (diff_time > 60 * 2) {
2007 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2009 return got_error_from_errno("asprintf");
2010 } else if (diff_time > 2) {
2011 if (asprintf(repo_age, "%lld %s", diff_time,
2013 return got_error_from_errno("asprintf");
2015 if (asprintf(repo_age, "%s", now) == -1)
2016 return got_error_from_errno("asprintf");
2020 if (gmtime_r(&committer_time, &tm) == NULL)
2021 return got_error_from_errno("gmtime_r");
2023 s = asctime_r(&tm, datebuf);
2025 return got_error_from_errno("asctime_r");
2027 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2028 return got_error_from_errno("asprintf");
2031 if (gmtime_r(&committer_time, &tm) == NULL)
2032 return got_error_from_errno("gmtime_r");
2034 r = strftime(datebuf, sizeof(datebuf),
2035 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2037 return got_error(GOT_ERR_NO_SPACE);
2039 *repo_age = strdup(datebuf);
2040 if (*repo_age == NULL)
2041 return got_error_from_errno("asprintf");