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_summary(struct request *);
98 static const struct got_error *gotweb_render_tags(struct request *);
99 static const struct got_error *gotweb_render_branches(struct request *);
101 static void gotweb_free_querystring(struct querystring *);
102 static void gotweb_free_repo_dir(struct repo_dir *);
104 struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 gotweb_process_request(struct request *c)
109 const struct got_error *error = NULL, *error2 = NULL;
110 struct got_blob_object *blob = NULL;
111 struct server *srv = NULL;
112 struct querystring *qs = NULL;
113 struct repo_dir *repo_dir = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
116 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
121 log_warnx("%s: %s", __func__, error->msg);
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
130 log_warnx("%s: error server is NULL", __func__);
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
137 log_warnx("%s: %s", __func__, error->msg);
141 error = gotweb_parse_querystring(&qs, c->querystring);
143 log_warnx("%s: %s", __func__, error->msg);
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
175 error = got_output_file_blob(c);
177 log_warnx("%s: %s", __func__, error->msg);
183 if (qs->action == BLOB) {
185 struct gotweb_url url = {
190 .commit = qs->commit,
191 .folder = qs->folder,
195 error = got_get_repo_commits(c, 1);
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
216 log_warnx("%s: %s", __func__, error->msg);
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 log_warnx("%s: %s", __func__, error->msg);
225 if (gotweb_render_rss(c->tp) == -1)
231 error = gotweb_render_content_type(c, "text/html");
233 log_warnx("%s: %s", __func__, error->msg);
238 if (gotweb_render_header(c->tp) == -1)
248 error = gotweb_render_blame(c);
250 log_warnx("%s: %s", __func__, error->msg);
255 if (gotweb_render_blob(c->tp, blob) == -1)
259 if (gotweb_render_briefs(c->tp) == -1)
263 error = got_get_repo_commits(c, srv->max_commits_display);
265 log_warnx("%s: %s", __func__, error->msg);
268 if (gotweb_render_commits(c->tp) == -1)
272 error = got_get_repo_commits(c, 1);
274 log_warnx("%s: %s", __func__, error->msg);
277 error = got_open_diff_for_output(&fp, &fd, c);
279 log_warnx("%s: %s", __func__, error->msg);
282 if (gotweb_render_diff(c->tp, fp) == -1)
286 error = gotweb_render_index(c);
288 log_warnx("%s: %s", __func__, error->msg);
293 error = gotweb_render_summary(c);
295 log_warnx("%s: %s", __func__, error->msg);
300 error = got_get_repo_tags(c, 1);
302 log_warnx("%s: %s", __func__, error->msg);
305 if (c->t->tag_count == 0) {
306 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
310 if (gotweb_render_tag(c->tp) == -1)
314 error = gotweb_render_tags(c);
316 log_warnx("%s: %s", __func__, error->msg);
321 error = got_get_repo_commits(c, 1);
323 log_warnx("%s: %s", __func__, error->msg);
326 if (gotweb_render_tree(c->tp) == -1)
331 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
332 "Erorr: Bad Querystring");
340 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
342 if (fcgi_printf(c, "\n%s", err) == -1)
345 if (fcgi_printf(c, "%s", error->msg) == -1)
348 if (fcgi_printf(c, "see daemon logs for details") == -1)
351 if (html && fcgi_printf(c, "</div>\n") == -1)
355 got_object_blob_close(blob);
357 error = got_gotweb_flushfile(fp, fd);
359 log_warnx("%s: got_gotweb_flushfile failure: %s",
360 __func__, error->msg);
365 if (html && srv != NULL)
366 gotweb_render_footer(c->tp);
370 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
372 struct server *srv = NULL;
374 /* check against the server name first */
375 if (strlen(server_name) > 0)
376 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
377 if (strcmp(srv->name, server_name) == 0)
380 /* check against subdomain second */
381 if (strlen(subdomain) > 0)
382 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
383 if (strcmp(srv->name, subdomain) == 0)
386 /* if those fail, send first server */
387 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
394 const struct got_error *
395 gotweb_init_transport(struct transport **t)
397 const struct got_error *error = NULL;
399 *t = calloc(1, sizeof(**t));
401 return got_error_from_errno2("%s: calloc", __func__);
403 TAILQ_INIT(&(*t)->repo_commits);
404 TAILQ_INIT(&(*t)->repo_tags);
407 (*t)->repo_dir = NULL;
409 (*t)->next_id = NULL;
410 (*t)->prev_id = NULL;
417 static const struct got_error *
418 gotweb_init_querystring(struct querystring **qs)
420 const struct got_error *error = NULL;
422 *qs = calloc(1, sizeof(**qs));
424 return got_error_from_errno2("%s: calloc", __func__);
426 (*qs)->headref = strdup("HEAD");
427 if ((*qs)->headref == NULL) {
430 return got_error_from_errno2("%s: strdup", __func__);
433 (*qs)->action = INDEX;
434 (*qs)->commit = NULL;
436 (*qs)->folder = NULL;
437 (*qs)->index_page = 0;
443 static const struct got_error *
444 gotweb_parse_querystring(struct querystring **qs, char *qst)
446 const struct got_error *error = NULL;
447 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
448 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
455 return got_error_from_errno2("%s: strdup", __func__);
460 while (tok1_pair != NULL) {
461 strsep(&tok1_end, "&");
463 tok2 = strdup(tok1_pair);
466 return got_error_from_errno2("%s: strdup", __func__);
472 while (tok2_pair != NULL) {
473 strsep(&tok2_end, "=");
475 error = gotweb_assign_querystring(qs, tok2_pair,
480 tok2_pair = tok2_end;
483 tok1_pair = tok1_end;
494 * Adapted from usr.sbin/httpd/httpd.c url_decode.
496 static const struct got_error *
497 gotweb_urldecode(char *url)
509 /* Encoding character is followed by two hex chars */
510 if (!isxdigit((unsigned char)p[1]) ||
511 !isxdigit((unsigned char)p[2]) ||
512 (p[1] == '0' && p[2] == '0'))
513 return got_error(GOT_ERR_BAD_QUERYSTRING);
519 * We don't have to validate "hex" because it is
520 * guaranteed to include two hex chars followed by nul.
522 x = strtoul(hex, NULL, 16);
538 static const struct got_error *
539 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
541 const struct got_error *error = NULL;
545 error = gotweb_urldecode(value);
549 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
550 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
553 switch (querystring_keys[el_cnt].element) {
555 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
556 if (strcmp(value, action_keys[a_cnt].name) != 0)
558 else if (strcmp(value,
559 action_keys[a_cnt].name) == 0){
561 action_keys[a_cnt].action;
569 (*qs)->commit = strdup(value);
570 if ((*qs)->commit == NULL) {
571 error = got_error_from_errno2("%s: strdup",
577 (*qs)->file = strdup(value);
578 if ((*qs)->file == NULL) {
579 error = got_error_from_errno2("%s: strdup",
585 (*qs)->folder = strdup(value);
586 if ((*qs)->folder == NULL) {
587 error = got_error_from_errno2("%s: strdup",
593 free((*qs)->headref);
594 (*qs)->headref = strdup(value);
595 if ((*qs)->headref == NULL) {
596 error = got_error_from_errno2("%s: strdup",
602 if (strlen(value) == 0)
604 (*qs)->index_page = strtonum(value, INT64_MIN,
607 error = got_error_from_errno3("%s: strtonum %s",
611 if ((*qs)->index_page < 0)
612 (*qs)->index_page = 0;
615 (*qs)->path = strdup(value);
616 if ((*qs)->path == NULL) {
617 error = got_error_from_errno2("%s: strdup",
623 if (strlen(value) == 0)
625 (*qs)->page = strtonum(value, INT64_MIN,
628 error = got_error_from_errno3("%s: strtonum %s",
644 gotweb_free_repo_tag(struct repo_tag *rt)
649 free(rt->tag_commit);
650 free(rt->commit_msg);
657 gotweb_free_repo_commit(struct repo_commit *rc)
667 free(rc->commit_msg);
673 gotweb_free_querystring(struct querystring *qs)
686 gotweb_free_repo_dir(struct repo_dir *repo_dir)
688 if (repo_dir != NULL) {
689 free(repo_dir->name);
690 free(repo_dir->owner);
691 free(repo_dir->description);
694 free(repo_dir->path);
700 gotweb_free_transport(struct transport *t)
702 struct repo_commit *rc = NULL, *trc = NULL;
703 struct repo_tag *rt = NULL, *trt = NULL;
705 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
706 TAILQ_REMOVE(&t->repo_commits, rc, entry);
707 gotweb_free_repo_commit(rc);
709 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
710 TAILQ_REMOVE(&t->repo_tags, rt, entry);
711 gotweb_free_repo_tag(rt);
713 gotweb_free_repo_dir(t->repo_dir);
714 gotweb_free_querystring(t->qs);
720 const struct got_error *
721 gotweb_render_content_type(struct request *c, const char *type)
723 const char *csp = "default-src 'self'; script-src 'none'; "
724 "object-src 'none';";
727 "Content-Security-Policy: %s\r\n"
728 "Content-Type: %s\r\n\r\n",
733 const struct got_error *
734 gotweb_render_content_type_file(struct request *c, const char *type,
735 const char *file, const char *suffix)
737 fcgi_printf(c, "Content-type: %s\r\n"
738 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
739 type, file, suffix ? suffix : "");
744 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
745 struct gotweb_url *next, int *have_next)
747 struct transport *t = c->t;
748 struct querystring *qs = t->qs;
749 struct server *srv = c->srv;
751 *have_prev = *have_next = 0;
755 if (qs->index_page > 0) {
757 *prev = (struct gotweb_url){
759 .index_page = qs->index_page - 1,
763 if (t->next_disp == srv->max_repos_display &&
764 t->repos_total != (qs->index_page + 1) *
765 srv->max_repos_display) {
767 *next = (struct gotweb_url){
769 .index_page = qs->index_page + 1,
775 if (t->prev_id && qs->commit != NULL &&
776 strcmp(qs->commit, t->prev_id) != 0) {
778 *prev = (struct gotweb_url){
781 .page = qs->page - 1,
783 .commit = t->prev_id,
784 .headref = qs->headref,
789 *next = (struct gotweb_url){
792 .page = qs->page + 1,
794 .commit = t->next_id,
795 .headref = qs->headref,
800 if (t->prev_id && qs->commit != NULL &&
801 strcmp(qs->commit, t->prev_id) != 0) {
803 *prev = (struct gotweb_url){
806 .page = qs->page - 1,
808 .commit = t->prev_id,
809 .headref = qs->headref,
810 .folder = qs->folder,
816 *next = (struct gotweb_url){
819 .page = qs->page + 1,
821 .commit = t->next_id,
822 .headref = qs->headref,
823 .folder = qs->folder,
829 if (t->prev_id && qs->commit != NULL &&
830 strcmp(qs->commit, t->prev_id) != 0) {
832 *prev = (struct gotweb_url){
835 .page = qs->page - 1,
837 .commit = t->prev_id,
838 .headref = qs->headref,
843 *next = (struct gotweb_url){
846 .page = qs->page + 1,
848 .commit = t->next_id,
849 .headref = qs->headref,
856 static const struct got_error *
857 gotweb_render_index(struct request *c)
859 const struct got_error *error = NULL;
860 struct server *srv = c->srv;
861 struct transport *t = c->t;
862 struct querystring *qs = t->qs;
863 struct repo_dir *repo_dir = NULL;
865 struct dirent **sd_dent = NULL;
866 unsigned int d_cnt, d_i, d_disp = 0;
867 unsigned int d_skipped = 0;
870 d = opendir(srv->repos_path);
872 error = got_error_from_errno2("opendir", srv->repos_path);
876 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
879 error = got_error_from_errno2("scandir", srv->repos_path);
883 if (gotweb_render_repo_table_hdr(c->tp) == -1)
886 for (d_i = 0; d_i < d_cnt; d_i++) {
887 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
890 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
891 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
896 error = got_path_dirent_type(&type, srv->repos_path,
900 if (type != DT_DIR) {
905 if (qs->index_page > 0 && (qs->index_page *
906 srv->max_repos_display) > t->prev_disp) {
911 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
915 error = gotweb_load_got_path(c, repo_dir);
916 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
918 gotweb_free_repo_dir(repo_dir);
923 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
929 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
932 gotweb_free_repo_dir(repo_dir);
935 if (d_disp == srv->max_repos_display)
938 t->repos_total = d_cnt - d_skipped;
940 if (srv->max_repos_display == 0)
942 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
944 if (t->repos_total <= srv->max_repos ||
945 t->repos_total <= srv->max_repos_display)
948 if (gotweb_render_navs(c->tp) == -1)
952 for (d_i = 0; d_i < d_cnt; d_i++)
956 if (d != NULL && closedir(d) == EOF && error == NULL)
957 error = got_error_from_errno("closedir");
961 static const struct got_error *
962 gotweb_render_blame(struct request *c)
964 const struct got_error *error = NULL;
965 struct transport *t = c->t;
966 struct repo_commit *rc = NULL;
967 char *age = NULL, *msg = NULL;
970 error = got_get_repo_commits(c, 1);
974 rc = TAILQ_FIRST(&t->repo_commits);
976 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
979 error = gotweb_escape_html(&msg, rc->commit_msg);
983 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
984 "<div id='blame_title'>Blame</div>\n"
985 "</div>\n" /* #blame_title_wrapper */
986 "<div id='blame_content'>\n"
987 "<div id='blame_header_wrapper'>\n"
988 "<div id='blame_header'>\n"
989 "<div class='header_age_title'>Date:</div>\n"
990 "<div class='header_age'>%s</div>\n"
991 "<div id='header_commit_msg_title'>Message:</div>\n"
992 "<div id='header_commit_msg'>%s</div>\n"
993 "</div>\n" /* #blame_header */
994 "</div>\n" /* #blame_header_wrapper */
995 "<div class='dotted_line'></div>\n"
996 "<div id='blame'>\n",
1002 error = got_output_file_blame(c);
1006 fcgi_printf(c, "</div>\n" /* #blame */
1007 "</div>\n"); /* #blame_content */
1014 static const struct got_error *
1015 gotweb_render_branches(struct request *c)
1017 const struct got_error *error = NULL;
1018 struct got_reflist_head refs;
1019 struct got_reflist_entry *re;
1020 struct transport *t = c->t;
1021 struct querystring *qs = t->qs;
1022 struct got_repository *repo = t->repo;
1023 char *escaped_refname = NULL;
1029 error = got_ref_list(&refs, repo, "refs/heads",
1030 got_ref_cmp_by_name, NULL);
1034 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1035 "<div id='branches_title'>Branches</div>\n"
1036 "</div>\n" /* #branches_title_wrapper */
1037 "<div id='branches_content'>\n");
1041 TAILQ_FOREACH(re, &refs, entry) {
1042 const char *refname = NULL;
1044 if (got_ref_is_symbolic(re->ref))
1047 refname = got_ref_get_name(re->ref);
1048 if (refname == NULL) {
1049 error = got_error_from_errno("strdup");
1052 if (strncmp(refname, "refs/heads/", 11) != 0)
1055 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1059 if (strncmp(refname, "refs/heads/", 11) == 0)
1061 error = gotweb_escape_html(&escaped_refname, refname);
1065 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1066 "<div class='branches_age'>%s</div>\n"
1067 "<div class='branches_space'> </div>\n"
1068 "<div class='branch'>", age);
1072 r = gotweb_link(c, &(struct gotweb_url){
1078 }, "%s", escaped_refname);
1082 if (fcgi_printf(c, "</div>\n" /* .branch */
1083 "<div class='navs_wrapper'>\n"
1084 "<div class='navs'>") == -1)
1087 r = gotweb_link(c, &(struct gotweb_url){
1097 if (fcgi_printf(c, " | ") == -1)
1100 r = gotweb_link(c, &(struct gotweb_url){
1106 }, "commit briefs");
1110 if (fcgi_printf(c, " | ") == -1)
1113 r = gotweb_link(c, &(struct gotweb_url){
1123 r = fcgi_printf(c, "</div>\n" /* .navs */
1124 "</div>\n" /* .navs_wrapper */
1125 "<div class='dotted_line'></div>\n"
1126 "</div>\n"); /* .branches_wrapper */
1132 free(escaped_refname);
1133 escaped_refname = NULL;
1135 fcgi_printf(c, "</div>\n"); /* #branches_content */
1138 free(escaped_refname);
1139 got_ref_list_free(&refs);
1143 static const struct got_error *
1144 gotweb_render_summary(struct request *c)
1146 const struct got_error *error = NULL;
1147 struct transport *t = c->t;
1148 struct server *srv = c->srv;
1151 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1154 if (srv->show_repo_description) {
1156 "<div id='description_title'>Description:</div>\n"
1157 "<div id='description'>%s</div>\n",
1158 t->repo_dir->description ? t->repo_dir->description : "");
1163 if (srv->show_repo_owner) {
1165 "<div id='repo_owner_title'>Owner:</div>\n"
1166 "<div id='repo_owner'>%s</div>\n",
1167 t->repo_dir->owner ? t->repo_dir->owner : "");
1172 if (srv->show_repo_age) {
1174 "<div id='last_change_title'>Last Change:</div>\n"
1175 "<div id='last_change'>%s</div>\n",
1181 if (srv->show_repo_cloneurl) {
1183 "<div id='cloneurl_title'>Clone URL:</div>\n"
1184 "<div id='cloneurl'>%s</div>\n",
1185 t->repo_dir->url ? t->repo_dir->url : "");
1190 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1194 if (gotweb_render_briefs(c->tp) == -1)
1197 error = gotweb_render_tags(c);
1199 log_warnx("%s: %s", __func__, error->msg);
1203 error = gotweb_render_branches(c);
1205 log_warnx("%s: %s", __func__, error->msg);
1210 static const struct got_error *
1211 gotweb_render_tags(struct request *c)
1213 const struct got_error *error = NULL;
1214 struct server *srv = c->srv;
1215 struct transport *t = c->t;
1216 struct querystring *qs = t->qs;
1218 if (qs->action == BRIEFS) {
1220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1222 error = got_get_repo_tags(c, srv->max_commits_display);
1226 if (gotweb_render_tags_tmpl(c->tp) == -1)
1233 const struct got_error *
1234 gotweb_escape_html(char **escaped_html, const char *orig_html)
1236 const struct got_error *error = NULL;
1237 struct escape_pair {
1248 size_t orig_len, len;
1251 orig_len = strlen(orig_html);
1253 for (i = 0; i < orig_len; i++) {
1254 for (j = 0; j < nitems(esc); j++) {
1255 if (orig_html[i] != esc[j].c)
1257 len += strlen(esc[j].s) - 1 /* escaped char */;
1261 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1262 if (*escaped_html == NULL)
1263 return got_error_from_errno("calloc");
1266 for (i = 0; i < orig_len; i++) {
1268 for (j = 0; j < nitems(esc); j++) {
1269 if (orig_html[i] != esc[j].c)
1272 if (strlcat(*escaped_html, esc[j].s, len + 1)
1274 error = got_error(GOT_ERR_NO_SPACE);
1277 x += strlen(esc[j].s);
1282 (*escaped_html)[x] = orig_html[i];
1288 free(*escaped_html);
1289 *escaped_html = NULL;
1291 (*escaped_html)[x] = '\0';
1298 should_urlencode(int c)
1300 if (c <= ' ' || c >= 127)
1324 /* needed because the URLs are embedded into the HTML */
1333 gotweb_urlencode(const char *str)
1341 for (s = str; *s; ++s) {
1343 if (should_urlencode(*s))
1347 escaped = calloc(1, len + 1);
1348 if (escaped == NULL)
1352 for (s = str; *s; ++s) {
1353 if (should_urlencode(*s)) {
1354 a = (*s & 0xF0) >> 4;
1358 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1359 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1368 gotweb_action_name(int action)
1403 gotweb_render_url(struct request *c, struct gotweb_url *url)
1405 const char *sep = "?", *action;
1409 action = gotweb_action_name(url->action);
1410 if (action != NULL) {
1411 if (fcgi_printf(c, "?action=%s", action) == -1)
1417 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1423 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1429 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1435 tmp = gotweb_urlencode(url->file);
1438 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1446 tmp = gotweb_urlencode(url->folder);
1449 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1457 tmp = gotweb_urlencode(url->headref);
1460 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1467 if (url->index_page != -1) {
1468 if (fcgi_printf(c, "%sindex_page=%d", sep,
1469 url->index_page) == -1)
1475 tmp = gotweb_urlencode(url->path);
1478 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1485 if (url->page != -1) {
1486 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1495 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1497 struct template *tp = c->tp;
1498 const char *proto = c->https ? "https" : "http";
1500 if (fcgi_puts(tp, proto) == -1 ||
1501 fcgi_puts(tp, "://") == -1 ||
1502 tp_htmlescape(tp, c->server_name) == -1 ||
1503 tp_htmlescape(tp, c->document_uri) == -1)
1506 return gotweb_render_url(c, url);
1510 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1515 if (fcgi_printf(c, "<a href='") == -1)
1518 if (gotweb_render_url(c, url) == -1)
1521 if (fcgi_printf(c, "'>") == -1)
1525 r = fcgi_vprintf(c, fmt, ap);
1530 if (fcgi_printf(c, "</a>"))
1535 static struct got_repository *
1536 find_cached_repo(struct server *srv, const char *path)
1540 for (i = 0; i < srv->ncached_repos; i++) {
1541 if (strcmp(srv->cached_repos[i].path, path) == 0)
1542 return srv->cached_repos[i].repo;
1548 static const struct got_error *
1549 cache_repo(struct got_repository **new, struct server *srv,
1550 struct repo_dir *repo_dir, struct socket *sock)
1552 const struct got_error *error = NULL;
1553 struct got_repository *repo;
1554 struct cached_repo *cr;
1557 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1558 cr = &srv->cached_repos[srv->ncached_repos - 1];
1559 error = got_repo_close(cr->repo);
1560 memset(cr, 0, sizeof(*cr));
1561 srv->ncached_repos--;
1564 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1565 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1566 cr = &srv->cached_repos[0];
1569 cr = &srv->cached_repos[srv->ncached_repos];
1572 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1575 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1576 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1581 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1582 >= sizeof(cr->path)) {
1584 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1585 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1587 return got_error(GOT_ERR_NO_SPACE);
1591 srv->ncached_repos++;
1596 static const struct got_error *
1597 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1599 const struct got_error *error = NULL;
1600 struct socket *sock = c->sock;
1601 struct server *srv = c->srv;
1602 struct transport *t = c->t;
1603 struct got_repository *repo = NULL;
1607 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1608 GOTWEB_GIT_DIR) == -1)
1609 return got_error_from_errno("asprintf");
1611 dt = opendir(dir_test);
1615 repo_dir->path = dir_test;
1620 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1621 repo_dir->name) == -1)
1622 return got_error_from_errno("asprintf");
1624 dt = opendir(dir_test);
1626 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1629 repo_dir->path = dir_test;
1634 if (srv->respect_exportok &&
1635 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1636 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1640 repo = find_cached_repo(srv, repo_dir->path);
1642 error = cache_repo(&repo, srv, repo_dir, sock);
1647 error = gotweb_get_repo_description(&repo_dir->description, srv,
1648 repo_dir->path, dirfd(dt));
1651 error = got_get_repo_owner(&repo_dir->owner, c);
1654 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1657 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1661 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1662 error = got_error_from_errno("closedir");
1666 static const struct got_error *
1667 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1669 const struct got_error *error;
1671 *repo_dir = calloc(1, sizeof(**repo_dir));
1672 if (*repo_dir == NULL)
1673 return got_error_from_errno("calloc");
1675 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1676 error = got_error_from_errno("asprintf");
1681 (*repo_dir)->owner = NULL;
1682 (*repo_dir)->description = NULL;
1683 (*repo_dir)->url = NULL;
1684 (*repo_dir)->age = NULL;
1685 (*repo_dir)->path = NULL;
1690 static const struct got_error *
1691 gotweb_get_repo_description(char **description, struct server *srv,
1692 const char *dirpath, int dir)
1694 const struct got_error *error = NULL;
1699 *description = NULL;
1700 if (srv->show_repo_description == 0)
1703 fd = openat(dir, "description", O_RDONLY);
1705 if (errno != ENOENT && errno != EACCES) {
1706 error = got_error_from_errno_fmt("openat %s/%s",
1707 dirpath, "description");
1712 if (fstat(fd, &sb) == -1) {
1713 error = got_error_from_errno_fmt("fstat %s/%s",
1714 dirpath, "description");
1719 if (len > GOTWEBD_MAXDESCRSZ - 1)
1720 len = GOTWEBD_MAXDESCRSZ - 1;
1722 *description = calloc(len + 1, sizeof(**description));
1723 if (*description == NULL) {
1724 error = got_error_from_errno("calloc");
1728 if (read(fd, *description, len) == -1)
1729 error = got_error_from_errno("read");
1731 if (fd != -1 && close(fd) == -1 && error == NULL)
1732 error = got_error_from_errno("close");
1736 static const struct got_error *
1737 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1740 const struct got_error *error = NULL;
1746 if (srv->show_repo_cloneurl == 0)
1749 fd = openat(dir, "cloneurl", O_RDONLY);
1751 if (errno != ENOENT && errno != EACCES) {
1752 error = got_error_from_errno_fmt("openat %s/%s",
1753 dirpath, "cloneurl");
1758 if (fstat(fd, &sb) == -1) {
1759 error = got_error_from_errno_fmt("fstat %s/%s",
1760 dirpath, "cloneurl");
1765 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1766 len = GOTWEBD_MAXCLONEURLSZ - 1;
1768 *url = calloc(len + 1, sizeof(**url));
1770 error = got_error_from_errno("calloc");
1774 if (read(fd, *url, len) == -1)
1775 error = got_error_from_errno("read");
1777 if (fd != -1 && close(fd) == -1 && error == NULL)
1778 error = got_error_from_errno("close");
1782 const struct got_error *
1783 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1786 long long diff_time;
1787 const char *years = "years ago", *months = "months ago";
1788 const char *weeks = "weeks ago", *days = "days ago";
1789 const char *hours = "hours ago", *minutes = "minutes ago";
1790 const char *seconds = "seconds ago", *now = "right now";
1799 diff_time = time(NULL) - committer_time;
1800 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1801 if (asprintf(repo_age, "%lld %s",
1802 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1803 return got_error_from_errno("asprintf");
1804 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1805 if (asprintf(repo_age, "%lld %s",
1806 (diff_time / 60 / 60 / 24 / (365 / 12)),
1808 return got_error_from_errno("asprintf");
1809 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1810 if (asprintf(repo_age, "%lld %s",
1811 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1812 return got_error_from_errno("asprintf");
1813 } else if (diff_time > 60 * 60 * 24 * 2) {
1814 if (asprintf(repo_age, "%lld %s",
1815 (diff_time / 60 / 60 / 24), days) == -1)
1816 return got_error_from_errno("asprintf");
1817 } else if (diff_time > 60 * 60 * 2) {
1818 if (asprintf(repo_age, "%lld %s",
1819 (diff_time / 60 / 60), hours) == -1)
1820 return got_error_from_errno("asprintf");
1821 } else if (diff_time > 60 * 2) {
1822 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1824 return got_error_from_errno("asprintf");
1825 } else if (diff_time > 2) {
1826 if (asprintf(repo_age, "%lld %s", diff_time,
1828 return got_error_from_errno("asprintf");
1830 if (asprintf(repo_age, "%s", now) == -1)
1831 return got_error_from_errno("asprintf");
1835 if (gmtime_r(&committer_time, &tm) == NULL)
1836 return got_error_from_errno("gmtime_r");
1838 s = asctime_r(&tm, datebuf);
1840 return got_error_from_errno("asctime_r");
1842 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1843 return got_error_from_errno("asprintf");
1846 if (gmtime_r(&committer_time, &tm) == NULL)
1847 return got_error_from_errno("gmtime_r");
1849 r = strftime(datebuf, sizeof(datebuf),
1850 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1852 return got_error(GOT_ERR_NO_SPACE);
1854 *repo_age = strdup(datebuf);
1855 if (*repo_age == NULL)
1856 return got_error_from_errno("asprintf");