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_tree(struct request *);
103 static const struct got_error *gotweb_render_branches(struct request *);
105 static void gotweb_free_querystring(struct querystring *);
106 static void gotweb_free_repo_dir(struct repo_dir *);
108 struct server *gotweb_get_server(uint8_t *, uint8_t *);
111 gotweb_process_request(struct request *c)
113 const struct got_error *error = NULL, *error2 = NULL;
114 struct got_blob_object *blob = NULL;
115 struct server *srv = NULL;
116 struct querystring *qs = NULL;
117 struct repo_dir *repo_dir = NULL;
118 uint8_t err[] = "gotwebd experienced an error: ";
119 int r, html = 0, fd = -1;
121 /* init the transport */
122 error = gotweb_init_transport(&c->t);
124 log_warnx("%s: %s", __func__, error->msg);
127 /* don't process any further if client disconnected */
128 if (c->sock->client_status == CLIENT_DISCONNECT)
130 /* get the gotwebd server */
131 srv = gotweb_get_server(c->server_name, c->http_host);
133 log_warnx("%s: error server is NULL", __func__);
137 /* parse our querystring */
138 error = gotweb_init_querystring(&qs);
140 log_warnx("%s: %s", __func__, error->msg);
144 error = gotweb_parse_querystring(&qs, c->querystring);
146 log_warnx("%s: %s", __func__, error->msg);
151 * certain actions require a commit id in the querystring. this stops
152 * bad actors from exploiting this by manually manipulating the
156 if (qs->action == BLAME || qs->action == BLOB ||
157 qs->action == BLOBRAW || qs->action == DIFF) {
158 if (qs->commit == NULL) {
159 error2 = got_error(GOT_ERR_QUERYSTRING);
164 if (qs->action != INDEX) {
165 error = gotweb_init_repo_dir(&repo_dir, qs->path);
168 error = gotweb_load_got_path(c, repo_dir);
169 c->t->repo_dir = repo_dir;
170 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
174 if (qs->action == BLOBRAW) {
175 error = got_get_repo_commits(c, 1);
178 error = got_output_file_blob(c);
180 log_warnx("%s: %s", __func__, error->msg);
186 if (qs->action == BLOB) {
188 struct gotweb_url url = {
193 .commit = qs->commit,
194 .folder = qs->folder,
198 error = got_get_repo_commits(c, 1);
202 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
206 fcgi_puts(c->tp, "Status: 302\r\n");
207 fcgi_puts(c->tp, "Location: ");
208 gotweb_render_url(c, &url);
209 fcgi_puts(c->tp, "\r\n\r\n");
214 if (qs->action == RSS) {
215 error = gotweb_render_content_type_file(c,
216 "application/rss+xml;charset=utf-8",
217 repo_dir->name, ".rss");
219 log_warnx("%s: %s", __func__, error->msg);
223 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
225 log_warnx("%s: %s", __func__, error->msg);
228 if (gotweb_render_rss(c->tp) == -1)
234 error = gotweb_render_content_type(c, "text/html");
236 log_warnx("%s: %s", __func__, error->msg);
241 if (gotweb_render_header(c->tp) == -1)
251 error = gotweb_render_blame(c);
253 log_warnx("%s: %s", __func__, error->msg);
258 if (gotweb_render_blob(c->tp, blob) == -1)
262 if (gotweb_render_briefs(c->tp) == -1)
266 error = got_get_repo_commits(c, srv->max_commits_display);
268 log_warnx("%s: %s", __func__, error->msg);
271 if (gotweb_render_commits(c->tp) == -1)
275 error = gotweb_render_diff(c);
277 log_warnx("%s: %s", __func__, error->msg);
282 error = gotweb_render_index(c);
284 log_warnx("%s: %s", __func__, error->msg);
289 error = gotweb_render_summary(c);
291 log_warnx("%s: %s", __func__, error->msg);
296 error = gotweb_render_tag(c);
298 log_warnx("%s: %s", __func__, error->msg);
303 error = gotweb_render_tags(c);
305 log_warnx("%s: %s", __func__, error->msg);
310 error = gotweb_render_tree(c);
312 log_warnx("%s: %s", __func__, error->msg);
318 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
319 "Erorr: Bad Querystring");
327 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
329 if (fcgi_printf(c, "\n%s", err) == -1)
332 if (fcgi_printf(c, "%s", error->msg) == -1)
335 if (fcgi_printf(c, "see daemon logs for details") == -1)
338 if (html && fcgi_printf(c, "</div>\n") == -1)
342 got_object_blob_close(blob);
345 if (html && srv != NULL)
346 gotweb_render_footer(c->tp);
350 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
352 struct server *srv = NULL;
354 /* check against the server name first */
355 if (strlen(server_name) > 0)
356 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
357 if (strcmp(srv->name, server_name) == 0)
360 /* check against subdomain second */
361 if (strlen(subdomain) > 0)
362 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
363 if (strcmp(srv->name, subdomain) == 0)
366 /* if those fail, send first server */
367 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
374 const struct got_error *
375 gotweb_init_transport(struct transport **t)
377 const struct got_error *error = NULL;
379 *t = calloc(1, sizeof(**t));
381 return got_error_from_errno2("%s: calloc", __func__);
383 TAILQ_INIT(&(*t)->repo_commits);
384 TAILQ_INIT(&(*t)->repo_tags);
387 (*t)->repo_dir = NULL;
389 (*t)->next_id = NULL;
390 (*t)->prev_id = NULL;
397 static const struct got_error *
398 gotweb_init_querystring(struct querystring **qs)
400 const struct got_error *error = NULL;
402 *qs = calloc(1, sizeof(**qs));
404 return got_error_from_errno2("%s: calloc", __func__);
406 (*qs)->headref = strdup("HEAD");
407 if ((*qs)->headref == NULL) {
410 return got_error_from_errno2("%s: strdup", __func__);
413 (*qs)->action = INDEX;
414 (*qs)->commit = NULL;
416 (*qs)->folder = NULL;
417 (*qs)->index_page = 0;
423 static const struct got_error *
424 gotweb_parse_querystring(struct querystring **qs, char *qst)
426 const struct got_error *error = NULL;
427 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
428 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
435 return got_error_from_errno2("%s: strdup", __func__);
440 while (tok1_pair != NULL) {
441 strsep(&tok1_end, "&");
443 tok2 = strdup(tok1_pair);
446 return got_error_from_errno2("%s: strdup", __func__);
452 while (tok2_pair != NULL) {
453 strsep(&tok2_end, "=");
455 error = gotweb_assign_querystring(qs, tok2_pair,
460 tok2_pair = tok2_end;
463 tok1_pair = tok1_end;
474 * Adapted from usr.sbin/httpd/httpd.c url_decode.
476 static const struct got_error *
477 gotweb_urldecode(char *url)
489 /* Encoding character is followed by two hex chars */
490 if (!isxdigit((unsigned char)p[1]) ||
491 !isxdigit((unsigned char)p[2]) ||
492 (p[1] == '0' && p[2] == '0'))
493 return got_error(GOT_ERR_BAD_QUERYSTRING);
499 * We don't have to validate "hex" because it is
500 * guaranteed to include two hex chars followed by nul.
502 x = strtoul(hex, NULL, 16);
518 static const struct got_error *
519 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
521 const struct got_error *error = NULL;
525 error = gotweb_urldecode(value);
529 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
530 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
533 switch (querystring_keys[el_cnt].element) {
535 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
536 if (strcmp(value, action_keys[a_cnt].name) != 0)
538 else if (strcmp(value,
539 action_keys[a_cnt].name) == 0){
541 action_keys[a_cnt].action;
549 (*qs)->commit = strdup(value);
550 if ((*qs)->commit == NULL) {
551 error = got_error_from_errno2("%s: strdup",
557 (*qs)->file = strdup(value);
558 if ((*qs)->file == NULL) {
559 error = got_error_from_errno2("%s: strdup",
565 (*qs)->folder = strdup(value);
566 if ((*qs)->folder == NULL) {
567 error = got_error_from_errno2("%s: strdup",
573 free((*qs)->headref);
574 (*qs)->headref = strdup(value);
575 if ((*qs)->headref == NULL) {
576 error = got_error_from_errno2("%s: strdup",
582 if (strlen(value) == 0)
584 (*qs)->index_page = strtonum(value, INT64_MIN,
587 error = got_error_from_errno3("%s: strtonum %s",
591 if ((*qs)->index_page < 0)
592 (*qs)->index_page = 0;
595 (*qs)->path = strdup(value);
596 if ((*qs)->path == NULL) {
597 error = got_error_from_errno2("%s: strdup",
603 if (strlen(value) == 0)
605 (*qs)->page = strtonum(value, INT64_MIN,
608 error = got_error_from_errno3("%s: strtonum %s",
624 gotweb_free_repo_tag(struct repo_tag *rt)
629 free(rt->tag_commit);
630 free(rt->commit_msg);
637 gotweb_free_repo_commit(struct repo_commit *rc)
647 free(rc->commit_msg);
653 gotweb_free_querystring(struct querystring *qs)
666 gotweb_free_repo_dir(struct repo_dir *repo_dir)
668 if (repo_dir != NULL) {
669 free(repo_dir->name);
670 free(repo_dir->owner);
671 free(repo_dir->description);
674 free(repo_dir->path);
680 gotweb_free_transport(struct transport *t)
682 struct repo_commit *rc = NULL, *trc = NULL;
683 struct repo_tag *rt = NULL, *trt = NULL;
685 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
686 TAILQ_REMOVE(&t->repo_commits, rc, entry);
687 gotweb_free_repo_commit(rc);
689 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
690 TAILQ_REMOVE(&t->repo_tags, rt, entry);
691 gotweb_free_repo_tag(rt);
693 gotweb_free_repo_dir(t->repo_dir);
694 gotweb_free_querystring(t->qs);
700 const struct got_error *
701 gotweb_render_content_type(struct request *c, const char *type)
703 const char *csp = "default-src 'self'; script-src 'none'; "
704 "object-src 'none';";
707 "Content-Security-Policy: %s\r\n"
708 "Content-Type: %s\r\n\r\n",
713 const struct got_error *
714 gotweb_render_content_type_file(struct request *c, const char *type,
715 const char *file, const char *suffix)
717 fcgi_printf(c, "Content-type: %s\r\n"
718 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
719 type, file, suffix ? suffix : "");
724 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
725 struct gotweb_url *next, int *have_next)
727 struct transport *t = c->t;
728 struct querystring *qs = t->qs;
729 struct server *srv = c->srv;
731 *have_prev = *have_next = 0;
735 if (qs->index_page > 0) {
737 *prev = (struct gotweb_url){
739 .index_page = qs->index_page - 1,
743 if (t->next_disp == srv->max_repos_display &&
744 t->repos_total != (qs->index_page + 1) *
745 srv->max_repos_display) {
747 *next = (struct gotweb_url){
749 .index_page = qs->index_page + 1,
755 if (t->prev_id && qs->commit != NULL &&
756 strcmp(qs->commit, t->prev_id) != 0) {
758 *prev = (struct gotweb_url){
761 .page = qs->page - 1,
763 .commit = t->prev_id,
764 .headref = qs->headref,
769 *next = (struct gotweb_url){
772 .page = qs->page + 1,
774 .commit = t->next_id,
775 .headref = qs->headref,
780 if (t->prev_id && qs->commit != NULL &&
781 strcmp(qs->commit, t->prev_id) != 0) {
783 *prev = (struct gotweb_url){
786 .page = qs->page - 1,
788 .commit = t->prev_id,
789 .headref = qs->headref,
790 .folder = qs->folder,
796 *next = (struct gotweb_url){
799 .page = qs->page + 1,
801 .commit = t->next_id,
802 .headref = qs->headref,
803 .folder = qs->folder,
809 if (t->prev_id && qs->commit != NULL &&
810 strcmp(qs->commit, t->prev_id) != 0) {
812 *prev = (struct gotweb_url){
815 .page = qs->page - 1,
817 .commit = t->prev_id,
818 .headref = qs->headref,
823 *next = (struct gotweb_url){
826 .page = qs->page + 1,
828 .commit = t->next_id,
829 .headref = qs->headref,
836 static const struct got_error *
837 gotweb_render_index(struct request *c)
839 const struct got_error *error = NULL;
840 struct server *srv = c->srv;
841 struct transport *t = c->t;
842 struct querystring *qs = t->qs;
843 struct repo_dir *repo_dir = NULL;
845 struct dirent **sd_dent = NULL;
846 unsigned int d_cnt, d_i, d_disp = 0;
847 unsigned int d_skipped = 0;
850 d = opendir(srv->repos_path);
852 error = got_error_from_errno2("opendir", srv->repos_path);
856 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
859 error = got_error_from_errno2("scandir", srv->repos_path);
863 if (gotweb_render_repo_table_hdr(c->tp) == -1)
866 for (d_i = 0; d_i < d_cnt; d_i++) {
867 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
870 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
871 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
876 error = got_path_dirent_type(&type, srv->repos_path,
880 if (type != DT_DIR) {
885 if (qs->index_page > 0 && (qs->index_page *
886 srv->max_repos_display) > t->prev_disp) {
891 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
895 error = gotweb_load_got_path(c, repo_dir);
896 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
898 gotweb_free_repo_dir(repo_dir);
903 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
909 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
912 gotweb_free_repo_dir(repo_dir);
915 if (d_disp == srv->max_repos_display)
918 t->repos_total = d_cnt - d_skipped;
920 if (srv->max_repos_display == 0)
922 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
924 if (t->repos_total <= srv->max_repos ||
925 t->repos_total <= srv->max_repos_display)
928 if (gotweb_render_navs(c->tp) == -1)
932 for (d_i = 0; d_i < d_cnt; d_i++)
936 if (d != NULL && closedir(d) == EOF && error == NULL)
937 error = got_error_from_errno("closedir");
941 static const struct got_error *
942 gotweb_render_blame(struct request *c)
944 const struct got_error *error = NULL;
945 struct transport *t = c->t;
946 struct repo_commit *rc = NULL;
947 char *age = NULL, *msg = NULL;
950 error = got_get_repo_commits(c, 1);
954 rc = TAILQ_FIRST(&t->repo_commits);
956 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
959 error = gotweb_escape_html(&msg, rc->commit_msg);
963 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
964 "<div id='blame_title'>Blame</div>\n"
965 "</div>\n" /* #blame_title_wrapper */
966 "<div id='blame_content'>\n"
967 "<div id='blame_header_wrapper'>\n"
968 "<div id='blame_header'>\n"
969 "<div class='header_age_title'>Date:</div>\n"
970 "<div class='header_age'>%s</div>\n"
971 "<div id='header_commit_msg_title'>Message:</div>\n"
972 "<div id='header_commit_msg'>%s</div>\n"
973 "</div>\n" /* #blame_header */
974 "</div>\n" /* #blame_header_wrapper */
975 "<div class='dotted_line'></div>\n"
976 "<div id='blame'>\n",
982 error = got_output_file_blame(c);
986 fcgi_printf(c, "</div>\n" /* #blame */
987 "</div>\n"); /* #blame_content */
994 static const struct got_error *
995 gotweb_render_branches(struct request *c)
997 const struct got_error *error = NULL;
998 struct got_reflist_head refs;
999 struct got_reflist_entry *re;
1000 struct transport *t = c->t;
1001 struct querystring *qs = t->qs;
1002 struct got_repository *repo = t->repo;
1003 char *escaped_refname = NULL;
1009 error = got_ref_list(&refs, repo, "refs/heads",
1010 got_ref_cmp_by_name, NULL);
1014 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1015 "<div id='branches_title'>Branches</div>\n"
1016 "</div>\n" /* #branches_title_wrapper */
1017 "<div id='branches_content'>\n");
1021 TAILQ_FOREACH(re, &refs, entry) {
1022 const char *refname = NULL;
1024 if (got_ref_is_symbolic(re->ref))
1027 refname = got_ref_get_name(re->ref);
1028 if (refname == NULL) {
1029 error = got_error_from_errno("strdup");
1032 if (strncmp(refname, "refs/heads/", 11) != 0)
1035 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1039 if (strncmp(refname, "refs/heads/", 11) == 0)
1041 error = gotweb_escape_html(&escaped_refname, refname);
1045 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1046 "<div class='branches_age'>%s</div>\n"
1047 "<div class='branches_space'> </div>\n"
1048 "<div class='branch'>", age);
1052 r = gotweb_link(c, &(struct gotweb_url){
1058 }, "%s", escaped_refname);
1062 if (fcgi_printf(c, "</div>\n" /* .branch */
1063 "<div class='navs_wrapper'>\n"
1064 "<div class='navs'>") == -1)
1067 r = gotweb_link(c, &(struct gotweb_url){
1077 if (fcgi_printf(c, " | ") == -1)
1080 r = gotweb_link(c, &(struct gotweb_url){
1086 }, "commit briefs");
1090 if (fcgi_printf(c, " | ") == -1)
1093 r = gotweb_link(c, &(struct gotweb_url){
1103 r = fcgi_printf(c, "</div>\n" /* .navs */
1104 "</div>\n" /* .navs_wrapper */
1105 "<div class='dotted_line'></div>\n"
1106 "</div>\n"); /* .branches_wrapper */
1112 free(escaped_refname);
1113 escaped_refname = NULL;
1115 fcgi_printf(c, "</div>\n"); /* #branches_content */
1118 free(escaped_refname);
1119 got_ref_list_free(&refs);
1123 static const struct got_error *
1124 gotweb_render_tree(struct request *c)
1126 const struct got_error *error = NULL;
1127 struct transport *t = c->t;
1128 struct repo_commit *rc = NULL;
1129 char *age = NULL, *msg = NULL;
1132 error = got_get_repo_commits(c, 1);
1136 rc = TAILQ_FIRST(&t->repo_commits);
1138 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1142 error = gotweb_escape_html(&msg, rc->commit_msg);
1146 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1147 "<div id='tree_title'>Tree</div>\n"
1148 "</div>\n" /* #tree_title_wrapper */
1149 "<div id='tree_content'>\n"
1150 "<div id='tree_header_wrapper'>\n"
1151 "<div id='tree_header'>\n"
1152 "<div id='header_tree_title'>Tree:</div>\n"
1153 "<div id='header_tree'>%s</div>\n"
1154 "<div class='header_age_title'>Date:</div>\n"
1155 "<div class='header_age'>%s</div>\n"
1156 "<div id='header_commit_msg_title'>Message:</div>\n"
1157 "<div id='header_commit_msg'>%s</div>\n"
1158 "</div>\n" /* #tree_header */
1159 "</div>\n" /* #tree_header_wrapper */
1160 "<div class='dotted_line'></div>\n"
1161 "<div id='tree'>\n",
1168 error = got_output_repo_tree(c);
1172 fcgi_printf(c, "</div>\n"); /* #tree */
1173 fcgi_printf(c, "</div>\n"); /* #tree_content */
1180 static const struct got_error *
1181 gotweb_render_diff(struct request *c)
1183 const struct got_error *error = NULL;
1184 struct transport *t = c->t;
1185 struct repo_commit *rc = NULL;
1186 char *age = NULL, *author = NULL, *msg = NULL;
1189 error = got_get_repo_commits(c, 1);
1193 rc = TAILQ_FIRST(&t->repo_commits);
1195 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1198 error = gotweb_escape_html(&author, rc->author);
1201 error = gotweb_escape_html(&msg, rc->commit_msg);
1205 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1206 "<div id='diff_title'>Commit Diff</div>\n"
1207 "</div>\n" /* #diff_title_wrapper */
1208 "<div id='diff_content'>\n"
1209 "<div id='diff_header_wrapper'>\n"
1210 "<div id='diff_header'>\n"
1211 "<div id='header_diff_title'>Diff:</div>\n"
1212 "<div id='header_diff'>%s<br />%s</div>\n"
1213 "<div class='header_commit_title'>Commit:</div>\n"
1214 "<div class='header_commit'>%s</div>\n"
1215 "<div id='header_tree_title'>Tree:</div>\n"
1216 "<div id='header_tree'>%s</div>\n"
1217 "<div class='header_author_title'>Author:</div>\n"
1218 "<div class='header_author'>%s</div>\n"
1219 "<div class='header_age_title'>Date:</div>\n"
1220 "<div class='header_age'>%s</div>\n"
1221 "<div id='header_commit_msg_title'>Message:</div>\n"
1222 "<div id='header_commit_msg'>%s</div>\n"
1223 "</div>\n" /* #diff_header */
1224 "</div>\n" /* #diff_header_wrapper */
1225 "<div class='dotted_line'></div>\n"
1226 "<div id='diff'>\n",
1227 rc->parent_id, rc->commit_id,
1236 error = got_output_repo_diff(c);
1240 fcgi_printf(c, "</div>\n"); /* #diff */
1241 fcgi_printf(c, "</div>\n"); /* #diff_content */
1249 static const struct got_error *
1250 gotweb_render_summary(struct request *c)
1252 const struct got_error *error = NULL;
1253 struct transport *t = c->t;
1254 struct server *srv = c->srv;
1257 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1260 if (srv->show_repo_description) {
1262 "<div id='description_title'>Description:</div>\n"
1263 "<div id='description'>%s</div>\n",
1264 t->repo_dir->description ? t->repo_dir->description : "");
1269 if (srv->show_repo_owner) {
1271 "<div id='repo_owner_title'>Owner:</div>\n"
1272 "<div id='repo_owner'>%s</div>\n",
1273 t->repo_dir->owner ? t->repo_dir->owner : "");
1278 if (srv->show_repo_age) {
1280 "<div id='last_change_title'>Last Change:</div>\n"
1281 "<div id='last_change'>%s</div>\n",
1287 if (srv->show_repo_cloneurl) {
1289 "<div id='cloneurl_title'>Clone URL:</div>\n"
1290 "<div id='cloneurl'>%s</div>\n",
1291 t->repo_dir->url ? t->repo_dir->url : "");
1296 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1300 if (gotweb_render_briefs(c->tp) == -1)
1303 error = gotweb_render_tags(c);
1305 log_warnx("%s: %s", __func__, error->msg);
1309 error = gotweb_render_branches(c);
1311 log_warnx("%s: %s", __func__, error->msg);
1316 static const struct got_error *
1317 gotweb_render_tag(struct request *c)
1319 const struct got_error *error = NULL;
1320 struct repo_tag *rt = NULL;
1321 struct transport *t = c->t;
1322 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1324 error = got_get_repo_tags(c, 1);
1328 if (t->tag_count == 0) {
1329 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1334 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1336 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1339 error = gotweb_escape_html(&author, rt->tagger);
1342 error = gotweb_escape_html(&msg, rt->commit_msg);
1346 tagname = rt->tag_name;
1347 if (strncmp(tagname, "refs/", 5) == 0)
1349 error = gotweb_escape_html(&tagname, tagname);
1353 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1354 "<div id='tags_title'>Tag</div>\n"
1355 "</div>\n" /* #tags_title_wrapper */
1356 "<div id='tags_content'>\n"
1357 "<div id='tag_header_wrapper'>\n"
1358 "<div id='tag_header'>\n"
1359 "<div class='header_commit_title'>Commit:</div>\n"
1360 "<div class='header_commit'>%s"
1361 " <span class='refs_str'>(%s)</span></div>\n"
1362 "<div class='header_author_title'>Tagger:</div>\n"
1363 "<div class='header_author'>%s</div>\n"
1364 "<div class='header_age_title'>Date:</div>\n"
1365 "<div class='header_age'>%s</div>\n"
1366 "<div id='header_commit_msg_title'>Message:</div>\n"
1367 "<div id='header_commit_msg'>%s</div>\n"
1368 "</div>\n" /* #tag_header */
1369 "<div class='dotted_line'></div>\n"
1370 "<div id='tag_commit'>\n%s</div>"
1371 "</div>" /* #tag_header_wrapper */
1372 "</div>", /* #tags_content */
1387 static const struct got_error *
1388 gotweb_render_tags(struct request *c)
1390 const struct got_error *error = NULL;
1391 struct repo_tag *rt = NULL;
1392 struct server *srv = c->srv;
1393 struct transport *t = c->t;
1394 struct querystring *qs = t->qs;
1395 struct repo_dir *repo_dir = t->repo_dir;
1396 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1397 int r, commit_found = 0;
1399 if (qs->action == BRIEFS) {
1401 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1403 error = got_get_repo_tags(c, srv->max_commits_display);
1407 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1408 "<div id='tags_title'>Tags</div>\n"
1409 "</div>\n" /* #tags_title_wrapper */
1410 "<div id='tags_content'>\n");
1414 if (t->tag_count == 0) {
1415 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1416 "This repository contains no tags");
1421 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1422 if (commit_found == 0 && qs->commit != NULL) {
1423 if (strcmp(qs->commit, rt->commit_id) != 0)
1428 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1432 tagname = rt->tag_name;
1433 if (strncmp(tagname, "refs/tags/", 10) == 0)
1435 error = gotweb_escape_html(&tagname, tagname);
1439 if (rt->tag_commit != NULL) {
1440 newline = strchr(rt->tag_commit, '\n');
1443 error = gotweb_escape_html(&msg, rt->tag_commit);
1448 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1449 "<div class='tag'>%s</div>\n"
1450 "<div class='tag_log'>", age, tagname) == -1)
1453 r = gotweb_link(c, &(struct gotweb_url){
1457 .path = repo_dir->name,
1458 .commit = rt->commit_id,
1459 }, "%s", msg ? msg : "");
1463 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1464 "<div class='navs_wrapper'>\n"
1465 "<div class='navs'>") == -1)
1468 r = gotweb_link(c, &(struct gotweb_url){
1472 .path = repo_dir->name,
1473 .commit = rt->commit_id,
1478 if (fcgi_printf(c, " | ") == -1)
1481 r = gotweb_link(c, &(struct gotweb_url){
1485 .path = repo_dir->name,
1486 .commit = rt->commit_id,
1487 }, "commit briefs");
1491 if (fcgi_printf(c, " | ") == -1)
1494 r = gotweb_link(c, &(struct gotweb_url){
1498 .path = repo_dir->name,
1499 .commit = rt->commit_id,
1505 "</div>\n" /* .navs */
1506 "</div>\n" /* .navs_wrapper */
1507 "<div class='dotted_line'></div>\n");
1518 if (t->next_id || t->prev_id) {
1519 if (gotweb_render_navs(c->tp) == -1)
1522 fcgi_printf(c, "</div>\n"); /* #tags_content */
1530 const struct got_error *
1531 gotweb_escape_html(char **escaped_html, const char *orig_html)
1533 const struct got_error *error = NULL;
1534 struct escape_pair {
1545 size_t orig_len, len;
1548 orig_len = strlen(orig_html);
1550 for (i = 0; i < orig_len; i++) {
1551 for (j = 0; j < nitems(esc); j++) {
1552 if (orig_html[i] != esc[j].c)
1554 len += strlen(esc[j].s) - 1 /* escaped char */;
1558 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1559 if (*escaped_html == NULL)
1560 return got_error_from_errno("calloc");
1563 for (i = 0; i < orig_len; i++) {
1565 for (j = 0; j < nitems(esc); j++) {
1566 if (orig_html[i] != esc[j].c)
1569 if (strlcat(*escaped_html, esc[j].s, len + 1)
1571 error = got_error(GOT_ERR_NO_SPACE);
1574 x += strlen(esc[j].s);
1579 (*escaped_html)[x] = orig_html[i];
1585 free(*escaped_html);
1586 *escaped_html = NULL;
1588 (*escaped_html)[x] = '\0';
1595 should_urlencode(int c)
1597 if (c <= ' ' || c >= 127)
1628 gotweb_urlencode(const char *str)
1636 for (s = str; *s; ++s) {
1638 if (should_urlencode(*s))
1642 escaped = calloc(1, len + 1);
1643 if (escaped == NULL)
1647 for (s = str; *s; ++s) {
1648 if (should_urlencode(*s)) {
1649 a = (*s & 0xF0) >> 4;
1653 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1654 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1663 gotweb_action_name(int action)
1698 gotweb_render_url(struct request *c, struct gotweb_url *url)
1700 const char *sep = "?", *action;
1704 action = gotweb_action_name(url->action);
1705 if (action != NULL) {
1706 if (fcgi_printf(c, "?action=%s", action) == -1)
1712 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1718 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1724 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1730 tmp = gotweb_urlencode(url->file);
1733 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1741 tmp = gotweb_urlencode(url->folder);
1744 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1752 tmp = gotweb_urlencode(url->headref);
1755 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1762 if (url->index_page != -1) {
1763 if (fcgi_printf(c, "%sindex_page=%d", sep,
1764 url->index_page) == -1)
1770 tmp = gotweb_urlencode(url->path);
1773 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1780 if (url->page != -1) {
1781 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1790 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1792 struct template *tp = c->tp;
1793 const char *proto = c->https ? "https" : "http";
1795 if (fcgi_puts(tp, proto) == -1 ||
1796 fcgi_puts(tp, "://") == -1 ||
1797 tp_htmlescape(tp, c->server_name) == -1 ||
1798 tp_htmlescape(tp, c->document_uri) == -1)
1801 return gotweb_render_url(c, url);
1805 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1810 if (fcgi_printf(c, "<a href='") == -1)
1813 if (gotweb_render_url(c, url) == -1)
1816 if (fcgi_printf(c, "'>") == -1)
1820 r = fcgi_vprintf(c, fmt, ap);
1825 if (fcgi_printf(c, "</a>"))
1830 static struct got_repository *
1831 find_cached_repo(struct server *srv, const char *path)
1835 for (i = 0; i < srv->ncached_repos; i++) {
1836 if (strcmp(srv->cached_repos[i].path, path) == 0)
1837 return srv->cached_repos[i].repo;
1843 static const struct got_error *
1844 cache_repo(struct got_repository **new, struct server *srv,
1845 struct repo_dir *repo_dir, struct socket *sock)
1847 const struct got_error *error = NULL;
1848 struct got_repository *repo;
1849 struct cached_repo *cr;
1852 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1853 cr = &srv->cached_repos[srv->ncached_repos - 1];
1854 error = got_repo_close(cr->repo);
1855 memset(cr, 0, sizeof(*cr));
1856 srv->ncached_repos--;
1859 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1860 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1861 cr = &srv->cached_repos[0];
1864 cr = &srv->cached_repos[srv->ncached_repos];
1867 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1870 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1871 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1876 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1877 >= sizeof(cr->path)) {
1879 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1880 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1882 return got_error(GOT_ERR_NO_SPACE);
1886 srv->ncached_repos++;
1891 static const struct got_error *
1892 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1894 const struct got_error *error = NULL;
1895 struct socket *sock = c->sock;
1896 struct server *srv = c->srv;
1897 struct transport *t = c->t;
1898 struct got_repository *repo = NULL;
1902 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1903 GOTWEB_GIT_DIR) == -1)
1904 return got_error_from_errno("asprintf");
1906 dt = opendir(dir_test);
1910 repo_dir->path = dir_test;
1915 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1916 repo_dir->name) == -1)
1917 return got_error_from_errno("asprintf");
1919 dt = opendir(dir_test);
1921 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1924 repo_dir->path = dir_test;
1929 if (srv->respect_exportok &&
1930 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1931 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1935 repo = find_cached_repo(srv, repo_dir->path);
1937 error = cache_repo(&repo, srv, repo_dir, sock);
1942 error = gotweb_get_repo_description(&repo_dir->description, srv,
1943 repo_dir->path, dirfd(dt));
1946 error = got_get_repo_owner(&repo_dir->owner, c);
1949 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1952 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1956 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1957 error = got_error_from_errno("closedir");
1961 static const struct got_error *
1962 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1964 const struct got_error *error;
1966 *repo_dir = calloc(1, sizeof(**repo_dir));
1967 if (*repo_dir == NULL)
1968 return got_error_from_errno("calloc");
1970 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1971 error = got_error_from_errno("asprintf");
1976 (*repo_dir)->owner = NULL;
1977 (*repo_dir)->description = NULL;
1978 (*repo_dir)->url = NULL;
1979 (*repo_dir)->age = NULL;
1980 (*repo_dir)->path = NULL;
1985 static const struct got_error *
1986 gotweb_get_repo_description(char **description, struct server *srv,
1987 const char *dirpath, int dir)
1989 const struct got_error *error = NULL;
1994 *description = NULL;
1995 if (srv->show_repo_description == 0)
1998 fd = openat(dir, "description", O_RDONLY);
2000 if (errno != ENOENT && errno != EACCES) {
2001 error = got_error_from_errno_fmt("openat %s/%s",
2002 dirpath, "description");
2007 if (fstat(fd, &sb) == -1) {
2008 error = got_error_from_errno_fmt("fstat %s/%s",
2009 dirpath, "description");
2014 if (len > GOTWEBD_MAXDESCRSZ - 1)
2015 len = GOTWEBD_MAXDESCRSZ - 1;
2017 *description = calloc(len + 1, sizeof(**description));
2018 if (*description == NULL) {
2019 error = got_error_from_errno("calloc");
2023 if (read(fd, *description, len) == -1)
2024 error = got_error_from_errno("read");
2026 if (fd != -1 && close(fd) == -1 && error == NULL)
2027 error = got_error_from_errno("close");
2031 static const struct got_error *
2032 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2035 const struct got_error *error = NULL;
2041 if (srv->show_repo_cloneurl == 0)
2044 fd = openat(dir, "cloneurl", O_RDONLY);
2046 if (errno != ENOENT && errno != EACCES) {
2047 error = got_error_from_errno_fmt("openat %s/%s",
2048 dirpath, "cloneurl");
2053 if (fstat(fd, &sb) == -1) {
2054 error = got_error_from_errno_fmt("fstat %s/%s",
2055 dirpath, "cloneurl");
2060 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2061 len = GOTWEBD_MAXCLONEURLSZ - 1;
2063 *url = calloc(len + 1, sizeof(**url));
2065 error = got_error_from_errno("calloc");
2069 if (read(fd, *url, len) == -1)
2070 error = got_error_from_errno("read");
2072 if (fd != -1 && close(fd) == -1 && error == NULL)
2073 error = got_error_from_errno("close");
2077 const struct got_error *
2078 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2081 long long diff_time;
2082 const char *years = "years ago", *months = "months ago";
2083 const char *weeks = "weeks ago", *days = "days ago";
2084 const char *hours = "hours ago", *minutes = "minutes ago";
2085 const char *seconds = "seconds ago", *now = "right now";
2094 diff_time = time(NULL) - committer_time;
2095 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2096 if (asprintf(repo_age, "%lld %s",
2097 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2098 return got_error_from_errno("asprintf");
2099 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2100 if (asprintf(repo_age, "%lld %s",
2101 (diff_time / 60 / 60 / 24 / (365 / 12)),
2103 return got_error_from_errno("asprintf");
2104 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2105 if (asprintf(repo_age, "%lld %s",
2106 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2107 return got_error_from_errno("asprintf");
2108 } else if (diff_time > 60 * 60 * 24 * 2) {
2109 if (asprintf(repo_age, "%lld %s",
2110 (diff_time / 60 / 60 / 24), days) == -1)
2111 return got_error_from_errno("asprintf");
2112 } else if (diff_time > 60 * 60 * 2) {
2113 if (asprintf(repo_age, "%lld %s",
2114 (diff_time / 60 / 60), hours) == -1)
2115 return got_error_from_errno("asprintf");
2116 } else if (diff_time > 60 * 2) {
2117 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2119 return got_error_from_errno("asprintf");
2120 } else if (diff_time > 2) {
2121 if (asprintf(repo_age, "%lld %s", diff_time,
2123 return got_error_from_errno("asprintf");
2125 if (asprintf(repo_age, "%s", now) == -1)
2126 return got_error_from_errno("asprintf");
2130 if (gmtime_r(&committer_time, &tm) == NULL)
2131 return got_error_from_errno("gmtime_r");
2133 s = asctime_r(&tm, datebuf);
2135 return got_error_from_errno("asctime_r");
2137 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2138 return got_error_from_errno("asprintf");
2141 if (gmtime_r(&committer_time, &tm) == NULL)
2142 return got_error_from_errno("gmtime_r");
2144 r = strftime(datebuf, sizeof(datebuf),
2145 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2147 return got_error(GOT_ERR_NO_SPACE);
2149 *repo_age = strdup(datebuf);
2150 if (*repo_age == NULL)
2151 return got_error_from_errno("asprintf");