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_summary(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 static void gotweb_free_querystring(struct querystring *);
103 static void gotweb_free_repo_dir(struct repo_dir *);
105 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 gotweb_process_request(struct request *c)
110 const struct got_error *error = NULL, *error2 = NULL;
111 struct got_blob_object *blob = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
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 = got_get_repo_commits(c, 1);
275 log_warnx("%s: %s", __func__, error->msg);
278 error = got_open_diff_for_output(&fp, &fd, c);
280 log_warnx("%s: %s", __func__, error->msg);
283 if (gotweb_render_diff(c->tp, fp) == -1)
287 error = gotweb_render_index(c);
289 log_warnx("%s: %s", __func__, error->msg);
294 error = gotweb_render_summary(c);
296 log_warnx("%s: %s", __func__, error->msg);
301 error = got_get_repo_tags(c, 1);
303 log_warnx("%s: %s", __func__, error->msg);
306 if (c->t->tag_count == 0) {
307 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
311 if (gotweb_render_tag(c->tp) == -1)
315 error = gotweb_render_tags(c);
317 log_warnx("%s: %s", __func__, error->msg);
322 error = got_get_repo_commits(c, 1);
324 log_warnx("%s: %s", __func__, error->msg);
327 if (gotweb_render_tree(c->tp) == -1)
332 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
333 "Erorr: Bad Querystring");
341 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
343 if (fcgi_printf(c, "\n%s", err) == -1)
346 if (fcgi_printf(c, "%s", error->msg) == -1)
349 if (fcgi_printf(c, "see daemon logs for details") == -1)
352 if (html && fcgi_printf(c, "</div>\n") == -1)
356 got_object_blob_close(blob);
358 error = got_gotweb_flushfile(fp, fd);
360 log_warnx("%s: got_gotweb_flushfile failure: %s",
361 __func__, error->msg);
366 if (html && srv != NULL)
367 gotweb_render_footer(c->tp);
371 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
373 struct server *srv = NULL;
375 /* check against the server name first */
376 if (strlen(server_name) > 0)
377 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
378 if (strcmp(srv->name, server_name) == 0)
381 /* check against subdomain second */
382 if (strlen(subdomain) > 0)
383 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
384 if (strcmp(srv->name, subdomain) == 0)
387 /* if those fail, send first server */
388 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
395 const struct got_error *
396 gotweb_init_transport(struct transport **t)
398 const struct got_error *error = NULL;
400 *t = calloc(1, sizeof(**t));
402 return got_error_from_errno2("%s: calloc", __func__);
404 TAILQ_INIT(&(*t)->repo_commits);
405 TAILQ_INIT(&(*t)->repo_tags);
408 (*t)->repo_dir = NULL;
410 (*t)->next_id = NULL;
411 (*t)->prev_id = NULL;
418 static const struct got_error *
419 gotweb_init_querystring(struct querystring **qs)
421 const struct got_error *error = NULL;
423 *qs = calloc(1, sizeof(**qs));
425 return got_error_from_errno2("%s: calloc", __func__);
427 (*qs)->headref = strdup("HEAD");
428 if ((*qs)->headref == NULL) {
431 return got_error_from_errno2("%s: strdup", __func__);
434 (*qs)->action = INDEX;
435 (*qs)->commit = NULL;
437 (*qs)->folder = NULL;
438 (*qs)->index_page = 0;
444 static const struct got_error *
445 gotweb_parse_querystring(struct querystring **qs, char *qst)
447 const struct got_error *error = NULL;
448 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
449 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
456 return got_error_from_errno2("%s: strdup", __func__);
461 while (tok1_pair != NULL) {
462 strsep(&tok1_end, "&");
464 tok2 = strdup(tok1_pair);
467 return got_error_from_errno2("%s: strdup", __func__);
473 while (tok2_pair != NULL) {
474 strsep(&tok2_end, "=");
476 error = gotweb_assign_querystring(qs, tok2_pair,
481 tok2_pair = tok2_end;
484 tok1_pair = tok1_end;
495 * Adapted from usr.sbin/httpd/httpd.c url_decode.
497 static const struct got_error *
498 gotweb_urldecode(char *url)
510 /* Encoding character is followed by two hex chars */
511 if (!isxdigit((unsigned char)p[1]) ||
512 !isxdigit((unsigned char)p[2]) ||
513 (p[1] == '0' && p[2] == '0'))
514 return got_error(GOT_ERR_BAD_QUERYSTRING);
520 * We don't have to validate "hex" because it is
521 * guaranteed to include two hex chars followed by nul.
523 x = strtoul(hex, NULL, 16);
539 static const struct got_error *
540 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
542 const struct got_error *error = NULL;
546 error = gotweb_urldecode(value);
550 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
551 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
554 switch (querystring_keys[el_cnt].element) {
556 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
557 if (strcmp(value, action_keys[a_cnt].name) != 0)
559 else if (strcmp(value,
560 action_keys[a_cnt].name) == 0){
562 action_keys[a_cnt].action;
570 (*qs)->commit = strdup(value);
571 if ((*qs)->commit == NULL) {
572 error = got_error_from_errno2("%s: strdup",
578 (*qs)->file = strdup(value);
579 if ((*qs)->file == NULL) {
580 error = got_error_from_errno2("%s: strdup",
586 (*qs)->folder = strdup(value);
587 if ((*qs)->folder == NULL) {
588 error = got_error_from_errno2("%s: strdup",
594 free((*qs)->headref);
595 (*qs)->headref = strdup(value);
596 if ((*qs)->headref == NULL) {
597 error = got_error_from_errno2("%s: strdup",
603 if (strlen(value) == 0)
605 (*qs)->index_page = strtonum(value, INT64_MIN,
608 error = got_error_from_errno3("%s: strtonum %s",
612 if ((*qs)->index_page < 0)
613 (*qs)->index_page = 0;
616 (*qs)->path = strdup(value);
617 if ((*qs)->path == NULL) {
618 error = got_error_from_errno2("%s: strdup",
624 if (strlen(value) == 0)
626 (*qs)->page = strtonum(value, INT64_MIN,
629 error = got_error_from_errno3("%s: strtonum %s",
645 gotweb_free_repo_tag(struct repo_tag *rt)
650 free(rt->tag_commit);
651 free(rt->commit_msg);
658 gotweb_free_repo_commit(struct repo_commit *rc)
668 free(rc->commit_msg);
674 gotweb_free_querystring(struct querystring *qs)
687 gotweb_free_repo_dir(struct repo_dir *repo_dir)
689 if (repo_dir != NULL) {
690 free(repo_dir->name);
691 free(repo_dir->owner);
692 free(repo_dir->description);
695 free(repo_dir->path);
701 gotweb_free_transport(struct transport *t)
703 struct repo_commit *rc = NULL, *trc = NULL;
704 struct repo_tag *rt = NULL, *trt = NULL;
706 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
707 TAILQ_REMOVE(&t->repo_commits, rc, entry);
708 gotweb_free_repo_commit(rc);
710 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
711 TAILQ_REMOVE(&t->repo_tags, rt, entry);
712 gotweb_free_repo_tag(rt);
714 gotweb_free_repo_dir(t->repo_dir);
715 gotweb_free_querystring(t->qs);
721 const struct got_error *
722 gotweb_render_content_type(struct request *c, const char *type)
724 const char *csp = "default-src 'self'; script-src 'none'; "
725 "object-src 'none';";
728 "Content-Security-Policy: %s\r\n"
729 "Content-Type: %s\r\n\r\n",
734 const struct got_error *
735 gotweb_render_content_type_file(struct request *c, const char *type,
736 const char *file, const char *suffix)
738 fcgi_printf(c, "Content-type: %s\r\n"
739 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
740 type, file, suffix ? suffix : "");
745 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
746 struct gotweb_url *next, int *have_next)
748 struct transport *t = c->t;
749 struct querystring *qs = t->qs;
750 struct server *srv = c->srv;
752 *have_prev = *have_next = 0;
756 if (qs->index_page > 0) {
758 *prev = (struct gotweb_url){
760 .index_page = qs->index_page - 1,
764 if (t->next_disp == srv->max_repos_display &&
765 t->repos_total != (qs->index_page + 1) *
766 srv->max_repos_display) {
768 *next = (struct gotweb_url){
770 .index_page = qs->index_page + 1,
776 if (t->prev_id && qs->commit != NULL &&
777 strcmp(qs->commit, t->prev_id) != 0) {
779 *prev = (struct gotweb_url){
782 .page = qs->page - 1,
784 .commit = t->prev_id,
785 .headref = qs->headref,
790 *next = (struct gotweb_url){
793 .page = qs->page + 1,
795 .commit = t->next_id,
796 .headref = qs->headref,
801 if (t->prev_id && qs->commit != NULL &&
802 strcmp(qs->commit, t->prev_id) != 0) {
804 *prev = (struct gotweb_url){
807 .page = qs->page - 1,
809 .commit = t->prev_id,
810 .headref = qs->headref,
811 .folder = qs->folder,
817 *next = (struct gotweb_url){
820 .page = qs->page + 1,
822 .commit = t->next_id,
823 .headref = qs->headref,
824 .folder = qs->folder,
830 if (t->prev_id && qs->commit != NULL &&
831 strcmp(qs->commit, t->prev_id) != 0) {
833 *prev = (struct gotweb_url){
836 .page = qs->page - 1,
838 .commit = t->prev_id,
839 .headref = qs->headref,
844 *next = (struct gotweb_url){
847 .page = qs->page + 1,
849 .commit = t->next_id,
850 .headref = qs->headref,
857 static const struct got_error *
858 gotweb_render_index(struct request *c)
860 const struct got_error *error = NULL;
861 struct server *srv = c->srv;
862 struct transport *t = c->t;
863 struct querystring *qs = t->qs;
864 struct repo_dir *repo_dir = NULL;
866 struct dirent **sd_dent = NULL;
867 unsigned int d_cnt, d_i, d_disp = 0;
868 unsigned int d_skipped = 0;
871 d = opendir(srv->repos_path);
873 error = got_error_from_errno2("opendir", srv->repos_path);
877 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
880 error = got_error_from_errno2("scandir", srv->repos_path);
884 if (gotweb_render_repo_table_hdr(c->tp) == -1)
887 for (d_i = 0; d_i < d_cnt; d_i++) {
888 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
891 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
892 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
897 error = got_path_dirent_type(&type, srv->repos_path,
901 if (type != DT_DIR) {
906 if (qs->index_page > 0 && (qs->index_page *
907 srv->max_repos_display) > t->prev_disp) {
912 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
916 error = gotweb_load_got_path(c, repo_dir);
917 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
919 gotweb_free_repo_dir(repo_dir);
924 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
930 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
933 gotweb_free_repo_dir(repo_dir);
936 if (d_disp == srv->max_repos_display)
939 t->repos_total = d_cnt - d_skipped;
941 if (srv->max_repos_display == 0)
943 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
945 if (t->repos_total <= srv->max_repos ||
946 t->repos_total <= srv->max_repos_display)
949 if (gotweb_render_navs(c->tp) == -1)
953 for (d_i = 0; d_i < d_cnt; d_i++)
957 if (d != NULL && closedir(d) == EOF && error == NULL)
958 error = got_error_from_errno("closedir");
962 static const struct got_error *
963 gotweb_render_blame(struct request *c)
965 const struct got_error *error = NULL;
966 struct transport *t = c->t;
967 struct repo_commit *rc = NULL;
968 char *age = NULL, *msg = NULL;
971 error = got_get_repo_commits(c, 1);
975 rc = TAILQ_FIRST(&t->repo_commits);
977 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
980 error = gotweb_escape_html(&msg, rc->commit_msg);
984 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
985 "<div id='blame_title'>Blame</div>\n"
986 "</div>\n" /* #blame_title_wrapper */
987 "<div id='blame_content'>\n"
988 "<div id='blame_header_wrapper'>\n"
989 "<div id='blame_header'>\n"
990 "<div class='header_age_title'>Date:</div>\n"
991 "<div class='header_age'>%s</div>\n"
992 "<div id='header_commit_msg_title'>Message:</div>\n"
993 "<div id='header_commit_msg'>%s</div>\n"
994 "</div>\n" /* #blame_header */
995 "</div>\n" /* #blame_header_wrapper */
996 "<div class='dotted_line'></div>\n"
997 "<div id='blame'>\n",
1003 error = got_output_file_blame(c);
1007 fcgi_printf(c, "</div>\n" /* #blame */
1008 "</div>\n"); /* #blame_content */
1015 static const struct got_error *
1016 gotweb_render_branches(struct request *c)
1018 const struct got_error *error = NULL;
1019 struct got_reflist_head refs;
1020 struct got_reflist_entry *re;
1021 struct transport *t = c->t;
1022 struct querystring *qs = t->qs;
1023 struct got_repository *repo = t->repo;
1024 char *escaped_refname = NULL;
1030 error = got_ref_list(&refs, repo, "refs/heads",
1031 got_ref_cmp_by_name, NULL);
1035 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1036 "<div id='branches_title'>Branches</div>\n"
1037 "</div>\n" /* #branches_title_wrapper */
1038 "<div id='branches_content'>\n");
1042 TAILQ_FOREACH(re, &refs, entry) {
1043 const char *refname = NULL;
1045 if (got_ref_is_symbolic(re->ref))
1048 refname = got_ref_get_name(re->ref);
1049 if (refname == NULL) {
1050 error = got_error_from_errno("strdup");
1053 if (strncmp(refname, "refs/heads/", 11) != 0)
1056 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1060 if (strncmp(refname, "refs/heads/", 11) == 0)
1062 error = gotweb_escape_html(&escaped_refname, refname);
1066 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1067 "<div class='branches_age'>%s</div>\n"
1068 "<div class='branches_space'> </div>\n"
1069 "<div class='branch'>", age);
1073 r = gotweb_link(c, &(struct gotweb_url){
1079 }, "%s", escaped_refname);
1083 if (fcgi_printf(c, "</div>\n" /* .branch */
1084 "<div class='navs_wrapper'>\n"
1085 "<div class='navs'>") == -1)
1088 r = gotweb_link(c, &(struct gotweb_url){
1098 if (fcgi_printf(c, " | ") == -1)
1101 r = gotweb_link(c, &(struct gotweb_url){
1107 }, "commit briefs");
1111 if (fcgi_printf(c, " | ") == -1)
1114 r = gotweb_link(c, &(struct gotweb_url){
1124 r = fcgi_printf(c, "</div>\n" /* .navs */
1125 "</div>\n" /* .navs_wrapper */
1126 "<div class='dotted_line'></div>\n"
1127 "</div>\n"); /* .branches_wrapper */
1133 free(escaped_refname);
1134 escaped_refname = NULL;
1136 fcgi_printf(c, "</div>\n"); /* #branches_content */
1139 free(escaped_refname);
1140 got_ref_list_free(&refs);
1144 static const struct got_error *
1145 gotweb_render_summary(struct request *c)
1147 const struct got_error *error = NULL;
1148 struct transport *t = c->t;
1149 struct server *srv = c->srv;
1152 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1155 if (srv->show_repo_description) {
1157 "<div id='description_title'>Description:</div>\n"
1158 "<div id='description'>%s</div>\n",
1159 t->repo_dir->description ? t->repo_dir->description : "");
1164 if (srv->show_repo_owner) {
1166 "<div id='repo_owner_title'>Owner:</div>\n"
1167 "<div id='repo_owner'>%s</div>\n",
1168 t->repo_dir->owner ? t->repo_dir->owner : "");
1173 if (srv->show_repo_age) {
1175 "<div id='last_change_title'>Last Change:</div>\n"
1176 "<div id='last_change'>%s</div>\n",
1182 if (srv->show_repo_cloneurl) {
1184 "<div id='cloneurl_title'>Clone URL:</div>\n"
1185 "<div id='cloneurl'>%s</div>\n",
1186 t->repo_dir->url ? t->repo_dir->url : "");
1191 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1195 if (gotweb_render_briefs(c->tp) == -1)
1198 error = gotweb_render_tags(c);
1200 log_warnx("%s: %s", __func__, error->msg);
1204 error = gotweb_render_branches(c);
1206 log_warnx("%s: %s", __func__, error->msg);
1211 static const struct got_error *
1212 gotweb_render_tags(struct request *c)
1214 const struct got_error *error = NULL;
1215 struct server *srv = c->srv;
1216 struct transport *t = c->t;
1217 struct querystring *qs = t->qs;
1219 if (qs->action == BRIEFS) {
1221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1223 error = got_get_repo_tags(c, srv->max_commits_display);
1227 if (gotweb_render_tags_tmpl(c->tp) == -1)
1234 const struct got_error *
1235 gotweb_escape_html(char **escaped_html, const char *orig_html)
1237 const struct got_error *error = NULL;
1238 struct escape_pair {
1249 size_t orig_len, len;
1252 orig_len = strlen(orig_html);
1254 for (i = 0; i < orig_len; i++) {
1255 for (j = 0; j < nitems(esc); j++) {
1256 if (orig_html[i] != esc[j].c)
1258 len += strlen(esc[j].s) - 1 /* escaped char */;
1262 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1263 if (*escaped_html == NULL)
1264 return got_error_from_errno("calloc");
1267 for (i = 0; i < orig_len; i++) {
1269 for (j = 0; j < nitems(esc); j++) {
1270 if (orig_html[i] != esc[j].c)
1273 if (strlcat(*escaped_html, esc[j].s, len + 1)
1275 error = got_error(GOT_ERR_NO_SPACE);
1278 x += strlen(esc[j].s);
1283 (*escaped_html)[x] = orig_html[i];
1289 free(*escaped_html);
1290 *escaped_html = NULL;
1292 (*escaped_html)[x] = '\0';
1299 should_urlencode(int c)
1301 if (c <= ' ' || c >= 127)
1325 /* needed because the URLs are embedded into the HTML */
1334 gotweb_urlencode(const char *str)
1342 for (s = str; *s; ++s) {
1344 if (should_urlencode(*s))
1348 escaped = calloc(1, len + 1);
1349 if (escaped == NULL)
1353 for (s = str; *s; ++s) {
1354 if (should_urlencode(*s)) {
1355 a = (*s & 0xF0) >> 4;
1359 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1360 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1369 gotweb_action_name(int action)
1404 gotweb_render_url(struct request *c, struct gotweb_url *url)
1406 const char *sep = "?", *action;
1410 action = gotweb_action_name(url->action);
1411 if (action != NULL) {
1412 if (fcgi_printf(c, "?action=%s", action) == -1)
1418 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1424 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1430 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1436 tmp = gotweb_urlencode(url->file);
1439 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1447 tmp = gotweb_urlencode(url->folder);
1450 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1458 tmp = gotweb_urlencode(url->headref);
1461 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1468 if (url->index_page != -1) {
1469 if (fcgi_printf(c, "%sindex_page=%d", sep,
1470 url->index_page) == -1)
1476 tmp = gotweb_urlencode(url->path);
1479 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1486 if (url->page != -1) {
1487 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1496 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1498 struct template *tp = c->tp;
1499 const char *proto = c->https ? "https" : "http";
1501 if (fcgi_puts(tp, proto) == -1 ||
1502 fcgi_puts(tp, "://") == -1 ||
1503 tp_htmlescape(tp, c->server_name) == -1 ||
1504 tp_htmlescape(tp, c->document_uri) == -1)
1507 return gotweb_render_url(c, url);
1511 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1516 if (fcgi_printf(c, "<a href='") == -1)
1519 if (gotweb_render_url(c, url) == -1)
1522 if (fcgi_printf(c, "'>") == -1)
1526 r = fcgi_vprintf(c, fmt, ap);
1531 if (fcgi_printf(c, "</a>"))
1536 static struct got_repository *
1537 find_cached_repo(struct server *srv, const char *path)
1541 for (i = 0; i < srv->ncached_repos; i++) {
1542 if (strcmp(srv->cached_repos[i].path, path) == 0)
1543 return srv->cached_repos[i].repo;
1549 static const struct got_error *
1550 cache_repo(struct got_repository **new, struct server *srv,
1551 struct repo_dir *repo_dir, struct socket *sock)
1553 const struct got_error *error = NULL;
1554 struct got_repository *repo;
1555 struct cached_repo *cr;
1558 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1559 cr = &srv->cached_repos[srv->ncached_repos - 1];
1560 error = got_repo_close(cr->repo);
1561 memset(cr, 0, sizeof(*cr));
1562 srv->ncached_repos--;
1565 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1566 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1567 cr = &srv->cached_repos[0];
1570 cr = &srv->cached_repos[srv->ncached_repos];
1573 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1576 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1577 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1582 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1583 >= sizeof(cr->path)) {
1585 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1586 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1588 return got_error(GOT_ERR_NO_SPACE);
1592 srv->ncached_repos++;
1597 static const struct got_error *
1598 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1600 const struct got_error *error = NULL;
1601 struct socket *sock = c->sock;
1602 struct server *srv = c->srv;
1603 struct transport *t = c->t;
1604 struct got_repository *repo = NULL;
1608 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1609 GOTWEB_GIT_DIR) == -1)
1610 return got_error_from_errno("asprintf");
1612 dt = opendir(dir_test);
1616 repo_dir->path = dir_test;
1621 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1622 repo_dir->name) == -1)
1623 return got_error_from_errno("asprintf");
1625 dt = opendir(dir_test);
1627 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1630 repo_dir->path = dir_test;
1635 if (srv->respect_exportok &&
1636 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1637 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1641 repo = find_cached_repo(srv, repo_dir->path);
1643 error = cache_repo(&repo, srv, repo_dir, sock);
1648 error = gotweb_get_repo_description(&repo_dir->description, srv,
1649 repo_dir->path, dirfd(dt));
1652 error = got_get_repo_owner(&repo_dir->owner, c);
1655 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1658 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1662 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1663 error = got_error_from_errno("closedir");
1667 static const struct got_error *
1668 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1670 const struct got_error *error;
1672 *repo_dir = calloc(1, sizeof(**repo_dir));
1673 if (*repo_dir == NULL)
1674 return got_error_from_errno("calloc");
1676 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1677 error = got_error_from_errno("asprintf");
1682 (*repo_dir)->owner = NULL;
1683 (*repo_dir)->description = NULL;
1684 (*repo_dir)->url = NULL;
1685 (*repo_dir)->age = NULL;
1686 (*repo_dir)->path = NULL;
1691 static const struct got_error *
1692 gotweb_get_repo_description(char **description, struct server *srv,
1693 const char *dirpath, int dir)
1695 const struct got_error *error = NULL;
1700 *description = NULL;
1701 if (srv->show_repo_description == 0)
1704 fd = openat(dir, "description", O_RDONLY);
1706 if (errno != ENOENT && errno != EACCES) {
1707 error = got_error_from_errno_fmt("openat %s/%s",
1708 dirpath, "description");
1713 if (fstat(fd, &sb) == -1) {
1714 error = got_error_from_errno_fmt("fstat %s/%s",
1715 dirpath, "description");
1720 if (len > GOTWEBD_MAXDESCRSZ - 1)
1721 len = GOTWEBD_MAXDESCRSZ - 1;
1723 *description = calloc(len + 1, sizeof(**description));
1724 if (*description == NULL) {
1725 error = got_error_from_errno("calloc");
1729 if (read(fd, *description, len) == -1)
1730 error = got_error_from_errno("read");
1732 if (fd != -1 && close(fd) == -1 && error == NULL)
1733 error = got_error_from_errno("close");
1737 static const struct got_error *
1738 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1741 const struct got_error *error = NULL;
1747 if (srv->show_repo_cloneurl == 0)
1750 fd = openat(dir, "cloneurl", O_RDONLY);
1752 if (errno != ENOENT && errno != EACCES) {
1753 error = got_error_from_errno_fmt("openat %s/%s",
1754 dirpath, "cloneurl");
1759 if (fstat(fd, &sb) == -1) {
1760 error = got_error_from_errno_fmt("fstat %s/%s",
1761 dirpath, "cloneurl");
1766 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1767 len = GOTWEBD_MAXCLONEURLSZ - 1;
1769 *url = calloc(len + 1, sizeof(**url));
1771 error = got_error_from_errno("calloc");
1775 if (read(fd, *url, len) == -1)
1776 error = got_error_from_errno("read");
1778 if (fd != -1 && close(fd) == -1 && error == NULL)
1779 error = got_error_from_errno("close");
1783 const struct got_error *
1784 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1787 long long diff_time;
1788 const char *years = "years ago", *months = "months ago";
1789 const char *weeks = "weeks ago", *days = "days ago";
1790 const char *hours = "hours ago", *minutes = "minutes ago";
1791 const char *seconds = "seconds ago", *now = "right now";
1800 diff_time = time(NULL) - committer_time;
1801 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1802 if (asprintf(repo_age, "%lld %s",
1803 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1804 return got_error_from_errno("asprintf");
1805 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1806 if (asprintf(repo_age, "%lld %s",
1807 (diff_time / 60 / 60 / 24 / (365 / 12)),
1809 return got_error_from_errno("asprintf");
1810 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1811 if (asprintf(repo_age, "%lld %s",
1812 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1813 return got_error_from_errno("asprintf");
1814 } else if (diff_time > 60 * 60 * 24 * 2) {
1815 if (asprintf(repo_age, "%lld %s",
1816 (diff_time / 60 / 60 / 24), days) == -1)
1817 return got_error_from_errno("asprintf");
1818 } else if (diff_time > 60 * 60 * 2) {
1819 if (asprintf(repo_age, "%lld %s",
1820 (diff_time / 60 / 60), hours) == -1)
1821 return got_error_from_errno("asprintf");
1822 } else if (diff_time > 60 * 2) {
1823 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1825 return got_error_from_errno("asprintf");
1826 } else if (diff_time > 2) {
1827 if (asprintf(repo_age, "%lld %s", diff_time,
1829 return got_error_from_errno("asprintf");
1831 if (asprintf(repo_age, "%s", now) == -1)
1832 return got_error_from_errno("asprintf");
1836 if (gmtime_r(&committer_time, &tm) == NULL)
1837 return got_error_from_errno("gmtime_r");
1839 s = asctime_r(&tm, datebuf);
1841 return got_error_from_errno("asctime_r");
1843 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1844 return got_error_from_errno("asprintf");
1847 if (gmtime_r(&committer_time, &tm) == NULL)
1848 return got_error_from_errno("gmtime_r");
1850 r = strftime(datebuf, sizeof(datebuf),
1851 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1853 return got_error(GOT_ERR_NO_SPACE);
1855 *repo_age = strdup(datebuf);
1856 if (*repo_age == NULL)
1857 return got_error_from_errno("asprintf");