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.
20 #include "got_compat.h"
23 #include <netinet/in.h>
24 #include <sys/queue.h>
26 #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"
54 static const struct querystring_keys querystring_keys[] = {
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
64 static const struct action_keys action_keys[] = {
67 { "blobraw", BLOBRAW },
69 { "commits", COMMITS },
74 { "summary", SUMMARY },
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 static int gotweb_render_index(struct template *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 static const struct got_error *gotweb_load_got_path(struct request *c,
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static void gotweb_free_querystring(struct querystring *);
97 static void gotweb_free_repo_dir(struct repo_dir *);
99 struct server *gotweb_get_server(const char *);
102 gotweb_reply(struct request *c, int status, const char *ctype,
103 struct gotweb_url *location)
107 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
111 if (tp_writes(c->tp, "Location: ") == -1 ||
112 gotweb_render_url(c, location) == -1 ||
113 tp_writes(c->tp, "\r\n") == -1)
117 csp = "Content-Security-Policy: default-src 'self'; "
118 "script-src 'none'; object-src 'none';\r\n";
119 if (tp_writes(c->tp, csp) == -1)
122 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
125 return tp_writes(c->tp, "\r\n");
129 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
134 r = tp_writef(c->tp, "Content-Disposition: attachment; "
135 "filename=%s%s\r\n", file, suffix ? suffix : "");
138 return gotweb_reply(c, 200, ctype, NULL);
142 gotweb_process_request(struct request *c)
144 const struct got_error *error = NULL;
145 struct server *srv = NULL;
146 struct querystring *qs = NULL;
147 struct repo_dir *repo_dir = NULL;
148 const char *rss_ctype = "application/rss+xml;charset=utf-8";
153 /* init the transport */
154 error = gotweb_init_transport(&c->t);
156 log_warnx("%s: %s", __func__, error->msg);
159 /* don't process any further if client disconnected */
160 if (c->sock->client_status == CLIENT_DISCONNECT)
162 /* get the gotwebd server */
163 srv = gotweb_get_server(c->server_name);
165 log_warnx("%s: error server is NULL", __func__);
169 /* parse our querystring */
170 error = gotweb_init_querystring(&qs);
172 log_warnx("%s: %s", __func__, error->msg);
176 error = gotweb_parse_querystring(&qs, c->querystring);
178 log_warnx("%s: %s", __func__, error->msg);
183 * certain actions require a commit id in the querystring. this stops
184 * bad actors from exploiting this by manually manipulating the
188 if (qs->action == BLAME || qs->action == BLOB ||
189 qs->action == BLOBRAW || qs->action == DIFF ||
190 qs->action == PATCH) {
191 if (qs->commit == NULL) {
192 error = got_error(GOT_ERR_BAD_QUERYSTRING);
197 if (qs->action != INDEX) {
198 error = gotweb_init_repo_dir(&repo_dir, qs->path);
201 error = gotweb_load_got_path(c, repo_dir);
202 c->t->repo_dir = repo_dir;
203 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
207 if (qs->action == BLOBRAW || qs->action == BLOB) {
208 error = got_get_repo_commits(c, 1);
212 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
213 &binary, c, qs->folder, qs->file, qs->commit);
218 switch (qs->action) {
220 error = got_get_repo_commits(c, 1);
222 log_warnx("%s: %s", __func__, error->msg);
225 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
227 gotweb_render_page(c->tp, gotweb_render_blame);
231 struct gotweb_url url = {
235 .commit = qs->commit,
236 .folder = qs->folder,
240 gotweb_reply(c, 302, NULL, &url);
244 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
246 gotweb_render_page(c->tp, gotweb_render_blob);
250 r = gotweb_reply_file(c, "application/octet-stream",
253 r = gotweb_reply(c, 200, "text/plain", NULL);
256 if (template_flush(c->tp) == -1)
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_write(c, buf, len) == -1)
271 error = got_get_repo_commits(c, srv->max_commits_display);
274 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
276 gotweb_render_page(c->tp, gotweb_render_briefs);
279 error = got_get_repo_commits(c, srv->max_commits_display);
281 log_warnx("%s: %s", __func__, error->msg);
284 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
286 gotweb_render_page(c->tp, gotweb_render_commits);
289 error = got_get_repo_commits(c, 1);
291 log_warnx("%s: %s", __func__, error->msg);
294 error = got_open_diff_for_output(&c->t->fp, c);
296 log_warnx("%s: %s", __func__, error->msg);
299 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
301 gotweb_render_page(c->tp, gotweb_render_diff);
304 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
306 if (c->t->nrepos == -1) {
308 error = got_error_from_errno2("scandir",
312 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
314 gotweb_render_page(c->tp, gotweb_render_index);
317 error = got_get_repo_commits(c, 1);
319 log_warnx("%s: %s", __func__, error->msg);
322 error = got_open_diff_for_output(&c->t->fp, c);
324 log_warnx("%s: %s", __func__, error->msg);
327 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
329 gotweb_render_patch(c->tp);
332 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
335 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
338 gotweb_render_rss(c->tp);
341 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
342 got_ref_cmp_by_name, NULL);
344 log_warnx("%s: got_ref_list: %s", __func__,
348 error = got_get_repo_commits(c, srv->summary_commits_display);
352 error = got_get_repo_tags(c, srv->summary_tags_display);
354 log_warnx("%s: got_get_repo_tags: %s", __func__,
358 qs->action = SUMMARY;
359 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
361 gotweb_render_page(c->tp, gotweb_render_summary);
364 error = got_get_repo_tags(c, 1);
366 log_warnx("%s: %s", __func__, error->msg);
369 if (c->t->tag_count == 0) {
370 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
374 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
376 gotweb_render_page(c->tp, gotweb_render_tag);
379 error = got_get_repo_tags(c, srv->max_commits_display);
381 log_warnx("%s: %s", __func__, error->msg);
384 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
386 gotweb_render_page(c->tp, gotweb_render_tags);
389 error = got_get_repo_commits(c, 1);
391 log_warnx("%s: %s", __func__, error->msg);
394 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
396 gotweb_render_page(c->tp, gotweb_render_tree);
400 error = got_error(GOT_ERR_BAD_QUERYSTRING);
405 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
407 gotweb_render_page(c->tp, gotweb_render_error);
411 gotweb_get_server(const char *server_name)
415 /* check against the server name first */
416 if (*server_name != '\0')
417 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
418 if (strcmp(srv->name, server_name) == 0)
421 /* otherwise, use the first server */
422 return TAILQ_FIRST(&gotwebd_env->servers);
425 const struct got_error *
426 gotweb_init_transport(struct transport **t)
428 const struct got_error *error = NULL;
430 *t = calloc(1, sizeof(**t));
432 return got_error_from_errno2(__func__, "calloc");
434 TAILQ_INIT(&(*t)->repo_commits);
435 TAILQ_INIT(&(*t)->repo_tags);
436 TAILQ_INIT(&(*t)->refs);
443 static const struct got_error *
444 gotweb_init_querystring(struct querystring **qs)
446 const struct got_error *error = NULL;
448 *qs = calloc(1, sizeof(**qs));
450 return got_error_from_errno2(__func__, "calloc");
452 (*qs)->headref = strdup("HEAD");
453 if ((*qs)->headref == NULL) {
456 return got_error_from_errno2(__func__, "strdup");
459 (*qs)->action = INDEX;
464 static const struct got_error *
465 gotweb_parse_querystring(struct querystring **qs, char *qst)
467 const struct got_error *error = NULL;
468 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
469 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
476 return got_error_from_errno2(__func__, "strdup");
481 while (tok1_pair != NULL) {
482 strsep(&tok1_end, "&");
484 tok2 = strdup(tok1_pair);
487 return got_error_from_errno2(__func__, "strdup");
493 while (tok2_pair != NULL) {
494 strsep(&tok2_end, "=");
496 error = gotweb_assign_querystring(qs, tok2_pair,
501 tok2_pair = tok2_end;
504 tok1_pair = tok1_end;
515 * Adapted from usr.sbin/httpd/httpd.c url_decode.
517 static const struct got_error *
518 gotweb_urldecode(char *url)
530 /* Encoding character is followed by two hex chars */
531 if (!isxdigit((unsigned char)p[1]) ||
532 !isxdigit((unsigned char)p[2]) ||
533 (p[1] == '0' && p[2] == '0'))
534 return got_error(GOT_ERR_BAD_QUERYSTRING);
540 * We don't have to validate "hex" because it is
541 * guaranteed to include two hex chars followed by nul.
543 x = strtoul(hex, NULL, 16);
559 static const struct got_error *
560 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
562 const struct got_error *error = NULL;
566 error = gotweb_urldecode(value);
570 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
571 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
574 switch (querystring_keys[el_cnt].element) {
576 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
577 if (strcmp(value, action_keys[a_cnt].name) != 0)
579 else if (strcmp(value,
580 action_keys[a_cnt].name) == 0){
582 action_keys[a_cnt].action;
590 (*qs)->commit = strdup(value);
591 if ((*qs)->commit == NULL) {
592 error = got_error_from_errno2(__func__,
598 (*qs)->file = strdup(value);
599 if ((*qs)->file == NULL) {
600 error = got_error_from_errno2(__func__,
606 (*qs)->folder = strdup(value);
607 if ((*qs)->folder == NULL) {
608 error = got_error_from_errno2(__func__,
614 free((*qs)->headref);
615 (*qs)->headref = strdup(value);
616 if ((*qs)->headref == NULL) {
617 error = got_error_from_errno2(__func__,
625 (*qs)->index_page = strtonum(value, INT64_MIN,
628 error = got_error_from_errno3(__func__,
632 if ((*qs)->index_page < 0)
633 (*qs)->index_page = 0;
636 (*qs)->path = strdup(value);
637 if ((*qs)->path == NULL) {
638 error = got_error_from_errno2(__func__,
653 gotweb_free_repo_tag(struct repo_tag *rt)
658 free(rt->tag_commit);
659 free(rt->commit_msg);
666 gotweb_free_repo_commit(struct repo_commit *rc)
676 free(rc->commit_msg);
682 gotweb_free_querystring(struct querystring *qs)
695 gotweb_free_repo_dir(struct repo_dir *repo_dir)
697 if (repo_dir != NULL) {
698 free(repo_dir->name);
699 free(repo_dir->owner);
700 free(repo_dir->description);
702 free(repo_dir->path);
708 gotweb_free_transport(struct transport *t)
710 const struct got_error *err;
711 struct repo_commit *rc = NULL, *trc = NULL;
712 struct repo_tag *rt = NULL, *trt = NULL;
715 got_ref_list_free(&t->refs);
716 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
717 TAILQ_REMOVE(&t->repo_commits, rc, entry);
718 gotweb_free_repo_commit(rc);
720 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
721 TAILQ_REMOVE(&t->repo_tags, rt, entry);
722 gotweb_free_repo_tag(rt);
724 gotweb_free_repo_dir(t->repo_dir);
725 gotweb_free_querystring(t->qs);
727 free(t->tags_more_id);
729 got_object_blob_close(t->blob);
731 err = got_gotweb_closefile(t->fp);
733 log_warnx("%s: got_gotweb_closefile failure: %s",
736 if (t->fd != -1 && close(t->fd) == -1)
737 log_warn("%s: close", __func__);
739 for (i = 0; i < t->nrepos; ++i)
744 got_repo_close(t->repo);
749 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
750 struct gotweb_url *next, int *have_next)
752 struct transport *t = c->t;
753 struct querystring *qs = t->qs;
754 struct server *srv = c->srv;
756 *have_prev = *have_next = 0;
758 if (qs->index_page > 0) {
760 *prev = (struct gotweb_url){
762 .index_page = qs->index_page - 1,
765 if (t->next_disp == srv->max_repos_display &&
766 t->repos_total != (qs->index_page + 1) *
767 srv->max_repos_display) {
769 *next = (struct gotweb_url){
771 .index_page = qs->index_page + 1,
777 gotweb_render_index(struct template *tp)
779 const struct got_error *error = NULL;
780 struct request *c = tp->tp_arg;
781 struct server *srv = c->srv;
782 struct transport *t = c->t;
783 struct querystring *qs = t->qs;
784 struct repo_dir *repo_dir = NULL;
785 struct dirent **sd_dent = t->repos;
786 unsigned int d_i, d_disp = 0;
787 unsigned int d_skipped = 0;
790 if (gotweb_render_repo_table_hdr(c->tp) == -1)
793 for (d_i = 0; d_i < t->nrepos; d_i++) {
794 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
795 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
800 error = got_path_dirent_type(&type, srv->repos_path,
804 if (type != DT_DIR) {
809 if (qs->index_page > 0 && (qs->index_page *
810 srv->max_repos_display) > t->prev_disp) {
815 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
819 error = gotweb_load_got_path(c, repo_dir);
820 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
821 if (error->code != GOT_ERR_NOT_GIT_REPO)
822 log_warnx("%s: %s: %s", __func__,
823 sd_dent[d_i]->d_name, error->msg);
824 gotweb_free_repo_dir(repo_dir);
833 r = gotweb_render_repo_fragment(c->tp, repo_dir);
834 gotweb_free_repo_dir(repo_dir);
836 got_repo_close(t->repo);
842 if (d_disp == srv->max_repos_display)
845 t->repos_total = t->nrepos - d_skipped;
847 if (srv->max_repos_display == 0 ||
848 t->repos_total <= srv->max_repos_display)
851 if (gotweb_render_navs(c->tp) == -1)
858 should_urlencode(int c)
860 if (c <= ' ' || c >= 127)
884 /* needed because the URLs are embedded into the HTML */
893 gotweb_urlencode(const char *str)
901 for (s = str; *s; ++s) {
903 if (should_urlencode(*s))
907 escaped = calloc(1, len + 1);
912 for (s = str; *s; ++s) {
913 if (should_urlencode(*s)) {
914 a = (*s & 0xF0) >> 4;
918 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
919 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
928 gotweb_action_name(int action)
965 gotweb_render_url(struct request *c, struct gotweb_url *url)
967 const char *sep = "?", *action;
971 action = gotweb_action_name(url->action);
972 if (action != NULL) {
973 if (tp_writef(c->tp, "?action=%s", action) == -1)
979 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
985 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
991 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
997 tmp = gotweb_urlencode(url->file);
1000 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1008 tmp = gotweb_urlencode(url->folder);
1011 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1019 tmp = gotweb_urlencode(url->headref);
1022 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1029 if (url->index_page != -1) {
1030 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1031 url->index_page) == -1)
1037 tmp = gotweb_urlencode(url->path);
1040 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1051 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1053 struct template *tp = c->tp;
1054 const char *proto = c->https ? "https" : "http";
1056 if (tp_writes(tp, proto) == -1 ||
1057 tp_writes(tp, "://") == -1 ||
1058 tp_htmlescape(tp, c->server_name) == -1 ||
1059 tp_htmlescape(tp, c->document_uri) == -1)
1062 return gotweb_render_url(c, url);
1065 static const struct got_error *
1066 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1068 const struct got_error *error = NULL;
1069 struct socket *sock = c->sock;
1070 struct server *srv = c->srv;
1071 struct transport *t = c->t;
1075 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1076 GOTWEB_GIT_DIR) == -1)
1077 return got_error_from_errno("asprintf");
1079 dt = opendir(dir_test);
1082 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1083 repo_dir->name) == -1)
1084 return got_error_from_errno("asprintf");
1085 dt = opendir(dir_test);
1088 return got_error_path(repo_dir->name,
1089 GOT_ERR_NOT_GIT_REPO);
1093 repo_dir->path = dir_test;
1096 if (srv->respect_exportok &&
1097 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1098 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1102 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1105 error = gotweb_get_repo_description(&repo_dir->description, srv,
1106 repo_dir->path, dirfd(dt));
1109 error = got_get_repo_owner(&repo_dir->owner, c);
1112 if (srv->show_repo_age) {
1113 error = got_get_repo_age(&repo_dir->age, c, NULL);
1117 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1121 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1122 error = got_error_from_errno("closedir");
1123 if (error && t->repo) {
1124 got_repo_close(t->repo);
1130 static const struct got_error *
1131 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1133 const struct got_error *error;
1135 *repo_dir = calloc(1, sizeof(**repo_dir));
1136 if (*repo_dir == NULL)
1137 return got_error_from_errno("calloc");
1139 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1140 error = got_error_from_errno("asprintf");
1145 (*repo_dir)->owner = NULL;
1146 (*repo_dir)->description = NULL;
1147 (*repo_dir)->url = NULL;
1148 (*repo_dir)->path = NULL;
1153 static const struct got_error *
1154 gotweb_get_repo_description(char **description, struct server *srv,
1155 const char *dirpath, int dir)
1157 const struct got_error *error = NULL;
1162 *description = NULL;
1163 if (srv->show_repo_description == 0)
1166 fd = openat(dir, "description", O_RDONLY);
1168 if (errno != ENOENT && errno != EACCES) {
1169 error = got_error_from_errno_fmt("openat %s/%s",
1170 dirpath, "description");
1175 if (fstat(fd, &sb) == -1) {
1176 error = got_error_from_errno_fmt("fstat %s/%s",
1177 dirpath, "description");
1182 if (len > GOTWEBD_MAXDESCRSZ - 1)
1183 len = GOTWEBD_MAXDESCRSZ - 1;
1185 *description = calloc(len + 1, sizeof(**description));
1186 if (*description == NULL) {
1187 error = got_error_from_errno("calloc");
1191 if (read(fd, *description, len) == -1)
1192 error = got_error_from_errno("read");
1194 if (fd != -1 && close(fd) == -1 && error == NULL)
1195 error = got_error_from_errno("close");
1199 static const struct got_error *
1200 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1203 const struct got_error *error = NULL;
1209 if (srv->show_repo_cloneurl == 0)
1212 fd = openat(dir, "cloneurl", O_RDONLY);
1214 if (errno != ENOENT && errno != EACCES) {
1215 error = got_error_from_errno_fmt("openat %s/%s",
1216 dirpath, "cloneurl");
1221 if (fstat(fd, &sb) == -1) {
1222 error = got_error_from_errno_fmt("fstat %s/%s",
1223 dirpath, "cloneurl");
1228 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1229 len = GOTWEBD_MAXCLONEURLSZ - 1;
1231 *url = calloc(len + 1, sizeof(**url));
1233 error = got_error_from_errno("calloc");
1237 if (read(fd, *url, len) == -1)
1238 error = got_error_from_errno("read");
1240 if (fd != -1 && close(fd) == -1 && error == NULL)
1241 error = got_error_from_errno("close");
1246 gotweb_render_age(struct template *tp, time_t committer_time)
1248 struct request *c = tp->tp_arg;
1249 long long diff_time;
1250 const char *years = "years ago", *months = "months ago";
1251 const char *weeks = "weeks ago", *days = "days ago";
1252 const char *hours = "hours ago", *minutes = "minutes ago";
1253 const char *seconds = "seconds ago", *now = "right now";
1255 diff_time = time(NULL) - committer_time;
1256 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1257 if (tp_writef(c->tp, "%lld %s",
1258 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1260 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1261 if (tp_writef(c->tp, "%lld %s",
1262 (diff_time / 60 / 60 / 24 / (365 / 12)),
1265 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1266 if (tp_writef(c->tp, "%lld %s",
1267 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1269 } else if (diff_time > 60 * 60 * 24 * 2) {
1270 if (tp_writef(c->tp, "%lld %s",
1271 (diff_time / 60 / 60 / 24), days) == -1)
1273 } else if (diff_time > 60 * 60 * 2) {
1274 if (tp_writef(c->tp, "%lld %s",
1275 (diff_time / 60 / 60), hours) == -1)
1277 } else if (diff_time > 60 * 2) {
1278 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1281 } else if (diff_time > 2) {
1282 if (tp_writef(c->tp, "%lld %s", diff_time,
1286 if (tp_writes(tp, now) == -1)