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"
54 static const struct querystring_keys querystring_keys[] = {
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
65 static const struct action_keys action_keys[] = {
69 { "commits", COMMITS },
73 { "summary", SUMMARY },
79 static const struct got_error *gotweb_init_querystring(struct querystring **);
80 static const struct got_error *gotweb_parse_querystring(struct querystring **,
82 static const struct got_error *gotweb_assign_querystring(struct querystring **,
84 static const struct got_error *gotweb_render_index(struct request *);
85 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
87 static const struct got_error *gotweb_load_got_path(struct request *c,
89 static const struct got_error *gotweb_get_repo_description(char **,
90 struct server *, const char *, int);
91 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
93 static const struct got_error *gotweb_render_blame(struct request *);
94 static const struct got_error *gotweb_render_diff(struct request *);
95 static const struct got_error *gotweb_render_summary(struct request *);
96 static const struct got_error *gotweb_render_tag(struct request *);
97 static const struct got_error *gotweb_render_tags(struct request *);
98 static const struct got_error *gotweb_render_tree(struct request *);
99 static const struct got_error *gotweb_render_branches(struct request *);
101 static void gotweb_free_querystring(struct querystring *);
102 static void gotweb_free_repo_dir(struct repo_dir *);
104 struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 gotweb_process_request(struct request *c)
109 const struct got_error *error = NULL, *error2 = NULL;
110 struct server *srv = NULL;
111 struct querystring *qs = NULL;
112 struct repo_dir *repo_dir = NULL;
113 uint8_t err[] = "gotwebd experienced an error: ";
116 /* init the transport */
117 error = gotweb_init_transport(&c->t);
119 log_warnx("%s: %s", __func__, error->msg);
122 /* don't process any further if client disconnected */
123 if (c->sock->client_status == CLIENT_DISCONNECT)
125 /* get the gotwebd server */
126 srv = gotweb_get_server(c->server_name, c->http_host);
128 log_warnx("%s: error server is NULL", __func__);
132 /* parse our querystring */
133 error = gotweb_init_querystring(&qs);
135 log_warnx("%s: %s", __func__, error->msg);
139 error = gotweb_parse_querystring(&qs, c->querystring);
141 log_warnx("%s: %s", __func__, error->msg);
146 * certain actions require a commit id in the querystring. this stops
147 * bad actors from exploiting this by manually manipulating the
151 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
152 qs->action == DIFF)) {
153 error2 = got_error(GOT_ERR_QUERYSTRING);
157 if (qs->action != INDEX) {
158 error = gotweb_init_repo_dir(&repo_dir, qs->path);
161 error = gotweb_load_got_path(c, repo_dir);
162 c->t->repo_dir = repo_dir;
163 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 /* render top of page */
168 if (qs != NULL && qs->action == BLOB) {
169 error = got_get_repo_commits(c, 1);
172 error = got_output_file_blob(c);
174 log_warnx("%s: %s", __func__, error->msg);
181 error = gotweb_render_content_type(c, "text/html");
183 log_warnx("%s: %s", __func__, error->msg);
188 if (gotweb_render_header(c->tp) == -1)
198 error = gotweb_render_blame(c);
200 log_warnx("%s: %s", __func__, error->msg);
205 if (gotweb_render_briefs(c->tp) == -1)
209 error = got_get_repo_commits(c, srv->max_commits_display);
211 log_warnx("%s: %s", __func__, error->msg);
214 if (gotweb_render_commits(c->tp) == -1)
218 error = gotweb_render_diff(c);
220 log_warnx("%s: %s", __func__, error->msg);
225 error = gotweb_render_index(c);
227 log_warnx("%s: %s", __func__, error->msg);
232 error = gotweb_render_summary(c);
234 log_warnx("%s: %s", __func__, error->msg);
239 error = gotweb_render_tag(c);
241 log_warnx("%s: %s", __func__, error->msg);
246 error = gotweb_render_tags(c);
248 log_warnx("%s: %s", __func__, error->msg);
253 error = gotweb_render_tree(c);
255 log_warnx("%s: %s", __func__, error->msg);
261 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
262 "Erorr: Bad Querystring");
270 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
272 if (fcgi_printf(c, "\n%s", err) == -1)
275 if (fcgi_printf(c, "%s", error->msg) == -1)
278 if (fcgi_printf(c, "see daemon logs for details") == -1)
281 if (html && fcgi_printf(c, "</div>\n") == -1)
284 if (html && srv != NULL)
285 gotweb_render_footer(c->tp);
289 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
291 struct server *srv = NULL;
293 /* check against the server name first */
294 if (strlen(server_name) > 0)
295 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
296 if (strcmp(srv->name, server_name) == 0)
299 /* check against subdomain second */
300 if (strlen(subdomain) > 0)
301 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
302 if (strcmp(srv->name, subdomain) == 0)
305 /* if those fail, send first server */
306 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
313 const struct got_error *
314 gotweb_init_transport(struct transport **t)
316 const struct got_error *error = NULL;
318 *t = calloc(1, sizeof(**t));
320 return got_error_from_errno2("%s: calloc", __func__);
322 TAILQ_INIT(&(*t)->repo_commits);
323 TAILQ_INIT(&(*t)->repo_tags);
326 (*t)->repo_dir = NULL;
328 (*t)->next_id = NULL;
329 (*t)->prev_id = NULL;
336 static const struct got_error *
337 gotweb_init_querystring(struct querystring **qs)
339 const struct got_error *error = NULL;
341 *qs = calloc(1, sizeof(**qs));
343 return got_error_from_errno2("%s: calloc", __func__);
345 (*qs)->headref = strdup("HEAD");
346 if ((*qs)->headref == NULL) {
349 return got_error_from_errno2("%s: strdup", __func__);
352 (*qs)->action = INDEX;
353 (*qs)->commit = NULL;
355 (*qs)->folder = NULL;
356 (*qs)->index_page = 0;
362 static const struct got_error *
363 gotweb_parse_querystring(struct querystring **qs, char *qst)
365 const struct got_error *error = NULL;
366 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
367 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
374 return got_error_from_errno2("%s: strdup", __func__);
379 while (tok1_pair != NULL) {
380 strsep(&tok1_end, "&");
382 tok2 = strdup(tok1_pair);
385 return got_error_from_errno2("%s: strdup", __func__);
391 while (tok2_pair != NULL) {
392 strsep(&tok2_end, "=");
394 error = gotweb_assign_querystring(qs, tok2_pair,
399 tok2_pair = tok2_end;
402 tok1_pair = tok1_end;
413 * Adapted from usr.sbin/httpd/httpd.c url_decode.
415 static const struct got_error *
416 gotweb_urldecode(char *url)
428 /* Encoding character is followed by two hex chars */
429 if (!isxdigit((unsigned char)p[1]) ||
430 !isxdigit((unsigned char)p[2]) ||
431 (p[1] == '0' && p[2] == '0'))
432 return got_error(GOT_ERR_BAD_QUERYSTRING);
438 * We don't have to validate "hex" because it is
439 * guaranteed to include two hex chars followed by nul.
441 x = strtoul(hex, NULL, 16);
457 static const struct got_error *
458 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
460 const struct got_error *error = NULL;
464 error = gotweb_urldecode(value);
468 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
469 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
472 switch (querystring_keys[el_cnt].element) {
474 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
475 if (strcmp(value, action_keys[a_cnt].name) != 0)
477 else if (strcmp(value,
478 action_keys[a_cnt].name) == 0){
480 action_keys[a_cnt].action;
488 (*qs)->commit = strdup(value);
489 if ((*qs)->commit == NULL) {
490 error = got_error_from_errno2("%s: strdup",
496 (*qs)->file = strdup(value);
497 if ((*qs)->file == NULL) {
498 error = got_error_from_errno2("%s: strdup",
504 (*qs)->folder = strdup(value);
505 if ((*qs)->folder == NULL) {
506 error = got_error_from_errno2("%s: strdup",
512 free((*qs)->headref);
513 (*qs)->headref = strdup(value);
514 if ((*qs)->headref == NULL) {
515 error = got_error_from_errno2("%s: strdup",
521 if (strlen(value) == 0)
523 (*qs)->index_page = strtonum(value, INT64_MIN,
526 error = got_error_from_errno3("%s: strtonum %s",
530 if ((*qs)->index_page < 0)
531 (*qs)->index_page = 0;
534 (*qs)->path = strdup(value);
535 if ((*qs)->path == NULL) {
536 error = got_error_from_errno2("%s: strdup",
542 if (strlen(value) == 0)
544 (*qs)->page = strtonum(value, INT64_MIN,
547 error = got_error_from_errno3("%s: strtonum %s",
563 gotweb_free_repo_tag(struct repo_tag *rt)
568 free(rt->tag_commit);
569 free(rt->commit_msg);
576 gotweb_free_repo_commit(struct repo_commit *rc)
586 free(rc->commit_msg);
592 gotweb_free_querystring(struct querystring *qs)
605 gotweb_free_repo_dir(struct repo_dir *repo_dir)
607 if (repo_dir != NULL) {
608 free(repo_dir->name);
609 free(repo_dir->owner);
610 free(repo_dir->description);
613 free(repo_dir->path);
619 gotweb_free_transport(struct transport *t)
621 struct repo_commit *rc = NULL, *trc = NULL;
622 struct repo_tag *rt = NULL, *trt = NULL;
624 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
625 TAILQ_REMOVE(&t->repo_commits, rc, entry);
626 gotweb_free_repo_commit(rc);
628 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
629 TAILQ_REMOVE(&t->repo_tags, rt, entry);
630 gotweb_free_repo_tag(rt);
632 gotweb_free_repo_dir(t->repo_dir);
633 gotweb_free_querystring(t->qs);
639 const struct got_error *
640 gotweb_render_content_type(struct request *c, const uint8_t *type)
642 const char *csp = "default-src 'self'; script-src 'none'; "
643 "object-src 'none';";
646 "Content-Security-Policy: %s\r\n"
647 "Content-Type: %s\r\n\r\n",
652 const struct got_error *
653 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
656 fcgi_printf(c, "Content-type: %s\r\n"
657 "Content-disposition: attachment; filename=%s\r\n\r\n",
663 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
664 struct gotweb_url *next, int *have_next)
666 struct transport *t = c->t;
667 struct querystring *qs = t->qs;
668 struct server *srv = c->srv;
670 *have_prev = *have_next = 0;
674 if (qs->index_page > 0) {
676 *prev = (struct gotweb_url){
678 .index_page = qs->index_page - 1,
682 if (t->next_disp == srv->max_repos_display &&
683 t->repos_total != (qs->index_page + 1) *
684 srv->max_repos_display) {
686 *next = (struct gotweb_url){
688 .index_page = qs->index_page + 1,
694 if (t->prev_id && qs->commit != NULL &&
695 strcmp(qs->commit, t->prev_id) != 0) {
697 *prev = (struct gotweb_url){
700 .page = qs->page - 1,
702 .commit = t->prev_id,
703 .headref = qs->headref,
708 *next = (struct gotweb_url){
711 .page = qs->page + 1,
713 .commit = t->next_id,
714 .headref = qs->headref,
719 if (t->prev_id && qs->commit != NULL &&
720 strcmp(qs->commit, t->prev_id) != 0) {
722 *prev = (struct gotweb_url){
725 .page = qs->page - 1,
727 .commit = t->prev_id,
728 .headref = qs->headref,
729 .folder = qs->folder,
735 *next = (struct gotweb_url){
738 .page = qs->page + 1,
740 .commit = t->next_id,
741 .headref = qs->headref,
742 .folder = qs->folder,
748 if (t->prev_id && qs->commit != NULL &&
749 strcmp(qs->commit, t->prev_id) != 0) {
751 *prev = (struct gotweb_url){
754 .page = qs->page - 1,
756 .commit = t->prev_id,
757 .headref = qs->headref,
762 *next = (struct gotweb_url){
765 .page = qs->page + 1,
767 .commit = t->next_id,
768 .headref = qs->headref,
775 static const struct got_error *
776 gotweb_render_index(struct request *c)
778 const struct got_error *error = NULL;
779 struct server *srv = c->srv;
780 struct transport *t = c->t;
781 struct querystring *qs = t->qs;
782 struct repo_dir *repo_dir = NULL;
784 struct dirent **sd_dent = NULL;
785 unsigned int d_cnt, d_i, d_disp = 0;
786 unsigned int d_skipped = 0;
789 d = opendir(srv->repos_path);
791 error = got_error_from_errno2("opendir", srv->repos_path);
795 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
798 error = got_error_from_errno2("scandir", srv->repos_path);
802 if (gotweb_render_repo_table_hdr(c->tp) == -1)
805 for (d_i = 0; d_i < d_cnt; d_i++) {
806 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
809 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
810 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
815 error = got_path_dirent_type(&type, srv->repos_path,
819 if (type != DT_DIR) {
824 if (qs->index_page > 0 && (qs->index_page *
825 srv->max_repos_display) > t->prev_disp) {
830 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
834 error = gotweb_load_got_path(c, repo_dir);
835 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
837 gotweb_free_repo_dir(repo_dir);
842 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
848 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
851 gotweb_free_repo_dir(repo_dir);
854 if (d_disp == srv->max_repos_display)
857 t->repos_total = d_cnt - d_skipped;
859 if (srv->max_repos_display == 0)
861 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
863 if (t->repos_total <= srv->max_repos ||
864 t->repos_total <= srv->max_repos_display)
867 if (gotweb_render_navs(c->tp) == -1)
871 for (d_i = 0; d_i < d_cnt; d_i++)
875 if (d != NULL && closedir(d) == EOF && error == NULL)
876 error = got_error_from_errno("closedir");
880 static const struct got_error *
881 gotweb_render_blame(struct request *c)
883 const struct got_error *error = NULL;
884 struct transport *t = c->t;
885 struct repo_commit *rc = NULL;
886 char *age = NULL, *msg = NULL;
889 error = got_get_repo_commits(c, 1);
893 rc = TAILQ_FIRST(&t->repo_commits);
895 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
898 error = gotweb_escape_html(&msg, rc->commit_msg);
902 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
903 "<div id='blame_title'>Blame</div>\n"
904 "</div>\n" /* #blame_title_wrapper */
905 "<div id='blame_content'>\n"
906 "<div id='blame_header_wrapper'>\n"
907 "<div id='blame_header'>\n"
908 "<div class='header_age_title'>Date:</div>\n"
909 "<div class='header_age'>%s</div>\n"
910 "<div id='header_commit_msg_title'>Message:</div>\n"
911 "<div id='header_commit_msg'>%s</div>\n"
912 "</div>\n" /* #blame_header */
913 "</div>\n" /* #blame_header_wrapper */
914 "<div class='dotted_line'></div>\n"
915 "<div id='blame'>\n",
921 error = got_output_file_blame(c);
925 fcgi_printf(c, "</div>\n" /* #blame */
926 "</div>\n"); /* #blame_content */
933 static const struct got_error *
934 gotweb_render_branches(struct request *c)
936 const struct got_error *error = NULL;
937 struct got_reflist_head refs;
938 struct got_reflist_entry *re;
939 struct transport *t = c->t;
940 struct querystring *qs = t->qs;
941 struct got_repository *repo = t->repo;
942 char *escaped_refname = NULL;
948 error = got_ref_list(&refs, repo, "refs/heads",
949 got_ref_cmp_by_name, NULL);
953 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
954 "<div id='branches_title'>Branches</div>\n"
955 "</div>\n" /* #branches_title_wrapper */
956 "<div id='branches_content'>\n");
960 TAILQ_FOREACH(re, &refs, entry) {
961 const char *refname = NULL;
963 if (got_ref_is_symbolic(re->ref))
966 refname = got_ref_get_name(re->ref);
967 if (refname == NULL) {
968 error = got_error_from_errno("strdup");
971 if (strncmp(refname, "refs/heads/", 11) != 0)
974 error = got_get_repo_age(&age, c, refname, TM_DIFF);
978 if (strncmp(refname, "refs/heads/", 11) == 0)
980 error = gotweb_escape_html(&escaped_refname, refname);
984 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
985 "<div class='branches_age'>%s</div>\n"
986 "<div class='branches_space'> </div>\n"
987 "<div class='branch'>", age);
991 r = gotweb_link(c, &(struct gotweb_url){
997 }, "%s", escaped_refname);
1001 if (fcgi_printf(c, "</div>\n" /* .branch */
1002 "<div class='navs_wrapper'>\n"
1003 "<div class='navs'>") == -1)
1006 r = gotweb_link(c, &(struct gotweb_url){
1016 if (fcgi_printf(c, " | ") == -1)
1019 r = gotweb_link(c, &(struct gotweb_url){
1025 }, "commit briefs");
1029 if (fcgi_printf(c, " | ") == -1)
1032 r = gotweb_link(c, &(struct gotweb_url){
1042 r = fcgi_printf(c, "</div>\n" /* .navs */
1043 "</div>\n" /* .navs_wrapper */
1044 "<div class='dotted_line'></div>\n"
1045 "</div>\n"); /* .branches_wrapper */
1051 free(escaped_refname);
1052 escaped_refname = NULL;
1054 fcgi_printf(c, "</div>\n"); /* #branches_content */
1057 free(escaped_refname);
1058 got_ref_list_free(&refs);
1062 static const struct got_error *
1063 gotweb_render_tree(struct request *c)
1065 const struct got_error *error = NULL;
1066 struct transport *t = c->t;
1067 struct repo_commit *rc = NULL;
1068 char *age = NULL, *msg = NULL;
1071 error = got_get_repo_commits(c, 1);
1075 rc = TAILQ_FIRST(&t->repo_commits);
1077 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1081 error = gotweb_escape_html(&msg, rc->commit_msg);
1085 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1086 "<div id='tree_title'>Tree</div>\n"
1087 "</div>\n" /* #tree_title_wrapper */
1088 "<div id='tree_content'>\n"
1089 "<div id='tree_header_wrapper'>\n"
1090 "<div id='tree_header'>\n"
1091 "<div id='header_tree_title'>Tree:</div>\n"
1092 "<div id='header_tree'>%s</div>\n"
1093 "<div class='header_age_title'>Date:</div>\n"
1094 "<div class='header_age'>%s</div>\n"
1095 "<div id='header_commit_msg_title'>Message:</div>\n"
1096 "<div id='header_commit_msg'>%s</div>\n"
1097 "</div>\n" /* #tree_header */
1098 "</div>\n" /* #tree_header_wrapper */
1099 "<div class='dotted_line'></div>\n"
1100 "<div id='tree'>\n",
1107 error = got_output_repo_tree(c);
1111 fcgi_printf(c, "</div>\n"); /* #tree */
1112 fcgi_printf(c, "</div>\n"); /* #tree_content */
1119 static const struct got_error *
1120 gotweb_render_diff(struct request *c)
1122 const struct got_error *error = NULL;
1123 struct transport *t = c->t;
1124 struct repo_commit *rc = NULL;
1125 char *age = NULL, *author = NULL, *msg = NULL;
1128 error = got_get_repo_commits(c, 1);
1132 rc = TAILQ_FIRST(&t->repo_commits);
1134 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1137 error = gotweb_escape_html(&author, rc->author);
1140 error = gotweb_escape_html(&msg, rc->commit_msg);
1144 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1145 "<div id='diff_title'>Commit Diff</div>\n"
1146 "</div>\n" /* #diff_title_wrapper */
1147 "<div id='diff_content'>\n"
1148 "<div id='diff_header_wrapper'>\n"
1149 "<div id='diff_header'>\n"
1150 "<div id='header_diff_title'>Diff:</div>\n"
1151 "<div id='header_diff'>%s<br />%s</div>\n"
1152 "<div class='header_commit_title'>Commit:</div>\n"
1153 "<div class='header_commit'>%s</div>\n"
1154 "<div id='header_tree_title'>Tree:</div>\n"
1155 "<div id='header_tree'>%s</div>\n"
1156 "<div class='header_author_title'>Author:</div>\n"
1157 "<div class='header_author'>%s</div>\n"
1158 "<div class='header_age_title'>Date:</div>\n"
1159 "<div class='header_age'>%s</div>\n"
1160 "<div id='header_commit_msg_title'>Message:</div>\n"
1161 "<div id='header_commit_msg'>%s</div>\n"
1162 "</div>\n" /* #diff_header */
1163 "</div>\n" /* #diff_header_wrapper */
1164 "<div class='dotted_line'></div>\n"
1165 "<div id='diff'>\n",
1166 rc->parent_id, rc->commit_id,
1175 error = got_output_repo_diff(c);
1179 fcgi_printf(c, "</div>\n"); /* #diff */
1180 fcgi_printf(c, "</div>\n"); /* #diff_content */
1188 static const struct got_error *
1189 gotweb_render_summary(struct request *c)
1191 const struct got_error *error = NULL;
1192 struct transport *t = c->t;
1193 struct server *srv = c->srv;
1196 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1199 if (srv->show_repo_description) {
1201 "<div id='description_title'>Description:</div>\n"
1202 "<div id='description'>%s</div>\n",
1203 t->repo_dir->description ? t->repo_dir->description : "");
1208 if (srv->show_repo_owner) {
1210 "<div id='repo_owner_title'>Owner:</div>\n"
1211 "<div id='repo_owner'>%s</div>\n",
1212 t->repo_dir->owner ? t->repo_dir->owner : "");
1217 if (srv->show_repo_age) {
1219 "<div id='last_change_title'>Last Change:</div>\n"
1220 "<div id='last_change'>%s</div>\n",
1226 if (srv->show_repo_cloneurl) {
1228 "<div id='cloneurl_title'>Clone URL:</div>\n"
1229 "<div id='cloneurl'>%s</div>\n",
1230 t->repo_dir->url ? t->repo_dir->url : "");
1235 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1239 if (gotweb_render_briefs(c->tp) == -1)
1242 error = gotweb_render_tags(c);
1244 log_warnx("%s: %s", __func__, error->msg);
1248 error = gotweb_render_branches(c);
1250 log_warnx("%s: %s", __func__, error->msg);
1255 static const struct got_error *
1256 gotweb_render_tag(struct request *c)
1258 const struct got_error *error = NULL;
1259 struct repo_tag *rt = NULL;
1260 struct transport *t = c->t;
1261 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1263 error = got_get_repo_tags(c, 1);
1267 if (t->tag_count == 0) {
1268 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1273 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1275 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1278 error = gotweb_escape_html(&author, rt->tagger);
1281 error = gotweb_escape_html(&msg, rt->commit_msg);
1285 tagname = rt->tag_name;
1286 if (strncmp(tagname, "refs/", 5) == 0)
1288 error = gotweb_escape_html(&tagname, tagname);
1292 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1293 "<div id='tags_title'>Tag</div>\n"
1294 "</div>\n" /* #tags_title_wrapper */
1295 "<div id='tags_content'>\n"
1296 "<div id='tag_header_wrapper'>\n"
1297 "<div id='tag_header'>\n"
1298 "<div class='header_commit_title'>Commit:</div>\n"
1299 "<div class='header_commit'>%s"
1300 " <span class='refs_str'>(%s)</span></div>\n"
1301 "<div class='header_author_title'>Tagger:</div>\n"
1302 "<div class='header_author'>%s</div>\n"
1303 "<div class='header_age_title'>Date:</div>\n"
1304 "<div class='header_age'>%s</div>\n"
1305 "<div id='header_commit_msg_title'>Message:</div>\n"
1306 "<div id='header_commit_msg'>%s</div>\n"
1307 "</div>\n" /* #tag_header */
1308 "<div class='dotted_line'></div>\n"
1309 "<div id='tag_commit'>\n%s</div>"
1310 "</div>" /* #tag_header_wrapper */
1311 "</div>", /* #tags_content */
1326 static const struct got_error *
1327 gotweb_render_tags(struct request *c)
1329 const struct got_error *error = NULL;
1330 struct repo_tag *rt = NULL;
1331 struct server *srv = c->srv;
1332 struct transport *t = c->t;
1333 struct querystring *qs = t->qs;
1334 struct repo_dir *repo_dir = t->repo_dir;
1335 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1336 int r, commit_found = 0;
1338 if (qs->action == BRIEFS) {
1340 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1342 error = got_get_repo_tags(c, srv->max_commits_display);
1346 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1347 "<div id='tags_title'>Tags</div>\n"
1348 "</div>\n" /* #tags_title_wrapper */
1349 "<div id='tags_content'>\n");
1353 if (t->tag_count == 0) {
1354 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1355 "This repository contains no tags");
1360 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1361 if (commit_found == 0 && qs->commit != NULL) {
1362 if (strcmp(qs->commit, rt->commit_id) != 0)
1367 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1371 tagname = rt->tag_name;
1372 if (strncmp(tagname, "refs/tags/", 10) == 0)
1374 error = gotweb_escape_html(&tagname, tagname);
1378 if (rt->tag_commit != NULL) {
1379 newline = strchr(rt->tag_commit, '\n');
1382 error = gotweb_escape_html(&msg, rt->tag_commit);
1387 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1388 "<div class='tag'>%s</div>\n"
1389 "<div class='tag_log'>", age, tagname) == -1)
1392 r = gotweb_link(c, &(struct gotweb_url){
1396 .path = repo_dir->name,
1397 .commit = rt->commit_id,
1398 }, "%s", msg ? msg : "");
1402 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1403 "<div class='navs_wrapper'>\n"
1404 "<div class='navs'>") == -1)
1407 r = gotweb_link(c, &(struct gotweb_url){
1411 .path = repo_dir->name,
1412 .commit = rt->commit_id,
1417 if (fcgi_printf(c, " | ") == -1)
1420 r = gotweb_link(c, &(struct gotweb_url){
1424 .path = repo_dir->name,
1425 .commit = rt->commit_id,
1426 }, "commit briefs");
1430 if (fcgi_printf(c, " | ") == -1)
1433 r = gotweb_link(c, &(struct gotweb_url){
1437 .path = repo_dir->name,
1438 .commit = rt->commit_id,
1444 "</div>\n" /* .navs */
1445 "</div>\n" /* .navs_wrapper */
1446 "<div class='dotted_line'></div>\n");
1457 if (t->next_id || t->prev_id) {
1458 if (gotweb_render_navs(c->tp) == -1)
1461 fcgi_printf(c, "</div>\n"); /* #tags_content */
1469 const struct got_error *
1470 gotweb_escape_html(char **escaped_html, const char *orig_html)
1472 const struct got_error *error = NULL;
1473 struct escape_pair {
1484 size_t orig_len, len;
1487 orig_len = strlen(orig_html);
1489 for (i = 0; i < orig_len; i++) {
1490 for (j = 0; j < nitems(esc); j++) {
1491 if (orig_html[i] != esc[j].c)
1493 len += strlen(esc[j].s) - 1 /* escaped char */;
1497 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1498 if (*escaped_html == NULL)
1499 return got_error_from_errno("calloc");
1502 for (i = 0; i < orig_len; i++) {
1504 for (j = 0; j < nitems(esc); j++) {
1505 if (orig_html[i] != esc[j].c)
1508 if (strlcat(*escaped_html, esc[j].s, len + 1)
1510 error = got_error(GOT_ERR_NO_SPACE);
1513 x += strlen(esc[j].s);
1518 (*escaped_html)[x] = orig_html[i];
1524 free(*escaped_html);
1525 *escaped_html = NULL;
1527 (*escaped_html)[x] = '\0';
1534 should_urlencode(int c)
1536 if (c <= ' ' || c >= 127)
1567 gotweb_urlencode(const char *str)
1575 for (s = str; *s; ++s) {
1577 if (should_urlencode(*s))
1581 escaped = calloc(1, len + 1);
1582 if (escaped == NULL)
1586 for (s = str; *s; ++s) {
1587 if (should_urlencode(*s)) {
1588 a = (*s & 0xF0) >> 4;
1592 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1593 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1602 gotweb_action_name(int action)
1633 gotweb_render_url(struct request *c, struct gotweb_url *url)
1635 const char *sep = "?", *action;
1639 action = gotweb_action_name(url->action);
1640 if (action != NULL) {
1641 if (fcgi_printf(c, "?action=%s", action) == -1)
1647 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1653 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1659 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1665 tmp = gotweb_urlencode(url->file);
1668 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1676 tmp = gotweb_urlencode(url->folder);
1679 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1687 tmp = gotweb_urlencode(url->headref);
1690 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1697 if (url->index_page != -1) {
1698 if (fcgi_printf(c, "%sindex_page=%d", sep,
1699 url->index_page) == -1)
1705 tmp = gotweb_urlencode(url->path);
1708 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1715 if (url->page != -1) {
1716 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1725 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1730 if (fcgi_printf(c, "<a href='") == -1)
1733 if (gotweb_render_url(c, url) == -1)
1736 if (fcgi_printf(c, "'>") == -1)
1740 r = fcgi_vprintf(c, fmt, ap);
1745 if (fcgi_printf(c, "</a>"))
1750 static struct got_repository *
1751 find_cached_repo(struct server *srv, const char *path)
1755 for (i = 0; i < srv->ncached_repos; i++) {
1756 if (strcmp(srv->cached_repos[i].path, path) == 0)
1757 return srv->cached_repos[i].repo;
1763 static const struct got_error *
1764 cache_repo(struct got_repository **new, struct server *srv,
1765 struct repo_dir *repo_dir, struct socket *sock)
1767 const struct got_error *error = NULL;
1768 struct got_repository *repo;
1769 struct cached_repo *cr;
1772 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1773 cr = &srv->cached_repos[srv->ncached_repos - 1];
1774 error = got_repo_close(cr->repo);
1775 memset(cr, 0, sizeof(*cr));
1776 srv->ncached_repos--;
1779 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1780 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1781 cr = &srv->cached_repos[0];
1784 cr = &srv->cached_repos[srv->ncached_repos];
1787 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1790 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1791 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1796 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1797 >= sizeof(cr->path)) {
1799 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1800 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1802 return got_error(GOT_ERR_NO_SPACE);
1806 srv->ncached_repos++;
1811 static const struct got_error *
1812 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1814 const struct got_error *error = NULL;
1815 struct socket *sock = c->sock;
1816 struct server *srv = c->srv;
1817 struct transport *t = c->t;
1818 struct got_repository *repo = NULL;
1822 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1823 GOTWEB_GIT_DIR) == -1)
1824 return got_error_from_errno("asprintf");
1826 dt = opendir(dir_test);
1830 repo_dir->path = dir_test;
1835 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1836 repo_dir->name) == -1)
1837 return got_error_from_errno("asprintf");
1839 dt = opendir(dir_test);
1841 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1844 repo_dir->path = dir_test;
1849 if (srv->respect_exportok &&
1850 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1851 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1855 repo = find_cached_repo(srv, repo_dir->path);
1857 error = cache_repo(&repo, srv, repo_dir, sock);
1862 error = gotweb_get_repo_description(&repo_dir->description, srv,
1863 repo_dir->path, dirfd(dt));
1866 error = got_get_repo_owner(&repo_dir->owner, c);
1869 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1872 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1876 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1877 error = got_error_from_errno("closedir");
1881 static const struct got_error *
1882 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1884 const struct got_error *error;
1886 *repo_dir = calloc(1, sizeof(**repo_dir));
1887 if (*repo_dir == NULL)
1888 return got_error_from_errno("calloc");
1890 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1891 error = got_error_from_errno("asprintf");
1896 (*repo_dir)->owner = NULL;
1897 (*repo_dir)->description = NULL;
1898 (*repo_dir)->url = NULL;
1899 (*repo_dir)->age = NULL;
1900 (*repo_dir)->path = NULL;
1905 static const struct got_error *
1906 gotweb_get_repo_description(char **description, struct server *srv,
1907 const char *dirpath, int dir)
1909 const struct got_error *error = NULL;
1914 *description = NULL;
1915 if (srv->show_repo_description == 0)
1918 fd = openat(dir, "description", O_RDONLY);
1920 if (errno != ENOENT && errno != EACCES) {
1921 error = got_error_from_errno_fmt("openat %s/%s",
1922 dirpath, "description");
1927 if (fstat(fd, &sb) == -1) {
1928 error = got_error_from_errno_fmt("fstat %s/%s",
1929 dirpath, "description");
1934 if (len > GOTWEBD_MAXDESCRSZ - 1)
1935 len = GOTWEBD_MAXDESCRSZ - 1;
1937 *description = calloc(len + 1, sizeof(**description));
1938 if (*description == NULL) {
1939 error = got_error_from_errno("calloc");
1943 if (read(fd, *description, len) == -1)
1944 error = got_error_from_errno("read");
1946 if (fd != -1 && close(fd) == -1 && error == NULL)
1947 error = got_error_from_errno("close");
1951 static const struct got_error *
1952 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1955 const struct got_error *error = NULL;
1961 if (srv->show_repo_cloneurl == 0)
1964 fd = openat(dir, "cloneurl", O_RDONLY);
1966 if (errno != ENOENT && errno != EACCES) {
1967 error = got_error_from_errno_fmt("openat %s/%s",
1968 dirpath, "cloneurl");
1973 if (fstat(fd, &sb) == -1) {
1974 error = got_error_from_errno_fmt("fstat %s/%s",
1975 dirpath, "cloneurl");
1980 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1981 len = GOTWEBD_MAXCLONEURLSZ - 1;
1983 *url = calloc(len + 1, sizeof(**url));
1985 error = got_error_from_errno("calloc");
1989 if (read(fd, *url, len) == -1)
1990 error = got_error_from_errno("read");
1992 if (fd != -1 && close(fd) == -1 && error == NULL)
1993 error = got_error_from_errno("close");
1997 const struct got_error *
1998 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2001 long long diff_time;
2002 const char *years = "years ago", *months = "months ago";
2003 const char *weeks = "weeks ago", *days = "days ago";
2004 const char *hours = "hours ago", *minutes = "minutes ago";
2005 const char *seconds = "seconds ago", *now = "right now";
2013 diff_time = time(NULL) - committer_time;
2014 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2015 if (asprintf(repo_age, "%lld %s",
2016 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2017 return got_error_from_errno("asprintf");
2018 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2019 if (asprintf(repo_age, "%lld %s",
2020 (diff_time / 60 / 60 / 24 / (365 / 12)),
2022 return got_error_from_errno("asprintf");
2023 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2024 if (asprintf(repo_age, "%lld %s",
2025 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2026 return got_error_from_errno("asprintf");
2027 } else if (diff_time > 60 * 60 * 24 * 2) {
2028 if (asprintf(repo_age, "%lld %s",
2029 (diff_time / 60 / 60 / 24), days) == -1)
2030 return got_error_from_errno("asprintf");
2031 } else if (diff_time > 60 * 60 * 2) {
2032 if (asprintf(repo_age, "%lld %s",
2033 (diff_time / 60 / 60), hours) == -1)
2034 return got_error_from_errno("asprintf");
2035 } else if (diff_time > 60 * 2) {
2036 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2038 return got_error_from_errno("asprintf");
2039 } else if (diff_time > 2) {
2040 if (asprintf(repo_age, "%lld %s", diff_time,
2042 return got_error_from_errno("asprintf");
2044 if (asprintf(repo_age, "%s", now) == -1)
2045 return got_error_from_errno("asprintf");
2049 if (gmtime_r(&committer_time, &tm) == NULL)
2050 return got_error_from_errno("gmtime_r");
2052 s = asctime_r(&tm, datebuf);
2054 return got_error_from_errno("asctime_r");
2056 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2057 return got_error_from_errno("asprintf");