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"
55 static const struct querystring_keys querystring_keys[] = {
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
66 static const struct action_keys action_keys[] = {
69 { "blobraw", BLOBRAW },
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 static const struct got_error *gotweb_load_got_path(struct request *c,
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_diff(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 static void gotweb_free_querystring(struct querystring *);
103 static void gotweb_free_repo_dir(struct repo_dir *);
105 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 gotweb_process_request(struct request *c)
110 const struct got_error *error = NULL, *error2 = NULL;
111 struct got_blob_object *blob = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
116 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
121 log_warnx("%s: %s", __func__, error->msg);
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
130 log_warnx("%s: error server is NULL", __func__);
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
137 log_warnx("%s: %s", __func__, error->msg);
141 error = gotweb_parse_querystring(&qs, c->querystring);
143 log_warnx("%s: %s", __func__, error->msg);
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
175 error = got_output_file_blob(c);
177 log_warnx("%s: %s", __func__, error->msg);
183 if (qs->action == BLOB) {
185 struct gotweb_url url = {
190 .commit = qs->commit,
191 .folder = qs->folder,
195 error = got_get_repo_commits(c, 1);
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
216 log_warnx("%s: %s", __func__, error->msg);
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 log_warnx("%s: %s", __func__, error->msg);
225 if (gotweb_render_rss(c->tp) == -1)
231 error = gotweb_render_content_type(c, "text/html");
233 log_warnx("%s: %s", __func__, error->msg);
238 if (gotweb_render_header(c->tp) == -1)
248 error = gotweb_render_blame(c);
250 log_warnx("%s: %s", __func__, error->msg);
255 if (gotweb_render_blob(c->tp, blob) == -1)
259 if (gotweb_render_briefs(c->tp) == -1)
263 error = got_get_repo_commits(c, srv->max_commits_display);
265 log_warnx("%s: %s", __func__, error->msg);
268 if (gotweb_render_commits(c->tp) == -1)
272 error = gotweb_render_diff(c);
274 log_warnx("%s: %s", __func__, error->msg);
279 error = gotweb_render_index(c);
281 log_warnx("%s: %s", __func__, error->msg);
286 error = gotweb_render_summary(c);
288 log_warnx("%s: %s", __func__, error->msg);
293 error = got_get_repo_tags(c, 1);
295 log_warnx("%s: %s", __func__, error->msg);
298 if (c->t->tag_count == 0) {
299 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
303 if (gotweb_render_tag(c->tp) == -1)
307 error = gotweb_render_tags(c);
309 log_warnx("%s: %s", __func__, error->msg);
314 error = got_get_repo_commits(c, 1);
316 log_warnx("%s: %s", __func__, error->msg);
319 if (gotweb_render_tree(c->tp) == -1)
324 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
325 "Erorr: Bad Querystring");
333 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
335 if (fcgi_printf(c, "\n%s", err) == -1)
338 if (fcgi_printf(c, "%s", error->msg) == -1)
341 if (fcgi_printf(c, "see daemon logs for details") == -1)
344 if (html && fcgi_printf(c, "</div>\n") == -1)
348 got_object_blob_close(blob);
351 if (html && srv != NULL)
352 gotweb_render_footer(c->tp);
356 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
358 struct server *srv = NULL;
360 /* check against the server name first */
361 if (strlen(server_name) > 0)
362 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
363 if (strcmp(srv->name, server_name) == 0)
366 /* check against subdomain second */
367 if (strlen(subdomain) > 0)
368 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
369 if (strcmp(srv->name, subdomain) == 0)
372 /* if those fail, send first server */
373 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
380 const struct got_error *
381 gotweb_init_transport(struct transport **t)
383 const struct got_error *error = NULL;
385 *t = calloc(1, sizeof(**t));
387 return got_error_from_errno2("%s: calloc", __func__);
389 TAILQ_INIT(&(*t)->repo_commits);
390 TAILQ_INIT(&(*t)->repo_tags);
393 (*t)->repo_dir = NULL;
395 (*t)->next_id = NULL;
396 (*t)->prev_id = NULL;
403 static const struct got_error *
404 gotweb_init_querystring(struct querystring **qs)
406 const struct got_error *error = NULL;
408 *qs = calloc(1, sizeof(**qs));
410 return got_error_from_errno2("%s: calloc", __func__);
412 (*qs)->headref = strdup("HEAD");
413 if ((*qs)->headref == NULL) {
416 return got_error_from_errno2("%s: strdup", __func__);
419 (*qs)->action = INDEX;
420 (*qs)->commit = NULL;
422 (*qs)->folder = NULL;
423 (*qs)->index_page = 0;
429 static const struct got_error *
430 gotweb_parse_querystring(struct querystring **qs, char *qst)
432 const struct got_error *error = NULL;
433 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
434 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
441 return got_error_from_errno2("%s: strdup", __func__);
446 while (tok1_pair != NULL) {
447 strsep(&tok1_end, "&");
449 tok2 = strdup(tok1_pair);
452 return got_error_from_errno2("%s: strdup", __func__);
458 while (tok2_pair != NULL) {
459 strsep(&tok2_end, "=");
461 error = gotweb_assign_querystring(qs, tok2_pair,
466 tok2_pair = tok2_end;
469 tok1_pair = tok1_end;
480 * Adapted from usr.sbin/httpd/httpd.c url_decode.
482 static const struct got_error *
483 gotweb_urldecode(char *url)
495 /* Encoding character is followed by two hex chars */
496 if (!isxdigit((unsigned char)p[1]) ||
497 !isxdigit((unsigned char)p[2]) ||
498 (p[1] == '0' && p[2] == '0'))
499 return got_error(GOT_ERR_BAD_QUERYSTRING);
505 * We don't have to validate "hex" because it is
506 * guaranteed to include two hex chars followed by nul.
508 x = strtoul(hex, NULL, 16);
524 static const struct got_error *
525 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
527 const struct got_error *error = NULL;
531 error = gotweb_urldecode(value);
535 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
536 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
539 switch (querystring_keys[el_cnt].element) {
541 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
542 if (strcmp(value, action_keys[a_cnt].name) != 0)
544 else if (strcmp(value,
545 action_keys[a_cnt].name) == 0){
547 action_keys[a_cnt].action;
555 (*qs)->commit = strdup(value);
556 if ((*qs)->commit == NULL) {
557 error = got_error_from_errno2("%s: strdup",
563 (*qs)->file = strdup(value);
564 if ((*qs)->file == NULL) {
565 error = got_error_from_errno2("%s: strdup",
571 (*qs)->folder = strdup(value);
572 if ((*qs)->folder == NULL) {
573 error = got_error_from_errno2("%s: strdup",
579 free((*qs)->headref);
580 (*qs)->headref = strdup(value);
581 if ((*qs)->headref == NULL) {
582 error = got_error_from_errno2("%s: strdup",
588 if (strlen(value) == 0)
590 (*qs)->index_page = strtonum(value, INT64_MIN,
593 error = got_error_from_errno3("%s: strtonum %s",
597 if ((*qs)->index_page < 0)
598 (*qs)->index_page = 0;
601 (*qs)->path = strdup(value);
602 if ((*qs)->path == NULL) {
603 error = got_error_from_errno2("%s: strdup",
609 if (strlen(value) == 0)
611 (*qs)->page = strtonum(value, INT64_MIN,
614 error = got_error_from_errno3("%s: strtonum %s",
630 gotweb_free_repo_tag(struct repo_tag *rt)
635 free(rt->tag_commit);
636 free(rt->commit_msg);
643 gotweb_free_repo_commit(struct repo_commit *rc)
653 free(rc->commit_msg);
659 gotweb_free_querystring(struct querystring *qs)
672 gotweb_free_repo_dir(struct repo_dir *repo_dir)
674 if (repo_dir != NULL) {
675 free(repo_dir->name);
676 free(repo_dir->owner);
677 free(repo_dir->description);
680 free(repo_dir->path);
686 gotweb_free_transport(struct transport *t)
688 struct repo_commit *rc = NULL, *trc = NULL;
689 struct repo_tag *rt = NULL, *trt = NULL;
691 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
692 TAILQ_REMOVE(&t->repo_commits, rc, entry);
693 gotweb_free_repo_commit(rc);
695 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
696 TAILQ_REMOVE(&t->repo_tags, rt, entry);
697 gotweb_free_repo_tag(rt);
699 gotweb_free_repo_dir(t->repo_dir);
700 gotweb_free_querystring(t->qs);
706 const struct got_error *
707 gotweb_render_content_type(struct request *c, const char *type)
709 const char *csp = "default-src 'self'; script-src 'none'; "
710 "object-src 'none';";
713 "Content-Security-Policy: %s\r\n"
714 "Content-Type: %s\r\n\r\n",
719 const struct got_error *
720 gotweb_render_content_type_file(struct request *c, const char *type,
721 const char *file, const char *suffix)
723 fcgi_printf(c, "Content-type: %s\r\n"
724 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
725 type, file, suffix ? suffix : "");
730 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
731 struct gotweb_url *next, int *have_next)
733 struct transport *t = c->t;
734 struct querystring *qs = t->qs;
735 struct server *srv = c->srv;
737 *have_prev = *have_next = 0;
741 if (qs->index_page > 0) {
743 *prev = (struct gotweb_url){
745 .index_page = qs->index_page - 1,
749 if (t->next_disp == srv->max_repos_display &&
750 t->repos_total != (qs->index_page + 1) *
751 srv->max_repos_display) {
753 *next = (struct gotweb_url){
755 .index_page = qs->index_page + 1,
761 if (t->prev_id && qs->commit != NULL &&
762 strcmp(qs->commit, t->prev_id) != 0) {
764 *prev = (struct gotweb_url){
767 .page = qs->page - 1,
769 .commit = t->prev_id,
770 .headref = qs->headref,
775 *next = (struct gotweb_url){
778 .page = qs->page + 1,
780 .commit = t->next_id,
781 .headref = qs->headref,
786 if (t->prev_id && qs->commit != NULL &&
787 strcmp(qs->commit, t->prev_id) != 0) {
789 *prev = (struct gotweb_url){
792 .page = qs->page - 1,
794 .commit = t->prev_id,
795 .headref = qs->headref,
796 .folder = qs->folder,
802 *next = (struct gotweb_url){
805 .page = qs->page + 1,
807 .commit = t->next_id,
808 .headref = qs->headref,
809 .folder = qs->folder,
815 if (t->prev_id && qs->commit != NULL &&
816 strcmp(qs->commit, t->prev_id) != 0) {
818 *prev = (struct gotweb_url){
821 .page = qs->page - 1,
823 .commit = t->prev_id,
824 .headref = qs->headref,
829 *next = (struct gotweb_url){
832 .page = qs->page + 1,
834 .commit = t->next_id,
835 .headref = qs->headref,
842 static const struct got_error *
843 gotweb_render_index(struct request *c)
845 const struct got_error *error = NULL;
846 struct server *srv = c->srv;
847 struct transport *t = c->t;
848 struct querystring *qs = t->qs;
849 struct repo_dir *repo_dir = NULL;
851 struct dirent **sd_dent = NULL;
852 unsigned int d_cnt, d_i, d_disp = 0;
853 unsigned int d_skipped = 0;
856 d = opendir(srv->repos_path);
858 error = got_error_from_errno2("opendir", srv->repos_path);
862 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
865 error = got_error_from_errno2("scandir", srv->repos_path);
869 if (gotweb_render_repo_table_hdr(c->tp) == -1)
872 for (d_i = 0; d_i < d_cnt; d_i++) {
873 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
876 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
877 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
882 error = got_path_dirent_type(&type, srv->repos_path,
886 if (type != DT_DIR) {
891 if (qs->index_page > 0 && (qs->index_page *
892 srv->max_repos_display) > t->prev_disp) {
897 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
901 error = gotweb_load_got_path(c, repo_dir);
902 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
904 gotweb_free_repo_dir(repo_dir);
909 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
915 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
918 gotweb_free_repo_dir(repo_dir);
921 if (d_disp == srv->max_repos_display)
924 t->repos_total = d_cnt - d_skipped;
926 if (srv->max_repos_display == 0)
928 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
930 if (t->repos_total <= srv->max_repos ||
931 t->repos_total <= srv->max_repos_display)
934 if (gotweb_render_navs(c->tp) == -1)
938 for (d_i = 0; d_i < d_cnt; d_i++)
942 if (d != NULL && closedir(d) == EOF && error == NULL)
943 error = got_error_from_errno("closedir");
947 static const struct got_error *
948 gotweb_render_blame(struct request *c)
950 const struct got_error *error = NULL;
951 struct transport *t = c->t;
952 struct repo_commit *rc = NULL;
953 char *age = NULL, *msg = NULL;
956 error = got_get_repo_commits(c, 1);
960 rc = TAILQ_FIRST(&t->repo_commits);
962 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
965 error = gotweb_escape_html(&msg, rc->commit_msg);
969 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
970 "<div id='blame_title'>Blame</div>\n"
971 "</div>\n" /* #blame_title_wrapper */
972 "<div id='blame_content'>\n"
973 "<div id='blame_header_wrapper'>\n"
974 "<div id='blame_header'>\n"
975 "<div class='header_age_title'>Date:</div>\n"
976 "<div class='header_age'>%s</div>\n"
977 "<div id='header_commit_msg_title'>Message:</div>\n"
978 "<div id='header_commit_msg'>%s</div>\n"
979 "</div>\n" /* #blame_header */
980 "</div>\n" /* #blame_header_wrapper */
981 "<div class='dotted_line'></div>\n"
982 "<div id='blame'>\n",
988 error = got_output_file_blame(c);
992 fcgi_printf(c, "</div>\n" /* #blame */
993 "</div>\n"); /* #blame_content */
1000 static const struct got_error *
1001 gotweb_render_branches(struct request *c)
1003 const struct got_error *error = NULL;
1004 struct got_reflist_head refs;
1005 struct got_reflist_entry *re;
1006 struct transport *t = c->t;
1007 struct querystring *qs = t->qs;
1008 struct got_repository *repo = t->repo;
1009 char *escaped_refname = NULL;
1015 error = got_ref_list(&refs, repo, "refs/heads",
1016 got_ref_cmp_by_name, NULL);
1020 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1021 "<div id='branches_title'>Branches</div>\n"
1022 "</div>\n" /* #branches_title_wrapper */
1023 "<div id='branches_content'>\n");
1027 TAILQ_FOREACH(re, &refs, entry) {
1028 const char *refname = NULL;
1030 if (got_ref_is_symbolic(re->ref))
1033 refname = got_ref_get_name(re->ref);
1034 if (refname == NULL) {
1035 error = got_error_from_errno("strdup");
1038 if (strncmp(refname, "refs/heads/", 11) != 0)
1041 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1045 if (strncmp(refname, "refs/heads/", 11) == 0)
1047 error = gotweb_escape_html(&escaped_refname, refname);
1051 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1052 "<div class='branches_age'>%s</div>\n"
1053 "<div class='branches_space'> </div>\n"
1054 "<div class='branch'>", age);
1058 r = gotweb_link(c, &(struct gotweb_url){
1064 }, "%s", escaped_refname);
1068 if (fcgi_printf(c, "</div>\n" /* .branch */
1069 "<div class='navs_wrapper'>\n"
1070 "<div class='navs'>") == -1)
1073 r = gotweb_link(c, &(struct gotweb_url){
1083 if (fcgi_printf(c, " | ") == -1)
1086 r = gotweb_link(c, &(struct gotweb_url){
1092 }, "commit briefs");
1096 if (fcgi_printf(c, " | ") == -1)
1099 r = gotweb_link(c, &(struct gotweb_url){
1109 r = fcgi_printf(c, "</div>\n" /* .navs */
1110 "</div>\n" /* .navs_wrapper */
1111 "<div class='dotted_line'></div>\n"
1112 "</div>\n"); /* .branches_wrapper */
1118 free(escaped_refname);
1119 escaped_refname = NULL;
1121 fcgi_printf(c, "</div>\n"); /* #branches_content */
1124 free(escaped_refname);
1125 got_ref_list_free(&refs);
1129 static const struct got_error *
1130 gotweb_render_diff(struct request *c)
1132 const struct got_error *error = NULL;
1133 struct transport *t = c->t;
1134 struct repo_commit *rc = NULL;
1135 char *age = NULL, *author = NULL, *msg = NULL;
1138 error = got_get_repo_commits(c, 1);
1142 rc = TAILQ_FIRST(&t->repo_commits);
1144 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1147 error = gotweb_escape_html(&author, rc->author);
1150 error = gotweb_escape_html(&msg, rc->commit_msg);
1154 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1155 "<div id='diff_title'>Commit Diff</div>\n"
1156 "</div>\n" /* #diff_title_wrapper */
1157 "<div id='diff_content'>\n"
1158 "<div id='diff_header_wrapper'>\n"
1159 "<div id='diff_header'>\n"
1160 "<div id='header_diff_title'>Diff:</div>\n"
1161 "<div id='header_diff'>%s<br />%s</div>\n"
1162 "<div class='header_commit_title'>Commit:</div>\n"
1163 "<div class='header_commit'>%s</div>\n"
1164 "<div id='header_tree_title'>Tree:</div>\n"
1165 "<div id='header_tree'>%s</div>\n"
1166 "<div class='header_author_title'>Author:</div>\n"
1167 "<div class='header_author'>%s</div>\n"
1168 "<div class='header_age_title'>Date:</div>\n"
1169 "<div class='header_age'>%s</div>\n"
1170 "<div id='header_commit_msg_title'>Message:</div>\n"
1171 "<div id='header_commit_msg'>%s</div>\n"
1172 "</div>\n" /* #diff_header */
1173 "</div>\n" /* #diff_header_wrapper */
1174 "<div class='dotted_line'></div>\n"
1175 "<div id='diff'>\n",
1176 rc->parent_id, rc->commit_id,
1185 error = got_output_repo_diff(c);
1189 fcgi_printf(c, "</div>\n"); /* #diff */
1190 fcgi_printf(c, "</div>\n"); /* #diff_content */
1198 static const struct got_error *
1199 gotweb_render_summary(struct request *c)
1201 const struct got_error *error = NULL;
1202 struct transport *t = c->t;
1203 struct server *srv = c->srv;
1206 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1209 if (srv->show_repo_description) {
1211 "<div id='description_title'>Description:</div>\n"
1212 "<div id='description'>%s</div>\n",
1213 t->repo_dir->description ? t->repo_dir->description : "");
1218 if (srv->show_repo_owner) {
1220 "<div id='repo_owner_title'>Owner:</div>\n"
1221 "<div id='repo_owner'>%s</div>\n",
1222 t->repo_dir->owner ? t->repo_dir->owner : "");
1227 if (srv->show_repo_age) {
1229 "<div id='last_change_title'>Last Change:</div>\n"
1230 "<div id='last_change'>%s</div>\n",
1236 if (srv->show_repo_cloneurl) {
1238 "<div id='cloneurl_title'>Clone URL:</div>\n"
1239 "<div id='cloneurl'>%s</div>\n",
1240 t->repo_dir->url ? t->repo_dir->url : "");
1245 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1249 if (gotweb_render_briefs(c->tp) == -1)
1252 error = gotweb_render_tags(c);
1254 log_warnx("%s: %s", __func__, error->msg);
1258 error = gotweb_render_branches(c);
1260 log_warnx("%s: %s", __func__, error->msg);
1265 static const struct got_error *
1266 gotweb_render_tags(struct request *c)
1268 const struct got_error *error = NULL;
1269 struct server *srv = c->srv;
1270 struct transport *t = c->t;
1271 struct querystring *qs = t->qs;
1273 if (qs->action == BRIEFS) {
1275 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1277 error = got_get_repo_tags(c, srv->max_commits_display);
1281 if (gotweb_render_tags_tmpl(c->tp) == -1)
1288 const struct got_error *
1289 gotweb_escape_html(char **escaped_html, const char *orig_html)
1291 const struct got_error *error = NULL;
1292 struct escape_pair {
1303 size_t orig_len, len;
1306 orig_len = strlen(orig_html);
1308 for (i = 0; i < orig_len; i++) {
1309 for (j = 0; j < nitems(esc); j++) {
1310 if (orig_html[i] != esc[j].c)
1312 len += strlen(esc[j].s) - 1 /* escaped char */;
1316 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1317 if (*escaped_html == NULL)
1318 return got_error_from_errno("calloc");
1321 for (i = 0; i < orig_len; i++) {
1323 for (j = 0; j < nitems(esc); j++) {
1324 if (orig_html[i] != esc[j].c)
1327 if (strlcat(*escaped_html, esc[j].s, len + 1)
1329 error = got_error(GOT_ERR_NO_SPACE);
1332 x += strlen(esc[j].s);
1337 (*escaped_html)[x] = orig_html[i];
1343 free(*escaped_html);
1344 *escaped_html = NULL;
1346 (*escaped_html)[x] = '\0';
1353 should_urlencode(int c)
1355 if (c <= ' ' || c >= 127)
1379 /* needed because the URLs are embedded into the HTML */
1388 gotweb_urlencode(const char *str)
1396 for (s = str; *s; ++s) {
1398 if (should_urlencode(*s))
1402 escaped = calloc(1, len + 1);
1403 if (escaped == NULL)
1407 for (s = str; *s; ++s) {
1408 if (should_urlencode(*s)) {
1409 a = (*s & 0xF0) >> 4;
1413 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1414 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1423 gotweb_action_name(int action)
1458 gotweb_render_url(struct request *c, struct gotweb_url *url)
1460 const char *sep = "?", *action;
1464 action = gotweb_action_name(url->action);
1465 if (action != NULL) {
1466 if (fcgi_printf(c, "?action=%s", action) == -1)
1472 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1478 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1484 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1490 tmp = gotweb_urlencode(url->file);
1493 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1501 tmp = gotweb_urlencode(url->folder);
1504 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1512 tmp = gotweb_urlencode(url->headref);
1515 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1522 if (url->index_page != -1) {
1523 if (fcgi_printf(c, "%sindex_page=%d", sep,
1524 url->index_page) == -1)
1530 tmp = gotweb_urlencode(url->path);
1533 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1540 if (url->page != -1) {
1541 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1550 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1552 struct template *tp = c->tp;
1553 const char *proto = c->https ? "https" : "http";
1555 if (fcgi_puts(tp, proto) == -1 ||
1556 fcgi_puts(tp, "://") == -1 ||
1557 tp_htmlescape(tp, c->server_name) == -1 ||
1558 tp_htmlescape(tp, c->document_uri) == -1)
1561 return gotweb_render_url(c, url);
1565 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1570 if (fcgi_printf(c, "<a href='") == -1)
1573 if (gotweb_render_url(c, url) == -1)
1576 if (fcgi_printf(c, "'>") == -1)
1580 r = fcgi_vprintf(c, fmt, ap);
1585 if (fcgi_printf(c, "</a>"))
1590 static struct got_repository *
1591 find_cached_repo(struct server *srv, const char *path)
1595 for (i = 0; i < srv->ncached_repos; i++) {
1596 if (strcmp(srv->cached_repos[i].path, path) == 0)
1597 return srv->cached_repos[i].repo;
1603 static const struct got_error *
1604 cache_repo(struct got_repository **new, struct server *srv,
1605 struct repo_dir *repo_dir, struct socket *sock)
1607 const struct got_error *error = NULL;
1608 struct got_repository *repo;
1609 struct cached_repo *cr;
1612 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1613 cr = &srv->cached_repos[srv->ncached_repos - 1];
1614 error = got_repo_close(cr->repo);
1615 memset(cr, 0, sizeof(*cr));
1616 srv->ncached_repos--;
1619 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1620 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1621 cr = &srv->cached_repos[0];
1624 cr = &srv->cached_repos[srv->ncached_repos];
1627 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1630 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1631 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1636 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1637 >= sizeof(cr->path)) {
1639 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1640 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1642 return got_error(GOT_ERR_NO_SPACE);
1646 srv->ncached_repos++;
1651 static const struct got_error *
1652 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1654 const struct got_error *error = NULL;
1655 struct socket *sock = c->sock;
1656 struct server *srv = c->srv;
1657 struct transport *t = c->t;
1658 struct got_repository *repo = NULL;
1662 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1663 GOTWEB_GIT_DIR) == -1)
1664 return got_error_from_errno("asprintf");
1666 dt = opendir(dir_test);
1670 repo_dir->path = dir_test;
1675 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1676 repo_dir->name) == -1)
1677 return got_error_from_errno("asprintf");
1679 dt = opendir(dir_test);
1681 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1684 repo_dir->path = dir_test;
1689 if (srv->respect_exportok &&
1690 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1691 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1695 repo = find_cached_repo(srv, repo_dir->path);
1697 error = cache_repo(&repo, srv, repo_dir, sock);
1702 error = gotweb_get_repo_description(&repo_dir->description, srv,
1703 repo_dir->path, dirfd(dt));
1706 error = got_get_repo_owner(&repo_dir->owner, c);
1709 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1712 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1716 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1717 error = got_error_from_errno("closedir");
1721 static const struct got_error *
1722 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1724 const struct got_error *error;
1726 *repo_dir = calloc(1, sizeof(**repo_dir));
1727 if (*repo_dir == NULL)
1728 return got_error_from_errno("calloc");
1730 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1731 error = got_error_from_errno("asprintf");
1736 (*repo_dir)->owner = NULL;
1737 (*repo_dir)->description = NULL;
1738 (*repo_dir)->url = NULL;
1739 (*repo_dir)->age = NULL;
1740 (*repo_dir)->path = NULL;
1745 static const struct got_error *
1746 gotweb_get_repo_description(char **description, struct server *srv,
1747 const char *dirpath, int dir)
1749 const struct got_error *error = NULL;
1754 *description = NULL;
1755 if (srv->show_repo_description == 0)
1758 fd = openat(dir, "description", O_RDONLY);
1760 if (errno != ENOENT && errno != EACCES) {
1761 error = got_error_from_errno_fmt("openat %s/%s",
1762 dirpath, "description");
1767 if (fstat(fd, &sb) == -1) {
1768 error = got_error_from_errno_fmt("fstat %s/%s",
1769 dirpath, "description");
1774 if (len > GOTWEBD_MAXDESCRSZ - 1)
1775 len = GOTWEBD_MAXDESCRSZ - 1;
1777 *description = calloc(len + 1, sizeof(**description));
1778 if (*description == NULL) {
1779 error = got_error_from_errno("calloc");
1783 if (read(fd, *description, len) == -1)
1784 error = got_error_from_errno("read");
1786 if (fd != -1 && close(fd) == -1 && error == NULL)
1787 error = got_error_from_errno("close");
1791 static const struct got_error *
1792 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1795 const struct got_error *error = NULL;
1801 if (srv->show_repo_cloneurl == 0)
1804 fd = openat(dir, "cloneurl", O_RDONLY);
1806 if (errno != ENOENT && errno != EACCES) {
1807 error = got_error_from_errno_fmt("openat %s/%s",
1808 dirpath, "cloneurl");
1813 if (fstat(fd, &sb) == -1) {
1814 error = got_error_from_errno_fmt("fstat %s/%s",
1815 dirpath, "cloneurl");
1820 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1821 len = GOTWEBD_MAXCLONEURLSZ - 1;
1823 *url = calloc(len + 1, sizeof(**url));
1825 error = got_error_from_errno("calloc");
1829 if (read(fd, *url, len) == -1)
1830 error = got_error_from_errno("read");
1832 if (fd != -1 && close(fd) == -1 && error == NULL)
1833 error = got_error_from_errno("close");
1837 const struct got_error *
1838 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1841 long long diff_time;
1842 const char *years = "years ago", *months = "months ago";
1843 const char *weeks = "weeks ago", *days = "days ago";
1844 const char *hours = "hours ago", *minutes = "minutes ago";
1845 const char *seconds = "seconds ago", *now = "right now";
1854 diff_time = time(NULL) - committer_time;
1855 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1856 if (asprintf(repo_age, "%lld %s",
1857 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1858 return got_error_from_errno("asprintf");
1859 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1860 if (asprintf(repo_age, "%lld %s",
1861 (diff_time / 60 / 60 / 24 / (365 / 12)),
1863 return got_error_from_errno("asprintf");
1864 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1865 if (asprintf(repo_age, "%lld %s",
1866 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1867 return got_error_from_errno("asprintf");
1868 } else if (diff_time > 60 * 60 * 24 * 2) {
1869 if (asprintf(repo_age, "%lld %s",
1870 (diff_time / 60 / 60 / 24), days) == -1)
1871 return got_error_from_errno("asprintf");
1872 } else if (diff_time > 60 * 60 * 2) {
1873 if (asprintf(repo_age, "%lld %s",
1874 (diff_time / 60 / 60), hours) == -1)
1875 return got_error_from_errno("asprintf");
1876 } else if (diff_time > 60 * 2) {
1877 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1879 return got_error_from_errno("asprintf");
1880 } else if (diff_time > 2) {
1881 if (asprintf(repo_age, "%lld %s", diff_time,
1883 return got_error_from_errno("asprintf");
1885 if (asprintf(repo_age, "%s", now) == -1)
1886 return got_error_from_errno("asprintf");
1890 if (gmtime_r(&committer_time, &tm) == NULL)
1891 return got_error_from_errno("gmtime_r");
1893 s = asctime_r(&tm, datebuf);
1895 return got_error_from_errno("asctime_r");
1897 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1898 return got_error_from_errno("asprintf");
1901 if (gmtime_r(&committer_time, &tm) == NULL)
1902 return got_error_from_errno("gmtime_r");
1904 r = strftime(datebuf, sizeof(datebuf),
1905 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1907 return got_error(GOT_ERR_NO_SPACE);
1909 *repo_age = strdup(datebuf);
1910 if (*repo_age == NULL)
1911 return got_error_from_errno("asprintf");