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, "\n%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);
745 if (qs->path != NULL) {
748 if (fcgi_printf(c, " / ") == -1)
751 err = gotweb_escape_html(&epath, qs->path);
754 r = gotweb_link(c, &(struct gotweb_url){
764 if (qs->action != INDEX) {
765 const char *action = "";
767 switch (qs->action) {
794 if (fcgi_printf(c, " / %s", action) == -1)
798 fcgi_printf(c, "</div>\n" /* #site_path */
799 "</div>\n" /* #site_link */
800 "<div id='content'>\n");
806 static const struct got_error *
807 gotweb_render_footer(struct request *c)
809 const struct got_error *error = NULL;
810 struct server *srv = c->srv;
811 const char *siteowner = " ";
812 char *escaped_owner = NULL;
814 if (srv->show_site_owner) {
815 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
818 siteowner = escaped_owner;
821 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
822 "<div id='site_owner'>%s</div>\n"
823 "</div>\n" /* #site_owner_wrapper */
824 "</div>\n" /* #content */
825 "</div>\n" /* #gw_body */
826 "</body>\n</html>\n", siteowner);
832 static const struct got_error *
833 gotweb_render_navs(struct request *c)
835 const struct got_error *error = NULL;
836 struct transport *t = c->t;
837 struct querystring *qs = t->qs;
838 struct server *srv = c->srv;
841 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
847 if (qs->index_page > 0) {
848 struct gotweb_url url = {
850 .index_page = qs->index_page - 1,
854 r = gotweb_link(c, &url, "Previous");
858 if (t->prev_id && qs->commit != NULL &&
859 strcmp(qs->commit, t->prev_id) != 0) {
860 struct gotweb_url url = {
863 .page = qs->page - 1,
865 .commit = t->prev_id,
866 .headref = qs->headref,
869 r = gotweb_link(c, &url, "Previous");
873 if (t->prev_id && qs->commit != NULL &&
874 strcmp(qs->commit, t->prev_id) != 0) {
875 struct gotweb_url url = {
878 .page = qs->page - 1,
880 .commit = t->prev_id,
881 .headref = qs->headref,
882 .folder = qs->folder,
886 r = gotweb_link(c, &url, "Previous");
890 if (t->prev_id && qs->commit != NULL &&
891 strcmp(qs->commit, t->prev_id) != 0) {
892 struct gotweb_url url = {
895 .page = qs->page - 1,
897 .commit = t->prev_id,
898 .headref = qs->headref,
901 r = gotweb_link(c, &url, "Previous");
909 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
910 "<div id='nav_next'>");
916 if (t->next_disp == srv->max_repos_display &&
917 t->repos_total != (qs->index_page + 1) *
918 srv->max_repos_display) {
919 struct gotweb_url url = {
921 .index_page = qs->index_page + 1,
925 r = gotweb_link(c, &url, "Next");
930 struct gotweb_url url = {
933 .page = qs->page + 1,
935 .commit = t->next_id,
936 .headref = qs->headref,
939 r = gotweb_link(c, &url, "Next");
944 struct gotweb_url url = {
947 .page = qs->page + 1,
949 .commit = t->next_id,
950 .headref = qs->headref,
951 .folder = qs->folder,
955 r = gotweb_link(c, &url, "Next");
960 struct gotweb_url url = {
963 .page = qs->page + 1,
965 .commit = t->next_id,
966 .headref = qs->headref,
969 r = gotweb_link(c, &url, "Next");
976 fcgi_printf(c, "</div>\n"); /* #nav_next */
977 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
986 static const struct got_error *
987 gotweb_render_index(struct request *c)
989 const struct got_error *error = NULL;
990 struct server *srv = c->srv;
991 struct transport *t = c->t;
992 struct querystring *qs = t->qs;
993 struct repo_dir *repo_dir = NULL;
995 struct dirent **sd_dent = NULL;
998 unsigned int d_cnt, d_i, d_disp = 0;
1001 d = opendir(srv->repos_path);
1003 error = got_error_from_errno2("opendir", srv->repos_path);
1007 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1010 error = got_error_from_errno2("scandir", srv->repos_path);
1014 /* get total count of repos */
1015 for (d_i = 0; d_i < d_cnt; d_i++) {
1016 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1017 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1020 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1021 sd_dent[d_i]->d_name) == -1) {
1022 error = got_error_from_errno("asprintf");
1026 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1027 !got_path_dir_is_empty(c_path))
1033 r = fcgi_printf(c, "<div id='index_header'>\n"
1034 "<div id='index_header_project'>Project</div>\n");
1038 if (srv->show_repo_description)
1039 if (fcgi_printf(c, "<div id='index_header_description'>"
1040 "Description</div>\n") == -1)
1042 if (srv->show_repo_owner)
1043 if (fcgi_printf(c, "<div id='index_header_owner'>"
1044 "Owner</div>\n") == -1)
1046 if (srv->show_repo_age)
1047 if (fcgi_printf(c, "<div id='index_header_age'>"
1048 "Last Change</div>\n") == -1)
1050 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1053 for (d_i = 0; d_i < d_cnt; d_i++) {
1054 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1055 break; /* account for parent and self */
1057 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1058 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1061 if (qs->index_page > 0 && (qs->index_page *
1062 srv->max_repos_display) > t->prev_disp) {
1067 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1071 error = gotweb_load_got_path(c, repo_dir);
1072 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1076 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1079 if (lstat(repo_dir->path, &st) == 0 &&
1080 S_ISDIR(st.st_mode) &&
1081 !got_path_dir_is_empty(repo_dir->path))
1084 gotweb_free_repo_dir(repo_dir);
1092 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1093 "<div class='index_project'>") == -1)
1096 r = gotweb_link(c, &(struct gotweb_url){
1100 .path = repo_dir->name,
1101 }, "%s", repo_dir->name);
1105 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1108 if (srv->show_repo_description) {
1110 "<div class='index_project_description'>\n"
1111 "%s</div>\n", repo_dir->description);
1116 if (srv->show_repo_owner) {
1117 r = fcgi_printf(c, "<div class='index_project_owner'>"
1118 "%s</div>\n", repo_dir->owner);
1123 if (srv->show_repo_age) {
1124 r = fcgi_printf(c, "<div class='index_project_age'>"
1125 "%s</div>\n", repo_dir->age);
1130 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1131 "<div class='navs'>") == -1)
1134 r = gotweb_link(c, &(struct gotweb_url){
1138 .path = repo_dir->name
1143 if (fcgi_printf(c, " | ") == -1)
1146 r = gotweb_link(c, &(struct gotweb_url){
1150 .path = repo_dir->name
1151 }, "commit briefs");
1155 if (fcgi_printf(c, " | ") == -1)
1158 r = gotweb_link(c, &(struct gotweb_url){
1162 .path = repo_dir->name
1167 if (fcgi_printf(c, " | ") == -1)
1170 r = gotweb_link(c, &(struct gotweb_url){
1174 .path = repo_dir->name
1179 if (fcgi_printf(c, " | ") == -1)
1182 r = gotweb_link(c, &(struct gotweb_url){
1186 .path = repo_dir->name
1191 r = fcgi_printf(c, "</div>" /* .navs */
1192 "<div class='dotted_line'></div>\n"
1193 "</div>\n" /* .navs_wrapper */
1194 "</div>\n"); /* .index_wrapper */
1198 gotweb_free_repo_dir(repo_dir);
1201 if (d_disp == srv->max_repos_display)
1204 if (srv->max_repos_display == 0)
1206 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1208 if (t->repos_total <= srv->max_repos ||
1209 t->repos_total <= srv->max_repos_display)
1212 error = gotweb_render_navs(c);
1217 for (d_i = 0; d_i < d_cnt; d_i++)
1221 if (d != NULL && closedir(d) == EOF && error == NULL)
1222 error = got_error_from_errno("closedir");
1226 static const struct got_error *
1227 gotweb_render_blame(struct request *c)
1229 const struct got_error *error = NULL;
1230 struct transport *t = c->t;
1231 struct repo_commit *rc = NULL;
1232 char *age = NULL, *msg = NULL;
1235 error = got_get_repo_commits(c, 1);
1239 rc = TAILQ_FIRST(&t->repo_commits);
1241 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1244 error = gotweb_escape_html(&msg, rc->commit_msg);
1248 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1249 "<div id='blame_title'>Blame</div>\n"
1250 "</div>\n" /* #blame_title_wrapper */
1251 "<div id='blame_content'>\n"
1252 "<div id='blame_header_wrapper'>\n"
1253 "<div id='blame_header'>\n"
1254 "<div class='header_age_title'>Date:</div>\n"
1255 "<div class='header_age'>%s</div>\n"
1256 "<div id='header_commit_msg_title'>Message:</div>\n"
1257 "<div id='header_commit_msg'>%s</div>\n"
1258 "</div>\n" /* #blame_header */
1259 "</div>\n" /* #blame_header_wrapper */
1260 "<div class='dotted_line'></div>\n"
1261 "<div id='blame'>\n",
1267 error = got_output_file_blame(c);
1271 fcgi_printf(c, "</div>\n" /* #blame */
1272 "</div>\n"); /* #blame_content */
1279 static const struct got_error *
1280 gotweb_render_briefs(struct request *c)
1282 const struct got_error *error = NULL;
1283 struct repo_commit *rc = NULL;
1284 struct server *srv = c->srv;
1285 struct transport *t = c->t;
1286 struct querystring *qs = t->qs;
1287 struct repo_dir *repo_dir = t->repo_dir;
1288 char *smallerthan, *newline;
1289 char *age = NULL, *author = NULL, *msg = NULL;
1292 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1293 "<div id='briefs_title'>Commit Briefs</div>\n"
1294 "</div>\n" /* #briefs_title_wrapper */
1295 "<div id='briefs_content'>\n");
1299 if (qs->action == SUMMARY) {
1300 qs->action = BRIEFS;
1301 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1303 error = got_get_repo_commits(c, srv->max_commits_display);
1307 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1308 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1312 smallerthan = strchr(rc->author, '<');
1314 *smallerthan = '\0';
1316 newline = strchr(rc->commit_msg, '\n');
1320 error = gotweb_escape_html(&author, rc->author);
1323 error = gotweb_escape_html(&msg, rc->commit_msg);
1327 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1328 "<div class='briefs_author'>%s</div>\n"
1329 "<div class='briefs_log'>",
1334 r = gotweb_link(c, &(struct gotweb_url){
1338 .path = repo_dir->name,
1339 .commit = rc->commit_id,
1340 .headref = qs->headref,
1348 error = gotweb_escape_html(&refs, rc->refs_str);
1352 " <span class='refs_str'>(%s)</span>", refs);
1357 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1360 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1361 "<div class='navs'>");
1365 r = gotweb_link(c, &(struct gotweb_url){
1369 .path = repo_dir->name,
1370 .commit = rc->commit_id,
1371 .headref = qs->headref,
1376 if (fcgi_printf(c, " | ") == -1)
1379 r = gotweb_link(c, &(struct gotweb_url){
1383 .path = repo_dir->name,
1384 .commit = rc->commit_id,
1385 .headref = qs->headref,
1390 if (fcgi_printf(c, "</div>\n" /* .navs */
1391 "</div>\n" /* .navs_wrapper */
1392 "<div class='dotted_line'></div>\n") == -1)
1403 if (t->next_id || t->prev_id) {
1404 error = gotweb_render_navs(c);
1408 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1416 static const struct got_error *
1417 gotweb_render_commits(struct request *c)
1419 const struct got_error *error = NULL;
1420 struct repo_commit *rc = NULL;
1421 struct server *srv = c->srv;
1422 struct transport *t = c->t;
1423 struct repo_dir *repo_dir = t->repo_dir;
1424 char *age = NULL, *author = NULL, *msg = NULL;
1427 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1428 "<div class='commits_title'>Commits</div>\n"
1429 "</div>\n" /* .commits_title_wrapper */
1430 "<div class='commits_content'>\n");
1434 error = got_get_repo_commits(c, srv->max_commits_display);
1438 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1439 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1442 error = gotweb_escape_html(&author, rc->author);
1445 error = gotweb_escape_html(&msg, rc->commit_msg);
1449 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1450 "<div class='commits_header'>\n"
1451 "<div class='header_commit_title'>Commit:</div>\n"
1452 "<div class='header_commit'>%s</div>\n"
1453 "<div class='header_author_title'>Author:</div>\n"
1454 "<div class='header_author'>%s</div>\n"
1455 "<div class='header_age_title'>Date:</div>\n"
1456 "<div class='header_age'>%s</div>\n"
1457 "</div>\n" /* .commits_header */
1458 "</div>\n" /* .commits_header_wrapper */
1459 "<div class='dotted_line'></div>\n"
1460 "<div class='commit'>\n%s</div>\n",
1468 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1469 "<div class='navs'>") == -1)
1472 r = gotweb_link(c, &(struct gotweb_url){
1476 .path = repo_dir->name,
1477 .commit = rc->commit_id,
1482 if (fcgi_printf(c, " | ") == -1)
1485 r = gotweb_link(c, &(struct gotweb_url){
1489 .path = repo_dir->name,
1490 .commit = rc->commit_id,
1495 if (fcgi_printf(c, "</div>\n" /* .navs */
1496 "</div>\n" /* .navs_wrapper */
1497 "<div class='dotted_line'></div>\n") == -1)
1508 if (t->next_id || t->prev_id) {
1509 error = gotweb_render_navs(c);
1513 fcgi_printf(c, "</div>\n"); /* .commits_content */
1521 static const struct got_error *
1522 gotweb_render_branches(struct request *c)
1524 const struct got_error *error = NULL;
1525 struct got_reflist_head refs;
1526 struct got_reflist_entry *re;
1527 struct transport *t = c->t;
1528 struct querystring *qs = t->qs;
1529 struct got_repository *repo = t->repo;
1530 char *escaped_refname = NULL;
1536 error = got_ref_list(&refs, repo, "refs/heads",
1537 got_ref_cmp_by_name, NULL);
1541 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1542 "<div id='branches_title'>Branches</div>\n"
1543 "</div>\n" /* #branches_title_wrapper */
1544 "<div id='branches_content'>\n");
1548 TAILQ_FOREACH(re, &refs, entry) {
1549 const char *refname = NULL;
1551 if (got_ref_is_symbolic(re->ref))
1554 refname = got_ref_get_name(re->ref);
1555 if (refname == NULL) {
1556 error = got_error_from_errno("strdup");
1559 if (strncmp(refname, "refs/heads/", 11) != 0)
1562 error = got_get_repo_age(&age, c, qs->path, refname,
1567 if (strncmp(refname, "refs/heads/", 11) == 0)
1569 error = gotweb_escape_html(&escaped_refname, refname);
1573 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1574 "<div class='branches_age'>%s</div>\n"
1575 "<div class='branches_space'> </div>\n"
1576 "<div class='branch'>", age);
1580 r = gotweb_link(c, &(struct gotweb_url){
1586 }, "%s", escaped_refname);
1590 if (fcgi_printf(c, "</div>\n" /* .branch */
1591 "<div class='navs_wrapper'>\n"
1592 "<div class='navs'>") == -1)
1595 r = gotweb_link(c, &(struct gotweb_url){
1605 if (fcgi_printf(c, " | ") == -1)
1608 r = gotweb_link(c, &(struct gotweb_url){
1614 }, "commit briefs");
1618 if (fcgi_printf(c, " | ") == -1)
1621 r = gotweb_link(c, &(struct gotweb_url){
1631 r = fcgi_printf(c, "</div>\n" /* .navs */
1632 "</div>\n" /* .navs_wrapper */
1633 "<div class='dotted_line'></div>\n"
1634 "</div>\n"); /* .branches_wrapper */
1640 free(escaped_refname);
1641 escaped_refname = NULL;
1643 fcgi_printf(c, "</div>\n"); /* #branches_content */
1646 free(escaped_refname);
1647 got_ref_list_free(&refs);
1651 static const struct got_error *
1652 gotweb_render_tree(struct request *c)
1654 const struct got_error *error = NULL;
1655 struct transport *t = c->t;
1656 struct repo_commit *rc = NULL;
1657 char *age = NULL, *msg = NULL;
1660 error = got_get_repo_commits(c, 1);
1664 rc = TAILQ_FIRST(&t->repo_commits);
1666 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1670 error = gotweb_escape_html(&msg, rc->commit_msg);
1674 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1675 "<div id='tree_title'>Tree</div>\n"
1676 "</div>\n" /* #tree_title_wrapper */
1677 "<div id='tree_content'>\n"
1678 "<div id='tree_header_wrapper'>\n"
1679 "<div id='tree_header'>\n"
1680 "<div id='header_tree_title'>Tree:</div>\n"
1681 "<div id='header_tree'>%s</div>\n"
1682 "<div class='header_age_title'>Date:</div>\n"
1683 "<div class='header_age'>%s</div>\n"
1684 "<div id='header_commit_msg_title'>Message:</div>\n"
1685 "<div id='header_commit_msg'>%s</div>\n"
1686 "</div>\n" /* #tree_header */
1687 "</div>\n" /* #tree_header_wrapper */
1688 "<div class='dotted_line'></div>\n"
1689 "<div id='tree'>\n",
1696 error = got_output_repo_tree(c);
1700 fcgi_printf(c, "</div>\n"); /* #tree */
1701 fcgi_printf(c, "</div>\n"); /* #tree_content */
1708 static const struct got_error *
1709 gotweb_render_diff(struct request *c)
1711 const struct got_error *error = NULL;
1712 struct transport *t = c->t;
1713 struct repo_commit *rc = NULL;
1714 char *age = NULL, *author = NULL, *msg = NULL;
1717 error = got_get_repo_commits(c, 1);
1721 rc = TAILQ_FIRST(&t->repo_commits);
1723 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1726 error = gotweb_escape_html(&author, rc->author);
1729 error = gotweb_escape_html(&msg, rc->commit_msg);
1733 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1734 "<div id='diff_title'>Commit Diff</div>\n"
1735 "</div>\n" /* #diff_title_wrapper */
1736 "<div id='diff_content'>\n"
1737 "<div id='diff_header_wrapper'>\n"
1738 "<div id='diff_header'>\n"
1739 "<div id='header_diff_title'>Diff:</div>\n"
1740 "<div id='header_diff'>%s<br />%s</div>\n"
1741 "<div class='header_commit_title'>Commit:</div>\n"
1742 "<div class='header_commit'>%s</div>\n"
1743 "<div id='header_tree_title'>Tree:</div>\n"
1744 "<div id='header_tree'>%s</div>\n"
1745 "<div class='header_author_title'>Author:</div>\n"
1746 "<div class='header_author'>%s</div>\n"
1747 "<div class='header_age_title'>Date:</div>\n"
1748 "<div class='header_age'>%s</div>\n"
1749 "<div id='header_commit_msg_title'>Message:</div>\n"
1750 "<div id='header_commit_msg'>%s</div>\n"
1751 "</div>\n" /* #diff_header */
1752 "</div>\n" /* #diff_header_wrapper */
1753 "<div class='dotted_line'></div>\n"
1754 "<div id='diff'>\n",
1755 rc->parent_id, rc->commit_id,
1764 error = got_output_repo_diff(c);
1768 fcgi_printf(c, "</div>\n"); /* #diff */
1769 fcgi_printf(c, "</div>\n"); /* #diff_content */
1777 static const struct got_error *
1778 gotweb_render_summary(struct request *c)
1780 const struct got_error *error = NULL;
1781 struct transport *t = c->t;
1782 struct server *srv = c->srv;
1785 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1788 if (srv->show_repo_description) {
1790 "<div id='description_title'>Description:</div>\n"
1791 "<div id='description'>%s</div>\n",
1792 t->repo_dir->description ? t->repo_dir->description : "");
1797 if (srv->show_repo_owner) {
1799 "<div id='repo_owner_title'>Owner:</div>\n"
1800 "<div id='repo_owner'>%s</div>\n",
1801 t->repo_dir->owner ? t->repo_dir->owner : "");
1806 if (srv->show_repo_age) {
1808 "<div id='last_change_title'>Last Change:</div>\n"
1809 "<div id='last_change'>%s</div>\n",
1815 if (srv->show_repo_cloneurl) {
1817 "<div id='cloneurl_title'>Clone URL:</div>\n"
1818 "<div id='cloneurl'>%s</div>\n",
1819 t->repo_dir->url ? t->repo_dir->url : "");
1824 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1828 error = gotweb_render_briefs(c);
1830 log_warnx("%s: %s", __func__, error->msg);
1834 error = gotweb_render_tags(c);
1836 log_warnx("%s: %s", __func__, error->msg);
1840 error = gotweb_render_branches(c);
1842 log_warnx("%s: %s", __func__, error->msg);
1847 static const struct got_error *
1848 gotweb_render_tag(struct request *c)
1850 const struct got_error *error = NULL;
1851 struct repo_tag *rt = NULL;
1852 struct transport *t = c->t;
1853 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1855 error = got_get_repo_tags(c, 1);
1859 if (t->tag_count == 0) {
1860 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1865 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1867 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1870 error = gotweb_escape_html(&author, rt->tagger);
1873 error = gotweb_escape_html(&msg, rt->commit_msg);
1877 tagname = rt->tag_name;
1878 if (strncmp(tagname, "refs/", 5) == 0)
1880 error = gotweb_escape_html(&tagname, tagname);
1884 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1885 "<div id='tags_title'>Tag</div>\n"
1886 "</div>\n" /* #tags_title_wrapper */
1887 "<div id='tags_content'>\n"
1888 "<div id='tag_header_wrapper'>\n"
1889 "<div id='tag_header'>\n"
1890 "<div class='header_commit_title'>Commit:</div>\n"
1891 "<div class='header_commit'>%s"
1892 " <span class='refs_str'>(%s)</span></div>\n"
1893 "<div class='header_author_title'>Tagger:</div>\n"
1894 "<div class='header_author'>%s</div>\n"
1895 "<div class='header_age_title'>Date:</div>\n"
1896 "<div class='header_age'>%s</div>\n"
1897 "<div id='header_commit_msg_title'>Message:</div>\n"
1898 "<div id='header_commit_msg'>%s</div>\n"
1899 "</div>\n" /* #tag_header */
1900 "<div class='dotted_line'></div>\n"
1901 "<div id='tag_commit'>\n%s</div>"
1902 "</div>" /* #tag_header_wrapper */
1903 "</div>", /* #tags_content */
1918 static const struct got_error *
1919 gotweb_render_tags(struct request *c)
1921 const struct got_error *error = NULL;
1922 struct repo_tag *rt = NULL;
1923 struct server *srv = c->srv;
1924 struct transport *t = c->t;
1925 struct querystring *qs = t->qs;
1926 struct repo_dir *repo_dir = t->repo_dir;
1927 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1928 int r, commit_found = 0;
1930 if (qs->action == BRIEFS) {
1932 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1934 error = got_get_repo_tags(c, srv->max_commits_display);
1938 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1939 "<div id='tags_title'>Tags</div>\n"
1940 "</div>\n" /* #tags_title_wrapper */
1941 "<div id='tags_content'>\n");
1945 if (t->tag_count == 0) {
1946 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1947 "This repository contains no tags");
1952 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1953 if (commit_found == 0 && qs->commit != NULL) {
1954 if (strcmp(qs->commit, rt->commit_id) != 0)
1959 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1963 tagname = rt->tag_name;
1964 if (strncmp(tagname, "refs/tags/", 10) == 0)
1966 error = gotweb_escape_html(&tagname, tagname);
1970 if (rt->tag_commit != NULL) {
1971 newline = strchr(rt->tag_commit, '\n');
1974 error = gotweb_escape_html(&msg, rt->tag_commit);
1979 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1980 "<div class='tag'>%s</div>\n"
1981 "<div class='tag_log'>", age, tagname) == -1)
1984 r = gotweb_link(c, &(struct gotweb_url){
1988 .path = repo_dir->name,
1989 .commit = rt->commit_id,
1990 }, "%s", msg ? msg : "");
1994 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1995 "<div class='navs_wrapper'>\n"
1996 "<div class='navs'>") == -1)
1999 r = gotweb_link(c, &(struct gotweb_url){
2003 .path = repo_dir->name,
2004 .commit = rt->commit_id,
2009 if (fcgi_printf(c, " | ") == -1)
2012 r = gotweb_link(c, &(struct gotweb_url){
2016 .path = repo_dir->name,
2017 .commit = rt->commit_id,
2018 }, "commit briefs");
2022 if (fcgi_printf(c, " | ") == -1)
2025 r = gotweb_link(c, &(struct gotweb_url){
2029 .path = repo_dir->name,
2030 .commit = rt->commit_id,
2036 "</div>\n" /* .navs */
2037 "</div>\n" /* .navs_wrapper */
2038 "<div class='dotted_line'></div>\n");
2049 if (t->next_id || t->prev_id) {
2050 error = gotweb_render_navs(c);
2054 fcgi_printf(c, "</div>\n"); /* #tags_content */
2062 const struct got_error *
2063 gotweb_escape_html(char **escaped_html, const char *orig_html)
2065 const struct got_error *error = NULL;
2066 struct escape_pair {
2077 size_t orig_len, len;
2080 orig_len = strlen(orig_html);
2082 for (i = 0; i < orig_len; i++) {
2083 for (j = 0; j < nitems(esc); j++) {
2084 if (orig_html[i] != esc[j].c)
2086 len += strlen(esc[j].s) - 1 /* escaped char */;
2090 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2091 if (*escaped_html == NULL)
2092 return got_error_from_errno("calloc");
2095 for (i = 0; i < orig_len; i++) {
2097 for (j = 0; j < nitems(esc); j++) {
2098 if (orig_html[i] != esc[j].c)
2101 if (strlcat(*escaped_html, esc[j].s, len + 1)
2103 error = got_error(GOT_ERR_NO_SPACE);
2106 x += strlen(esc[j].s);
2111 (*escaped_html)[x] = orig_html[i];
2117 free(*escaped_html);
2118 *escaped_html = NULL;
2120 (*escaped_html)[x] = '\0';
2127 should_urlencode(int c)
2129 if (c <= ' ' || c >= 127)
2160 gotweb_urlencode(const char *str)
2168 for (s = str; *s; ++s) {
2170 if (should_urlencode(*s))
2174 escaped = calloc(1, len + 1);
2175 if (escaped == NULL)
2179 for (s = str; *s; ++s) {
2180 if (should_urlencode(*s)) {
2181 a = (*s & 0xF0) >> 4;
2185 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2186 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2194 static inline const char *
2195 action_name(int action)
2226 gotweb_print_url(struct request *c, struct gotweb_url *url)
2228 const char *sep = "?", *action;
2232 action = action_name(url->action);
2233 if (action != NULL) {
2234 if (fcgi_printf(c, "?action=%s", action) == -1)
2240 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2246 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2252 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2258 tmp = gotweb_urlencode(url->file);
2261 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2269 tmp = gotweb_urlencode(url->folder);
2272 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2280 tmp = gotweb_urlencode(url->headref);
2283 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2290 if (url->index_page != -1) {
2291 if (fcgi_printf(c, "%sindex_page=%d", sep,
2292 url->index_page) == -1)
2298 tmp = gotweb_urlencode(url->path);
2301 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2308 if (url->page != -1) {
2309 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2318 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2323 if (fcgi_printf(c, "<a href='") == -1)
2326 if (gotweb_print_url(c, url) == -1)
2329 if (fcgi_printf(c, "'>") == -1)
2333 r = fcgi_vprintf(c, fmt, ap);
2338 if (fcgi_printf(c, "</a>"))
2343 static struct got_repository *
2344 find_cached_repo(struct server *srv, const char *path)
2348 for (i = 0; i < srv->ncached_repos; i++) {
2349 if (strcmp(srv->cached_repos[i].path, path) == 0)
2350 return srv->cached_repos[i].repo;
2356 static const struct got_error *
2357 cache_repo(struct got_repository **new, struct server *srv,
2358 struct repo_dir *repo_dir, struct socket *sock)
2360 const struct got_error *error = NULL;
2361 struct got_repository *repo;
2362 struct cached_repo *cr;
2365 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2366 cr = &srv->cached_repos[srv->ncached_repos - 1];
2367 error = got_repo_close(cr->repo);
2368 memset(cr, 0, sizeof(*cr));
2369 srv->ncached_repos--;
2372 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2373 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2374 cr = &srv->cached_repos[0];
2377 cr = &srv->cached_repos[srv->ncached_repos];
2380 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2383 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2384 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2389 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2390 >= sizeof(cr->path)) {
2392 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2393 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2395 return got_error(GOT_ERR_NO_SPACE);
2399 srv->ncached_repos++;
2404 static const struct got_error *
2405 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2407 const struct got_error *error = NULL;
2408 struct socket *sock = c->sock;
2409 struct server *srv = c->srv;
2410 struct transport *t = c->t;
2411 struct got_repository *repo = NULL;
2415 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2416 GOTWEB_GIT_DIR) == -1)
2417 return got_error_from_errno("asprintf");
2419 dt = opendir(dir_test);
2423 repo_dir->path = dir_test;
2428 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2429 repo_dir->name) == -1)
2430 return got_error_from_errno("asprintf");
2432 dt = opendir(dir_test);
2434 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2437 repo_dir->path = dir_test;
2442 if (srv->respect_exportok &&
2443 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2444 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2449 repo = find_cached_repo(srv, repo_dir->path);
2451 error = cache_repo(&repo, srv, repo_dir, sock);
2456 error = gotweb_get_repo_description(&repo_dir->description, srv,
2460 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2463 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2467 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2470 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2471 error = got_error_from_errno("closedir");
2475 static const struct got_error *
2476 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2478 const struct got_error *error;
2480 *repo_dir = calloc(1, sizeof(**repo_dir));
2481 if (*repo_dir == NULL)
2482 return got_error_from_errno("calloc");
2484 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2485 error = got_error_from_errno("asprintf");
2490 (*repo_dir)->owner = NULL;
2491 (*repo_dir)->description = NULL;
2492 (*repo_dir)->url = NULL;
2493 (*repo_dir)->age = NULL;
2494 (*repo_dir)->path = NULL;
2499 static const struct got_error *
2500 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2502 const struct got_error *error = NULL;
2504 char *d_file = NULL;
2508 *description = NULL;
2509 if (srv->show_repo_description == 0)
2512 if (asprintf(&d_file, "%s/description", dir) == -1)
2513 return got_error_from_errno("asprintf");
2515 f = fopen(d_file, "r");
2517 if (errno != ENOENT && errno != EACCES)
2518 error = got_error_from_errno2("fopen", d_file);
2522 if (fseek(f, 0, SEEK_END) == -1) {
2523 error = got_ferror(f, GOT_ERR_IO);
2528 error = got_ferror(f, GOT_ERR_IO);
2533 *description = strdup("");
2534 if (*description == NULL)
2535 error = got_error_from_errno("strdup");
2539 if (fseek(f, 0, SEEK_SET) == -1) {
2540 error = got_ferror(f, GOT_ERR_IO);
2543 *description = calloc(len + 1, sizeof(**description));
2544 if (*description == NULL) {
2545 error = got_error_from_errno("calloc");
2549 n = fread(*description, 1, len, f);
2550 if (n == 0 && ferror(f))
2551 error = got_ferror(f, GOT_ERR_IO);
2553 if (f != NULL && fclose(f) == EOF && error == NULL)
2554 error = got_error_from_errno("fclose");
2559 static const struct got_error *
2560 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2562 const struct got_error *error = NULL;
2564 char *d_file = NULL;
2570 if (srv->show_repo_cloneurl == 0)
2573 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2574 return got_error_from_errno("asprintf");
2576 f = fopen(d_file, "r");
2578 if (errno != ENOENT && errno != EACCES)
2579 error = got_error_from_errno2("fopen", d_file);
2583 if (fseek(f, 0, SEEK_END) == -1) {
2584 error = got_ferror(f, GOT_ERR_IO);
2589 error = got_ferror(f, GOT_ERR_IO);
2595 if (fseek(f, 0, SEEK_SET) == -1) {
2596 error = got_ferror(f, GOT_ERR_IO);
2600 *url = calloc(len + 1, sizeof(**url));
2602 error = got_error_from_errno("calloc");
2606 n = fread(*url, 1, len, f);
2607 if (n == 0 && ferror(f))
2608 error = got_ferror(f, GOT_ERR_IO);
2610 if (f != NULL && fclose(f) == EOF && error == NULL)
2611 error = got_error_from_errno("fclose");
2616 const struct got_error *
2617 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2620 long long diff_time;
2621 const char *years = "years ago", *months = "months ago";
2622 const char *weeks = "weeks ago", *days = "days ago";
2623 const char *hours = "hours ago", *minutes = "minutes ago";
2624 const char *seconds = "seconds ago", *now = "right now";
2632 diff_time = time(NULL) - committer_time;
2633 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2634 if (asprintf(repo_age, "%lld %s",
2635 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2638 if (asprintf(repo_age, "%lld %s",
2639 (diff_time / 60 / 60 / 24 / (365 / 12)),
2641 return got_error_from_errno("asprintf");
2642 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2643 if (asprintf(repo_age, "%lld %s",
2644 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2645 return got_error_from_errno("asprintf");
2646 } else if (diff_time > 60 * 60 * 24 * 2) {
2647 if (asprintf(repo_age, "%lld %s",
2648 (diff_time / 60 / 60 / 24), days) == -1)
2649 return got_error_from_errno("asprintf");
2650 } else if (diff_time > 60 * 60 * 2) {
2651 if (asprintf(repo_age, "%lld %s",
2652 (diff_time / 60 / 60), hours) == -1)
2653 return got_error_from_errno("asprintf");
2654 } else if (diff_time > 60 * 2) {
2655 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2657 return got_error_from_errno("asprintf");
2658 } else if (diff_time > 2) {
2659 if (asprintf(repo_age, "%lld %s", diff_time,
2661 return got_error_from_errno("asprintf");
2663 if (asprintf(repo_age, "%s", now) == -1)
2664 return got_error_from_errno("asprintf");
2668 if (gmtime_r(&committer_time, &tm) == NULL)
2669 return got_error_from_errno("gmtime_r");
2671 s = asctime_r(&tm, datebuf);
2673 return got_error_from_errno("asctime_r");
2675 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2676 return got_error_from_errno("asprintf");