Blob


1 /*
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>
7 *
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.
11 *
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.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
54 static const struct querystring_keys querystring_keys[] = {
55 { "action", ACTION },
56 { "commit", COMMIT },
57 { "file", RFILE },
58 { "folder", FOLDER },
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
61 { "path", PATH },
62 { "page", PAGE },
63 };
65 static const struct action_keys action_keys[] = {
66 { "blame", BLAME },
67 { "blob", BLOB },
68 { "briefs", BRIEFS },
69 { "commits", COMMITS },
70 { "diff", DIFF },
71 { "error", ERR },
72 { "index", INDEX },
73 { "summary", SUMMARY },
74 { "tag", TAG },
75 { "tags", TAGS },
76 { "tree", TREE },
77 };
79 static const struct got_error *gotweb_init_querystring(struct querystring **);
80 static const struct got_error *gotweb_parse_querystring(struct querystring **,
81 char *);
82 static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 char *, char *);
84 static const struct got_error *gotweb_render_index(struct request *);
85 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 const char *);
87 static const struct got_error *gotweb_load_got_path(struct request *c,
88 struct repo_dir *);
89 static const struct got_error *gotweb_get_repo_description(char **,
90 struct server *, const char *, int);
91 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 const char *, int);
93 static const struct got_error *gotweb_render_blame(struct request *);
94 static const struct got_error *gotweb_render_commits(struct request *);
95 static const struct got_error *gotweb_render_diff(struct request *);
96 static const struct got_error *gotweb_render_summary(struct request *);
97 static const struct got_error *gotweb_render_tag(struct request *);
98 static const struct got_error *gotweb_render_tags(struct request *);
99 static const struct got_error *gotweb_render_tree(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 const struct got_error *gotweb_render_navs(struct request *);
104 static void gotweb_free_querystring(struct querystring *);
105 static void gotweb_free_repo_dir(struct repo_dir *);
107 struct server *gotweb_get_server(uint8_t *, uint8_t *);
109 void
110 gotweb_process_request(struct request *c)
112 const struct got_error *error = NULL, *error2 = 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;
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
121 if (error) {
122 log_warnx("%s: %s", __func__, error->msg);
123 return;
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
127 return;
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
130 if (srv == NULL) {
131 log_warnx("%s: error server is NULL", __func__);
132 goto err;
134 c->srv = srv;
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
137 if (error) {
138 log_warnx("%s: %s", __func__, error->msg);
139 goto err;
141 c->t->qs = qs;
142 error = gotweb_parse_querystring(&qs, c->querystring);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
148 /*
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
151 * querystring.
152 */
154 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == DIFF)) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
157 goto render;
160 if (qs->action != INDEX) {
161 error = gotweb_init_repo_dir(&repo_dir, qs->path);
162 if (error)
163 goto done;
164 error = gotweb_load_got_path(c, repo_dir);
165 c->t->repo_dir = repo_dir;
166 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 goto err;
170 /* render top of page */
171 if (qs != NULL && qs->action == BLOB) {
172 error = got_get_repo_commits(c, 1);
173 if (error)
174 goto done;
175 error = got_output_file_blob(c);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
180 goto done;
183 render:
184 error = gotweb_render_content_type(c, "text/html");
185 if (error) {
186 log_warnx("%s: %s", __func__, error->msg);
187 goto err;
189 html = 1;
191 if (gotweb_render_header(c->tp) == -1)
192 goto err;
194 if (error2) {
195 error = error2;
196 goto err;
199 switch(qs->action) {
200 case BLAME:
201 error = gotweb_render_blame(c);
202 if (error) {
203 log_warnx("%s: %s", __func__, error->msg);
204 goto err;
206 break;
207 case BRIEFS:
208 if (gotweb_render_briefs(c->tp) == -1)
209 goto err;
210 break;
211 case COMMITS:
212 error = gotweb_render_commits(c);
213 if (error) {
214 log_warnx("%s: %s", __func__, error->msg);
215 goto err;
217 break;
218 case DIFF:
219 error = gotweb_render_diff(c);
220 if (error) {
221 log_warnx("%s: %s", __func__, error->msg);
222 goto err;
224 break;
225 case INDEX:
226 error = gotweb_render_index(c);
227 if (error) {
228 log_warnx("%s: %s", __func__, error->msg);
229 goto err;
231 break;
232 case SUMMARY:
233 error = gotweb_render_summary(c);
234 if (error) {
235 log_warnx("%s: %s", __func__, error->msg);
236 goto err;
238 break;
239 case TAG:
240 error = gotweb_render_tag(c);
241 if (error) {
242 log_warnx("%s: %s", __func__, error->msg);
243 goto err;
245 break;
246 case TAGS:
247 error = gotweb_render_tags(c);
248 if (error) {
249 log_warnx("%s: %s", __func__, error->msg);
250 goto err;
252 break;
253 case TREE:
254 error = gotweb_render_tree(c);
255 if (error) {
256 log_warnx("%s: %s", __func__, error->msg);
257 goto err;
259 break;
260 case ERR:
261 default:
262 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
263 "Erorr: Bad Querystring");
264 if (r == -1)
265 goto err;
266 break;
269 goto done;
270 err:
271 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
272 return;
273 if (fcgi_printf(c, "\n%s", err) == -1)
274 return;
275 if (error) {
276 if (fcgi_printf(c, "%s", error->msg) == -1)
277 return;
278 } else {
279 if (fcgi_printf(c, "see daemon logs for details") == -1)
280 return;
282 if (html && fcgi_printf(c, "</div>\n") == -1)
283 return;
284 done:
285 if (html && srv != NULL)
286 gotweb_render_footer(c->tp);
289 struct server *
290 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
292 struct server *srv = NULL;
294 /* check against the server name first */
295 if (strlen(server_name) > 0)
296 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
297 if (strcmp(srv->name, server_name) == 0)
298 goto done;
300 /* check against subdomain second */
301 if (strlen(subdomain) > 0)
302 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
303 if (strcmp(srv->name, subdomain) == 0)
304 goto done;
306 /* if those fail, send first server */
307 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
308 if (srv != NULL)
309 break;
310 done:
311 return srv;
312 };
314 const struct got_error *
315 gotweb_init_transport(struct transport **t)
317 const struct got_error *error = NULL;
319 *t = calloc(1, sizeof(**t));
320 if (*t == NULL)
321 return got_error_from_errno2("%s: calloc", __func__);
323 TAILQ_INIT(&(*t)->repo_commits);
324 TAILQ_INIT(&(*t)->repo_tags);
326 (*t)->repo = NULL;
327 (*t)->repo_dir = NULL;
328 (*t)->qs = NULL;
329 (*t)->next_id = NULL;
330 (*t)->prev_id = NULL;
331 (*t)->next_disp = 0;
332 (*t)->prev_disp = 0;
334 return error;
337 static const struct got_error *
338 gotweb_init_querystring(struct querystring **qs)
340 const struct got_error *error = NULL;
342 *qs = calloc(1, sizeof(**qs));
343 if (*qs == NULL)
344 return got_error_from_errno2("%s: calloc", __func__);
346 (*qs)->headref = strdup("HEAD");
347 if ((*qs)->headref == NULL) {
348 free(*qs);
349 *qs = NULL;
350 return got_error_from_errno2("%s: strdup", __func__);
353 (*qs)->action = INDEX;
354 (*qs)->commit = NULL;
355 (*qs)->file = NULL;
356 (*qs)->folder = NULL;
357 (*qs)->index_page = 0;
358 (*qs)->path = NULL;
360 return error;
363 static const struct got_error *
364 gotweb_parse_querystring(struct querystring **qs, char *qst)
366 const struct got_error *error = NULL;
367 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
368 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
370 if (qst == NULL)
371 return error;
373 tok1 = strdup(qst);
374 if (tok1 == NULL)
375 return got_error_from_errno2("%s: strdup", __func__);
377 tok1_pair = tok1;
378 tok1_end = tok1;
380 while (tok1_pair != NULL) {
381 strsep(&tok1_end, "&");
383 tok2 = strdup(tok1_pair);
384 if (tok2 == NULL) {
385 free(tok1);
386 return got_error_from_errno2("%s: strdup", __func__);
389 tok2_pair = tok2;
390 tok2_end = tok2;
392 while (tok2_pair != NULL) {
393 strsep(&tok2_end, "=");
394 if (tok2_end) {
395 error = gotweb_assign_querystring(qs, tok2_pair,
396 tok2_end);
397 if (error)
398 goto err;
400 tok2_pair = tok2_end;
402 free(tok2);
403 tok1_pair = tok1_end;
405 free(tok1);
406 return error;
407 err:
408 free(tok2);
409 free(tok1);
410 return error;
413 /*
414 * Adapted from usr.sbin/httpd/httpd.c url_decode.
415 */
416 static const struct got_error *
417 gotweb_urldecode(char *url)
419 char *p, *q;
420 char hex[3];
421 unsigned long x;
423 hex[2] = '\0';
424 p = q = url;
426 while (*p != '\0') {
427 switch (*p) {
428 case '%':
429 /* Encoding character is followed by two hex chars */
430 if (!isxdigit((unsigned char)p[1]) ||
431 !isxdigit((unsigned char)p[2]) ||
432 (p[1] == '0' && p[2] == '0'))
433 return got_error(GOT_ERR_BAD_QUERYSTRING);
435 hex[0] = p[1];
436 hex[1] = p[2];
438 /*
439 * We don't have to validate "hex" because it is
440 * guaranteed to include two hex chars followed by nul.
441 */
442 x = strtoul(hex, NULL, 16);
443 *q = (char)x;
444 p += 2;
445 break;
446 default:
447 *q = *p;
448 break;
450 p++;
451 q++;
453 *q = '\0';
455 return NULL;
458 static const struct got_error *
459 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
461 const struct got_error *error = NULL;
462 const char *errstr;
463 int a_cnt, el_cnt;
465 error = gotweb_urldecode(value);
466 if (error)
467 return error;
469 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
470 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
471 continue;
473 switch (querystring_keys[el_cnt].element) {
474 case ACTION:
475 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
476 if (strcmp(value, action_keys[a_cnt].name) != 0)
477 continue;
478 else if (strcmp(value,
479 action_keys[a_cnt].name) == 0){
480 (*qs)->action =
481 action_keys[a_cnt].action;
482 goto qa_found;
485 (*qs)->action = ERR;
486 qa_found:
487 break;
488 case COMMIT:
489 (*qs)->commit = strdup(value);
490 if ((*qs)->commit == NULL) {
491 error = got_error_from_errno2("%s: strdup",
492 __func__);
493 goto done;
495 break;
496 case RFILE:
497 (*qs)->file = strdup(value);
498 if ((*qs)->file == NULL) {
499 error = got_error_from_errno2("%s: strdup",
500 __func__);
501 goto done;
503 break;
504 case FOLDER:
505 (*qs)->folder = strdup(value);
506 if ((*qs)->folder == NULL) {
507 error = got_error_from_errno2("%s: strdup",
508 __func__);
509 goto done;
511 break;
512 case HEADREF:
513 free((*qs)->headref);
514 (*qs)->headref = strdup(value);
515 if ((*qs)->headref == NULL) {
516 error = got_error_from_errno2("%s: strdup",
517 __func__);
518 goto done;
520 break;
521 case INDEX_PAGE:
522 if (strlen(value) == 0)
523 break;
524 (*qs)->index_page = strtonum(value, INT64_MIN,
525 INT64_MAX, &errstr);
526 if (errstr) {
527 error = got_error_from_errno3("%s: strtonum %s",
528 __func__, errstr);
529 goto done;
531 if ((*qs)->index_page < 0)
532 (*qs)->index_page = 0;
533 break;
534 case PATH:
535 (*qs)->path = strdup(value);
536 if ((*qs)->path == NULL) {
537 error = got_error_from_errno2("%s: strdup",
538 __func__);
539 goto done;
541 break;
542 case PAGE:
543 if (strlen(value) == 0)
544 break;
545 (*qs)->page = strtonum(value, INT64_MIN,
546 INT64_MAX, &errstr);
547 if (errstr) {
548 error = got_error_from_errno3("%s: strtonum %s",
549 __func__, errstr);
550 goto done;
552 if ((*qs)->page < 0)
553 (*qs)->page = 0;
554 break;
555 default:
556 break;
559 done:
560 return error;
563 void
564 gotweb_free_repo_tag(struct repo_tag *rt)
566 if (rt != NULL) {
567 free(rt->commit_id);
568 free(rt->tag_name);
569 free(rt->tag_commit);
570 free(rt->commit_msg);
571 free(rt->tagger);
573 free(rt);
576 void
577 gotweb_free_repo_commit(struct repo_commit *rc)
579 if (rc != NULL) {
580 free(rc->path);
581 free(rc->refs_str);
582 free(rc->commit_id);
583 free(rc->parent_id);
584 free(rc->tree_id);
585 free(rc->author);
586 free(rc->committer);
587 free(rc->commit_msg);
589 free(rc);
592 static void
593 gotweb_free_querystring(struct querystring *qs)
595 if (qs != NULL) {
596 free(qs->commit);
597 free(qs->file);
598 free(qs->folder);
599 free(qs->headref);
600 free(qs->path);
602 free(qs);
605 static void
606 gotweb_free_repo_dir(struct repo_dir *repo_dir)
608 if (repo_dir != NULL) {
609 free(repo_dir->name);
610 free(repo_dir->owner);
611 free(repo_dir->description);
612 free(repo_dir->url);
613 free(repo_dir->age);
614 free(repo_dir->path);
616 free(repo_dir);
619 void
620 gotweb_free_transport(struct transport *t)
622 struct repo_commit *rc = NULL, *trc = NULL;
623 struct repo_tag *rt = NULL, *trt = NULL;
625 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
626 TAILQ_REMOVE(&t->repo_commits, rc, entry);
627 gotweb_free_repo_commit(rc);
629 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
630 TAILQ_REMOVE(&t->repo_tags, rt, entry);
631 gotweb_free_repo_tag(rt);
633 gotweb_free_repo_dir(t->repo_dir);
634 gotweb_free_querystring(t->qs);
635 free(t->next_id);
636 free(t->prev_id);
637 free(t);
640 const struct got_error *
641 gotweb_render_content_type(struct request *c, const uint8_t *type)
643 const char *csp = "default-src 'self'; script-src 'none'; "
644 "object-src 'none';";
646 fcgi_printf(c,
647 "Content-Security-Policy: %s\r\n"
648 "Content-Type: %s\r\n\r\n",
649 csp, type);
650 return NULL;
653 const struct got_error *
654 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
655 char *file)
657 fcgi_printf(c, "Content-type: %s\r\n"
658 "Content-disposition: attachment; filename=%s\r\n\r\n",
659 type, file);
660 return NULL;
663 const struct got_error *
664 gotweb_render_navs(struct request *c)
666 const struct got_error *error = NULL;
667 struct transport *t = c->t;
668 struct querystring *qs = t->qs;
669 struct server *srv = c->srv;
670 int r;
672 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
673 if (r == -1)
674 goto done;
676 switch(qs->action) {
677 case INDEX:
678 if (qs->index_page > 0) {
679 struct gotweb_url url = {
680 .action = -1,
681 .index_page = qs->index_page - 1,
682 .page = -1,
683 };
685 r = gotweb_link(c, &url, "Previous");
687 break;
688 case BRIEFS:
689 if (t->prev_id && qs->commit != NULL &&
690 strcmp(qs->commit, t->prev_id) != 0) {
691 struct gotweb_url url = {
692 .action = BRIEFS,
693 .index_page = -1,
694 .page = qs->page - 1,
695 .path = qs->path,
696 .commit = t->prev_id,
697 .headref = qs->headref,
698 };
700 r = gotweb_link(c, &url, "Previous");
702 break;
703 case COMMITS:
704 if (t->prev_id && qs->commit != NULL &&
705 strcmp(qs->commit, t->prev_id) != 0) {
706 struct gotweb_url url = {
707 .action = COMMITS,
708 .index_page = -1,
709 .page = qs->page - 1,
710 .path = qs->path,
711 .commit = t->prev_id,
712 .headref = qs->headref,
713 .folder = qs->folder,
714 .file = qs->file,
715 };
717 r = gotweb_link(c, &url, "Previous");
719 break;
720 case TAGS:
721 if (t->prev_id && qs->commit != NULL &&
722 strcmp(qs->commit, t->prev_id) != 0) {
723 struct gotweb_url url = {
724 .action = TAGS,
725 .index_page = -1,
726 .page = qs->page - 1,
727 .path = qs->path,
728 .commit = t->prev_id,
729 .headref = qs->headref,
730 };
732 r = gotweb_link(c, &url, "Previous");
734 break;
737 if (r == -1)
738 goto done;
740 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
741 "<div id='nav_next'>");
742 if (r == -1)
743 goto done;
745 switch(qs->action) {
746 case INDEX:
747 if (t->next_disp == srv->max_repos_display &&
748 t->repos_total != (qs->index_page + 1) *
749 srv->max_repos_display) {
750 struct gotweb_url url = {
751 .action = -1,
752 .index_page = qs->index_page + 1,
753 .page = -1,
754 };
756 r = gotweb_link(c, &url, "Next");
758 break;
759 case BRIEFS:
760 if (t->next_id) {
761 struct gotweb_url url = {
762 .action = BRIEFS,
763 .index_page = -1,
764 .page = qs->page + 1,
765 .path = qs->path,
766 .commit = t->next_id,
767 .headref = qs->headref,
768 };
770 r = gotweb_link(c, &url, "Next");
772 break;
773 case COMMITS:
774 if (t->next_id) {
775 struct gotweb_url url = {
776 .action = COMMITS,
777 .index_page = -1,
778 .page = qs->page + 1,
779 .path = qs->path,
780 .commit = t->next_id,
781 .headref = qs->headref,
782 .folder = qs->folder,
783 .file = qs->file,
784 };
786 r = gotweb_link(c, &url, "Next");
788 break;
789 case TAGS:
790 if (t->next_id) {
791 struct gotweb_url url = {
792 .action = TAGS,
793 .index_page = -1,
794 .page = qs->page + 1,
795 .path = qs->path,
796 .commit = t->next_id,
797 .headref = qs->headref,
798 };
800 r = gotweb_link(c, &url, "Next");
802 break;
804 if (r == -1)
805 goto done;
807 fcgi_printf(c, "</div>\n"); /* #nav_next */
808 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
809 done:
810 free(t->next_id);
811 t->next_id = NULL;
812 free(t->prev_id);
813 t->prev_id = NULL;
814 return error;
817 static const struct got_error *
818 gotweb_render_index(struct request *c)
820 const struct got_error *error = NULL;
821 struct server *srv = c->srv;
822 struct transport *t = c->t;
823 struct querystring *qs = t->qs;
824 struct repo_dir *repo_dir = NULL;
825 DIR *d;
826 struct dirent **sd_dent = NULL;
827 unsigned int d_cnt, d_i, d_disp = 0;
828 unsigned int d_skipped = 0;
829 int type;
831 d = opendir(srv->repos_path);
832 if (d == NULL) {
833 error = got_error_from_errno2("opendir", srv->repos_path);
834 return error;
837 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
838 if (d_cnt == -1) {
839 sd_dent = NULL;
840 error = got_error_from_errno2("scandir", srv->repos_path);
841 goto done;
844 if (gotweb_render_repo_table_hdr(c->tp) == -1)
845 goto done;
847 for (d_i = 0; d_i < d_cnt; d_i++) {
848 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
849 break;
851 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
852 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
853 d_skipped++;
854 continue;
857 error = got_path_dirent_type(&type, srv->repos_path,
858 sd_dent[d_i]);
859 if (error)
860 goto done;
861 if (type != DT_DIR) {
862 d_skipped++;
863 continue;
866 if (qs->index_page > 0 && (qs->index_page *
867 srv->max_repos_display) > t->prev_disp) {
868 t->prev_disp++;
869 continue;
872 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
873 if (error)
874 goto done;
876 error = gotweb_load_got_path(c, repo_dir);
877 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
878 error = NULL;
879 gotweb_free_repo_dir(repo_dir);
880 repo_dir = NULL;
881 d_skipped++;
882 continue;
884 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
885 goto done;
887 d_disp++;
888 t->prev_disp++;
890 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
891 goto done;
893 gotweb_free_repo_dir(repo_dir);
894 repo_dir = NULL;
895 t->next_disp++;
896 if (d_disp == srv->max_repos_display)
897 break;
899 t->repos_total = d_cnt - d_skipped;
901 if (srv->max_repos_display == 0)
902 goto done;
903 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
904 goto done;
905 if (t->repos_total <= srv->max_repos ||
906 t->repos_total <= srv->max_repos_display)
907 goto done;
909 error = gotweb_render_navs(c);
910 if (error)
911 goto done;
912 done:
913 if (sd_dent) {
914 for (d_i = 0; d_i < d_cnt; d_i++)
915 free(sd_dent[d_i]);
916 free(sd_dent);
918 if (d != NULL && closedir(d) == EOF && error == NULL)
919 error = got_error_from_errno("closedir");
920 return error;
923 static const struct got_error *
924 gotweb_render_blame(struct request *c)
926 const struct got_error *error = NULL;
927 struct transport *t = c->t;
928 struct repo_commit *rc = NULL;
929 char *age = NULL, *msg = NULL;
930 int r;
932 error = got_get_repo_commits(c, 1);
933 if (error)
934 return error;
936 rc = TAILQ_FIRST(&t->repo_commits);
938 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
939 if (error)
940 goto done;
941 error = gotweb_escape_html(&msg, rc->commit_msg);
942 if (error)
943 goto done;
945 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
946 "<div id='blame_title'>Blame</div>\n"
947 "</div>\n" /* #blame_title_wrapper */
948 "<div id='blame_content'>\n"
949 "<div id='blame_header_wrapper'>\n"
950 "<div id='blame_header'>\n"
951 "<div class='header_age_title'>Date:</div>\n"
952 "<div class='header_age'>%s</div>\n"
953 "<div id='header_commit_msg_title'>Message:</div>\n"
954 "<div id='header_commit_msg'>%s</div>\n"
955 "</div>\n" /* #blame_header */
956 "</div>\n" /* #blame_header_wrapper */
957 "<div class='dotted_line'></div>\n"
958 "<div id='blame'>\n",
959 age,
960 msg);
961 if (r == -1)
962 goto done;
964 error = got_output_file_blame(c);
965 if (error)
966 goto done;
968 fcgi_printf(c, "</div>\n" /* #blame */
969 "</div>\n"); /* #blame_content */
970 done:
971 free(age);
972 free(msg);
973 return error;
976 static const struct got_error *
977 gotweb_render_commits(struct request *c)
979 const struct got_error *error = NULL;
980 struct repo_commit *rc = NULL;
981 struct server *srv = c->srv;
982 struct transport *t = c->t;
983 struct repo_dir *repo_dir = t->repo_dir;
984 char *age = NULL, *author = NULL, *msg = NULL;
985 int r;
987 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
988 "<div class='commits_title'>Commits</div>\n"
989 "</div>\n" /* .commits_title_wrapper */
990 "<div class='commits_content'>\n");
991 if (r == -1)
992 goto done;
994 error = got_get_repo_commits(c, srv->max_commits_display);
995 if (error)
996 goto done;
998 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
999 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1000 if (error)
1001 goto done;
1002 error = gotweb_escape_html(&author, rc->author);
1003 if (error)
1004 goto done;
1005 error = gotweb_escape_html(&msg, rc->commit_msg);
1006 if (error)
1007 goto done;
1009 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1010 "<div class='commits_header'>\n"
1011 "<div class='header_commit_title'>Commit:</div>\n"
1012 "<div class='header_commit'>%s</div>\n"
1013 "<div class='header_author_title'>Author:</div>\n"
1014 "<div class='header_author'>%s</div>\n"
1015 "<div class='header_age_title'>Date:</div>\n"
1016 "<div class='header_age'>%s</div>\n"
1017 "</div>\n" /* .commits_header */
1018 "</div>\n" /* .commits_header_wrapper */
1019 "<div class='dotted_line'></div>\n"
1020 "<div class='commit'>\n%s</div>\n",
1021 rc->commit_id,
1022 author,
1023 age,
1024 msg);
1025 if (r == -1)
1026 goto done;
1028 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1029 "<div class='navs'>") == -1)
1030 goto done;
1032 r = gotweb_link(c, &(struct gotweb_url){
1033 .action = DIFF,
1034 .index_page = -1,
1035 .page = -1,
1036 .path = repo_dir->name,
1037 .commit = rc->commit_id,
1038 }, "diff");
1039 if (r == -1)
1040 goto done;
1042 if (fcgi_printf(c, " | ") == -1)
1043 goto done;
1045 r = gotweb_link(c, &(struct gotweb_url){
1046 .action = TREE,
1047 .index_page = -1,
1048 .page = -1,
1049 .path = repo_dir->name,
1050 .commit = rc->commit_id,
1051 }, "tree");
1052 if (r == -1)
1053 goto done;
1055 if (fcgi_printf(c, "</div>\n" /* .navs */
1056 "</div>\n" /* .navs_wrapper */
1057 "<div class='dotted_line'></div>\n") == -1)
1058 goto done;
1060 free(age);
1061 age = NULL;
1062 free(author);
1063 author = NULL;
1064 free(msg);
1065 msg = NULL;
1068 if (t->next_id || t->prev_id) {
1069 error = gotweb_render_navs(c);
1070 if (error)
1071 goto done;
1073 fcgi_printf(c, "</div>\n"); /* .commits_content */
1074 done:
1075 free(age);
1076 free(author);
1077 free(msg);
1078 return error;
1081 static const struct got_error *
1082 gotweb_render_branches(struct request *c)
1084 const struct got_error *error = NULL;
1085 struct got_reflist_head refs;
1086 struct got_reflist_entry *re;
1087 struct transport *t = c->t;
1088 struct querystring *qs = t->qs;
1089 struct got_repository *repo = t->repo;
1090 char *escaped_refname = NULL;
1091 char *age = NULL;
1092 int r;
1094 TAILQ_INIT(&refs);
1096 error = got_ref_list(&refs, repo, "refs/heads",
1097 got_ref_cmp_by_name, NULL);
1098 if (error)
1099 goto done;
1101 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1102 "<div id='branches_title'>Branches</div>\n"
1103 "</div>\n" /* #branches_title_wrapper */
1104 "<div id='branches_content'>\n");
1105 if (r == -1)
1106 goto done;
1108 TAILQ_FOREACH(re, &refs, entry) {
1109 const char *refname = NULL;
1111 if (got_ref_is_symbolic(re->ref))
1112 continue;
1114 refname = got_ref_get_name(re->ref);
1115 if (refname == NULL) {
1116 error = got_error_from_errno("strdup");
1117 goto done;
1119 if (strncmp(refname, "refs/heads/", 11) != 0)
1120 continue;
1122 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1123 if (error)
1124 goto done;
1126 if (strncmp(refname, "refs/heads/", 11) == 0)
1127 refname += 11;
1128 error = gotweb_escape_html(&escaped_refname, refname);
1129 if (error)
1130 goto done;
1132 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1133 "<div class='branches_age'>%s</div>\n"
1134 "<div class='branches_space'>&nbsp;</div>\n"
1135 "<div class='branch'>", age);
1136 if (r == -1)
1137 goto done;
1139 r = gotweb_link(c, &(struct gotweb_url){
1140 .action = SUMMARY,
1141 .index_page = -1,
1142 .page = -1,
1143 .path = qs->path,
1144 .headref = refname,
1145 }, "%s", escaped_refname);
1146 if (r == -1)
1147 goto done;
1149 if (fcgi_printf(c, "</div>\n" /* .branch */
1150 "<div class='navs_wrapper'>\n"
1151 "<div class='navs'>") == -1)
1152 goto done;
1154 r = gotweb_link(c, &(struct gotweb_url){
1155 .action = SUMMARY,
1156 .index_page = -1,
1157 .page = -1,
1158 .path = qs->path,
1159 .headref = refname,
1160 }, "summary");
1161 if (r == -1)
1162 goto done;
1164 if (fcgi_printf(c, " | ") == -1)
1165 goto done;
1167 r = gotweb_link(c, &(struct gotweb_url){
1168 .action = BRIEFS,
1169 .index_page = -1,
1170 .page = -1,
1171 .path = qs->path,
1172 .headref = refname,
1173 }, "commit briefs");
1174 if (r == -1)
1175 goto done;
1177 if (fcgi_printf(c, " | ") == -1)
1178 goto done;
1180 r = gotweb_link(c, &(struct gotweb_url){
1181 .action = COMMITS,
1182 .index_page = -1,
1183 .page = -1,
1184 .path = qs->path,
1185 .headref = refname,
1186 }, "commits");
1187 if (r == -1)
1188 goto done;
1190 r = fcgi_printf(c, "</div>\n" /* .navs */
1191 "</div>\n" /* .navs_wrapper */
1192 "<div class='dotted_line'></div>\n"
1193 "</div>\n"); /* .branches_wrapper */
1194 if (r == -1)
1195 goto done;
1197 free(age);
1198 age = NULL;
1199 free(escaped_refname);
1200 escaped_refname = NULL;
1202 fcgi_printf(c, "</div>\n"); /* #branches_content */
1203 done:
1204 free(age);
1205 free(escaped_refname);
1206 got_ref_list_free(&refs);
1207 return error;
1210 static const struct got_error *
1211 gotweb_render_tree(struct request *c)
1213 const struct got_error *error = NULL;
1214 struct transport *t = c->t;
1215 struct repo_commit *rc = NULL;
1216 char *age = NULL, *msg = NULL;
1217 int r;
1219 error = got_get_repo_commits(c, 1);
1220 if (error)
1221 return error;
1223 rc = TAILQ_FIRST(&t->repo_commits);
1225 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1226 if (error)
1227 goto done;
1229 error = gotweb_escape_html(&msg, rc->commit_msg);
1230 if (error)
1231 goto done;
1233 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1234 "<div id='tree_title'>Tree</div>\n"
1235 "</div>\n" /* #tree_title_wrapper */
1236 "<div id='tree_content'>\n"
1237 "<div id='tree_header_wrapper'>\n"
1238 "<div id='tree_header'>\n"
1239 "<div id='header_tree_title'>Tree:</div>\n"
1240 "<div id='header_tree'>%s</div>\n"
1241 "<div class='header_age_title'>Date:</div>\n"
1242 "<div class='header_age'>%s</div>\n"
1243 "<div id='header_commit_msg_title'>Message:</div>\n"
1244 "<div id='header_commit_msg'>%s</div>\n"
1245 "</div>\n" /* #tree_header */
1246 "</div>\n" /* #tree_header_wrapper */
1247 "<div class='dotted_line'></div>\n"
1248 "<div id='tree'>\n",
1249 rc->tree_id,
1250 age,
1251 msg);
1252 if (r == -1)
1253 goto done;
1255 error = got_output_repo_tree(c);
1256 if (error)
1257 goto done;
1259 fcgi_printf(c, "</div>\n"); /* #tree */
1260 fcgi_printf(c, "</div>\n"); /* #tree_content */
1261 done:
1262 free(age);
1263 free(msg);
1264 return error;
1267 static const struct got_error *
1268 gotweb_render_diff(struct request *c)
1270 const struct got_error *error = NULL;
1271 struct transport *t = c->t;
1272 struct repo_commit *rc = NULL;
1273 char *age = NULL, *author = NULL, *msg = NULL;
1274 int r;
1276 error = got_get_repo_commits(c, 1);
1277 if (error)
1278 return error;
1280 rc = TAILQ_FIRST(&t->repo_commits);
1282 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1283 if (error)
1284 goto done;
1285 error = gotweb_escape_html(&author, rc->author);
1286 if (error)
1287 goto done;
1288 error = gotweb_escape_html(&msg, rc->commit_msg);
1289 if (error)
1290 goto done;
1292 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1293 "<div id='diff_title'>Commit Diff</div>\n"
1294 "</div>\n" /* #diff_title_wrapper */
1295 "<div id='diff_content'>\n"
1296 "<div id='diff_header_wrapper'>\n"
1297 "<div id='diff_header'>\n"
1298 "<div id='header_diff_title'>Diff:</div>\n"
1299 "<div id='header_diff'>%s<br />%s</div>\n"
1300 "<div class='header_commit_title'>Commit:</div>\n"
1301 "<div class='header_commit'>%s</div>\n"
1302 "<div id='header_tree_title'>Tree:</div>\n"
1303 "<div id='header_tree'>%s</div>\n"
1304 "<div class='header_author_title'>Author:</div>\n"
1305 "<div class='header_author'>%s</div>\n"
1306 "<div class='header_age_title'>Date:</div>\n"
1307 "<div class='header_age'>%s</div>\n"
1308 "<div id='header_commit_msg_title'>Message:</div>\n"
1309 "<div id='header_commit_msg'>%s</div>\n"
1310 "</div>\n" /* #diff_header */
1311 "</div>\n" /* #diff_header_wrapper */
1312 "<div class='dotted_line'></div>\n"
1313 "<div id='diff'>\n",
1314 rc->parent_id, rc->commit_id,
1315 rc->commit_id,
1316 rc->tree_id,
1317 author,
1318 age,
1319 msg);
1320 if (r == -1)
1321 goto done;
1323 error = got_output_repo_diff(c);
1324 if (error)
1325 goto done;
1327 fcgi_printf(c, "</div>\n"); /* #diff */
1328 fcgi_printf(c, "</div>\n"); /* #diff_content */
1329 done:
1330 free(age);
1331 free(author);
1332 free(msg);
1333 return error;
1336 static const struct got_error *
1337 gotweb_render_summary(struct request *c)
1339 const struct got_error *error = NULL;
1340 struct transport *t = c->t;
1341 struct server *srv = c->srv;
1342 int r;
1344 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1345 goto done;
1347 if (srv->show_repo_description) {
1348 r = fcgi_printf(c,
1349 "<div id='description_title'>Description:</div>\n"
1350 "<div id='description'>%s</div>\n",
1351 t->repo_dir->description ? t->repo_dir->description : "");
1352 if (r == -1)
1353 goto done;
1356 if (srv->show_repo_owner) {
1357 r = fcgi_printf(c,
1358 "<div id='repo_owner_title'>Owner:</div>\n"
1359 "<div id='repo_owner'>%s</div>\n",
1360 t->repo_dir->owner ? t->repo_dir->owner : "");
1361 if (r == -1)
1362 goto done;
1365 if (srv->show_repo_age) {
1366 r = fcgi_printf(c,
1367 "<div id='last_change_title'>Last Change:</div>\n"
1368 "<div id='last_change'>%s</div>\n",
1369 t->repo_dir->age);
1370 if (r == -1)
1371 goto done;
1374 if (srv->show_repo_cloneurl) {
1375 r = fcgi_printf(c,
1376 "<div id='cloneurl_title'>Clone URL:</div>\n"
1377 "<div id='cloneurl'>%s</div>\n",
1378 t->repo_dir->url ? t->repo_dir->url : "");
1379 if (r == -1)
1380 goto done;
1383 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1384 if (r == -1)
1385 goto done;
1387 if (gotweb_render_briefs(c->tp) == -1)
1388 goto done;
1390 error = gotweb_render_tags(c);
1391 if (error) {
1392 log_warnx("%s: %s", __func__, error->msg);
1393 goto done;
1396 error = gotweb_render_branches(c);
1397 if (error)
1398 log_warnx("%s: %s", __func__, error->msg);
1399 done:
1400 return error;
1403 static const struct got_error *
1404 gotweb_render_tag(struct request *c)
1406 const struct got_error *error = NULL;
1407 struct repo_tag *rt = NULL;
1408 struct transport *t = c->t;
1409 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1411 error = got_get_repo_tags(c, 1);
1412 if (error)
1413 goto done;
1415 if (t->tag_count == 0) {
1416 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1417 "bad commit id");
1418 goto done;
1421 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1423 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1424 if (error)
1425 goto done;
1426 error = gotweb_escape_html(&author, rt->tagger);
1427 if (error)
1428 goto done;
1429 error = gotweb_escape_html(&msg, rt->commit_msg);
1430 if (error)
1431 goto done;
1433 tagname = rt->tag_name;
1434 if (strncmp(tagname, "refs/", 5) == 0)
1435 tagname += 5;
1436 error = gotweb_escape_html(&tagname, tagname);
1437 if (error)
1438 goto done;
1440 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1441 "<div id='tags_title'>Tag</div>\n"
1442 "</div>\n" /* #tags_title_wrapper */
1443 "<div id='tags_content'>\n"
1444 "<div id='tag_header_wrapper'>\n"
1445 "<div id='tag_header'>\n"
1446 "<div class='header_commit_title'>Commit:</div>\n"
1447 "<div class='header_commit'>%s"
1448 " <span class='refs_str'>(%s)</span></div>\n"
1449 "<div class='header_author_title'>Tagger:</div>\n"
1450 "<div class='header_author'>%s</div>\n"
1451 "<div class='header_age_title'>Date:</div>\n"
1452 "<div class='header_age'>%s</div>\n"
1453 "<div id='header_commit_msg_title'>Message:</div>\n"
1454 "<div id='header_commit_msg'>%s</div>\n"
1455 "</div>\n" /* #tag_header */
1456 "<div class='dotted_line'></div>\n"
1457 "<div id='tag_commit'>\n%s</div>"
1458 "</div>" /* #tag_header_wrapper */
1459 "</div>", /* #tags_content */
1460 rt->commit_id,
1461 tagname,
1462 author,
1463 age,
1464 msg,
1465 rt->tag_commit);
1467 done:
1468 free(age);
1469 free(author);
1470 free(msg);
1471 return error;
1474 static const struct got_error *
1475 gotweb_render_tags(struct request *c)
1477 const struct got_error *error = NULL;
1478 struct repo_tag *rt = NULL;
1479 struct server *srv = c->srv;
1480 struct transport *t = c->t;
1481 struct querystring *qs = t->qs;
1482 struct repo_dir *repo_dir = t->repo_dir;
1483 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1484 int r, commit_found = 0;
1486 if (qs->action == BRIEFS) {
1487 qs->action = TAGS;
1488 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1489 } else
1490 error = got_get_repo_tags(c, srv->max_commits_display);
1491 if (error)
1492 goto done;
1494 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1495 "<div id='tags_title'>Tags</div>\n"
1496 "</div>\n" /* #tags_title_wrapper */
1497 "<div id='tags_content'>\n");
1498 if (r == -1)
1499 goto done;
1501 if (t->tag_count == 0) {
1502 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1503 "This repository contains no tags");
1504 if (r == -1)
1505 goto done;
1508 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1509 if (commit_found == 0 && qs->commit != NULL) {
1510 if (strcmp(qs->commit, rt->commit_id) != 0)
1511 continue;
1512 else
1513 commit_found = 1;
1515 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1516 if (error)
1517 goto done;
1519 tagname = rt->tag_name;
1520 if (strncmp(tagname, "refs/tags/", 10) == 0)
1521 tagname += 10;
1522 error = gotweb_escape_html(&tagname, tagname);
1523 if (error)
1524 goto done;
1526 if (rt->tag_commit != NULL) {
1527 newline = strchr(rt->tag_commit, '\n');
1528 if (newline)
1529 *newline = '\0';
1530 error = gotweb_escape_html(&msg, rt->tag_commit);
1531 if (error)
1532 goto done;
1535 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1536 "<div class='tag'>%s</div>\n"
1537 "<div class='tag_log'>", age, tagname) == -1)
1538 goto done;
1540 r = gotweb_link(c, &(struct gotweb_url){
1541 .action = TAG,
1542 .index_page = -1,
1543 .page = -1,
1544 .path = repo_dir->name,
1545 .commit = rt->commit_id,
1546 }, "%s", msg ? msg : "");
1547 if (r == -1)
1548 goto done;
1550 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1551 "<div class='navs_wrapper'>\n"
1552 "<div class='navs'>") == -1)
1553 goto done;
1555 r = gotweb_link(c, &(struct gotweb_url){
1556 .action = TAG,
1557 .index_page = -1,
1558 .page = -1,
1559 .path = repo_dir->name,
1560 .commit = rt->commit_id,
1561 }, "tag");
1562 if (r == -1)
1563 goto done;
1565 if (fcgi_printf(c, " | ") == -1)
1566 goto done;
1568 r = gotweb_link(c, &(struct gotweb_url){
1569 .action = BRIEFS,
1570 .index_page = -1,
1571 .page = -1,
1572 .path = repo_dir->name,
1573 .commit = rt->commit_id,
1574 }, "commit briefs");
1575 if (r == -1)
1576 goto done;
1578 if (fcgi_printf(c, " | ") == -1)
1579 goto done;
1581 r = gotweb_link(c, &(struct gotweb_url){
1582 .action = COMMITS,
1583 .index_page = -1,
1584 .page = -1,
1585 .path = repo_dir->name,
1586 .commit = rt->commit_id,
1587 }, "commits");
1588 if (r == -1)
1589 goto done;
1591 r = fcgi_printf(c,
1592 "</div>\n" /* .navs */
1593 "</div>\n" /* .navs_wrapper */
1594 "<div class='dotted_line'></div>\n");
1595 if (r == -1)
1596 goto done;
1598 free(age);
1599 age = NULL;
1600 free(tagname);
1601 tagname = NULL;
1602 free(msg);
1603 msg = NULL;
1605 if (t->next_id || t->prev_id) {
1606 error = gotweb_render_navs(c);
1607 if (error)
1608 goto done;
1610 fcgi_printf(c, "</div>\n"); /* #tags_content */
1611 done:
1612 free(age);
1613 free(tagname);
1614 free(msg);
1615 return error;
1618 const struct got_error *
1619 gotweb_escape_html(char **escaped_html, const char *orig_html)
1621 const struct got_error *error = NULL;
1622 struct escape_pair {
1623 char c;
1624 const char *s;
1625 } esc[] = {
1626 { '>', "&gt;" },
1627 { '<', "&lt;" },
1628 { '&', "&amp;" },
1629 { '"', "&quot;" },
1630 { '\'', "&apos;" },
1631 { '\n', "<br />" },
1633 size_t orig_len, len;
1634 int i, j, x;
1636 orig_len = strlen(orig_html);
1637 len = orig_len;
1638 for (i = 0; i < orig_len; i++) {
1639 for (j = 0; j < nitems(esc); j++) {
1640 if (orig_html[i] != esc[j].c)
1641 continue;
1642 len += strlen(esc[j].s) - 1 /* escaped char */;
1646 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1647 if (*escaped_html == NULL)
1648 return got_error_from_errno("calloc");
1650 x = 0;
1651 for (i = 0; i < orig_len; i++) {
1652 int escaped = 0;
1653 for (j = 0; j < nitems(esc); j++) {
1654 if (orig_html[i] != esc[j].c)
1655 continue;
1657 if (strlcat(*escaped_html, esc[j].s, len + 1)
1658 >= len + 1) {
1659 error = got_error(GOT_ERR_NO_SPACE);
1660 goto done;
1662 x += strlen(esc[j].s);
1663 escaped = 1;
1664 break;
1666 if (!escaped) {
1667 (*escaped_html)[x] = orig_html[i];
1668 x++;
1671 done:
1672 if (error) {
1673 free(*escaped_html);
1674 *escaped_html = NULL;
1675 } else {
1676 (*escaped_html)[x] = '\0';
1679 return error;
1682 static inline int
1683 should_urlencode(int c)
1685 if (c <= ' ' || c >= 127)
1686 return 1;
1688 switch (c) {
1689 /* gen-delim */
1690 case ':':
1691 case '/':
1692 case '?':
1693 case '#':
1694 case '[':
1695 case ']':
1696 case '@':
1697 /* sub-delims */
1698 case '!':
1699 case '$':
1700 case '&':
1701 case '\'':
1702 case '(':
1703 case ')':
1704 case '*':
1705 case '+':
1706 case ',':
1707 case ';':
1708 case '=':
1709 return 1;
1710 default:
1711 return 0;
1715 static char *
1716 gotweb_urlencode(const char *str)
1718 const char *s;
1719 char *escaped;
1720 size_t i, len;
1721 int a, b;
1723 len = 0;
1724 for (s = str; *s; ++s) {
1725 len++;
1726 if (should_urlencode(*s))
1727 len += 2;
1730 escaped = calloc(1, len + 1);
1731 if (escaped == NULL)
1732 return NULL;
1734 i = 0;
1735 for (s = str; *s; ++s) {
1736 if (should_urlencode(*s)) {
1737 a = (*s & 0xF0) >> 4;
1738 b = (*s & 0x0F);
1740 escaped[i++] = '%';
1741 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1742 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1743 } else
1744 escaped[i++] = *s;
1747 return escaped;
1750 const char *
1751 gotweb_action_name(int action)
1753 switch (action) {
1754 case BLAME:
1755 return "blame";
1756 case BLOB:
1757 return "blob";
1758 case BRIEFS:
1759 return "briefs";
1760 case COMMITS:
1761 return "commits";
1762 case DIFF:
1763 return "diff";
1764 case ERR:
1765 return "err";
1766 case INDEX:
1767 return "index";
1768 case SUMMARY:
1769 return "summary";
1770 case TAG:
1771 return "tag";
1772 case TAGS:
1773 return "tags";
1774 case TREE:
1775 return "tree";
1776 default:
1777 return NULL;
1781 int
1782 gotweb_render_url(struct request *c, struct gotweb_url *url)
1784 const char *sep = "?", *action;
1785 char *tmp;
1786 int r;
1788 action = gotweb_action_name(url->action);
1789 if (action != NULL) {
1790 if (fcgi_printf(c, "?action=%s", action) == -1)
1791 return -1;
1792 sep = "&";
1795 if (url->commit) {
1796 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1797 return -1;
1798 sep = "&";
1801 if (url->previd) {
1802 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1803 return -1;
1804 sep = "&";
1807 if (url->prevset) {
1808 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1809 return -1;
1810 sep = "&";
1813 if (url->file) {
1814 tmp = gotweb_urlencode(url->file);
1815 if (tmp == NULL)
1816 return -1;
1817 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1818 free(tmp);
1819 if (r == -1)
1820 return -1;
1821 sep = "&";
1824 if (url->folder) {
1825 tmp = gotweb_urlencode(url->folder);
1826 if (tmp == NULL)
1827 return -1;
1828 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1829 free(tmp);
1830 if (r == -1)
1831 return -1;
1832 sep = "&";
1835 if (url->headref) {
1836 tmp = gotweb_urlencode(url->headref);
1837 if (tmp == NULL)
1838 return -1;
1839 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1840 free(tmp);
1841 if (r == -1)
1842 return -1;
1843 sep = "&";
1846 if (url->index_page != -1) {
1847 if (fcgi_printf(c, "%sindex_page=%d", sep,
1848 url->index_page) == -1)
1849 return -1;
1850 sep = "&";
1853 if (url->path) {
1854 tmp = gotweb_urlencode(url->path);
1855 if (tmp == NULL)
1856 return -1;
1857 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1858 free(tmp);
1859 if (r == -1)
1860 return -1;
1861 sep = "&";
1864 if (url->page != -1) {
1865 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1866 return -1;
1867 sep = "&";
1870 return 0;
1873 int
1874 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1876 va_list ap;
1877 int r;
1879 if (fcgi_printf(c, "<a href='") == -1)
1880 return -1;
1882 if (gotweb_render_url(c, url) == -1)
1883 return -1;
1885 if (fcgi_printf(c, "'>") == -1)
1886 return -1;
1888 va_start(ap, fmt);
1889 r = fcgi_vprintf(c, fmt, ap);
1890 va_end(ap);
1891 if (r == -1)
1892 return -1;
1894 if (fcgi_printf(c, "</a>"))
1895 return -1;
1896 return 0;
1899 static struct got_repository *
1900 find_cached_repo(struct server *srv, const char *path)
1902 int i;
1904 for (i = 0; i < srv->ncached_repos; i++) {
1905 if (strcmp(srv->cached_repos[i].path, path) == 0)
1906 return srv->cached_repos[i].repo;
1909 return NULL;
1912 static const struct got_error *
1913 cache_repo(struct got_repository **new, struct server *srv,
1914 struct repo_dir *repo_dir, struct socket *sock)
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo;
1918 struct cached_repo *cr;
1919 int evicted = 0;
1921 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1922 cr = &srv->cached_repos[srv->ncached_repos - 1];
1923 error = got_repo_close(cr->repo);
1924 memset(cr, 0, sizeof(*cr));
1925 srv->ncached_repos--;
1926 if (error)
1927 return error;
1928 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1929 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1930 cr = &srv->cached_repos[0];
1931 evicted = 1;
1932 } else {
1933 cr = &srv->cached_repos[srv->ncached_repos];
1936 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1937 if (error) {
1938 if (evicted) {
1939 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1940 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1942 return error;
1945 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1946 >= sizeof(cr->path)) {
1947 if (evicted) {
1948 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1949 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1951 return got_error(GOT_ERR_NO_SPACE);
1954 cr->repo = repo;
1955 srv->ncached_repos++;
1956 *new = repo;
1957 return NULL;
1960 static const struct got_error *
1961 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1963 const struct got_error *error = NULL;
1964 struct socket *sock = c->sock;
1965 struct server *srv = c->srv;
1966 struct transport *t = c->t;
1967 struct got_repository *repo = NULL;
1968 DIR *dt;
1969 char *dir_test;
1971 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1972 GOTWEB_GIT_DIR) == -1)
1973 return got_error_from_errno("asprintf");
1975 dt = opendir(dir_test);
1976 if (dt == NULL) {
1977 free(dir_test);
1978 } else {
1979 repo_dir->path = dir_test;
1980 dir_test = NULL;
1981 goto done;
1984 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1985 repo_dir->name) == -1)
1986 return got_error_from_errno("asprintf");
1988 dt = opendir(dir_test);
1989 if (dt == NULL) {
1990 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1991 goto err;
1992 } else {
1993 repo_dir->path = dir_test;
1994 dir_test = NULL;
1997 done:
1998 if (srv->respect_exportok &&
1999 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2000 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2001 goto err;
2004 repo = find_cached_repo(srv, repo_dir->path);
2005 if (repo == NULL) {
2006 error = cache_repo(&repo, srv, repo_dir, sock);
2007 if (error)
2008 goto err;
2010 t->repo = repo;
2011 error = gotweb_get_repo_description(&repo_dir->description, srv,
2012 repo_dir->path, dirfd(dt));
2013 if (error)
2014 goto err;
2015 error = got_get_repo_owner(&repo_dir->owner, c);
2016 if (error)
2017 goto err;
2018 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2019 if (error)
2020 goto err;
2021 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2022 dirfd(dt));
2023 err:
2024 free(dir_test);
2025 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2026 error = got_error_from_errno("closedir");
2027 return error;
2030 static const struct got_error *
2031 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2033 const struct got_error *error;
2035 *repo_dir = calloc(1, sizeof(**repo_dir));
2036 if (*repo_dir == NULL)
2037 return got_error_from_errno("calloc");
2039 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2040 error = got_error_from_errno("asprintf");
2041 free(*repo_dir);
2042 *repo_dir = NULL;
2043 return error;
2045 (*repo_dir)->owner = NULL;
2046 (*repo_dir)->description = NULL;
2047 (*repo_dir)->url = NULL;
2048 (*repo_dir)->age = NULL;
2049 (*repo_dir)->path = NULL;
2051 return NULL;
2054 static const struct got_error *
2055 gotweb_get_repo_description(char **description, struct server *srv,
2056 const char *dirpath, int dir)
2058 const struct got_error *error = NULL;
2059 struct stat sb;
2060 int fd = -1;
2061 off_t len;
2063 *description = NULL;
2064 if (srv->show_repo_description == 0)
2065 return NULL;
2067 fd = openat(dir, "description", O_RDONLY);
2068 if (fd == -1) {
2069 if (errno != ENOENT && errno != EACCES) {
2070 error = got_error_from_errno_fmt("openat %s/%s",
2071 dirpath, "description");
2073 goto done;
2076 if (fstat(fd, &sb) == -1) {
2077 error = got_error_from_errno_fmt("fstat %s/%s",
2078 dirpath, "description");
2079 goto done;
2082 len = sb.st_size;
2083 if (len > GOTWEBD_MAXDESCRSZ - 1)
2084 len = GOTWEBD_MAXDESCRSZ - 1;
2086 *description = calloc(len + 1, sizeof(**description));
2087 if (*description == NULL) {
2088 error = got_error_from_errno("calloc");
2089 goto done;
2092 if (read(fd, *description, len) == -1)
2093 error = got_error_from_errno("read");
2094 done:
2095 if (fd != -1 && close(fd) == -1 && error == NULL)
2096 error = got_error_from_errno("close");
2097 return error;
2100 static const struct got_error *
2101 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2102 int dir)
2104 const struct got_error *error = NULL;
2105 struct stat sb;
2106 int fd = -1;
2107 off_t len;
2109 *url = NULL;
2110 if (srv->show_repo_cloneurl == 0)
2111 return NULL;
2113 fd = openat(dir, "cloneurl", O_RDONLY);
2114 if (fd == -1) {
2115 if (errno != ENOENT && errno != EACCES) {
2116 error = got_error_from_errno_fmt("openat %s/%s",
2117 dirpath, "cloneurl");
2119 goto done;
2122 if (fstat(fd, &sb) == -1) {
2123 error = got_error_from_errno_fmt("fstat %s/%s",
2124 dirpath, "cloneurl");
2125 goto done;
2128 len = sb.st_size;
2129 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2130 len = GOTWEBD_MAXCLONEURLSZ - 1;
2132 *url = calloc(len + 1, sizeof(**url));
2133 if (*url == NULL) {
2134 error = got_error_from_errno("calloc");
2135 goto done;
2138 if (read(fd, *url, len) == -1)
2139 error = got_error_from_errno("read");
2140 done:
2141 if (fd != -1 && close(fd) == -1 && error == NULL)
2142 error = got_error_from_errno("close");
2143 return error;
2146 const struct got_error *
2147 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2149 struct tm tm;
2150 long long diff_time;
2151 const char *years = "years ago", *months = "months ago";
2152 const char *weeks = "weeks ago", *days = "days ago";
2153 const char *hours = "hours ago", *minutes = "minutes ago";
2154 const char *seconds = "seconds ago", *now = "right now";
2155 char *s;
2156 char datebuf[29];
2158 *repo_age = NULL;
2160 switch (ref_tm) {
2161 case TM_DIFF:
2162 diff_time = time(NULL) - committer_time;
2163 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2164 if (asprintf(repo_age, "%lld %s",
2165 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2166 return got_error_from_errno("asprintf");
2167 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2168 if (asprintf(repo_age, "%lld %s",
2169 (diff_time / 60 / 60 / 24 / (365 / 12)),
2170 months) == -1)
2171 return got_error_from_errno("asprintf");
2172 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2173 if (asprintf(repo_age, "%lld %s",
2174 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2175 return got_error_from_errno("asprintf");
2176 } else if (diff_time > 60 * 60 * 24 * 2) {
2177 if (asprintf(repo_age, "%lld %s",
2178 (diff_time / 60 / 60 / 24), days) == -1)
2179 return got_error_from_errno("asprintf");
2180 } else if (diff_time > 60 * 60 * 2) {
2181 if (asprintf(repo_age, "%lld %s",
2182 (diff_time / 60 / 60), hours) == -1)
2183 return got_error_from_errno("asprintf");
2184 } else if (diff_time > 60 * 2) {
2185 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2186 minutes) == -1)
2187 return got_error_from_errno("asprintf");
2188 } else if (diff_time > 2) {
2189 if (asprintf(repo_age, "%lld %s", diff_time,
2190 seconds) == -1)
2191 return got_error_from_errno("asprintf");
2192 } else {
2193 if (asprintf(repo_age, "%s", now) == -1)
2194 return got_error_from_errno("asprintf");
2196 break;
2197 case TM_LONG:
2198 if (gmtime_r(&committer_time, &tm) == NULL)
2199 return got_error_from_errno("gmtime_r");
2201 s = asctime_r(&tm, datebuf);
2202 if (s == NULL)
2203 return got_error_from_errno("asctime_r");
2205 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2206 return got_error_from_errno("asprintf");
2207 break;
2209 return NULL;