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 <imsg.h>
30 #include <sha1.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
48 #include "proc.h"
49 #include "gotwebd.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 (c->t->repo != NULL && qs && qs->action != INDEX)
296 got_repo_close(c->t->repo);
297 if (html && srv != NULL)
298 gotweb_render_footer(c);
301 struct server *
302 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
304 struct server *srv = NULL;
306 /* check against the server name first */
307 if (strlen(server_name) > 0)
308 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
309 if (strcmp(srv->name, server_name) == 0)
310 goto done;
312 /* check against subdomain second */
313 if (strlen(subdomain) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, subdomain) == 0)
316 goto done;
318 /* if those fail, send first server */
319 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
320 if (srv != NULL)
321 break;
322 done:
323 return srv;
324 };
326 const struct got_error *
327 gotweb_init_transport(struct transport **t)
329 const struct got_error *error = NULL;
331 *t = calloc(1, sizeof(**t));
332 if (*t == NULL)
333 return got_error_from_errno2("%s: calloc", __func__);
335 TAILQ_INIT(&(*t)->repo_commits);
336 TAILQ_INIT(&(*t)->repo_tags);
338 (*t)->repo = NULL;
339 (*t)->repo_dir = NULL;
340 (*t)->qs = NULL;
341 (*t)->next_id = NULL;
342 (*t)->prev_id = NULL;
343 (*t)->next_disp = 0;
344 (*t)->prev_disp = 0;
346 return error;
349 static const struct got_error *
350 gotweb_init_querystring(struct querystring **qs)
352 const struct got_error *error = NULL;
354 *qs = calloc(1, sizeof(**qs));
355 if (*qs == NULL)
356 return got_error_from_errno2("%s: calloc", __func__);
358 (*qs)->action = INDEX;
359 (*qs)->commit = NULL;
360 (*qs)->file = NULL;
361 (*qs)->folder = NULL;
362 (*qs)->headref = strdup("HEAD");
363 if ((*qs)->headref == NULL) {
364 return got_error_from_errno2("%s: strdup", __func__);
366 (*qs)->index_page = 0;
367 (*qs)->index_page_str = NULL;
368 (*qs)->path = NULL;
370 return error;
373 static const struct got_error *
374 gotweb_parse_querystring(struct querystring **qs, char *qst)
376 const struct got_error *error = NULL;
377 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
378 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
380 if (qst == NULL)
381 return error;
383 tok1 = strdup(qst);
384 if (tok1 == NULL)
385 return got_error_from_errno2("%s: strdup", __func__);
387 tok1_pair = tok1;
388 tok1_end = tok1;
390 while (tok1_pair != NULL) {
391 strsep(&tok1_end, "&");
393 tok2 = strdup(tok1_pair);
394 if (tok2 == NULL) {
395 free(tok1);
396 return got_error_from_errno2("%s: strdup", __func__);
399 tok2_pair = tok2;
400 tok2_end = tok2;
402 while (tok2_pair != NULL) {
403 strsep(&tok2_end, "=");
404 if (tok2_end) {
405 error = gotweb_assign_querystring(qs, tok2_pair,
406 tok2_end);
407 if (error)
408 goto err;
410 tok2_pair = tok2_end;
412 free(tok2);
413 tok1_pair = tok1_end;
415 free(tok1);
416 return error;
417 err:
418 free(tok2);
419 free(tok1);
420 return error;
423 static const struct got_error *
424 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
426 const struct got_error *error = NULL;
427 const char *errstr;
428 int a_cnt, el_cnt;
430 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
431 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
432 continue;
434 switch (querystring_keys[el_cnt].element) {
435 case ACTION:
436 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
437 if (strcmp(value, action_keys[a_cnt].name) != 0)
438 continue;
439 else if (strcmp(value,
440 action_keys[a_cnt].name) == 0){
441 (*qs)->action =
442 action_keys[a_cnt].action;
443 goto qa_found;
446 (*qs)->action = ERR;
447 qa_found:
448 break;
449 case COMMIT:
450 (*qs)->commit = strdup(value);
451 if ((*qs)->commit == NULL) {
452 error = got_error_from_errno2("%s: strdup",
453 __func__);
454 goto done;
456 break;
457 case RFILE:
458 (*qs)->file = strdup(value);
459 if ((*qs)->file == NULL) {
460 error = got_error_from_errno2("%s: strdup",
461 __func__);
462 goto done;
464 break;
465 case FOLDER:
466 (*qs)->folder = strdup(value);
467 if ((*qs)->folder == NULL) {
468 error = got_error_from_errno2("%s: strdup",
469 __func__);
470 goto done;
472 break;
473 case HEADREF:
474 (*qs)->headref = strdup(value);
475 if ((*qs)->headref == NULL) {
476 error = got_error_from_errno2("%s: strdup",
477 __func__);
478 goto done;
480 break;
481 case INDEX_PAGE:
482 if (strlen(value) == 0)
483 break;
484 (*qs)->index_page_str = strdup(value);
485 if ((*qs)->index_page_str == NULL) {
486 error = got_error_from_errno2("%s: strdup",
487 __func__);
488 goto done;
490 (*qs)->index_page = strtonum(value, INT64_MIN,
491 INT64_MAX, &errstr);
492 if (errstr) {
493 error = got_error_from_errno3("%s: strtonum %s",
494 __func__, errstr);
495 goto done;
497 if ((*qs)->index_page < 0) {
498 (*qs)->index_page = 0;
499 sprintf((*qs)->index_page_str, "%d", 0);
501 break;
502 case PATH:
503 (*qs)->path = strdup(value);
504 if ((*qs)->path == NULL) {
505 error = got_error_from_errno2("%s: strdup",
506 __func__);
507 goto done;
509 break;
510 case PAGE:
511 if (strlen(value) == 0)
512 break;
513 (*qs)->page_str = strdup(value);
514 if ((*qs)->page_str == NULL) {
515 error = got_error_from_errno2("%s: strdup",
516 __func__);
517 goto done;
519 (*qs)->page = strtonum(value, INT64_MIN,
520 INT64_MAX, &errstr);
521 if (errstr) {
522 error = got_error_from_errno3("%s: strtonum %s",
523 __func__, errstr);
524 goto done;
526 if ((*qs)->page < 0) {
527 (*qs)->page = 0;
528 sprintf((*qs)->page_str, "%d", 0);
530 break;
531 default:
532 break;
535 done:
536 return error;
539 void
540 gotweb_free_repo_tag(struct repo_tag *rt)
542 if (rt != NULL) {
543 free(rt->commit_msg);
544 free(rt->commit_id);
545 free(rt->tagger);
547 free(rt);
550 void
551 gotweb_free_repo_commit(struct repo_commit *rc)
553 if (rc != NULL) {
554 free(rc->path);
555 free(rc->refs_str);
556 free(rc->commit_id);
557 free(rc->parent_id);
558 free(rc->tree_id);
559 free(rc->author);
560 free(rc->committer);
561 free(rc->commit_msg);
563 free(rc);
566 static void
567 gotweb_free_querystring(struct querystring *qs)
569 if (qs != NULL) {
570 free(qs->commit);
571 free(qs->file);
572 free(qs->folder);
573 free(qs->headref);
574 free(qs->index_page_str);
575 free(qs->path);
576 free(qs->page_str);
578 free(qs);
581 static void
582 gotweb_free_repo_dir(struct repo_dir *repo_dir)
584 if (repo_dir != NULL) {
585 free(repo_dir->name);
586 free(repo_dir->owner);
587 free(repo_dir->description);
588 free(repo_dir->url);
589 free(repo_dir->age);
590 free(repo_dir->path);
592 free(repo_dir);
595 void
596 gotweb_free_transport(struct transport *t)
598 struct repo_commit *rc = NULL, *trc = NULL;
599 struct repo_tag *rt = NULL, *trt = NULL;
601 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
602 TAILQ_REMOVE(&t->repo_commits, rc, entry);
603 gotweb_free_repo_commit(rc);
605 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
606 TAILQ_REMOVE(&t->repo_tags, rt, entry);
607 gotweb_free_repo_tag(rt);
609 gotweb_free_repo_dir(t->repo_dir);
610 gotweb_free_querystring(t->qs);
611 if (t != NULL) {
612 free(t->next_id);
613 free(t->prev_id);
615 free(t);
618 const struct got_error *
619 gotweb_render_content_type(struct request *c, const uint8_t *type)
621 const char *csp = "default-src 'self'; script-src 'none'; "
622 "object-src 'none';";
624 fcgi_printf(c,
625 "Content-Security-Policy: %s\r\n"
626 "Content-Type: %s\r\n\r\n",
627 csp, type);
628 return NULL;
631 const struct got_error *
632 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
633 char *file)
635 fcgi_printf(c, "Content-type: %s\r\n"
636 "Content-disposition: attachment; filename=%s\r\n\r\n",
637 type, file);
638 return NULL;
641 static const struct got_error *
642 gotweb_render_header(struct request *c)
644 struct server *srv = c->srv;
645 struct querystring *qs = c->t->qs;
646 int r;
648 r = fcgi_printf(c, "<!doctype html>\n"
649 "<html>\n"
650 "<head>\n"
651 "<title>%s</title>\n"
652 "<meta charset='utf-8' />\n"
653 "<meta name='viewport' content='initial-scale=.75' />\n"
654 "<meta name='msapplication-TileColor' content='#da532c' />\n"
655 "<meta name='theme-color' content='#ffffff'/>\n"
656 "<link rel='apple-touch-icon' sizes='180x180'"
657 " href='/apple-touch-icon.png' />\n"
658 "<link rel='icon' type='image/png' sizes='32x32'"
659 " href='/favicon-32x32.png' />\n"
660 "<link rel='icon' type='image/png' sizes='16x16'"
661 " href='/favicon-16x16.png' />\n"
662 "<link rel='manifest' href='/site.webmanifest'/>\n"
663 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
664 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
665 "</head>\n"
666 "<body>\n"
667 "<div id='gw_body'>\n"
668 "<div id='header'>\n"
669 "<div id='got_link'>"
670 "<a href='%s' target='_blank'>"
671 "<img src='%s%s' alt='logo' id='logo' />"
672 "</a>\n"
673 "</div>\n" /* #got_link */
674 "</div>\n" /* #header */
675 "<div id='site_path'>\n"
676 "<div id='site_link'>\n"
677 "<a href='?index_page=%d'>%s</a>",
678 srv->site_name,
679 c->script_name, srv->custom_css,
680 srv->logo_url,
681 c->script_name, srv->logo,
682 qs->index_page, srv->site_link);
683 if (r == -1)
684 goto done;
686 if (qs != NULL) {
687 if (qs->path != NULL) {
688 r = fcgi_printf(c, " / "
689 "<a href='?index_page=%d&path=%s&action=summary'>"
690 "%s</a>",
691 qs->index_page, qs->path, qs->path);
692 if (r == -1)
693 goto done;
695 if (qs->action != INDEX) {
696 const char *action = "";
698 switch (qs->action) {
699 case BLAME:
700 action = "blame";
701 break;
702 case BRIEFS:
703 action = "briefs";
704 break;
705 case COMMITS:
706 action = "commits";
707 break;
708 case DIFF:
709 action = "diff";
710 break;
711 case SUMMARY:
712 action = "summary";
713 break;
714 case TAG:
715 action = "tag";
716 break;
717 case TAGS:
718 action = "tags";
719 break;
720 case TREE:
721 action = "tree";
722 break;
725 if (fcgi_printf(c, " / %s", action) == -1)
726 goto done;
730 fcgi_printf(c, "</div>\n" /* #site_path */
731 "</div>\n" /* #site_link */
732 "<div id='content'>\n");
734 done:
735 return NULL;
738 static const struct got_error *
739 gotweb_render_footer(struct request *c)
741 const struct got_error *error = NULL;
742 struct server *srv = c->srv;
743 const char *siteowner = "&nbsp;";
744 char *escaped_owner = NULL;
746 if (srv->show_site_owner) {
747 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
748 if (error)
749 return error;
750 siteowner = escaped_owner;
753 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
754 "<div id='site_owner'>%s</div>\n"
755 "</div>\n" /* #site_owner_wrapper */
756 "</div>\n" /* #content */
757 "</div>\n" /* #gw_body */
758 "</body>\n</html>\n", siteowner);
760 free(escaped_owner);
761 return NULL;
764 static const struct got_error *
765 gotweb_render_navs(struct request *c)
767 const struct got_error *error = NULL;
768 struct transport *t = c->t;
769 struct querystring *qs = t->qs;
770 struct server *srv = c->srv;
771 char *nhref = NULL, *phref = NULL;
772 int r, disp = 0;
774 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
775 if (r == -1)
776 goto done;
778 switch(qs->action) {
779 case INDEX:
780 if (qs->index_page > 0) {
781 if (asprintf(&phref, "index_page=%d",
782 qs->index_page - 1) == -1) {
783 error = got_error_from_errno2("%s: asprintf",
784 __func__);
785 goto done;
787 disp = 1;
789 break;
790 case BRIEFS:
791 if (t->prev_id && qs->commit != NULL &&
792 strcmp(qs->commit, t->prev_id) != 0) {
793 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
794 "&action=briefs&commit=%s&headref=%s",
795 qs->index_page, qs->path, qs->page - 1, t->prev_id,
796 qs->headref) == -1) {
797 error = got_error_from_errno2("%s: asprintf",
798 __func__);
799 goto done;
801 disp = 1;
803 break;
804 case COMMITS:
805 if (t->prev_id && qs->commit != NULL &&
806 strcmp(qs->commit, t->prev_id) != 0) {
807 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
808 "&action=commits&commit=%s&headref=%s&folder=%s"
809 "&file=%s",
810 qs->index_page, qs->path, qs->page - 1, t->prev_id,
811 qs->headref, qs->folder ? qs->folder : "",
812 qs->file ? qs->file : "") == -1) {
813 error = got_error_from_errno2("%s: asprintf",
814 __func__);
815 goto done;
817 disp = 1;
819 break;
820 case TAGS:
821 if (t->prev_id && qs->commit != NULL &&
822 strcmp(qs->commit, t->prev_id) != 0) {
823 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
824 "&action=tags&commit=%s&headref=%s",
825 qs->index_page, qs->path, qs->page - 1, t->prev_id,
826 qs->headref) == -1) {
827 error = got_error_from_errno2("%s: asprintf",
828 __func__);
829 goto done;
831 disp = 1;
833 break;
834 default:
835 disp = 0;
836 break;
839 if (disp) {
840 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
841 if (r == -1)
842 goto done;
845 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
846 "<div id='nav_next'>");
847 if (r == -1)
848 goto done;
850 disp = 0;
851 switch(qs->action) {
852 case INDEX:
853 if (t->next_disp == srv->max_repos_display &&
854 t->repos_total != (qs->index_page + 1) *
855 srv->max_repos_display) {
856 if (asprintf(&nhref, "index_page=%d",
857 qs->index_page + 1) == -1) {
858 error = got_error_from_errno2("%s: asprintf",
859 __func__);
860 goto done;
862 disp = 1;
864 break;
865 case BRIEFS:
866 if (t->next_id) {
867 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
868 "&action=briefs&commit=%s&headref=%s",
869 qs->index_page, qs->path, qs->page + 1, t->next_id,
870 qs->headref) == -1) {
871 error = got_error_from_errno2("%s: asprintf",
872 __func__);
873 goto done;
875 disp = 1;
877 break;
878 case COMMITS:
879 if (t->next_id) {
880 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
881 "&action=commits&commit=%s&headref=%s&folder=%s"
882 "&file=%s",
883 qs->index_page, qs->path, qs->page + 1, t->next_id,
884 qs->headref, qs->folder ? qs->folder : "",
885 qs->file ? qs->file : "") == -1) {
886 error = got_error_from_errno2("%s: asprintf",
887 __func__);
888 goto done;
890 disp = 1;
892 break;
893 case TAGS:
894 if (t->next_id) {
895 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
896 "&action=tags&commit=%s&headref=%s",
897 qs->index_page, qs->path, qs->page + 1, t->next_id,
898 qs->headref) == -1) {
899 error = got_error_from_errno2("%s: asprintf",
900 __func__);
901 goto done;
903 disp = 1;
905 break;
906 default:
907 disp = 0;
908 break;
910 if (disp) {
911 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
912 if (r == -1)
913 goto done;
915 fcgi_printf(c, "</div>\n"); /* #nav_next */
916 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
917 done:
918 free(t->next_id);
919 t->next_id = NULL;
920 free(t->prev_id);
921 t->prev_id = NULL;
922 free(phref);
923 free(nhref);
924 return error;
927 static const struct got_error *
928 gotweb_render_index(struct request *c)
930 const struct got_error *error = NULL;
931 struct server *srv = c->srv;
932 struct transport *t = c->t;
933 struct querystring *qs = t->qs;
934 struct repo_dir *repo_dir = NULL;
935 DIR *d;
936 struct dirent **sd_dent;
937 const char *index_page_str;
938 char *c_path = NULL;
939 struct stat st;
940 unsigned int d_cnt, d_i, d_disp = 0;
941 int r;
943 index_page_str = qs->index_page_str ? qs->index_page_str : "";
945 d = opendir(srv->repos_path);
946 if (d == NULL) {
947 error = got_error_from_errno2("opendir", srv->repos_path);
948 return error;
951 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
952 if (d_cnt == -1) {
953 error = got_error_from_errno2("scandir", srv->repos_path);
954 goto done;
957 /* get total count of repos */
958 for (d_i = 0; d_i < d_cnt; d_i++) {
959 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
960 strcmp(sd_dent[d_i]->d_name, "..") == 0)
961 continue;
963 if (asprintf(&c_path, "%s/%s", srv->repos_path,
964 sd_dent[d_i]->d_name) == -1) {
965 error = got_error_from_errno("asprintf");
966 return error;
969 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
970 !got_path_dir_is_empty(c_path))
971 t->repos_total++;
972 free(c_path);
973 c_path = NULL;
976 r = fcgi_printf(c, "<div id='index_header'>\n"
977 "<div id='index_header_project'>Project</div>\n");
978 if (r == -1)
979 goto done;
981 if (srv->show_repo_description)
982 if (fcgi_printf(c, "<div id='index_header_description'>"
983 "Description</div>\n") == -1)
984 goto done;
985 if (srv->show_repo_owner)
986 if (fcgi_printf(c, "<div id='index_header_owner'>"
987 "Owner</div>\n") == -1)
988 goto done;
989 if (srv->show_repo_age)
990 if (fcgi_printf(c, "<div id='index_header_age'>"
991 "Last Change</div>\n") == -1)
992 goto done;
993 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
994 goto done;
996 for (d_i = 0; d_i < d_cnt; d_i++) {
997 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
998 break; /* account for parent and self */
1000 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1001 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1002 continue;
1004 if (qs->index_page > 0 && (qs->index_page *
1005 srv->max_repos_display) > t->prev_disp) {
1006 t->prev_disp++;
1007 continue;
1010 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1011 if (error)
1012 goto done;
1014 error = gotweb_load_got_path(c, repo_dir);
1015 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1016 error = NULL;
1017 continue;
1019 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1020 goto done;
1022 if (lstat(repo_dir->path, &st) == 0 &&
1023 S_ISDIR(st.st_mode) &&
1024 !got_path_dir_is_empty(repo_dir->path))
1025 goto render;
1026 else {
1027 gotweb_free_repo_dir(repo_dir);
1028 repo_dir = NULL;
1029 continue;
1031 render:
1032 d_disp++;
1033 t->prev_disp++;
1035 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1036 "<div class='index_project'>"
1037 "<a href='?index_page=%s&path=%s&action=summary'>"
1038 " %s "
1039 "</a>"
1040 "</div>", /* .index_project */
1041 index_page_str, repo_dir->name,
1042 repo_dir->name);
1043 if (r == -1)
1044 goto done;
1046 if (srv->show_repo_description) {
1047 r = fcgi_printf(c,
1048 "<div class='index_project_description'>\n"
1049 "%s</div>\n", repo_dir->description);
1050 if (r == -1)
1051 goto done;
1054 if (srv->show_repo_owner) {
1055 r = fcgi_printf(c, "<div class='index_project_owner'>"
1056 "%s</div>\n", repo_dir->owner);
1057 if (r == -1)
1058 goto done;
1061 if (srv->show_repo_age) {
1062 r = fcgi_printf(c, "<div class='index_project_age'>"
1063 "%s</div>\n", repo_dir->age);
1064 if (r == -1)
1065 goto done;
1068 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1069 "<div class='navs'>"
1070 "<a href='?index_page=%s&path=%s&action=summary'>"
1071 "summary"
1072 "</a> | "
1073 "<a href='?index_page=%s&path=%s&action=briefs'>"
1074 "commit briefs"
1075 "</a> | "
1076 "<a href='?index_page=%s&path=%s&action=commits'>"
1077 "commits"
1078 "</a> | "
1079 "<a href='?index_page=%s&path=%s&action=tags'>"
1080 "tags"
1081 "</a> | "
1082 "<a href='?index_page=%s&path=%s&action=tree'>"
1083 "tree"
1084 "</a>"
1085 "</div>" /* .navs */
1086 "<div class='dotted_line'></div>\n"
1087 "</div>\n" /* .navs_wrapper */
1088 "</div>\n", /* .index_wrapper */
1089 index_page_str, repo_dir->name,
1090 index_page_str, repo_dir->name,
1091 index_page_str, repo_dir->name,
1092 index_page_str, repo_dir->name,
1093 index_page_str, repo_dir->name);
1094 if (r == -1)
1095 goto done;
1097 gotweb_free_repo_dir(repo_dir);
1098 repo_dir = NULL;
1099 error = got_repo_close(t->repo);
1100 if (error)
1101 goto done;
1102 t->next_disp++;
1103 if (d_disp == srv->max_repos_display)
1104 break;
1106 if (srv->max_repos_display == 0)
1107 goto done;
1108 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1109 goto done;
1110 if (t->repos_total <= srv->max_repos ||
1111 t->repos_total <= srv->max_repos_display)
1112 goto done;
1114 error = gotweb_render_navs(c);
1115 if (error)
1116 goto done;
1117 done:
1118 if (d != NULL && closedir(d) == EOF && error == NULL)
1119 error = got_error_from_errno("closedir");
1120 return error;
1123 static const struct got_error *
1124 gotweb_render_blame(struct request *c)
1126 const struct got_error *error = NULL;
1127 struct transport *t = c->t;
1128 struct repo_commit *rc = NULL;
1129 char *age = NULL, *msg = NULL;
1130 int r;
1132 error = got_get_repo_commits(c, 1);
1133 if (error)
1134 return error;
1136 rc = TAILQ_FIRST(&t->repo_commits);
1138 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1139 if (error)
1140 goto done;
1141 error = gotweb_escape_html(&msg, rc->commit_msg);
1142 if (error)
1143 goto done;
1145 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1146 "<div id='blame_title'>Blame</div>\n"
1147 "</div>\n" /* #blame_title_wrapper */
1148 "<div id='blame_content'>\n"
1149 "<div id='blame_header_wrapper'>\n"
1150 "<div id='blame_header'>\n"
1151 "<div class='header_age_title'>Date:</div>\n"
1152 "<div class='header_age'>%s</div>\n"
1153 "<div id='header_commit_msg_title'>Message:</div>\n"
1154 "<div id='header_commit_msg'>%s</div>\n"
1155 "</div>\n" /* #blame_header */
1156 "</div>\n" /* #blame_header_wrapper */
1157 "<div class='dotted_line'></div>\n"
1158 "<div id='blame'>\n",
1159 age ? age : "",
1160 msg);
1161 if (r == -1)
1162 goto done;
1164 error = got_output_file_blame(c);
1165 if (error)
1166 goto done;
1168 fcgi_printf(c, "</div>\n" /* #blame */
1169 "</div>\n"); /* #blame_content */
1170 done:
1171 free(msg);
1172 return error;
1175 static const struct got_error *
1176 gotweb_render_briefs(struct request *c)
1178 const struct got_error *error = NULL;
1179 struct repo_commit *rc = NULL;
1180 struct server *srv = c->srv;
1181 struct transport *t = c->t;
1182 struct querystring *qs = t->qs;
1183 struct repo_dir *repo_dir = t->repo_dir;
1184 const char *index_page_str;
1185 char *smallerthan, *newline;
1186 char *age = NULL, *author = NULL, *msg = NULL;
1187 int r;
1189 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1191 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1192 "<div id='briefs_title'>Commit Briefs</div>\n"
1193 "</div>\n" /* #briefs_title_wrapper */
1194 "<div id='briefs_content'>\n");
1195 if (r == -1)
1196 goto done;
1198 if (qs->action == SUMMARY) {
1199 qs->action = BRIEFS;
1200 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1201 } else
1202 error = got_get_repo_commits(c, srv->max_commits_display);
1203 if (error)
1204 goto done;
1206 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1207 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1208 if (error)
1209 goto done;
1211 smallerthan = strchr(rc->author, '<');
1212 if (smallerthan)
1213 *smallerthan = '\0';
1215 newline = strchr(rc->commit_msg, '\n');
1216 if (newline)
1217 *newline = '\0';
1219 error = gotweb_escape_html(&author, rc->author);
1220 if (error)
1221 goto done;
1222 error = gotweb_escape_html(&msg, rc->commit_msg);
1223 if (error)
1224 goto done;
1226 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1227 "<div class='briefs_author'>%s</div>\n"
1228 "<div class='briefs_log'>"
1229 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1230 "&headref=%s'>%s</a>",
1231 age ? age : "",
1232 author,
1233 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1234 msg);
1235 if (r == -1)
1236 goto done;
1238 if (rc->refs_str) {
1239 char *refs;
1241 error = gotweb_escape_html(&refs, rc->refs_str);
1242 if (error)
1243 goto done;
1244 r = fcgi_printf(c,
1245 " <span class='refs_str'>(%s)</span>", refs);
1246 free(refs);
1247 if (r == -1)
1248 goto done;
1250 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1251 goto done;
1253 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1254 "<div class='navs'>"
1255 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1256 "&headref=%s'>diff</a>"
1257 " | "
1258 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1259 "&headref=%s'>tree</a>"
1260 "</div>\n" /* .navs */
1261 "</div>\n" /* .navs_wrapper */
1262 "<div class='dotted_line'></div>\n",
1263 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1264 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1265 if (r == -1)
1266 goto done;
1268 free(age);
1269 age = NULL;
1270 free(author);
1271 author = NULL;
1272 free(msg);
1273 msg = NULL;
1276 if (t->next_id || t->prev_id) {
1277 error = gotweb_render_navs(c);
1278 if (error)
1279 goto done;
1281 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1282 done:
1283 free(age);
1284 free(author);
1285 free(msg);
1286 return error;
1289 static const struct got_error *
1290 gotweb_render_commits(struct request *c)
1292 const struct got_error *error = NULL;
1293 struct repo_commit *rc = NULL;
1294 struct server *srv = c->srv;
1295 struct transport *t = c->t;
1296 struct querystring *qs = t->qs;
1297 struct repo_dir *repo_dir = t->repo_dir;
1298 const char *index_page_str;
1299 char *age = NULL, *author = NULL, *msg = NULL;
1300 int r;
1302 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1304 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1305 "<div class='commits_title'>Commits</div>\n"
1306 "</div>\n" /* .commits_title_wrapper */
1307 "<div class='commits_content'>\n");
1308 if (r == -1)
1309 goto done;
1311 error = got_get_repo_commits(c, srv->max_commits_display);
1312 if (error)
1313 goto done;
1315 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1316 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1317 if (error)
1318 goto done;
1319 error = gotweb_escape_html(&author, rc->author);
1320 if (error)
1321 goto done;
1322 error = gotweb_escape_html(&msg, rc->commit_msg);
1323 if (error)
1324 goto done;
1326 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1327 "<div class='commits_header'>\n"
1328 "<div class='header_commit_title'>Commit:</div>\n"
1329 "<div class='header_commit'>%s</div>\n"
1330 "<div class='header_author_title'>Author:</div>\n"
1331 "<div class='header_author'>%s</div>\n"
1332 "<div class='header_age_title'>Date:</div>\n"
1333 "<div class='header_age'>%s</div>\n"
1334 "</div>\n" /* .commits_header */
1335 "</div>\n" /* .commits_header_wrapper */
1336 "<div class='dotted_line'></div>\n"
1337 "<div class='commit'>\n%s</div>\n",
1338 rc->commit_id,
1339 author,
1340 age ? age : "",
1341 msg);
1342 if (r == -1)
1343 goto done;
1345 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1346 "<div class='navs'>"
1347 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1348 "diff</a>"
1349 " | "
1350 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1351 "tree</a>"
1352 "</div>\n" /* .navs */
1353 "</div>\n" /* .navs_wrapper */
1354 "<div class='dotted_line'></div>\n",
1355 index_page_str, repo_dir->name, rc->commit_id,
1356 index_page_str, repo_dir->name, rc->commit_id);
1358 free(age);
1359 age = NULL;
1360 free(author);
1361 author = NULL;
1362 free(msg);
1363 msg = NULL;
1366 if (t->next_id || t->prev_id) {
1367 error = gotweb_render_navs(c);
1368 if (error)
1369 goto done;
1371 fcgi_printf(c, "</div>\n"); /* .commits_content */
1372 done:
1373 free(age);
1374 free(author);
1375 free(msg);
1376 return error;
1379 static const struct got_error *
1380 gotweb_render_branches(struct request *c)
1382 const struct got_error *error = NULL;
1383 struct got_reflist_head refs;
1384 struct got_reflist_entry *re;
1385 struct transport *t = c->t;
1386 struct querystring *qs = t->qs;
1387 struct got_repository *repo = t->repo;
1388 const char *index_page_str;
1389 char *age = NULL;
1390 int r;
1392 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1394 TAILQ_INIT(&refs);
1396 error = got_ref_list(&refs, repo, "refs/heads",
1397 got_ref_cmp_by_name, NULL);
1398 if (error)
1399 goto done;
1401 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1402 "<div id='branches_title'>Branches</div>\n"
1403 "</div>\n" /* #branches_title_wrapper */
1404 "<div id='branches_content'>\n");
1405 if (r == -1)
1406 goto done;
1408 TAILQ_FOREACH(re, &refs, entry) {
1409 const char *refname = NULL;
1410 char *escaped_refname = NULL;
1412 if (got_ref_is_symbolic(re->ref))
1413 continue;
1415 refname = got_ref_get_name(re->ref);
1416 if (refname == NULL) {
1417 error = got_error_from_errno("strdup");
1418 goto done;
1420 if (strncmp(refname, "refs/heads/", 11) != 0)
1421 continue;
1423 error = got_get_repo_age(&age, c, qs->path, refname,
1424 TM_DIFF);
1425 if (error)
1426 goto done;
1428 if (strncmp(refname, "refs/heads/", 11) == 0)
1429 refname += 11;
1430 error = gotweb_escape_html(&escaped_refname, refname);
1431 if (error)
1432 goto done;
1434 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1435 "<div class='branches_age'>%s</div>\n"
1436 "<div class='branches_space'>&nbsp;</div>\n"
1437 "<div class='branch'>"
1438 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1439 "%s</a>"
1440 "</div>\n" /* .branch */
1441 "<div class='navs_wrapper'>\n"
1442 "<div class='navs'>"
1443 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1444 "summary</a>"
1445 " | "
1446 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1447 "commit briefs</a>"
1448 " | "
1449 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1450 "commits</a>"
1451 "</div>\n" /* .navs */
1452 "</div>\n" /* .navs_wrapper */
1453 "<div class='dotted_line'></div>\n"
1454 "</div>\n", /* .branches_wrapper */
1455 age ? age : "",
1456 index_page_str, qs->path, refname,
1457 escaped_refname,
1458 index_page_str, qs->path, refname,
1459 index_page_str, qs->path, refname,
1460 index_page_str, qs->path, refname);
1461 free(escaped_refname);
1462 if (r == -1)
1463 goto done;
1465 free(age);
1466 age = NULL;
1469 fcgi_printf(c, "</div>\n"); /* #branches_content */
1470 done:
1471 return error;
1474 static const struct got_error *
1475 gotweb_render_tree(struct request *c)
1477 const struct got_error *error = NULL;
1478 struct transport *t = c->t;
1479 struct repo_commit *rc = NULL;
1480 char *age = NULL, *msg = NULL;
1481 int r;
1483 error = got_get_repo_commits(c, 1);
1484 if (error)
1485 return error;
1487 rc = TAILQ_FIRST(&t->repo_commits);
1489 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1490 if (error)
1491 goto done;
1493 error = gotweb_escape_html(&msg, rc->commit_msg);
1494 if (error)
1495 goto done;
1497 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1498 "<div id='tree_title'>Tree</div>\n"
1499 "</div>\n" /* #tree_title_wrapper */
1500 "<div id='tree_content'>\n"
1501 "<div id='tree_header_wrapper'>\n"
1502 "<div id='tree_header'>\n"
1503 "<div id='header_tree_title'>Tree:</div>\n"
1504 "<div id='header_tree'>%s</div>\n"
1505 "<div class='header_age_title'>Date:</div>\n"
1506 "<div class='header_age'>%s</div>\n"
1507 "<div id='header_commit_msg_title'>Message:</div>\n"
1508 "<div id='header_commit_msg'>%s</div>\n"
1509 "</div>\n" /* #tree_header */
1510 "</div>\n" /* #tree_header_wrapper */
1511 "<div class='dotted_line'></div>\n"
1512 "<div id='tree'>\n",
1513 rc->tree_id,
1514 age ? age : "",
1515 msg);
1516 if (r == -1)
1517 goto done;
1519 error = got_output_repo_tree(c);
1520 if (error)
1521 goto done;
1523 fcgi_printf(c, "</div>\n"); /* #tree */
1524 fcgi_printf(c, "</div>\n"); /* #tree_content */
1525 done:
1526 free(msg);
1527 return error;
1530 static const struct got_error *
1531 gotweb_render_diff(struct request *c)
1533 const struct got_error *error = NULL;
1534 struct transport *t = c->t;
1535 struct repo_commit *rc = NULL;
1536 char *age = NULL, *author = NULL, *msg = NULL;
1537 int r;
1539 error = got_get_repo_commits(c, 1);
1540 if (error)
1541 return error;
1543 rc = TAILQ_FIRST(&t->repo_commits);
1545 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1546 if (error)
1547 goto done;
1548 error = gotweb_escape_html(&author, rc->author);
1549 if (error)
1550 goto done;
1551 error = gotweb_escape_html(&msg, rc->commit_msg);
1552 if (error)
1553 goto done;
1555 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1556 "<div id='diff_title'>Commit Diff</div>\n"
1557 "</div>\n" /* #diff_title_wrapper */
1558 "<div id='diff_content'>\n"
1559 "<div id='diff_header_wrapper'>\n"
1560 "<div id='diff_header'>\n"
1561 "<div id='header_diff_title'>Diff:</div>\n"
1562 "<div id='header_diff'>%s<br />%s</div>\n"
1563 "<div class='header_commit_title'>Commit:</div>\n"
1564 "<div class='header_commit'>%s</div>\n"
1565 "<div id='header_tree_title'>Tree:</div>\n"
1566 "<div id='header_tree'>%s</div>\n"
1567 "<div class='header_author_title'>Author:</div>\n"
1568 "<div class='header_author'>%s</div>\n"
1569 "<div class='header_age_title'>Date:</div>\n"
1570 "<div class='header_age'>%s</div>\n"
1571 "<div id='header_commit_msg_title'>Message:</div>\n"
1572 "<div id='header_commit_msg'>%s</div>\n"
1573 "</div>\n" /* #diff_header */
1574 "</div>\n" /* #diff_header_wrapper */
1575 "<div class='dotted_line'></div>\n"
1576 "<div id='diff'>\n",
1577 rc->parent_id, rc->commit_id,
1578 rc->commit_id,
1579 rc->tree_id,
1580 author,
1581 age ? age : "",
1582 msg);
1583 if (r == -1)
1584 goto done;
1586 error = got_output_repo_diff(c);
1587 if (error)
1588 goto done;
1590 fcgi_printf(c, "</div>\n"); /* #diff */
1591 fcgi_printf(c, "</div>\n"); /* #diff_content */
1592 done:
1593 free(age);
1594 free(author);
1595 free(msg);
1596 return error;
1599 static const struct got_error *
1600 gotweb_render_summary(struct request *c)
1602 const struct got_error *error = NULL;
1603 struct transport *t = c->t;
1604 struct server *srv = c->srv;
1605 int r;
1607 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1608 goto done;
1610 if (srv->show_repo_description) {
1611 r = fcgi_printf(c,
1612 "<div id='description_title'>Description:</div>\n"
1613 "<div id='description'>%s</div>\n",
1614 t->repo_dir->description ? t->repo_dir->description : "");
1615 if (r == -1)
1616 goto done;
1619 if (srv->show_repo_owner) {
1620 r = fcgi_printf(c,
1621 "<div id='repo_owner_title'>Owner:</div>\n"
1622 "<div id='repo_owner'>%s</div>\n",
1623 t->repo_dir->owner ? t->repo_dir->owner : "");
1624 if (r == -1)
1625 goto done;
1628 if (srv->show_repo_age) {
1629 r = fcgi_printf(c,
1630 "<div id='last_change_title'>Last Change:</div>\n"
1631 "<div id='last_change'>%s</div>\n",
1632 t->repo_dir->age);
1633 if (r == -1)
1634 goto done;
1637 if (srv->show_repo_cloneurl) {
1638 r = fcgi_printf(c,
1639 "<div id='cloneurl_title'>Clone URL:</div>\n"
1640 "<div id='cloneurl'>%s</div>\n",
1641 t->repo_dir->url ? t->repo_dir->url : "");
1642 if (r == -1)
1643 goto done;
1646 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1647 if (r == -1)
1648 goto done;
1650 error = gotweb_render_briefs(c);
1651 if (error) {
1652 log_warnx("%s: %s", __func__, error->msg);
1653 goto done;
1656 error = gotweb_render_tags(c);
1657 if (error) {
1658 log_warnx("%s: %s", __func__, error->msg);
1659 goto done;
1662 error = gotweb_render_branches(c);
1663 if (error)
1664 log_warnx("%s: %s", __func__, error->msg);
1665 done:
1666 return error;
1669 static const struct got_error *
1670 gotweb_render_tag(struct request *c)
1672 const struct got_error *error = NULL;
1673 struct repo_tag *rt = NULL;
1674 struct transport *t = c->t;
1675 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1677 error = got_get_repo_tags(c, 1);
1678 if (error)
1679 goto done;
1681 if (t->tag_count == 0) {
1682 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1683 "bad commit id");
1684 goto done;
1687 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1689 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1690 if (error)
1691 goto done;
1692 error = gotweb_escape_html(&author, rt->tagger);
1693 if (error)
1694 goto done;
1695 error = gotweb_escape_html(&msg, rt->commit_msg);
1696 if (error)
1697 goto done;
1699 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1700 rt->tag_name += 5;
1701 error = gotweb_escape_html(&tagname, rt->tag_name);
1702 if (error)
1703 goto done;
1705 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1706 "<div id='tags_title'>Tag</div>\n"
1707 "</div>\n" /* #tags_title_wrapper */
1708 "<div id='tags_content'>\n"
1709 "<div id='tag_header_wrapper'>\n"
1710 "<div id='tag_header'>\n"
1711 "<div class='header_commit_title'>Commit:</div>\n"
1712 "<div class='header_commit'>%s"
1713 " <span class='refs_str'>(%s)</span></div>\n"
1714 "<div class='header_author_title'>Tagger:</div>\n"
1715 "<div class='header_author'>%s</div>\n"
1716 "<div class='header_age_title'>Date:</div>\n"
1717 "<div class='header_age'>%s</div>\n"
1718 "<div id='header_commit_msg_title'>Message:</div>\n"
1719 "<div id='header_commit_msg'>%s</div>\n"
1720 "</div>\n" /* #tag_header */
1721 "<div class='dotted_line'></div>\n"
1722 "<div id='tag_commit'>\n%s</div>"
1723 "</div>", /* tag_header_wrapper */
1724 rt->commit_id,
1725 tagname,
1726 author,
1727 age ? age : "",
1728 msg,
1729 rt->tag_commit);
1731 done:
1732 free(age);
1733 free(author);
1734 free(msg);
1735 return error;
1738 static const struct got_error *
1739 gotweb_render_tags(struct request *c)
1741 const struct got_error *error = NULL;
1742 struct repo_tag *rt = NULL;
1743 struct server *srv = c->srv;
1744 struct transport *t = c->t;
1745 struct querystring *qs = t->qs;
1746 struct repo_dir *repo_dir = t->repo_dir;
1747 const char *index_page_str;
1748 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1749 int r, commit_found = 0;
1751 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1753 if (qs->action == BRIEFS) {
1754 qs->action = TAGS;
1755 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1756 } else
1757 error = got_get_repo_tags(c, srv->max_commits_display);
1758 if (error)
1759 goto done;
1761 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1762 "<div id='tags_title'>Tags</div>\n"
1763 "</div>\n" /* #tags_title_wrapper */
1764 "<div id='tags_content'>\n");
1765 if (r == -1)
1766 goto done;
1768 if (t->tag_count == 0) {
1769 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1770 "This repository contains no tags");
1771 if (r == -1)
1772 goto done;
1775 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1776 if (commit_found == 0 && qs->commit != NULL) {
1777 if (strcmp(qs->commit, rt->commit_id) != 0)
1778 continue;
1779 else
1780 commit_found = 1;
1782 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1783 if (error)
1784 goto done;
1786 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1787 rt->tag_name += 10;
1788 error = gotweb_escape_html(&tagname, rt->tag_name);
1789 if (error)
1790 goto done;
1792 if (rt->tag_commit != NULL) {
1793 newline = strchr(rt->tag_commit, '\n');
1794 if (newline)
1795 *newline = '\0';
1796 error = gotweb_escape_html(&msg, rt->tag_commit);
1797 if (error)
1798 goto done;
1801 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1802 "<div class='tag'>%s</div>\n"
1803 "<div class='tag_log'>"
1804 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1805 "%s</a>"
1806 "</div>\n" /* .tag_log */
1807 "<div class='navs_wrapper'>\n"
1808 "<div class='navs'>"
1809 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1810 "tag</a>"
1811 " | "
1812 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1813 "commit briefs</a>"
1814 " | "
1815 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1816 "commits</a>"
1817 "</div>\n" /* .navs */
1818 "</div>\n" /* .navs_wrapper */
1819 "<div class='dotted_line'></div>\n",
1820 age ? age : "",
1821 tagname,
1822 index_page_str, repo_dir->name, rt->commit_id,
1823 msg ? msg : "",
1824 index_page_str, repo_dir->name, rt->commit_id,
1825 index_page_str, repo_dir->name, rt->commit_id,
1826 index_page_str, repo_dir->name, rt->commit_id);
1827 if (r == -1)
1828 goto done;
1830 free(age);
1831 age = NULL;
1832 free(tagname);
1833 tagname = NULL;
1834 free(msg);
1835 msg = NULL;
1837 if (t->next_id || t->prev_id) {
1838 error = gotweb_render_navs(c);
1839 if (error)
1840 goto done;
1842 fcgi_printf(c, "</div>\n"); /* #tags_content */
1843 done:
1844 free(age);
1845 free(tagname);
1846 free(msg);
1847 return error;
1850 const struct got_error *
1851 gotweb_escape_html(char **escaped_html, const char *orig_html)
1853 const struct got_error *error = NULL;
1854 struct escape_pair {
1855 char c;
1856 const char *s;
1857 } esc[] = {
1858 { '>', "&gt;" },
1859 { '<', "&lt;" },
1860 { '&', "&amp;" },
1861 { '"', "&quot;" },
1862 { '\'', "&apos;" },
1863 { '\n', "<br />" },
1865 size_t orig_len, len;
1866 int i, j, x;
1868 orig_len = strlen(orig_html);
1869 len = orig_len;
1870 for (i = 0; i < orig_len; i++) {
1871 for (j = 0; j < nitems(esc); j++) {
1872 if (orig_html[i] != esc[j].c)
1873 continue;
1874 len += strlen(esc[j].s) - 1 /* escaped char */;
1878 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1879 if (*escaped_html == NULL)
1880 return got_error_from_errno("calloc");
1882 x = 0;
1883 for (i = 0; i < orig_len; i++) {
1884 int escaped = 0;
1885 for (j = 0; j < nitems(esc); j++) {
1886 if (orig_html[i] != esc[j].c)
1887 continue;
1889 if (strlcat(*escaped_html, esc[j].s, len + 1)
1890 >= len + 1) {
1891 error = got_error(GOT_ERR_NO_SPACE);
1892 goto done;
1894 x += strlen(esc[j].s);
1895 escaped = 1;
1896 break;
1898 if (!escaped) {
1899 (*escaped_html)[x] = orig_html[i];
1900 x++;
1903 done:
1904 if (error) {
1905 free(*escaped_html);
1906 *escaped_html = NULL;
1907 } else {
1908 (*escaped_html)[x] = '\0';
1911 return error;
1914 static const struct got_error *
1915 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1917 const struct got_error *error = NULL;
1918 struct socket *sock = c->sock;
1919 struct server *srv = c->srv;
1920 struct transport *t = c->t;
1921 DIR *dt;
1922 char *dir_test;
1923 int opened = 0;
1925 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1926 GOTWEB_GIT_DIR) == -1)
1927 return got_error_from_errno("asprintf");
1929 dt = opendir(dir_test);
1930 if (dt == NULL) {
1931 free(dir_test);
1932 } else {
1933 repo_dir->path = strdup(dir_test);
1934 if (repo_dir->path == NULL) {
1935 opened = 1;
1936 error = got_error_from_errno("strdup");
1937 goto err;
1939 opened = 1;
1940 goto done;
1943 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1944 GOTWEB_GOT_DIR) == -1) {
1945 dir_test = NULL;
1946 error = got_error_from_errno("asprintf");
1947 goto err;
1950 dt = opendir(dir_test);
1951 if (dt == NULL)
1952 free(dir_test);
1953 else {
1954 opened = 1;
1955 error = got_error(GOT_ERR_NOT_GIT_REPO);
1956 goto err;
1959 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1960 repo_dir->name) == -1) {
1961 error = got_error_from_errno("asprintf");
1962 dir_test = NULL;
1963 goto err;
1966 repo_dir->path = strdup(dir_test);
1967 if (repo_dir->path == NULL) {
1968 opened = 1;
1969 error = got_error_from_errno("strdup");
1970 goto err;
1973 dt = opendir(dir_test);
1974 if (dt == NULL) {
1975 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1976 goto err;
1977 } else
1978 opened = 1;
1979 done:
1980 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1981 if (error)
1982 goto err;
1983 error = gotweb_get_repo_description(&repo_dir->description, srv,
1984 repo_dir->path);
1985 if (error)
1986 goto err;
1987 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
1988 if (error)
1989 goto err;
1990 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
1991 NULL, TM_DIFF);
1992 if (error)
1993 goto err;
1994 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
1995 err:
1996 free(dir_test);
1997 if (opened)
1998 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1999 error = got_error_from_errno("closedir");
2000 return error;
2003 static const struct got_error *
2004 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2006 const struct got_error *error;
2008 *repo_dir = calloc(1, sizeof(**repo_dir));
2009 if (*repo_dir == NULL)
2010 return got_error_from_errno("calloc");
2012 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2013 error = got_error_from_errno("asprintf");
2014 free(*repo_dir);
2015 *repo_dir = NULL;
2016 return error;
2018 (*repo_dir)->owner = NULL;
2019 (*repo_dir)->description = NULL;
2020 (*repo_dir)->url = NULL;
2021 (*repo_dir)->age = NULL;
2022 (*repo_dir)->path = NULL;
2024 return NULL;
2027 static const struct got_error *
2028 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2030 const struct got_error *error = NULL;
2031 FILE *f = NULL;
2032 char *d_file = NULL;
2033 unsigned int len;
2034 size_t n;
2036 *description = NULL;
2037 if (srv->show_repo_description == 0)
2038 return NULL;
2040 if (asprintf(&d_file, "%s/description", dir) == -1)
2041 return got_error_from_errno("asprintf");
2043 f = fopen(d_file, "r");
2044 if (f == NULL) {
2045 if (errno == ENOENT || errno == EACCES)
2046 return NULL;
2047 error = got_error_from_errno2("fopen", d_file);
2048 goto done;
2051 if (fseek(f, 0, SEEK_END) == -1) {
2052 error = got_ferror(f, GOT_ERR_IO);
2053 goto done;
2055 len = ftell(f);
2056 if (len == -1) {
2057 error = got_ferror(f, GOT_ERR_IO);
2058 goto done;
2061 if (len == 0)
2062 goto done;
2064 if (fseek(f, 0, SEEK_SET) == -1) {
2065 error = got_ferror(f, GOT_ERR_IO);
2066 goto done;
2068 *description = calloc(len + 1, sizeof(**description));
2069 if (*description == NULL) {
2070 error = got_error_from_errno("calloc");
2071 goto done;
2074 n = fread(*description, 1, len, f);
2075 if (n == 0 && ferror(f))
2076 error = got_ferror(f, GOT_ERR_IO);
2077 done:
2078 if (f != NULL && fclose(f) == EOF && error == NULL)
2079 error = got_error_from_errno("fclose");
2080 free(d_file);
2081 return error;
2084 static const struct got_error *
2085 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2087 const struct got_error *error = NULL;
2088 FILE *f;
2089 char *d_file = NULL;
2090 unsigned int len;
2091 size_t n;
2093 *url = NULL;
2095 if (srv->show_repo_cloneurl == 0)
2096 return NULL;
2098 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2099 return got_error_from_errno("asprintf");
2101 f = fopen(d_file, "r");
2102 if (f == NULL) {
2103 if (errno != ENOENT && errno != EACCES)
2104 error = got_error_from_errno2("fopen", d_file);
2105 goto done;
2108 if (fseek(f, 0, SEEK_END) == -1) {
2109 error = got_ferror(f, GOT_ERR_IO);
2110 goto done;
2112 len = ftell(f);
2113 if (len == -1) {
2114 error = got_ferror(f, GOT_ERR_IO);
2115 goto done;
2117 if (len == 0)
2118 goto done;
2120 if (fseek(f, 0, SEEK_SET) == -1) {
2121 error = got_ferror(f, GOT_ERR_IO);
2122 goto done;
2125 *url = calloc(len + 1, sizeof(**url));
2126 if (*url == NULL) {
2127 error = got_error_from_errno("calloc");
2128 goto done;
2131 n = fread(*url, 1, len, f);
2132 if (n == 0 && ferror(f))
2133 error = got_ferror(f, GOT_ERR_IO);
2134 done:
2135 if (f != NULL && fclose(f) == EOF && error == NULL)
2136 error = got_error_from_errno("fclose");
2137 free(d_file);
2138 return error;
2141 const struct got_error *
2142 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2144 struct tm tm;
2145 long long diff_time;
2146 const char *years = "years ago", *months = "months ago";
2147 const char *weeks = "weeks ago", *days = "days ago";
2148 const char *hours = "hours ago", *minutes = "minutes ago";
2149 const char *seconds = "seconds ago", *now = "right now";
2150 char *s;
2151 char datebuf[29];
2153 *repo_age = NULL;
2155 switch (ref_tm) {
2156 case TM_DIFF:
2157 diff_time = time(NULL) - committer_time;
2158 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2159 if (asprintf(repo_age, "%lld %s",
2160 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2161 return got_error_from_errno("asprintf");
2162 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2163 if (asprintf(repo_age, "%lld %s",
2164 (diff_time / 60 / 60 / 24 / (365 / 12)),
2165 months) == -1)
2166 return got_error_from_errno("asprintf");
2167 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2168 if (asprintf(repo_age, "%lld %s",
2169 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2170 return got_error_from_errno("asprintf");
2171 } else if (diff_time > 60 * 60 * 24 * 2) {
2172 if (asprintf(repo_age, "%lld %s",
2173 (diff_time / 60 / 60 / 24), days) == -1)
2174 return got_error_from_errno("asprintf");
2175 } else if (diff_time > 60 * 60 * 2) {
2176 if (asprintf(repo_age, "%lld %s",
2177 (diff_time / 60 / 60), hours) == -1)
2178 return got_error_from_errno("asprintf");
2179 } else if (diff_time > 60 * 2) {
2180 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2181 minutes) == -1)
2182 return got_error_from_errno("asprintf");
2183 } else if (diff_time > 2) {
2184 if (asprintf(repo_age, "%lld %s", diff_time,
2185 seconds) == -1)
2186 return got_error_from_errno("asprintf");
2187 } else {
2188 if (asprintf(repo_age, "%s", now) == -1)
2189 return got_error_from_errno("asprintf");
2191 break;
2192 case TM_LONG:
2193 if (gmtime_r(&committer_time, &tm) == NULL)
2194 return got_error_from_errno("gmtime_r");
2196 s = asctime_r(&tm, datebuf);
2197 if (s == NULL)
2198 return got_error_from_errno("asctime_r");
2200 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2201 return got_error_from_errno("asprintf");
2202 break;
2204 return NULL;