2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
59 static const struct querystring_keys querystring_keys[] = {
64 { "headref", HEADREF },
65 { "index_page", INDEX_PAGE },
70 static const struct action_keys action_keys[] = {
74 { "commits", COMMITS },
78 { "summary", SUMMARY },
84 static const struct got_error *gotweb_init_querystring(struct querystring **);
85 static const struct got_error *gotweb_parse_querystring(struct querystring **,
87 static const struct got_error *gotweb_assign_querystring(struct querystring **,
89 static const struct got_error *gotweb_render_header(struct request *);
90 static const struct got_error *gotweb_render_footer(struct request *);
91 static const struct got_error *gotweb_render_index(struct request *);
92 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
94 static const struct got_error *gotweb_load_got_path(struct request *c,
96 static const struct got_error *gotweb_get_repo_description(char **,
97 struct server *, const char *, int);
98 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
100 static const struct got_error *gotweb_render_navs(struct request *);
101 static const struct got_error *gotweb_render_blame(struct request *);
102 static const struct got_error *gotweb_render_briefs(struct request *);
103 static const struct got_error *gotweb_render_commits(struct request *);
104 static const struct got_error *gotweb_render_diff(struct request *);
105 static const struct got_error *gotweb_render_summary(struct request *);
106 static const struct got_error *gotweb_render_tag(struct request *);
107 static const struct got_error *gotweb_render_tags(struct request *);
108 static const struct got_error *gotweb_render_tree(struct request *);
109 static const struct got_error *gotweb_render_branches(struct request *);
111 static void gotweb_free_querystring(struct querystring *);
112 static void gotweb_free_repo_dir(struct repo_dir *);
114 struct server *gotweb_get_server(uint8_t *, uint8_t *);
117 gotweb_process_request(struct request *c)
119 const struct got_error *error = NULL, *error2 = NULL;
120 struct server *srv = NULL;
121 struct querystring *qs = NULL;
122 struct repo_dir *repo_dir = NULL;
123 uint8_t err[] = "gotwebd experienced an error: ";
126 /* init the transport */
127 error = gotweb_init_transport(&c->t);
129 log_warnx("%s: %s", __func__, error->msg);
132 /* don't process any further if client disconnected */
133 if (c->sock->client_status == CLIENT_DISCONNECT)
135 /* get the gotwebd server */
136 srv = gotweb_get_server(c->server_name, c->http_host);
138 log_warnx("%s: error server is NULL", __func__);
142 /* parse our querystring */
143 error = gotweb_init_querystring(&qs);
145 log_warnx("%s: %s", __func__, error->msg);
149 error = gotweb_parse_querystring(&qs, c->querystring);
151 log_warnx("%s: %s", __func__, error->msg);
156 * certain actions require a commit id in the querystring. this stops
157 * bad actors from exploiting this by manually manipulating the
161 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
162 qs->action == DIFF)) {
163 error2 = got_error(GOT_ERR_QUERYSTRING);
167 if (qs->action != INDEX) {
168 error = gotweb_init_repo_dir(&repo_dir, qs->path);
171 error = gotweb_load_got_path(c, repo_dir);
172 c->t->repo_dir = repo_dir;
173 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
177 /* render top of page */
178 if (qs != NULL && qs->action == BLOB) {
179 error = got_get_repo_commits(c, 1);
182 error = got_output_file_blob(c);
184 log_warnx("%s: %s", __func__, error->msg);
190 error = gotweb_render_content_type(c, "text/html");
192 log_warnx("%s: %s", __func__, error->msg);
198 error = gotweb_render_header(c);
200 log_warnx("%s: %s", __func__, error->msg);
211 error = gotweb_render_blame(c);
213 log_warnx("%s: %s", __func__, error->msg);
218 error = gotweb_render_briefs(c);
220 log_warnx("%s: %s", __func__, error->msg);
225 error = gotweb_render_commits(c);
227 log_warnx("%s: %s", __func__, error->msg);
232 error = gotweb_render_diff(c);
234 log_warnx("%s: %s", __func__, error->msg);
239 error = gotweb_render_index(c);
241 log_warnx("%s: %s", __func__, error->msg);
246 error = gotweb_render_summary(c);
248 log_warnx("%s: %s", __func__, error->msg);
253 error = gotweb_render_tag(c);
255 log_warnx("%s: %s", __func__, error->msg);
260 error = gotweb_render_tags(c);
262 log_warnx("%s: %s", __func__, error->msg);
267 error = gotweb_render_tree(c);
269 log_warnx("%s: %s", __func__, error->msg);
275 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
276 "Erorr: Bad Querystring");
284 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
286 if (fcgi_printf(c, "\n%s", err) == -1)
289 if (fcgi_printf(c, "%s", error->msg) == -1)
292 if (fcgi_printf(c, "see daemon logs for details") == -1)
295 if (html && fcgi_printf(c, "</div>\n") == -1)
298 if (html && srv != NULL)
299 gotweb_render_footer(c);
303 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
305 struct server *srv = NULL;
307 /* check against the server name first */
308 if (strlen(server_name) > 0)
309 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
310 if (strcmp(srv->name, server_name) == 0)
313 /* check against subdomain second */
314 if (strlen(subdomain) > 0)
315 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
316 if (strcmp(srv->name, subdomain) == 0)
319 /* if those fail, send first server */
320 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
327 const struct got_error *
328 gotweb_init_transport(struct transport **t)
330 const struct got_error *error = NULL;
332 *t = calloc(1, sizeof(**t));
334 return got_error_from_errno2("%s: calloc", __func__);
336 TAILQ_INIT(&(*t)->repo_commits);
337 TAILQ_INIT(&(*t)->repo_tags);
340 (*t)->repo_dir = NULL;
342 (*t)->next_id = NULL;
343 (*t)->prev_id = NULL;
350 static const struct got_error *
351 gotweb_init_querystring(struct querystring **qs)
353 const struct got_error *error = NULL;
355 *qs = calloc(1, sizeof(**qs));
357 return got_error_from_errno2("%s: calloc", __func__);
359 (*qs)->headref = strdup("HEAD");
360 if ((*qs)->headref == NULL) {
363 return got_error_from_errno2("%s: strdup", __func__);
366 (*qs)->action = INDEX;
367 (*qs)->commit = NULL;
369 (*qs)->folder = NULL;
370 (*qs)->index_page = 0;
371 (*qs)->index_page_str = NULL;
377 static const struct got_error *
378 gotweb_parse_querystring(struct querystring **qs, char *qst)
380 const struct got_error *error = NULL;
381 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
382 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
389 return got_error_from_errno2("%s: strdup", __func__);
394 while (tok1_pair != NULL) {
395 strsep(&tok1_end, "&");
397 tok2 = strdup(tok1_pair);
400 return got_error_from_errno2("%s: strdup", __func__);
406 while (tok2_pair != NULL) {
407 strsep(&tok2_end, "=");
409 error = gotweb_assign_querystring(qs, tok2_pair,
414 tok2_pair = tok2_end;
417 tok1_pair = tok1_end;
428 * Adapted from usr.sbin/httpd/httpd.c url_decode.
430 static const struct got_error *
431 gotweb_urldecode(char *url)
443 /* Encoding character is followed by two hex chars */
444 if (!isxdigit((unsigned char)p[1]) ||
445 !isxdigit((unsigned char)p[2]) ||
446 (p[1] == '0' && p[2] == '0'))
447 return got_error(GOT_ERR_BAD_QUERYSTRING);
453 * We don't have to validate "hex" because it is
454 * guaranteed to include two hex chars followed by nul.
456 x = strtoul(hex, NULL, 16);
472 static const struct got_error *
473 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
475 const struct got_error *error = NULL;
479 error = gotweb_urldecode(value);
483 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
484 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
487 switch (querystring_keys[el_cnt].element) {
489 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
490 if (strcmp(value, action_keys[a_cnt].name) != 0)
492 else if (strcmp(value,
493 action_keys[a_cnt].name) == 0){
495 action_keys[a_cnt].action;
503 (*qs)->commit = strdup(value);
504 if ((*qs)->commit == NULL) {
505 error = got_error_from_errno2("%s: strdup",
511 (*qs)->file = strdup(value);
512 if ((*qs)->file == NULL) {
513 error = got_error_from_errno2("%s: strdup",
519 (*qs)->folder = strdup(value);
520 if ((*qs)->folder == NULL) {
521 error = got_error_from_errno2("%s: strdup",
527 free((*qs)->headref);
528 (*qs)->headref = strdup(value);
529 if ((*qs)->headref == NULL) {
530 error = got_error_from_errno2("%s: strdup",
536 if (strlen(value) == 0)
538 (*qs)->index_page_str = strdup(value);
539 if ((*qs)->index_page_str == NULL) {
540 error = got_error_from_errno2("%s: strdup",
544 (*qs)->index_page = strtonum(value, INT64_MIN,
547 error = got_error_from_errno3("%s: strtonum %s",
551 if ((*qs)->index_page < 0) {
552 (*qs)->index_page = 0;
553 sprintf((*qs)->index_page_str, "%d", 0);
557 (*qs)->path = strdup(value);
558 if ((*qs)->path == NULL) {
559 error = got_error_from_errno2("%s: strdup",
565 if (strlen(value) == 0)
567 (*qs)->page_str = strdup(value);
568 if ((*qs)->page_str == NULL) {
569 error = got_error_from_errno2("%s: strdup",
573 (*qs)->page = strtonum(value, INT64_MIN,
576 error = got_error_from_errno3("%s: strtonum %s",
580 if ((*qs)->page < 0) {
582 sprintf((*qs)->page_str, "%d", 0);
594 gotweb_free_repo_tag(struct repo_tag *rt)
599 free(rt->tag_commit);
600 free(rt->commit_msg);
607 gotweb_free_repo_commit(struct repo_commit *rc)
617 free(rc->commit_msg);
623 gotweb_free_querystring(struct querystring *qs)
630 free(qs->index_page_str);
638 gotweb_free_repo_dir(struct repo_dir *repo_dir)
640 if (repo_dir != NULL) {
641 free(repo_dir->name);
642 free(repo_dir->owner);
643 free(repo_dir->description);
646 free(repo_dir->path);
652 gotweb_free_transport(struct transport *t)
654 struct repo_commit *rc = NULL, *trc = NULL;
655 struct repo_tag *rt = NULL, *trt = NULL;
657 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
658 TAILQ_REMOVE(&t->repo_commits, rc, entry);
659 gotweb_free_repo_commit(rc);
661 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
662 TAILQ_REMOVE(&t->repo_tags, rt, entry);
663 gotweb_free_repo_tag(rt);
665 gotweb_free_repo_dir(t->repo_dir);
666 gotweb_free_querystring(t->qs);
672 const struct got_error *
673 gotweb_render_content_type(struct request *c, const uint8_t *type)
675 const char *csp = "default-src 'self'; script-src 'none'; "
676 "object-src 'none';";
679 "Content-Security-Policy: %s\r\n"
680 "Content-Type: %s\r\n\r\n",
685 const struct got_error *
686 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
689 fcgi_printf(c, "Content-type: %s\r\n"
690 "Content-disposition: attachment; filename=%s\r\n\r\n",
695 static const struct got_error *
696 gotweb_render_header(struct request *c)
698 const struct got_error *err = NULL;
699 struct server *srv = c->srv;
700 struct querystring *qs = c->t->qs;
703 r = fcgi_printf(c, "<!doctype html>\n"
706 "<title>%s</title>\n"
707 "<meta charset='utf-8' />\n"
708 "<meta name='viewport' content='initial-scale=.75' />\n"
709 "<meta name='msapplication-TileColor' content='#da532c' />\n"
710 "<meta name='theme-color' content='#ffffff'/>\n"
711 "<link rel='apple-touch-icon' sizes='180x180'"
712 " href='%sapple-touch-icon.png' />\n"
713 "<link rel='icon' type='image/png' sizes='32x32'"
714 " href='%sfavicon-32x32.png' />\n"
715 "<link rel='icon' type='image/png' sizes='16x16'"
716 " href='%sfavicon-16x16.png' />\n"
717 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
718 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
719 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
722 "<div id='gw_body'>\n"
723 "<div id='header'>\n"
724 "<div id='got_link'>"
725 "<a href='%s' target='_blank'>"
726 "<img src='%s%s' alt='logo' id='logo' />"
728 "</div>\n" /* #got_link */
729 "</div>\n" /* #header */
730 "<div id='site_path'>\n"
731 "<div id='site_link'>\n"
732 "<a href='?index_page=%d'>%s</a>",
739 c->script_name, srv->custom_css,
741 c->script_name, srv->logo,
742 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)
799 fcgi_printf(c, "</div>\n" /* #site_path */
800 "</div>\n" /* #site_link */
801 "<div id='content'>\n");
807 static const struct got_error *
808 gotweb_render_footer(struct request *c)
810 const struct got_error *error = NULL;
811 struct server *srv = c->srv;
812 const char *siteowner = " ";
813 char *escaped_owner = NULL;
815 if (srv->show_site_owner) {
816 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
819 siteowner = escaped_owner;
822 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
823 "<div id='site_owner'>%s</div>\n"
824 "</div>\n" /* #site_owner_wrapper */
825 "</div>\n" /* #content */
826 "</div>\n" /* #gw_body */
827 "</body>\n</html>\n", siteowner);
833 static const struct got_error *
834 gotweb_render_navs(struct request *c)
836 const struct got_error *error = NULL;
837 struct transport *t = c->t;
838 struct querystring *qs = t->qs;
839 struct server *srv = c->srv;
842 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
848 if (qs->index_page > 0) {
849 struct gotweb_url url = {
851 .index_page = qs->index_page - 1,
855 r = gotweb_link(c, &url, "Previous");
859 if (t->prev_id && qs->commit != NULL &&
860 strcmp(qs->commit, t->prev_id) != 0) {
861 struct gotweb_url url = {
864 .page = qs->page - 1,
866 .commit = t->prev_id,
867 .headref = qs->headref,
870 r = gotweb_link(c, &url, "Previous");
874 if (t->prev_id && qs->commit != NULL &&
875 strcmp(qs->commit, t->prev_id) != 0) {
876 struct gotweb_url url = {
879 .page = qs->page - 1,
881 .commit = t->prev_id,
882 .headref = qs->headref,
883 .folder = qs->folder,
887 r = gotweb_link(c, &url, "Previous");
891 if (t->prev_id && qs->commit != NULL &&
892 strcmp(qs->commit, t->prev_id) != 0) {
893 struct gotweb_url url = {
896 .page = qs->page - 1,
898 .commit = t->prev_id,
899 .headref = qs->headref,
902 r = gotweb_link(c, &url, "Previous");
910 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
911 "<div id='nav_next'>");
917 if (t->next_disp == srv->max_repos_display &&
918 t->repos_total != (qs->index_page + 1) *
919 srv->max_repos_display) {
920 struct gotweb_url url = {
922 .index_page = qs->index_page + 1,
926 r = gotweb_link(c, &url, "Next");
931 struct gotweb_url url = {
934 .page = qs->page + 1,
936 .commit = t->next_id,
937 .headref = qs->headref,
940 r = gotweb_link(c, &url, "Next");
945 struct gotweb_url url = {
948 .page = qs->page + 1,
950 .commit = t->next_id,
951 .headref = qs->headref,
952 .folder = qs->folder,
956 r = gotweb_link(c, &url, "Next");
961 struct gotweb_url url = {
964 .page = qs->page + 1,
966 .commit = t->next_id,
967 .headref = qs->headref,
970 r = gotweb_link(c, &url, "Next");
977 fcgi_printf(c, "</div>\n"); /* #nav_next */
978 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
987 static const struct got_error *
988 gotweb_render_index(struct request *c)
990 const struct got_error *error = NULL;
991 struct server *srv = c->srv;
992 struct transport *t = c->t;
993 struct querystring *qs = t->qs;
994 struct repo_dir *repo_dir = NULL;
996 struct dirent **sd_dent = NULL;
997 unsigned int d_cnt, d_i, d_disp = 0;
998 unsigned int d_skipped = 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 r = fcgi_printf(c, "<div id='index_header'>\n"
1015 "<div id='index_header_project'>Project</div>\n");
1019 if (srv->show_repo_description)
1020 if (fcgi_printf(c, "<div id='index_header_description'>"
1021 "Description</div>\n") == -1)
1023 if (srv->show_repo_owner)
1024 if (fcgi_printf(c, "<div id='index_header_owner'>"
1025 "Owner</div>\n") == -1)
1027 if (srv->show_repo_age)
1028 if (fcgi_printf(c, "<div id='index_header_age'>"
1029 "Last Change</div>\n") == -1)
1031 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1034 for (d_i = 0; d_i < d_cnt; d_i++) {
1035 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
1038 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1039 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
1044 error = got_path_dirent_type(&type, srv->repos_path,
1048 if (type != DT_DIR) {
1053 if (qs->index_page > 0 && (qs->index_page *
1054 srv->max_repos_display) > t->prev_disp) {
1059 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1063 error = gotweb_load_got_path(c, repo_dir);
1064 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1066 gotweb_free_repo_dir(repo_dir);
1071 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1077 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1078 "<div class='index_project'>") == -1)
1081 r = gotweb_link(c, &(struct gotweb_url){
1085 .path = repo_dir->name,
1086 }, "%s", repo_dir->name);
1090 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1093 if (srv->show_repo_description) {
1095 "<div class='index_project_description'>\n"
1096 "%s</div>\n", repo_dir->description);
1101 if (srv->show_repo_owner) {
1102 r = fcgi_printf(c, "<div class='index_project_owner'>"
1103 "%s</div>\n", repo_dir->owner);
1108 if (srv->show_repo_age) {
1109 r = fcgi_printf(c, "<div class='index_project_age'>"
1110 "%s</div>\n", repo_dir->age);
1115 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1116 "<div class='navs'>") == -1)
1119 r = gotweb_link(c, &(struct gotweb_url){
1123 .path = repo_dir->name
1128 if (fcgi_printf(c, " | ") == -1)
1131 r = gotweb_link(c, &(struct gotweb_url){
1135 .path = repo_dir->name
1136 }, "commit briefs");
1140 if (fcgi_printf(c, " | ") == -1)
1143 r = gotweb_link(c, &(struct gotweb_url){
1147 .path = repo_dir->name
1152 if (fcgi_printf(c, " | ") == -1)
1155 r = gotweb_link(c, &(struct gotweb_url){
1159 .path = repo_dir->name
1164 if (fcgi_printf(c, " | ") == -1)
1167 r = gotweb_link(c, &(struct gotweb_url){
1171 .path = repo_dir->name
1176 r = fcgi_printf(c, "</div>" /* .navs */
1177 "<div class='dotted_line'></div>\n"
1178 "</div>\n" /* .navs_wrapper */
1179 "</div>\n"); /* .index_wrapper */
1183 gotweb_free_repo_dir(repo_dir);
1186 if (d_disp == srv->max_repos_display)
1189 t->repos_total = d_cnt - d_skipped;
1191 if (srv->max_repos_display == 0)
1193 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1195 if (t->repos_total <= srv->max_repos ||
1196 t->repos_total <= srv->max_repos_display)
1199 error = gotweb_render_navs(c);
1204 for (d_i = 0; d_i < d_cnt; d_i++)
1208 if (d != NULL && closedir(d) == EOF && error == NULL)
1209 error = got_error_from_errno("closedir");
1213 static const struct got_error *
1214 gotweb_render_blame(struct request *c)
1216 const struct got_error *error = NULL;
1217 struct transport *t = c->t;
1218 struct repo_commit *rc = NULL;
1219 char *age = NULL, *msg = NULL;
1222 error = got_get_repo_commits(c, 1);
1226 rc = TAILQ_FIRST(&t->repo_commits);
1228 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1231 error = gotweb_escape_html(&msg, rc->commit_msg);
1235 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1236 "<div id='blame_title'>Blame</div>\n"
1237 "</div>\n" /* #blame_title_wrapper */
1238 "<div id='blame_content'>\n"
1239 "<div id='blame_header_wrapper'>\n"
1240 "<div id='blame_header'>\n"
1241 "<div class='header_age_title'>Date:</div>\n"
1242 "<div class='header_age'>%s</div>\n"
1243 "<div id='header_commit_msg_title'>Message:</div>\n"
1244 "<div id='header_commit_msg'>%s</div>\n"
1245 "</div>\n" /* #blame_header */
1246 "</div>\n" /* #blame_header_wrapper */
1247 "<div class='dotted_line'></div>\n"
1248 "<div id='blame'>\n",
1254 error = got_output_file_blame(c);
1258 fcgi_printf(c, "</div>\n" /* #blame */
1259 "</div>\n"); /* #blame_content */
1266 static const struct got_error *
1267 gotweb_render_briefs(struct request *c)
1269 const struct got_error *error = NULL;
1270 struct repo_commit *rc = NULL;
1271 struct server *srv = c->srv;
1272 struct transport *t = c->t;
1273 struct querystring *qs = t->qs;
1274 struct repo_dir *repo_dir = t->repo_dir;
1275 char *smallerthan, *newline;
1276 char *age = NULL, *author = NULL, *msg = NULL;
1279 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1280 "<div id='briefs_title'>Commit Briefs</div>\n"
1281 "</div>\n" /* #briefs_title_wrapper */
1282 "<div id='briefs_content'>\n");
1286 if (qs->action == SUMMARY) {
1287 qs->action = BRIEFS;
1288 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1290 error = got_get_repo_commits(c, srv->max_commits_display);
1294 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1295 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1299 smallerthan = strchr(rc->author, '<');
1301 *smallerthan = '\0';
1303 newline = strchr(rc->commit_msg, '\n');
1307 error = gotweb_escape_html(&author, rc->author);
1310 error = gotweb_escape_html(&msg, rc->commit_msg);
1314 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1315 "<div class='briefs_author'>%s</div>\n"
1316 "<div class='briefs_log'>",
1321 r = gotweb_link(c, &(struct gotweb_url){
1325 .path = repo_dir->name,
1326 .commit = rc->commit_id,
1327 .headref = qs->headref,
1335 error = gotweb_escape_html(&refs, rc->refs_str);
1339 " <span class='refs_str'>(%s)</span>", refs);
1344 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1347 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1348 "<div class='navs'>");
1352 r = gotweb_link(c, &(struct gotweb_url){
1356 .path = repo_dir->name,
1357 .commit = rc->commit_id,
1358 .headref = qs->headref,
1363 if (fcgi_printf(c, " | ") == -1)
1366 r = gotweb_link(c, &(struct gotweb_url){
1370 .path = repo_dir->name,
1371 .commit = rc->commit_id,
1372 .headref = qs->headref,
1377 if (fcgi_printf(c, "</div>\n" /* .navs */
1378 "</div>\n" /* .navs_wrapper */
1379 "<div class='dotted_line'></div>\n") == -1)
1390 if (t->next_id || t->prev_id) {
1391 error = gotweb_render_navs(c);
1395 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1403 static const struct got_error *
1404 gotweb_render_commits(struct request *c)
1406 const struct got_error *error = NULL;
1407 struct repo_commit *rc = NULL;
1408 struct server *srv = c->srv;
1409 struct transport *t = c->t;
1410 struct repo_dir *repo_dir = t->repo_dir;
1411 char *age = NULL, *author = NULL, *msg = NULL;
1414 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1415 "<div class='commits_title'>Commits</div>\n"
1416 "</div>\n" /* .commits_title_wrapper */
1417 "<div class='commits_content'>\n");
1421 error = got_get_repo_commits(c, srv->max_commits_display);
1425 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1426 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1429 error = gotweb_escape_html(&author, rc->author);
1432 error = gotweb_escape_html(&msg, rc->commit_msg);
1436 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1437 "<div class='commits_header'>\n"
1438 "<div class='header_commit_title'>Commit:</div>\n"
1439 "<div class='header_commit'>%s</div>\n"
1440 "<div class='header_author_title'>Author:</div>\n"
1441 "<div class='header_author'>%s</div>\n"
1442 "<div class='header_age_title'>Date:</div>\n"
1443 "<div class='header_age'>%s</div>\n"
1444 "</div>\n" /* .commits_header */
1445 "</div>\n" /* .commits_header_wrapper */
1446 "<div class='dotted_line'></div>\n"
1447 "<div class='commit'>\n%s</div>\n",
1455 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1456 "<div class='navs'>") == -1)
1459 r = gotweb_link(c, &(struct gotweb_url){
1463 .path = repo_dir->name,
1464 .commit = rc->commit_id,
1469 if (fcgi_printf(c, " | ") == -1)
1472 r = gotweb_link(c, &(struct gotweb_url){
1476 .path = repo_dir->name,
1477 .commit = rc->commit_id,
1482 if (fcgi_printf(c, "</div>\n" /* .navs */
1483 "</div>\n" /* .navs_wrapper */
1484 "<div class='dotted_line'></div>\n") == -1)
1495 if (t->next_id || t->prev_id) {
1496 error = gotweb_render_navs(c);
1500 fcgi_printf(c, "</div>\n"); /* .commits_content */
1508 static const struct got_error *
1509 gotweb_render_branches(struct request *c)
1511 const struct got_error *error = NULL;
1512 struct got_reflist_head refs;
1513 struct got_reflist_entry *re;
1514 struct transport *t = c->t;
1515 struct querystring *qs = t->qs;
1516 struct got_repository *repo = t->repo;
1517 char *escaped_refname = NULL;
1523 error = got_ref_list(&refs, repo, "refs/heads",
1524 got_ref_cmp_by_name, NULL);
1528 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1529 "<div id='branches_title'>Branches</div>\n"
1530 "</div>\n" /* #branches_title_wrapper */
1531 "<div id='branches_content'>\n");
1535 TAILQ_FOREACH(re, &refs, entry) {
1536 const char *refname = NULL;
1538 if (got_ref_is_symbolic(re->ref))
1541 refname = got_ref_get_name(re->ref);
1542 if (refname == NULL) {
1543 error = got_error_from_errno("strdup");
1546 if (strncmp(refname, "refs/heads/", 11) != 0)
1549 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1553 if (strncmp(refname, "refs/heads/", 11) == 0)
1555 error = gotweb_escape_html(&escaped_refname, refname);
1559 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1560 "<div class='branches_age'>%s</div>\n"
1561 "<div class='branches_space'> </div>\n"
1562 "<div class='branch'>", age);
1566 r = gotweb_link(c, &(struct gotweb_url){
1572 }, "%s", escaped_refname);
1576 if (fcgi_printf(c, "</div>\n" /* .branch */
1577 "<div class='navs_wrapper'>\n"
1578 "<div class='navs'>") == -1)
1581 r = gotweb_link(c, &(struct gotweb_url){
1591 if (fcgi_printf(c, " | ") == -1)
1594 r = gotweb_link(c, &(struct gotweb_url){
1600 }, "commit briefs");
1604 if (fcgi_printf(c, " | ") == -1)
1607 r = gotweb_link(c, &(struct gotweb_url){
1617 r = fcgi_printf(c, "</div>\n" /* .navs */
1618 "</div>\n" /* .navs_wrapper */
1619 "<div class='dotted_line'></div>\n"
1620 "</div>\n"); /* .branches_wrapper */
1626 free(escaped_refname);
1627 escaped_refname = NULL;
1629 fcgi_printf(c, "</div>\n"); /* #branches_content */
1632 free(escaped_refname);
1633 got_ref_list_free(&refs);
1637 static const struct got_error *
1638 gotweb_render_tree(struct request *c)
1640 const struct got_error *error = NULL;
1641 struct transport *t = c->t;
1642 struct repo_commit *rc = NULL;
1643 char *age = NULL, *msg = NULL;
1646 error = got_get_repo_commits(c, 1);
1650 rc = TAILQ_FIRST(&t->repo_commits);
1652 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1656 error = gotweb_escape_html(&msg, rc->commit_msg);
1660 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1661 "<div id='tree_title'>Tree</div>\n"
1662 "</div>\n" /* #tree_title_wrapper */
1663 "<div id='tree_content'>\n"
1664 "<div id='tree_header_wrapper'>\n"
1665 "<div id='tree_header'>\n"
1666 "<div id='header_tree_title'>Tree:</div>\n"
1667 "<div id='header_tree'>%s</div>\n"
1668 "<div class='header_age_title'>Date:</div>\n"
1669 "<div class='header_age'>%s</div>\n"
1670 "<div id='header_commit_msg_title'>Message:</div>\n"
1671 "<div id='header_commit_msg'>%s</div>\n"
1672 "</div>\n" /* #tree_header */
1673 "</div>\n" /* #tree_header_wrapper */
1674 "<div class='dotted_line'></div>\n"
1675 "<div id='tree'>\n",
1682 error = got_output_repo_tree(c);
1686 fcgi_printf(c, "</div>\n"); /* #tree */
1687 fcgi_printf(c, "</div>\n"); /* #tree_content */
1694 static const struct got_error *
1695 gotweb_render_diff(struct request *c)
1697 const struct got_error *error = NULL;
1698 struct transport *t = c->t;
1699 struct repo_commit *rc = NULL;
1700 char *age = NULL, *author = NULL, *msg = NULL;
1703 error = got_get_repo_commits(c, 1);
1707 rc = TAILQ_FIRST(&t->repo_commits);
1709 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1712 error = gotweb_escape_html(&author, rc->author);
1715 error = gotweb_escape_html(&msg, rc->commit_msg);
1719 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1720 "<div id='diff_title'>Commit Diff</div>\n"
1721 "</div>\n" /* #diff_title_wrapper */
1722 "<div id='diff_content'>\n"
1723 "<div id='diff_header_wrapper'>\n"
1724 "<div id='diff_header'>\n"
1725 "<div id='header_diff_title'>Diff:</div>\n"
1726 "<div id='header_diff'>%s<br />%s</div>\n"
1727 "<div class='header_commit_title'>Commit:</div>\n"
1728 "<div class='header_commit'>%s</div>\n"
1729 "<div id='header_tree_title'>Tree:</div>\n"
1730 "<div id='header_tree'>%s</div>\n"
1731 "<div class='header_author_title'>Author:</div>\n"
1732 "<div class='header_author'>%s</div>\n"
1733 "<div class='header_age_title'>Date:</div>\n"
1734 "<div class='header_age'>%s</div>\n"
1735 "<div id='header_commit_msg_title'>Message:</div>\n"
1736 "<div id='header_commit_msg'>%s</div>\n"
1737 "</div>\n" /* #diff_header */
1738 "</div>\n" /* #diff_header_wrapper */
1739 "<div class='dotted_line'></div>\n"
1740 "<div id='diff'>\n",
1741 rc->parent_id, rc->commit_id,
1750 error = got_output_repo_diff(c);
1754 fcgi_printf(c, "</div>\n"); /* #diff */
1755 fcgi_printf(c, "</div>\n"); /* #diff_content */
1763 static const struct got_error *
1764 gotweb_render_summary(struct request *c)
1766 const struct got_error *error = NULL;
1767 struct transport *t = c->t;
1768 struct server *srv = c->srv;
1771 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1774 if (srv->show_repo_description) {
1776 "<div id='description_title'>Description:</div>\n"
1777 "<div id='description'>%s</div>\n",
1778 t->repo_dir->description ? t->repo_dir->description : "");
1783 if (srv->show_repo_owner) {
1785 "<div id='repo_owner_title'>Owner:</div>\n"
1786 "<div id='repo_owner'>%s</div>\n",
1787 t->repo_dir->owner ? t->repo_dir->owner : "");
1792 if (srv->show_repo_age) {
1794 "<div id='last_change_title'>Last Change:</div>\n"
1795 "<div id='last_change'>%s</div>\n",
1801 if (srv->show_repo_cloneurl) {
1803 "<div id='cloneurl_title'>Clone URL:</div>\n"
1804 "<div id='cloneurl'>%s</div>\n",
1805 t->repo_dir->url ? t->repo_dir->url : "");
1810 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1814 error = gotweb_render_briefs(c);
1816 log_warnx("%s: %s", __func__, error->msg);
1820 error = gotweb_render_tags(c);
1822 log_warnx("%s: %s", __func__, error->msg);
1826 error = gotweb_render_branches(c);
1828 log_warnx("%s: %s", __func__, error->msg);
1833 static const struct got_error *
1834 gotweb_render_tag(struct request *c)
1836 const struct got_error *error = NULL;
1837 struct repo_tag *rt = NULL;
1838 struct transport *t = c->t;
1839 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1841 error = got_get_repo_tags(c, 1);
1845 if (t->tag_count == 0) {
1846 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1851 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1853 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1856 error = gotweb_escape_html(&author, rt->tagger);
1859 error = gotweb_escape_html(&msg, rt->commit_msg);
1863 tagname = rt->tag_name;
1864 if (strncmp(tagname, "refs/", 5) == 0)
1866 error = gotweb_escape_html(&tagname, tagname);
1870 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1871 "<div id='tags_title'>Tag</div>\n"
1872 "</div>\n" /* #tags_title_wrapper */
1873 "<div id='tags_content'>\n"
1874 "<div id='tag_header_wrapper'>\n"
1875 "<div id='tag_header'>\n"
1876 "<div class='header_commit_title'>Commit:</div>\n"
1877 "<div class='header_commit'>%s"
1878 " <span class='refs_str'>(%s)</span></div>\n"
1879 "<div class='header_author_title'>Tagger:</div>\n"
1880 "<div class='header_author'>%s</div>\n"
1881 "<div class='header_age_title'>Date:</div>\n"
1882 "<div class='header_age'>%s</div>\n"
1883 "<div id='header_commit_msg_title'>Message:</div>\n"
1884 "<div id='header_commit_msg'>%s</div>\n"
1885 "</div>\n" /* #tag_header */
1886 "<div class='dotted_line'></div>\n"
1887 "<div id='tag_commit'>\n%s</div>"
1888 "</div>" /* #tag_header_wrapper */
1889 "</div>", /* #tags_content */
1904 static const struct got_error *
1905 gotweb_render_tags(struct request *c)
1907 const struct got_error *error = NULL;
1908 struct repo_tag *rt = NULL;
1909 struct server *srv = c->srv;
1910 struct transport *t = c->t;
1911 struct querystring *qs = t->qs;
1912 struct repo_dir *repo_dir = t->repo_dir;
1913 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1914 int r, commit_found = 0;
1916 if (qs->action == BRIEFS) {
1918 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1920 error = got_get_repo_tags(c, srv->max_commits_display);
1924 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1925 "<div id='tags_title'>Tags</div>\n"
1926 "</div>\n" /* #tags_title_wrapper */
1927 "<div id='tags_content'>\n");
1931 if (t->tag_count == 0) {
1932 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1933 "This repository contains no tags");
1938 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1939 if (commit_found == 0 && qs->commit != NULL) {
1940 if (strcmp(qs->commit, rt->commit_id) != 0)
1945 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1949 tagname = rt->tag_name;
1950 if (strncmp(tagname, "refs/tags/", 10) == 0)
1952 error = gotweb_escape_html(&tagname, tagname);
1956 if (rt->tag_commit != NULL) {
1957 newline = strchr(rt->tag_commit, '\n');
1960 error = gotweb_escape_html(&msg, rt->tag_commit);
1965 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1966 "<div class='tag'>%s</div>\n"
1967 "<div class='tag_log'>", age, tagname) == -1)
1970 r = gotweb_link(c, &(struct gotweb_url){
1974 .path = repo_dir->name,
1975 .commit = rt->commit_id,
1976 }, "%s", msg ? msg : "");
1980 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1981 "<div class='navs_wrapper'>\n"
1982 "<div class='navs'>") == -1)
1985 r = gotweb_link(c, &(struct gotweb_url){
1989 .path = repo_dir->name,
1990 .commit = rt->commit_id,
1995 if (fcgi_printf(c, " | ") == -1)
1998 r = gotweb_link(c, &(struct gotweb_url){
2002 .path = repo_dir->name,
2003 .commit = rt->commit_id,
2004 }, "commit briefs");
2008 if (fcgi_printf(c, " | ") == -1)
2011 r = gotweb_link(c, &(struct gotweb_url){
2015 .path = repo_dir->name,
2016 .commit = rt->commit_id,
2022 "</div>\n" /* .navs */
2023 "</div>\n" /* .navs_wrapper */
2024 "<div class='dotted_line'></div>\n");
2035 if (t->next_id || t->prev_id) {
2036 error = gotweb_render_navs(c);
2040 fcgi_printf(c, "</div>\n"); /* #tags_content */
2048 const struct got_error *
2049 gotweb_escape_html(char **escaped_html, const char *orig_html)
2051 const struct got_error *error = NULL;
2052 struct escape_pair {
2063 size_t orig_len, len;
2066 orig_len = strlen(orig_html);
2068 for (i = 0; i < orig_len; i++) {
2069 for (j = 0; j < nitems(esc); j++) {
2070 if (orig_html[i] != esc[j].c)
2072 len += strlen(esc[j].s) - 1 /* escaped char */;
2076 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2077 if (*escaped_html == NULL)
2078 return got_error_from_errno("calloc");
2081 for (i = 0; i < orig_len; i++) {
2083 for (j = 0; j < nitems(esc); j++) {
2084 if (orig_html[i] != esc[j].c)
2087 if (strlcat(*escaped_html, esc[j].s, len + 1)
2089 error = got_error(GOT_ERR_NO_SPACE);
2092 x += strlen(esc[j].s);
2097 (*escaped_html)[x] = orig_html[i];
2103 free(*escaped_html);
2104 *escaped_html = NULL;
2106 (*escaped_html)[x] = '\0';
2113 should_urlencode(int c)
2115 if (c <= ' ' || c >= 127)
2146 gotweb_urlencode(const char *str)
2154 for (s = str; *s; ++s) {
2156 if (should_urlencode(*s))
2160 escaped = calloc(1, len + 1);
2161 if (escaped == NULL)
2165 for (s = str; *s; ++s) {
2166 if (should_urlencode(*s)) {
2167 a = (*s & 0xF0) >> 4;
2171 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2172 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2180 static inline const char *
2181 action_name(int action)
2212 gotweb_print_url(struct request *c, struct gotweb_url *url)
2214 const char *sep = "?", *action;
2218 action = action_name(url->action);
2219 if (action != NULL) {
2220 if (fcgi_printf(c, "?action=%s", action) == -1)
2226 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2232 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2238 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2244 tmp = gotweb_urlencode(url->file);
2247 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2255 tmp = gotweb_urlencode(url->folder);
2258 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2266 tmp = gotweb_urlencode(url->headref);
2269 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2276 if (url->index_page != -1) {
2277 if (fcgi_printf(c, "%sindex_page=%d", sep,
2278 url->index_page) == -1)
2284 tmp = gotweb_urlencode(url->path);
2287 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2294 if (url->page != -1) {
2295 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2304 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2309 if (fcgi_printf(c, "<a href='") == -1)
2312 if (gotweb_print_url(c, url) == -1)
2315 if (fcgi_printf(c, "'>") == -1)
2319 r = fcgi_vprintf(c, fmt, ap);
2324 if (fcgi_printf(c, "</a>"))
2329 static struct got_repository *
2330 find_cached_repo(struct server *srv, const char *path)
2334 for (i = 0; i < srv->ncached_repos; i++) {
2335 if (strcmp(srv->cached_repos[i].path, path) == 0)
2336 return srv->cached_repos[i].repo;
2342 static const struct got_error *
2343 cache_repo(struct got_repository **new, struct server *srv,
2344 struct repo_dir *repo_dir, struct socket *sock)
2346 const struct got_error *error = NULL;
2347 struct got_repository *repo;
2348 struct cached_repo *cr;
2351 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2352 cr = &srv->cached_repos[srv->ncached_repos - 1];
2353 error = got_repo_close(cr->repo);
2354 memset(cr, 0, sizeof(*cr));
2355 srv->ncached_repos--;
2358 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2359 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2360 cr = &srv->cached_repos[0];
2363 cr = &srv->cached_repos[srv->ncached_repos];
2366 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2369 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2370 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2375 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2376 >= sizeof(cr->path)) {
2378 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2379 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2381 return got_error(GOT_ERR_NO_SPACE);
2385 srv->ncached_repos++;
2390 static const struct got_error *
2391 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2393 const struct got_error *error = NULL;
2394 struct socket *sock = c->sock;
2395 struct server *srv = c->srv;
2396 struct transport *t = c->t;
2397 struct got_repository *repo = NULL;
2401 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2402 GOTWEB_GIT_DIR) == -1)
2403 return got_error_from_errno("asprintf");
2405 dt = opendir(dir_test);
2409 repo_dir->path = dir_test;
2414 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2415 repo_dir->name) == -1)
2416 return got_error_from_errno("asprintf");
2418 dt = opendir(dir_test);
2420 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2423 repo_dir->path = dir_test;
2428 if (srv->respect_exportok &&
2429 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2430 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2434 repo = find_cached_repo(srv, repo_dir->path);
2436 error = cache_repo(&repo, srv, repo_dir, sock);
2441 error = gotweb_get_repo_description(&repo_dir->description, srv,
2442 repo_dir->path, dirfd(dt));
2445 error = got_get_repo_owner(&repo_dir->owner, c);
2448 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2451 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2455 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2456 error = got_error_from_errno("closedir");
2460 static const struct got_error *
2461 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2463 const struct got_error *error;
2465 *repo_dir = calloc(1, sizeof(**repo_dir));
2466 if (*repo_dir == NULL)
2467 return got_error_from_errno("calloc");
2469 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2470 error = got_error_from_errno("asprintf");
2475 (*repo_dir)->owner = NULL;
2476 (*repo_dir)->description = NULL;
2477 (*repo_dir)->url = NULL;
2478 (*repo_dir)->age = NULL;
2479 (*repo_dir)->path = NULL;
2484 static const struct got_error *
2485 gotweb_get_repo_description(char **description, struct server *srv,
2486 const char *dirpath, int dir)
2488 const struct got_error *error = NULL;
2493 *description = NULL;
2494 if (srv->show_repo_description == 0)
2497 fd = openat(dir, "description", O_RDONLY);
2499 if (errno != ENOENT && errno != EACCES) {
2500 error = got_error_from_errno_fmt("openat %s/%s",
2501 dirpath, "description");
2506 if (fstat(fd, &sb) == -1) {
2507 error = got_error_from_errno_fmt("fstat %s/%s",
2508 dirpath, "description");
2513 if (len > GOTWEBD_MAXDESCRSZ - 1)
2514 len = GOTWEBD_MAXDESCRSZ - 1;
2516 *description = calloc(len + 1, sizeof(**description));
2517 if (*description == NULL) {
2518 error = got_error_from_errno("calloc");
2522 if (read(fd, *description, len) == -1)
2523 error = got_error_from_errno("read");
2525 if (fd != -1 && close(fd) == -1 && error == NULL)
2526 error = got_error_from_errno("close");
2530 static const struct got_error *
2531 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2534 const struct got_error *error = NULL;
2540 if (srv->show_repo_cloneurl == 0)
2543 fd = openat(dir, "cloneurl", O_RDONLY);
2545 if (errno != ENOENT && errno != EACCES) {
2546 error = got_error_from_errno_fmt("openat %s/%s",
2547 dirpath, "cloneurl");
2552 if (fstat(fd, &sb) == -1) {
2553 error = got_error_from_errno_fmt("fstat %s/%s",
2554 dirpath, "cloneurl");
2559 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2560 len = GOTWEBD_MAXCLONEURLSZ - 1;
2562 *url = calloc(len + 1, sizeof(**url));
2564 error = got_error_from_errno("calloc");
2568 if (read(fd, *url, len) == -1)
2569 error = got_error_from_errno("read");
2571 if (fd != -1 && close(fd) == -1 && error == NULL)
2572 error = got_error_from_errno("close");
2576 const struct got_error *
2577 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2580 long long diff_time;
2581 const char *years = "years ago", *months = "months ago";
2582 const char *weeks = "weeks ago", *days = "days ago";
2583 const char *hours = "hours ago", *minutes = "minutes ago";
2584 const char *seconds = "seconds ago", *now = "right now";
2592 diff_time = time(NULL) - committer_time;
2593 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2594 if (asprintf(repo_age, "%lld %s",
2595 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2596 return got_error_from_errno("asprintf");
2597 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2598 if (asprintf(repo_age, "%lld %s",
2599 (diff_time / 60 / 60 / 24 / (365 / 12)),
2601 return got_error_from_errno("asprintf");
2602 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2603 if (asprintf(repo_age, "%lld %s",
2604 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2605 return got_error_from_errno("asprintf");
2606 } else if (diff_time > 60 * 60 * 24 * 2) {
2607 if (asprintf(repo_age, "%lld %s",
2608 (diff_time / 60 / 60 / 24), days) == -1)
2609 return got_error_from_errno("asprintf");
2610 } else if (diff_time > 60 * 60 * 2) {
2611 if (asprintf(repo_age, "%lld %s",
2612 (diff_time / 60 / 60), hours) == -1)
2613 return got_error_from_errno("asprintf");
2614 } else if (diff_time > 60 * 2) {
2615 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2617 return got_error_from_errno("asprintf");
2618 } else if (diff_time > 2) {
2619 if (asprintf(repo_age, "%lld %s", diff_time,
2621 return got_error_from_errno("asprintf");
2623 if (asprintf(repo_age, "%s", now) == -1)
2624 return got_error_from_errno("asprintf");
2628 if (gmtime_r(&committer_time, &tm) == NULL)
2629 return got_error_from_errno("gmtime_r");
2631 s = asctime_r(&tm, datebuf);
2633 return got_error_from_errno("asctime_r");
2635 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2636 return got_error_from_errno("asprintf");