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) 2013 David Gwynne <dlg@openbsd.org>
5 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
20 #include <net/if.h>
21 #include <netinet/in.h>
22 #include <sys/queue.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <event.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_path.h"
39 #include "got_cancel.h"
40 #include "got_worktree.h"
41 #include "got_diff.h"
42 #include "got_commit_graph.h"
43 #include "got_blame.h"
44 #include "got_privsep.h"
46 #include "proc.h"
47 #include "gotwebd.h"
49 #include "got_compat.h"
51 enum gotweb_ref_tm {
52 TM_DIFF,
53 TM_LONG,
54 };
56 static const struct querystring_keys querystring_keys[] = {
57 { "action", ACTION },
58 { "commit", COMMIT },
59 { "file", RFILE },
60 { "folder", FOLDER },
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
63 { "path", PATH },
64 { "page", PAGE },
65 };
67 static const struct action_keys action_keys[] = {
68 { "blame", BLAME },
69 { "blob", BLOB },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 };
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 char *);
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 char *, char *);
86 static const struct got_error *gotweb_render_header(struct request *);
87 static const struct got_error *gotweb_render_footer(struct request *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 const char *);
91 static const struct got_error *gotweb_load_got_path(struct request *c,
92 struct repo_dir *);
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, char *);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 char *);
97 static const struct got_error *gotweb_render_navs(struct request *);
98 static const struct got_error *gotweb_render_blame(struct request *);
99 static const struct got_error *gotweb_render_briefs(struct request *);
100 static const struct got_error *gotweb_render_commits(struct request *);
101 static const struct got_error *gotweb_render_diff(struct request *);
102 static const struct got_error *gotweb_render_summary(struct request *);
103 static const struct got_error *gotweb_render_tag(struct request *);
104 static const struct got_error *gotweb_render_tags(struct request *);
105 static const struct got_error *gotweb_render_tree(struct request *);
106 static const struct got_error *gotweb_render_branches(struct request *);
108 static void gotweb_free_querystring(struct querystring *);
109 static void gotweb_free_repo_dir(struct repo_dir *);
111 struct server *gotweb_get_server(uint8_t *, uint8_t *);
113 void
114 gotweb_process_request(struct request *c)
116 const struct got_error *error = NULL, *error2 = NULL;
117 struct server *srv = NULL;
118 struct querystring *qs = NULL;
119 struct repo_dir *repo_dir = NULL;
120 uint8_t err[] = "gotwebd experienced an error: ";
121 int r, html = 0;
123 /* init the transport */
124 error = gotweb_init_transport(&c->t);
125 if (error) {
126 log_warnx("%s: %s", __func__, error->msg);
127 return;
129 /* don't process any further if client disconnected */
130 if (c->sock->client_status == CLIENT_DISCONNECT)
131 return;
132 /* get the gotwebd server */
133 srv = gotweb_get_server(c->server_name, c->http_host);
134 if (srv == NULL) {
135 log_warnx("%s: error server is NULL", __func__);
136 goto err;
138 c->srv = srv;
139 /* parse our querystring */
140 error = gotweb_init_querystring(&qs);
141 if (error) {
142 log_warnx("%s: %s", __func__, error->msg);
143 goto err;
145 c->t->qs = qs;
146 error = gotweb_parse_querystring(&qs, c->querystring);
147 if (error) {
148 log_warnx("%s: %s", __func__, error->msg);
149 goto err;
152 /*
153 * certain actions require a commit id in the querystring. this stops
154 * bad actors from exploiting this by manually manipulating the
155 * querystring.
156 */
158 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
159 qs->action == DIFF)) {
160 error2 = got_error(GOT_ERR_QUERYSTRING);
161 goto render;
164 if (qs->action != INDEX) {
165 error = gotweb_init_repo_dir(&repo_dir, qs->path);
166 if (error)
167 goto done;
168 error = gotweb_load_got_path(c, repo_dir);
169 c->t->repo_dir = repo_dir;
170 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
171 goto err;
174 /* render top of page */
175 if (qs != NULL && qs->action == BLOB) {
176 error = got_get_repo_commits(c, 1);
177 if (error)
178 goto done;
179 error = got_output_file_blob(c);
180 if (error) {
181 log_warnx("%s: %s", __func__, error->msg);
182 goto err;
184 goto done;
185 } else {
186 render:
187 error = gotweb_render_content_type(c, "text/html");
188 if (error) {
189 log_warnx("%s: %s", __func__, error->msg);
190 goto err;
192 html = 1;
195 error = gotweb_render_header(c);
196 if (error) {
197 log_warnx("%s: %s", __func__, error->msg);
198 goto err;
201 if (error2) {
202 error = error2;
203 goto err;
206 switch(qs->action) {
207 case BLAME:
208 error = gotweb_render_blame(c);
209 if (error) {
210 log_warnx("%s: %s", __func__, error->msg);
211 goto err;
213 break;
214 case BRIEFS:
215 error = gotweb_render_briefs(c);
216 if (error) {
217 log_warnx("%s: %s", __func__, error->msg);
218 goto err;
220 break;
221 case COMMITS:
222 error = gotweb_render_commits(c);
223 if (error) {
224 log_warnx("%s: %s", __func__, error->msg);
225 goto err;
227 break;
228 case DIFF:
229 error = gotweb_render_diff(c);
230 if (error) {
231 log_warnx("%s: %s", __func__, error->msg);
232 goto err;
234 break;
235 case INDEX:
236 error = gotweb_render_index(c);
237 if (error) {
238 log_warnx("%s: %s", __func__, error->msg);
239 goto err;
241 break;
242 case SUMMARY:
243 error = gotweb_render_summary(c);
244 if (error) {
245 log_warnx("%s: %s", __func__, error->msg);
246 goto err;
248 break;
249 case TAG:
250 error = gotweb_render_tag(c);
251 if (error) {
252 log_warnx("%s: %s", __func__, error->msg);
253 goto err;
255 break;
256 case TAGS:
257 error = gotweb_render_tags(c);
258 if (error) {
259 log_warnx("%s: %s", __func__, error->msg);
260 goto err;
262 break;
263 case TREE:
264 error = gotweb_render_tree(c);
265 if (error) {
266 log_warnx("%s: %s", __func__, error->msg);
267 goto err;
269 break;
270 case ERR:
271 default:
272 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
273 "Erorr: Bad Querystring");
274 if (r == -1)
275 goto err;
276 break;
279 goto done;
280 err:
281 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
282 return;
283 if (fcgi_printf(c, "%s", err) == -1)
284 return;
285 if (error) {
286 if (fcgi_printf(c, "%s", error->msg) == -1)
287 return;
288 } else {
289 if (fcgi_printf(c, "see daemon logs for details") == -1)
290 return;
292 if (html && fcgi_printf(c, "</div>\n") == -1)
293 return;
294 done:
295 if (html && srv != NULL)
296 gotweb_render_footer(c);
299 struct server *
300 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
302 struct server *srv = NULL;
304 /* check against the server name first */
305 if (strlen(server_name) > 0)
306 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
307 if (strcmp(srv->name, server_name) == 0)
308 goto done;
310 /* check against subdomain second */
311 if (strlen(subdomain) > 0)
312 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
313 if (strcmp(srv->name, subdomain) == 0)
314 goto done;
316 /* if those fail, send first server */
317 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
318 if (srv != NULL)
319 break;
320 done:
321 return srv;
322 };
324 const struct got_error *
325 gotweb_init_transport(struct transport **t)
327 const struct got_error *error = NULL;
329 *t = calloc(1, sizeof(**t));
330 if (*t == NULL)
331 return got_error_from_errno2("%s: calloc", __func__);
333 TAILQ_INIT(&(*t)->repo_commits);
334 TAILQ_INIT(&(*t)->repo_tags);
336 (*t)->repo = NULL;
337 (*t)->repo_dir = NULL;
338 (*t)->qs = NULL;
339 (*t)->next_id = NULL;
340 (*t)->prev_id = NULL;
341 (*t)->next_disp = 0;
342 (*t)->prev_disp = 0;
344 return error;
347 static const struct got_error *
348 gotweb_init_querystring(struct querystring **qs)
350 const struct got_error *error = NULL;
352 *qs = calloc(1, sizeof(**qs));
353 if (*qs == NULL)
354 return got_error_from_errno2("%s: calloc", __func__);
356 (*qs)->action = INDEX;
357 (*qs)->commit = NULL;
358 (*qs)->file = NULL;
359 (*qs)->folder = NULL;
360 (*qs)->headref = strdup("HEAD");
361 if ((*qs)->headref == NULL) {
362 return got_error_from_errno2("%s: strdup", __func__);
364 (*qs)->index_page = 0;
365 (*qs)->index_page_str = NULL;
366 (*qs)->path = NULL;
368 return error;
371 static const struct got_error *
372 gotweb_parse_querystring(struct querystring **qs, char *qst)
374 const struct got_error *error = NULL;
375 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
376 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
378 if (qst == NULL)
379 return error;
381 tok1 = strdup(qst);
382 if (tok1 == NULL)
383 return got_error_from_errno2("%s: strdup", __func__);
385 tok1_pair = tok1;
386 tok1_end = tok1;
388 while (tok1_pair != NULL) {
389 strsep(&tok1_end, "&");
391 tok2 = strdup(tok1_pair);
392 if (tok2 == NULL) {
393 free(tok1);
394 return got_error_from_errno2("%s: strdup", __func__);
397 tok2_pair = tok2;
398 tok2_end = tok2;
400 while (tok2_pair != NULL) {
401 strsep(&tok2_end, "=");
402 if (tok2_end) {
403 error = gotweb_assign_querystring(qs, tok2_pair,
404 tok2_end);
405 if (error)
406 goto err;
408 tok2_pair = tok2_end;
410 free(tok2);
411 tok1_pair = tok1_end;
413 free(tok1);
414 return error;
415 err:
416 free(tok2);
417 free(tok1);
418 return error;
421 static const struct got_error *
422 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
424 const struct got_error *error = NULL;
425 const char *errstr;
426 int a_cnt, el_cnt;
428 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
429 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
430 continue;
432 switch (querystring_keys[el_cnt].element) {
433 case ACTION:
434 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
435 if (strcmp(value, action_keys[a_cnt].name) != 0)
436 continue;
437 else if (strcmp(value,
438 action_keys[a_cnt].name) == 0){
439 (*qs)->action =
440 action_keys[a_cnt].action;
441 goto qa_found;
444 (*qs)->action = ERR;
445 qa_found:
446 break;
447 case COMMIT:
448 (*qs)->commit = strdup(value);
449 if ((*qs)->commit == NULL) {
450 error = got_error_from_errno2("%s: strdup",
451 __func__);
452 goto done;
454 break;
455 case RFILE:
456 (*qs)->file = strdup(value);
457 if ((*qs)->file == NULL) {
458 error = got_error_from_errno2("%s: strdup",
459 __func__);
460 goto done;
462 break;
463 case FOLDER:
464 (*qs)->folder = strdup(value);
465 if ((*qs)->folder == NULL) {
466 error = got_error_from_errno2("%s: strdup",
467 __func__);
468 goto done;
470 break;
471 case HEADREF:
472 (*qs)->headref = strdup(value);
473 if ((*qs)->headref == NULL) {
474 error = got_error_from_errno2("%s: strdup",
475 __func__);
476 goto done;
478 break;
479 case INDEX_PAGE:
480 if (strlen(value) == 0)
481 break;
482 (*qs)->index_page_str = strdup(value);
483 if ((*qs)->index_page_str == NULL) {
484 error = got_error_from_errno2("%s: strdup",
485 __func__);
486 goto done;
488 (*qs)->index_page = strtonum(value, INT64_MIN,
489 INT64_MAX, &errstr);
490 if (errstr) {
491 error = got_error_from_errno3("%s: strtonum %s",
492 __func__, errstr);
493 goto done;
495 if ((*qs)->index_page < 0) {
496 (*qs)->index_page = 0;
497 sprintf((*qs)->index_page_str, "%d", 0);
499 break;
500 case PATH:
501 (*qs)->path = strdup(value);
502 if ((*qs)->path == NULL) {
503 error = got_error_from_errno2("%s: strdup",
504 __func__);
505 goto done;
507 break;
508 case PAGE:
509 if (strlen(value) == 0)
510 break;
511 (*qs)->page_str = strdup(value);
512 if ((*qs)->page_str == NULL) {
513 error = got_error_from_errno2("%s: strdup",
514 __func__);
515 goto done;
517 (*qs)->page = strtonum(value, INT64_MIN,
518 INT64_MAX, &errstr);
519 if (errstr) {
520 error = got_error_from_errno3("%s: strtonum %s",
521 __func__, errstr);
522 goto done;
524 if ((*qs)->page < 0) {
525 (*qs)->page = 0;
526 sprintf((*qs)->page_str, "%d", 0);
528 break;
529 default:
530 break;
533 done:
534 return error;
537 void
538 gotweb_free_repo_tag(struct repo_tag *rt)
540 if (rt != NULL) {
541 free(rt->commit_msg);
542 free(rt->commit_id);
543 free(rt->tagger);
545 free(rt);
548 void
549 gotweb_free_repo_commit(struct repo_commit *rc)
551 if (rc != NULL) {
552 free(rc->path);
553 free(rc->refs_str);
554 free(rc->commit_id);
555 free(rc->parent_id);
556 free(rc->tree_id);
557 free(rc->author);
558 free(rc->committer);
559 free(rc->commit_msg);
561 free(rc);
564 static void
565 gotweb_free_querystring(struct querystring *qs)
567 if (qs != NULL) {
568 free(qs->commit);
569 free(qs->file);
570 free(qs->folder);
571 free(qs->headref);
572 free(qs->index_page_str);
573 free(qs->path);
574 free(qs->page_str);
576 free(qs);
579 static void
580 gotweb_free_repo_dir(struct repo_dir *repo_dir)
582 if (repo_dir != NULL) {
583 free(repo_dir->name);
584 free(repo_dir->owner);
585 free(repo_dir->description);
586 free(repo_dir->url);
587 free(repo_dir->age);
588 free(repo_dir->path);
590 free(repo_dir);
593 void
594 gotweb_free_transport(struct transport *t)
596 struct repo_commit *rc = NULL, *trc = NULL;
597 struct repo_tag *rt = NULL, *trt = NULL;
599 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
600 TAILQ_REMOVE(&t->repo_commits, rc, entry);
601 gotweb_free_repo_commit(rc);
603 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
604 TAILQ_REMOVE(&t->repo_tags, rt, entry);
605 gotweb_free_repo_tag(rt);
607 gotweb_free_repo_dir(t->repo_dir);
608 gotweb_free_querystring(t->qs);
609 free(t->next_id);
610 free(t->prev_id);
611 free(t);
614 const struct got_error *
615 gotweb_render_content_type(struct request *c, const uint8_t *type)
617 const char *csp = "default-src 'self'; script-src 'none'; "
618 "object-src 'none';";
620 fcgi_printf(c,
621 "Content-Security-Policy: %s\r\n"
622 "Content-Type: %s\r\n\r\n",
623 csp, type);
624 return NULL;
627 const struct got_error *
628 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
629 char *file)
631 fcgi_printf(c, "Content-type: %s\r\n"
632 "Content-disposition: attachment; filename=%s\r\n\r\n",
633 type, file);
634 return NULL;
637 static const struct got_error *
638 gotweb_render_header(struct request *c)
640 struct server *srv = c->srv;
641 struct querystring *qs = c->t->qs;
642 int r;
644 r = fcgi_printf(c, "<!doctype html>\n"
645 "<html>\n"
646 "<head>\n"
647 "<title>%s</title>\n"
648 "<meta charset='utf-8' />\n"
649 "<meta name='viewport' content='initial-scale=.75' />\n"
650 "<meta name='msapplication-TileColor' content='#da532c' />\n"
651 "<meta name='theme-color' content='#ffffff'/>\n"
652 "<link rel='apple-touch-icon' sizes='180x180'"
653 " href='/apple-touch-icon.png' />\n"
654 "<link rel='icon' type='image/png' sizes='32x32'"
655 " href='/favicon-32x32.png' />\n"
656 "<link rel='icon' type='image/png' sizes='16x16'"
657 " href='/favicon-16x16.png' />\n"
658 "<link rel='manifest' href='/site.webmanifest'/>\n"
659 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
660 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
661 "</head>\n"
662 "<body>\n"
663 "<div id='gw_body'>\n"
664 "<div id='header'>\n"
665 "<div id='got_link'>"
666 "<a href='%s' target='_blank'>"
667 "<img src='%s%s' alt='logo' id='logo' />"
668 "</a>\n"
669 "</div>\n" /* #got_link */
670 "</div>\n" /* #header */
671 "<div id='site_path'>\n"
672 "<div id='site_link'>\n"
673 "<a href='?index_page=%d'>%s</a>",
674 srv->site_name,
675 c->script_name, srv->custom_css,
676 srv->logo_url,
677 c->script_name, srv->logo,
678 qs->index_page, srv->site_link);
679 if (r == -1)
680 goto done;
682 if (qs != NULL) {
683 if (qs->path != NULL) {
684 r = fcgi_printf(c, " / "
685 "<a href='?index_page=%d&path=%s&action=summary'>"
686 "%s</a>",
687 qs->index_page, qs->path, qs->path);
688 if (r == -1)
689 goto done;
691 if (qs->action != INDEX) {
692 const char *action = "";
694 switch (qs->action) {
695 case BLAME:
696 action = "blame";
697 break;
698 case BRIEFS:
699 action = "briefs";
700 break;
701 case COMMITS:
702 action = "commits";
703 break;
704 case DIFF:
705 action = "diff";
706 break;
707 case SUMMARY:
708 action = "summary";
709 break;
710 case TAG:
711 action = "tag";
712 break;
713 case TAGS:
714 action = "tags";
715 break;
716 case TREE:
717 action = "tree";
718 break;
721 if (fcgi_printf(c, " / %s", action) == -1)
722 goto done;
726 fcgi_printf(c, "</div>\n" /* #site_path */
727 "</div>\n" /* #site_link */
728 "<div id='content'>\n");
730 done:
731 return NULL;
734 static const struct got_error *
735 gotweb_render_footer(struct request *c)
737 const struct got_error *error = NULL;
738 struct server *srv = c->srv;
739 const char *siteowner = "&nbsp;";
740 char *escaped_owner = NULL;
742 if (srv->show_site_owner) {
743 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
744 if (error)
745 return error;
746 siteowner = escaped_owner;
749 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
750 "<div id='site_owner'>%s</div>\n"
751 "</div>\n" /* #site_owner_wrapper */
752 "</div>\n" /* #content */
753 "</div>\n" /* #gw_body */
754 "</body>\n</html>\n", siteowner);
756 free(escaped_owner);
757 return NULL;
760 static const struct got_error *
761 gotweb_render_navs(struct request *c)
763 const struct got_error *error = NULL;
764 struct transport *t = c->t;
765 struct querystring *qs = t->qs;
766 struct server *srv = c->srv;
767 char *nhref = NULL, *phref = NULL;
768 int r, disp = 0;
770 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
771 if (r == -1)
772 goto done;
774 switch(qs->action) {
775 case INDEX:
776 if (qs->index_page > 0) {
777 if (asprintf(&phref, "index_page=%d",
778 qs->index_page - 1) == -1) {
779 error = got_error_from_errno2("%s: asprintf",
780 __func__);
781 goto done;
783 disp = 1;
785 break;
786 case BRIEFS:
787 if (t->prev_id && qs->commit != NULL &&
788 strcmp(qs->commit, t->prev_id) != 0) {
789 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
790 "&action=briefs&commit=%s&headref=%s",
791 qs->index_page, qs->path, qs->page - 1, t->prev_id,
792 qs->headref) == -1) {
793 error = got_error_from_errno2("%s: asprintf",
794 __func__);
795 goto done;
797 disp = 1;
799 break;
800 case COMMITS:
801 if (t->prev_id && qs->commit != NULL &&
802 strcmp(qs->commit, t->prev_id) != 0) {
803 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
804 "&action=commits&commit=%s&headref=%s&folder=%s"
805 "&file=%s",
806 qs->index_page, qs->path, qs->page - 1, t->prev_id,
807 qs->headref, qs->folder ? qs->folder : "",
808 qs->file ? qs->file : "") == -1) {
809 error = got_error_from_errno2("%s: asprintf",
810 __func__);
811 goto done;
813 disp = 1;
815 break;
816 case TAGS:
817 if (t->prev_id && qs->commit != NULL &&
818 strcmp(qs->commit, t->prev_id) != 0) {
819 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
820 "&action=tags&commit=%s&headref=%s",
821 qs->index_page, qs->path, qs->page - 1, t->prev_id,
822 qs->headref) == -1) {
823 error = got_error_from_errno2("%s: asprintf",
824 __func__);
825 goto done;
827 disp = 1;
829 break;
830 default:
831 disp = 0;
832 break;
835 if (disp) {
836 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
837 if (r == -1)
838 goto done;
841 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
842 "<div id='nav_next'>");
843 if (r == -1)
844 goto done;
846 disp = 0;
847 switch(qs->action) {
848 case INDEX:
849 if (t->next_disp == srv->max_repos_display &&
850 t->repos_total != (qs->index_page + 1) *
851 srv->max_repos_display) {
852 if (asprintf(&nhref, "index_page=%d",
853 qs->index_page + 1) == -1) {
854 error = got_error_from_errno2("%s: asprintf",
855 __func__);
856 goto done;
858 disp = 1;
860 break;
861 case BRIEFS:
862 if (t->next_id) {
863 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
864 "&action=briefs&commit=%s&headref=%s",
865 qs->index_page, qs->path, qs->page + 1, t->next_id,
866 qs->headref) == -1) {
867 error = got_error_from_errno2("%s: asprintf",
868 __func__);
869 goto done;
871 disp = 1;
873 break;
874 case COMMITS:
875 if (t->next_id) {
876 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
877 "&action=commits&commit=%s&headref=%s&folder=%s"
878 "&file=%s",
879 qs->index_page, qs->path, qs->page + 1, t->next_id,
880 qs->headref, qs->folder ? qs->folder : "",
881 qs->file ? qs->file : "") == -1) {
882 error = got_error_from_errno2("%s: asprintf",
883 __func__);
884 goto done;
886 disp = 1;
888 break;
889 case TAGS:
890 if (t->next_id) {
891 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
892 "&action=tags&commit=%s&headref=%s",
893 qs->index_page, qs->path, qs->page + 1, t->next_id,
894 qs->headref) == -1) {
895 error = got_error_from_errno2("%s: asprintf",
896 __func__);
897 goto done;
899 disp = 1;
901 break;
902 default:
903 disp = 0;
904 break;
906 if (disp) {
907 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
908 if (r == -1)
909 goto done;
911 fcgi_printf(c, "</div>\n"); /* #nav_next */
912 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
913 done:
914 free(t->next_id);
915 t->next_id = NULL;
916 free(t->prev_id);
917 t->prev_id = NULL;
918 free(phref);
919 free(nhref);
920 return error;
923 static const struct got_error *
924 gotweb_render_index(struct request *c)
926 const struct got_error *error = NULL;
927 struct server *srv = c->srv;
928 struct transport *t = c->t;
929 struct querystring *qs = t->qs;
930 struct repo_dir *repo_dir = NULL;
931 DIR *d;
932 struct dirent **sd_dent;
933 const char *index_page_str;
934 char *c_path = NULL;
935 struct stat st;
936 unsigned int d_cnt, d_i, d_disp = 0;
937 int r;
939 index_page_str = qs->index_page_str ? qs->index_page_str : "";
941 d = opendir(srv->repos_path);
942 if (d == NULL) {
943 error = got_error_from_errno2("opendir", srv->repos_path);
944 return error;
947 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
948 if (d_cnt == -1) {
949 error = got_error_from_errno2("scandir", srv->repos_path);
950 goto done;
953 /* get total count of repos */
954 for (d_i = 0; d_i < d_cnt; d_i++) {
955 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
956 strcmp(sd_dent[d_i]->d_name, "..") == 0)
957 continue;
959 if (asprintf(&c_path, "%s/%s", srv->repos_path,
960 sd_dent[d_i]->d_name) == -1) {
961 error = got_error_from_errno("asprintf");
962 return error;
965 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
966 !got_path_dir_is_empty(c_path))
967 t->repos_total++;
968 free(c_path);
969 c_path = NULL;
972 r = fcgi_printf(c, "<div id='index_header'>\n"
973 "<div id='index_header_project'>Project</div>\n");
974 if (r == -1)
975 goto done;
977 if (srv->show_repo_description)
978 if (fcgi_printf(c, "<div id='index_header_description'>"
979 "Description</div>\n") == -1)
980 goto done;
981 if (srv->show_repo_owner)
982 if (fcgi_printf(c, "<div id='index_header_owner'>"
983 "Owner</div>\n") == -1)
984 goto done;
985 if (srv->show_repo_age)
986 if (fcgi_printf(c, "<div id='index_header_age'>"
987 "Last Change</div>\n") == -1)
988 goto done;
989 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
990 goto done;
992 for (d_i = 0; d_i < d_cnt; d_i++) {
993 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
994 break; /* account for parent and self */
996 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
997 strcmp(sd_dent[d_i]->d_name, "..") == 0)
998 continue;
1000 if (qs->index_page > 0 && (qs->index_page *
1001 srv->max_repos_display) > t->prev_disp) {
1002 t->prev_disp++;
1003 continue;
1006 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1007 if (error)
1008 goto done;
1010 error = gotweb_load_got_path(c, repo_dir);
1011 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1012 error = NULL;
1013 continue;
1015 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1016 goto done;
1018 if (lstat(repo_dir->path, &st) == 0 &&
1019 S_ISDIR(st.st_mode) &&
1020 !got_path_dir_is_empty(repo_dir->path))
1021 goto render;
1022 else {
1023 gotweb_free_repo_dir(repo_dir);
1024 repo_dir = NULL;
1025 continue;
1027 render:
1028 d_disp++;
1029 t->prev_disp++;
1031 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1032 "<div class='index_project'>"
1033 "<a href='?index_page=%s&path=%s&action=summary'>"
1034 " %s "
1035 "</a>"
1036 "</div>", /* .index_project */
1037 index_page_str, repo_dir->name,
1038 repo_dir->name);
1039 if (r == -1)
1040 goto done;
1042 if (srv->show_repo_description) {
1043 r = fcgi_printf(c,
1044 "<div class='index_project_description'>\n"
1045 "%s</div>\n", repo_dir->description);
1046 if (r == -1)
1047 goto done;
1050 if (srv->show_repo_owner) {
1051 r = fcgi_printf(c, "<div class='index_project_owner'>"
1052 "%s</div>\n", repo_dir->owner);
1053 if (r == -1)
1054 goto done;
1057 if (srv->show_repo_age) {
1058 r = fcgi_printf(c, "<div class='index_project_age'>"
1059 "%s</div>\n", repo_dir->age);
1060 if (r == -1)
1061 goto done;
1064 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1065 "<div class='navs'>"
1066 "<a href='?index_page=%s&path=%s&action=summary'>"
1067 "summary"
1068 "</a> | "
1069 "<a href='?index_page=%s&path=%s&action=briefs'>"
1070 "commit briefs"
1071 "</a> | "
1072 "<a href='?index_page=%s&path=%s&action=commits'>"
1073 "commits"
1074 "</a> | "
1075 "<a href='?index_page=%s&path=%s&action=tags'>"
1076 "tags"
1077 "</a> | "
1078 "<a href='?index_page=%s&path=%s&action=tree'>"
1079 "tree"
1080 "</a>"
1081 "</div>" /* .navs */
1082 "<div class='dotted_line'></div>\n"
1083 "</div>\n" /* .navs_wrapper */
1084 "</div>\n", /* .index_wrapper */
1085 index_page_str, repo_dir->name,
1086 index_page_str, repo_dir->name,
1087 index_page_str, repo_dir->name,
1088 index_page_str, repo_dir->name,
1089 index_page_str, repo_dir->name);
1090 if (r == -1)
1091 goto done;
1093 gotweb_free_repo_dir(repo_dir);
1094 repo_dir = NULL;
1095 t->next_disp++;
1096 if (d_disp == srv->max_repos_display)
1097 break;
1099 if (srv->max_repos_display == 0)
1100 goto done;
1101 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1102 goto done;
1103 if (t->repos_total <= srv->max_repos ||
1104 t->repos_total <= srv->max_repos_display)
1105 goto done;
1107 error = gotweb_render_navs(c);
1108 if (error)
1109 goto done;
1110 done:
1111 if (d != NULL && closedir(d) == EOF && error == NULL)
1112 error = got_error_from_errno("closedir");
1113 return error;
1116 static const struct got_error *
1117 gotweb_render_blame(struct request *c)
1119 const struct got_error *error = NULL;
1120 struct transport *t = c->t;
1121 struct repo_commit *rc = NULL;
1122 char *age = NULL, *msg = NULL;
1123 int r;
1125 error = got_get_repo_commits(c, 1);
1126 if (error)
1127 return error;
1129 rc = TAILQ_FIRST(&t->repo_commits);
1131 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1132 if (error)
1133 goto done;
1134 error = gotweb_escape_html(&msg, rc->commit_msg);
1135 if (error)
1136 goto done;
1138 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1139 "<div id='blame_title'>Blame</div>\n"
1140 "</div>\n" /* #blame_title_wrapper */
1141 "<div id='blame_content'>\n"
1142 "<div id='blame_header_wrapper'>\n"
1143 "<div id='blame_header'>\n"
1144 "<div class='header_age_title'>Date:</div>\n"
1145 "<div class='header_age'>%s</div>\n"
1146 "<div id='header_commit_msg_title'>Message:</div>\n"
1147 "<div id='header_commit_msg'>%s</div>\n"
1148 "</div>\n" /* #blame_header */
1149 "</div>\n" /* #blame_header_wrapper */
1150 "<div class='dotted_line'></div>\n"
1151 "<div id='blame'>\n",
1152 age,
1153 msg);
1154 if (r == -1)
1155 goto done;
1157 error = got_output_file_blame(c);
1158 if (error)
1159 goto done;
1161 fcgi_printf(c, "</div>\n" /* #blame */
1162 "</div>\n"); /* #blame_content */
1163 done:
1164 free(age);
1165 free(msg);
1166 return error;
1169 static const struct got_error *
1170 gotweb_render_briefs(struct request *c)
1172 const struct got_error *error = NULL;
1173 struct repo_commit *rc = NULL;
1174 struct server *srv = c->srv;
1175 struct transport *t = c->t;
1176 struct querystring *qs = t->qs;
1177 struct repo_dir *repo_dir = t->repo_dir;
1178 const char *index_page_str;
1179 char *smallerthan, *newline;
1180 char *age = NULL, *author = NULL, *msg = NULL;
1181 int r;
1183 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1185 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1186 "<div id='briefs_title'>Commit Briefs</div>\n"
1187 "</div>\n" /* #briefs_title_wrapper */
1188 "<div id='briefs_content'>\n");
1189 if (r == -1)
1190 goto done;
1192 if (qs->action == SUMMARY) {
1193 qs->action = BRIEFS;
1194 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1195 } else
1196 error = got_get_repo_commits(c, srv->max_commits_display);
1197 if (error)
1198 goto done;
1200 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1201 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1202 if (error)
1203 goto done;
1205 smallerthan = strchr(rc->author, '<');
1206 if (smallerthan)
1207 *smallerthan = '\0';
1209 newline = strchr(rc->commit_msg, '\n');
1210 if (newline)
1211 *newline = '\0';
1213 error = gotweb_escape_html(&author, rc->author);
1214 if (error)
1215 goto done;
1216 error = gotweb_escape_html(&msg, rc->commit_msg);
1217 if (error)
1218 goto done;
1220 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1221 "<div class='briefs_author'>%s</div>\n"
1222 "<div class='briefs_log'>"
1223 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1224 "&headref=%s'>%s</a>",
1225 age,
1226 author,
1227 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1228 msg);
1229 if (r == -1)
1230 goto done;
1232 if (rc->refs_str) {
1233 char *refs;
1235 error = gotweb_escape_html(&refs, rc->refs_str);
1236 if (error)
1237 goto done;
1238 r = fcgi_printf(c,
1239 " <span class='refs_str'>(%s)</span>", refs);
1240 free(refs);
1241 if (r == -1)
1242 goto done;
1244 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1245 goto done;
1247 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1248 "<div class='navs'>"
1249 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1250 "&headref=%s'>diff</a>"
1251 " | "
1252 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1253 "&headref=%s'>tree</a>"
1254 "</div>\n" /* .navs */
1255 "</div>\n" /* .navs_wrapper */
1256 "<div class='dotted_line'></div>\n",
1257 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1258 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1259 if (r == -1)
1260 goto done;
1262 free(age);
1263 age = NULL;
1264 free(author);
1265 author = NULL;
1266 free(msg);
1267 msg = NULL;
1270 if (t->next_id || t->prev_id) {
1271 error = gotweb_render_navs(c);
1272 if (error)
1273 goto done;
1275 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1276 done:
1277 free(age);
1278 free(author);
1279 free(msg);
1280 return error;
1283 static const struct got_error *
1284 gotweb_render_commits(struct request *c)
1286 const struct got_error *error = NULL;
1287 struct repo_commit *rc = NULL;
1288 struct server *srv = c->srv;
1289 struct transport *t = c->t;
1290 struct querystring *qs = t->qs;
1291 struct repo_dir *repo_dir = t->repo_dir;
1292 const char *index_page_str;
1293 char *age = NULL, *author = NULL, *msg = NULL;
1294 int r;
1296 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1298 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1299 "<div class='commits_title'>Commits</div>\n"
1300 "</div>\n" /* .commits_title_wrapper */
1301 "<div class='commits_content'>\n");
1302 if (r == -1)
1303 goto done;
1305 error = got_get_repo_commits(c, srv->max_commits_display);
1306 if (error)
1307 goto done;
1309 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1310 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1311 if (error)
1312 goto done;
1313 error = gotweb_escape_html(&author, rc->author);
1314 if (error)
1315 goto done;
1316 error = gotweb_escape_html(&msg, rc->commit_msg);
1317 if (error)
1318 goto done;
1320 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1321 "<div class='commits_header'>\n"
1322 "<div class='header_commit_title'>Commit:</div>\n"
1323 "<div class='header_commit'>%s</div>\n"
1324 "<div class='header_author_title'>Author:</div>\n"
1325 "<div class='header_author'>%s</div>\n"
1326 "<div class='header_age_title'>Date:</div>\n"
1327 "<div class='header_age'>%s</div>\n"
1328 "</div>\n" /* .commits_header */
1329 "</div>\n" /* .commits_header_wrapper */
1330 "<div class='dotted_line'></div>\n"
1331 "<div class='commit'>\n%s</div>\n",
1332 rc->commit_id,
1333 author,
1334 age,
1335 msg);
1336 if (r == -1)
1337 goto done;
1339 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1340 "<div class='navs'>"
1341 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1342 "diff</a>"
1343 " | "
1344 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1345 "tree</a>"
1346 "</div>\n" /* .navs */
1347 "</div>\n" /* .navs_wrapper */
1348 "<div class='dotted_line'></div>\n",
1349 index_page_str, repo_dir->name, rc->commit_id,
1350 index_page_str, repo_dir->name, rc->commit_id);
1352 free(age);
1353 age = NULL;
1354 free(author);
1355 author = NULL;
1356 free(msg);
1357 msg = NULL;
1360 if (t->next_id || t->prev_id) {
1361 error = gotweb_render_navs(c);
1362 if (error)
1363 goto done;
1365 fcgi_printf(c, "</div>\n"); /* .commits_content */
1366 done:
1367 free(age);
1368 free(author);
1369 free(msg);
1370 return error;
1373 static const struct got_error *
1374 gotweb_render_branches(struct request *c)
1376 const struct got_error *error = NULL;
1377 struct got_reflist_head refs;
1378 struct got_reflist_entry *re;
1379 struct transport *t = c->t;
1380 struct querystring *qs = t->qs;
1381 struct got_repository *repo = t->repo;
1382 const char *index_page_str;
1383 char *age = NULL;
1384 int r;
1386 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1388 TAILQ_INIT(&refs);
1390 error = got_ref_list(&refs, repo, "refs/heads",
1391 got_ref_cmp_by_name, NULL);
1392 if (error)
1393 goto done;
1395 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1396 "<div id='branches_title'>Branches</div>\n"
1397 "</div>\n" /* #branches_title_wrapper */
1398 "<div id='branches_content'>\n");
1399 if (r == -1)
1400 goto done;
1402 TAILQ_FOREACH(re, &refs, entry) {
1403 const char *refname = NULL;
1404 char *escaped_refname = NULL;
1406 if (got_ref_is_symbolic(re->ref))
1407 continue;
1409 refname = got_ref_get_name(re->ref);
1410 if (refname == NULL) {
1411 error = got_error_from_errno("strdup");
1412 goto done;
1414 if (strncmp(refname, "refs/heads/", 11) != 0)
1415 continue;
1417 error = got_get_repo_age(&age, c, qs->path, refname,
1418 TM_DIFF);
1419 if (error)
1420 goto done;
1422 if (strncmp(refname, "refs/heads/", 11) == 0)
1423 refname += 11;
1424 error = gotweb_escape_html(&escaped_refname, refname);
1425 if (error)
1426 goto done;
1428 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1429 "<div class='branches_age'>%s</div>\n"
1430 "<div class='branches_space'>&nbsp;</div>\n"
1431 "<div class='branch'>"
1432 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1433 "%s</a>"
1434 "</div>\n" /* .branch */
1435 "<div class='navs_wrapper'>\n"
1436 "<div class='navs'>"
1437 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1438 "summary</a>"
1439 " | "
1440 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1441 "commit briefs</a>"
1442 " | "
1443 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1444 "commits</a>"
1445 "</div>\n" /* .navs */
1446 "</div>\n" /* .navs_wrapper */
1447 "<div class='dotted_line'></div>\n"
1448 "</div>\n", /* .branches_wrapper */
1449 age ? age : "",
1450 index_page_str, qs->path, refname,
1451 escaped_refname,
1452 index_page_str, qs->path, refname,
1453 index_page_str, qs->path, refname,
1454 index_page_str, qs->path, refname);
1455 free(escaped_refname);
1456 if (r == -1)
1457 goto done;
1459 free(age);
1460 age = NULL;
1463 fcgi_printf(c, "</div>\n"); /* #branches_content */
1464 done:
1465 return error;
1468 static const struct got_error *
1469 gotweb_render_tree(struct request *c)
1471 const struct got_error *error = NULL;
1472 struct transport *t = c->t;
1473 struct repo_commit *rc = NULL;
1474 char *age = NULL, *msg = NULL;
1475 int r;
1477 error = got_get_repo_commits(c, 1);
1478 if (error)
1479 return error;
1481 rc = TAILQ_FIRST(&t->repo_commits);
1483 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1484 if (error)
1485 goto done;
1487 error = gotweb_escape_html(&msg, rc->commit_msg);
1488 if (error)
1489 goto done;
1491 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1492 "<div id='tree_title'>Tree</div>\n"
1493 "</div>\n" /* #tree_title_wrapper */
1494 "<div id='tree_content'>\n"
1495 "<div id='tree_header_wrapper'>\n"
1496 "<div id='tree_header'>\n"
1497 "<div id='header_tree_title'>Tree:</div>\n"
1498 "<div id='header_tree'>%s</div>\n"
1499 "<div class='header_age_title'>Date:</div>\n"
1500 "<div class='header_age'>%s</div>\n"
1501 "<div id='header_commit_msg_title'>Message:</div>\n"
1502 "<div id='header_commit_msg'>%s</div>\n"
1503 "</div>\n" /* #tree_header */
1504 "</div>\n" /* #tree_header_wrapper */
1505 "<div class='dotted_line'></div>\n"
1506 "<div id='tree'>\n",
1507 rc->tree_id,
1508 age,
1509 msg);
1510 if (r == -1)
1511 goto done;
1513 error = got_output_repo_tree(c);
1514 if (error)
1515 goto done;
1517 fcgi_printf(c, "</div>\n"); /* #tree */
1518 fcgi_printf(c, "</div>\n"); /* #tree_content */
1519 done:
1520 free(age);
1521 free(msg);
1522 return error;
1525 static const struct got_error *
1526 gotweb_render_diff(struct request *c)
1528 const struct got_error *error = NULL;
1529 struct transport *t = c->t;
1530 struct repo_commit *rc = NULL;
1531 char *age = NULL, *author = NULL, *msg = NULL;
1532 int r;
1534 error = got_get_repo_commits(c, 1);
1535 if (error)
1536 return error;
1538 rc = TAILQ_FIRST(&t->repo_commits);
1540 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1541 if (error)
1542 goto done;
1543 error = gotweb_escape_html(&author, rc->author);
1544 if (error)
1545 goto done;
1546 error = gotweb_escape_html(&msg, rc->commit_msg);
1547 if (error)
1548 goto done;
1550 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1551 "<div id='diff_title'>Commit Diff</div>\n"
1552 "</div>\n" /* #diff_title_wrapper */
1553 "<div id='diff_content'>\n"
1554 "<div id='diff_header_wrapper'>\n"
1555 "<div id='diff_header'>\n"
1556 "<div id='header_diff_title'>Diff:</div>\n"
1557 "<div id='header_diff'>%s<br />%s</div>\n"
1558 "<div class='header_commit_title'>Commit:</div>\n"
1559 "<div class='header_commit'>%s</div>\n"
1560 "<div id='header_tree_title'>Tree:</div>\n"
1561 "<div id='header_tree'>%s</div>\n"
1562 "<div class='header_author_title'>Author:</div>\n"
1563 "<div class='header_author'>%s</div>\n"
1564 "<div class='header_age_title'>Date:</div>\n"
1565 "<div class='header_age'>%s</div>\n"
1566 "<div id='header_commit_msg_title'>Message:</div>\n"
1567 "<div id='header_commit_msg'>%s</div>\n"
1568 "</div>\n" /* #diff_header */
1569 "</div>\n" /* #diff_header_wrapper */
1570 "<div class='dotted_line'></div>\n"
1571 "<div id='diff'>\n",
1572 rc->parent_id, rc->commit_id,
1573 rc->commit_id,
1574 rc->tree_id,
1575 author,
1576 age,
1577 msg);
1578 if (r == -1)
1579 goto done;
1581 error = got_output_repo_diff(c);
1582 if (error)
1583 goto done;
1585 fcgi_printf(c, "</div>\n"); /* #diff */
1586 fcgi_printf(c, "</div>\n"); /* #diff_content */
1587 done:
1588 free(age);
1589 free(author);
1590 free(msg);
1591 return error;
1594 static const struct got_error *
1595 gotweb_render_summary(struct request *c)
1597 const struct got_error *error = NULL;
1598 struct transport *t = c->t;
1599 struct server *srv = c->srv;
1600 int r;
1602 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1603 goto done;
1605 if (srv->show_repo_description) {
1606 r = fcgi_printf(c,
1607 "<div id='description_title'>Description:</div>\n"
1608 "<div id='description'>%s</div>\n",
1609 t->repo_dir->description ? t->repo_dir->description : "");
1610 if (r == -1)
1611 goto done;
1614 if (srv->show_repo_owner) {
1615 r = fcgi_printf(c,
1616 "<div id='repo_owner_title'>Owner:</div>\n"
1617 "<div id='repo_owner'>%s</div>\n",
1618 t->repo_dir->owner ? t->repo_dir->owner : "");
1619 if (r == -1)
1620 goto done;
1623 if (srv->show_repo_age) {
1624 r = fcgi_printf(c,
1625 "<div id='last_change_title'>Last Change:</div>\n"
1626 "<div id='last_change'>%s</div>\n",
1627 t->repo_dir->age);
1628 if (r == -1)
1629 goto done;
1632 if (srv->show_repo_cloneurl) {
1633 r = fcgi_printf(c,
1634 "<div id='cloneurl_title'>Clone URL:</div>\n"
1635 "<div id='cloneurl'>%s</div>\n",
1636 t->repo_dir->url ? t->repo_dir->url : "");
1637 if (r == -1)
1638 goto done;
1641 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1642 if (r == -1)
1643 goto done;
1645 error = gotweb_render_briefs(c);
1646 if (error) {
1647 log_warnx("%s: %s", __func__, error->msg);
1648 goto done;
1651 error = gotweb_render_tags(c);
1652 if (error) {
1653 log_warnx("%s: %s", __func__, error->msg);
1654 goto done;
1657 error = gotweb_render_branches(c);
1658 if (error)
1659 log_warnx("%s: %s", __func__, error->msg);
1660 done:
1661 return error;
1664 static const struct got_error *
1665 gotweb_render_tag(struct request *c)
1667 const struct got_error *error = NULL;
1668 struct repo_tag *rt = NULL;
1669 struct transport *t = c->t;
1670 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1672 error = got_get_repo_tags(c, 1);
1673 if (error)
1674 goto done;
1676 if (t->tag_count == 0) {
1677 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1678 "bad commit id");
1679 goto done;
1682 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1684 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1685 if (error)
1686 goto done;
1687 error = gotweb_escape_html(&author, rt->tagger);
1688 if (error)
1689 goto done;
1690 error = gotweb_escape_html(&msg, rt->commit_msg);
1691 if (error)
1692 goto done;
1694 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1695 rt->tag_name += 5;
1696 error = gotweb_escape_html(&tagname, rt->tag_name);
1697 if (error)
1698 goto done;
1700 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1701 "<div id='tags_title'>Tag</div>\n"
1702 "</div>\n" /* #tags_title_wrapper */
1703 "<div id='tags_content'>\n"
1704 "<div id='tag_header_wrapper'>\n"
1705 "<div id='tag_header'>\n"
1706 "<div class='header_commit_title'>Commit:</div>\n"
1707 "<div class='header_commit'>%s"
1708 " <span class='refs_str'>(%s)</span></div>\n"
1709 "<div class='header_author_title'>Tagger:</div>\n"
1710 "<div class='header_author'>%s</div>\n"
1711 "<div class='header_age_title'>Date:</div>\n"
1712 "<div class='header_age'>%s</div>\n"
1713 "<div id='header_commit_msg_title'>Message:</div>\n"
1714 "<div id='header_commit_msg'>%s</div>\n"
1715 "</div>\n" /* #tag_header */
1716 "<div class='dotted_line'></div>\n"
1717 "<div id='tag_commit'>\n%s</div>"
1718 "</div>", /* tag_header_wrapper */
1719 rt->commit_id,
1720 tagname,
1721 author,
1722 age,
1723 msg,
1724 rt->tag_commit);
1726 done:
1727 free(age);
1728 free(author);
1729 free(msg);
1730 return error;
1733 static const struct got_error *
1734 gotweb_render_tags(struct request *c)
1736 const struct got_error *error = NULL;
1737 struct repo_tag *rt = NULL;
1738 struct server *srv = c->srv;
1739 struct transport *t = c->t;
1740 struct querystring *qs = t->qs;
1741 struct repo_dir *repo_dir = t->repo_dir;
1742 const char *index_page_str;
1743 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1744 int r, commit_found = 0;
1746 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1748 if (qs->action == BRIEFS) {
1749 qs->action = TAGS;
1750 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1751 } else
1752 error = got_get_repo_tags(c, srv->max_commits_display);
1753 if (error)
1754 goto done;
1756 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1757 "<div id='tags_title'>Tags</div>\n"
1758 "</div>\n" /* #tags_title_wrapper */
1759 "<div id='tags_content'>\n");
1760 if (r == -1)
1761 goto done;
1763 if (t->tag_count == 0) {
1764 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1765 "This repository contains no tags");
1766 if (r == -1)
1767 goto done;
1770 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1771 if (commit_found == 0 && qs->commit != NULL) {
1772 if (strcmp(qs->commit, rt->commit_id) != 0)
1773 continue;
1774 else
1775 commit_found = 1;
1777 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1778 if (error)
1779 goto done;
1781 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1782 rt->tag_name += 10;
1783 error = gotweb_escape_html(&tagname, rt->tag_name);
1784 if (error)
1785 goto done;
1787 if (rt->tag_commit != NULL) {
1788 newline = strchr(rt->tag_commit, '\n');
1789 if (newline)
1790 *newline = '\0';
1791 error = gotweb_escape_html(&msg, rt->tag_commit);
1792 if (error)
1793 goto done;
1796 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1797 "<div class='tag'>%s</div>\n"
1798 "<div class='tag_log'>"
1799 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1800 "%s</a>"
1801 "</div>\n" /* .tag_log */
1802 "<div class='navs_wrapper'>\n"
1803 "<div class='navs'>"
1804 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1805 "tag</a>"
1806 " | "
1807 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1808 "commit briefs</a>"
1809 " | "
1810 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1811 "commits</a>"
1812 "</div>\n" /* .navs */
1813 "</div>\n" /* .navs_wrapper */
1814 "<div class='dotted_line'></div>\n",
1815 age,
1816 tagname,
1817 index_page_str, repo_dir->name, rt->commit_id,
1818 msg ? msg : "",
1819 index_page_str, repo_dir->name, rt->commit_id,
1820 index_page_str, repo_dir->name, rt->commit_id,
1821 index_page_str, repo_dir->name, rt->commit_id);
1822 if (r == -1)
1823 goto done;
1825 free(age);
1826 age = NULL;
1827 free(tagname);
1828 tagname = NULL;
1829 free(msg);
1830 msg = NULL;
1832 if (t->next_id || t->prev_id) {
1833 error = gotweb_render_navs(c);
1834 if (error)
1835 goto done;
1837 fcgi_printf(c, "</div>\n"); /* #tags_content */
1838 done:
1839 free(age);
1840 free(tagname);
1841 free(msg);
1842 return error;
1845 const struct got_error *
1846 gotweb_escape_html(char **escaped_html, const char *orig_html)
1848 const struct got_error *error = NULL;
1849 struct escape_pair {
1850 char c;
1851 const char *s;
1852 } esc[] = {
1853 { '>', "&gt;" },
1854 { '<', "&lt;" },
1855 { '&', "&amp;" },
1856 { '"', "&quot;" },
1857 { '\'', "&apos;" },
1858 { '\n', "<br />" },
1860 size_t orig_len, len;
1861 int i, j, x;
1863 orig_len = strlen(orig_html);
1864 len = orig_len;
1865 for (i = 0; i < orig_len; i++) {
1866 for (j = 0; j < nitems(esc); j++) {
1867 if (orig_html[i] != esc[j].c)
1868 continue;
1869 len += strlen(esc[j].s) - 1 /* escaped char */;
1873 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1874 if (*escaped_html == NULL)
1875 return got_error_from_errno("calloc");
1877 x = 0;
1878 for (i = 0; i < orig_len; i++) {
1879 int escaped = 0;
1880 for (j = 0; j < nitems(esc); j++) {
1881 if (orig_html[i] != esc[j].c)
1882 continue;
1884 if (strlcat(*escaped_html, esc[j].s, len + 1)
1885 >= len + 1) {
1886 error = got_error(GOT_ERR_NO_SPACE);
1887 goto done;
1889 x += strlen(esc[j].s);
1890 escaped = 1;
1891 break;
1893 if (!escaped) {
1894 (*escaped_html)[x] = orig_html[i];
1895 x++;
1898 done:
1899 if (error) {
1900 free(*escaped_html);
1901 *escaped_html = NULL;
1902 } else {
1903 (*escaped_html)[x] = '\0';
1906 return error;
1909 static struct got_repository *
1910 find_cached_repo(struct server *srv, const char *path)
1912 int i;
1914 for (i = 0; i < srv->ncached_repos; i++) {
1915 if (strcmp(srv->cached_repos[i].path, path) == 0)
1916 return srv->cached_repos[i].repo;
1919 return NULL;
1922 static const struct got_error *
1923 cache_repo(struct got_repository **new, struct server *srv,
1924 struct repo_dir *repo_dir, struct socket *sock)
1926 const struct got_error *error = NULL;
1927 struct got_repository *repo;
1928 struct cached_repo *cr;
1929 int evicted = 0;
1931 if (srv->ncached_repos >= nitems(srv->cached_repos)) {
1932 cr = &srv->cached_repos[srv->ncached_repos - 1];
1933 error = got_repo_close(cr->repo);
1934 memset(cr, 0, sizeof(*cr));
1935 srv->ncached_repos--;
1936 if (error)
1937 return error;
1938 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1939 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1940 cr = &srv->cached_repos[0];
1941 evicted = 1;
1942 } else {
1943 cr = &srv->cached_repos[srv->ncached_repos];
1946 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1947 if (error) {
1948 if (evicted) {
1949 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1950 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1952 return error;
1955 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1956 >= sizeof(cr->path)) {
1957 if (evicted) {
1958 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1959 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1961 return got_error(GOT_ERR_NO_SPACE);
1964 cr->repo = repo;
1965 srv->ncached_repos++;
1966 *new = repo;
1967 return NULL;
1970 static const struct got_error *
1971 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1973 const struct got_error *error = NULL;
1974 struct socket *sock = c->sock;
1975 struct server *srv = c->srv;
1976 struct transport *t = c->t;
1977 struct got_repository *repo = NULL;
1978 DIR *dt;
1979 char *dir_test;
1981 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1982 GOTWEB_GIT_DIR) == -1)
1983 return got_error_from_errno("asprintf");
1985 dt = opendir(dir_test);
1986 if (dt == NULL) {
1987 free(dir_test);
1988 } else {
1989 repo_dir->path = dir_test;
1990 dir_test = NULL;
1991 goto done;
1994 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1995 repo_dir->name) == -1)
1996 return got_error_from_errno("asprintf");
1998 dt = opendir(dir_test);
1999 if (dt == NULL) {
2000 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2001 goto err;
2002 } else {
2003 repo_dir->path = dir_test;
2004 dir_test = NULL;
2007 done:
2008 repo = find_cached_repo(srv, repo_dir->path);
2009 if (repo == NULL) {
2010 error = cache_repo(&repo, srv, repo_dir, sock);
2011 if (error)
2012 goto err;
2014 t->repo = repo;
2015 error = gotweb_get_repo_description(&repo_dir->description, srv,
2016 repo_dir->path);
2017 if (error)
2018 goto err;
2019 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2020 if (error)
2021 goto err;
2022 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2023 NULL, TM_DIFF);
2024 if (error)
2025 goto err;
2026 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2027 err:
2028 free(dir_test);
2029 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2030 error = got_error_from_errno("closedir");
2031 return error;
2034 static const struct got_error *
2035 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2037 const struct got_error *error;
2039 *repo_dir = calloc(1, sizeof(**repo_dir));
2040 if (*repo_dir == NULL)
2041 return got_error_from_errno("calloc");
2043 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2044 error = got_error_from_errno("asprintf");
2045 free(*repo_dir);
2046 *repo_dir = NULL;
2047 return error;
2049 (*repo_dir)->owner = NULL;
2050 (*repo_dir)->description = NULL;
2051 (*repo_dir)->url = NULL;
2052 (*repo_dir)->age = NULL;
2053 (*repo_dir)->path = NULL;
2055 return NULL;
2058 static const struct got_error *
2059 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2061 const struct got_error *error = NULL;
2062 FILE *f = NULL;
2063 char *d_file = NULL;
2064 unsigned int len;
2065 size_t n;
2067 *description = NULL;
2068 if (srv->show_repo_description == 0)
2069 return NULL;
2071 if (asprintf(&d_file, "%s/description", dir) == -1)
2072 return got_error_from_errno("asprintf");
2074 f = fopen(d_file, "r");
2075 if (f == NULL) {
2076 if (errno == ENOENT || errno == EACCES)
2077 return NULL;
2078 error = got_error_from_errno2("fopen", d_file);
2079 goto done;
2082 if (fseek(f, 0, SEEK_END) == -1) {
2083 error = got_ferror(f, GOT_ERR_IO);
2084 goto done;
2086 len = ftell(f);
2087 if (len == -1) {
2088 error = got_ferror(f, GOT_ERR_IO);
2089 goto done;
2092 if (len == 0) {
2093 *description = strdup("");
2094 if (*description == NULL)
2095 return got_error_from_errno("strdup");
2096 goto done;
2099 if (fseek(f, 0, SEEK_SET) == -1) {
2100 error = got_ferror(f, GOT_ERR_IO);
2101 goto done;
2103 *description = calloc(len + 1, sizeof(**description));
2104 if (*description == NULL) {
2105 error = got_error_from_errno("calloc");
2106 goto done;
2109 n = fread(*description, 1, len, f);
2110 if (n == 0 && ferror(f))
2111 error = got_ferror(f, GOT_ERR_IO);
2112 done:
2113 if (f != NULL && fclose(f) == EOF && error == NULL)
2114 error = got_error_from_errno("fclose");
2115 free(d_file);
2116 return error;
2119 static const struct got_error *
2120 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2122 const struct got_error *error = NULL;
2123 FILE *f;
2124 char *d_file = NULL;
2125 unsigned int len;
2126 size_t n;
2128 *url = NULL;
2130 if (srv->show_repo_cloneurl == 0)
2131 return NULL;
2133 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2134 return got_error_from_errno("asprintf");
2136 f = fopen(d_file, "r");
2137 if (f == NULL) {
2138 if (errno != ENOENT && errno != EACCES)
2139 error = got_error_from_errno2("fopen", d_file);
2140 goto done;
2143 if (fseek(f, 0, SEEK_END) == -1) {
2144 error = got_ferror(f, GOT_ERR_IO);
2145 goto done;
2147 len = ftell(f);
2148 if (len == -1) {
2149 error = got_ferror(f, GOT_ERR_IO);
2150 goto done;
2152 if (len == 0)
2153 goto done;
2155 if (fseek(f, 0, SEEK_SET) == -1) {
2156 error = got_ferror(f, GOT_ERR_IO);
2157 goto done;
2160 *url = calloc(len + 1, sizeof(**url));
2161 if (*url == NULL) {
2162 error = got_error_from_errno("calloc");
2163 goto done;
2166 n = fread(*url, 1, len, f);
2167 if (n == 0 && ferror(f))
2168 error = got_ferror(f, GOT_ERR_IO);
2169 done:
2170 if (f != NULL && fclose(f) == EOF && error == NULL)
2171 error = got_error_from_errno("fclose");
2172 free(d_file);
2173 return error;
2176 const struct got_error *
2177 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2179 struct tm tm;
2180 long long diff_time;
2181 const char *years = "years ago", *months = "months ago";
2182 const char *weeks = "weeks ago", *days = "days ago";
2183 const char *hours = "hours ago", *minutes = "minutes ago";
2184 const char *seconds = "seconds ago", *now = "right now";
2185 char *s;
2186 char datebuf[29];
2188 *repo_age = NULL;
2190 switch (ref_tm) {
2191 case TM_DIFF:
2192 diff_time = time(NULL) - committer_time;
2193 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2194 if (asprintf(repo_age, "%lld %s",
2195 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2196 return got_error_from_errno("asprintf");
2197 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2198 if (asprintf(repo_age, "%lld %s",
2199 (diff_time / 60 / 60 / 24 / (365 / 12)),
2200 months) == -1)
2201 return got_error_from_errno("asprintf");
2202 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2203 if (asprintf(repo_age, "%lld %s",
2204 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2205 return got_error_from_errno("asprintf");
2206 } else if (diff_time > 60 * 60 * 24 * 2) {
2207 if (asprintf(repo_age, "%lld %s",
2208 (diff_time / 60 / 60 / 24), days) == -1)
2209 return got_error_from_errno("asprintf");
2210 } else if (diff_time > 60 * 60 * 2) {
2211 if (asprintf(repo_age, "%lld %s",
2212 (diff_time / 60 / 60), hours) == -1)
2213 return got_error_from_errno("asprintf");
2214 } else if (diff_time > 60 * 2) {
2215 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2216 minutes) == -1)
2217 return got_error_from_errno("asprintf");
2218 } else if (diff_time > 2) {
2219 if (asprintf(repo_age, "%lld %s", diff_time,
2220 seconds) == -1)
2221 return got_error_from_errno("asprintf");
2222 } else {
2223 if (asprintf(repo_age, "%s", now) == -1)
2224 return got_error_from_errno("asprintf");
2226 break;
2227 case TM_LONG:
2228 if (gmtime_r(&committer_time, &tm) == NULL)
2229 return got_error_from_errno("gmtime_r");
2231 s = asctime_r(&tm, datebuf);
2232 if (s == NULL)
2233 return got_error_from_errno("asctime_r");
2235 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2236 return got_error_from_errno("asprintf");
2237 break;
2239 return NULL;