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;
996 unsigned int d_cnt, d_i, d_disp = 0;
997 unsigned int d_skipped = 0;
1000 d = opendir(srv->repos_path);
1002 error = got_error_from_errno2("opendir", srv->repos_path);
1006 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1009 error = got_error_from_errno2("scandir", srv->repos_path);
1013 r = fcgi_printf(c, "<div id='index_header'>\n"
1014 "<div id='index_header_project'>Project</div>\n");
1018 if (srv->show_repo_description)
1019 if (fcgi_printf(c, "<div id='index_header_description'>"
1020 "Description</div>\n") == -1)
1022 if (srv->show_repo_owner)
1023 if (fcgi_printf(c, "<div id='index_header_owner'>"
1024 "Owner</div>\n") == -1)
1026 if (srv->show_repo_age)
1027 if (fcgi_printf(c, "<div id='index_header_age'>"
1028 "Last Change</div>\n") == -1)
1030 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1033 for (d_i = 0; d_i < d_cnt; d_i++) {
1034 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
1037 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1038 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
1043 error = got_path_dirent_type(&type, srv->repos_path,
1047 if (type != DT_DIR) {
1052 if (qs->index_page > 0 && (qs->index_page *
1053 srv->max_repos_display) > t->prev_disp) {
1058 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1062 error = gotweb_load_got_path(c, repo_dir);
1063 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1065 gotweb_free_repo_dir(repo_dir);
1070 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1076 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1077 "<div class='index_project'>") == -1)
1080 r = gotweb_link(c, &(struct gotweb_url){
1084 .path = repo_dir->name,
1085 }, "%s", repo_dir->name);
1089 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1092 if (srv->show_repo_description) {
1094 "<div class='index_project_description'>\n"
1095 "%s</div>\n", repo_dir->description);
1100 if (srv->show_repo_owner) {
1101 r = fcgi_printf(c, "<div class='index_project_owner'>"
1102 "%s</div>\n", repo_dir->owner);
1107 if (srv->show_repo_age) {
1108 r = fcgi_printf(c, "<div class='index_project_age'>"
1109 "%s</div>\n", repo_dir->age);
1114 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1115 "<div class='navs'>") == -1)
1118 r = gotweb_link(c, &(struct gotweb_url){
1122 .path = repo_dir->name
1127 if (fcgi_printf(c, " | ") == -1)
1130 r = gotweb_link(c, &(struct gotweb_url){
1134 .path = repo_dir->name
1135 }, "commit briefs");
1139 if (fcgi_printf(c, " | ") == -1)
1142 r = gotweb_link(c, &(struct gotweb_url){
1146 .path = repo_dir->name
1151 if (fcgi_printf(c, " | ") == -1)
1154 r = gotweb_link(c, &(struct gotweb_url){
1158 .path = repo_dir->name
1163 if (fcgi_printf(c, " | ") == -1)
1166 r = gotweb_link(c, &(struct gotweb_url){
1170 .path = repo_dir->name
1175 r = fcgi_printf(c, "</div>" /* .navs */
1176 "<div class='dotted_line'></div>\n"
1177 "</div>\n" /* .navs_wrapper */
1178 "</div>\n"); /* .index_wrapper */
1182 gotweb_free_repo_dir(repo_dir);
1185 if (d_disp == srv->max_repos_display)
1188 t->repos_total = d_cnt - d_skipped;
1190 if (srv->max_repos_display == 0)
1192 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1194 if (t->repos_total <= srv->max_repos ||
1195 t->repos_total <= srv->max_repos_display)
1198 error = gotweb_render_navs(c);
1203 for (d_i = 0; d_i < d_cnt; d_i++)
1207 if (d != NULL && closedir(d) == EOF && error == NULL)
1208 error = got_error_from_errno("closedir");
1212 static const struct got_error *
1213 gotweb_render_blame(struct request *c)
1215 const struct got_error *error = NULL;
1216 struct transport *t = c->t;
1217 struct repo_commit *rc = NULL;
1218 char *age = NULL, *msg = NULL;
1221 error = got_get_repo_commits(c, 1);
1225 rc = TAILQ_FIRST(&t->repo_commits);
1227 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1230 error = gotweb_escape_html(&msg, rc->commit_msg);
1234 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1235 "<div id='blame_title'>Blame</div>\n"
1236 "</div>\n" /* #blame_title_wrapper */
1237 "<div id='blame_content'>\n"
1238 "<div id='blame_header_wrapper'>\n"
1239 "<div id='blame_header'>\n"
1240 "<div class='header_age_title'>Date:</div>\n"
1241 "<div class='header_age'>%s</div>\n"
1242 "<div id='header_commit_msg_title'>Message:</div>\n"
1243 "<div id='header_commit_msg'>%s</div>\n"
1244 "</div>\n" /* #blame_header */
1245 "</div>\n" /* #blame_header_wrapper */
1246 "<div class='dotted_line'></div>\n"
1247 "<div id='blame'>\n",
1253 error = got_output_file_blame(c);
1257 fcgi_printf(c, "</div>\n" /* #blame */
1258 "</div>\n"); /* #blame_content */
1265 static const struct got_error *
1266 gotweb_render_briefs(struct request *c)
1268 const struct got_error *error = NULL;
1269 struct repo_commit *rc = NULL;
1270 struct server *srv = c->srv;
1271 struct transport *t = c->t;
1272 struct querystring *qs = t->qs;
1273 struct repo_dir *repo_dir = t->repo_dir;
1274 char *smallerthan, *newline;
1275 char *age = NULL, *author = NULL, *msg = NULL;
1278 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1279 "<div id='briefs_title'>Commit Briefs</div>\n"
1280 "</div>\n" /* #briefs_title_wrapper */
1281 "<div id='briefs_content'>\n");
1285 if (qs->action == SUMMARY) {
1286 qs->action = BRIEFS;
1287 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1289 error = got_get_repo_commits(c, srv->max_commits_display);
1293 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1294 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1298 smallerthan = strchr(rc->author, '<');
1300 *smallerthan = '\0';
1302 newline = strchr(rc->commit_msg, '\n');
1306 error = gotweb_escape_html(&author, rc->author);
1309 error = gotweb_escape_html(&msg, rc->commit_msg);
1313 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1314 "<div class='briefs_author'>%s</div>\n"
1315 "<div class='briefs_log'>",
1320 r = gotweb_link(c, &(struct gotweb_url){
1324 .path = repo_dir->name,
1325 .commit = rc->commit_id,
1326 .headref = qs->headref,
1334 error = gotweb_escape_html(&refs, rc->refs_str);
1338 " <span class='refs_str'>(%s)</span>", refs);
1343 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1346 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1347 "<div class='navs'>");
1351 r = gotweb_link(c, &(struct gotweb_url){
1355 .path = repo_dir->name,
1356 .commit = rc->commit_id,
1357 .headref = qs->headref,
1362 if (fcgi_printf(c, " | ") == -1)
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, "</div>\n" /* .navs */
1377 "</div>\n" /* .navs_wrapper */
1378 "<div class='dotted_line'></div>\n") == -1)
1389 if (t->next_id || t->prev_id) {
1390 error = gotweb_render_navs(c);
1394 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1402 static const struct got_error *
1403 gotweb_render_commits(struct request *c)
1405 const struct got_error *error = NULL;
1406 struct repo_commit *rc = NULL;
1407 struct server *srv = c->srv;
1408 struct transport *t = c->t;
1409 struct repo_dir *repo_dir = t->repo_dir;
1410 char *age = NULL, *author = NULL, *msg = NULL;
1413 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1414 "<div class='commits_title'>Commits</div>\n"
1415 "</div>\n" /* .commits_title_wrapper */
1416 "<div class='commits_content'>\n");
1420 error = got_get_repo_commits(c, srv->max_commits_display);
1424 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1425 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1428 error = gotweb_escape_html(&author, rc->author);
1431 error = gotweb_escape_html(&msg, rc->commit_msg);
1435 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1436 "<div class='commits_header'>\n"
1437 "<div class='header_commit_title'>Commit:</div>\n"
1438 "<div class='header_commit'>%s</div>\n"
1439 "<div class='header_author_title'>Author:</div>\n"
1440 "<div class='header_author'>%s</div>\n"
1441 "<div class='header_age_title'>Date:</div>\n"
1442 "<div class='header_age'>%s</div>\n"
1443 "</div>\n" /* .commits_header */
1444 "</div>\n" /* .commits_header_wrapper */
1445 "<div class='dotted_line'></div>\n"
1446 "<div class='commit'>\n%s</div>\n",
1454 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1455 "<div class='navs'>") == -1)
1458 r = gotweb_link(c, &(struct gotweb_url){
1462 .path = repo_dir->name,
1463 .commit = rc->commit_id,
1468 if (fcgi_printf(c, " | ") == -1)
1471 r = gotweb_link(c, &(struct gotweb_url){
1475 .path = repo_dir->name,
1476 .commit = rc->commit_id,
1481 if (fcgi_printf(c, "</div>\n" /* .navs */
1482 "</div>\n" /* .navs_wrapper */
1483 "<div class='dotted_line'></div>\n") == -1)
1494 if (t->next_id || t->prev_id) {
1495 error = gotweb_render_navs(c);
1499 fcgi_printf(c, "</div>\n"); /* .commits_content */
1507 static const struct got_error *
1508 gotweb_render_branches(struct request *c)
1510 const struct got_error *error = NULL;
1511 struct got_reflist_head refs;
1512 struct got_reflist_entry *re;
1513 struct transport *t = c->t;
1514 struct querystring *qs = t->qs;
1515 struct got_repository *repo = t->repo;
1516 char *escaped_refname = NULL;
1522 error = got_ref_list(&refs, repo, "refs/heads",
1523 got_ref_cmp_by_name, NULL);
1527 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1528 "<div id='branches_title'>Branches</div>\n"
1529 "</div>\n" /* #branches_title_wrapper */
1530 "<div id='branches_content'>\n");
1534 TAILQ_FOREACH(re, &refs, entry) {
1535 const char *refname = NULL;
1537 if (got_ref_is_symbolic(re->ref))
1540 refname = got_ref_get_name(re->ref);
1541 if (refname == NULL) {
1542 error = got_error_from_errno("strdup");
1545 if (strncmp(refname, "refs/heads/", 11) != 0)
1548 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1552 if (strncmp(refname, "refs/heads/", 11) == 0)
1554 error = gotweb_escape_html(&escaped_refname, refname);
1558 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1559 "<div class='branches_age'>%s</div>\n"
1560 "<div class='branches_space'> </div>\n"
1561 "<div class='branch'>", age);
1565 r = gotweb_link(c, &(struct gotweb_url){
1571 }, "%s", escaped_refname);
1575 if (fcgi_printf(c, "</div>\n" /* .branch */
1576 "<div class='navs_wrapper'>\n"
1577 "<div class='navs'>") == -1)
1580 r = gotweb_link(c, &(struct gotweb_url){
1590 if (fcgi_printf(c, " | ") == -1)
1593 r = gotweb_link(c, &(struct gotweb_url){
1599 }, "commit briefs");
1603 if (fcgi_printf(c, " | ") == -1)
1606 r = gotweb_link(c, &(struct gotweb_url){
1616 r = fcgi_printf(c, "</div>\n" /* .navs */
1617 "</div>\n" /* .navs_wrapper */
1618 "<div class='dotted_line'></div>\n"
1619 "</div>\n"); /* .branches_wrapper */
1625 free(escaped_refname);
1626 escaped_refname = NULL;
1628 fcgi_printf(c, "</div>\n"); /* #branches_content */
1631 free(escaped_refname);
1632 got_ref_list_free(&refs);
1636 static const struct got_error *
1637 gotweb_render_tree(struct request *c)
1639 const struct got_error *error = NULL;
1640 struct transport *t = c->t;
1641 struct repo_commit *rc = NULL;
1642 char *age = NULL, *msg = NULL;
1645 error = got_get_repo_commits(c, 1);
1649 rc = TAILQ_FIRST(&t->repo_commits);
1651 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1655 error = gotweb_escape_html(&msg, rc->commit_msg);
1659 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1660 "<div id='tree_title'>Tree</div>\n"
1661 "</div>\n" /* #tree_title_wrapper */
1662 "<div id='tree_content'>\n"
1663 "<div id='tree_header_wrapper'>\n"
1664 "<div id='tree_header'>\n"
1665 "<div id='header_tree_title'>Tree:</div>\n"
1666 "<div id='header_tree'>%s</div>\n"
1667 "<div class='header_age_title'>Date:</div>\n"
1668 "<div class='header_age'>%s</div>\n"
1669 "<div id='header_commit_msg_title'>Message:</div>\n"
1670 "<div id='header_commit_msg'>%s</div>\n"
1671 "</div>\n" /* #tree_header */
1672 "</div>\n" /* #tree_header_wrapper */
1673 "<div class='dotted_line'></div>\n"
1674 "<div id='tree'>\n",
1681 error = got_output_repo_tree(c);
1685 fcgi_printf(c, "</div>\n"); /* #tree */
1686 fcgi_printf(c, "</div>\n"); /* #tree_content */
1693 static const struct got_error *
1694 gotweb_render_diff(struct request *c)
1696 const struct got_error *error = NULL;
1697 struct transport *t = c->t;
1698 struct repo_commit *rc = NULL;
1699 char *age = NULL, *author = NULL, *msg = NULL;
1702 error = got_get_repo_commits(c, 1);
1706 rc = TAILQ_FIRST(&t->repo_commits);
1708 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1711 error = gotweb_escape_html(&author, rc->author);
1714 error = gotweb_escape_html(&msg, rc->commit_msg);
1718 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1719 "<div id='diff_title'>Commit Diff</div>\n"
1720 "</div>\n" /* #diff_title_wrapper */
1721 "<div id='diff_content'>\n"
1722 "<div id='diff_header_wrapper'>\n"
1723 "<div id='diff_header'>\n"
1724 "<div id='header_diff_title'>Diff:</div>\n"
1725 "<div id='header_diff'>%s<br />%s</div>\n"
1726 "<div class='header_commit_title'>Commit:</div>\n"
1727 "<div class='header_commit'>%s</div>\n"
1728 "<div id='header_tree_title'>Tree:</div>\n"
1729 "<div id='header_tree'>%s</div>\n"
1730 "<div class='header_author_title'>Author:</div>\n"
1731 "<div class='header_author'>%s</div>\n"
1732 "<div class='header_age_title'>Date:</div>\n"
1733 "<div class='header_age'>%s</div>\n"
1734 "<div id='header_commit_msg_title'>Message:</div>\n"
1735 "<div id='header_commit_msg'>%s</div>\n"
1736 "</div>\n" /* #diff_header */
1737 "</div>\n" /* #diff_header_wrapper */
1738 "<div class='dotted_line'></div>\n"
1739 "<div id='diff'>\n",
1740 rc->parent_id, rc->commit_id,
1749 error = got_output_repo_diff(c);
1753 fcgi_printf(c, "</div>\n"); /* #diff */
1754 fcgi_printf(c, "</div>\n"); /* #diff_content */
1762 static const struct got_error *
1763 gotweb_render_summary(struct request *c)
1765 const struct got_error *error = NULL;
1766 struct transport *t = c->t;
1767 struct server *srv = c->srv;
1770 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1773 if (srv->show_repo_description) {
1775 "<div id='description_title'>Description:</div>\n"
1776 "<div id='description'>%s</div>\n",
1777 t->repo_dir->description ? t->repo_dir->description : "");
1782 if (srv->show_repo_owner) {
1784 "<div id='repo_owner_title'>Owner:</div>\n"
1785 "<div id='repo_owner'>%s</div>\n",
1786 t->repo_dir->owner ? t->repo_dir->owner : "");
1791 if (srv->show_repo_age) {
1793 "<div id='last_change_title'>Last Change:</div>\n"
1794 "<div id='last_change'>%s</div>\n",
1800 if (srv->show_repo_cloneurl) {
1802 "<div id='cloneurl_title'>Clone URL:</div>\n"
1803 "<div id='cloneurl'>%s</div>\n",
1804 t->repo_dir->url ? t->repo_dir->url : "");
1809 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1813 error = gotweb_render_briefs(c);
1815 log_warnx("%s: %s", __func__, error->msg);
1819 error = gotweb_render_tags(c);
1821 log_warnx("%s: %s", __func__, error->msg);
1825 error = gotweb_render_branches(c);
1827 log_warnx("%s: %s", __func__, error->msg);
1832 static const struct got_error *
1833 gotweb_render_tag(struct request *c)
1835 const struct got_error *error = NULL;
1836 struct repo_tag *rt = NULL;
1837 struct transport *t = c->t;
1838 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1840 error = got_get_repo_tags(c, 1);
1844 if (t->tag_count == 0) {
1845 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1850 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1852 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1855 error = gotweb_escape_html(&author, rt->tagger);
1858 error = gotweb_escape_html(&msg, rt->commit_msg);
1862 tagname = rt->tag_name;
1863 if (strncmp(tagname, "refs/", 5) == 0)
1865 error = gotweb_escape_html(&tagname, tagname);
1869 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1870 "<div id='tags_title'>Tag</div>\n"
1871 "</div>\n" /* #tags_title_wrapper */
1872 "<div id='tags_content'>\n"
1873 "<div id='tag_header_wrapper'>\n"
1874 "<div id='tag_header'>\n"
1875 "<div class='header_commit_title'>Commit:</div>\n"
1876 "<div class='header_commit'>%s"
1877 " <span class='refs_str'>(%s)</span></div>\n"
1878 "<div class='header_author_title'>Tagger:</div>\n"
1879 "<div class='header_author'>%s</div>\n"
1880 "<div class='header_age_title'>Date:</div>\n"
1881 "<div class='header_age'>%s</div>\n"
1882 "<div id='header_commit_msg_title'>Message:</div>\n"
1883 "<div id='header_commit_msg'>%s</div>\n"
1884 "</div>\n" /* #tag_header */
1885 "<div class='dotted_line'></div>\n"
1886 "<div id='tag_commit'>\n%s</div>"
1887 "</div>" /* #tag_header_wrapper */
1888 "</div>", /* #tags_content */
1903 static const struct got_error *
1904 gotweb_render_tags(struct request *c)
1906 const struct got_error *error = NULL;
1907 struct repo_tag *rt = NULL;
1908 struct server *srv = c->srv;
1909 struct transport *t = c->t;
1910 struct querystring *qs = t->qs;
1911 struct repo_dir *repo_dir = t->repo_dir;
1912 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1913 int r, commit_found = 0;
1915 if (qs->action == BRIEFS) {
1917 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1919 error = got_get_repo_tags(c, srv->max_commits_display);
1923 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1924 "<div id='tags_title'>Tags</div>\n"
1925 "</div>\n" /* #tags_title_wrapper */
1926 "<div id='tags_content'>\n");
1930 if (t->tag_count == 0) {
1931 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1932 "This repository contains no tags");
1937 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1938 if (commit_found == 0 && qs->commit != NULL) {
1939 if (strcmp(qs->commit, rt->commit_id) != 0)
1944 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1948 tagname = rt->tag_name;
1949 if (strncmp(tagname, "refs/tags/", 10) == 0)
1951 error = gotweb_escape_html(&tagname, tagname);
1955 if (rt->tag_commit != NULL) {
1956 newline = strchr(rt->tag_commit, '\n');
1959 error = gotweb_escape_html(&msg, rt->tag_commit);
1964 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1965 "<div class='tag'>%s</div>\n"
1966 "<div class='tag_log'>", age, tagname) == -1)
1969 r = gotweb_link(c, &(struct gotweb_url){
1973 .path = repo_dir->name,
1974 .commit = rt->commit_id,
1975 }, "%s", msg ? msg : "");
1979 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1980 "<div class='navs_wrapper'>\n"
1981 "<div class='navs'>") == -1)
1984 r = gotweb_link(c, &(struct gotweb_url){
1988 .path = repo_dir->name,
1989 .commit = rt->commit_id,
1994 if (fcgi_printf(c, " | ") == -1)
1997 r = gotweb_link(c, &(struct gotweb_url){
2001 .path = repo_dir->name,
2002 .commit = rt->commit_id,
2003 }, "commit briefs");
2007 if (fcgi_printf(c, " | ") == -1)
2010 r = gotweb_link(c, &(struct gotweb_url){
2014 .path = repo_dir->name,
2015 .commit = rt->commit_id,
2021 "</div>\n" /* .navs */
2022 "</div>\n" /* .navs_wrapper */
2023 "<div class='dotted_line'></div>\n");
2034 if (t->next_id || t->prev_id) {
2035 error = gotweb_render_navs(c);
2039 fcgi_printf(c, "</div>\n"); /* #tags_content */
2047 const struct got_error *
2048 gotweb_escape_html(char **escaped_html, const char *orig_html)
2050 const struct got_error *error = NULL;
2051 struct escape_pair {
2062 size_t orig_len, len;
2065 orig_len = strlen(orig_html);
2067 for (i = 0; i < orig_len; i++) {
2068 for (j = 0; j < nitems(esc); j++) {
2069 if (orig_html[i] != esc[j].c)
2071 len += strlen(esc[j].s) - 1 /* escaped char */;
2075 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2076 if (*escaped_html == NULL)
2077 return got_error_from_errno("calloc");
2080 for (i = 0; i < orig_len; i++) {
2082 for (j = 0; j < nitems(esc); j++) {
2083 if (orig_html[i] != esc[j].c)
2086 if (strlcat(*escaped_html, esc[j].s, len + 1)
2088 error = got_error(GOT_ERR_NO_SPACE);
2091 x += strlen(esc[j].s);
2096 (*escaped_html)[x] = orig_html[i];
2102 free(*escaped_html);
2103 *escaped_html = NULL;
2105 (*escaped_html)[x] = '\0';
2112 should_urlencode(int c)
2114 if (c <= ' ' || c >= 127)
2145 gotweb_urlencode(const char *str)
2153 for (s = str; *s; ++s) {
2155 if (should_urlencode(*s))
2159 escaped = calloc(1, len + 1);
2160 if (escaped == NULL)
2164 for (s = str; *s; ++s) {
2165 if (should_urlencode(*s)) {
2166 a = (*s & 0xF0) >> 4;
2170 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2171 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2179 static inline const char *
2180 action_name(int action)
2211 gotweb_print_url(struct request *c, struct gotweb_url *url)
2213 const char *sep = "?", *action;
2217 action = action_name(url->action);
2218 if (action != NULL) {
2219 if (fcgi_printf(c, "?action=%s", action) == -1)
2225 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2231 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2237 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2243 tmp = gotweb_urlencode(url->file);
2246 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2254 tmp = gotweb_urlencode(url->folder);
2257 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2265 tmp = gotweb_urlencode(url->headref);
2268 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2275 if (url->index_page != -1) {
2276 if (fcgi_printf(c, "%sindex_page=%d", sep,
2277 url->index_page) == -1)
2283 tmp = gotweb_urlencode(url->path);
2286 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2293 if (url->page != -1) {
2294 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2303 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2308 if (fcgi_printf(c, "<a href='") == -1)
2311 if (gotweb_print_url(c, url) == -1)
2314 if (fcgi_printf(c, "'>") == -1)
2318 r = fcgi_vprintf(c, fmt, ap);
2323 if (fcgi_printf(c, "</a>"))
2328 static struct got_repository *
2329 find_cached_repo(struct server *srv, const char *path)
2333 for (i = 0; i < srv->ncached_repos; i++) {
2334 if (strcmp(srv->cached_repos[i].path, path) == 0)
2335 return srv->cached_repos[i].repo;
2341 static const struct got_error *
2342 cache_repo(struct got_repository **new, struct server *srv,
2343 struct repo_dir *repo_dir, struct socket *sock)
2345 const struct got_error *error = NULL;
2346 struct got_repository *repo;
2347 struct cached_repo *cr;
2350 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2351 cr = &srv->cached_repos[srv->ncached_repos - 1];
2352 error = got_repo_close(cr->repo);
2353 memset(cr, 0, sizeof(*cr));
2354 srv->ncached_repos--;
2357 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2358 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2359 cr = &srv->cached_repos[0];
2362 cr = &srv->cached_repos[srv->ncached_repos];
2365 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2368 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2369 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2374 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2375 >= sizeof(cr->path)) {
2377 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2378 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2380 return got_error(GOT_ERR_NO_SPACE);
2384 srv->ncached_repos++;
2389 static const struct got_error *
2390 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2392 const struct got_error *error = NULL;
2393 struct socket *sock = c->sock;
2394 struct server *srv = c->srv;
2395 struct transport *t = c->t;
2396 struct got_repository *repo = NULL;
2400 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2401 GOTWEB_GIT_DIR) == -1)
2402 return got_error_from_errno("asprintf");
2404 dt = opendir(dir_test);
2408 repo_dir->path = dir_test;
2413 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2414 repo_dir->name) == -1)
2415 return got_error_from_errno("asprintf");
2417 dt = opendir(dir_test);
2419 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2422 repo_dir->path = dir_test;
2427 if (srv->respect_exportok &&
2428 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2429 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2433 repo = find_cached_repo(srv, repo_dir->path);
2435 error = cache_repo(&repo, srv, repo_dir, sock);
2440 error = gotweb_get_repo_description(&repo_dir->description, srv,
2444 error = got_get_repo_owner(&repo_dir->owner, c);
2447 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2450 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2453 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2454 error = got_error_from_errno("closedir");
2458 static const struct got_error *
2459 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2461 const struct got_error *error;
2463 *repo_dir = calloc(1, sizeof(**repo_dir));
2464 if (*repo_dir == NULL)
2465 return got_error_from_errno("calloc");
2467 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2468 error = got_error_from_errno("asprintf");
2473 (*repo_dir)->owner = NULL;
2474 (*repo_dir)->description = NULL;
2475 (*repo_dir)->url = NULL;
2476 (*repo_dir)->age = NULL;
2477 (*repo_dir)->path = NULL;
2482 static const struct got_error *
2483 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2485 const struct got_error *error = NULL;
2487 char *d_file = NULL;
2491 *description = NULL;
2492 if (srv->show_repo_description == 0)
2495 if (asprintf(&d_file, "%s/description", dir) == -1)
2496 return got_error_from_errno("asprintf");
2498 f = fopen(d_file, "r");
2500 if (errno != ENOENT && errno != EACCES)
2501 error = got_error_from_errno2("fopen", d_file);
2505 if (fseek(f, 0, SEEK_END) == -1) {
2506 error = got_ferror(f, GOT_ERR_IO);
2511 error = got_ferror(f, GOT_ERR_IO);
2516 *description = strdup("");
2517 if (*description == NULL)
2518 error = got_error_from_errno("strdup");
2522 if (fseek(f, 0, SEEK_SET) == -1) {
2523 error = got_ferror(f, GOT_ERR_IO);
2526 *description = calloc(len + 1, sizeof(**description));
2527 if (*description == NULL) {
2528 error = got_error_from_errno("calloc");
2532 n = fread(*description, 1, len, f);
2533 if (n == 0 && ferror(f))
2534 error = got_ferror(f, GOT_ERR_IO);
2536 if (f != NULL && fclose(f) == EOF && error == NULL)
2537 error = got_error_from_errno("fclose");
2542 static const struct got_error *
2543 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2545 const struct got_error *error = NULL;
2547 char *d_file = NULL;
2553 if (srv->show_repo_cloneurl == 0)
2556 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2557 return got_error_from_errno("asprintf");
2559 f = fopen(d_file, "r");
2561 if (errno != ENOENT && errno != EACCES)
2562 error = got_error_from_errno2("fopen", d_file);
2566 if (fseek(f, 0, SEEK_END) == -1) {
2567 error = got_ferror(f, GOT_ERR_IO);
2572 error = got_ferror(f, GOT_ERR_IO);
2578 if (fseek(f, 0, SEEK_SET) == -1) {
2579 error = got_ferror(f, GOT_ERR_IO);
2583 *url = calloc(len + 1, sizeof(**url));
2585 error = got_error_from_errno("calloc");
2589 n = fread(*url, 1, len, f);
2590 if (n == 0 && ferror(f))
2591 error = got_ferror(f, GOT_ERR_IO);
2593 if (f != NULL && fclose(f) == EOF && error == NULL)
2594 error = got_error_from_errno("fclose");
2599 const struct got_error *
2600 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2603 long long diff_time;
2604 const char *years = "years ago", *months = "months ago";
2605 const char *weeks = "weeks ago", *days = "days ago";
2606 const char *hours = "hours ago", *minutes = "minutes ago";
2607 const char *seconds = "seconds ago", *now = "right now";
2615 diff_time = time(NULL) - committer_time;
2616 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2617 if (asprintf(repo_age, "%lld %s",
2618 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2619 return got_error_from_errno("asprintf");
2620 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2621 if (asprintf(repo_age, "%lld %s",
2622 (diff_time / 60 / 60 / 24 / (365 / 12)),
2624 return got_error_from_errno("asprintf");
2625 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2626 if (asprintf(repo_age, "%lld %s",
2627 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2628 return got_error_from_errno("asprintf");
2629 } else if (diff_time > 60 * 60 * 24 * 2) {
2630 if (asprintf(repo_age, "%lld %s",
2631 (diff_time / 60 / 60 / 24), days) == -1)
2632 return got_error_from_errno("asprintf");
2633 } else if (diff_time > 60 * 60 * 2) {
2634 if (asprintf(repo_age, "%lld %s",
2635 (diff_time / 60 / 60), hours) == -1)
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 2) {
2638 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2640 return got_error_from_errno("asprintf");
2641 } else if (diff_time > 2) {
2642 if (asprintf(repo_age, "%lld %s", diff_time,
2644 return got_error_from_errno("asprintf");
2646 if (asprintf(repo_age, "%s", now) == -1)
2647 return got_error_from_errno("asprintf");
2651 if (gmtime_r(&committer_time, &tm) == NULL)
2652 return got_error_from_errno("gmtime_r");
2654 s = asctime_r(&tm, datebuf);
2656 return got_error_from_errno("asctime_r");
2658 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2659 return got_error_from_errno("asprintf");