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)->index_page_str = NULL;
359 (*qs)->path = NULL;
361 return error;
364 static const struct got_error *
365 gotweb_parse_querystring(struct querystring **qs, char *qst)
367 const struct got_error *error = NULL;
368 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
369 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
371 if (qst == NULL)
372 return error;
374 tok1 = strdup(qst);
375 if (tok1 == NULL)
376 return got_error_from_errno2("%s: strdup", __func__);
378 tok1_pair = tok1;
379 tok1_end = tok1;
381 while (tok1_pair != NULL) {
382 strsep(&tok1_end, "&");
384 tok2 = strdup(tok1_pair);
385 if (tok2 == NULL) {
386 free(tok1);
387 return got_error_from_errno2("%s: strdup", __func__);
390 tok2_pair = tok2;
391 tok2_end = tok2;
393 while (tok2_pair != NULL) {
394 strsep(&tok2_end, "=");
395 if (tok2_end) {
396 error = gotweb_assign_querystring(qs, tok2_pair,
397 tok2_end);
398 if (error)
399 goto err;
401 tok2_pair = tok2_end;
403 free(tok2);
404 tok1_pair = tok1_end;
406 free(tok1);
407 return error;
408 err:
409 free(tok2);
410 free(tok1);
411 return error;
414 /*
415 * Adapted from usr.sbin/httpd/httpd.c url_decode.
416 */
417 static const struct got_error *
418 gotweb_urldecode(char *url)
420 char *p, *q;
421 char hex[3];
422 unsigned long x;
424 hex[2] = '\0';
425 p = q = url;
427 while (*p != '\0') {
428 switch (*p) {
429 case '%':
430 /* Encoding character is followed by two hex chars */
431 if (!isxdigit((unsigned char)p[1]) ||
432 !isxdigit((unsigned char)p[2]) ||
433 (p[1] == '0' && p[2] == '0'))
434 return got_error(GOT_ERR_BAD_QUERYSTRING);
436 hex[0] = p[1];
437 hex[1] = p[2];
439 /*
440 * We don't have to validate "hex" because it is
441 * guaranteed to include two hex chars followed by nul.
442 */
443 x = strtoul(hex, NULL, 16);
444 *q = (char)x;
445 p += 2;
446 break;
447 default:
448 *q = *p;
449 break;
451 p++;
452 q++;
454 *q = '\0';
456 return NULL;
459 static const struct got_error *
460 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
462 const struct got_error *error = NULL;
463 const char *errstr;
464 int a_cnt, el_cnt;
466 error = gotweb_urldecode(value);
467 if (error)
468 return error;
470 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
471 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
472 continue;
474 switch (querystring_keys[el_cnt].element) {
475 case ACTION:
476 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
477 if (strcmp(value, action_keys[a_cnt].name) != 0)
478 continue;
479 else if (strcmp(value,
480 action_keys[a_cnt].name) == 0){
481 (*qs)->action =
482 action_keys[a_cnt].action;
483 goto qa_found;
486 (*qs)->action = ERR;
487 qa_found:
488 break;
489 case COMMIT:
490 (*qs)->commit = strdup(value);
491 if ((*qs)->commit == NULL) {
492 error = got_error_from_errno2("%s: strdup",
493 __func__);
494 goto done;
496 break;
497 case RFILE:
498 (*qs)->file = strdup(value);
499 if ((*qs)->file == NULL) {
500 error = got_error_from_errno2("%s: strdup",
501 __func__);
502 goto done;
504 break;
505 case FOLDER:
506 (*qs)->folder = strdup(value);
507 if ((*qs)->folder == NULL) {
508 error = got_error_from_errno2("%s: strdup",
509 __func__);
510 goto done;
512 break;
513 case HEADREF:
514 free((*qs)->headref);
515 (*qs)->headref = strdup(value);
516 if ((*qs)->headref == NULL) {
517 error = got_error_from_errno2("%s: strdup",
518 __func__);
519 goto done;
521 break;
522 case INDEX_PAGE:
523 if (strlen(value) == 0)
524 break;
525 (*qs)->index_page_str = strdup(value);
526 if ((*qs)->index_page_str == NULL) {
527 error = got_error_from_errno2("%s: strdup",
528 __func__);
529 goto done;
531 (*qs)->index_page = strtonum(value, INT64_MIN,
532 INT64_MAX, &errstr);
533 if (errstr) {
534 error = got_error_from_errno3("%s: strtonum %s",
535 __func__, errstr);
536 goto done;
538 if ((*qs)->index_page < 0) {
539 (*qs)->index_page = 0;
540 sprintf((*qs)->index_page_str, "%d", 0);
542 break;
543 case PATH:
544 (*qs)->path = strdup(value);
545 if ((*qs)->path == NULL) {
546 error = got_error_from_errno2("%s: strdup",
547 __func__);
548 goto done;
550 break;
551 case PAGE:
552 if (strlen(value) == 0)
553 break;
554 (*qs)->page_str = strdup(value);
555 if ((*qs)->page_str == NULL) {
556 error = got_error_from_errno2("%s: strdup",
557 __func__);
558 goto done;
560 (*qs)->page = strtonum(value, INT64_MIN,
561 INT64_MAX, &errstr);
562 if (errstr) {
563 error = got_error_from_errno3("%s: strtonum %s",
564 __func__, errstr);
565 goto done;
567 if ((*qs)->page < 0) {
568 (*qs)->page = 0;
569 sprintf((*qs)->page_str, "%d", 0);
571 break;
572 default:
573 break;
576 done:
577 return error;
580 void
581 gotweb_free_repo_tag(struct repo_tag *rt)
583 if (rt != NULL) {
584 free(rt->commit_id);
585 free(rt->tag_name);
586 free(rt->tag_commit);
587 free(rt->commit_msg);
588 free(rt->tagger);
590 free(rt);
593 void
594 gotweb_free_repo_commit(struct repo_commit *rc)
596 if (rc != NULL) {
597 free(rc->path);
598 free(rc->refs_str);
599 free(rc->commit_id);
600 free(rc->parent_id);
601 free(rc->tree_id);
602 free(rc->author);
603 free(rc->committer);
604 free(rc->commit_msg);
606 free(rc);
609 static void
610 gotweb_free_querystring(struct querystring *qs)
612 if (qs != NULL) {
613 free(qs->commit);
614 free(qs->file);
615 free(qs->folder);
616 free(qs->headref);
617 free(qs->index_page_str);
618 free(qs->path);
619 free(qs->page_str);
621 free(qs);
624 static void
625 gotweb_free_repo_dir(struct repo_dir *repo_dir)
627 if (repo_dir != NULL) {
628 free(repo_dir->name);
629 free(repo_dir->owner);
630 free(repo_dir->description);
631 free(repo_dir->url);
632 free(repo_dir->age);
633 free(repo_dir->path);
635 free(repo_dir);
638 void
639 gotweb_free_transport(struct transport *t)
641 struct repo_commit *rc = NULL, *trc = NULL;
642 struct repo_tag *rt = NULL, *trt = NULL;
644 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
645 TAILQ_REMOVE(&t->repo_commits, rc, entry);
646 gotweb_free_repo_commit(rc);
648 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
649 TAILQ_REMOVE(&t->repo_tags, rt, entry);
650 gotweb_free_repo_tag(rt);
652 gotweb_free_repo_dir(t->repo_dir);
653 gotweb_free_querystring(t->qs);
654 free(t->next_id);
655 free(t->prev_id);
656 free(t);
659 const struct got_error *
660 gotweb_render_content_type(struct request *c, const uint8_t *type)
662 const char *csp = "default-src 'self'; script-src 'none'; "
663 "object-src 'none';";
665 fcgi_printf(c,
666 "Content-Security-Policy: %s\r\n"
667 "Content-Type: %s\r\n\r\n",
668 csp, type);
669 return NULL;
672 const struct got_error *
673 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
674 char *file)
676 fcgi_printf(c, "Content-type: %s\r\n"
677 "Content-disposition: attachment; filename=%s\r\n\r\n",
678 type, file);
679 return NULL;
682 const struct got_error *
683 gotweb_render_navs(struct request *c)
685 const struct got_error *error = NULL;
686 struct transport *t = c->t;
687 struct querystring *qs = t->qs;
688 struct server *srv = c->srv;
689 int r;
691 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
692 if (r == -1)
693 goto done;
695 switch(qs->action) {
696 case INDEX:
697 if (qs->index_page > 0) {
698 struct gotweb_url url = {
699 .action = -1,
700 .index_page = qs->index_page - 1,
701 .page = -1,
702 };
704 r = gotweb_link(c, &url, "Previous");
706 break;
707 case BRIEFS:
708 if (t->prev_id && qs->commit != NULL &&
709 strcmp(qs->commit, t->prev_id) != 0) {
710 struct gotweb_url url = {
711 .action = BRIEFS,
712 .index_page = -1,
713 .page = qs->page - 1,
714 .path = qs->path,
715 .commit = t->prev_id,
716 .headref = qs->headref,
717 };
719 r = gotweb_link(c, &url, "Previous");
721 break;
722 case COMMITS:
723 if (t->prev_id && qs->commit != NULL &&
724 strcmp(qs->commit, t->prev_id) != 0) {
725 struct gotweb_url url = {
726 .action = COMMIT,
727 .index_page = -1,
728 .page = qs->page - 1,
729 .path = qs->path,
730 .commit = t->prev_id,
731 .headref = qs->headref,
732 .folder = qs->folder,
733 .file = qs->file,
734 };
736 r = gotweb_link(c, &url, "Previous");
738 break;
739 case TAGS:
740 if (t->prev_id && qs->commit != NULL &&
741 strcmp(qs->commit, t->prev_id) != 0) {
742 struct gotweb_url url = {
743 .action = TAGS,
744 .index_page = -1,
745 .page = qs->page - 1,
746 .path = qs->path,
747 .commit = t->prev_id,
748 .headref = qs->headref,
749 };
751 r = gotweb_link(c, &url, "Previous");
753 break;
756 if (r == -1)
757 goto done;
759 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
760 "<div id='nav_next'>");
761 if (r == -1)
762 goto done;
764 switch(qs->action) {
765 case INDEX:
766 if (t->next_disp == srv->max_repos_display &&
767 t->repos_total != (qs->index_page + 1) *
768 srv->max_repos_display) {
769 struct gotweb_url url = {
770 .action = -1,
771 .index_page = qs->index_page + 1,
772 .page = -1,
773 };
775 r = gotweb_link(c, &url, "Next");
777 break;
778 case BRIEFS:
779 if (t->next_id) {
780 struct gotweb_url url = {
781 .action = BRIEFS,
782 .index_page = -1,
783 .page = qs->page + 1,
784 .path = qs->path,
785 .commit = t->next_id,
786 .headref = qs->headref,
787 };
789 r = gotweb_link(c, &url, "Next");
791 break;
792 case COMMITS:
793 if (t->next_id) {
794 struct gotweb_url url = {
795 .action = COMMIT,
796 .index_page = -1,
797 .page = qs->page + 1,
798 .path = qs->path,
799 .commit = t->next_id,
800 .headref = qs->headref,
801 .folder = qs->folder,
802 .file = qs->file,
803 };
805 r = gotweb_link(c, &url, "Next");
807 break;
808 case TAGS:
809 if (t->next_id) {
810 struct gotweb_url url = {
811 .action = TAGS,
812 .index_page = -1,
813 .page = qs->page + 1,
814 .path = qs->path,
815 .commit = t->next_id,
816 .headref = qs->headref,
817 };
819 r = gotweb_link(c, &url, "Next");
821 break;
823 if (r == -1)
824 goto done;
826 fcgi_printf(c, "</div>\n"); /* #nav_next */
827 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
828 done:
829 free(t->next_id);
830 t->next_id = NULL;
831 free(t->prev_id);
832 t->prev_id = NULL;
833 return error;
836 static const struct got_error *
837 gotweb_render_index(struct request *c)
839 const struct got_error *error = NULL;
840 struct server *srv = c->srv;
841 struct transport *t = c->t;
842 struct querystring *qs = t->qs;
843 struct repo_dir *repo_dir = NULL;
844 DIR *d;
845 struct dirent **sd_dent = NULL;
846 unsigned int d_cnt, d_i, d_disp = 0;
847 unsigned int d_skipped = 0;
848 int type;
850 d = opendir(srv->repos_path);
851 if (d == NULL) {
852 error = got_error_from_errno2("opendir", srv->repos_path);
853 return error;
856 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
857 if (d_cnt == -1) {
858 sd_dent = NULL;
859 error = got_error_from_errno2("scandir", srv->repos_path);
860 goto done;
863 if (gotweb_render_repo_table_hdr(c->tp) == -1)
864 goto done;
866 for (d_i = 0; d_i < d_cnt; d_i++) {
867 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
868 break;
870 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
871 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
872 d_skipped++;
873 continue;
876 error = got_path_dirent_type(&type, srv->repos_path,
877 sd_dent[d_i]);
878 if (error)
879 goto done;
880 if (type != DT_DIR) {
881 d_skipped++;
882 continue;
885 if (qs->index_page > 0 && (qs->index_page *
886 srv->max_repos_display) > t->prev_disp) {
887 t->prev_disp++;
888 continue;
891 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
892 if (error)
893 goto done;
895 error = gotweb_load_got_path(c, repo_dir);
896 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
897 error = NULL;
898 gotweb_free_repo_dir(repo_dir);
899 repo_dir = NULL;
900 d_skipped++;
901 continue;
903 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
904 goto done;
906 d_disp++;
907 t->prev_disp++;
909 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
910 goto done;
912 gotweb_free_repo_dir(repo_dir);
913 repo_dir = NULL;
914 t->next_disp++;
915 if (d_disp == srv->max_repos_display)
916 break;
918 t->repos_total = d_cnt - d_skipped;
920 if (srv->max_repos_display == 0)
921 goto done;
922 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
923 goto done;
924 if (t->repos_total <= srv->max_repos ||
925 t->repos_total <= srv->max_repos_display)
926 goto done;
928 error = gotweb_render_navs(c);
929 if (error)
930 goto done;
931 done:
932 if (sd_dent) {
933 for (d_i = 0; d_i < d_cnt; d_i++)
934 free(sd_dent[d_i]);
935 free(sd_dent);
937 if (d != NULL && closedir(d) == EOF && error == NULL)
938 error = got_error_from_errno("closedir");
939 return error;
942 static const struct got_error *
943 gotweb_render_blame(struct request *c)
945 const struct got_error *error = NULL;
946 struct transport *t = c->t;
947 struct repo_commit *rc = NULL;
948 char *age = NULL, *msg = NULL;
949 int r;
951 error = got_get_repo_commits(c, 1);
952 if (error)
953 return error;
955 rc = TAILQ_FIRST(&t->repo_commits);
957 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
958 if (error)
959 goto done;
960 error = gotweb_escape_html(&msg, rc->commit_msg);
961 if (error)
962 goto done;
964 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
965 "<div id='blame_title'>Blame</div>\n"
966 "</div>\n" /* #blame_title_wrapper */
967 "<div id='blame_content'>\n"
968 "<div id='blame_header_wrapper'>\n"
969 "<div id='blame_header'>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "<div id='header_commit_msg_title'>Message:</div>\n"
973 "<div id='header_commit_msg'>%s</div>\n"
974 "</div>\n" /* #blame_header */
975 "</div>\n" /* #blame_header_wrapper */
976 "<div class='dotted_line'></div>\n"
977 "<div id='blame'>\n",
978 age,
979 msg);
980 if (r == -1)
981 goto done;
983 error = got_output_file_blame(c);
984 if (error)
985 goto done;
987 fcgi_printf(c, "</div>\n" /* #blame */
988 "</div>\n"); /* #blame_content */
989 done:
990 free(age);
991 free(msg);
992 return error;
995 static const struct got_error *
996 gotweb_render_commits(struct request *c)
998 const struct got_error *error = NULL;
999 struct repo_commit *rc = NULL;
1000 struct server *srv = c->srv;
1001 struct transport *t = c->t;
1002 struct repo_dir *repo_dir = t->repo_dir;
1003 char *age = NULL, *author = NULL, *msg = NULL;
1004 int r;
1006 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1007 "<div class='commits_title'>Commits</div>\n"
1008 "</div>\n" /* .commits_title_wrapper */
1009 "<div class='commits_content'>\n");
1010 if (r == -1)
1011 goto done;
1013 error = got_get_repo_commits(c, srv->max_commits_display);
1014 if (error)
1015 goto done;
1017 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1018 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1019 if (error)
1020 goto done;
1021 error = gotweb_escape_html(&author, rc->author);
1022 if (error)
1023 goto done;
1024 error = gotweb_escape_html(&msg, rc->commit_msg);
1025 if (error)
1026 goto done;
1028 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1029 "<div class='commits_header'>\n"
1030 "<div class='header_commit_title'>Commit:</div>\n"
1031 "<div class='header_commit'>%s</div>\n"
1032 "<div class='header_author_title'>Author:</div>\n"
1033 "<div class='header_author'>%s</div>\n"
1034 "<div class='header_age_title'>Date:</div>\n"
1035 "<div class='header_age'>%s</div>\n"
1036 "</div>\n" /* .commits_header */
1037 "</div>\n" /* .commits_header_wrapper */
1038 "<div class='dotted_line'></div>\n"
1039 "<div class='commit'>\n%s</div>\n",
1040 rc->commit_id,
1041 author,
1042 age,
1043 msg);
1044 if (r == -1)
1045 goto done;
1047 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1048 "<div class='navs'>") == -1)
1049 goto done;
1051 r = gotweb_link(c, &(struct gotweb_url){
1052 .action = DIFF,
1053 .index_page = -1,
1054 .page = -1,
1055 .path = repo_dir->name,
1056 .commit = rc->commit_id,
1057 }, "diff");
1058 if (r == -1)
1059 goto done;
1061 if (fcgi_printf(c, " | ") == -1)
1062 goto done;
1064 r = gotweb_link(c, &(struct gotweb_url){
1065 .action = TREE,
1066 .index_page = -1,
1067 .page = -1,
1068 .path = repo_dir->name,
1069 .commit = rc->commit_id,
1070 }, "tree");
1071 if (r == -1)
1072 goto done;
1074 if (fcgi_printf(c, "</div>\n" /* .navs */
1075 "</div>\n" /* .navs_wrapper */
1076 "<div class='dotted_line'></div>\n") == -1)
1077 goto done;
1079 free(age);
1080 age = NULL;
1081 free(author);
1082 author = NULL;
1083 free(msg);
1084 msg = NULL;
1087 if (t->next_id || t->prev_id) {
1088 error = gotweb_render_navs(c);
1089 if (error)
1090 goto done;
1092 fcgi_printf(c, "</div>\n"); /* .commits_content */
1093 done:
1094 free(age);
1095 free(author);
1096 free(msg);
1097 return error;
1100 static const struct got_error *
1101 gotweb_render_branches(struct request *c)
1103 const struct got_error *error = NULL;
1104 struct got_reflist_head refs;
1105 struct got_reflist_entry *re;
1106 struct transport *t = c->t;
1107 struct querystring *qs = t->qs;
1108 struct got_repository *repo = t->repo;
1109 char *escaped_refname = NULL;
1110 char *age = NULL;
1111 int r;
1113 TAILQ_INIT(&refs);
1115 error = got_ref_list(&refs, repo, "refs/heads",
1116 got_ref_cmp_by_name, NULL);
1117 if (error)
1118 goto done;
1120 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1121 "<div id='branches_title'>Branches</div>\n"
1122 "</div>\n" /* #branches_title_wrapper */
1123 "<div id='branches_content'>\n");
1124 if (r == -1)
1125 goto done;
1127 TAILQ_FOREACH(re, &refs, entry) {
1128 const char *refname = NULL;
1130 if (got_ref_is_symbolic(re->ref))
1131 continue;
1133 refname = got_ref_get_name(re->ref);
1134 if (refname == NULL) {
1135 error = got_error_from_errno("strdup");
1136 goto done;
1138 if (strncmp(refname, "refs/heads/", 11) != 0)
1139 continue;
1141 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1142 if (error)
1143 goto done;
1145 if (strncmp(refname, "refs/heads/", 11) == 0)
1146 refname += 11;
1147 error = gotweb_escape_html(&escaped_refname, refname);
1148 if (error)
1149 goto done;
1151 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1152 "<div class='branches_age'>%s</div>\n"
1153 "<div class='branches_space'>&nbsp;</div>\n"
1154 "<div class='branch'>", age);
1155 if (r == -1)
1156 goto done;
1158 r = gotweb_link(c, &(struct gotweb_url){
1159 .action = SUMMARY,
1160 .index_page = -1,
1161 .page = -1,
1162 .path = qs->path,
1163 .headref = refname,
1164 }, "%s", escaped_refname);
1165 if (r == -1)
1166 goto done;
1168 if (fcgi_printf(c, "</div>\n" /* .branch */
1169 "<div class='navs_wrapper'>\n"
1170 "<div class='navs'>") == -1)
1171 goto done;
1173 r = gotweb_link(c, &(struct gotweb_url){
1174 .action = SUMMARY,
1175 .index_page = -1,
1176 .page = -1,
1177 .path = qs->path,
1178 .headref = refname,
1179 }, "summary");
1180 if (r == -1)
1181 goto done;
1183 if (fcgi_printf(c, " | ") == -1)
1184 goto done;
1186 r = gotweb_link(c, &(struct gotweb_url){
1187 .action = BRIEFS,
1188 .index_page = -1,
1189 .page = -1,
1190 .path = qs->path,
1191 .headref = refname,
1192 }, "commit briefs");
1193 if (r == -1)
1194 goto done;
1196 if (fcgi_printf(c, " | ") == -1)
1197 goto done;
1199 r = gotweb_link(c, &(struct gotweb_url){
1200 .action = COMMITS,
1201 .index_page = -1,
1202 .page = -1,
1203 .path = qs->path,
1204 .headref = refname,
1205 }, "commits");
1206 if (r == -1)
1207 goto done;
1209 r = fcgi_printf(c, "</div>\n" /* .navs */
1210 "</div>\n" /* .navs_wrapper */
1211 "<div class='dotted_line'></div>\n"
1212 "</div>\n"); /* .branches_wrapper */
1213 if (r == -1)
1214 goto done;
1216 free(age);
1217 age = NULL;
1218 free(escaped_refname);
1219 escaped_refname = NULL;
1221 fcgi_printf(c, "</div>\n"); /* #branches_content */
1222 done:
1223 free(age);
1224 free(escaped_refname);
1225 got_ref_list_free(&refs);
1226 return error;
1229 static const struct got_error *
1230 gotweb_render_tree(struct request *c)
1232 const struct got_error *error = NULL;
1233 struct transport *t = c->t;
1234 struct repo_commit *rc = NULL;
1235 char *age = NULL, *msg = NULL;
1236 int r;
1238 error = got_get_repo_commits(c, 1);
1239 if (error)
1240 return error;
1242 rc = TAILQ_FIRST(&t->repo_commits);
1244 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1245 if (error)
1246 goto done;
1248 error = gotweb_escape_html(&msg, rc->commit_msg);
1249 if (error)
1250 goto done;
1252 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1253 "<div id='tree_title'>Tree</div>\n"
1254 "</div>\n" /* #tree_title_wrapper */
1255 "<div id='tree_content'>\n"
1256 "<div id='tree_header_wrapper'>\n"
1257 "<div id='tree_header'>\n"
1258 "<div id='header_tree_title'>Tree:</div>\n"
1259 "<div id='header_tree'>%s</div>\n"
1260 "<div class='header_age_title'>Date:</div>\n"
1261 "<div class='header_age'>%s</div>\n"
1262 "<div id='header_commit_msg_title'>Message:</div>\n"
1263 "<div id='header_commit_msg'>%s</div>\n"
1264 "</div>\n" /* #tree_header */
1265 "</div>\n" /* #tree_header_wrapper */
1266 "<div class='dotted_line'></div>\n"
1267 "<div id='tree'>\n",
1268 rc->tree_id,
1269 age,
1270 msg);
1271 if (r == -1)
1272 goto done;
1274 error = got_output_repo_tree(c);
1275 if (error)
1276 goto done;
1278 fcgi_printf(c, "</div>\n"); /* #tree */
1279 fcgi_printf(c, "</div>\n"); /* #tree_content */
1280 done:
1281 free(age);
1282 free(msg);
1283 return error;
1286 static const struct got_error *
1287 gotweb_render_diff(struct request *c)
1289 const struct got_error *error = NULL;
1290 struct transport *t = c->t;
1291 struct repo_commit *rc = NULL;
1292 char *age = NULL, *author = NULL, *msg = NULL;
1293 int r;
1295 error = got_get_repo_commits(c, 1);
1296 if (error)
1297 return error;
1299 rc = TAILQ_FIRST(&t->repo_commits);
1301 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1302 if (error)
1303 goto done;
1304 error = gotweb_escape_html(&author, rc->author);
1305 if (error)
1306 goto done;
1307 error = gotweb_escape_html(&msg, rc->commit_msg);
1308 if (error)
1309 goto done;
1311 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1312 "<div id='diff_title'>Commit Diff</div>\n"
1313 "</div>\n" /* #diff_title_wrapper */
1314 "<div id='diff_content'>\n"
1315 "<div id='diff_header_wrapper'>\n"
1316 "<div id='diff_header'>\n"
1317 "<div id='header_diff_title'>Diff:</div>\n"
1318 "<div id='header_diff'>%s<br />%s</div>\n"
1319 "<div class='header_commit_title'>Commit:</div>\n"
1320 "<div class='header_commit'>%s</div>\n"
1321 "<div id='header_tree_title'>Tree:</div>\n"
1322 "<div id='header_tree'>%s</div>\n"
1323 "<div class='header_author_title'>Author:</div>\n"
1324 "<div class='header_author'>%s</div>\n"
1325 "<div class='header_age_title'>Date:</div>\n"
1326 "<div class='header_age'>%s</div>\n"
1327 "<div id='header_commit_msg_title'>Message:</div>\n"
1328 "<div id='header_commit_msg'>%s</div>\n"
1329 "</div>\n" /* #diff_header */
1330 "</div>\n" /* #diff_header_wrapper */
1331 "<div class='dotted_line'></div>\n"
1332 "<div id='diff'>\n",
1333 rc->parent_id, rc->commit_id,
1334 rc->commit_id,
1335 rc->tree_id,
1336 author,
1337 age,
1338 msg);
1339 if (r == -1)
1340 goto done;
1342 error = got_output_repo_diff(c);
1343 if (error)
1344 goto done;
1346 fcgi_printf(c, "</div>\n"); /* #diff */
1347 fcgi_printf(c, "</div>\n"); /* #diff_content */
1348 done:
1349 free(age);
1350 free(author);
1351 free(msg);
1352 return error;
1355 static const struct got_error *
1356 gotweb_render_summary(struct request *c)
1358 const struct got_error *error = NULL;
1359 struct transport *t = c->t;
1360 struct server *srv = c->srv;
1361 int r;
1363 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1364 goto done;
1366 if (srv->show_repo_description) {
1367 r = fcgi_printf(c,
1368 "<div id='description_title'>Description:</div>\n"
1369 "<div id='description'>%s</div>\n",
1370 t->repo_dir->description ? t->repo_dir->description : "");
1371 if (r == -1)
1372 goto done;
1375 if (srv->show_repo_owner) {
1376 r = fcgi_printf(c,
1377 "<div id='repo_owner_title'>Owner:</div>\n"
1378 "<div id='repo_owner'>%s</div>\n",
1379 t->repo_dir->owner ? t->repo_dir->owner : "");
1380 if (r == -1)
1381 goto done;
1384 if (srv->show_repo_age) {
1385 r = fcgi_printf(c,
1386 "<div id='last_change_title'>Last Change:</div>\n"
1387 "<div id='last_change'>%s</div>\n",
1388 t->repo_dir->age);
1389 if (r == -1)
1390 goto done;
1393 if (srv->show_repo_cloneurl) {
1394 r = fcgi_printf(c,
1395 "<div id='cloneurl_title'>Clone URL:</div>\n"
1396 "<div id='cloneurl'>%s</div>\n",
1397 t->repo_dir->url ? t->repo_dir->url : "");
1398 if (r == -1)
1399 goto done;
1402 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1403 if (r == -1)
1404 goto done;
1406 if (gotweb_render_briefs(c->tp) == -1)
1407 goto done;
1409 error = gotweb_render_tags(c);
1410 if (error) {
1411 log_warnx("%s: %s", __func__, error->msg);
1412 goto done;
1415 error = gotweb_render_branches(c);
1416 if (error)
1417 log_warnx("%s: %s", __func__, error->msg);
1418 done:
1419 return error;
1422 static const struct got_error *
1423 gotweb_render_tag(struct request *c)
1425 const struct got_error *error = NULL;
1426 struct repo_tag *rt = NULL;
1427 struct transport *t = c->t;
1428 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1430 error = got_get_repo_tags(c, 1);
1431 if (error)
1432 goto done;
1434 if (t->tag_count == 0) {
1435 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1436 "bad commit id");
1437 goto done;
1440 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1442 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1443 if (error)
1444 goto done;
1445 error = gotweb_escape_html(&author, rt->tagger);
1446 if (error)
1447 goto done;
1448 error = gotweb_escape_html(&msg, rt->commit_msg);
1449 if (error)
1450 goto done;
1452 tagname = rt->tag_name;
1453 if (strncmp(tagname, "refs/", 5) == 0)
1454 tagname += 5;
1455 error = gotweb_escape_html(&tagname, tagname);
1456 if (error)
1457 goto done;
1459 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1460 "<div id='tags_title'>Tag</div>\n"
1461 "</div>\n" /* #tags_title_wrapper */
1462 "<div id='tags_content'>\n"
1463 "<div id='tag_header_wrapper'>\n"
1464 "<div id='tag_header'>\n"
1465 "<div class='header_commit_title'>Commit:</div>\n"
1466 "<div class='header_commit'>%s"
1467 " <span class='refs_str'>(%s)</span></div>\n"
1468 "<div class='header_author_title'>Tagger:</div>\n"
1469 "<div class='header_author'>%s</div>\n"
1470 "<div class='header_age_title'>Date:</div>\n"
1471 "<div class='header_age'>%s</div>\n"
1472 "<div id='header_commit_msg_title'>Message:</div>\n"
1473 "<div id='header_commit_msg'>%s</div>\n"
1474 "</div>\n" /* #tag_header */
1475 "<div class='dotted_line'></div>\n"
1476 "<div id='tag_commit'>\n%s</div>"
1477 "</div>" /* #tag_header_wrapper */
1478 "</div>", /* #tags_content */
1479 rt->commit_id,
1480 tagname,
1481 author,
1482 age,
1483 msg,
1484 rt->tag_commit);
1486 done:
1487 free(age);
1488 free(author);
1489 free(msg);
1490 return error;
1493 static const struct got_error *
1494 gotweb_render_tags(struct request *c)
1496 const struct got_error *error = NULL;
1497 struct repo_tag *rt = NULL;
1498 struct server *srv = c->srv;
1499 struct transport *t = c->t;
1500 struct querystring *qs = t->qs;
1501 struct repo_dir *repo_dir = t->repo_dir;
1502 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1503 int r, commit_found = 0;
1505 if (qs->action == BRIEFS) {
1506 qs->action = TAGS;
1507 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1508 } else
1509 error = got_get_repo_tags(c, srv->max_commits_display);
1510 if (error)
1511 goto done;
1513 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1514 "<div id='tags_title'>Tags</div>\n"
1515 "</div>\n" /* #tags_title_wrapper */
1516 "<div id='tags_content'>\n");
1517 if (r == -1)
1518 goto done;
1520 if (t->tag_count == 0) {
1521 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1522 "This repository contains no tags");
1523 if (r == -1)
1524 goto done;
1527 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1528 if (commit_found == 0 && qs->commit != NULL) {
1529 if (strcmp(qs->commit, rt->commit_id) != 0)
1530 continue;
1531 else
1532 commit_found = 1;
1534 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1535 if (error)
1536 goto done;
1538 tagname = rt->tag_name;
1539 if (strncmp(tagname, "refs/tags/", 10) == 0)
1540 tagname += 10;
1541 error = gotweb_escape_html(&tagname, tagname);
1542 if (error)
1543 goto done;
1545 if (rt->tag_commit != NULL) {
1546 newline = strchr(rt->tag_commit, '\n');
1547 if (newline)
1548 *newline = '\0';
1549 error = gotweb_escape_html(&msg, rt->tag_commit);
1550 if (error)
1551 goto done;
1554 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1555 "<div class='tag'>%s</div>\n"
1556 "<div class='tag_log'>", age, tagname) == -1)
1557 goto done;
1559 r = gotweb_link(c, &(struct gotweb_url){
1560 .action = TAG,
1561 .index_page = -1,
1562 .page = -1,
1563 .path = repo_dir->name,
1564 .commit = rt->commit_id,
1565 }, "%s", msg ? msg : "");
1566 if (r == -1)
1567 goto done;
1569 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1570 "<div class='navs_wrapper'>\n"
1571 "<div class='navs'>") == -1)
1572 goto done;
1574 r = gotweb_link(c, &(struct gotweb_url){
1575 .action = TAG,
1576 .index_page = -1,
1577 .page = -1,
1578 .path = repo_dir->name,
1579 .commit = rt->commit_id,
1580 }, "tag");
1581 if (r == -1)
1582 goto done;
1584 if (fcgi_printf(c, " | ") == -1)
1585 goto done;
1587 r = gotweb_link(c, &(struct gotweb_url){
1588 .action = BRIEFS,
1589 .index_page = -1,
1590 .page = -1,
1591 .path = repo_dir->name,
1592 .commit = rt->commit_id,
1593 }, "commit briefs");
1594 if (r == -1)
1595 goto done;
1597 if (fcgi_printf(c, " | ") == -1)
1598 goto done;
1600 r = gotweb_link(c, &(struct gotweb_url){
1601 .action = COMMITS,
1602 .index_page = -1,
1603 .page = -1,
1604 .path = repo_dir->name,
1605 .commit = rt->commit_id,
1606 }, "commits");
1607 if (r == -1)
1608 goto done;
1610 r = fcgi_printf(c,
1611 "</div>\n" /* .navs */
1612 "</div>\n" /* .navs_wrapper */
1613 "<div class='dotted_line'></div>\n");
1614 if (r == -1)
1615 goto done;
1617 free(age);
1618 age = NULL;
1619 free(tagname);
1620 tagname = NULL;
1621 free(msg);
1622 msg = NULL;
1624 if (t->next_id || t->prev_id) {
1625 error = gotweb_render_navs(c);
1626 if (error)
1627 goto done;
1629 fcgi_printf(c, "</div>\n"); /* #tags_content */
1630 done:
1631 free(age);
1632 free(tagname);
1633 free(msg);
1634 return error;
1637 const struct got_error *
1638 gotweb_escape_html(char **escaped_html, const char *orig_html)
1640 const struct got_error *error = NULL;
1641 struct escape_pair {
1642 char c;
1643 const char *s;
1644 } esc[] = {
1645 { '>', "&gt;" },
1646 { '<', "&lt;" },
1647 { '&', "&amp;" },
1648 { '"', "&quot;" },
1649 { '\'', "&apos;" },
1650 { '\n', "<br />" },
1652 size_t orig_len, len;
1653 int i, j, x;
1655 orig_len = strlen(orig_html);
1656 len = orig_len;
1657 for (i = 0; i < orig_len; i++) {
1658 for (j = 0; j < nitems(esc); j++) {
1659 if (orig_html[i] != esc[j].c)
1660 continue;
1661 len += strlen(esc[j].s) - 1 /* escaped char */;
1665 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1666 if (*escaped_html == NULL)
1667 return got_error_from_errno("calloc");
1669 x = 0;
1670 for (i = 0; i < orig_len; i++) {
1671 int escaped = 0;
1672 for (j = 0; j < nitems(esc); j++) {
1673 if (orig_html[i] != esc[j].c)
1674 continue;
1676 if (strlcat(*escaped_html, esc[j].s, len + 1)
1677 >= len + 1) {
1678 error = got_error(GOT_ERR_NO_SPACE);
1679 goto done;
1681 x += strlen(esc[j].s);
1682 escaped = 1;
1683 break;
1685 if (!escaped) {
1686 (*escaped_html)[x] = orig_html[i];
1687 x++;
1690 done:
1691 if (error) {
1692 free(*escaped_html);
1693 *escaped_html = NULL;
1694 } else {
1695 (*escaped_html)[x] = '\0';
1698 return error;
1701 static inline int
1702 should_urlencode(int c)
1704 if (c <= ' ' || c >= 127)
1705 return 1;
1707 switch (c) {
1708 /* gen-delim */
1709 case ':':
1710 case '/':
1711 case '?':
1712 case '#':
1713 case '[':
1714 case ']':
1715 case '@':
1716 /* sub-delims */
1717 case '!':
1718 case '$':
1719 case '&':
1720 case '\'':
1721 case '(':
1722 case ')':
1723 case '*':
1724 case '+':
1725 case ',':
1726 case ';':
1727 case '=':
1728 return 1;
1729 default:
1730 return 0;
1734 static char *
1735 gotweb_urlencode(const char *str)
1737 const char *s;
1738 char *escaped;
1739 size_t i, len;
1740 int a, b;
1742 len = 0;
1743 for (s = str; *s; ++s) {
1744 len++;
1745 if (should_urlencode(*s))
1746 len += 2;
1749 escaped = calloc(1, len + 1);
1750 if (escaped == NULL)
1751 return NULL;
1753 i = 0;
1754 for (s = str; *s; ++s) {
1755 if (should_urlencode(*s)) {
1756 a = (*s & 0xF0) >> 4;
1757 b = (*s & 0x0F);
1759 escaped[i++] = '%';
1760 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1761 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1762 } else
1763 escaped[i++] = *s;
1766 return escaped;
1769 const char *
1770 gotweb_action_name(int action)
1772 switch (action) {
1773 case BLAME:
1774 return "blame";
1775 case BLOB:
1776 return "blob";
1777 case BRIEFS:
1778 return "briefs";
1779 case COMMITS:
1780 return "commits";
1781 case DIFF:
1782 return "diff";
1783 case ERR:
1784 return "err";
1785 case INDEX:
1786 return "index";
1787 case SUMMARY:
1788 return "summary";
1789 case TAG:
1790 return "tag";
1791 case TAGS:
1792 return "tags";
1793 case TREE:
1794 return "tree";
1795 default:
1796 return NULL;
1800 int
1801 gotweb_render_url(struct request *c, struct gotweb_url *url)
1803 const char *sep = "?", *action;
1804 char *tmp;
1805 int r;
1807 action = gotweb_action_name(url->action);
1808 if (action != NULL) {
1809 if (fcgi_printf(c, "?action=%s", action) == -1)
1810 return -1;
1811 sep = "&";
1814 if (url->commit) {
1815 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1816 return -1;
1817 sep = "&";
1820 if (url->previd) {
1821 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1822 return -1;
1823 sep = "&";
1826 if (url->prevset) {
1827 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1828 return -1;
1829 sep = "&";
1832 if (url->file) {
1833 tmp = gotweb_urlencode(url->file);
1834 if (tmp == NULL)
1835 return -1;
1836 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1837 free(tmp);
1838 if (r == -1)
1839 return -1;
1840 sep = "&";
1843 if (url->folder) {
1844 tmp = gotweb_urlencode(url->folder);
1845 if (tmp == NULL)
1846 return -1;
1847 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1848 free(tmp);
1849 if (r == -1)
1850 return -1;
1851 sep = "&";
1854 if (url->headref) {
1855 tmp = gotweb_urlencode(url->headref);
1856 if (tmp == NULL)
1857 return -1;
1858 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1859 free(tmp);
1860 if (r == -1)
1861 return -1;
1862 sep = "&";
1865 if (url->index_page != -1) {
1866 if (fcgi_printf(c, "%sindex_page=%d", sep,
1867 url->index_page) == -1)
1868 return -1;
1869 sep = "&";
1872 if (url->path) {
1873 tmp = gotweb_urlencode(url->path);
1874 if (tmp == NULL)
1875 return -1;
1876 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1877 free(tmp);
1878 if (r == -1)
1879 return -1;
1880 sep = "&";
1883 if (url->page != -1) {
1884 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1885 return -1;
1886 sep = "&";
1889 return 0;
1892 int
1893 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1895 va_list ap;
1896 int r;
1898 if (fcgi_printf(c, "<a href='") == -1)
1899 return -1;
1901 if (gotweb_render_url(c, url) == -1)
1902 return -1;
1904 if (fcgi_printf(c, "'>") == -1)
1905 return -1;
1907 va_start(ap, fmt);
1908 r = fcgi_vprintf(c, fmt, ap);
1909 va_end(ap);
1910 if (r == -1)
1911 return -1;
1913 if (fcgi_printf(c, "</a>"))
1914 return -1;
1915 return 0;
1918 static struct got_repository *
1919 find_cached_repo(struct server *srv, const char *path)
1921 int i;
1923 for (i = 0; i < srv->ncached_repos; i++) {
1924 if (strcmp(srv->cached_repos[i].path, path) == 0)
1925 return srv->cached_repos[i].repo;
1928 return NULL;
1931 static const struct got_error *
1932 cache_repo(struct got_repository **new, struct server *srv,
1933 struct repo_dir *repo_dir, struct socket *sock)
1935 const struct got_error *error = NULL;
1936 struct got_repository *repo;
1937 struct cached_repo *cr;
1938 int evicted = 0;
1940 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1941 cr = &srv->cached_repos[srv->ncached_repos - 1];
1942 error = got_repo_close(cr->repo);
1943 memset(cr, 0, sizeof(*cr));
1944 srv->ncached_repos--;
1945 if (error)
1946 return error;
1947 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1948 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1949 cr = &srv->cached_repos[0];
1950 evicted = 1;
1951 } else {
1952 cr = &srv->cached_repos[srv->ncached_repos];
1955 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1956 if (error) {
1957 if (evicted) {
1958 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1959 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1961 return error;
1964 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1965 >= sizeof(cr->path)) {
1966 if (evicted) {
1967 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1968 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1970 return got_error(GOT_ERR_NO_SPACE);
1973 cr->repo = repo;
1974 srv->ncached_repos++;
1975 *new = repo;
1976 return NULL;
1979 static const struct got_error *
1980 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1982 const struct got_error *error = NULL;
1983 struct socket *sock = c->sock;
1984 struct server *srv = c->srv;
1985 struct transport *t = c->t;
1986 struct got_repository *repo = NULL;
1987 DIR *dt;
1988 char *dir_test;
1990 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1991 GOTWEB_GIT_DIR) == -1)
1992 return got_error_from_errno("asprintf");
1994 dt = opendir(dir_test);
1995 if (dt == NULL) {
1996 free(dir_test);
1997 } else {
1998 repo_dir->path = dir_test;
1999 dir_test = NULL;
2000 goto done;
2003 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2004 repo_dir->name) == -1)
2005 return got_error_from_errno("asprintf");
2007 dt = opendir(dir_test);
2008 if (dt == NULL) {
2009 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2010 goto err;
2011 } else {
2012 repo_dir->path = dir_test;
2013 dir_test = NULL;
2016 done:
2017 if (srv->respect_exportok &&
2018 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2019 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2020 goto err;
2023 repo = find_cached_repo(srv, repo_dir->path);
2024 if (repo == NULL) {
2025 error = cache_repo(&repo, srv, repo_dir, sock);
2026 if (error)
2027 goto err;
2029 t->repo = repo;
2030 error = gotweb_get_repo_description(&repo_dir->description, srv,
2031 repo_dir->path, dirfd(dt));
2032 if (error)
2033 goto err;
2034 error = got_get_repo_owner(&repo_dir->owner, c);
2035 if (error)
2036 goto err;
2037 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2038 if (error)
2039 goto err;
2040 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2041 dirfd(dt));
2042 err:
2043 free(dir_test);
2044 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2045 error = got_error_from_errno("closedir");
2046 return error;
2049 static const struct got_error *
2050 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2052 const struct got_error *error;
2054 *repo_dir = calloc(1, sizeof(**repo_dir));
2055 if (*repo_dir == NULL)
2056 return got_error_from_errno("calloc");
2058 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2059 error = got_error_from_errno("asprintf");
2060 free(*repo_dir);
2061 *repo_dir = NULL;
2062 return error;
2064 (*repo_dir)->owner = NULL;
2065 (*repo_dir)->description = NULL;
2066 (*repo_dir)->url = NULL;
2067 (*repo_dir)->age = NULL;
2068 (*repo_dir)->path = NULL;
2070 return NULL;
2073 static const struct got_error *
2074 gotweb_get_repo_description(char **description, struct server *srv,
2075 const char *dirpath, int dir)
2077 const struct got_error *error = NULL;
2078 struct stat sb;
2079 int fd = -1;
2080 off_t len;
2082 *description = NULL;
2083 if (srv->show_repo_description == 0)
2084 return NULL;
2086 fd = openat(dir, "description", O_RDONLY);
2087 if (fd == -1) {
2088 if (errno != ENOENT && errno != EACCES) {
2089 error = got_error_from_errno_fmt("openat %s/%s",
2090 dirpath, "description");
2092 goto done;
2095 if (fstat(fd, &sb) == -1) {
2096 error = got_error_from_errno_fmt("fstat %s/%s",
2097 dirpath, "description");
2098 goto done;
2101 len = sb.st_size;
2102 if (len > GOTWEBD_MAXDESCRSZ - 1)
2103 len = GOTWEBD_MAXDESCRSZ - 1;
2105 *description = calloc(len + 1, sizeof(**description));
2106 if (*description == NULL) {
2107 error = got_error_from_errno("calloc");
2108 goto done;
2111 if (read(fd, *description, len) == -1)
2112 error = got_error_from_errno("read");
2113 done:
2114 if (fd != -1 && close(fd) == -1 && error == NULL)
2115 error = got_error_from_errno("close");
2116 return error;
2119 static const struct got_error *
2120 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2121 int dir)
2123 const struct got_error *error = NULL;
2124 struct stat sb;
2125 int fd = -1;
2126 off_t len;
2128 *url = NULL;
2129 if (srv->show_repo_cloneurl == 0)
2130 return NULL;
2132 fd = openat(dir, "cloneurl", O_RDONLY);
2133 if (fd == -1) {
2134 if (errno != ENOENT && errno != EACCES) {
2135 error = got_error_from_errno_fmt("openat %s/%s",
2136 dirpath, "cloneurl");
2138 goto done;
2141 if (fstat(fd, &sb) == -1) {
2142 error = got_error_from_errno_fmt("fstat %s/%s",
2143 dirpath, "cloneurl");
2144 goto done;
2147 len = sb.st_size;
2148 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2149 len = GOTWEBD_MAXCLONEURLSZ - 1;
2151 *url = calloc(len + 1, sizeof(**url));
2152 if (*url == NULL) {
2153 error = got_error_from_errno("calloc");
2154 goto done;
2157 if (read(fd, *url, len) == -1)
2158 error = got_error_from_errno("read");
2159 done:
2160 if (fd != -1 && close(fd) == -1 && error == NULL)
2161 error = got_error_from_errno("close");
2162 return error;
2165 const struct got_error *
2166 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2168 struct tm tm;
2169 long long diff_time;
2170 const char *years = "years ago", *months = "months ago";
2171 const char *weeks = "weeks ago", *days = "days ago";
2172 const char *hours = "hours ago", *minutes = "minutes ago";
2173 const char *seconds = "seconds ago", *now = "right now";
2174 char *s;
2175 char datebuf[29];
2177 *repo_age = NULL;
2179 switch (ref_tm) {
2180 case TM_DIFF:
2181 diff_time = time(NULL) - committer_time;
2182 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2183 if (asprintf(repo_age, "%lld %s",
2184 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2185 return got_error_from_errno("asprintf");
2186 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2187 if (asprintf(repo_age, "%lld %s",
2188 (diff_time / 60 / 60 / 24 / (365 / 12)),
2189 months) == -1)
2190 return got_error_from_errno("asprintf");
2191 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2192 if (asprintf(repo_age, "%lld %s",
2193 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2194 return got_error_from_errno("asprintf");
2195 } else if (diff_time > 60 * 60 * 24 * 2) {
2196 if (asprintf(repo_age, "%lld %s",
2197 (diff_time / 60 / 60 / 24), days) == -1)
2198 return got_error_from_errno("asprintf");
2199 } else if (diff_time > 60 * 60 * 2) {
2200 if (asprintf(repo_age, "%lld %s",
2201 (diff_time / 60 / 60), hours) == -1)
2202 return got_error_from_errno("asprintf");
2203 } else if (diff_time > 60 * 2) {
2204 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2205 minutes) == -1)
2206 return got_error_from_errno("asprintf");
2207 } else if (diff_time > 2) {
2208 if (asprintf(repo_age, "%lld %s", diff_time,
2209 seconds) == -1)
2210 return got_error_from_errno("asprintf");
2211 } else {
2212 if (asprintf(repo_age, "%s", now) == -1)
2213 return got_error_from_errno("asprintf");
2215 break;
2216 case TM_LONG:
2217 if (gmtime_r(&committer_time, &tm) == NULL)
2218 return got_error_from_errno("gmtime_r");
2220 s = asctime_r(&tm, datebuf);
2221 if (s == NULL)
2222 return got_error_from_errno("asctime_r");
2224 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2225 return got_error_from_errno("asprintf");
2226 break;
2228 return NULL;