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,
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(age);
1172 free(msg);
1173 return error;
1176 static const struct got_error *
1177 gotweb_render_briefs(struct request *c)
1179 const struct got_error *error = NULL;
1180 struct repo_commit *rc = NULL;
1181 struct server *srv = c->srv;
1182 struct transport *t = c->t;
1183 struct querystring *qs = t->qs;
1184 struct repo_dir *repo_dir = t->repo_dir;
1185 const char *index_page_str;
1186 char *smallerthan, *newline;
1187 char *age = NULL, *author = NULL, *msg = NULL;
1188 int r;
1190 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1192 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1193 "<div id='briefs_title'>Commit Briefs</div>\n"
1194 "</div>\n" /* #briefs_title_wrapper */
1195 "<div id='briefs_content'>\n");
1196 if (r == -1)
1197 goto done;
1199 if (qs->action == SUMMARY) {
1200 qs->action = BRIEFS;
1201 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1202 } else
1203 error = got_get_repo_commits(c, srv->max_commits_display);
1204 if (error)
1205 goto done;
1207 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1208 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1209 if (error)
1210 goto done;
1212 smallerthan = strchr(rc->author, '<');
1213 if (smallerthan)
1214 *smallerthan = '\0';
1216 newline = strchr(rc->commit_msg, '\n');
1217 if (newline)
1218 *newline = '\0';
1220 error = gotweb_escape_html(&author, rc->author);
1221 if (error)
1222 goto done;
1223 error = gotweb_escape_html(&msg, rc->commit_msg);
1224 if (error)
1225 goto done;
1227 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1228 "<div class='briefs_author'>%s</div>\n"
1229 "<div class='briefs_log'>"
1230 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1231 "&headref=%s'>%s</a>",
1232 age,
1233 author,
1234 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1235 msg);
1236 if (r == -1)
1237 goto done;
1239 if (rc->refs_str) {
1240 char *refs;
1242 error = gotweb_escape_html(&refs, rc->refs_str);
1243 if (error)
1244 goto done;
1245 r = fcgi_printf(c,
1246 " <span class='refs_str'>(%s)</span>", refs);
1247 free(refs);
1248 if (r == -1)
1249 goto done;
1251 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1252 goto done;
1254 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1255 "<div class='navs'>"
1256 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1257 "&headref=%s'>diff</a>"
1258 " | "
1259 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1260 "&headref=%s'>tree</a>"
1261 "</div>\n" /* .navs */
1262 "</div>\n" /* .navs_wrapper */
1263 "<div class='dotted_line'></div>\n",
1264 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1265 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1266 if (r == -1)
1267 goto done;
1269 free(age);
1270 age = NULL;
1271 free(author);
1272 author = NULL;
1273 free(msg);
1274 msg = NULL;
1277 if (t->next_id || t->prev_id) {
1278 error = gotweb_render_navs(c);
1279 if (error)
1280 goto done;
1282 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1283 done:
1284 free(age);
1285 free(author);
1286 free(msg);
1287 return error;
1290 static const struct got_error *
1291 gotweb_render_commits(struct request *c)
1293 const struct got_error *error = NULL;
1294 struct repo_commit *rc = NULL;
1295 struct server *srv = c->srv;
1296 struct transport *t = c->t;
1297 struct querystring *qs = t->qs;
1298 struct repo_dir *repo_dir = t->repo_dir;
1299 const char *index_page_str;
1300 char *age = NULL, *author = NULL, *msg = NULL;
1301 int r;
1303 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1305 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1306 "<div class='commits_title'>Commits</div>\n"
1307 "</div>\n" /* .commits_title_wrapper */
1308 "<div class='commits_content'>\n");
1309 if (r == -1)
1310 goto done;
1312 error = got_get_repo_commits(c, srv->max_commits_display);
1313 if (error)
1314 goto done;
1316 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1317 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1318 if (error)
1319 goto done;
1320 error = gotweb_escape_html(&author, rc->author);
1321 if (error)
1322 goto done;
1323 error = gotweb_escape_html(&msg, rc->commit_msg);
1324 if (error)
1325 goto done;
1327 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1328 "<div class='commits_header'>\n"
1329 "<div class='header_commit_title'>Commit:</div>\n"
1330 "<div class='header_commit'>%s</div>\n"
1331 "<div class='header_author_title'>Author:</div>\n"
1332 "<div class='header_author'>%s</div>\n"
1333 "<div class='header_age_title'>Date:</div>\n"
1334 "<div class='header_age'>%s</div>\n"
1335 "</div>\n" /* .commits_header */
1336 "</div>\n" /* .commits_header_wrapper */
1337 "<div class='dotted_line'></div>\n"
1338 "<div class='commit'>\n%s</div>\n",
1339 rc->commit_id,
1340 author,
1341 age,
1342 msg);
1343 if (r == -1)
1344 goto done;
1346 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1347 "<div class='navs'>"
1348 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1349 "diff</a>"
1350 " | "
1351 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1352 "tree</a>"
1353 "</div>\n" /* .navs */
1354 "</div>\n" /* .navs_wrapper */
1355 "<div class='dotted_line'></div>\n",
1356 index_page_str, repo_dir->name, rc->commit_id,
1357 index_page_str, repo_dir->name, rc->commit_id);
1359 free(age);
1360 age = NULL;
1361 free(author);
1362 author = NULL;
1363 free(msg);
1364 msg = NULL;
1367 if (t->next_id || t->prev_id) {
1368 error = gotweb_render_navs(c);
1369 if (error)
1370 goto done;
1372 fcgi_printf(c, "</div>\n"); /* .commits_content */
1373 done:
1374 free(age);
1375 free(author);
1376 free(msg);
1377 return error;
1380 static const struct got_error *
1381 gotweb_render_branches(struct request *c)
1383 const struct got_error *error = NULL;
1384 struct got_reflist_head refs;
1385 struct got_reflist_entry *re;
1386 struct transport *t = c->t;
1387 struct querystring *qs = t->qs;
1388 struct got_repository *repo = t->repo;
1389 const char *index_page_str;
1390 char *age = NULL;
1391 int r;
1393 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1395 TAILQ_INIT(&refs);
1397 error = got_ref_list(&refs, repo, "refs/heads",
1398 got_ref_cmp_by_name, NULL);
1399 if (error)
1400 goto done;
1402 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1403 "<div id='branches_title'>Branches</div>\n"
1404 "</div>\n" /* #branches_title_wrapper */
1405 "<div id='branches_content'>\n");
1406 if (r == -1)
1407 goto done;
1409 TAILQ_FOREACH(re, &refs, entry) {
1410 const char *refname = NULL;
1411 char *escaped_refname = NULL;
1413 if (got_ref_is_symbolic(re->ref))
1414 continue;
1416 refname = got_ref_get_name(re->ref);
1417 if (refname == NULL) {
1418 error = got_error_from_errno("strdup");
1419 goto done;
1421 if (strncmp(refname, "refs/heads/", 11) != 0)
1422 continue;
1424 error = got_get_repo_age(&age, c, qs->path, refname,
1425 TM_DIFF);
1426 if (error)
1427 goto done;
1429 if (strncmp(refname, "refs/heads/", 11) == 0)
1430 refname += 11;
1431 error = gotweb_escape_html(&escaped_refname, refname);
1432 if (error)
1433 goto done;
1435 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1436 "<div class='branches_age'>%s</div>\n"
1437 "<div class='branches_space'>&nbsp;</div>\n"
1438 "<div class='branch'>"
1439 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1440 "%s</a>"
1441 "</div>\n" /* .branch */
1442 "<div class='navs_wrapper'>\n"
1443 "<div class='navs'>"
1444 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1445 "summary</a>"
1446 " | "
1447 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1448 "commit briefs</a>"
1449 " | "
1450 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1451 "commits</a>"
1452 "</div>\n" /* .navs */
1453 "</div>\n" /* .navs_wrapper */
1454 "<div class='dotted_line'></div>\n"
1455 "</div>\n", /* .branches_wrapper */
1456 age ? age : "",
1457 index_page_str, qs->path, refname,
1458 escaped_refname,
1459 index_page_str, qs->path, refname,
1460 index_page_str, qs->path, refname,
1461 index_page_str, qs->path, refname);
1462 free(escaped_refname);
1463 if (r == -1)
1464 goto done;
1466 free(age);
1467 age = NULL;
1470 fcgi_printf(c, "</div>\n"); /* #branches_content */
1471 done:
1472 return error;
1475 static const struct got_error *
1476 gotweb_render_tree(struct request *c)
1478 const struct got_error *error = NULL;
1479 struct transport *t = c->t;
1480 struct repo_commit *rc = NULL;
1481 char *age = NULL, *msg = NULL;
1482 int r;
1484 error = got_get_repo_commits(c, 1);
1485 if (error)
1486 return error;
1488 rc = TAILQ_FIRST(&t->repo_commits);
1490 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1491 if (error)
1492 goto done;
1494 error = gotweb_escape_html(&msg, rc->commit_msg);
1495 if (error)
1496 goto done;
1498 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1499 "<div id='tree_title'>Tree</div>\n"
1500 "</div>\n" /* #tree_title_wrapper */
1501 "<div id='tree_content'>\n"
1502 "<div id='tree_header_wrapper'>\n"
1503 "<div id='tree_header'>\n"
1504 "<div id='header_tree_title'>Tree:</div>\n"
1505 "<div id='header_tree'>%s</div>\n"
1506 "<div class='header_age_title'>Date:</div>\n"
1507 "<div class='header_age'>%s</div>\n"
1508 "<div id='header_commit_msg_title'>Message:</div>\n"
1509 "<div id='header_commit_msg'>%s</div>\n"
1510 "</div>\n" /* #tree_header */
1511 "</div>\n" /* #tree_header_wrapper */
1512 "<div class='dotted_line'></div>\n"
1513 "<div id='tree'>\n",
1514 rc->tree_id,
1515 age,
1516 msg);
1517 if (r == -1)
1518 goto done;
1520 error = got_output_repo_tree(c);
1521 if (error)
1522 goto done;
1524 fcgi_printf(c, "</div>\n"); /* #tree */
1525 fcgi_printf(c, "</div>\n"); /* #tree_content */
1526 done:
1527 free(age);
1528 free(msg);
1529 return error;
1532 static const struct got_error *
1533 gotweb_render_diff(struct request *c)
1535 const struct got_error *error = NULL;
1536 struct transport *t = c->t;
1537 struct repo_commit *rc = NULL;
1538 char *age = NULL, *author = NULL, *msg = NULL;
1539 int r;
1541 error = got_get_repo_commits(c, 1);
1542 if (error)
1543 return error;
1545 rc = TAILQ_FIRST(&t->repo_commits);
1547 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1548 if (error)
1549 goto done;
1550 error = gotweb_escape_html(&author, rc->author);
1551 if (error)
1552 goto done;
1553 error = gotweb_escape_html(&msg, rc->commit_msg);
1554 if (error)
1555 goto done;
1557 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1558 "<div id='diff_title'>Commit Diff</div>\n"
1559 "</div>\n" /* #diff_title_wrapper */
1560 "<div id='diff_content'>\n"
1561 "<div id='diff_header_wrapper'>\n"
1562 "<div id='diff_header'>\n"
1563 "<div id='header_diff_title'>Diff:</div>\n"
1564 "<div id='header_diff'>%s<br />%s</div>\n"
1565 "<div class='header_commit_title'>Commit:</div>\n"
1566 "<div class='header_commit'>%s</div>\n"
1567 "<div id='header_tree_title'>Tree:</div>\n"
1568 "<div id='header_tree'>%s</div>\n"
1569 "<div class='header_author_title'>Author:</div>\n"
1570 "<div class='header_author'>%s</div>\n"
1571 "<div class='header_age_title'>Date:</div>\n"
1572 "<div class='header_age'>%s</div>\n"
1573 "<div id='header_commit_msg_title'>Message:</div>\n"
1574 "<div id='header_commit_msg'>%s</div>\n"
1575 "</div>\n" /* #diff_header */
1576 "</div>\n" /* #diff_header_wrapper */
1577 "<div class='dotted_line'></div>\n"
1578 "<div id='diff'>\n",
1579 rc->parent_id, rc->commit_id,
1580 rc->commit_id,
1581 rc->tree_id,
1582 author,
1583 age,
1584 msg);
1585 if (r == -1)
1586 goto done;
1588 error = got_output_repo_diff(c);
1589 if (error)
1590 goto done;
1592 fcgi_printf(c, "</div>\n"); /* #diff */
1593 fcgi_printf(c, "</div>\n"); /* #diff_content */
1594 done:
1595 free(age);
1596 free(author);
1597 free(msg);
1598 return error;
1601 static const struct got_error *
1602 gotweb_render_summary(struct request *c)
1604 const struct got_error *error = NULL;
1605 struct transport *t = c->t;
1606 struct server *srv = c->srv;
1607 int r;
1609 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1610 goto done;
1612 if (srv->show_repo_description) {
1613 r = fcgi_printf(c,
1614 "<div id='description_title'>Description:</div>\n"
1615 "<div id='description'>%s</div>\n",
1616 t->repo_dir->description ? t->repo_dir->description : "");
1617 if (r == -1)
1618 goto done;
1621 if (srv->show_repo_owner) {
1622 r = fcgi_printf(c,
1623 "<div id='repo_owner_title'>Owner:</div>\n"
1624 "<div id='repo_owner'>%s</div>\n",
1625 t->repo_dir->owner ? t->repo_dir->owner : "");
1626 if (r == -1)
1627 goto done;
1630 if (srv->show_repo_age) {
1631 r = fcgi_printf(c,
1632 "<div id='last_change_title'>Last Change:</div>\n"
1633 "<div id='last_change'>%s</div>\n",
1634 t->repo_dir->age);
1635 if (r == -1)
1636 goto done;
1639 if (srv->show_repo_cloneurl) {
1640 r = fcgi_printf(c,
1641 "<div id='cloneurl_title'>Clone URL:</div>\n"
1642 "<div id='cloneurl'>%s</div>\n",
1643 t->repo_dir->url ? t->repo_dir->url : "");
1644 if (r == -1)
1645 goto done;
1648 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1649 if (r == -1)
1650 goto done;
1652 error = gotweb_render_briefs(c);
1653 if (error) {
1654 log_warnx("%s: %s", __func__, error->msg);
1655 goto done;
1658 error = gotweb_render_tags(c);
1659 if (error) {
1660 log_warnx("%s: %s", __func__, error->msg);
1661 goto done;
1664 error = gotweb_render_branches(c);
1665 if (error)
1666 log_warnx("%s: %s", __func__, error->msg);
1667 done:
1668 return error;
1671 static const struct got_error *
1672 gotweb_render_tag(struct request *c)
1674 const struct got_error *error = NULL;
1675 struct repo_tag *rt = NULL;
1676 struct transport *t = c->t;
1677 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1679 error = got_get_repo_tags(c, 1);
1680 if (error)
1681 goto done;
1683 if (t->tag_count == 0) {
1684 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1685 "bad commit id");
1686 goto done;
1689 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1691 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1692 if (error)
1693 goto done;
1694 error = gotweb_escape_html(&author, rt->tagger);
1695 if (error)
1696 goto done;
1697 error = gotweb_escape_html(&msg, rt->commit_msg);
1698 if (error)
1699 goto done;
1701 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1702 rt->tag_name += 5;
1703 error = gotweb_escape_html(&tagname, rt->tag_name);
1704 if (error)
1705 goto done;
1707 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1708 "<div id='tags_title'>Tag</div>\n"
1709 "</div>\n" /* #tags_title_wrapper */
1710 "<div id='tags_content'>\n"
1711 "<div id='tag_header_wrapper'>\n"
1712 "<div id='tag_header'>\n"
1713 "<div class='header_commit_title'>Commit:</div>\n"
1714 "<div class='header_commit'>%s"
1715 " <span class='refs_str'>(%s)</span></div>\n"
1716 "<div class='header_author_title'>Tagger:</div>\n"
1717 "<div class='header_author'>%s</div>\n"
1718 "<div class='header_age_title'>Date:</div>\n"
1719 "<div class='header_age'>%s</div>\n"
1720 "<div id='header_commit_msg_title'>Message:</div>\n"
1721 "<div id='header_commit_msg'>%s</div>\n"
1722 "</div>\n" /* #tag_header */
1723 "<div class='dotted_line'></div>\n"
1724 "<div id='tag_commit'>\n%s</div>"
1725 "</div>", /* tag_header_wrapper */
1726 rt->commit_id,
1727 tagname,
1728 author,
1729 age,
1730 msg,
1731 rt->tag_commit);
1733 done:
1734 free(age);
1735 free(author);
1736 free(msg);
1737 return error;
1740 static const struct got_error *
1741 gotweb_render_tags(struct request *c)
1743 const struct got_error *error = NULL;
1744 struct repo_tag *rt = NULL;
1745 struct server *srv = c->srv;
1746 struct transport *t = c->t;
1747 struct querystring *qs = t->qs;
1748 struct repo_dir *repo_dir = t->repo_dir;
1749 const char *index_page_str;
1750 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1751 int r, commit_found = 0;
1753 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1755 if (qs->action == BRIEFS) {
1756 qs->action = TAGS;
1757 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1758 } else
1759 error = got_get_repo_tags(c, srv->max_commits_display);
1760 if (error)
1761 goto done;
1763 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1764 "<div id='tags_title'>Tags</div>\n"
1765 "</div>\n" /* #tags_title_wrapper */
1766 "<div id='tags_content'>\n");
1767 if (r == -1)
1768 goto done;
1770 if (t->tag_count == 0) {
1771 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1772 "This repository contains no tags");
1773 if (r == -1)
1774 goto done;
1777 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1778 if (commit_found == 0 && qs->commit != NULL) {
1779 if (strcmp(qs->commit, rt->commit_id) != 0)
1780 continue;
1781 else
1782 commit_found = 1;
1784 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1785 if (error)
1786 goto done;
1788 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1789 rt->tag_name += 10;
1790 error = gotweb_escape_html(&tagname, rt->tag_name);
1791 if (error)
1792 goto done;
1794 if (rt->tag_commit != NULL) {
1795 newline = strchr(rt->tag_commit, '\n');
1796 if (newline)
1797 *newline = '\0';
1798 error = gotweb_escape_html(&msg, rt->tag_commit);
1799 if (error)
1800 goto done;
1803 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1804 "<div class='tag'>%s</div>\n"
1805 "<div class='tag_log'>"
1806 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1807 "%s</a>"
1808 "</div>\n" /* .tag_log */
1809 "<div class='navs_wrapper'>\n"
1810 "<div class='navs'>"
1811 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1812 "tag</a>"
1813 " | "
1814 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1815 "commit briefs</a>"
1816 " | "
1817 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1818 "commits</a>"
1819 "</div>\n" /* .navs */
1820 "</div>\n" /* .navs_wrapper */
1821 "<div class='dotted_line'></div>\n",
1822 age,
1823 tagname,
1824 index_page_str, repo_dir->name, rt->commit_id,
1825 msg ? msg : "",
1826 index_page_str, repo_dir->name, rt->commit_id,
1827 index_page_str, repo_dir->name, rt->commit_id,
1828 index_page_str, repo_dir->name, rt->commit_id);
1829 if (r == -1)
1830 goto done;
1832 free(age);
1833 age = NULL;
1834 free(tagname);
1835 tagname = NULL;
1836 free(msg);
1837 msg = NULL;
1839 if (t->next_id || t->prev_id) {
1840 error = gotweb_render_navs(c);
1841 if (error)
1842 goto done;
1844 fcgi_printf(c, "</div>\n"); /* #tags_content */
1845 done:
1846 free(age);
1847 free(tagname);
1848 free(msg);
1849 return error;
1852 const struct got_error *
1853 gotweb_escape_html(char **escaped_html, const char *orig_html)
1855 const struct got_error *error = NULL;
1856 struct escape_pair {
1857 char c;
1858 const char *s;
1859 } esc[] = {
1860 { '>', "&gt;" },
1861 { '<', "&lt;" },
1862 { '&', "&amp;" },
1863 { '"', "&quot;" },
1864 { '\'', "&apos;" },
1865 { '\n', "<br />" },
1867 size_t orig_len, len;
1868 int i, j, x;
1870 orig_len = strlen(orig_html);
1871 len = orig_len;
1872 for (i = 0; i < orig_len; i++) {
1873 for (j = 0; j < nitems(esc); j++) {
1874 if (orig_html[i] != esc[j].c)
1875 continue;
1876 len += strlen(esc[j].s) - 1 /* escaped char */;
1880 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1881 if (*escaped_html == NULL)
1882 return got_error_from_errno("calloc");
1884 x = 0;
1885 for (i = 0; i < orig_len; i++) {
1886 int escaped = 0;
1887 for (j = 0; j < nitems(esc); j++) {
1888 if (orig_html[i] != esc[j].c)
1889 continue;
1891 if (strlcat(*escaped_html, esc[j].s, len + 1)
1892 >= len + 1) {
1893 error = got_error(GOT_ERR_NO_SPACE);
1894 goto done;
1896 x += strlen(esc[j].s);
1897 escaped = 1;
1898 break;
1900 if (!escaped) {
1901 (*escaped_html)[x] = orig_html[i];
1902 x++;
1905 done:
1906 if (error) {
1907 free(*escaped_html);
1908 *escaped_html = NULL;
1909 } else {
1910 (*escaped_html)[x] = '\0';
1913 return error;
1916 static const struct got_error *
1917 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1919 const struct got_error *error = NULL;
1920 struct socket *sock = c->sock;
1921 struct server *srv = c->srv;
1922 struct transport *t = c->t;
1923 DIR *dt;
1924 char *dir_test;
1925 int opened = 0;
1927 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1928 GOTWEB_GIT_DIR) == -1)
1929 return got_error_from_errno("asprintf");
1931 dt = opendir(dir_test);
1932 if (dt == NULL) {
1933 free(dir_test);
1934 } else {
1935 repo_dir->path = strdup(dir_test);
1936 if (repo_dir->path == NULL) {
1937 opened = 1;
1938 error = got_error_from_errno("strdup");
1939 goto err;
1941 opened = 1;
1942 goto done;
1945 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1946 GOTWEB_GOT_DIR) == -1) {
1947 dir_test = NULL;
1948 error = got_error_from_errno("asprintf");
1949 goto err;
1952 dt = opendir(dir_test);
1953 if (dt == NULL)
1954 free(dir_test);
1955 else {
1956 opened = 1;
1957 error = got_error(GOT_ERR_NOT_GIT_REPO);
1958 goto err;
1961 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1962 repo_dir->name) == -1) {
1963 error = got_error_from_errno("asprintf");
1964 dir_test = NULL;
1965 goto err;
1968 repo_dir->path = strdup(dir_test);
1969 if (repo_dir->path == NULL) {
1970 opened = 1;
1971 error = got_error_from_errno("strdup");
1972 goto err;
1975 dt = opendir(dir_test);
1976 if (dt == NULL) {
1977 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1978 goto err;
1979 } else
1980 opened = 1;
1981 done:
1982 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1983 if (error)
1984 goto err;
1985 error = gotweb_get_repo_description(&repo_dir->description, srv,
1986 repo_dir->path);
1987 if (error)
1988 goto err;
1989 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
1990 if (error)
1991 goto err;
1992 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
1993 NULL, TM_DIFF);
1994 if (error)
1995 goto err;
1996 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
1997 err:
1998 free(dir_test);
1999 if (opened)
2000 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2001 error = got_error_from_errno("closedir");
2002 return error;
2005 static const struct got_error *
2006 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2008 const struct got_error *error;
2010 *repo_dir = calloc(1, sizeof(**repo_dir));
2011 if (*repo_dir == NULL)
2012 return got_error_from_errno("calloc");
2014 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2015 error = got_error_from_errno("asprintf");
2016 free(*repo_dir);
2017 *repo_dir = NULL;
2018 return error;
2020 (*repo_dir)->owner = NULL;
2021 (*repo_dir)->description = NULL;
2022 (*repo_dir)->url = NULL;
2023 (*repo_dir)->age = NULL;
2024 (*repo_dir)->path = NULL;
2026 return NULL;
2029 static const struct got_error *
2030 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2032 const struct got_error *error = NULL;
2033 FILE *f = NULL;
2034 char *d_file = NULL;
2035 unsigned int len;
2036 size_t n;
2038 *description = NULL;
2039 if (srv->show_repo_description == 0)
2040 return NULL;
2042 if (asprintf(&d_file, "%s/description", dir) == -1)
2043 return got_error_from_errno("asprintf");
2045 f = fopen(d_file, "r");
2046 if (f == NULL) {
2047 if (errno == ENOENT || errno == EACCES)
2048 return NULL;
2049 error = got_error_from_errno2("fopen", d_file);
2050 goto done;
2053 if (fseek(f, 0, SEEK_END) == -1) {
2054 error = got_ferror(f, GOT_ERR_IO);
2055 goto done;
2057 len = ftell(f);
2058 if (len == -1) {
2059 error = got_ferror(f, GOT_ERR_IO);
2060 goto done;
2063 if (len == 0) {
2064 *description = strdup("");
2065 if (*description == NULL)
2066 return got_error_from_errno("strdup");
2067 goto done;
2070 if (fseek(f, 0, SEEK_SET) == -1) {
2071 error = got_ferror(f, GOT_ERR_IO);
2072 goto done;
2074 *description = calloc(len + 1, sizeof(**description));
2075 if (*description == NULL) {
2076 error = got_error_from_errno("calloc");
2077 goto done;
2080 n = fread(*description, 1, len, f);
2081 if (n == 0 && ferror(f))
2082 error = got_ferror(f, GOT_ERR_IO);
2083 done:
2084 if (f != NULL && fclose(f) == EOF && error == NULL)
2085 error = got_error_from_errno("fclose");
2086 free(d_file);
2087 return error;
2090 static const struct got_error *
2091 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2093 const struct got_error *error = NULL;
2094 FILE *f;
2095 char *d_file = NULL;
2096 unsigned int len;
2097 size_t n;
2099 *url = NULL;
2101 if (srv->show_repo_cloneurl == 0)
2102 return NULL;
2104 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2105 return got_error_from_errno("asprintf");
2107 f = fopen(d_file, "r");
2108 if (f == NULL) {
2109 if (errno != ENOENT && errno != EACCES)
2110 error = got_error_from_errno2("fopen", d_file);
2111 goto done;
2114 if (fseek(f, 0, SEEK_END) == -1) {
2115 error = got_ferror(f, GOT_ERR_IO);
2116 goto done;
2118 len = ftell(f);
2119 if (len == -1) {
2120 error = got_ferror(f, GOT_ERR_IO);
2121 goto done;
2123 if (len == 0)
2124 goto done;
2126 if (fseek(f, 0, SEEK_SET) == -1) {
2127 error = got_ferror(f, GOT_ERR_IO);
2128 goto done;
2131 *url = calloc(len + 1, sizeof(**url));
2132 if (*url == NULL) {
2133 error = got_error_from_errno("calloc");
2134 goto done;
2137 n = fread(*url, 1, len, f);
2138 if (n == 0 && ferror(f))
2139 error = got_ferror(f, GOT_ERR_IO);
2140 done:
2141 if (f != NULL && fclose(f) == EOF && error == NULL)
2142 error = got_error_from_errno("fclose");
2143 free(d_file);
2144 return error;
2147 const struct got_error *
2148 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2150 struct tm tm;
2151 long long diff_time;
2152 const char *years = "years ago", *months = "months ago";
2153 const char *weeks = "weeks ago", *days = "days ago";
2154 const char *hours = "hours ago", *minutes = "minutes ago";
2155 const char *seconds = "seconds ago", *now = "right now";
2156 char *s;
2157 char datebuf[29];
2159 *repo_age = NULL;
2161 switch (ref_tm) {
2162 case TM_DIFF:
2163 diff_time = time(NULL) - committer_time;
2164 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2165 if (asprintf(repo_age, "%lld %s",
2166 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2167 return got_error_from_errno("asprintf");
2168 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2169 if (asprintf(repo_age, "%lld %s",
2170 (diff_time / 60 / 60 / 24 / (365 / 12)),
2171 months) == -1)
2172 return got_error_from_errno("asprintf");
2173 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2174 if (asprintf(repo_age, "%lld %s",
2175 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2176 return got_error_from_errno("asprintf");
2177 } else if (diff_time > 60 * 60 * 24 * 2) {
2178 if (asprintf(repo_age, "%lld %s",
2179 (diff_time / 60 / 60 / 24), days) == -1)
2180 return got_error_from_errno("asprintf");
2181 } else if (diff_time > 60 * 60 * 2) {
2182 if (asprintf(repo_age, "%lld %s",
2183 (diff_time / 60 / 60), hours) == -1)
2184 return got_error_from_errno("asprintf");
2185 } else if (diff_time > 60 * 2) {
2186 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2187 minutes) == -1)
2188 return got_error_from_errno("asprintf");
2189 } else if (diff_time > 2) {
2190 if (asprintf(repo_age, "%lld %s", diff_time,
2191 seconds) == -1)
2192 return got_error_from_errno("asprintf");
2193 } else {
2194 if (asprintf(repo_age, "%s", now) == -1)
2195 return got_error_from_errno("asprintf");
2197 break;
2198 case TM_LONG:
2199 if (gmtime_r(&committer_time, &tm) == NULL)
2200 return got_error_from_errno("gmtime_r");
2202 s = asctime_r(&tm, datebuf);
2203 if (s == NULL)
2204 return got_error_from_errno("asctime_r");
2206 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2207 return got_error_from_errno("asprintf");
2208 break;
2210 return NULL;