2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
58 static const struct querystring_keys querystring_keys[] = {
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
69 static const struct action_keys action_keys[] = {
73 { "commits", COMMITS },
77 { "summary", SUMMARY },
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
88 static const struct got_error *gotweb_render_header(struct request *);
89 static const struct got_error *gotweb_render_footer(struct request *);
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
93 static const struct got_error *gotweb_load_got_path(struct request *c,
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, char *);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
99 static const struct got_error *gotweb_render_navs(struct request *);
100 static const struct got_error *gotweb_render_blame(struct request *);
101 static const struct got_error *gotweb_render_briefs(struct request *);
102 static const struct got_error *gotweb_render_commits(struct request *);
103 static const struct got_error *gotweb_render_diff(struct request *);
104 static const struct got_error *gotweb_render_summary(struct request *);
105 static const struct got_error *gotweb_render_tag(struct request *);
106 static const struct got_error *gotweb_render_tags(struct request *);
107 static const struct got_error *gotweb_render_tree(struct request *);
108 static const struct got_error *gotweb_render_branches(struct request *);
110 static void gotweb_free_querystring(struct querystring *);
111 static void gotweb_free_repo_dir(struct repo_dir *);
113 struct server *gotweb_get_server(uint8_t *, uint8_t *);
116 gotweb_process_request(struct request *c)
118 const struct got_error *error = NULL, *error2 = NULL;
119 struct server *srv = NULL;
120 struct querystring *qs = NULL;
121 struct repo_dir *repo_dir = NULL;
122 uint8_t err[] = "gotwebd experienced an error: ";
125 /* init the transport */
126 error = gotweb_init_transport(&c->t);
128 log_warnx("%s: %s", __func__, error->msg);
131 /* don't process any further if client disconnected */
132 if (c->sock->client_status == CLIENT_DISCONNECT)
134 /* get the gotwebd server */
135 srv = gotweb_get_server(c->server_name, c->http_host);
137 log_warnx("%s: error server is NULL", __func__);
141 /* parse our querystring */
142 error = gotweb_init_querystring(&qs);
144 log_warnx("%s: %s", __func__, error->msg);
148 error = gotweb_parse_querystring(&qs, c->querystring);
150 log_warnx("%s: %s", __func__, error->msg);
155 * certain actions require a commit id in the querystring. this stops
156 * bad actors from exploiting this by manually manipulating the
160 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
161 qs->action == DIFF)) {
162 error2 = got_error(GOT_ERR_QUERYSTRING);
166 if (qs->action != INDEX) {
167 error = gotweb_init_repo_dir(&repo_dir, qs->path);
170 error = gotweb_load_got_path(c, repo_dir);
171 c->t->repo_dir = repo_dir;
172 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
176 /* render top of page */
177 if (qs != NULL && qs->action == BLOB) {
178 error = got_get_repo_commits(c, 1);
181 error = got_output_file_blob(c);
183 log_warnx("%s: %s", __func__, error->msg);
189 error = gotweb_render_content_type(c, "text/html");
191 log_warnx("%s: %s", __func__, error->msg);
197 error = gotweb_render_header(c);
199 log_warnx("%s: %s", __func__, error->msg);
210 error = gotweb_render_blame(c);
212 log_warnx("%s: %s", __func__, error->msg);
217 error = gotweb_render_briefs(c);
219 log_warnx("%s: %s", __func__, error->msg);
224 error = gotweb_render_commits(c);
226 log_warnx("%s: %s", __func__, error->msg);
231 error = gotweb_render_diff(c);
233 log_warnx("%s: %s", __func__, error->msg);
238 error = gotweb_render_index(c);
240 log_warnx("%s: %s", __func__, error->msg);
245 error = gotweb_render_summary(c);
247 log_warnx("%s: %s", __func__, error->msg);
252 error = gotweb_render_tag(c);
254 log_warnx("%s: %s", __func__, error->msg);
259 error = gotweb_render_tags(c);
261 log_warnx("%s: %s", __func__, error->msg);
266 error = gotweb_render_tree(c);
268 log_warnx("%s: %s", __func__, error->msg);
274 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
275 "Erorr: Bad Querystring");
283 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
285 if (fcgi_printf(c, "%s", err) == -1)
288 if (fcgi_printf(c, "%s", error->msg) == -1)
291 if (fcgi_printf(c, "see daemon logs for details") == -1)
294 if (html && fcgi_printf(c, "</div>\n") == -1)
297 if (html && srv != NULL)
298 gotweb_render_footer(c);
302 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
304 struct server *srv = NULL;
306 /* check against the server name first */
307 if (strlen(server_name) > 0)
308 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
309 if (strcmp(srv->name, server_name) == 0)
312 /* check against subdomain second */
313 if (strlen(subdomain) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, subdomain) == 0)
318 /* if those fail, send first server */
319 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
326 const struct got_error *
327 gotweb_init_transport(struct transport **t)
329 const struct got_error *error = NULL;
331 *t = calloc(1, sizeof(**t));
333 return got_error_from_errno2("%s: calloc", __func__);
335 TAILQ_INIT(&(*t)->repo_commits);
336 TAILQ_INIT(&(*t)->repo_tags);
339 (*t)->repo_dir = NULL;
341 (*t)->next_id = NULL;
342 (*t)->prev_id = NULL;
349 static const struct got_error *
350 gotweb_init_querystring(struct querystring **qs)
352 const struct got_error *error = NULL;
354 *qs = calloc(1, sizeof(**qs));
356 return got_error_from_errno2("%s: calloc", __func__);
358 (*qs)->headref = strdup("HEAD");
359 if ((*qs)->headref == NULL) {
362 return got_error_from_errno2("%s: strdup", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->index_page = 0;
370 (*qs)->index_page_str = NULL;
376 static const struct got_error *
377 gotweb_parse_querystring(struct querystring **qs, char *qst)
379 const struct got_error *error = NULL;
380 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
381 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
388 return got_error_from_errno2("%s: strdup", __func__);
393 while (tok1_pair != NULL) {
394 strsep(&tok1_end, "&");
396 tok2 = strdup(tok1_pair);
399 return got_error_from_errno2("%s: strdup", __func__);
405 while (tok2_pair != NULL) {
406 strsep(&tok2_end, "=");
408 error = gotweb_assign_querystring(qs, tok2_pair,
413 tok2_pair = tok2_end;
416 tok1_pair = tok1_end;
427 * Adapted from usr.sbin/httpd/httpd.c url_decode.
429 static const struct got_error *
430 gotweb_urldecode(char *url)
442 /* Encoding character is followed by two hex chars */
443 if (!isxdigit((unsigned char)p[1]) ||
444 !isxdigit((unsigned char)p[2]) ||
445 (p[1] == '0' && p[2] == '0'))
446 return got_error(GOT_ERR_BAD_QUERYSTRING);
452 * We don't have to validate "hex" because it is
453 * guaranteed to include two hex chars followed by nul.
455 x = strtoul(hex, NULL, 16);
471 static const struct got_error *
472 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
474 const struct got_error *error = NULL;
478 error = gotweb_urldecode(value);
482 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
483 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
486 switch (querystring_keys[el_cnt].element) {
488 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
489 if (strcmp(value, action_keys[a_cnt].name) != 0)
491 else if (strcmp(value,
492 action_keys[a_cnt].name) == 0){
494 action_keys[a_cnt].action;
502 (*qs)->commit = strdup(value);
503 if ((*qs)->commit == NULL) {
504 error = got_error_from_errno2("%s: strdup",
510 (*qs)->file = strdup(value);
511 if ((*qs)->file == NULL) {
512 error = got_error_from_errno2("%s: strdup",
518 (*qs)->folder = strdup(value);
519 if ((*qs)->folder == NULL) {
520 error = got_error_from_errno2("%s: strdup",
526 free((*qs)->headref);
527 (*qs)->headref = strdup(value);
528 if ((*qs)->headref == NULL) {
529 error = got_error_from_errno2("%s: strdup",
535 if (strlen(value) == 0)
537 (*qs)->index_page_str = strdup(value);
538 if ((*qs)->index_page_str == NULL) {
539 error = got_error_from_errno2("%s: strdup",
543 (*qs)->index_page = strtonum(value, INT64_MIN,
546 error = got_error_from_errno3("%s: strtonum %s",
550 if ((*qs)->index_page < 0) {
551 (*qs)->index_page = 0;
552 sprintf((*qs)->index_page_str, "%d", 0);
556 (*qs)->path = strdup(value);
557 if ((*qs)->path == NULL) {
558 error = got_error_from_errno2("%s: strdup",
564 if (strlen(value) == 0)
566 (*qs)->page_str = strdup(value);
567 if ((*qs)->page_str == NULL) {
568 error = got_error_from_errno2("%s: strdup",
572 (*qs)->page = strtonum(value, INT64_MIN,
575 error = got_error_from_errno3("%s: strtonum %s",
579 if ((*qs)->page < 0) {
581 sprintf((*qs)->page_str, "%d", 0);
593 gotweb_free_repo_tag(struct repo_tag *rt)
598 free(rt->tag_commit);
599 free(rt->commit_msg);
606 gotweb_free_repo_commit(struct repo_commit *rc)
616 free(rc->commit_msg);
622 gotweb_free_querystring(struct querystring *qs)
629 free(qs->index_page_str);
637 gotweb_free_repo_dir(struct repo_dir *repo_dir)
639 if (repo_dir != NULL) {
640 free(repo_dir->name);
641 free(repo_dir->owner);
642 free(repo_dir->description);
645 free(repo_dir->path);
651 gotweb_free_transport(struct transport *t)
653 struct repo_commit *rc = NULL, *trc = NULL;
654 struct repo_tag *rt = NULL, *trt = NULL;
656 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
657 TAILQ_REMOVE(&t->repo_commits, rc, entry);
658 gotweb_free_repo_commit(rc);
660 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
661 TAILQ_REMOVE(&t->repo_tags, rt, entry);
662 gotweb_free_repo_tag(rt);
664 gotweb_free_repo_dir(t->repo_dir);
665 gotweb_free_querystring(t->qs);
671 const struct got_error *
672 gotweb_render_content_type(struct request *c, const uint8_t *type)
674 const char *csp = "default-src 'self'; script-src 'none'; "
675 "object-src 'none';";
678 "Content-Security-Policy: %s\r\n"
679 "Content-Type: %s\r\n\r\n",
684 const struct got_error *
685 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
688 fcgi_printf(c, "Content-type: %s\r\n"
689 "Content-disposition: attachment; filename=%s\r\n\r\n",
694 static const struct got_error *
695 gotweb_render_header(struct request *c)
697 const struct got_error *err = NULL;
698 struct server *srv = c->srv;
699 struct querystring *qs = c->t->qs;
702 r = fcgi_printf(c, "<!doctype html>\n"
705 "<title>%s</title>\n"
706 "<meta charset='utf-8' />\n"
707 "<meta name='viewport' content='initial-scale=.75' />\n"
708 "<meta name='msapplication-TileColor' content='#da532c' />\n"
709 "<meta name='theme-color' content='#ffffff'/>\n"
710 "<link rel='apple-touch-icon' sizes='180x180'"
711 " href='%sapple-touch-icon.png' />\n"
712 "<link rel='icon' type='image/png' sizes='32x32'"
713 " href='%sfavicon-32x32.png' />\n"
714 "<link rel='icon' type='image/png' sizes='16x16'"
715 " href='%sfavicon-16x16.png' />\n"
716 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
717 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
718 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
721 "<div id='gw_body'>\n"
722 "<div id='header'>\n"
723 "<div id='got_link'>"
724 "<a href='%s' target='_blank'>"
725 "<img src='%s%s' alt='logo' id='logo' />"
727 "</div>\n" /* #got_link */
728 "</div>\n" /* #header */
729 "<div id='site_path'>\n"
730 "<div id='site_link'>\n"
731 "<a href='?index_page=%d'>%s</a>",
738 c->script_name, srv->custom_css,
740 c->script_name, srv->logo,
741 qs->index_page, srv->site_link);
746 if (qs->path != NULL) {
749 if (fcgi_printf(c, " / ") == -1)
752 err = gotweb_escape_html(&epath, qs->path);
755 r = gotweb_link(c, &(struct gotweb_url){
765 if (qs->action != INDEX) {
766 const char *action = "";
768 switch (qs->action) {
795 if (fcgi_printf(c, " / %s", action) == -1)
800 fcgi_printf(c, "</div>\n" /* #site_path */
801 "</div>\n" /* #site_link */
802 "<div id='content'>\n");
808 static const struct got_error *
809 gotweb_render_footer(struct request *c)
811 const struct got_error *error = NULL;
812 struct server *srv = c->srv;
813 const char *siteowner = " ";
814 char *escaped_owner = NULL;
816 if (srv->show_site_owner) {
817 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
820 siteowner = escaped_owner;
823 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
824 "<div id='site_owner'>%s</div>\n"
825 "</div>\n" /* #site_owner_wrapper */
826 "</div>\n" /* #content */
827 "</div>\n" /* #gw_body */
828 "</body>\n</html>\n", siteowner);
834 static const struct got_error *
835 gotweb_render_navs(struct request *c)
837 const struct got_error *error = NULL;
838 struct transport *t = c->t;
839 struct querystring *qs = t->qs;
840 struct server *srv = c->srv;
843 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
849 if (qs->index_page > 0) {
850 struct gotweb_url url = {
852 .index_page = qs->index_page - 1,
856 r = gotweb_link(c, &url, "Previous");
860 if (t->prev_id && qs->commit != NULL &&
861 strcmp(qs->commit, t->prev_id) != 0) {
862 struct gotweb_url url = {
865 .page = qs->page - 1,
867 .commit = t->prev_id,
868 .headref = qs->headref,
871 r = gotweb_link(c, &url, "Previous");
875 if (t->prev_id && qs->commit != NULL &&
876 strcmp(qs->commit, t->prev_id) != 0) {
877 struct gotweb_url url = {
880 .page = qs->page - 1,
882 .commit = t->prev_id,
883 .headref = qs->headref,
884 .folder = qs->folder,
888 r = gotweb_link(c, &url, "Previous");
892 if (t->prev_id && qs->commit != NULL &&
893 strcmp(qs->commit, t->prev_id) != 0) {
894 struct gotweb_url url = {
897 .page = qs->page - 1,
899 .commit = t->prev_id,
900 .headref = qs->headref,
903 r = gotweb_link(c, &url, "Previous");
911 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
912 "<div id='nav_next'>");
918 if (t->next_disp == srv->max_repos_display &&
919 t->repos_total != (qs->index_page + 1) *
920 srv->max_repos_display) {
921 struct gotweb_url url = {
923 .index_page = qs->index_page + 1,
927 r = gotweb_link(c, &url, "Next");
932 struct gotweb_url url = {
935 .page = qs->page + 1,
937 .commit = t->next_id,
938 .headref = qs->headref,
941 r = gotweb_link(c, &url, "Next");
946 struct gotweb_url url = {
949 .page = qs->page + 1,
951 .commit = t->next_id,
952 .headref = qs->headref,
953 .folder = qs->folder,
957 r = gotweb_link(c, &url, "Next");
962 struct gotweb_url url = {
965 .page = qs->page + 1,
967 .commit = t->next_id,
968 .headref = qs->headref,
971 r = gotweb_link(c, &url, "Next");
978 fcgi_printf(c, "</div>\n"); /* #nav_next */
979 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
988 static const struct got_error *
989 gotweb_render_index(struct request *c)
991 const struct got_error *error = NULL;
992 struct server *srv = c->srv;
993 struct transport *t = c->t;
994 struct querystring *qs = t->qs;
995 struct repo_dir *repo_dir = NULL;
997 struct dirent **sd_dent = NULL;
1000 unsigned int d_cnt, d_i, d_disp = 0;
1003 d = opendir(srv->repos_path);
1005 error = got_error_from_errno2("opendir", srv->repos_path);
1009 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1012 error = got_error_from_errno2("scandir", srv->repos_path);
1016 /* get total count of repos */
1017 for (d_i = 0; d_i < d_cnt; d_i++) {
1018 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1019 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1022 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1023 sd_dent[d_i]->d_name) == -1) {
1024 error = got_error_from_errno("asprintf");
1028 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1029 !got_path_dir_is_empty(c_path))
1035 r = fcgi_printf(c, "<div id='index_header'>\n"
1036 "<div id='index_header_project'>Project</div>\n");
1040 if (srv->show_repo_description)
1041 if (fcgi_printf(c, "<div id='index_header_description'>"
1042 "Description</div>\n") == -1)
1044 if (srv->show_repo_owner)
1045 if (fcgi_printf(c, "<div id='index_header_owner'>"
1046 "Owner</div>\n") == -1)
1048 if (srv->show_repo_age)
1049 if (fcgi_printf(c, "<div id='index_header_age'>"
1050 "Last Change</div>\n") == -1)
1052 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1055 for (d_i = 0; d_i < d_cnt; d_i++) {
1056 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1057 break; /* account for parent and self */
1059 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1060 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1063 if (qs->index_page > 0 && (qs->index_page *
1064 srv->max_repos_display) > t->prev_disp) {
1069 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1073 error = gotweb_load_got_path(c, repo_dir);
1074 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1078 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1081 if (lstat(repo_dir->path, &st) == 0 &&
1082 S_ISDIR(st.st_mode) &&
1083 !got_path_dir_is_empty(repo_dir->path))
1086 gotweb_free_repo_dir(repo_dir);
1094 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1095 "<div class='index_project'>") == -1)
1098 r = gotweb_link(c, &(struct gotweb_url){
1102 .path = repo_dir->name,
1103 }, "%s", repo_dir->name);
1107 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1110 if (srv->show_repo_description) {
1112 "<div class='index_project_description'>\n"
1113 "%s</div>\n", repo_dir->description);
1118 if (srv->show_repo_owner) {
1119 r = fcgi_printf(c, "<div class='index_project_owner'>"
1120 "%s</div>\n", repo_dir->owner);
1125 if (srv->show_repo_age) {
1126 r = fcgi_printf(c, "<div class='index_project_age'>"
1127 "%s</div>\n", repo_dir->age);
1132 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1133 "<div class='navs'>") == -1)
1136 r = gotweb_link(c, &(struct gotweb_url){
1140 .path = repo_dir->name
1145 if (fcgi_printf(c, " | ") == -1)
1148 r = gotweb_link(c, &(struct gotweb_url){
1152 .path = repo_dir->name
1153 }, "commit briefs");
1157 if (fcgi_printf(c, " | ") == -1)
1160 r = gotweb_link(c, &(struct gotweb_url){
1164 .path = repo_dir->name
1169 if (fcgi_printf(c, " | ") == -1)
1172 r = gotweb_link(c, &(struct gotweb_url){
1176 .path = repo_dir->name
1181 if (fcgi_printf(c, " | ") == -1)
1184 r = gotweb_link(c, &(struct gotweb_url){
1188 .path = repo_dir->name
1193 r = fcgi_printf(c, "</div>" /* .navs */
1194 "<div class='dotted_line'></div>\n"
1195 "</div>\n" /* .navs_wrapper */
1196 "</div>\n"); /* .index_wrapper */
1200 gotweb_free_repo_dir(repo_dir);
1203 if (d_disp == srv->max_repos_display)
1206 if (srv->max_repos_display == 0)
1208 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1210 if (t->repos_total <= srv->max_repos ||
1211 t->repos_total <= srv->max_repos_display)
1214 error = gotweb_render_navs(c);
1219 for (d_i = 0; d_i < d_cnt; d_i++)
1223 if (d != NULL && closedir(d) == EOF && error == NULL)
1224 error = got_error_from_errno("closedir");
1228 static const struct got_error *
1229 gotweb_render_blame(struct request *c)
1231 const struct got_error *error = NULL;
1232 struct transport *t = c->t;
1233 struct repo_commit *rc = NULL;
1234 char *age = NULL, *msg = NULL;
1237 error = got_get_repo_commits(c, 1);
1241 rc = TAILQ_FIRST(&t->repo_commits);
1243 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1246 error = gotweb_escape_html(&msg, rc->commit_msg);
1250 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1251 "<div id='blame_title'>Blame</div>\n"
1252 "</div>\n" /* #blame_title_wrapper */
1253 "<div id='blame_content'>\n"
1254 "<div id='blame_header_wrapper'>\n"
1255 "<div id='blame_header'>\n"
1256 "<div class='header_age_title'>Date:</div>\n"
1257 "<div class='header_age'>%s</div>\n"
1258 "<div id='header_commit_msg_title'>Message:</div>\n"
1259 "<div id='header_commit_msg'>%s</div>\n"
1260 "</div>\n" /* #blame_header */
1261 "</div>\n" /* #blame_header_wrapper */
1262 "<div class='dotted_line'></div>\n"
1263 "<div id='blame'>\n",
1269 error = got_output_file_blame(c);
1273 fcgi_printf(c, "</div>\n" /* #blame */
1274 "</div>\n"); /* #blame_content */
1281 static const struct got_error *
1282 gotweb_render_briefs(struct request *c)
1284 const struct got_error *error = NULL;
1285 struct repo_commit *rc = NULL;
1286 struct server *srv = c->srv;
1287 struct transport *t = c->t;
1288 struct querystring *qs = t->qs;
1289 struct repo_dir *repo_dir = t->repo_dir;
1290 char *smallerthan, *newline;
1291 char *age = NULL, *author = NULL, *msg = NULL;
1294 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1295 "<div id='briefs_title'>Commit Briefs</div>\n"
1296 "</div>\n" /* #briefs_title_wrapper */
1297 "<div id='briefs_content'>\n");
1301 if (qs->action == SUMMARY) {
1302 qs->action = BRIEFS;
1303 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1305 error = got_get_repo_commits(c, srv->max_commits_display);
1309 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1310 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1314 smallerthan = strchr(rc->author, '<');
1316 *smallerthan = '\0';
1318 newline = strchr(rc->commit_msg, '\n');
1322 error = gotweb_escape_html(&author, rc->author);
1325 error = gotweb_escape_html(&msg, rc->commit_msg);
1329 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1330 "<div class='briefs_author'>%s</div>\n"
1331 "<div class='briefs_log'>",
1336 r = gotweb_link(c, &(struct gotweb_url){
1340 .path = repo_dir->name,
1341 .commit = rc->commit_id,
1342 .headref = qs->headref,
1350 error = gotweb_escape_html(&refs, rc->refs_str);
1354 " <span class='refs_str'>(%s)</span>", refs);
1359 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1362 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1363 "<div class='navs'>");
1367 r = gotweb_link(c, &(struct gotweb_url){
1371 .path = repo_dir->name,
1372 .commit = rc->commit_id,
1373 .headref = qs->headref,
1378 if (fcgi_printf(c, " | ") == -1)
1381 r = gotweb_link(c, &(struct gotweb_url){
1385 .path = repo_dir->name,
1386 .commit = rc->commit_id,
1387 .headref = qs->headref,
1392 if (fcgi_printf(c, "</div>\n" /* .navs */
1393 "</div>\n" /* .navs_wrapper */
1394 "<div class='dotted_line'></div>\n") == -1)
1405 if (t->next_id || t->prev_id) {
1406 error = gotweb_render_navs(c);
1410 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1418 static const struct got_error *
1419 gotweb_render_commits(struct request *c)
1421 const struct got_error *error = NULL;
1422 struct repo_commit *rc = NULL;
1423 struct server *srv = c->srv;
1424 struct transport *t = c->t;
1425 struct repo_dir *repo_dir = t->repo_dir;
1426 char *age = NULL, *author = NULL, *msg = NULL;
1429 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1430 "<div class='commits_title'>Commits</div>\n"
1431 "</div>\n" /* .commits_title_wrapper */
1432 "<div class='commits_content'>\n");
1436 error = got_get_repo_commits(c, srv->max_commits_display);
1440 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1441 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1444 error = gotweb_escape_html(&author, rc->author);
1447 error = gotweb_escape_html(&msg, rc->commit_msg);
1451 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1452 "<div class='commits_header'>\n"
1453 "<div class='header_commit_title'>Commit:</div>\n"
1454 "<div class='header_commit'>%s</div>\n"
1455 "<div class='header_author_title'>Author:</div>\n"
1456 "<div class='header_author'>%s</div>\n"
1457 "<div class='header_age_title'>Date:</div>\n"
1458 "<div class='header_age'>%s</div>\n"
1459 "</div>\n" /* .commits_header */
1460 "</div>\n" /* .commits_header_wrapper */
1461 "<div class='dotted_line'></div>\n"
1462 "<div class='commit'>\n%s</div>\n",
1470 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1471 "<div class='navs'>") == -1)
1474 r = gotweb_link(c, &(struct gotweb_url){
1478 .path = repo_dir->name,
1479 .commit = rc->commit_id,
1484 if (fcgi_printf(c, " | ") == -1)
1487 r = gotweb_link(c, &(struct gotweb_url){
1491 .path = repo_dir->name,
1492 .commit = rc->commit_id,
1497 if (fcgi_printf(c, "</div>\n" /* .navs */
1498 "</div>\n" /* .navs_wrapper */
1499 "<div class='dotted_line'></div>\n") == -1)
1510 if (t->next_id || t->prev_id) {
1511 error = gotweb_render_navs(c);
1515 fcgi_printf(c, "</div>\n"); /* .commits_content */
1523 static const struct got_error *
1524 gotweb_render_branches(struct request *c)
1526 const struct got_error *error = NULL;
1527 struct got_reflist_head refs;
1528 struct got_reflist_entry *re;
1529 struct transport *t = c->t;
1530 struct querystring *qs = t->qs;
1531 struct got_repository *repo = t->repo;
1532 char *escaped_refname = NULL;
1538 error = got_ref_list(&refs, repo, "refs/heads",
1539 got_ref_cmp_by_name, NULL);
1543 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1544 "<div id='branches_title'>Branches</div>\n"
1545 "</div>\n" /* #branches_title_wrapper */
1546 "<div id='branches_content'>\n");
1550 TAILQ_FOREACH(re, &refs, entry) {
1551 const char *refname = NULL;
1553 if (got_ref_is_symbolic(re->ref))
1556 refname = got_ref_get_name(re->ref);
1557 if (refname == NULL) {
1558 error = got_error_from_errno("strdup");
1561 if (strncmp(refname, "refs/heads/", 11) != 0)
1564 error = got_get_repo_age(&age, c, qs->path, refname,
1569 if (strncmp(refname, "refs/heads/", 11) == 0)
1571 error = gotweb_escape_html(&escaped_refname, refname);
1575 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1576 "<div class='branches_age'>%s</div>\n"
1577 "<div class='branches_space'> </div>\n"
1578 "<div class='branch'>", age);
1582 r = gotweb_link(c, &(struct gotweb_url){
1588 }, "%s", escaped_refname);
1592 if (fcgi_printf(c, "</div>\n" /* .branch */
1593 "<div class='navs_wrapper'>\n"
1594 "<div class='navs'>") == -1)
1597 r = gotweb_link(c, &(struct gotweb_url){
1607 if (fcgi_printf(c, " | ") == -1)
1610 r = gotweb_link(c, &(struct gotweb_url){
1616 }, "commit briefs");
1620 if (fcgi_printf(c, " | ") == -1)
1623 r = gotweb_link(c, &(struct gotweb_url){
1633 r = fcgi_printf(c, "</div>\n" /* .navs */
1634 "</div>\n" /* .navs_wrapper */
1635 "<div class='dotted_line'></div>\n"
1636 "</div>\n"); /* .branches_wrapper */
1642 free(escaped_refname);
1643 escaped_refname = NULL;
1645 fcgi_printf(c, "</div>\n"); /* #branches_content */
1648 free(escaped_refname);
1649 got_ref_list_free(&refs);
1653 static const struct got_error *
1654 gotweb_render_tree(struct request *c)
1656 const struct got_error *error = NULL;
1657 struct transport *t = c->t;
1658 struct repo_commit *rc = NULL;
1659 char *age = NULL, *msg = NULL;
1662 error = got_get_repo_commits(c, 1);
1666 rc = TAILQ_FIRST(&t->repo_commits);
1668 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1672 error = gotweb_escape_html(&msg, rc->commit_msg);
1676 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1677 "<div id='tree_title'>Tree</div>\n"
1678 "</div>\n" /* #tree_title_wrapper */
1679 "<div id='tree_content'>\n"
1680 "<div id='tree_header_wrapper'>\n"
1681 "<div id='tree_header'>\n"
1682 "<div id='header_tree_title'>Tree:</div>\n"
1683 "<div id='header_tree'>%s</div>\n"
1684 "<div class='header_age_title'>Date:</div>\n"
1685 "<div class='header_age'>%s</div>\n"
1686 "<div id='header_commit_msg_title'>Message:</div>\n"
1687 "<div id='header_commit_msg'>%s</div>\n"
1688 "</div>\n" /* #tree_header */
1689 "</div>\n" /* #tree_header_wrapper */
1690 "<div class='dotted_line'></div>\n"
1691 "<div id='tree'>\n",
1698 error = got_output_repo_tree(c);
1702 fcgi_printf(c, "</div>\n"); /* #tree */
1703 fcgi_printf(c, "</div>\n"); /* #tree_content */
1710 static const struct got_error *
1711 gotweb_render_diff(struct request *c)
1713 const struct got_error *error = NULL;
1714 struct transport *t = c->t;
1715 struct repo_commit *rc = NULL;
1716 char *age = NULL, *author = NULL, *msg = NULL;
1719 error = got_get_repo_commits(c, 1);
1723 rc = TAILQ_FIRST(&t->repo_commits);
1725 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1728 error = gotweb_escape_html(&author, rc->author);
1731 error = gotweb_escape_html(&msg, rc->commit_msg);
1735 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1736 "<div id='diff_title'>Commit Diff</div>\n"
1737 "</div>\n" /* #diff_title_wrapper */
1738 "<div id='diff_content'>\n"
1739 "<div id='diff_header_wrapper'>\n"
1740 "<div id='diff_header'>\n"
1741 "<div id='header_diff_title'>Diff:</div>\n"
1742 "<div id='header_diff'>%s<br />%s</div>\n"
1743 "<div class='header_commit_title'>Commit:</div>\n"
1744 "<div class='header_commit'>%s</div>\n"
1745 "<div id='header_tree_title'>Tree:</div>\n"
1746 "<div id='header_tree'>%s</div>\n"
1747 "<div class='header_author_title'>Author:</div>\n"
1748 "<div class='header_author'>%s</div>\n"
1749 "<div class='header_age_title'>Date:</div>\n"
1750 "<div class='header_age'>%s</div>\n"
1751 "<div id='header_commit_msg_title'>Message:</div>\n"
1752 "<div id='header_commit_msg'>%s</div>\n"
1753 "</div>\n" /* #diff_header */
1754 "</div>\n" /* #diff_header_wrapper */
1755 "<div class='dotted_line'></div>\n"
1756 "<div id='diff'>\n",
1757 rc->parent_id, rc->commit_id,
1766 error = got_output_repo_diff(c);
1770 fcgi_printf(c, "</div>\n"); /* #diff */
1771 fcgi_printf(c, "</div>\n"); /* #diff_content */
1779 static const struct got_error *
1780 gotweb_render_summary(struct request *c)
1782 const struct got_error *error = NULL;
1783 struct transport *t = c->t;
1784 struct server *srv = c->srv;
1787 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1790 if (srv->show_repo_description) {
1792 "<div id='description_title'>Description:</div>\n"
1793 "<div id='description'>%s</div>\n",
1794 t->repo_dir->description ? t->repo_dir->description : "");
1799 if (srv->show_repo_owner) {
1801 "<div id='repo_owner_title'>Owner:</div>\n"
1802 "<div id='repo_owner'>%s</div>\n",
1803 t->repo_dir->owner ? t->repo_dir->owner : "");
1808 if (srv->show_repo_age) {
1810 "<div id='last_change_title'>Last Change:</div>\n"
1811 "<div id='last_change'>%s</div>\n",
1817 if (srv->show_repo_cloneurl) {
1819 "<div id='cloneurl_title'>Clone URL:</div>\n"
1820 "<div id='cloneurl'>%s</div>\n",
1821 t->repo_dir->url ? t->repo_dir->url : "");
1826 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1830 error = gotweb_render_briefs(c);
1832 log_warnx("%s: %s", __func__, error->msg);
1836 error = gotweb_render_tags(c);
1838 log_warnx("%s: %s", __func__, error->msg);
1842 error = gotweb_render_branches(c);
1844 log_warnx("%s: %s", __func__, error->msg);
1849 static const struct got_error *
1850 gotweb_render_tag(struct request *c)
1852 const struct got_error *error = NULL;
1853 struct repo_tag *rt = NULL;
1854 struct transport *t = c->t;
1855 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1857 error = got_get_repo_tags(c, 1);
1861 if (t->tag_count == 0) {
1862 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1867 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1869 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1872 error = gotweb_escape_html(&author, rt->tagger);
1875 error = gotweb_escape_html(&msg, rt->commit_msg);
1879 tagname = rt->tag_name;
1880 if (strncmp(tagname, "refs/", 5) == 0)
1882 error = gotweb_escape_html(&tagname, tagname);
1886 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1887 "<div id='tags_title'>Tag</div>\n"
1888 "</div>\n" /* #tags_title_wrapper */
1889 "<div id='tags_content'>\n"
1890 "<div id='tag_header_wrapper'>\n"
1891 "<div id='tag_header'>\n"
1892 "<div class='header_commit_title'>Commit:</div>\n"
1893 "<div class='header_commit'>%s"
1894 " <span class='refs_str'>(%s)</span></div>\n"
1895 "<div class='header_author_title'>Tagger:</div>\n"
1896 "<div class='header_author'>%s</div>\n"
1897 "<div class='header_age_title'>Date:</div>\n"
1898 "<div class='header_age'>%s</div>\n"
1899 "<div id='header_commit_msg_title'>Message:</div>\n"
1900 "<div id='header_commit_msg'>%s</div>\n"
1901 "</div>\n" /* #tag_header */
1902 "<div class='dotted_line'></div>\n"
1903 "<div id='tag_commit'>\n%s</div>"
1904 "</div>" /* #tag_header_wrapper */
1905 "</div>", /* #tags_content */
1920 static const struct got_error *
1921 gotweb_render_tags(struct request *c)
1923 const struct got_error *error = NULL;
1924 struct repo_tag *rt = NULL;
1925 struct server *srv = c->srv;
1926 struct transport *t = c->t;
1927 struct querystring *qs = t->qs;
1928 struct repo_dir *repo_dir = t->repo_dir;
1929 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1930 int r, commit_found = 0;
1932 if (qs->action == BRIEFS) {
1934 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1936 error = got_get_repo_tags(c, srv->max_commits_display);
1940 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1941 "<div id='tags_title'>Tags</div>\n"
1942 "</div>\n" /* #tags_title_wrapper */
1943 "<div id='tags_content'>\n");
1947 if (t->tag_count == 0) {
1948 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1949 "This repository contains no tags");
1954 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1955 if (commit_found == 0 && qs->commit != NULL) {
1956 if (strcmp(qs->commit, rt->commit_id) != 0)
1961 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1965 tagname = rt->tag_name;
1966 if (strncmp(tagname, "refs/tags/", 10) == 0)
1968 error = gotweb_escape_html(&tagname, tagname);
1972 if (rt->tag_commit != NULL) {
1973 newline = strchr(rt->tag_commit, '\n');
1976 error = gotweb_escape_html(&msg, rt->tag_commit);
1981 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1982 "<div class='tag'>%s</div>\n"
1983 "<div class='tag_log'>", age, tagname) == -1)
1986 r = gotweb_link(c, &(struct gotweb_url){
1990 .path = repo_dir->name,
1991 .commit = rt->commit_id,
1992 }, "%s", msg ? msg : "");
1996 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1997 "<div class='navs_wrapper'>\n"
1998 "<div class='navs'>") == -1)
2001 r = gotweb_link(c, &(struct gotweb_url){
2005 .path = repo_dir->name,
2006 .commit = rt->commit_id,
2011 if (fcgi_printf(c, " | ") == -1)
2014 r = gotweb_link(c, &(struct gotweb_url){
2018 .path = repo_dir->name,
2019 .commit = rt->commit_id,
2020 }, "commit briefs");
2024 if (fcgi_printf(c, " | ") == -1)
2027 r = gotweb_link(c, &(struct gotweb_url){
2031 .path = repo_dir->name,
2032 .commit = rt->commit_id,
2038 "</div>\n" /* .navs */
2039 "</div>\n" /* .navs_wrapper */
2040 "<div class='dotted_line'></div>\n");
2051 if (t->next_id || t->prev_id) {
2052 error = gotweb_render_navs(c);
2056 fcgi_printf(c, "</div>\n"); /* #tags_content */
2064 const struct got_error *
2065 gotweb_escape_html(char **escaped_html, const char *orig_html)
2067 const struct got_error *error = NULL;
2068 struct escape_pair {
2079 size_t orig_len, len;
2082 orig_len = strlen(orig_html);
2084 for (i = 0; i < orig_len; i++) {
2085 for (j = 0; j < nitems(esc); j++) {
2086 if (orig_html[i] != esc[j].c)
2088 len += strlen(esc[j].s) - 1 /* escaped char */;
2092 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2093 if (*escaped_html == NULL)
2094 return got_error_from_errno("calloc");
2097 for (i = 0; i < orig_len; i++) {
2099 for (j = 0; j < nitems(esc); j++) {
2100 if (orig_html[i] != esc[j].c)
2103 if (strlcat(*escaped_html, esc[j].s, len + 1)
2105 error = got_error(GOT_ERR_NO_SPACE);
2108 x += strlen(esc[j].s);
2113 (*escaped_html)[x] = orig_html[i];
2119 free(*escaped_html);
2120 *escaped_html = NULL;
2122 (*escaped_html)[x] = '\0';
2129 should_urlencode(int c)
2131 if (c <= ' ' || c >= 127)
2162 gotweb_urlencode(const char *str)
2170 for (s = str; *s; ++s) {
2172 if (should_urlencode(*s))
2176 escaped = calloc(1, len + 1);
2177 if (escaped == NULL)
2181 for (s = str; *s; ++s) {
2182 if (should_urlencode(*s)) {
2183 a = (*s & 0xF0) >> 4;
2187 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2188 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2196 static inline const char *
2197 action_name(int action)
2228 gotweb_print_url(struct request *c, struct gotweb_url *url)
2230 const char *sep = "?", *action;
2234 action = action_name(url->action);
2235 if (action != NULL) {
2236 if (fcgi_printf(c, "?action=%s", action) == -1)
2242 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2248 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2254 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2260 tmp = gotweb_urlencode(url->file);
2263 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2271 tmp = gotweb_urlencode(url->folder);
2274 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2282 tmp = gotweb_urlencode(url->headref);
2285 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2292 if (url->index_page != -1) {
2293 if (fcgi_printf(c, "%sindex_page=%d", sep,
2294 url->index_page) == -1)
2300 tmp = gotweb_urlencode(url->path);
2303 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2310 if (url->page != -1) {
2311 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2320 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2325 if (fcgi_printf(c, "<a href='") == -1)
2328 if (gotweb_print_url(c, url) == -1)
2331 if (fcgi_printf(c, "'>") == -1)
2335 r = fcgi_vprintf(c, fmt, ap);
2340 if (fcgi_printf(c, "</a>"))
2345 static struct got_repository *
2346 find_cached_repo(struct server *srv, const char *path)
2350 for (i = 0; i < srv->ncached_repos; i++) {
2351 if (strcmp(srv->cached_repos[i].path, path) == 0)
2352 return srv->cached_repos[i].repo;
2358 static const struct got_error *
2359 cache_repo(struct got_repository **new, struct server *srv,
2360 struct repo_dir *repo_dir, struct socket *sock)
2362 const struct got_error *error = NULL;
2363 struct got_repository *repo;
2364 struct cached_repo *cr;
2367 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2368 cr = &srv->cached_repos[srv->ncached_repos - 1];
2369 error = got_repo_close(cr->repo);
2370 memset(cr, 0, sizeof(*cr));
2371 srv->ncached_repos--;
2374 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2375 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2376 cr = &srv->cached_repos[0];
2379 cr = &srv->cached_repos[srv->ncached_repos];
2382 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2385 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2386 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2391 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2392 >= sizeof(cr->path)) {
2394 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2395 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2397 return got_error(GOT_ERR_NO_SPACE);
2401 srv->ncached_repos++;
2406 static const struct got_error *
2407 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2409 const struct got_error *error = NULL;
2410 struct socket *sock = c->sock;
2411 struct server *srv = c->srv;
2412 struct transport *t = c->t;
2413 struct got_repository *repo = NULL;
2417 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2418 GOTWEB_GIT_DIR) == -1)
2419 return got_error_from_errno("asprintf");
2421 dt = opendir(dir_test);
2425 repo_dir->path = dir_test;
2430 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2431 repo_dir->name) == -1)
2432 return got_error_from_errno("asprintf");
2434 dt = opendir(dir_test);
2436 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2439 repo_dir->path = dir_test;
2444 repo = find_cached_repo(srv, repo_dir->path);
2446 error = cache_repo(&repo, srv, repo_dir, sock);
2451 error = gotweb_get_repo_description(&repo_dir->description, srv,
2455 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2458 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2462 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2465 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2466 error = got_error_from_errno("closedir");
2470 static const struct got_error *
2471 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2473 const struct got_error *error;
2475 *repo_dir = calloc(1, sizeof(**repo_dir));
2476 if (*repo_dir == NULL)
2477 return got_error_from_errno("calloc");
2479 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2480 error = got_error_from_errno("asprintf");
2485 (*repo_dir)->owner = NULL;
2486 (*repo_dir)->description = NULL;
2487 (*repo_dir)->url = NULL;
2488 (*repo_dir)->age = NULL;
2489 (*repo_dir)->path = NULL;
2494 static const struct got_error *
2495 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2497 const struct got_error *error = NULL;
2499 char *d_file = NULL;
2503 *description = NULL;
2504 if (srv->show_repo_description == 0)
2507 if (asprintf(&d_file, "%s/description", dir) == -1)
2508 return got_error_from_errno("asprintf");
2510 f = fopen(d_file, "r");
2512 if (errno != ENOENT && errno != EACCES)
2513 error = got_error_from_errno2("fopen", d_file);
2517 if (fseek(f, 0, SEEK_END) == -1) {
2518 error = got_ferror(f, GOT_ERR_IO);
2523 error = got_ferror(f, GOT_ERR_IO);
2528 *description = strdup("");
2529 if (*description == NULL)
2530 error = got_error_from_errno("strdup");
2534 if (fseek(f, 0, SEEK_SET) == -1) {
2535 error = got_ferror(f, GOT_ERR_IO);
2538 *description = calloc(len + 1, sizeof(**description));
2539 if (*description == NULL) {
2540 error = got_error_from_errno("calloc");
2544 n = fread(*description, 1, len, f);
2545 if (n == 0 && ferror(f))
2546 error = got_ferror(f, GOT_ERR_IO);
2548 if (f != NULL && fclose(f) == EOF && error == NULL)
2549 error = got_error_from_errno("fclose");
2554 static const struct got_error *
2555 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2557 const struct got_error *error = NULL;
2559 char *d_file = NULL;
2565 if (srv->show_repo_cloneurl == 0)
2568 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2569 return got_error_from_errno("asprintf");
2571 f = fopen(d_file, "r");
2573 if (errno != ENOENT && errno != EACCES)
2574 error = got_error_from_errno2("fopen", d_file);
2578 if (fseek(f, 0, SEEK_END) == -1) {
2579 error = got_ferror(f, GOT_ERR_IO);
2584 error = got_ferror(f, GOT_ERR_IO);
2590 if (fseek(f, 0, SEEK_SET) == -1) {
2591 error = got_ferror(f, GOT_ERR_IO);
2595 *url = calloc(len + 1, sizeof(**url));
2597 error = got_error_from_errno("calloc");
2601 n = fread(*url, 1, len, f);
2602 if (n == 0 && ferror(f))
2603 error = got_ferror(f, GOT_ERR_IO);
2605 if (f != NULL && fclose(f) == EOF && error == NULL)
2606 error = got_error_from_errno("fclose");
2611 const struct got_error *
2612 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2615 long long diff_time;
2616 const char *years = "years ago", *months = "months ago";
2617 const char *weeks = "weeks ago", *days = "days ago";
2618 const char *hours = "hours ago", *minutes = "minutes ago";
2619 const char *seconds = "seconds ago", *now = "right now";
2627 diff_time = time(NULL) - committer_time;
2628 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2629 if (asprintf(repo_age, "%lld %s",
2630 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2631 return got_error_from_errno("asprintf");
2632 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2633 if (asprintf(repo_age, "%lld %s",
2634 (diff_time / 60 / 60 / 24 / (365 / 12)),
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2638 if (asprintf(repo_age, "%lld %s",
2639 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2640 return got_error_from_errno("asprintf");
2641 } else if (diff_time > 60 * 60 * 24 * 2) {
2642 if (asprintf(repo_age, "%lld %s",
2643 (diff_time / 60 / 60 / 24), days) == -1)
2644 return got_error_from_errno("asprintf");
2645 } else if (diff_time > 60 * 60 * 2) {
2646 if (asprintf(repo_age, "%lld %s",
2647 (diff_time / 60 / 60), hours) == -1)
2648 return got_error_from_errno("asprintf");
2649 } else if (diff_time > 60 * 2) {
2650 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2652 return got_error_from_errno("asprintf");
2653 } else if (diff_time > 2) {
2654 if (asprintf(repo_age, "%lld %s", diff_time,
2656 return got_error_from_errno("asprintf");
2658 if (asprintf(repo_age, "%s", now) == -1)
2659 return got_error_from_errno("asprintf");
2663 if (gmtime_r(&committer_time, &tm) == NULL)
2664 return got_error_from_errno("gmtime_r");
2666 s = asctime_r(&tm, datebuf);
2668 return got_error_from_errno("asctime_r");
2670 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2671 return got_error_from_errno("asprintf");