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 *, 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->document_root, 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 *document_root,
303 uint8_t *subdomain)
305 struct server *srv = NULL;
307 /* check against document_root first */
308 if (strlen(server_name) > 0)
309 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
310 if (strcmp(srv->name, server_name) == 0)
311 goto done;
313 /* check against document_root second */
314 if (strlen(document_root) > 0)
315 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
316 if (strcmp(srv->name, document_root) == 0)
317 goto done;
319 /* check against subdomain third */
320 if (strlen(subdomain) > 0)
321 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
322 if (strcmp(srv->name, subdomain) == 0)
323 goto done;
325 /* if those fail, send first server */
326 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
327 if (srv != NULL)
328 break;
329 done:
330 return srv;
331 };
333 const struct got_error *
334 gotweb_init_transport(struct transport **t)
336 const struct got_error *error = NULL;
338 *t = calloc(1, sizeof(**t));
339 if (*t == NULL)
340 return got_error_from_errno2("%s: calloc", __func__);
342 TAILQ_INIT(&(*t)->repo_commits);
343 TAILQ_INIT(&(*t)->repo_tags);
345 (*t)->repo = NULL;
346 (*t)->repo_dir = NULL;
347 (*t)->qs = NULL;
348 (*t)->next_id = NULL;
349 (*t)->prev_id = NULL;
350 (*t)->next_disp = 0;
351 (*t)->prev_disp = 0;
353 return error;
356 static const struct got_error *
357 gotweb_init_querystring(struct querystring **qs)
359 const struct got_error *error = NULL;
361 *qs = calloc(1, sizeof(**qs));
362 if (*qs == NULL)
363 return got_error_from_errno2("%s: calloc", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
367 (*qs)->file = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->headref = strdup("HEAD");
370 if ((*qs)->headref == NULL) {
371 return got_error_from_errno2("%s: strdup", __func__);
373 (*qs)->index_page = 0;
374 (*qs)->index_page_str = NULL;
375 (*qs)->path = NULL;
377 return error;
380 static const struct got_error *
381 gotweb_parse_querystring(struct querystring **qs, char *qst)
383 const struct got_error *error = NULL;
384 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
385 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
387 if (qst == NULL)
388 return error;
390 tok1 = strdup(qst);
391 if (tok1 == NULL)
392 return got_error_from_errno2("%s: strdup", __func__);
394 tok1_pair = tok1;
395 tok1_end = tok1;
397 while (tok1_pair != NULL) {
398 strsep(&tok1_end, "&");
400 tok2 = strdup(tok1_pair);
401 if (tok2 == NULL) {
402 free(tok1);
403 return got_error_from_errno2("%s: strdup", __func__);
406 tok2_pair = tok2;
407 tok2_end = tok2;
409 while (tok2_pair != NULL) {
410 strsep(&tok2_end, "=");
411 if (tok2_end) {
412 error = gotweb_assign_querystring(qs, tok2_pair,
413 tok2_end);
414 if (error)
415 goto err;
417 tok2_pair = tok2_end;
419 free(tok2);
420 tok1_pair = tok1_end;
422 free(tok1);
423 return error;
424 err:
425 free(tok2);
426 free(tok1);
427 return error;
430 static const struct got_error *
431 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
433 const struct got_error *error = NULL;
434 const char *errstr;
435 int a_cnt, el_cnt;
437 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
438 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
439 continue;
441 switch (querystring_keys[el_cnt].element) {
442 case ACTION:
443 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
444 if (strcmp(value, action_keys[a_cnt].name) != 0)
445 continue;
446 else if (strcmp(value,
447 action_keys[a_cnt].name) == 0){
448 (*qs)->action =
449 action_keys[a_cnt].action;
450 goto qa_found;
453 (*qs)->action = ERR;
454 qa_found:
455 break;
456 case COMMIT:
457 (*qs)->commit = strdup(value);
458 if ((*qs)->commit == NULL) {
459 error = got_error_from_errno2("%s: strdup",
460 __func__);
461 goto done;
463 break;
464 case RFILE:
465 (*qs)->file = strdup(value);
466 if ((*qs)->file == NULL) {
467 error = got_error_from_errno2("%s: strdup",
468 __func__);
469 goto done;
471 break;
472 case FOLDER:
473 (*qs)->folder = strdup(value);
474 if ((*qs)->folder == NULL) {
475 error = got_error_from_errno2("%s: strdup",
476 __func__);
477 goto done;
479 break;
480 case HEADREF:
481 (*qs)->headref = strdup(value);
482 if ((*qs)->headref == NULL) {
483 error = got_error_from_errno2("%s: strdup",
484 __func__);
485 goto done;
487 break;
488 case INDEX_PAGE:
489 if (strlen(value) == 0)
490 break;
491 (*qs)->index_page_str = strdup(value);
492 if ((*qs)->index_page_str == NULL) {
493 error = got_error_from_errno2("%s: strdup",
494 __func__);
495 goto done;
497 (*qs)->index_page = strtonum(value, INT64_MIN,
498 INT64_MAX, &errstr);
499 if (errstr) {
500 error = got_error_from_errno3("%s: strtonum %s",
501 __func__, errstr);
502 goto done;
504 if ((*qs)->index_page < 0) {
505 (*qs)->index_page = 0;
506 sprintf((*qs)->index_page_str, "%d", 0);
508 break;
509 case PATH:
510 (*qs)->path = strdup(value);
511 if ((*qs)->path == NULL) {
512 error = got_error_from_errno2("%s: strdup",
513 __func__);
514 goto done;
516 break;
517 case PAGE:
518 if (strlen(value) == 0)
519 break;
520 (*qs)->page_str = strdup(value);
521 if ((*qs)->page_str == NULL) {
522 error = got_error_from_errno2("%s: strdup",
523 __func__);
524 goto done;
526 (*qs)->page = strtonum(value, INT64_MIN,
527 INT64_MAX, &errstr);
528 if (errstr) {
529 error = got_error_from_errno3("%s: strtonum %s",
530 __func__, errstr);
531 goto done;
533 if ((*qs)->page < 0) {
534 (*qs)->page = 0;
535 sprintf((*qs)->page_str, "%d", 0);
537 break;
538 default:
539 break;
542 done:
543 return error;
546 void
547 gotweb_free_repo_tag(struct repo_tag *rt)
549 if (rt != NULL) {
550 free(rt->commit_msg);
551 free(rt->commit_id);
552 free(rt->tagger);
554 free(rt);
557 void
558 gotweb_free_repo_commit(struct repo_commit *rc)
560 if (rc != NULL) {
561 free(rc->path);
562 free(rc->refs_str);
563 free(rc->commit_id);
564 free(rc->parent_id);
565 free(rc->tree_id);
566 free(rc->author);
567 free(rc->committer);
568 free(rc->commit_msg);
570 free(rc);
573 static void
574 gotweb_free_querystring(struct querystring *qs)
576 if (qs != NULL) {
577 free(qs->commit);
578 free(qs->file);
579 free(qs->folder);
580 free(qs->headref);
581 free(qs->index_page_str);
582 free(qs->path);
583 free(qs->page_str);
585 free(qs);
588 static void
589 gotweb_free_repo_dir(struct repo_dir *repo_dir)
591 if (repo_dir != NULL) {
592 free(repo_dir->name);
593 free(repo_dir->owner);
594 free(repo_dir->description);
595 free(repo_dir->url);
596 free(repo_dir->age);
597 free(repo_dir->path);
599 free(repo_dir);
602 void
603 gotweb_free_transport(struct transport *t)
605 struct repo_commit *rc = NULL, *trc = NULL;
606 struct repo_tag *rt = NULL, *trt = NULL;
608 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
609 TAILQ_REMOVE(&t->repo_commits, rc, entry);
610 gotweb_free_repo_commit(rc);
612 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
613 TAILQ_REMOVE(&t->repo_tags, rt, entry);
614 gotweb_free_repo_tag(rt);
616 gotweb_free_repo_dir(t->repo_dir);
617 gotweb_free_querystring(t->qs);
618 if (t != NULL) {
619 free(t->next_id);
620 free(t->prev_id);
622 free(t);
625 const struct got_error *
626 gotweb_render_content_type(struct request *c, const uint8_t *type)
628 const char *csp = "default-src 'self'; script-src 'none'; "
629 "object-src 'none';";
631 fcgi_printf(c,
632 "Content-Security-Policy: %s\r\n"
633 "Content-Type: %s\r\n\r\n",
634 csp, type);
635 return NULL;
638 const struct got_error *
639 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
640 char *file)
642 fcgi_printf(c, "Content-type: %s\r\n"
643 "Content-disposition: attachment; filename=%s\r\n\r\n",
644 type, file);
645 return NULL;
648 static const struct got_error *
649 gotweb_render_header(struct request *c)
651 struct server *srv = c->srv;
652 struct querystring *qs = c->t->qs;
653 char droot[PATH_MAX];
654 int r;
656 if (strlen(c->document_root) > 0) {
657 r = snprintf(droot, sizeof(droot), "/%s/", c->document_root);
658 if (r < 0 || (size_t)r >= sizeof(droot))
659 return got_error(GOT_ERR_NO_SPACE);
660 } else
661 strlcpy(droot, "/", sizeof(droot));
663 r = fcgi_printf(c, "<!doctype html>\n"
664 "<html>\n"
665 "<head>\n"
666 "<title>%s</title>\n"
667 "<meta charset='utf-8' />\n"
668 "<meta name='viewport' content='initial-scale=.75' />\n"
669 "<meta name='msapplication-TileColor' content='#da532c' />\n"
670 "<meta name='theme-color' content='#ffffff'/>\n"
671 "<link rel='apple-touch-icon' sizes='180x180'"
672 " href='/apple-touch-icon.png' />\n"
673 "<link rel='icon' type='image/png' sizes='32x32'"
674 " href='/favicon-32x32.png' />\n"
675 "<link rel='icon' type='image/png' sizes='16x16'"
676 " href='/favicon-16x16.png' />\n"
677 "<link rel='manifest' href='/site.webmanifest'/>\n"
678 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
679 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
680 "</head>\n"
681 "<body>\n"
682 "<div id='gw_body'>\n"
683 "<div id='header'>\n"
684 "<div id='got_link'>"
685 "<a href='%s' target='_blank'>"
686 "<img src='%s%s' alt='logo' id='logo' />"
687 "</a>\n"
688 "</div>\n" /* #got_link */
689 "</div>\n" /* #header */
690 "<div id='site_path'>\n"
691 "<div id='site_link'>\n"
692 "<a href='/%s?index_page=%d'>%s</a>",
693 srv->site_name,
694 droot, srv->custom_css,
695 srv->logo_url,
696 droot, srv->logo,
697 c->document_root, qs->index_page, srv->site_link);
698 if (r == -1)
699 goto done;
701 if (qs != NULL) {
702 if (qs->path != NULL) {
703 r = fcgi_printf(c, " / "
704 "<a href='/%s?index_page=%d&path=%s&action=summary'>"
705 "%s</a>",
706 c->document_root, qs->index_page, qs->path,
707 qs->path);
708 if (r == -1)
709 goto done;
711 if (qs->action != INDEX) {
712 const char *action = "";
714 switch (qs->action) {
715 case BLAME:
716 action = "blame";
717 break;
718 case BRIEFS:
719 action = "briefs";
720 break;
721 case COMMITS:
722 action = "commits";
723 break;
724 case DIFF:
725 action = "diff";
726 break;
727 case SUMMARY:
728 action = "summary";
729 break;
730 case TAG:
731 action = "tag";
732 break;
733 case TAGS:
734 action = "tags";
735 break;
736 case TREE:
737 action = "tree";
738 break;
741 if (fcgi_printf(c, " / %s", action) == -1)
742 goto done;
746 fcgi_printf(c, "</div>\n" /* #site_path */
747 "</div>\n" /* #site_link */
748 "<div id='content'>\n");
750 done:
751 return NULL;
754 static const struct got_error *
755 gotweb_render_footer(struct request *c)
757 const struct got_error *error = NULL;
758 struct server *srv = c->srv;
759 const char *siteowner = "&nbsp;";
760 char *escaped_owner = NULL;
762 if (srv->show_site_owner) {
763 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
764 if (error)
765 return error;
766 siteowner = escaped_owner;
769 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
770 "<div id='site_owner'>%s</div>\n"
771 "</div>\n" /* #site_owner_wrapper */
772 "</div>\n" /* #content */
773 "</div>\n" /* #gw_body */
774 "</body>\n</html>\n", siteowner);
776 free(escaped_owner);
777 return NULL;
780 static const struct got_error *
781 gotweb_render_navs(struct request *c)
783 const struct got_error *error = NULL;
784 struct transport *t = c->t;
785 struct querystring *qs = t->qs;
786 struct server *srv = c->srv;
787 char *nhref = NULL, *phref = NULL;
788 int r, disp = 0;
790 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
791 if (r == -1)
792 goto done;
794 switch(qs->action) {
795 case INDEX:
796 if (qs->index_page > 0) {
797 if (asprintf(&phref, "index_page=%d",
798 qs->index_page - 1) == -1) {
799 error = got_error_from_errno2("%s: asprintf",
800 __func__);
801 goto done;
803 disp = 1;
805 break;
806 case BRIEFS:
807 if (t->prev_id && qs->commit != NULL &&
808 strcmp(qs->commit, t->prev_id) != 0) {
809 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
810 "&action=briefs&commit=%s&headref=%s",
811 qs->index_page, qs->path, qs->page - 1, t->prev_id,
812 qs->headref) == -1) {
813 error = got_error_from_errno2("%s: asprintf",
814 __func__);
815 goto done;
817 disp = 1;
819 break;
820 case COMMITS:
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=commits&commit=%s&headref=%s&folder=%s"
825 "&file=%s",
826 qs->index_page, qs->path, qs->page - 1, t->prev_id,
827 qs->headref, qs->folder ? qs->folder : "",
828 qs->file ? qs->file : "") == -1) {
829 error = got_error_from_errno2("%s: asprintf",
830 __func__);
831 goto done;
833 disp = 1;
835 break;
836 case TAGS:
837 if (t->prev_id && qs->commit != NULL &&
838 strcmp(qs->commit, t->prev_id) != 0) {
839 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
840 "&action=tags&commit=%s&headref=%s",
841 qs->index_page, qs->path, qs->page - 1, t->prev_id,
842 qs->headref) == -1) {
843 error = got_error_from_errno2("%s: asprintf",
844 __func__);
845 goto done;
847 disp = 1;
849 break;
850 default:
851 disp = 0;
852 break;
855 if (disp) {
856 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
857 if (r == -1)
858 goto done;
861 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
862 "<div id='nav_next'>");
863 if (r == -1)
864 goto done;
866 disp = 0;
867 switch(qs->action) {
868 case INDEX:
869 if (t->next_disp == srv->max_repos_display &&
870 t->repos_total != (qs->index_page + 1) *
871 srv->max_repos_display) {
872 if (asprintf(&nhref, "index_page=%d",
873 qs->index_page + 1) == -1) {
874 error = got_error_from_errno2("%s: asprintf",
875 __func__);
876 goto done;
878 disp = 1;
880 break;
881 case BRIEFS:
882 if (t->next_id) {
883 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
884 "&action=briefs&commit=%s&headref=%s",
885 qs->index_page, qs->path, qs->page + 1, t->next_id,
886 qs->headref) == -1) {
887 error = got_error_from_errno2("%s: asprintf",
888 __func__);
889 goto done;
891 disp = 1;
893 break;
894 case COMMITS:
895 if (t->next_id) {
896 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
897 "&action=commits&commit=%s&headref=%s&folder=%s"
898 "&file=%s",
899 qs->index_page, qs->path, qs->page + 1, t->next_id,
900 qs->headref, qs->folder ? qs->folder : "",
901 qs->file ? qs->file : "") == -1) {
902 error = got_error_from_errno2("%s: asprintf",
903 __func__);
904 goto done;
906 disp = 1;
908 break;
909 case TAGS:
910 if (t->next_id) {
911 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
912 "&action=tags&commit=%s&headref=%s",
913 qs->index_page, qs->path, qs->page + 1, t->next_id,
914 qs->headref) == -1) {
915 error = got_error_from_errno2("%s: asprintf",
916 __func__);
917 goto done;
919 disp = 1;
921 break;
922 default:
923 disp = 0;
924 break;
926 if (disp) {
927 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
928 if (r == -1)
929 goto done;
931 fcgi_printf(c, "</div>\n"); /* #nav_next */
932 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
933 done:
934 free(t->next_id);
935 t->next_id = NULL;
936 free(t->prev_id);
937 t->prev_id = NULL;
938 free(phref);
939 free(nhref);
940 return error;
943 static const struct got_error *
944 gotweb_render_index(struct request *c)
946 const struct got_error *error = NULL;
947 struct server *srv = c->srv;
948 struct transport *t = c->t;
949 struct querystring *qs = t->qs;
950 struct repo_dir *repo_dir = NULL;
951 DIR *d;
952 struct dirent **sd_dent;
953 const char *index_page_str;
954 char *c_path = NULL;
955 struct stat st;
956 unsigned int d_cnt, d_i, d_disp = 0;
957 int r;
959 index_page_str = qs->index_page_str ? qs->index_page_str : "";
961 d = opendir(srv->repos_path);
962 if (d == NULL) {
963 error = got_error_from_errno2("opendir", srv->repos_path);
964 return error;
967 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
968 if (d_cnt == -1) {
969 error = got_error_from_errno2("scandir", srv->repos_path);
970 goto done;
973 /* get total count of repos */
974 for (d_i = 0; d_i < d_cnt; d_i++) {
975 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
976 strcmp(sd_dent[d_i]->d_name, "..") == 0)
977 continue;
979 if (asprintf(&c_path, "%s/%s", srv->repos_path,
980 sd_dent[d_i]->d_name) == -1) {
981 error = got_error_from_errno("asprintf");
982 return error;
985 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
986 !got_path_dir_is_empty(c_path))
987 t->repos_total++;
988 free(c_path);
989 c_path = NULL;
992 r = fcgi_printf(c, "<div id='index_header'>\n"
993 "<div id='index_header_project'>Project</div>\n");
994 if (r == -1)
995 goto done;
997 if (srv->show_repo_description)
998 if (fcgi_printf(c, "<div id='index_header_description'>"
999 "Description</div>\n") == -1)
1000 goto done;
1001 if (srv->show_repo_owner)
1002 if (fcgi_printf(c, "<div id='index_header_owner'>"
1003 "Owner</div>\n") == -1)
1004 goto done;
1005 if (srv->show_repo_age)
1006 if (fcgi_printf(c, "<div id='index_header_age'>"
1007 "Last Change</div>\n") == -1)
1008 goto done;
1009 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1010 goto done;
1012 for (d_i = 0; d_i < d_cnt; d_i++) {
1013 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1014 break; /* account for parent and self */
1016 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1017 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1018 continue;
1020 if (qs->index_page > 0 && (qs->index_page *
1021 srv->max_repos_display) > t->prev_disp) {
1022 t->prev_disp++;
1023 continue;
1026 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1027 if (error)
1028 goto done;
1030 error = gotweb_load_got_path(c, repo_dir);
1031 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1032 error = NULL;
1033 continue;
1035 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1036 goto done;
1038 if (lstat(repo_dir->path, &st) == 0 &&
1039 S_ISDIR(st.st_mode) &&
1040 !got_path_dir_is_empty(repo_dir->path))
1041 goto render;
1042 else {
1043 gotweb_free_repo_dir(repo_dir);
1044 repo_dir = NULL;
1045 continue;
1047 render:
1048 d_disp++;
1049 t->prev_disp++;
1051 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1052 "<div class='index_project'>"
1053 "<a href='?index_page=%s&path=%s&action=summary'>"
1054 " %s "
1055 "</a>"
1056 "</div>", /* .index_project */
1057 index_page_str, repo_dir->name,
1058 repo_dir->name);
1059 if (r == -1)
1060 goto done;
1062 if (srv->show_repo_description) {
1063 r = fcgi_printf(c,
1064 "<div class='index_project_description'>\n"
1065 "%s</div>\n", repo_dir->description);
1066 if (r == -1)
1067 goto done;
1070 if (srv->show_repo_owner) {
1071 r = fcgi_printf(c, "<div class='index_project_owner'>"
1072 "%s</div>\n", repo_dir->owner);
1073 if (r == -1)
1074 goto done;
1077 if (srv->show_repo_age) {
1078 r = fcgi_printf(c, "<div class='index_project_age'>"
1079 "%s</div>\n", repo_dir->age);
1080 if (r == -1)
1081 goto done;
1084 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1085 "<div class='navs'>"
1086 "<a href='?index_page=%s&path=%s&action=summary'>"
1087 "summary"
1088 "</a> | "
1089 "<a href='?index_page=%s&path=%s&action=briefs'>"
1090 "commit briefs"
1091 "</a> | "
1092 "<a href='?index_page=%s&path=%s&action=commits'>"
1093 "commits"
1094 "</a> | "
1095 "<a href='?index_page=%s&path=%s&action=tags'>"
1096 "tags"
1097 "</a> | "
1098 "<a href='?index_page=%s&path=%s&action=tree'>"
1099 "tree"
1100 "</a>"
1101 "</div>" /* .navs */
1102 "<div class='dotted_line'></div>\n"
1103 "</div>\n" /* .navs_wrapper */
1104 "</div>\n", /* .index_wrapper */
1105 index_page_str, repo_dir->name,
1106 index_page_str, repo_dir->name,
1107 index_page_str, repo_dir->name,
1108 index_page_str, repo_dir->name,
1109 index_page_str, repo_dir->name);
1110 if (r == -1)
1111 goto done;
1113 gotweb_free_repo_dir(repo_dir);
1114 repo_dir = NULL;
1115 error = got_repo_close(t->repo);
1116 if (error)
1117 goto done;
1118 t->next_disp++;
1119 if (d_disp == srv->max_repos_display)
1120 break;
1122 if (srv->max_repos_display == 0)
1123 goto done;
1124 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1125 goto done;
1126 if (t->repos_total <= srv->max_repos ||
1127 t->repos_total <= srv->max_repos_display)
1128 goto done;
1130 error = gotweb_render_navs(c);
1131 if (error)
1132 goto done;
1133 done:
1134 if (d != NULL && closedir(d) == EOF && error == NULL)
1135 error = got_error_from_errno("closedir");
1136 return error;
1139 static const struct got_error *
1140 gotweb_render_blame(struct request *c)
1142 const struct got_error *error = NULL;
1143 struct transport *t = c->t;
1144 struct repo_commit *rc = NULL;
1145 char *age = NULL, *msg = NULL;
1146 int r;
1148 error = got_get_repo_commits(c, 1);
1149 if (error)
1150 return error;
1152 rc = TAILQ_FIRST(&t->repo_commits);
1154 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1155 if (error)
1156 goto done;
1157 error = gotweb_escape_html(&msg, rc->commit_msg);
1158 if (error)
1159 goto done;
1161 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1162 "<div id='blame_title'>Blame</div>\n"
1163 "</div>\n" /* #blame_title_wrapper */
1164 "<div id='blame_content'>\n"
1165 "<div id='blame_header_wrapper'>\n"
1166 "<div id='blame_header'>\n"
1167 "<div class='header_age_title'>Date:</div>\n"
1168 "<div class='header_age'>%s</div>\n"
1169 "<div id='header_commit_msg_title'>Message:</div>\n"
1170 "<div id='header_commit_msg'>%s</div>\n"
1171 "</div>\n" /* #blame_header */
1172 "</div>\n" /* #blame_header_wrapper */
1173 "<div class='dotted_line'></div>\n"
1174 "<div id='blame'>\n",
1175 age ? age : "",
1176 msg);
1177 if (r == -1)
1178 goto done;
1180 error = got_output_file_blame(c);
1181 if (error)
1182 goto done;
1184 fcgi_printf(c, "</div>\n" /* #blame */
1185 "</div>\n"); /* #blame_content */
1186 done:
1187 free(msg);
1188 return error;
1191 static const struct got_error *
1192 gotweb_render_briefs(struct request *c)
1194 const struct got_error *error = NULL;
1195 struct repo_commit *rc = NULL;
1196 struct server *srv = c->srv;
1197 struct transport *t = c->t;
1198 struct querystring *qs = t->qs;
1199 struct repo_dir *repo_dir = t->repo_dir;
1200 const char *index_page_str;
1201 char *smallerthan, *newline;
1202 char *age = NULL, *author = NULL, *msg = NULL;
1203 int r;
1205 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1207 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1208 "<div id='briefs_title'>Commit Briefs</div>\n"
1209 "</div>\n" /* #briefs_title_wrapper */
1210 "<div id='briefs_content'>\n");
1211 if (r == -1)
1212 goto done;
1214 if (qs->action == SUMMARY) {
1215 qs->action = BRIEFS;
1216 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1217 } else
1218 error = got_get_repo_commits(c, srv->max_commits_display);
1219 if (error)
1220 goto done;
1222 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1223 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1224 if (error)
1225 goto done;
1227 smallerthan = strchr(rc->author, '<');
1228 if (smallerthan)
1229 *smallerthan = '\0';
1231 newline = strchr(rc->commit_msg, '\n');
1232 if (newline)
1233 *newline = '\0';
1235 error = gotweb_escape_html(&author, rc->author);
1236 if (error)
1237 goto done;
1238 error = gotweb_escape_html(&msg, rc->commit_msg);
1239 if (error)
1240 goto done;
1242 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1243 "<div class='briefs_author'>%s</div>\n"
1244 "<div class='briefs_log'>"
1245 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1246 "&headref=%s'>%s</a>",
1247 age ? age : "",
1248 author,
1249 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1250 msg);
1251 if (r == -1)
1252 goto done;
1254 if (rc->refs_str) {
1255 char *refs;
1257 error = gotweb_escape_html(&refs, rc->refs_str);
1258 if (error)
1259 goto done;
1260 r = fcgi_printf(c,
1261 " <span class='refs_str'>(%s)</span>", refs);
1262 free(refs);
1263 if (r == -1)
1264 goto done;
1266 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1267 goto done;
1269 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1270 "<div class='navs'>"
1271 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1272 "&headref=%s'>diff</a>"
1273 " | "
1274 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1275 "&headref=%s'>tree</a>"
1276 "</div>\n" /* .navs */
1277 "</div>\n" /* .navs_wrapper */
1278 "<div class='dotted_line'></div>\n",
1279 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1280 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1281 if (r == -1)
1282 goto done;
1284 free(age);
1285 age = NULL;
1286 free(author);
1287 author = NULL;
1288 free(msg);
1289 msg = NULL;
1292 if (t->next_id || t->prev_id) {
1293 error = gotweb_render_navs(c);
1294 if (error)
1295 goto done;
1297 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1298 done:
1299 free(age);
1300 free(author);
1301 free(msg);
1302 return error;
1305 static const struct got_error *
1306 gotweb_render_commits(struct request *c)
1308 const struct got_error *error = NULL;
1309 struct repo_commit *rc = NULL;
1310 struct server *srv = c->srv;
1311 struct transport *t = c->t;
1312 struct querystring *qs = t->qs;
1313 struct repo_dir *repo_dir = t->repo_dir;
1314 const char *index_page_str;
1315 char *age = NULL, *author = NULL, *msg = NULL;
1316 int r;
1318 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1320 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1321 "<div class='commits_title'>Commits</div>\n"
1322 "</div>\n" /* .commits_title_wrapper */
1323 "<div class='commits_content'>\n");
1324 if (r == -1)
1325 goto done;
1327 error = got_get_repo_commits(c, srv->max_commits_display);
1328 if (error)
1329 goto done;
1331 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1332 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1333 if (error)
1334 goto done;
1335 error = gotweb_escape_html(&author, rc->author);
1336 if (error)
1337 goto done;
1338 error = gotweb_escape_html(&msg, rc->commit_msg);
1339 if (error)
1340 goto done;
1342 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1343 "<div class='commits_header'>\n"
1344 "<div class='header_commit_title'>Commit:</div>\n"
1345 "<div class='header_commit'>%s</div>\n"
1346 "<div class='header_author_title'>Author:</div>\n"
1347 "<div class='header_author'>%s</div>\n"
1348 "<div class='header_age_title'>Date:</div>\n"
1349 "<div class='header_age'>%s</div>\n"
1350 "</div>\n" /* .commits_header */
1351 "</div>\n" /* .commits_header_wrapper */
1352 "<div class='dotted_line'></div>\n"
1353 "<div class='commit'>\n%s</div>\n",
1354 rc->commit_id,
1355 author,
1356 age ? age : "",
1357 msg);
1358 if (r == -1)
1359 goto done;
1361 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1362 "<div class='navs'>"
1363 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1364 "diff</a>"
1365 " | "
1366 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1367 "tree</a>"
1368 "</div>\n" /* .navs */
1369 "</div>\n" /* .navs_wrapper */
1370 "<div class='dotted_line'></div>\n",
1371 index_page_str, repo_dir->name, rc->commit_id,
1372 index_page_str, repo_dir->name, rc->commit_id);
1374 free(age);
1375 age = NULL;
1376 free(author);
1377 author = NULL;
1378 free(msg);
1379 msg = NULL;
1382 if (t->next_id || t->prev_id) {
1383 error = gotweb_render_navs(c);
1384 if (error)
1385 goto done;
1387 fcgi_printf(c, "</div>\n"); /* .commits_content */
1388 done:
1389 free(age);
1390 free(author);
1391 free(msg);
1392 return error;
1395 static const struct got_error *
1396 gotweb_render_branches(struct request *c)
1398 const struct got_error *error = NULL;
1399 struct got_reflist_head refs;
1400 struct got_reflist_entry *re;
1401 struct transport *t = c->t;
1402 struct querystring *qs = t->qs;
1403 struct got_repository *repo = t->repo;
1404 const char *index_page_str;
1405 char *age = NULL;
1406 int r;
1408 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1410 TAILQ_INIT(&refs);
1412 error = got_ref_list(&refs, repo, "refs/heads",
1413 got_ref_cmp_by_name, NULL);
1414 if (error)
1415 goto done;
1417 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1418 "<div id='branches_title'>Branches</div>\n"
1419 "</div>\n" /* #branches_title_wrapper */
1420 "<div id='branches_content'>\n");
1421 if (r == -1)
1422 goto done;
1424 TAILQ_FOREACH(re, &refs, entry) {
1425 const char *refname = NULL;
1426 char *escaped_refname = NULL;
1428 if (got_ref_is_symbolic(re->ref))
1429 continue;
1431 refname = got_ref_get_name(re->ref);
1432 if (refname == NULL) {
1433 error = got_error_from_errno("strdup");
1434 goto done;
1436 if (strncmp(refname, "refs/heads/", 11) != 0)
1437 continue;
1439 error = got_get_repo_age(&age, c, qs->path, refname,
1440 TM_DIFF);
1441 if (error)
1442 goto done;
1444 if (strncmp(refname, "refs/heads/", 11) == 0)
1445 refname += 11;
1446 error = gotweb_escape_html(&escaped_refname, refname);
1447 if (error)
1448 goto done;
1450 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1451 "<div class='branches_age'>%s</div>\n"
1452 "<div class='branches_space'>&nbsp;</div>\n"
1453 "<div class='branch'>"
1454 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1455 "%s</a>"
1456 "</div>\n" /* .branch */
1457 "<div class='navs_wrapper'>\n"
1458 "<div class='navs'>"
1459 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1460 "summary</a>"
1461 " | "
1462 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1463 "commit briefs</a>"
1464 " | "
1465 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1466 "commits</a>"
1467 "</div>\n" /* .navs */
1468 "</div>\n" /* .navs_wrapper */
1469 "<div class='dotted_line'></div>\n"
1470 "</div>\n", /* .branches_wrapper */
1471 age ? age : "",
1472 index_page_str, qs->path, refname,
1473 escaped_refname,
1474 index_page_str, qs->path, refname,
1475 index_page_str, qs->path, refname,
1476 index_page_str, qs->path, refname);
1477 free(escaped_refname);
1478 if (r == -1)
1479 goto done;
1481 free(age);
1482 age = NULL;
1485 fcgi_printf(c, "</div>\n"); /* #branches_content */
1486 done:
1487 return error;
1490 static const struct got_error *
1491 gotweb_render_tree(struct request *c)
1493 const struct got_error *error = NULL;
1494 struct transport *t = c->t;
1495 struct repo_commit *rc = NULL;
1496 char *age = NULL, *msg = NULL;
1497 int r;
1499 error = got_get_repo_commits(c, 1);
1500 if (error)
1501 return error;
1503 rc = TAILQ_FIRST(&t->repo_commits);
1505 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1506 if (error)
1507 goto done;
1509 error = gotweb_escape_html(&msg, rc->commit_msg);
1510 if (error)
1511 goto done;
1513 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1514 "<div id='tree_title'>Tree</div>\n"
1515 "</div>\n" /* #tree_title_wrapper */
1516 "<div id='tree_content'>\n"
1517 "<div id='tree_header_wrapper'>\n"
1518 "<div id='tree_header'>\n"
1519 "<div id='header_tree_title'>Tree:</div>\n"
1520 "<div id='header_tree'>%s</div>\n"
1521 "<div class='header_age_title'>Date:</div>\n"
1522 "<div class='header_age'>%s</div>\n"
1523 "<div id='header_commit_msg_title'>Message:</div>\n"
1524 "<div id='header_commit_msg'>%s</div>\n"
1525 "</div>\n" /* #tree_header */
1526 "</div>\n" /* #tree_header_wrapper */
1527 "<div class='dotted_line'></div>\n"
1528 "<div id='tree'>\n",
1529 rc->tree_id,
1530 age ? age : "",
1531 msg);
1532 if (r == -1)
1533 goto done;
1535 error = got_output_repo_tree(c);
1536 if (error)
1537 goto done;
1539 fcgi_printf(c, "</div>\n"); /* #tree */
1540 fcgi_printf(c, "</div>\n"); /* #tree_content */
1541 done:
1542 free(msg);
1543 return error;
1546 static const struct got_error *
1547 gotweb_render_diff(struct request *c)
1549 const struct got_error *error = NULL;
1550 struct transport *t = c->t;
1551 struct repo_commit *rc = NULL;
1552 char *age = NULL, *author = NULL, *msg = NULL;
1553 int r;
1555 error = got_get_repo_commits(c, 1);
1556 if (error)
1557 return error;
1559 rc = TAILQ_FIRST(&t->repo_commits);
1561 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1562 if (error)
1563 goto done;
1564 error = gotweb_escape_html(&author, rc->author);
1565 if (error)
1566 goto done;
1567 error = gotweb_escape_html(&msg, rc->commit_msg);
1568 if (error)
1569 goto done;
1571 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1572 "<div id='diff_title'>Commit Diff</div>\n"
1573 "</div>\n" /* #diff_title_wrapper */
1574 "<div id='diff_content'>\n"
1575 "<div id='diff_header_wrapper'>\n"
1576 "<div id='diff_header'>\n"
1577 "<div id='header_diff_title'>Diff:</div>\n"
1578 "<div id='header_diff'>%s<br />%s</div>\n"
1579 "<div class='header_commit_title'>Commit:</div>\n"
1580 "<div class='header_commit'>%s</div>\n"
1581 "<div id='header_tree_title'>Tree:</div>\n"
1582 "<div id='header_tree'>%s</div>\n"
1583 "<div class='header_author_title'>Author:</div>\n"
1584 "<div class='header_author'>%s</div>\n"
1585 "<div class='header_age_title'>Date:</div>\n"
1586 "<div class='header_age'>%s</div>\n"
1587 "<div id='header_commit_msg_title'>Message:</div>\n"
1588 "<div id='header_commit_msg'>%s</div>\n"
1589 "</div>\n" /* #diff_header */
1590 "</div>\n" /* #diff_header_wrapper */
1591 "<div class='dotted_line'></div>\n"
1592 "<div id='diff'>\n",
1593 rc->parent_id, rc->commit_id,
1594 rc->commit_id,
1595 rc->tree_id,
1596 author,
1597 age ? age : "",
1598 msg);
1599 if (r == -1)
1600 goto done;
1602 error = got_output_repo_diff(c);
1603 if (error)
1604 goto done;
1606 fcgi_printf(c, "</div>\n"); /* #diff */
1607 fcgi_printf(c, "</div>\n"); /* #diff_content */
1608 done:
1609 free(age);
1610 free(author);
1611 free(msg);
1612 return error;
1615 static const struct got_error *
1616 gotweb_render_summary(struct request *c)
1618 const struct got_error *error = NULL;
1619 struct transport *t = c->t;
1620 struct server *srv = c->srv;
1621 int r;
1623 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1624 goto done;
1626 if (srv->show_repo_description) {
1627 r = fcgi_printf(c,
1628 "<div id='description_title'>Description:</div>\n"
1629 "<div id='description'>%s</div>\n",
1630 t->repo_dir->description ? t->repo_dir->description : "");
1631 if (r == -1)
1632 goto done;
1635 if (srv->show_repo_owner) {
1636 r = fcgi_printf(c,
1637 "<div id='repo_owner_title'>Owner:</div>\n"
1638 "<div id='repo_owner'>%s</div>\n",
1639 t->repo_dir->owner ? t->repo_dir->owner : "");
1640 if (r == -1)
1641 goto done;
1644 if (srv->show_repo_age) {
1645 r = fcgi_printf(c,
1646 "<div id='last_change_title'>Last Change:</div>\n"
1647 "<div id='last_change'>%s</div>\n",
1648 t->repo_dir->age);
1649 if (r == -1)
1650 goto done;
1653 if (srv->show_repo_cloneurl) {
1654 r = fcgi_printf(c,
1655 "<div id='cloneurl_title'>Clone URL:</div>\n"
1656 "<div id='cloneurl'>%s</div>\n",
1657 t->repo_dir->url ? t->repo_dir->url : "");
1658 if (r == -1)
1659 goto done;
1662 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1663 if (r == -1)
1664 goto done;
1666 error = gotweb_render_briefs(c);
1667 if (error) {
1668 log_warnx("%s: %s", __func__, error->msg);
1669 goto done;
1672 error = gotweb_render_tags(c);
1673 if (error) {
1674 log_warnx("%s: %s", __func__, error->msg);
1675 goto done;
1678 error = gotweb_render_branches(c);
1679 if (error)
1680 log_warnx("%s: %s", __func__, error->msg);
1681 done:
1682 return error;
1685 static const struct got_error *
1686 gotweb_render_tag(struct request *c)
1688 const struct got_error *error = NULL;
1689 struct repo_tag *rt = NULL;
1690 struct transport *t = c->t;
1691 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1693 error = got_get_repo_tags(c, 1);
1694 if (error)
1695 goto done;
1697 if (t->tag_count == 0) {
1698 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1699 "bad commit id");
1700 goto done;
1703 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1705 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1706 if (error)
1707 goto done;
1708 error = gotweb_escape_html(&author, rt->tagger);
1709 if (error)
1710 goto done;
1711 error = gotweb_escape_html(&msg, rt->commit_msg);
1712 if (error)
1713 goto done;
1715 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1716 rt->tag_name += 5;
1717 error = gotweb_escape_html(&tagname, rt->tag_name);
1718 if (error)
1719 goto done;
1721 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1722 "<div id='tags_title'>Tag</div>\n"
1723 "</div>\n" /* #tags_title_wrapper */
1724 "<div id='tags_content'>\n"
1725 "<div id='tag_header_wrapper'>\n"
1726 "<div id='tag_header'>\n"
1727 "<div class='header_commit_title'>Commit:</div>\n"
1728 "<div class='header_commit'>%s"
1729 " <span class='refs_str'>(%s)</span></div>\n"
1730 "<div class='header_author_title'>Tagger:</div>\n"
1731 "<div class='header_author'>%s</div>\n"
1732 "<div class='header_age_title'>Date:</div>\n"
1733 "<div class='header_age'>%s</div>\n"
1734 "<div id='header_commit_msg_title'>Message:</div>\n"
1735 "<div id='header_commit_msg'>%s</div>\n"
1736 "</div>\n" /* #tag_header */
1737 "<div class='dotted_line'></div>\n"
1738 "<div id='tag_commit'>\n%s</div>"
1739 "</div>", /* tag_header_wrapper */
1740 rt->commit_id,
1741 tagname,
1742 author,
1743 age ? age : "",
1744 msg,
1745 rt->tag_commit);
1747 done:
1748 free(age);
1749 free(author);
1750 free(msg);
1751 return error;
1754 static const struct got_error *
1755 gotweb_render_tags(struct request *c)
1757 const struct got_error *error = NULL;
1758 struct repo_tag *rt = NULL;
1759 struct server *srv = c->srv;
1760 struct transport *t = c->t;
1761 struct querystring *qs = t->qs;
1762 struct repo_dir *repo_dir = t->repo_dir;
1763 const char *index_page_str;
1764 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1765 int r, commit_found = 0;
1767 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1769 if (qs->action == BRIEFS) {
1770 qs->action = TAGS;
1771 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1772 } else
1773 error = got_get_repo_tags(c, srv->max_commits_display);
1774 if (error)
1775 goto done;
1777 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1778 "<div id='tags_title'>Tags</div>\n"
1779 "</div>\n" /* #tags_title_wrapper */
1780 "<div id='tags_content'>\n");
1781 if (r == -1)
1782 goto done;
1784 if (t->tag_count == 0) {
1785 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1786 "This repository contains no tags");
1787 if (r == -1)
1788 goto done;
1791 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1792 if (commit_found == 0 && qs->commit != NULL) {
1793 if (strcmp(qs->commit, rt->commit_id) != 0)
1794 continue;
1795 else
1796 commit_found = 1;
1798 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1799 if (error)
1800 goto done;
1802 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1803 rt->tag_name += 10;
1804 error = gotweb_escape_html(&tagname, rt->tag_name);
1805 if (error)
1806 goto done;
1808 if (rt->tag_commit != NULL) {
1809 newline = strchr(rt->tag_commit, '\n');
1810 if (newline)
1811 *newline = '\0';
1812 error = gotweb_escape_html(&msg, rt->tag_commit);
1813 if (error)
1814 goto done;
1817 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1818 "<div class='tag'>%s</div>\n"
1819 "<div class='tag_log'>"
1820 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1821 "%s</a>"
1822 "</div>\n" /* .tag_log */
1823 "<div class='navs_wrapper'>\n"
1824 "<div class='navs'>"
1825 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1826 "tag</a>"
1827 " | "
1828 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1829 "commit briefs</a>"
1830 " | "
1831 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1832 "commits</a>"
1833 "</div>\n" /* .navs */
1834 "</div>\n" /* .navs_wrapper */
1835 "<div class='dotted_line'></div>\n",
1836 age ? age : "",
1837 tagname,
1838 index_page_str, repo_dir->name, rt->commit_id,
1839 msg ? msg : "",
1840 index_page_str, repo_dir->name, rt->commit_id,
1841 index_page_str, repo_dir->name, rt->commit_id,
1842 index_page_str, repo_dir->name, rt->commit_id);
1843 if (r == -1)
1844 goto done;
1846 free(age);
1847 age = NULL;
1848 free(tagname);
1849 tagname = NULL;
1850 free(msg);
1851 msg = NULL;
1853 if (t->next_id || t->prev_id) {
1854 error = gotweb_render_navs(c);
1855 if (error)
1856 goto done;
1858 fcgi_printf(c, "</div>\n"); /* #tags_content */
1859 done:
1860 free(age);
1861 free(tagname);
1862 free(msg);
1863 return error;
1866 const struct got_error *
1867 gotweb_escape_html(char **escaped_html, const char *orig_html)
1869 const struct got_error *error = NULL;
1870 struct escape_pair {
1871 char c;
1872 const char *s;
1873 } esc[] = {
1874 { '>', "&gt;" },
1875 { '<', "&lt;" },
1876 { '&', "&amp;" },
1877 { '"', "&quot;" },
1878 { '\'', "&apos;" },
1879 { '\n', "<br />" },
1881 size_t orig_len, len;
1882 int i, j, x;
1884 orig_len = strlen(orig_html);
1885 len = orig_len;
1886 for (i = 0; i < orig_len; i++) {
1887 for (j = 0; j < nitems(esc); j++) {
1888 if (orig_html[i] != esc[j].c)
1889 continue;
1890 len += strlen(esc[j].s) - 1 /* escaped char */;
1894 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1895 if (*escaped_html == NULL)
1896 return got_error_from_errno("calloc");
1898 x = 0;
1899 for (i = 0; i < orig_len; i++) {
1900 int escaped = 0;
1901 for (j = 0; j < nitems(esc); j++) {
1902 if (orig_html[i] != esc[j].c)
1903 continue;
1905 if (strlcat(*escaped_html, esc[j].s, len + 1)
1906 >= len + 1) {
1907 error = got_error(GOT_ERR_NO_SPACE);
1908 goto done;
1910 x += strlen(esc[j].s);
1911 escaped = 1;
1912 break;
1914 if (!escaped) {
1915 (*escaped_html)[x] = orig_html[i];
1916 x++;
1919 done:
1920 if (error) {
1921 free(*escaped_html);
1922 *escaped_html = NULL;
1923 } else {
1924 (*escaped_html)[x] = '\0';
1927 return error;
1930 static const struct got_error *
1931 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1933 const struct got_error *error = NULL;
1934 struct socket *sock = c->sock;
1935 struct server *srv = c->srv;
1936 struct transport *t = c->t;
1937 DIR *dt;
1938 char *dir_test;
1939 int opened = 0;
1941 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1942 GOTWEB_GIT_DIR) == -1)
1943 return got_error_from_errno("asprintf");
1945 dt = opendir(dir_test);
1946 if (dt == NULL) {
1947 free(dir_test);
1948 } else {
1949 repo_dir->path = strdup(dir_test);
1950 if (repo_dir->path == NULL) {
1951 opened = 1;
1952 error = got_error_from_errno("strdup");
1953 goto err;
1955 opened = 1;
1956 goto done;
1959 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1960 GOTWEB_GOT_DIR) == -1) {
1961 dir_test = NULL;
1962 error = got_error_from_errno("asprintf");
1963 goto err;
1966 dt = opendir(dir_test);
1967 if (dt == NULL)
1968 free(dir_test);
1969 else {
1970 opened = 1;
1971 error = got_error(GOT_ERR_NOT_GIT_REPO);
1972 goto err;
1975 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1976 repo_dir->name) == -1) {
1977 error = got_error_from_errno("asprintf");
1978 dir_test = NULL;
1979 goto err;
1982 repo_dir->path = strdup(dir_test);
1983 if (repo_dir->path == NULL) {
1984 opened = 1;
1985 error = got_error_from_errno("strdup");
1986 goto err;
1989 dt = opendir(dir_test);
1990 if (dt == NULL) {
1991 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1992 goto err;
1993 } else
1994 opened = 1;
1995 done:
1996 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1997 if (error)
1998 goto err;
1999 error = gotweb_get_repo_description(&repo_dir->description, srv,
2000 repo_dir->path);
2001 if (error)
2002 goto err;
2003 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2004 if (error)
2005 goto err;
2006 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2007 NULL, TM_DIFF);
2008 if (error)
2009 goto err;
2010 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2011 err:
2012 free(dir_test);
2013 if (opened)
2014 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2015 error = got_error_from_errno("closedir");
2016 return error;
2019 static const struct got_error *
2020 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2022 const struct got_error *error;
2024 *repo_dir = calloc(1, sizeof(**repo_dir));
2025 if (*repo_dir == NULL)
2026 return got_error_from_errno("calloc");
2028 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2029 error = got_error_from_errno("asprintf");
2030 free(*repo_dir);
2031 *repo_dir = NULL;
2032 return error;
2034 (*repo_dir)->owner = NULL;
2035 (*repo_dir)->description = NULL;
2036 (*repo_dir)->url = NULL;
2037 (*repo_dir)->age = NULL;
2038 (*repo_dir)->path = NULL;
2040 return NULL;
2043 static const struct got_error *
2044 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2046 const struct got_error *error = NULL;
2047 FILE *f = NULL;
2048 char *d_file = NULL;
2049 unsigned int len;
2050 size_t n;
2052 *description = NULL;
2053 if (srv->show_repo_description == 0)
2054 return NULL;
2056 if (asprintf(&d_file, "%s/description", dir) == -1)
2057 return got_error_from_errno("asprintf");
2059 f = fopen(d_file, "r");
2060 if (f == NULL) {
2061 if (errno == ENOENT || errno == EACCES)
2062 return NULL;
2063 error = got_error_from_errno2("fopen", d_file);
2064 goto done;
2067 if (fseek(f, 0, SEEK_END) == -1) {
2068 error = got_ferror(f, GOT_ERR_IO);
2069 goto done;
2071 len = ftell(f);
2072 if (len == -1) {
2073 error = got_ferror(f, GOT_ERR_IO);
2074 goto done;
2077 if (len == 0)
2078 goto done;
2080 if (fseek(f, 0, SEEK_SET) == -1) {
2081 error = got_ferror(f, GOT_ERR_IO);
2082 goto done;
2084 *description = calloc(len + 1, sizeof(**description));
2085 if (*description == NULL) {
2086 error = got_error_from_errno("calloc");
2087 goto done;
2090 n = fread(*description, 1, len, f);
2091 if (n == 0 && ferror(f))
2092 error = got_ferror(f, GOT_ERR_IO);
2093 done:
2094 if (f != NULL && fclose(f) == EOF && error == NULL)
2095 error = got_error_from_errno("fclose");
2096 free(d_file);
2097 return error;
2100 static const struct got_error *
2101 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2103 const struct got_error *error = NULL;
2104 FILE *f;
2105 char *d_file = NULL;
2106 unsigned int len;
2107 size_t n;
2109 *url = NULL;
2111 if (srv->show_repo_cloneurl == 0)
2112 return NULL;
2114 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2115 return got_error_from_errno("asprintf");
2117 f = fopen(d_file, "r");
2118 if (f == NULL) {
2119 if (errno != ENOENT && errno != EACCES)
2120 error = got_error_from_errno2("fopen", d_file);
2121 goto done;
2124 if (fseek(f, 0, SEEK_END) == -1) {
2125 error = got_ferror(f, GOT_ERR_IO);
2126 goto done;
2128 len = ftell(f);
2129 if (len == -1) {
2130 error = got_ferror(f, GOT_ERR_IO);
2131 goto done;
2133 if (len == 0)
2134 goto done;
2136 if (fseek(f, 0, SEEK_SET) == -1) {
2137 error = got_ferror(f, GOT_ERR_IO);
2138 goto done;
2141 *url = calloc(len + 1, sizeof(**url));
2142 if (*url == NULL) {
2143 error = got_error_from_errno("calloc");
2144 goto done;
2147 n = fread(*url, 1, len, f);
2148 if (n == 0 && ferror(f))
2149 error = got_ferror(f, GOT_ERR_IO);
2150 done:
2151 if (f != NULL && fclose(f) == EOF && error == NULL)
2152 error = got_error_from_errno("fclose");
2153 free(d_file);
2154 return error;
2157 const struct got_error *
2158 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2160 struct tm tm;
2161 long long diff_time;
2162 const char *years = "years ago", *months = "months ago";
2163 const char *weeks = "weeks ago", *days = "days ago";
2164 const char *hours = "hours ago", *minutes = "minutes ago";
2165 const char *seconds = "seconds ago", *now = "right now";
2166 char *s;
2167 char datebuf[29];
2169 *repo_age = NULL;
2171 switch (ref_tm) {
2172 case TM_DIFF:
2173 diff_time = time(NULL) - committer_time;
2174 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2175 if (asprintf(repo_age, "%lld %s",
2176 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2177 return got_error_from_errno("asprintf");
2178 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2179 if (asprintf(repo_age, "%lld %s",
2180 (diff_time / 60 / 60 / 24 / (365 / 12)),
2181 months) == -1)
2182 return got_error_from_errno("asprintf");
2183 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2184 if (asprintf(repo_age, "%lld %s",
2185 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2186 return got_error_from_errno("asprintf");
2187 } else if (diff_time > 60 * 60 * 24 * 2) {
2188 if (asprintf(repo_age, "%lld %s",
2189 (diff_time / 60 / 60 / 24), days) == -1)
2190 return got_error_from_errno("asprintf");
2191 } else if (diff_time > 60 * 60 * 2) {
2192 if (asprintf(repo_age, "%lld %s",
2193 (diff_time / 60 / 60), hours) == -1)
2194 return got_error_from_errno("asprintf");
2195 } else if (diff_time > 60 * 2) {
2196 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2197 minutes) == -1)
2198 return got_error_from_errno("asprintf");
2199 } else if (diff_time > 2) {
2200 if (asprintf(repo_age, "%lld %s", diff_time,
2201 seconds) == -1)
2202 return got_error_from_errno("asprintf");
2203 } else {
2204 if (asprintf(repo_age, "%s", now) == -1)
2205 return got_error_from_errno("asprintf");
2207 break;
2208 case TM_LONG:
2209 if (gmtime_r(&committer_time, &tm) == NULL)
2210 return got_error_from_errno("gmtime_r");
2212 s = asctime_r(&tm, datebuf);
2213 if (s == NULL)
2214 return got_error_from_errno("asctime_r");
2216 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2217 return got_error_from_errno("asprintf");
2218 break;
2220 return NULL;