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>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.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 int gotweb_render_index(struct template *);
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 *,
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_reply(struct request *c, int status, const char *ctype,
105 struct gotweb_url *location)
109 if (status != 200 && fcgi_printf(c, "Status: %d\r\n", status) == -1)
113 if (fcgi_puts(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 fcgi_puts(c->tp, "\r\n") == -1)
119 csp = "Content-Security-Policy: default-src 'self'; "
120 "script-src 'none'; object-src 'none';\r\n";
121 if (fcgi_puts(c->tp, csp) == -1)
124 if (ctype && fcgi_printf(c, "Content-Type: %s\r\n", ctype) == -1)
127 return fcgi_puts(c->tp, "\r\n");
131 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
136 r = fcgi_printf(c, "Content-Disposition: attachment; "
137 "filename=%s%s\r\n", file, suffix ? suffix : "");
140 return gotweb_reply(c, 200, ctype, NULL);
144 gotweb_process_request(struct request *c)
146 const struct got_error *error = NULL;
147 struct server *srv = NULL;
148 struct querystring *qs = NULL;
149 struct repo_dir *repo_dir = NULL;
150 const char *rss_ctype = "application/rss+xml;charset=utf-8";
155 /* init the transport */
156 error = gotweb_init_transport(&c->t);
158 log_warnx("%s: %s", __func__, error->msg);
161 /* don't process any further if client disconnected */
162 if (c->sock->client_status == CLIENT_DISCONNECT)
164 /* get the gotwebd server */
165 srv = gotweb_get_server(c->server_name, c->http_host);
167 log_warnx("%s: error server is NULL", __func__);
171 /* parse our querystring */
172 error = gotweb_init_querystring(&qs);
174 log_warnx("%s: %s", __func__, error->msg);
178 error = gotweb_parse_querystring(&qs, c->querystring);
180 log_warnx("%s: %s", __func__, error->msg);
185 * certain actions require a commit id in the querystring. this stops
186 * bad actors from exploiting this by manually manipulating the
190 if (qs->action == BLAME || qs->action == BLOB ||
191 qs->action == BLOBRAW || qs->action == DIFF) {
192 if (qs->commit == NULL) {
193 error = got_error(GOT_ERR_QUERYSTRING);
198 if (qs->action != INDEX) {
199 error = gotweb_init_repo_dir(&repo_dir, qs->path);
202 error = gotweb_load_got_path(c, repo_dir);
203 c->t->repo_dir = repo_dir;
204 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
208 if (qs->action == BLOBRAW || qs->action == BLOB) {
209 error = got_get_repo_commits(c, 1);
213 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
221 error = got_get_repo_commits(c, 1);
223 log_warnx("%s: %s", __func__, error->msg);
226 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
228 gotweb_render_page(c->tp, gotweb_render_blame);
232 struct gotweb_url url = {
237 .commit = qs->commit,
238 .folder = qs->folder,
242 gotweb_reply(c, 302, NULL, &url);
246 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
248 gotweb_render_page(c->tp, gotweb_render_blob);
252 r = gotweb_reply_file(c, "application/octet-stream",
255 r = gotweb_reply(c, 200, "text/plain", NULL);
260 error = got_object_blob_read_block(&len, c->t->blob);
265 buf = got_object_blob_get_read_buf(c->t->blob);
266 if (fcgi_gen_binary_response(c, buf, len) == -1)
271 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
273 gotweb_render_page(c->tp, gotweb_render_briefs);
276 error = got_get_repo_commits(c, srv->max_commits_display);
278 log_warnx("%s: %s", __func__, error->msg);
281 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
283 gotweb_render_page(c->tp, gotweb_render_commits);
286 error = got_get_repo_commits(c, 1);
288 log_warnx("%s: %s", __func__, error->msg);
291 error = got_open_diff_for_output(&c->t->fp, c);
293 log_warnx("%s: %s", __func__, error->msg);
296 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
298 gotweb_render_page(c->tp, gotweb_render_diff);
301 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
303 if (c->t->nrepos == -1) {
305 error = got_error_from_errno2("scandir",
309 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
311 gotweb_render_page(c->tp, gotweb_render_index);
314 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
317 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
320 gotweb_render_rss(c->tp);
323 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
324 got_ref_cmp_by_name, NULL);
326 log_warnx("%s: got_ref_list: %s", __func__,
331 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
333 log_warnx("%s: got_get_repo_tags: %s", __func__,
337 qs->action = SUMMARY;
338 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
340 gotweb_render_page(c->tp, gotweb_render_summary);
343 error = got_get_repo_tags(c, 1);
345 log_warnx("%s: %s", __func__, error->msg);
348 if (c->t->tag_count == 0) {
349 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
353 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
355 gotweb_render_page(c->tp, gotweb_render_tag);
358 error = got_get_repo_tags(c, srv->max_commits_display);
360 log_warnx("%s: %s", __func__, error->msg);
363 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
365 gotweb_render_page(c->tp, gotweb_render_tags);
368 error = got_get_repo_commits(c, 1);
370 log_warnx("%s: %s", __func__, error->msg);
373 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
375 gotweb_render_page(c->tp, gotweb_render_tree);
379 error = got_error(GOT_ERR_BAD_QUERYSTRING);
384 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
386 gotweb_render_page(c->tp, gotweb_render_error);
390 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
392 struct server *srv = NULL;
394 /* check against the server name first */
395 if (strlen(server_name) > 0)
396 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
397 if (strcmp(srv->name, server_name) == 0)
400 /* check against subdomain second */
401 if (strlen(subdomain) > 0)
402 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
403 if (strcmp(srv->name, subdomain) == 0)
406 /* if those fail, send first server */
407 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
414 const struct got_error *
415 gotweb_init_transport(struct transport **t)
417 const struct got_error *error = NULL;
419 *t = calloc(1, sizeof(**t));
421 return got_error_from_errno2(__func__, "calloc");
423 TAILQ_INIT(&(*t)->repo_commits);
424 TAILQ_INIT(&(*t)->repo_tags);
425 TAILQ_INIT(&(*t)->refs);
432 static const struct got_error *
433 gotweb_init_querystring(struct querystring **qs)
435 const struct got_error *error = NULL;
437 *qs = calloc(1, sizeof(**qs));
439 return got_error_from_errno2(__func__, "calloc");
441 (*qs)->headref = strdup("HEAD");
442 if ((*qs)->headref == NULL) {
445 return got_error_from_errno2(__func__, "strdup");
448 (*qs)->action = INDEX;
449 (*qs)->commit = NULL;
451 (*qs)->folder = NULL;
452 (*qs)->index_page = 0;
458 static const struct got_error *
459 gotweb_parse_querystring(struct querystring **qs, char *qst)
461 const struct got_error *error = NULL;
462 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
463 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
470 return got_error_from_errno2(__func__, "strdup");
475 while (tok1_pair != NULL) {
476 strsep(&tok1_end, "&");
478 tok2 = strdup(tok1_pair);
481 return got_error_from_errno2(__func__, "strdup");
487 while (tok2_pair != NULL) {
488 strsep(&tok2_end, "=");
490 error = gotweb_assign_querystring(qs, tok2_pair,
495 tok2_pair = tok2_end;
498 tok1_pair = tok1_end;
509 * Adapted from usr.sbin/httpd/httpd.c url_decode.
511 static const struct got_error *
512 gotweb_urldecode(char *url)
524 /* Encoding character is followed by two hex chars */
525 if (!isxdigit((unsigned char)p[1]) ||
526 !isxdigit((unsigned char)p[2]) ||
527 (p[1] == '0' && p[2] == '0'))
528 return got_error(GOT_ERR_BAD_QUERYSTRING);
534 * We don't have to validate "hex" because it is
535 * guaranteed to include two hex chars followed by nul.
537 x = strtoul(hex, NULL, 16);
553 static const struct got_error *
554 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
556 const struct got_error *error = NULL;
560 error = gotweb_urldecode(value);
564 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
565 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
568 switch (querystring_keys[el_cnt].element) {
570 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
571 if (strcmp(value, action_keys[a_cnt].name) != 0)
573 else if (strcmp(value,
574 action_keys[a_cnt].name) == 0){
576 action_keys[a_cnt].action;
584 (*qs)->commit = strdup(value);
585 if ((*qs)->commit == NULL) {
586 error = got_error_from_errno2(__func__,
592 (*qs)->file = strdup(value);
593 if ((*qs)->file == NULL) {
594 error = got_error_from_errno2(__func__,
600 (*qs)->folder = strdup(value);
601 if ((*qs)->folder == NULL) {
602 error = got_error_from_errno2(__func__,
608 free((*qs)->headref);
609 (*qs)->headref = strdup(value);
610 if ((*qs)->headref == NULL) {
611 error = got_error_from_errno2(__func__,
617 if (strlen(value) == 0)
619 (*qs)->index_page = strtonum(value, INT64_MIN,
622 error = got_error_from_errno3(__func__,
626 if ((*qs)->index_page < 0)
627 (*qs)->index_page = 0;
630 (*qs)->path = strdup(value);
631 if ((*qs)->path == NULL) {
632 error = got_error_from_errno2(__func__,
638 if (strlen(value) == 0)
640 (*qs)->page = strtonum(value, INT64_MIN,
643 error = got_error_from_errno3(__func__,
659 gotweb_free_repo_tag(struct repo_tag *rt)
664 free(rt->tag_commit);
665 free(rt->commit_msg);
672 gotweb_free_repo_commit(struct repo_commit *rc)
682 free(rc->commit_msg);
688 gotweb_free_querystring(struct querystring *qs)
701 gotweb_free_repo_dir(struct repo_dir *repo_dir)
703 if (repo_dir != NULL) {
704 free(repo_dir->name);
705 free(repo_dir->owner);
706 free(repo_dir->description);
708 free(repo_dir->path);
714 gotweb_free_transport(struct transport *t)
716 const struct got_error *err;
717 struct repo_commit *rc = NULL, *trc = NULL;
718 struct repo_tag *rt = NULL, *trt = NULL;
721 got_ref_list_free(&t->refs);
722 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
723 TAILQ_REMOVE(&t->repo_commits, rc, entry);
724 gotweb_free_repo_commit(rc);
726 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
727 TAILQ_REMOVE(&t->repo_tags, rt, entry);
728 gotweb_free_repo_tag(rt);
730 gotweb_free_repo_dir(t->repo_dir);
731 gotweb_free_querystring(t->qs);
736 got_object_blob_close(t->blob);
738 err = got_gotweb_closefile(t->fp);
740 log_warnx("%s: got_gotweb_closefile failure: %s",
743 if (t->fd != -1 && close(t->fd) == -1)
744 log_warn("%s: close", __func__);
746 for (i = 0; i < t->nrepos; ++i)
754 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
755 struct gotweb_url *next, int *have_next)
757 struct transport *t = c->t;
758 struct querystring *qs = t->qs;
759 struct server *srv = c->srv;
761 *have_prev = *have_next = 0;
765 if (qs->index_page > 0) {
767 *prev = (struct gotweb_url){
769 .index_page = qs->index_page - 1,
773 if (t->next_disp == srv->max_repos_display &&
774 t->repos_total != (qs->index_page + 1) *
775 srv->max_repos_display) {
777 *next = (struct gotweb_url){
779 .index_page = qs->index_page + 1,
785 if (t->prev_id && qs->commit != NULL &&
786 strcmp(qs->commit, t->prev_id) != 0) {
788 *prev = (struct gotweb_url){
791 .page = qs->page - 1,
793 .commit = t->prev_id,
794 .headref = qs->headref,
799 *next = (struct gotweb_url){
802 .page = qs->page + 1,
804 .commit = t->next_id,
805 .headref = qs->headref,
813 gotweb_render_index(struct template *tp)
815 const struct got_error *error = NULL;
816 struct request *c = tp->tp_arg;
817 struct server *srv = c->srv;
818 struct transport *t = c->t;
819 struct querystring *qs = t->qs;
820 struct repo_dir *repo_dir = NULL;
821 struct dirent **sd_dent = t->repos;
822 unsigned int d_i, d_disp = 0;
823 unsigned int d_skipped = 0;
826 if (gotweb_render_repo_table_hdr(c->tp) == -1)
829 for (d_i = 0; d_i < t->nrepos; d_i++) {
830 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
833 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
834 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
839 error = got_path_dirent_type(&type, srv->repos_path,
843 if (type != DT_DIR) {
848 if (qs->index_page > 0 && (qs->index_page *
849 srv->max_repos_display) > t->prev_disp) {
854 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
858 error = gotweb_load_got_path(c, repo_dir);
859 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
860 if (error->code != GOT_ERR_NOT_GIT_REPO)
861 log_warnx("%s: %s: %s", __func__,
862 sd_dent[d_i]->d_name, error->msg);
863 gotweb_free_repo_dir(repo_dir);
872 r = gotweb_render_repo_fragment(c->tp, repo_dir);
873 gotweb_free_repo_dir(repo_dir);
878 if (d_disp == srv->max_repos_display)
881 t->repos_total = t->nrepos - d_skipped;
883 if (srv->max_repos_display == 0)
885 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
887 if (t->repos_total <= srv->max_repos ||
888 t->repos_total <= srv->max_repos_display)
891 if (gotweb_render_navs(c->tp) == -1)
898 should_urlencode(int c)
900 if (c <= ' ' || c >= 127)
924 /* needed because the URLs are embedded into the HTML */
933 gotweb_urlencode(const char *str)
941 for (s = str; *s; ++s) {
943 if (should_urlencode(*s))
947 escaped = calloc(1, len + 1);
952 for (s = str; *s; ++s) {
953 if (should_urlencode(*s)) {
954 a = (*s & 0xF0) >> 4;
958 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
959 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
968 gotweb_action_name(int action)
1003 gotweb_render_url(struct request *c, struct gotweb_url *url)
1005 const char *sep = "?", *action;
1009 action = gotweb_action_name(url->action);
1010 if (action != NULL) {
1011 if (fcgi_printf(c, "?action=%s", action) == -1)
1017 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1023 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1029 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1035 tmp = gotweb_urlencode(url->file);
1038 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1046 tmp = gotweb_urlencode(url->folder);
1049 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1057 tmp = gotweb_urlencode(url->headref);
1060 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1067 if (url->index_page != -1) {
1068 if (fcgi_printf(c, "%sindex_page=%d", sep,
1069 url->index_page) == -1)
1075 tmp = gotweb_urlencode(url->path);
1078 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1085 if (url->page != -1) {
1086 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1095 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1097 struct template *tp = c->tp;
1098 const char *proto = c->https ? "https" : "http";
1100 if (fcgi_puts(tp, proto) == -1 ||
1101 fcgi_puts(tp, "://") == -1 ||
1102 tp_htmlescape(tp, c->server_name) == -1 ||
1103 tp_htmlescape(tp, c->document_uri) == -1)
1106 return gotweb_render_url(c, url);
1109 static struct got_repository *
1110 find_cached_repo(struct server *srv, const char *path)
1114 for (i = 0; i < srv->ncached_repos; i++) {
1115 if (strcmp(srv->cached_repos[i].path, path) == 0)
1116 return srv->cached_repos[i].repo;
1122 static const struct got_error *
1123 cache_repo(struct got_repository **new, struct server *srv,
1124 struct repo_dir *repo_dir, struct socket *sock)
1126 const struct got_error *error = NULL;
1127 struct got_repository *repo;
1128 struct cached_repo *cr;
1131 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1132 cr = &srv->cached_repos[srv->ncached_repos - 1];
1133 error = got_repo_close(cr->repo);
1134 memset(cr, 0, sizeof(*cr));
1135 srv->ncached_repos--;
1138 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1139 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1140 cr = &srv->cached_repos[0];
1143 cr = &srv->cached_repos[srv->ncached_repos];
1146 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1149 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1150 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1155 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1156 >= sizeof(cr->path)) {
1158 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1159 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1161 return got_error(GOT_ERR_NO_SPACE);
1165 srv->ncached_repos++;
1170 static const struct got_error *
1171 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1173 const struct got_error *error = NULL;
1174 struct socket *sock = c->sock;
1175 struct server *srv = c->srv;
1176 struct transport *t = c->t;
1177 struct got_repository *repo = NULL;
1181 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1182 GOTWEB_GIT_DIR) == -1)
1183 return got_error_from_errno("asprintf");
1185 dt = opendir(dir_test);
1189 repo_dir->path = dir_test;
1194 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1195 repo_dir->name) == -1)
1196 return got_error_from_errno("asprintf");
1198 dt = opendir(dir_test);
1200 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1203 repo_dir->path = dir_test;
1208 if (srv->respect_exportok &&
1209 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1210 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1214 repo = find_cached_repo(srv, repo_dir->path);
1216 error = cache_repo(&repo, srv, repo_dir, sock);
1221 error = gotweb_get_repo_description(&repo_dir->description, srv,
1222 repo_dir->path, dirfd(dt));
1225 error = got_get_repo_owner(&repo_dir->owner, c);
1228 error = got_get_repo_age(&repo_dir->age, c, NULL);
1231 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1235 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1236 error = got_error_from_errno("closedir");
1240 static const struct got_error *
1241 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1243 const struct got_error *error;
1245 *repo_dir = calloc(1, sizeof(**repo_dir));
1246 if (*repo_dir == NULL)
1247 return got_error_from_errno("calloc");
1249 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1250 error = got_error_from_errno("asprintf");
1255 (*repo_dir)->owner = NULL;
1256 (*repo_dir)->description = NULL;
1257 (*repo_dir)->url = NULL;
1258 (*repo_dir)->path = NULL;
1263 static const struct got_error *
1264 gotweb_get_repo_description(char **description, struct server *srv,
1265 const char *dirpath, int dir)
1267 const struct got_error *error = NULL;
1272 *description = NULL;
1273 if (srv->show_repo_description == 0)
1276 fd = openat(dir, "description", O_RDONLY);
1278 if (errno != ENOENT && errno != EACCES) {
1279 error = got_error_from_errno_fmt("openat %s/%s",
1280 dirpath, "description");
1285 if (fstat(fd, &sb) == -1) {
1286 error = got_error_from_errno_fmt("fstat %s/%s",
1287 dirpath, "description");
1292 if (len > GOTWEBD_MAXDESCRSZ - 1)
1293 len = GOTWEBD_MAXDESCRSZ - 1;
1295 *description = calloc(len + 1, sizeof(**description));
1296 if (*description == NULL) {
1297 error = got_error_from_errno("calloc");
1301 if (read(fd, *description, len) == -1)
1302 error = got_error_from_errno("read");
1304 if (fd != -1 && close(fd) == -1 && error == NULL)
1305 error = got_error_from_errno("close");
1309 static const struct got_error *
1310 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1313 const struct got_error *error = NULL;
1319 if (srv->show_repo_cloneurl == 0)
1322 fd = openat(dir, "cloneurl", O_RDONLY);
1324 if (errno != ENOENT && errno != EACCES) {
1325 error = got_error_from_errno_fmt("openat %s/%s",
1326 dirpath, "cloneurl");
1331 if (fstat(fd, &sb) == -1) {
1332 error = got_error_from_errno_fmt("fstat %s/%s",
1333 dirpath, "cloneurl");
1338 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1339 len = GOTWEBD_MAXCLONEURLSZ - 1;
1341 *url = calloc(len + 1, sizeof(**url));
1343 error = got_error_from_errno("calloc");
1347 if (read(fd, *url, len) == -1)
1348 error = got_error_from_errno("read");
1350 if (fd != -1 && close(fd) == -1 && error == NULL)
1351 error = got_error_from_errno("close");
1356 gotweb_render_age(struct template *tp, time_t committer_time, int ref_tm)
1358 struct request *c = tp->tp_arg;
1360 long long diff_time;
1361 const char *years = "years ago", *months = "months ago";
1362 const char *weeks = "weeks ago", *days = "days ago";
1363 const char *hours = "hours ago", *minutes = "minutes ago";
1364 const char *seconds = "seconds ago", *now = "right now";
1371 diff_time = time(NULL) - committer_time;
1372 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1373 if (fcgi_printf(c, "%lld %s",
1374 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1376 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1377 if (fcgi_printf(c, "%lld %s",
1378 (diff_time / 60 / 60 / 24 / (365 / 12)),
1381 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1382 if (fcgi_printf(c, "%lld %s",
1383 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1385 } else if (diff_time > 60 * 60 * 24 * 2) {
1386 if (fcgi_printf(c, "%lld %s",
1387 (diff_time / 60 / 60 / 24), days) == -1)
1389 } else if (diff_time > 60 * 60 * 2) {
1390 if (fcgi_printf(c, "%lld %s",
1391 (diff_time / 60 / 60), hours) == -1)
1393 } else if (diff_time > 60 * 2) {
1394 if (fcgi_printf(c, "%lld %s", (diff_time / 60),
1397 } else if (diff_time > 2) {
1398 if (fcgi_printf(c, "%lld %s", diff_time,
1402 if (fcgi_puts(tp, now) == -1)
1407 if (gmtime_r(&committer_time, &tm) == NULL)
1410 s = asctime_r(&tm, datebuf);
1414 if (fcgi_puts(tp, datebuf) == -1 ||
1415 fcgi_puts(tp, " UTC") == -1)
1419 if (gmtime_r(&committer_time, &tm) == NULL)
1422 r = strftime(datebuf, sizeof(datebuf),
1423 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1427 if (fcgi_puts(tp, datebuf) == -1)