2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <netinet/in.h>
23 #include <sys/queue.h>
25 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
55 static const struct querystring_keys querystring_keys[] = {
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
66 static const struct action_keys action_keys[] = {
69 { "blobraw", BLOBRAW },
71 { "commits", COMMITS },
75 { "summary", SUMMARY },
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 static const struct got_error *gotweb_load_got_path(struct request *c,
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_diff(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tag(struct request *);
100 static const struct got_error *gotweb_render_tags(struct request *);
101 static const struct got_error *gotweb_render_branches(struct request *);
103 static void gotweb_free_querystring(struct querystring *);
104 static void gotweb_free_repo_dir(struct repo_dir *);
106 struct server *gotweb_get_server(uint8_t *, uint8_t *);
109 gotweb_process_request(struct request *c)
111 const struct got_error *error = NULL, *error2 = NULL;
112 struct got_blob_object *blob = NULL;
113 struct server *srv = NULL;
114 struct querystring *qs = NULL;
115 struct repo_dir *repo_dir = NULL;
116 uint8_t err[] = "gotwebd experienced an error: ";
117 int r, html = 0, fd = -1;
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
122 log_warnx("%s: %s", __func__, error->msg);
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
131 log_warnx("%s: error server is NULL", __func__);
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
138 log_warnx("%s: %s", __func__, error->msg);
142 error = gotweb_parse_querystring(&qs, c->querystring);
144 log_warnx("%s: %s", __func__, error->msg);
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
154 if (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == BLOBRAW || qs->action == DIFF) {
156 if (qs->commit == NULL) {
157 error2 = got_error(GOT_ERR_QUERYSTRING);
162 if (qs->action != INDEX) {
163 error = gotweb_init_repo_dir(&repo_dir, qs->path);
166 error = gotweb_load_got_path(c, repo_dir);
167 c->t->repo_dir = repo_dir;
168 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
172 if (qs->action == BLOBRAW) {
173 error = got_get_repo_commits(c, 1);
176 error = got_output_file_blob(c);
178 log_warnx("%s: %s", __func__, error->msg);
184 if (qs->action == BLOB) {
186 struct gotweb_url url = {
191 .commit = qs->commit,
192 .folder = qs->folder,
196 error = got_get_repo_commits(c, 1);
200 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
204 fcgi_puts(c->tp, "Status: 302\r\n");
205 fcgi_puts(c->tp, "Location: ");
206 gotweb_render_url(c, &url);
207 fcgi_puts(c->tp, "\r\n\r\n");
212 if (qs->action == RSS) {
213 error = gotweb_render_content_type_file(c,
214 "application/rss+xml;charset=utf-8",
215 repo_dir->name, ".rss");
217 log_warnx("%s: %s", __func__, error->msg);
221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
223 log_warnx("%s: %s", __func__, error->msg);
226 if (gotweb_render_rss(c->tp) == -1)
232 error = gotweb_render_content_type(c, "text/html");
234 log_warnx("%s: %s", __func__, error->msg);
239 if (gotweb_render_header(c->tp) == -1)
249 error = gotweb_render_blame(c);
251 log_warnx("%s: %s", __func__, error->msg);
256 if (gotweb_render_blob(c->tp, blob) == -1)
260 if (gotweb_render_briefs(c->tp) == -1)
264 error = got_get_repo_commits(c, srv->max_commits_display);
266 log_warnx("%s: %s", __func__, error->msg);
269 if (gotweb_render_commits(c->tp) == -1)
273 error = gotweb_render_diff(c);
275 log_warnx("%s: %s", __func__, error->msg);
280 error = gotweb_render_index(c);
282 log_warnx("%s: %s", __func__, error->msg);
287 error = gotweb_render_summary(c);
289 log_warnx("%s: %s", __func__, error->msg);
294 error = gotweb_render_tag(c);
296 log_warnx("%s: %s", __func__, error->msg);
301 error = gotweb_render_tags(c);
303 log_warnx("%s: %s", __func__, error->msg);
308 error = got_get_repo_commits(c, 1);
310 log_warnx("%s: %s", __func__, error->msg);
313 if (gotweb_render_tree(c->tp) == -1)
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_diff(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, *author = 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);
1141 error = gotweb_escape_html(&author, rc->author);
1144 error = gotweb_escape_html(&msg, rc->commit_msg);
1148 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1149 "<div id='diff_title'>Commit Diff</div>\n"
1150 "</div>\n" /* #diff_title_wrapper */
1151 "<div id='diff_content'>\n"
1152 "<div id='diff_header_wrapper'>\n"
1153 "<div id='diff_header'>\n"
1154 "<div id='header_diff_title'>Diff:</div>\n"
1155 "<div id='header_diff'>%s<br />%s</div>\n"
1156 "<div class='header_commit_title'>Commit:</div>\n"
1157 "<div class='header_commit'>%s</div>\n"
1158 "<div id='header_tree_title'>Tree:</div>\n"
1159 "<div id='header_tree'>%s</div>\n"
1160 "<div class='header_author_title'>Author:</div>\n"
1161 "<div class='header_author'>%s</div>\n"
1162 "<div class='header_age_title'>Date:</div>\n"
1163 "<div class='header_age'>%s</div>\n"
1164 "<div id='header_commit_msg_title'>Message:</div>\n"
1165 "<div id='header_commit_msg'>%s</div>\n"
1166 "</div>\n" /* #diff_header */
1167 "</div>\n" /* #diff_header_wrapper */
1168 "<div class='dotted_line'></div>\n"
1169 "<div id='diff'>\n",
1170 rc->parent_id, rc->commit_id,
1179 error = got_output_repo_diff(c);
1183 fcgi_printf(c, "</div>\n"); /* #diff */
1184 fcgi_printf(c, "</div>\n"); /* #diff_content */
1192 static const struct got_error *
1193 gotweb_render_summary(struct request *c)
1195 const struct got_error *error = NULL;
1196 struct transport *t = c->t;
1197 struct server *srv = c->srv;
1200 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1203 if (srv->show_repo_description) {
1205 "<div id='description_title'>Description:</div>\n"
1206 "<div id='description'>%s</div>\n",
1207 t->repo_dir->description ? t->repo_dir->description : "");
1212 if (srv->show_repo_owner) {
1214 "<div id='repo_owner_title'>Owner:</div>\n"
1215 "<div id='repo_owner'>%s</div>\n",
1216 t->repo_dir->owner ? t->repo_dir->owner : "");
1221 if (srv->show_repo_age) {
1223 "<div id='last_change_title'>Last Change:</div>\n"
1224 "<div id='last_change'>%s</div>\n",
1230 if (srv->show_repo_cloneurl) {
1232 "<div id='cloneurl_title'>Clone URL:</div>\n"
1233 "<div id='cloneurl'>%s</div>\n",
1234 t->repo_dir->url ? t->repo_dir->url : "");
1239 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1243 if (gotweb_render_briefs(c->tp) == -1)
1246 error = gotweb_render_tags(c);
1248 log_warnx("%s: %s", __func__, error->msg);
1252 error = gotweb_render_branches(c);
1254 log_warnx("%s: %s", __func__, error->msg);
1259 static const struct got_error *
1260 gotweb_render_tag(struct request *c)
1262 const struct got_error *error = NULL;
1263 struct repo_tag *rt = NULL;
1264 struct transport *t = c->t;
1265 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1267 error = got_get_repo_tags(c, 1);
1271 if (t->tag_count == 0) {
1272 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1277 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1279 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1282 error = gotweb_escape_html(&author, rt->tagger);
1285 error = gotweb_escape_html(&msg, rt->commit_msg);
1289 tagname = rt->tag_name;
1290 if (strncmp(tagname, "refs/", 5) == 0)
1292 error = gotweb_escape_html(&tagname, tagname);
1296 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1297 "<div id='tags_title'>Tag</div>\n"
1298 "</div>\n" /* #tags_title_wrapper */
1299 "<div id='tags_content'>\n"
1300 "<div id='tag_header_wrapper'>\n"
1301 "<div id='tag_header'>\n"
1302 "<div class='header_commit_title'>Commit:</div>\n"
1303 "<div class='header_commit'>%s"
1304 " <span class='refs_str'>(%s)</span></div>\n"
1305 "<div class='header_author_title'>Tagger:</div>\n"
1306 "<div class='header_author'>%s</div>\n"
1307 "<div class='header_age_title'>Date:</div>\n"
1308 "<div class='header_age'>%s</div>\n"
1309 "<div id='header_commit_msg_title'>Message:</div>\n"
1310 "<div id='header_commit_msg'>%s</div>\n"
1311 "</div>\n" /* #tag_header */
1312 "<div class='dotted_line'></div>\n"
1313 "<div id='tag_commit'>\n%s</div>"
1314 "</div>" /* #tag_header_wrapper */
1315 "</div>", /* #tags_content */
1330 static const struct got_error *
1331 gotweb_render_tags(struct request *c)
1333 const struct got_error *error = NULL;
1334 struct repo_tag *rt = NULL;
1335 struct server *srv = c->srv;
1336 struct transport *t = c->t;
1337 struct querystring *qs = t->qs;
1338 struct repo_dir *repo_dir = t->repo_dir;
1339 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1340 int r, commit_found = 0;
1342 if (qs->action == BRIEFS) {
1344 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1346 error = got_get_repo_tags(c, srv->max_commits_display);
1350 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1351 "<div id='tags_title'>Tags</div>\n"
1352 "</div>\n" /* #tags_title_wrapper */
1353 "<div id='tags_content'>\n");
1357 if (t->tag_count == 0) {
1358 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1359 "This repository contains no tags");
1364 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1365 if (commit_found == 0 && qs->commit != NULL) {
1366 if (strcmp(qs->commit, rt->commit_id) != 0)
1371 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1375 tagname = rt->tag_name;
1376 if (strncmp(tagname, "refs/tags/", 10) == 0)
1378 error = gotweb_escape_html(&tagname, tagname);
1382 if (rt->tag_commit != NULL) {
1383 newline = strchr(rt->tag_commit, '\n');
1386 error = gotweb_escape_html(&msg, rt->tag_commit);
1391 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1392 "<div class='tag'>%s</div>\n"
1393 "<div class='tag_log'>", age, tagname) == -1)
1396 r = gotweb_link(c, &(struct gotweb_url){
1400 .path = repo_dir->name,
1401 .commit = rt->commit_id,
1402 }, "%s", msg ? msg : "");
1406 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1407 "<div class='navs_wrapper'>\n"
1408 "<div class='navs'>") == -1)
1411 r = gotweb_link(c, &(struct gotweb_url){
1415 .path = repo_dir->name,
1416 .commit = rt->commit_id,
1421 if (fcgi_printf(c, " | ") == -1)
1424 r = gotweb_link(c, &(struct gotweb_url){
1428 .path = repo_dir->name,
1429 .commit = rt->commit_id,
1430 }, "commit briefs");
1434 if (fcgi_printf(c, " | ") == -1)
1437 r = gotweb_link(c, &(struct gotweb_url){
1441 .path = repo_dir->name,
1442 .commit = rt->commit_id,
1448 "</div>\n" /* .navs */
1449 "</div>\n" /* .navs_wrapper */
1450 "<div class='dotted_line'></div>\n");
1461 if (t->next_id || t->prev_id) {
1462 if (gotweb_render_navs(c->tp) == -1)
1465 fcgi_printf(c, "</div>\n"); /* #tags_content */
1473 const struct got_error *
1474 gotweb_escape_html(char **escaped_html, const char *orig_html)
1476 const struct got_error *error = NULL;
1477 struct escape_pair {
1488 size_t orig_len, len;
1491 orig_len = strlen(orig_html);
1493 for (i = 0; i < orig_len; i++) {
1494 for (j = 0; j < nitems(esc); j++) {
1495 if (orig_html[i] != esc[j].c)
1497 len += strlen(esc[j].s) - 1 /* escaped char */;
1501 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1502 if (*escaped_html == NULL)
1503 return got_error_from_errno("calloc");
1506 for (i = 0; i < orig_len; i++) {
1508 for (j = 0; j < nitems(esc); j++) {
1509 if (orig_html[i] != esc[j].c)
1512 if (strlcat(*escaped_html, esc[j].s, len + 1)
1514 error = got_error(GOT_ERR_NO_SPACE);
1517 x += strlen(esc[j].s);
1522 (*escaped_html)[x] = orig_html[i];
1528 free(*escaped_html);
1529 *escaped_html = NULL;
1531 (*escaped_html)[x] = '\0';
1538 should_urlencode(int c)
1540 if (c <= ' ' || c >= 127)
1564 /* needed because the URLs are embedded into the HTML */
1573 gotweb_urlencode(const char *str)
1581 for (s = str; *s; ++s) {
1583 if (should_urlencode(*s))
1587 escaped = calloc(1, len + 1);
1588 if (escaped == NULL)
1592 for (s = str; *s; ++s) {
1593 if (should_urlencode(*s)) {
1594 a = (*s & 0xF0) >> 4;
1598 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1599 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1608 gotweb_action_name(int action)
1643 gotweb_render_url(struct request *c, struct gotweb_url *url)
1645 const char *sep = "?", *action;
1649 action = gotweb_action_name(url->action);
1650 if (action != NULL) {
1651 if (fcgi_printf(c, "?action=%s", action) == -1)
1657 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1663 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1669 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1675 tmp = gotweb_urlencode(url->file);
1678 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1686 tmp = gotweb_urlencode(url->folder);
1689 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1697 tmp = gotweb_urlencode(url->headref);
1700 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1707 if (url->index_page != -1) {
1708 if (fcgi_printf(c, "%sindex_page=%d", sep,
1709 url->index_page) == -1)
1715 tmp = gotweb_urlencode(url->path);
1718 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1725 if (url->page != -1) {
1726 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1735 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1737 struct template *tp = c->tp;
1738 const char *proto = c->https ? "https" : "http";
1740 if (fcgi_puts(tp, proto) == -1 ||
1741 fcgi_puts(tp, "://") == -1 ||
1742 tp_htmlescape(tp, c->server_name) == -1 ||
1743 tp_htmlescape(tp, c->document_uri) == -1)
1746 return gotweb_render_url(c, url);
1750 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1755 if (fcgi_printf(c, "<a href='") == -1)
1758 if (gotweb_render_url(c, url) == -1)
1761 if (fcgi_printf(c, "'>") == -1)
1765 r = fcgi_vprintf(c, fmt, ap);
1770 if (fcgi_printf(c, "</a>"))
1775 static struct got_repository *
1776 find_cached_repo(struct server *srv, const char *path)
1780 for (i = 0; i < srv->ncached_repos; i++) {
1781 if (strcmp(srv->cached_repos[i].path, path) == 0)
1782 return srv->cached_repos[i].repo;
1788 static const struct got_error *
1789 cache_repo(struct got_repository **new, struct server *srv,
1790 struct repo_dir *repo_dir, struct socket *sock)
1792 const struct got_error *error = NULL;
1793 struct got_repository *repo;
1794 struct cached_repo *cr;
1797 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1798 cr = &srv->cached_repos[srv->ncached_repos - 1];
1799 error = got_repo_close(cr->repo);
1800 memset(cr, 0, sizeof(*cr));
1801 srv->ncached_repos--;
1804 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1805 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1806 cr = &srv->cached_repos[0];
1809 cr = &srv->cached_repos[srv->ncached_repos];
1812 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1815 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1816 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1821 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1822 >= sizeof(cr->path)) {
1824 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1825 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1827 return got_error(GOT_ERR_NO_SPACE);
1831 srv->ncached_repos++;
1836 static const struct got_error *
1837 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1839 const struct got_error *error = NULL;
1840 struct socket *sock = c->sock;
1841 struct server *srv = c->srv;
1842 struct transport *t = c->t;
1843 struct got_repository *repo = NULL;
1847 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1848 GOTWEB_GIT_DIR) == -1)
1849 return got_error_from_errno("asprintf");
1851 dt = opendir(dir_test);
1855 repo_dir->path = dir_test;
1860 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1861 repo_dir->name) == -1)
1862 return got_error_from_errno("asprintf");
1864 dt = opendir(dir_test);
1866 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1869 repo_dir->path = dir_test;
1874 if (srv->respect_exportok &&
1875 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1876 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1880 repo = find_cached_repo(srv, repo_dir->path);
1882 error = cache_repo(&repo, srv, repo_dir, sock);
1887 error = gotweb_get_repo_description(&repo_dir->description, srv,
1888 repo_dir->path, dirfd(dt));
1891 error = got_get_repo_owner(&repo_dir->owner, c);
1894 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1897 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1901 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1902 error = got_error_from_errno("closedir");
1906 static const struct got_error *
1907 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1909 const struct got_error *error;
1911 *repo_dir = calloc(1, sizeof(**repo_dir));
1912 if (*repo_dir == NULL)
1913 return got_error_from_errno("calloc");
1915 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1916 error = got_error_from_errno("asprintf");
1921 (*repo_dir)->owner = NULL;
1922 (*repo_dir)->description = NULL;
1923 (*repo_dir)->url = NULL;
1924 (*repo_dir)->age = NULL;
1925 (*repo_dir)->path = NULL;
1930 static const struct got_error *
1931 gotweb_get_repo_description(char **description, struct server *srv,
1932 const char *dirpath, int dir)
1934 const struct got_error *error = NULL;
1939 *description = NULL;
1940 if (srv->show_repo_description == 0)
1943 fd = openat(dir, "description", O_RDONLY);
1945 if (errno != ENOENT && errno != EACCES) {
1946 error = got_error_from_errno_fmt("openat %s/%s",
1947 dirpath, "description");
1952 if (fstat(fd, &sb) == -1) {
1953 error = got_error_from_errno_fmt("fstat %s/%s",
1954 dirpath, "description");
1959 if (len > GOTWEBD_MAXDESCRSZ - 1)
1960 len = GOTWEBD_MAXDESCRSZ - 1;
1962 *description = calloc(len + 1, sizeof(**description));
1963 if (*description == NULL) {
1964 error = got_error_from_errno("calloc");
1968 if (read(fd, *description, len) == -1)
1969 error = got_error_from_errno("read");
1971 if (fd != -1 && close(fd) == -1 && error == NULL)
1972 error = got_error_from_errno("close");
1976 static const struct got_error *
1977 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1980 const struct got_error *error = NULL;
1986 if (srv->show_repo_cloneurl == 0)
1989 fd = openat(dir, "cloneurl", O_RDONLY);
1991 if (errno != ENOENT && errno != EACCES) {
1992 error = got_error_from_errno_fmt("openat %s/%s",
1993 dirpath, "cloneurl");
1998 if (fstat(fd, &sb) == -1) {
1999 error = got_error_from_errno_fmt("fstat %s/%s",
2000 dirpath, "cloneurl");
2005 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2006 len = GOTWEBD_MAXCLONEURLSZ - 1;
2008 *url = calloc(len + 1, sizeof(**url));
2010 error = got_error_from_errno("calloc");
2014 if (read(fd, *url, len) == -1)
2015 error = got_error_from_errno("read");
2017 if (fd != -1 && close(fd) == -1 && error == NULL)
2018 error = got_error_from_errno("close");
2022 const struct got_error *
2023 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2026 long long diff_time;
2027 const char *years = "years ago", *months = "months ago";
2028 const char *weeks = "weeks ago", *days = "days ago";
2029 const char *hours = "hours ago", *minutes = "minutes ago";
2030 const char *seconds = "seconds ago", *now = "right now";
2039 diff_time = time(NULL) - committer_time;
2040 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2041 if (asprintf(repo_age, "%lld %s",
2042 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2043 return got_error_from_errno("asprintf");
2044 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2045 if (asprintf(repo_age, "%lld %s",
2046 (diff_time / 60 / 60 / 24 / (365 / 12)),
2048 return got_error_from_errno("asprintf");
2049 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2050 if (asprintf(repo_age, "%lld %s",
2051 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2052 return got_error_from_errno("asprintf");
2053 } else if (diff_time > 60 * 60 * 24 * 2) {
2054 if (asprintf(repo_age, "%lld %s",
2055 (diff_time / 60 / 60 / 24), days) == -1)
2056 return got_error_from_errno("asprintf");
2057 } else if (diff_time > 60 * 60 * 2) {
2058 if (asprintf(repo_age, "%lld %s",
2059 (diff_time / 60 / 60), hours) == -1)
2060 return got_error_from_errno("asprintf");
2061 } else if (diff_time > 60 * 2) {
2062 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2064 return got_error_from_errno("asprintf");
2065 } else if (diff_time > 2) {
2066 if (asprintf(repo_age, "%lld %s", diff_time,
2068 return got_error_from_errno("asprintf");
2070 if (asprintf(repo_age, "%s", now) == -1)
2071 return got_error_from_errno("asprintf");
2075 if (gmtime_r(&committer_time, &tm) == NULL)
2076 return got_error_from_errno("gmtime_r");
2078 s = asctime_r(&tm, datebuf);
2080 return got_error_from_errno("asctime_r");
2082 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2083 return got_error_from_errno("asprintf");
2086 if (gmtime_r(&committer_time, &tm) == NULL)
2087 return got_error_from_errno("gmtime_r");
2089 r = strftime(datebuf, sizeof(datebuf),
2090 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2092 return got_error(GOT_ERR_NO_SPACE);
2094 *repo_age = strdup(datebuf);
2095 if (*repo_age == NULL)
2096 return got_error_from_errno("asprintf");