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"
50 #include "got_compat.h"
56 static const struct querystring_keys querystring_keys[] = {
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
67 static const struct action_keys action_keys[] = {
70 { "blobraw", BLOBRAW },
72 { "commits", COMMITS },
76 { "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_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
91 static const struct got_error *gotweb_load_got_path(struct request *c,
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
97 static const struct got_error *gotweb_render_blame(struct request *);
98 static const struct got_error *gotweb_render_diff(struct request *);
99 static const struct got_error *gotweb_render_summary(struct request *);
100 static const struct got_error *gotweb_render_tag(struct request *);
101 static const struct got_error *gotweb_render_tags(struct request *);
102 static const struct got_error *gotweb_render_branches(struct request *);
104 static void gotweb_free_querystring(struct querystring *);
105 static void gotweb_free_repo_dir(struct repo_dir *);
107 struct server *gotweb_get_server(uint8_t *, uint8_t *);
110 gotweb_process_request(struct request *c)
112 const struct got_error *error = NULL, *error2 = NULL;
113 struct got_blob_object *blob = NULL;
114 struct server *srv = NULL;
115 struct querystring *qs = NULL;
116 struct repo_dir *repo_dir = NULL;
117 uint8_t err[] = "gotwebd experienced an error: ";
118 int r, html = 0, fd = -1;
120 /* init the transport */
121 error = gotweb_init_transport(&c->t);
123 log_warnx("%s: %s", __func__, error->msg);
126 /* don't process any further if client disconnected */
127 if (c->sock->client_status == CLIENT_DISCONNECT)
129 /* get the gotwebd server */
130 srv = gotweb_get_server(c->server_name, c->http_host);
132 log_warnx("%s: error server is NULL", __func__);
136 /* parse our querystring */
137 error = gotweb_init_querystring(&qs);
139 log_warnx("%s: %s", __func__, error->msg);
143 error = gotweb_parse_querystring(&qs, c->querystring);
145 log_warnx("%s: %s", __func__, error->msg);
150 * certain actions require a commit id in the querystring. this stops
151 * bad actors from exploiting this by manually manipulating the
155 if (qs->action == BLAME || qs->action == BLOB ||
156 qs->action == BLOBRAW || qs->action == DIFF) {
157 if (qs->commit == NULL) {
158 error2 = got_error(GOT_ERR_QUERYSTRING);
163 if (qs->action != INDEX) {
164 error = gotweb_init_repo_dir(&repo_dir, qs->path);
167 error = gotweb_load_got_path(c, repo_dir);
168 c->t->repo_dir = repo_dir;
169 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
173 if (qs->action == BLOBRAW) {
174 error = got_get_repo_commits(c, 1);
177 error = got_output_file_blob(c);
179 log_warnx("%s: %s", __func__, error->msg);
185 if (qs->action == BLOB) {
187 struct gotweb_url url = {
192 .commit = qs->commit,
193 .folder = qs->folder,
197 error = got_get_repo_commits(c, 1);
201 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
205 fcgi_puts(c->tp, "Status: 302\r\n");
206 fcgi_puts(c->tp, "Location: ");
207 gotweb_render_url(c, &url);
208 fcgi_puts(c->tp, "\r\n\r\n");
213 if (qs->action == RSS) {
214 error = gotweb_render_content_type_file(c,
215 "application/rss+xml;charset=utf-8",
216 repo_dir->name, ".rss");
218 log_warnx("%s: %s", __func__, error->msg);
222 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
224 log_warnx("%s: %s", __func__, error->msg);
227 if (gotweb_render_rss(c->tp) == -1)
233 error = gotweb_render_content_type(c, "text/html");
235 log_warnx("%s: %s", __func__, error->msg);
240 if (gotweb_render_header(c->tp) == -1)
250 error = gotweb_render_blame(c);
252 log_warnx("%s: %s", __func__, error->msg);
257 if (gotweb_render_blob(c->tp, blob) == -1)
261 if (gotweb_render_briefs(c->tp) == -1)
265 error = got_get_repo_commits(c, srv->max_commits_display);
267 log_warnx("%s: %s", __func__, error->msg);
270 if (gotweb_render_commits(c->tp) == -1)
274 error = gotweb_render_diff(c);
276 log_warnx("%s: %s", __func__, error->msg);
281 error = gotweb_render_index(c);
283 log_warnx("%s: %s", __func__, error->msg);
288 error = gotweb_render_summary(c);
290 log_warnx("%s: %s", __func__, error->msg);
295 error = gotweb_render_tag(c);
297 log_warnx("%s: %s", __func__, error->msg);
302 error = gotweb_render_tags(c);
304 log_warnx("%s: %s", __func__, error->msg);
309 error = got_get_repo_commits(c, 1);
311 log_warnx("%s: %s", __func__, error->msg);
314 if (gotweb_render_tree(c->tp) == -1)
319 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
320 "Erorr: Bad Querystring");
328 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
330 if (fcgi_printf(c, "\n%s", err) == -1)
333 if (fcgi_printf(c, "%s", error->msg) == -1)
336 if (fcgi_printf(c, "see daemon logs for details") == -1)
339 if (html && fcgi_printf(c, "</div>\n") == -1)
343 got_object_blob_close(blob);
346 if (html && srv != NULL)
347 gotweb_render_footer(c->tp);
351 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
353 struct server *srv = NULL;
355 /* check against the server name first */
356 if (strlen(server_name) > 0)
357 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
358 if (strcmp(srv->name, server_name) == 0)
361 /* check against subdomain second */
362 if (strlen(subdomain) > 0)
363 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
364 if (strcmp(srv->name, subdomain) == 0)
367 /* if those fail, send first server */
368 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
375 const struct got_error *
376 gotweb_init_transport(struct transport **t)
378 const struct got_error *error = NULL;
380 *t = calloc(1, sizeof(**t));
382 return got_error_from_errno2("%s: calloc", __func__);
384 TAILQ_INIT(&(*t)->repo_commits);
385 TAILQ_INIT(&(*t)->repo_tags);
388 (*t)->repo_dir = NULL;
390 (*t)->next_id = NULL;
391 (*t)->prev_id = NULL;
398 static const struct got_error *
399 gotweb_init_querystring(struct querystring **qs)
401 const struct got_error *error = NULL;
403 *qs = calloc(1, sizeof(**qs));
405 return got_error_from_errno2("%s: calloc", __func__);
407 (*qs)->headref = strdup("HEAD");
408 if ((*qs)->headref == NULL) {
411 return got_error_from_errno2("%s: strdup", __func__);
414 (*qs)->action = INDEX;
415 (*qs)->commit = NULL;
417 (*qs)->folder = NULL;
418 (*qs)->index_page = 0;
424 static const struct got_error *
425 gotweb_parse_querystring(struct querystring **qs, char *qst)
427 const struct got_error *error = NULL;
428 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
429 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
436 return got_error_from_errno2("%s: strdup", __func__);
441 while (tok1_pair != NULL) {
442 strsep(&tok1_end, "&");
444 tok2 = strdup(tok1_pair);
447 return got_error_from_errno2("%s: strdup", __func__);
453 while (tok2_pair != NULL) {
454 strsep(&tok2_end, "=");
456 error = gotweb_assign_querystring(qs, tok2_pair,
461 tok2_pair = tok2_end;
464 tok1_pair = tok1_end;
475 * Adapted from usr.sbin/httpd/httpd.c url_decode.
477 static const struct got_error *
478 gotweb_urldecode(char *url)
490 /* Encoding character is followed by two hex chars */
491 if (!isxdigit((unsigned char)p[1]) ||
492 !isxdigit((unsigned char)p[2]) ||
493 (p[1] == '0' && p[2] == '0'))
494 return got_error(GOT_ERR_BAD_QUERYSTRING);
500 * We don't have to validate "hex" because it is
501 * guaranteed to include two hex chars followed by nul.
503 x = strtoul(hex, NULL, 16);
519 static const struct got_error *
520 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
522 const struct got_error *error = NULL;
526 error = gotweb_urldecode(value);
530 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
531 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
534 switch (querystring_keys[el_cnt].element) {
536 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
537 if (strcmp(value, action_keys[a_cnt].name) != 0)
539 else if (strcmp(value,
540 action_keys[a_cnt].name) == 0){
542 action_keys[a_cnt].action;
550 (*qs)->commit = strdup(value);
551 if ((*qs)->commit == NULL) {
552 error = got_error_from_errno2("%s: strdup",
558 (*qs)->file = strdup(value);
559 if ((*qs)->file == NULL) {
560 error = got_error_from_errno2("%s: strdup",
566 (*qs)->folder = strdup(value);
567 if ((*qs)->folder == NULL) {
568 error = got_error_from_errno2("%s: strdup",
574 free((*qs)->headref);
575 (*qs)->headref = strdup(value);
576 if ((*qs)->headref == NULL) {
577 error = got_error_from_errno2("%s: strdup",
583 if (strlen(value) == 0)
585 (*qs)->index_page = strtonum(value, INT64_MIN,
588 error = got_error_from_errno3("%s: strtonum %s",
592 if ((*qs)->index_page < 0)
593 (*qs)->index_page = 0;
596 (*qs)->path = strdup(value);
597 if ((*qs)->path == NULL) {
598 error = got_error_from_errno2("%s: strdup",
604 if (strlen(value) == 0)
606 (*qs)->page = strtonum(value, INT64_MIN,
609 error = got_error_from_errno3("%s: strtonum %s",
625 gotweb_free_repo_tag(struct repo_tag *rt)
630 free(rt->tag_commit);
631 free(rt->commit_msg);
638 gotweb_free_repo_commit(struct repo_commit *rc)
648 free(rc->commit_msg);
654 gotweb_free_querystring(struct querystring *qs)
667 gotweb_free_repo_dir(struct repo_dir *repo_dir)
669 if (repo_dir != NULL) {
670 free(repo_dir->name);
671 free(repo_dir->owner);
672 free(repo_dir->description);
675 free(repo_dir->path);
681 gotweb_free_transport(struct transport *t)
683 struct repo_commit *rc = NULL, *trc = NULL;
684 struct repo_tag *rt = NULL, *trt = NULL;
686 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
687 TAILQ_REMOVE(&t->repo_commits, rc, entry);
688 gotweb_free_repo_commit(rc);
690 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
691 TAILQ_REMOVE(&t->repo_tags, rt, entry);
692 gotweb_free_repo_tag(rt);
694 gotweb_free_repo_dir(t->repo_dir);
695 gotweb_free_querystring(t->qs);
701 const struct got_error *
702 gotweb_render_content_type(struct request *c, const char *type)
704 const char *csp = "default-src 'self'; script-src 'none'; "
705 "object-src 'none';";
708 "Content-Security-Policy: %s\r\n"
709 "Content-Type: %s\r\n\r\n",
714 const struct got_error *
715 gotweb_render_content_type_file(struct request *c, const char *type,
716 const char *file, const char *suffix)
718 fcgi_printf(c, "Content-type: %s\r\n"
719 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
720 type, file, suffix ? suffix : "");
725 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
726 struct gotweb_url *next, int *have_next)
728 struct transport *t = c->t;
729 struct querystring *qs = t->qs;
730 struct server *srv = c->srv;
732 *have_prev = *have_next = 0;
736 if (qs->index_page > 0) {
738 *prev = (struct gotweb_url){
740 .index_page = qs->index_page - 1,
744 if (t->next_disp == srv->max_repos_display &&
745 t->repos_total != (qs->index_page + 1) *
746 srv->max_repos_display) {
748 *next = (struct gotweb_url){
750 .index_page = qs->index_page + 1,
756 if (t->prev_id && qs->commit != NULL &&
757 strcmp(qs->commit, t->prev_id) != 0) {
759 *prev = (struct gotweb_url){
762 .page = qs->page - 1,
764 .commit = t->prev_id,
765 .headref = qs->headref,
770 *next = (struct gotweb_url){
773 .page = qs->page + 1,
775 .commit = t->next_id,
776 .headref = qs->headref,
781 if (t->prev_id && qs->commit != NULL &&
782 strcmp(qs->commit, t->prev_id) != 0) {
784 *prev = (struct gotweb_url){
787 .page = qs->page - 1,
789 .commit = t->prev_id,
790 .headref = qs->headref,
791 .folder = qs->folder,
797 *next = (struct gotweb_url){
800 .page = qs->page + 1,
802 .commit = t->next_id,
803 .headref = qs->headref,
804 .folder = qs->folder,
810 if (t->prev_id && qs->commit != NULL &&
811 strcmp(qs->commit, t->prev_id) != 0) {
813 *prev = (struct gotweb_url){
816 .page = qs->page - 1,
818 .commit = t->prev_id,
819 .headref = qs->headref,
824 *next = (struct gotweb_url){
827 .page = qs->page + 1,
829 .commit = t->next_id,
830 .headref = qs->headref,
837 static const struct got_error *
838 gotweb_render_index(struct request *c)
840 const struct got_error *error = NULL;
841 struct server *srv = c->srv;
842 struct transport *t = c->t;
843 struct querystring *qs = t->qs;
844 struct repo_dir *repo_dir = NULL;
846 struct dirent **sd_dent = NULL;
847 unsigned int d_cnt, d_i, d_disp = 0;
848 unsigned int d_skipped = 0;
851 d = opendir(srv->repos_path);
853 error = got_error_from_errno2("opendir", srv->repos_path);
857 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
860 error = got_error_from_errno2("scandir", srv->repos_path);
864 if (gotweb_render_repo_table_hdr(c->tp) == -1)
867 for (d_i = 0; d_i < d_cnt; d_i++) {
868 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
871 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
872 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
877 error = got_path_dirent_type(&type, srv->repos_path,
881 if (type != DT_DIR) {
886 if (qs->index_page > 0 && (qs->index_page *
887 srv->max_repos_display) > t->prev_disp) {
892 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
896 error = gotweb_load_got_path(c, repo_dir);
897 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
899 gotweb_free_repo_dir(repo_dir);
904 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
910 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
913 gotweb_free_repo_dir(repo_dir);
916 if (d_disp == srv->max_repos_display)
919 t->repos_total = d_cnt - d_skipped;
921 if (srv->max_repos_display == 0)
923 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
925 if (t->repos_total <= srv->max_repos ||
926 t->repos_total <= srv->max_repos_display)
929 if (gotweb_render_navs(c->tp) == -1)
933 for (d_i = 0; d_i < d_cnt; d_i++)
937 if (d != NULL && closedir(d) == EOF && error == NULL)
938 error = got_error_from_errno("closedir");
942 static const struct got_error *
943 gotweb_render_blame(struct request *c)
945 const struct got_error *error = NULL;
946 struct transport *t = c->t;
947 struct repo_commit *rc = NULL;
948 char *age = NULL, *msg = NULL;
951 error = got_get_repo_commits(c, 1);
955 rc = TAILQ_FIRST(&t->repo_commits);
957 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
960 error = gotweb_escape_html(&msg, rc->commit_msg);
964 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
965 "<div id='blame_title'>Blame</div>\n"
966 "</div>\n" /* #blame_title_wrapper */
967 "<div id='blame_content'>\n"
968 "<div id='blame_header_wrapper'>\n"
969 "<div id='blame_header'>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "<div id='header_commit_msg_title'>Message:</div>\n"
973 "<div id='header_commit_msg'>%s</div>\n"
974 "</div>\n" /* #blame_header */
975 "</div>\n" /* #blame_header_wrapper */
976 "<div class='dotted_line'></div>\n"
977 "<div id='blame'>\n",
983 error = got_output_file_blame(c);
987 fcgi_printf(c, "</div>\n" /* #blame */
988 "</div>\n"); /* #blame_content */
995 static const struct got_error *
996 gotweb_render_branches(struct request *c)
998 const struct got_error *error = NULL;
999 struct got_reflist_head refs;
1000 struct got_reflist_entry *re;
1001 struct transport *t = c->t;
1002 struct querystring *qs = t->qs;
1003 struct got_repository *repo = t->repo;
1004 char *escaped_refname = NULL;
1010 error = got_ref_list(&refs, repo, "refs/heads",
1011 got_ref_cmp_by_name, NULL);
1015 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1016 "<div id='branches_title'>Branches</div>\n"
1017 "</div>\n" /* #branches_title_wrapper */
1018 "<div id='branches_content'>\n");
1022 TAILQ_FOREACH(re, &refs, entry) {
1023 const char *refname = NULL;
1025 if (got_ref_is_symbolic(re->ref))
1028 refname = got_ref_get_name(re->ref);
1029 if (refname == NULL) {
1030 error = got_error_from_errno("strdup");
1033 if (strncmp(refname, "refs/heads/", 11) != 0)
1036 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1040 if (strncmp(refname, "refs/heads/", 11) == 0)
1042 error = gotweb_escape_html(&escaped_refname, refname);
1046 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1047 "<div class='branches_age'>%s</div>\n"
1048 "<div class='branches_space'> </div>\n"
1049 "<div class='branch'>", age);
1053 r = gotweb_link(c, &(struct gotweb_url){
1059 }, "%s", escaped_refname);
1063 if (fcgi_printf(c, "</div>\n" /* .branch */
1064 "<div class='navs_wrapper'>\n"
1065 "<div class='navs'>") == -1)
1068 r = gotweb_link(c, &(struct gotweb_url){
1078 if (fcgi_printf(c, " | ") == -1)
1081 r = gotweb_link(c, &(struct gotweb_url){
1087 }, "commit briefs");
1091 if (fcgi_printf(c, " | ") == -1)
1094 r = gotweb_link(c, &(struct gotweb_url){
1104 r = fcgi_printf(c, "</div>\n" /* .navs */
1105 "</div>\n" /* .navs_wrapper */
1106 "<div class='dotted_line'></div>\n"
1107 "</div>\n"); /* .branches_wrapper */
1113 free(escaped_refname);
1114 escaped_refname = NULL;
1116 fcgi_printf(c, "</div>\n"); /* #branches_content */
1119 free(escaped_refname);
1120 got_ref_list_free(&refs);
1124 static const struct got_error *
1125 gotweb_render_diff(struct request *c)
1127 const struct got_error *error = NULL;
1128 struct transport *t = c->t;
1129 struct repo_commit *rc = NULL;
1130 char *age = NULL, *author = NULL, *msg = NULL;
1133 error = got_get_repo_commits(c, 1);
1137 rc = TAILQ_FIRST(&t->repo_commits);
1139 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1142 error = gotweb_escape_html(&author, rc->author);
1145 error = gotweb_escape_html(&msg, rc->commit_msg);
1149 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1150 "<div id='diff_title'>Commit Diff</div>\n"
1151 "</div>\n" /* #diff_title_wrapper */
1152 "<div id='diff_content'>\n"
1153 "<div id='diff_header_wrapper'>\n"
1154 "<div id='diff_header'>\n"
1155 "<div id='header_diff_title'>Diff:</div>\n"
1156 "<div id='header_diff'>%s<br />%s</div>\n"
1157 "<div class='header_commit_title'>Commit:</div>\n"
1158 "<div class='header_commit'>%s</div>\n"
1159 "<div id='header_tree_title'>Tree:</div>\n"
1160 "<div id='header_tree'>%s</div>\n"
1161 "<div class='header_author_title'>Author:</div>\n"
1162 "<div class='header_author'>%s</div>\n"
1163 "<div class='header_age_title'>Date:</div>\n"
1164 "<div class='header_age'>%s</div>\n"
1165 "<div id='header_commit_msg_title'>Message:</div>\n"
1166 "<div id='header_commit_msg'>%s</div>\n"
1167 "</div>\n" /* #diff_header */
1168 "</div>\n" /* #diff_header_wrapper */
1169 "<div class='dotted_line'></div>\n"
1170 "<div id='diff'>\n",
1171 rc->parent_id, rc->commit_id,
1180 error = got_output_repo_diff(c);
1184 fcgi_printf(c, "</div>\n"); /* #diff */
1185 fcgi_printf(c, "</div>\n"); /* #diff_content */
1193 static const struct got_error *
1194 gotweb_render_summary(struct request *c)
1196 const struct got_error *error = NULL;
1197 struct transport *t = c->t;
1198 struct server *srv = c->srv;
1201 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1204 if (srv->show_repo_description) {
1206 "<div id='description_title'>Description:</div>\n"
1207 "<div id='description'>%s</div>\n",
1208 t->repo_dir->description ? t->repo_dir->description : "");
1213 if (srv->show_repo_owner) {
1215 "<div id='repo_owner_title'>Owner:</div>\n"
1216 "<div id='repo_owner'>%s</div>\n",
1217 t->repo_dir->owner ? t->repo_dir->owner : "");
1222 if (srv->show_repo_age) {
1224 "<div id='last_change_title'>Last Change:</div>\n"
1225 "<div id='last_change'>%s</div>\n",
1231 if (srv->show_repo_cloneurl) {
1233 "<div id='cloneurl_title'>Clone URL:</div>\n"
1234 "<div id='cloneurl'>%s</div>\n",
1235 t->repo_dir->url ? t->repo_dir->url : "");
1240 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1244 if (gotweb_render_briefs(c->tp) == -1)
1247 error = gotweb_render_tags(c);
1249 log_warnx("%s: %s", __func__, error->msg);
1253 error = gotweb_render_branches(c);
1255 log_warnx("%s: %s", __func__, error->msg);
1260 static const struct got_error *
1261 gotweb_render_tag(struct request *c)
1263 const struct got_error *error = NULL;
1264 struct repo_tag *rt = NULL;
1265 struct transport *t = c->t;
1266 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1268 error = got_get_repo_tags(c, 1);
1272 if (t->tag_count == 0) {
1273 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1278 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1280 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1283 error = gotweb_escape_html(&author, rt->tagger);
1286 error = gotweb_escape_html(&msg, rt->commit_msg);
1290 tagname = rt->tag_name;
1291 if (strncmp(tagname, "refs/", 5) == 0)
1293 error = gotweb_escape_html(&tagname, tagname);
1297 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1298 "<div id='tags_title'>Tag</div>\n"
1299 "</div>\n" /* #tags_title_wrapper */
1300 "<div id='tags_content'>\n"
1301 "<div id='tag_header_wrapper'>\n"
1302 "<div id='tag_header'>\n"
1303 "<div class='header_commit_title'>Commit:</div>\n"
1304 "<div class='header_commit'>%s"
1305 " <span class='refs_str'>(%s)</span></div>\n"
1306 "<div class='header_author_title'>Tagger:</div>\n"
1307 "<div class='header_author'>%s</div>\n"
1308 "<div class='header_age_title'>Date:</div>\n"
1309 "<div class='header_age'>%s</div>\n"
1310 "<div id='header_commit_msg_title'>Message:</div>\n"
1311 "<div id='header_commit_msg'>%s</div>\n"
1312 "</div>\n" /* #tag_header */
1313 "<div class='dotted_line'></div>\n"
1314 "<div id='tag_commit'>\n%s</div>"
1315 "</div>" /* #tag_header_wrapper */
1316 "</div>", /* #tags_content */
1331 static const struct got_error *
1332 gotweb_render_tags(struct request *c)
1334 const struct got_error *error = NULL;
1335 struct repo_tag *rt = NULL;
1336 struct server *srv = c->srv;
1337 struct transport *t = c->t;
1338 struct querystring *qs = t->qs;
1339 struct repo_dir *repo_dir = t->repo_dir;
1340 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1341 int r, commit_found = 0;
1343 if (qs->action == BRIEFS) {
1345 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1347 error = got_get_repo_tags(c, srv->max_commits_display);
1351 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1352 "<div id='tags_title'>Tags</div>\n"
1353 "</div>\n" /* #tags_title_wrapper */
1354 "<div id='tags_content'>\n");
1358 if (t->tag_count == 0) {
1359 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1360 "This repository contains no tags");
1365 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1366 if (commit_found == 0 && qs->commit != NULL) {
1367 if (strcmp(qs->commit, rt->commit_id) != 0)
1372 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1376 tagname = rt->tag_name;
1377 if (strncmp(tagname, "refs/tags/", 10) == 0)
1379 error = gotweb_escape_html(&tagname, tagname);
1383 if (rt->tag_commit != NULL) {
1384 newline = strchr(rt->tag_commit, '\n');
1387 error = gotweb_escape_html(&msg, rt->tag_commit);
1392 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1393 "<div class='tag'>%s</div>\n"
1394 "<div class='tag_log'>", age, tagname) == -1)
1397 r = gotweb_link(c, &(struct gotweb_url){
1401 .path = repo_dir->name,
1402 .commit = rt->commit_id,
1403 }, "%s", msg ? msg : "");
1407 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1408 "<div class='navs_wrapper'>\n"
1409 "<div class='navs'>") == -1)
1412 r = gotweb_link(c, &(struct gotweb_url){
1416 .path = repo_dir->name,
1417 .commit = rt->commit_id,
1422 if (fcgi_printf(c, " | ") == -1)
1425 r = gotweb_link(c, &(struct gotweb_url){
1429 .path = repo_dir->name,
1430 .commit = rt->commit_id,
1431 }, "commit briefs");
1435 if (fcgi_printf(c, " | ") == -1)
1438 r = gotweb_link(c, &(struct gotweb_url){
1442 .path = repo_dir->name,
1443 .commit = rt->commit_id,
1449 "</div>\n" /* .navs */
1450 "</div>\n" /* .navs_wrapper */
1451 "<div class='dotted_line'></div>\n");
1462 if (t->next_id || t->prev_id) {
1463 if (gotweb_render_navs(c->tp) == -1)
1466 fcgi_printf(c, "</div>\n"); /* #tags_content */
1474 const struct got_error *
1475 gotweb_escape_html(char **escaped_html, const char *orig_html)
1477 const struct got_error *error = NULL;
1478 struct escape_pair {
1489 size_t orig_len, len;
1492 orig_len = strlen(orig_html);
1494 for (i = 0; i < orig_len; i++) {
1495 for (j = 0; j < nitems(esc); j++) {
1496 if (orig_html[i] != esc[j].c)
1498 len += strlen(esc[j].s) - 1 /* escaped char */;
1502 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1503 if (*escaped_html == NULL)
1504 return got_error_from_errno("calloc");
1507 for (i = 0; i < orig_len; i++) {
1509 for (j = 0; j < nitems(esc); j++) {
1510 if (orig_html[i] != esc[j].c)
1513 if (strlcat(*escaped_html, esc[j].s, len + 1)
1515 error = got_error(GOT_ERR_NO_SPACE);
1518 x += strlen(esc[j].s);
1523 (*escaped_html)[x] = orig_html[i];
1529 free(*escaped_html);
1530 *escaped_html = NULL;
1532 (*escaped_html)[x] = '\0';
1539 should_urlencode(int c)
1541 if (c <= ' ' || c >= 127)
1565 /* needed because the URLs are embedded into the HTML */
1574 gotweb_urlencode(const char *str)
1582 for (s = str; *s; ++s) {
1584 if (should_urlencode(*s))
1588 escaped = calloc(1, len + 1);
1589 if (escaped == NULL)
1593 for (s = str; *s; ++s) {
1594 if (should_urlencode(*s)) {
1595 a = (*s & 0xF0) >> 4;
1599 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1600 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1609 gotweb_action_name(int action)
1644 gotweb_render_url(struct request *c, struct gotweb_url *url)
1646 const char *sep = "?", *action;
1650 action = gotweb_action_name(url->action);
1651 if (action != NULL) {
1652 if (fcgi_printf(c, "?action=%s", action) == -1)
1658 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1664 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1670 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1676 tmp = gotweb_urlencode(url->file);
1679 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1687 tmp = gotweb_urlencode(url->folder);
1690 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1698 tmp = gotweb_urlencode(url->headref);
1701 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1708 if (url->index_page != -1) {
1709 if (fcgi_printf(c, "%sindex_page=%d", sep,
1710 url->index_page) == -1)
1716 tmp = gotweb_urlencode(url->path);
1719 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1726 if (url->page != -1) {
1727 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1736 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1738 struct template *tp = c->tp;
1739 const char *proto = c->https ? "https" : "http";
1741 if (fcgi_puts(tp, proto) == -1 ||
1742 fcgi_puts(tp, "://") == -1 ||
1743 tp_htmlescape(tp, c->server_name) == -1 ||
1744 tp_htmlescape(tp, c->document_uri) == -1)
1747 return gotweb_render_url(c, url);
1751 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1756 if (fcgi_printf(c, "<a href='") == -1)
1759 if (gotweb_render_url(c, url) == -1)
1762 if (fcgi_printf(c, "'>") == -1)
1766 r = fcgi_vprintf(c, fmt, ap);
1771 if (fcgi_printf(c, "</a>"))
1776 static struct got_repository *
1777 find_cached_repo(struct server *srv, const char *path)
1781 for (i = 0; i < srv->ncached_repos; i++) {
1782 if (strcmp(srv->cached_repos[i].path, path) == 0)
1783 return srv->cached_repos[i].repo;
1789 static const struct got_error *
1790 cache_repo(struct got_repository **new, struct server *srv,
1791 struct repo_dir *repo_dir, struct socket *sock)
1793 const struct got_error *error = NULL;
1794 struct got_repository *repo;
1795 struct cached_repo *cr;
1798 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1799 cr = &srv->cached_repos[srv->ncached_repos - 1];
1800 error = got_repo_close(cr->repo);
1801 memset(cr, 0, sizeof(*cr));
1802 srv->ncached_repos--;
1805 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1806 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1807 cr = &srv->cached_repos[0];
1810 cr = &srv->cached_repos[srv->ncached_repos];
1813 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1816 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1817 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1822 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1823 >= sizeof(cr->path)) {
1825 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1826 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1828 return got_error(GOT_ERR_NO_SPACE);
1832 srv->ncached_repos++;
1837 static const struct got_error *
1838 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1840 const struct got_error *error = NULL;
1841 struct socket *sock = c->sock;
1842 struct server *srv = c->srv;
1843 struct transport *t = c->t;
1844 struct got_repository *repo = NULL;
1848 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1849 GOTWEB_GIT_DIR) == -1)
1850 return got_error_from_errno("asprintf");
1852 dt = opendir(dir_test);
1856 repo_dir->path = dir_test;
1861 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1862 repo_dir->name) == -1)
1863 return got_error_from_errno("asprintf");
1865 dt = opendir(dir_test);
1867 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1870 repo_dir->path = dir_test;
1875 if (srv->respect_exportok &&
1876 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1877 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1881 repo = find_cached_repo(srv, repo_dir->path);
1883 error = cache_repo(&repo, srv, repo_dir, sock);
1888 error = gotweb_get_repo_description(&repo_dir->description, srv,
1889 repo_dir->path, dirfd(dt));
1892 error = got_get_repo_owner(&repo_dir->owner, c);
1895 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1898 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1902 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1903 error = got_error_from_errno("closedir");
1907 static const struct got_error *
1908 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1910 const struct got_error *error;
1912 *repo_dir = calloc(1, sizeof(**repo_dir));
1913 if (*repo_dir == NULL)
1914 return got_error_from_errno("calloc");
1916 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1917 error = got_error_from_errno("asprintf");
1922 (*repo_dir)->owner = NULL;
1923 (*repo_dir)->description = NULL;
1924 (*repo_dir)->url = NULL;
1925 (*repo_dir)->age = NULL;
1926 (*repo_dir)->path = NULL;
1931 static const struct got_error *
1932 gotweb_get_repo_description(char **description, struct server *srv,
1933 const char *dirpath, int dir)
1935 const struct got_error *error = NULL;
1940 *description = NULL;
1941 if (srv->show_repo_description == 0)
1944 fd = openat(dir, "description", O_RDONLY);
1946 if (errno != ENOENT && errno != EACCES) {
1947 error = got_error_from_errno_fmt("openat %s/%s",
1948 dirpath, "description");
1953 if (fstat(fd, &sb) == -1) {
1954 error = got_error_from_errno_fmt("fstat %s/%s",
1955 dirpath, "description");
1960 if (len > GOTWEBD_MAXDESCRSZ - 1)
1961 len = GOTWEBD_MAXDESCRSZ - 1;
1963 *description = calloc(len + 1, sizeof(**description));
1964 if (*description == NULL) {
1965 error = got_error_from_errno("calloc");
1969 if (read(fd, *description, len) == -1)
1970 error = got_error_from_errno("read");
1972 if (fd != -1 && close(fd) == -1 && error == NULL)
1973 error = got_error_from_errno("close");
1977 static const struct got_error *
1978 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1981 const struct got_error *error = NULL;
1987 if (srv->show_repo_cloneurl == 0)
1990 fd = openat(dir, "cloneurl", O_RDONLY);
1992 if (errno != ENOENT && errno != EACCES) {
1993 error = got_error_from_errno_fmt("openat %s/%s",
1994 dirpath, "cloneurl");
1999 if (fstat(fd, &sb) == -1) {
2000 error = got_error_from_errno_fmt("fstat %s/%s",
2001 dirpath, "cloneurl");
2006 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2007 len = GOTWEBD_MAXCLONEURLSZ - 1;
2009 *url = calloc(len + 1, sizeof(**url));
2011 error = got_error_from_errno("calloc");
2015 if (read(fd, *url, len) == -1)
2016 error = got_error_from_errno("read");
2018 if (fd != -1 && close(fd) == -1 && error == NULL)
2019 error = got_error_from_errno("close");
2023 const struct got_error *
2024 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2027 long long diff_time;
2028 const char *years = "years ago", *months = "months ago";
2029 const char *weeks = "weeks ago", *days = "days ago";
2030 const char *hours = "hours ago", *minutes = "minutes ago";
2031 const char *seconds = "seconds ago", *now = "right now";
2040 diff_time = time(NULL) - committer_time;
2041 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2042 if (asprintf(repo_age, "%lld %s",
2043 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2044 return got_error_from_errno("asprintf");
2045 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2046 if (asprintf(repo_age, "%lld %s",
2047 (diff_time / 60 / 60 / 24 / (365 / 12)),
2049 return got_error_from_errno("asprintf");
2050 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2051 if (asprintf(repo_age, "%lld %s",
2052 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2053 return got_error_from_errno("asprintf");
2054 } else if (diff_time > 60 * 60 * 24 * 2) {
2055 if (asprintf(repo_age, "%lld %s",
2056 (diff_time / 60 / 60 / 24), days) == -1)
2057 return got_error_from_errno("asprintf");
2058 } else if (diff_time > 60 * 60 * 2) {
2059 if (asprintf(repo_age, "%lld %s",
2060 (diff_time / 60 / 60), hours) == -1)
2061 return got_error_from_errno("asprintf");
2062 } else if (diff_time > 60 * 2) {
2063 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2065 return got_error_from_errno("asprintf");
2066 } else if (diff_time > 2) {
2067 if (asprintf(repo_age, "%lld %s", diff_time,
2069 return got_error_from_errno("asprintf");
2071 if (asprintf(repo_age, "%s", now) == -1)
2072 return got_error_from_errno("asprintf");
2076 if (gmtime_r(&committer_time, &tm) == NULL)
2077 return got_error_from_errno("gmtime_r");
2079 s = asctime_r(&tm, datebuf);
2081 return got_error_from_errno("asctime_r");
2083 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2084 return got_error_from_errno("asprintf");
2087 if (gmtime_r(&committer_time, &tm) == NULL)
2088 return got_error_from_errno("gmtime_r");
2090 r = strftime(datebuf, sizeof(datebuf),
2091 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2093 return got_error(GOT_ERR_NO_SPACE);
2095 *repo_age = strdup(datebuf);
2096 if (*repo_age == NULL)
2097 return got_error_from_errno("asprintf");