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 *);
98 static void gotweb_free_querystring(struct querystring *);
99 static void gotweb_free_repo_dir(struct repo_dir *);
101 struct server *gotweb_get_server(uint8_t *, uint8_t *);
104 gotweb_process_request(struct request *c)
106 const struct got_error *error = NULL, *error2 = NULL;
107 struct got_blob_object *blob = NULL;
108 struct server *srv = NULL;
109 struct querystring *qs = NULL;
110 struct repo_dir *repo_dir = NULL;
111 struct got_reflist_head refs;
113 uint8_t err[] = "gotwebd experienced an error: ";
114 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
121 log_warnx("%s: %s", __func__, error->msg);
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
130 log_warnx("%s: error server is NULL", __func__);
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
137 log_warnx("%s: %s", __func__, error->msg);
141 error = gotweb_parse_querystring(&qs, c->querystring);
143 log_warnx("%s: %s", __func__, error->msg);
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
175 error = got_output_file_blob(c);
177 log_warnx("%s: %s", __func__, error->msg);
183 if (qs->action == BLOB) {
185 struct gotweb_url url = {
190 .commit = qs->commit,
191 .folder = qs->folder,
195 error = got_get_repo_commits(c, 1);
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
216 log_warnx("%s: %s", __func__, error->msg);
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 log_warnx("%s: %s", __func__, error->msg);
225 if (gotweb_render_rss(c->tp) == -1)
231 error = gotweb_render_content_type(c, "text/html");
233 log_warnx("%s: %s", __func__, error->msg);
238 if (gotweb_render_header(c->tp) == -1)
248 error = gotweb_render_blame(c);
250 log_warnx("%s: %s", __func__, error->msg);
255 if (gotweb_render_blob(c->tp, blob) == -1)
259 if (gotweb_render_briefs(c->tp) == -1)
263 error = got_get_repo_commits(c, srv->max_commits_display);
265 log_warnx("%s: %s", __func__, error->msg);
268 if (gotweb_render_commits(c->tp) == -1)
272 error = got_get_repo_commits(c, 1);
274 log_warnx("%s: %s", __func__, error->msg);
277 error = got_open_diff_for_output(&fp, &fd, c);
279 log_warnx("%s: %s", __func__, error->msg);
282 if (gotweb_render_diff(c->tp, fp) == -1)
286 error = gotweb_render_index(c);
288 log_warnx("%s: %s", __func__, error->msg);
293 error = got_ref_list(&refs, c->t->repo, "refs/heads",
294 got_ref_cmp_by_name, NULL);
296 log_warnx("%s: got_ref_list: %s", __func__,
301 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
303 log_warnx("%s: got_get_repo_tags: %s", __func__,
307 qs->action = SUMMARY;
308 if (gotweb_render_summary(c->tp, &refs) == -1)
312 error = got_get_repo_tags(c, 1);
314 log_warnx("%s: %s", __func__, error->msg);
317 if (c->t->tag_count == 0) {
318 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
322 if (gotweb_render_tag(c->tp) == -1)
326 error = got_get_repo_tags(c, srv->max_commits_display);
328 log_warnx("%s: %s", __func__, error->msg);
331 if (gotweb_render_tags(c->tp) == -1)
335 error = got_get_repo_commits(c, 1);
337 log_warnx("%s: %s", __func__, error->msg);
340 if (gotweb_render_tree(c->tp) == -1)
345 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
346 "Erorr: Bad Querystring");
354 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
356 if (fcgi_printf(c, "\n%s", err) == -1)
359 if (fcgi_printf(c, "%s", error->msg) == -1)
362 if (fcgi_printf(c, "see daemon logs for details") == -1)
365 if (html && fcgi_printf(c, "</div>\n") == -1)
369 got_object_blob_close(blob);
371 error = got_gotweb_flushfile(fp, fd);
373 log_warnx("%s: got_gotweb_flushfile failure: %s",
374 __func__, error->msg);
379 if (html && srv != NULL)
380 gotweb_render_footer(c->tp);
382 got_ref_list_free(&refs);
386 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
388 struct server *srv = NULL;
390 /* check against the server name first */
391 if (strlen(server_name) > 0)
392 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
393 if (strcmp(srv->name, server_name) == 0)
396 /* check against subdomain second */
397 if (strlen(subdomain) > 0)
398 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
399 if (strcmp(srv->name, subdomain) == 0)
402 /* if those fail, send first server */
403 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
410 const struct got_error *
411 gotweb_init_transport(struct transport **t)
413 const struct got_error *error = NULL;
415 *t = calloc(1, sizeof(**t));
417 return got_error_from_errno2("%s: calloc", __func__);
419 TAILQ_INIT(&(*t)->repo_commits);
420 TAILQ_INIT(&(*t)->repo_tags);
423 (*t)->repo_dir = NULL;
425 (*t)->next_id = NULL;
426 (*t)->prev_id = NULL;
433 static const struct got_error *
434 gotweb_init_querystring(struct querystring **qs)
436 const struct got_error *error = NULL;
438 *qs = calloc(1, sizeof(**qs));
440 return got_error_from_errno2("%s: calloc", __func__);
442 (*qs)->headref = strdup("HEAD");
443 if ((*qs)->headref == NULL) {
446 return got_error_from_errno2("%s: strdup", __func__);
449 (*qs)->action = INDEX;
450 (*qs)->commit = NULL;
452 (*qs)->folder = NULL;
453 (*qs)->index_page = 0;
459 static const struct got_error *
460 gotweb_parse_querystring(struct querystring **qs, char *qst)
462 const struct got_error *error = NULL;
463 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
464 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
471 return got_error_from_errno2("%s: strdup", __func__);
476 while (tok1_pair != NULL) {
477 strsep(&tok1_end, "&");
479 tok2 = strdup(tok1_pair);
482 return got_error_from_errno2("%s: strdup", __func__);
488 while (tok2_pair != NULL) {
489 strsep(&tok2_end, "=");
491 error = gotweb_assign_querystring(qs, tok2_pair,
496 tok2_pair = tok2_end;
499 tok1_pair = tok1_end;
510 * Adapted from usr.sbin/httpd/httpd.c url_decode.
512 static const struct got_error *
513 gotweb_urldecode(char *url)
525 /* Encoding character is followed by two hex chars */
526 if (!isxdigit((unsigned char)p[1]) ||
527 !isxdigit((unsigned char)p[2]) ||
528 (p[1] == '0' && p[2] == '0'))
529 return got_error(GOT_ERR_BAD_QUERYSTRING);
535 * We don't have to validate "hex" because it is
536 * guaranteed to include two hex chars followed by nul.
538 x = strtoul(hex, NULL, 16);
554 static const struct got_error *
555 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
557 const struct got_error *error = NULL;
561 error = gotweb_urldecode(value);
565 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
566 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
569 switch (querystring_keys[el_cnt].element) {
571 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
572 if (strcmp(value, action_keys[a_cnt].name) != 0)
574 else if (strcmp(value,
575 action_keys[a_cnt].name) == 0){
577 action_keys[a_cnt].action;
585 (*qs)->commit = strdup(value);
586 if ((*qs)->commit == NULL) {
587 error = got_error_from_errno2("%s: strdup",
593 (*qs)->file = strdup(value);
594 if ((*qs)->file == NULL) {
595 error = got_error_from_errno2("%s: strdup",
601 (*qs)->folder = strdup(value);
602 if ((*qs)->folder == NULL) {
603 error = got_error_from_errno2("%s: strdup",
609 free((*qs)->headref);
610 (*qs)->headref = strdup(value);
611 if ((*qs)->headref == NULL) {
612 error = got_error_from_errno2("%s: strdup",
618 if (strlen(value) == 0)
620 (*qs)->index_page = strtonum(value, INT64_MIN,
623 error = got_error_from_errno3("%s: strtonum %s",
627 if ((*qs)->index_page < 0)
628 (*qs)->index_page = 0;
631 (*qs)->path = strdup(value);
632 if ((*qs)->path == NULL) {
633 error = got_error_from_errno2("%s: strdup",
639 if (strlen(value) == 0)
641 (*qs)->page = strtonum(value, INT64_MIN,
644 error = got_error_from_errno3("%s: strtonum %s",
660 gotweb_free_repo_tag(struct repo_tag *rt)
665 free(rt->tag_commit);
666 free(rt->commit_msg);
673 gotweb_free_repo_commit(struct repo_commit *rc)
683 free(rc->commit_msg);
689 gotweb_free_querystring(struct querystring *qs)
702 gotweb_free_repo_dir(struct repo_dir *repo_dir)
704 if (repo_dir != NULL) {
705 free(repo_dir->name);
706 free(repo_dir->owner);
707 free(repo_dir->description);
710 free(repo_dir->path);
716 gotweb_free_transport(struct transport *t)
718 struct repo_commit *rc = NULL, *trc = NULL;
719 struct repo_tag *rt = NULL, *trt = NULL;
721 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
722 TAILQ_REMOVE(&t->repo_commits, rc, entry);
723 gotweb_free_repo_commit(rc);
725 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
726 TAILQ_REMOVE(&t->repo_tags, rt, entry);
727 gotweb_free_repo_tag(rt);
729 gotweb_free_repo_dir(t->repo_dir);
730 gotweb_free_querystring(t->qs);
736 const struct got_error *
737 gotweb_render_content_type(struct request *c, const char *type)
739 const char *csp = "default-src 'self'; script-src 'none'; "
740 "object-src 'none';";
743 "Content-Security-Policy: %s\r\n"
744 "Content-Type: %s\r\n\r\n",
749 const struct got_error *
750 gotweb_render_content_type_file(struct request *c, const char *type,
751 const char *file, const char *suffix)
753 fcgi_printf(c, "Content-type: %s\r\n"
754 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
755 type, file, suffix ? suffix : "");
760 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
761 struct gotweb_url *next, int *have_next)
763 struct transport *t = c->t;
764 struct querystring *qs = t->qs;
765 struct server *srv = c->srv;
767 *have_prev = *have_next = 0;
771 if (qs->index_page > 0) {
773 *prev = (struct gotweb_url){
775 .index_page = qs->index_page - 1,
779 if (t->next_disp == srv->max_repos_display &&
780 t->repos_total != (qs->index_page + 1) *
781 srv->max_repos_display) {
783 *next = (struct gotweb_url){
785 .index_page = qs->index_page + 1,
791 if (t->prev_id && qs->commit != NULL &&
792 strcmp(qs->commit, t->prev_id) != 0) {
794 *prev = (struct gotweb_url){
797 .page = qs->page - 1,
799 .commit = t->prev_id,
800 .headref = qs->headref,
805 *next = (struct gotweb_url){
808 .page = qs->page + 1,
810 .commit = t->next_id,
811 .headref = qs->headref,
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,
826 .folder = qs->folder,
832 *next = (struct gotweb_url){
835 .page = qs->page + 1,
837 .commit = t->next_id,
838 .headref = qs->headref,
839 .folder = qs->folder,
845 if (t->prev_id && qs->commit != NULL &&
846 strcmp(qs->commit, t->prev_id) != 0) {
848 *prev = (struct gotweb_url){
851 .page = qs->page - 1,
853 .commit = t->prev_id,
854 .headref = qs->headref,
859 *next = (struct gotweb_url){
862 .page = qs->page + 1,
864 .commit = t->next_id,
865 .headref = qs->headref,
872 static const struct got_error *
873 gotweb_render_index(struct request *c)
875 const struct got_error *error = NULL;
876 struct server *srv = c->srv;
877 struct transport *t = c->t;
878 struct querystring *qs = t->qs;
879 struct repo_dir *repo_dir = NULL;
881 struct dirent **sd_dent = NULL;
882 unsigned int d_cnt, d_i, d_disp = 0;
883 unsigned int d_skipped = 0;
886 d = opendir(srv->repos_path);
888 error = got_error_from_errno2("opendir", srv->repos_path);
892 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
895 error = got_error_from_errno2("scandir", srv->repos_path);
899 if (gotweb_render_repo_table_hdr(c->tp) == -1)
902 for (d_i = 0; d_i < d_cnt; d_i++) {
903 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
906 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
907 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
912 error = got_path_dirent_type(&type, srv->repos_path,
916 if (type != DT_DIR) {
921 if (qs->index_page > 0 && (qs->index_page *
922 srv->max_repos_display) > t->prev_disp) {
927 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
931 error = gotweb_load_got_path(c, repo_dir);
932 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
934 gotweb_free_repo_dir(repo_dir);
939 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
945 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
948 gotweb_free_repo_dir(repo_dir);
951 if (d_disp == srv->max_repos_display)
954 t->repos_total = d_cnt - d_skipped;
956 if (srv->max_repos_display == 0)
958 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
960 if (t->repos_total <= srv->max_repos ||
961 t->repos_total <= srv->max_repos_display)
964 if (gotweb_render_navs(c->tp) == -1)
968 for (d_i = 0; d_i < d_cnt; d_i++)
972 if (d != NULL && closedir(d) == EOF && error == NULL)
973 error = got_error_from_errno("closedir");
977 static const struct got_error *
978 gotweb_render_blame(struct request *c)
980 const struct got_error *error = NULL;
981 struct transport *t = c->t;
982 struct repo_commit *rc = NULL;
983 char *age = NULL, *msg = NULL;
986 error = got_get_repo_commits(c, 1);
990 rc = TAILQ_FIRST(&t->repo_commits);
992 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
995 error = gotweb_escape_html(&msg, rc->commit_msg);
999 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1000 "<div id='blame_title'>Blame</div>\n"
1001 "</div>\n" /* #blame_title_wrapper */
1002 "<div id='blame_content'>\n"
1003 "<div id='blame_header_wrapper'>\n"
1004 "<div id='blame_header'>\n"
1005 "<div class='header_age_title'>Date:</div>\n"
1006 "<div class='header_age'>%s</div>\n"
1007 "<div id='header_commit_msg_title'>Message:</div>\n"
1008 "<div id='header_commit_msg'>%s</div>\n"
1009 "</div>\n" /* #blame_header */
1010 "</div>\n" /* #blame_header_wrapper */
1011 "<div class='dotted_line'></div>\n"
1012 "<div id='blame'>\n",
1018 error = got_output_file_blame(c);
1022 fcgi_printf(c, "</div>\n" /* #blame */
1023 "</div>\n"); /* #blame_content */
1030 const struct got_error *
1031 gotweb_escape_html(char **escaped_html, const char *orig_html)
1033 const struct got_error *error = NULL;
1034 struct escape_pair {
1045 size_t orig_len, len;
1048 orig_len = strlen(orig_html);
1050 for (i = 0; i < orig_len; i++) {
1051 for (j = 0; j < nitems(esc); j++) {
1052 if (orig_html[i] != esc[j].c)
1054 len += strlen(esc[j].s) - 1 /* escaped char */;
1058 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1059 if (*escaped_html == NULL)
1060 return got_error_from_errno("calloc");
1063 for (i = 0; i < orig_len; i++) {
1065 for (j = 0; j < nitems(esc); j++) {
1066 if (orig_html[i] != esc[j].c)
1069 if (strlcat(*escaped_html, esc[j].s, len + 1)
1071 error = got_error(GOT_ERR_NO_SPACE);
1074 x += strlen(esc[j].s);
1079 (*escaped_html)[x] = orig_html[i];
1085 free(*escaped_html);
1086 *escaped_html = NULL;
1088 (*escaped_html)[x] = '\0';
1095 should_urlencode(int c)
1097 if (c <= ' ' || c >= 127)
1121 /* needed because the URLs are embedded into the HTML */
1130 gotweb_urlencode(const char *str)
1138 for (s = str; *s; ++s) {
1140 if (should_urlencode(*s))
1144 escaped = calloc(1, len + 1);
1145 if (escaped == NULL)
1149 for (s = str; *s; ++s) {
1150 if (should_urlencode(*s)) {
1151 a = (*s & 0xF0) >> 4;
1155 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1156 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1165 gotweb_action_name(int action)
1200 gotweb_render_url(struct request *c, struct gotweb_url *url)
1202 const char *sep = "?", *action;
1206 action = gotweb_action_name(url->action);
1207 if (action != NULL) {
1208 if (fcgi_printf(c, "?action=%s", action) == -1)
1214 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1220 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1226 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1232 tmp = gotweb_urlencode(url->file);
1235 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1243 tmp = gotweb_urlencode(url->folder);
1246 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1254 tmp = gotweb_urlencode(url->headref);
1257 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1264 if (url->index_page != -1) {
1265 if (fcgi_printf(c, "%sindex_page=%d", sep,
1266 url->index_page) == -1)
1272 tmp = gotweb_urlencode(url->path);
1275 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1282 if (url->page != -1) {
1283 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1292 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1294 struct template *tp = c->tp;
1295 const char *proto = c->https ? "https" : "http";
1297 if (fcgi_puts(tp, proto) == -1 ||
1298 fcgi_puts(tp, "://") == -1 ||
1299 tp_htmlescape(tp, c->server_name) == -1 ||
1300 tp_htmlescape(tp, c->document_uri) == -1)
1303 return gotweb_render_url(c, url);
1307 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1312 if (fcgi_printf(c, "<a href='") == -1)
1315 if (gotweb_render_url(c, url) == -1)
1318 if (fcgi_printf(c, "'>") == -1)
1322 r = fcgi_vprintf(c, fmt, ap);
1327 if (fcgi_printf(c, "</a>"))
1332 static struct got_repository *
1333 find_cached_repo(struct server *srv, const char *path)
1337 for (i = 0; i < srv->ncached_repos; i++) {
1338 if (strcmp(srv->cached_repos[i].path, path) == 0)
1339 return srv->cached_repos[i].repo;
1345 static const struct got_error *
1346 cache_repo(struct got_repository **new, struct server *srv,
1347 struct repo_dir *repo_dir, struct socket *sock)
1349 const struct got_error *error = NULL;
1350 struct got_repository *repo;
1351 struct cached_repo *cr;
1354 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1355 cr = &srv->cached_repos[srv->ncached_repos - 1];
1356 error = got_repo_close(cr->repo);
1357 memset(cr, 0, sizeof(*cr));
1358 srv->ncached_repos--;
1361 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1362 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1363 cr = &srv->cached_repos[0];
1366 cr = &srv->cached_repos[srv->ncached_repos];
1369 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1372 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1373 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1378 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1379 >= sizeof(cr->path)) {
1381 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1382 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1384 return got_error(GOT_ERR_NO_SPACE);
1388 srv->ncached_repos++;
1393 static const struct got_error *
1394 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1396 const struct got_error *error = NULL;
1397 struct socket *sock = c->sock;
1398 struct server *srv = c->srv;
1399 struct transport *t = c->t;
1400 struct got_repository *repo = NULL;
1404 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1405 GOTWEB_GIT_DIR) == -1)
1406 return got_error_from_errno("asprintf");
1408 dt = opendir(dir_test);
1412 repo_dir->path = dir_test;
1417 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1418 repo_dir->name) == -1)
1419 return got_error_from_errno("asprintf");
1421 dt = opendir(dir_test);
1423 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1426 repo_dir->path = dir_test;
1431 if (srv->respect_exportok &&
1432 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1433 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1437 repo = find_cached_repo(srv, repo_dir->path);
1439 error = cache_repo(&repo, srv, repo_dir, sock);
1444 error = gotweb_get_repo_description(&repo_dir->description, srv,
1445 repo_dir->path, dirfd(dt));
1448 error = got_get_repo_owner(&repo_dir->owner, c);
1451 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1454 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1458 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1459 error = got_error_from_errno("closedir");
1463 static const struct got_error *
1464 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1466 const struct got_error *error;
1468 *repo_dir = calloc(1, sizeof(**repo_dir));
1469 if (*repo_dir == NULL)
1470 return got_error_from_errno("calloc");
1472 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1473 error = got_error_from_errno("asprintf");
1478 (*repo_dir)->owner = NULL;
1479 (*repo_dir)->description = NULL;
1480 (*repo_dir)->url = NULL;
1481 (*repo_dir)->age = NULL;
1482 (*repo_dir)->path = NULL;
1487 static const struct got_error *
1488 gotweb_get_repo_description(char **description, struct server *srv,
1489 const char *dirpath, int dir)
1491 const struct got_error *error = NULL;
1496 *description = NULL;
1497 if (srv->show_repo_description == 0)
1500 fd = openat(dir, "description", O_RDONLY);
1502 if (errno != ENOENT && errno != EACCES) {
1503 error = got_error_from_errno_fmt("openat %s/%s",
1504 dirpath, "description");
1509 if (fstat(fd, &sb) == -1) {
1510 error = got_error_from_errno_fmt("fstat %s/%s",
1511 dirpath, "description");
1516 if (len > GOTWEBD_MAXDESCRSZ - 1)
1517 len = GOTWEBD_MAXDESCRSZ - 1;
1519 *description = calloc(len + 1, sizeof(**description));
1520 if (*description == NULL) {
1521 error = got_error_from_errno("calloc");
1525 if (read(fd, *description, len) == -1)
1526 error = got_error_from_errno("read");
1528 if (fd != -1 && close(fd) == -1 && error == NULL)
1529 error = got_error_from_errno("close");
1533 static const struct got_error *
1534 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1537 const struct got_error *error = NULL;
1543 if (srv->show_repo_cloneurl == 0)
1546 fd = openat(dir, "cloneurl", O_RDONLY);
1548 if (errno != ENOENT && errno != EACCES) {
1549 error = got_error_from_errno_fmt("openat %s/%s",
1550 dirpath, "cloneurl");
1555 if (fstat(fd, &sb) == -1) {
1556 error = got_error_from_errno_fmt("fstat %s/%s",
1557 dirpath, "cloneurl");
1562 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1563 len = GOTWEBD_MAXCLONEURLSZ - 1;
1565 *url = calloc(len + 1, sizeof(**url));
1567 error = got_error_from_errno("calloc");
1571 if (read(fd, *url, len) == -1)
1572 error = got_error_from_errno("read");
1574 if (fd != -1 && close(fd) == -1 && error == NULL)
1575 error = got_error_from_errno("close");
1579 const struct got_error *
1580 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1583 long long diff_time;
1584 const char *years = "years ago", *months = "months ago";
1585 const char *weeks = "weeks ago", *days = "days ago";
1586 const char *hours = "hours ago", *minutes = "minutes ago";
1587 const char *seconds = "seconds ago", *now = "right now";
1596 diff_time = time(NULL) - committer_time;
1597 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1598 if (asprintf(repo_age, "%lld %s",
1599 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1600 return got_error_from_errno("asprintf");
1601 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1602 if (asprintf(repo_age, "%lld %s",
1603 (diff_time / 60 / 60 / 24 / (365 / 12)),
1605 return got_error_from_errno("asprintf");
1606 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1607 if (asprintf(repo_age, "%lld %s",
1608 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1609 return got_error_from_errno("asprintf");
1610 } else if (diff_time > 60 * 60 * 24 * 2) {
1611 if (asprintf(repo_age, "%lld %s",
1612 (diff_time / 60 / 60 / 24), days) == -1)
1613 return got_error_from_errno("asprintf");
1614 } else if (diff_time > 60 * 60 * 2) {
1615 if (asprintf(repo_age, "%lld %s",
1616 (diff_time / 60 / 60), hours) == -1)
1617 return got_error_from_errno("asprintf");
1618 } else if (diff_time > 60 * 2) {
1619 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1621 return got_error_from_errno("asprintf");
1622 } else if (diff_time > 2) {
1623 if (asprintf(repo_age, "%lld %s", diff_time,
1625 return got_error_from_errno("asprintf");
1627 if (asprintf(repo_age, "%s", now) == -1)
1628 return got_error_from_errno("asprintf");
1632 if (gmtime_r(&committer_time, &tm) == NULL)
1633 return got_error_from_errno("gmtime_r");
1635 s = asctime_r(&tm, datebuf);
1637 return got_error_from_errno("asctime_r");
1639 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1640 return got_error_from_errno("asprintf");
1643 if (gmtime_r(&committer_time, &tm) == NULL)
1644 return got_error_from_errno("gmtime_r");
1646 r = strftime(datebuf, sizeof(datebuf),
1647 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1649 return got_error(GOT_ERR_NO_SPACE);
1651 *repo_age = strdup(datebuf);
1652 if (*repo_age == NULL)
1653 return got_error_from_errno("asprintf");