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 goto err;
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->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 fcgi_printf(c, "Content-Type: %s\r\n\r\n", type);
629 return NULL;
632 const struct got_error *
633 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
634 char *file)
636 fcgi_printf(c, "Content-type: %s\r\n"
637 "Content-disposition: attachment; filename=%s\r\n\r\n",
638 type, file);
639 return NULL;
642 static const struct got_error *
643 gotweb_render_header(struct request *c)
645 struct server *srv = c->srv;
646 struct querystring *qs = c->t->qs;
647 char droot[PATH_MAX];
648 int r;
650 if (strlen(c->document_root) > 0) {
651 r = snprintf(droot, sizeof(droot), "/%s/", c->document_root);
652 if (r < 0 || (size_t)r >= sizeof(droot))
653 return got_error(GOT_ERR_NO_SPACE);
654 } else
655 strlcpy(droot, "/", sizeof(droot));
657 r = fcgi_printf(c, "<!doctype html>\n"
658 "<html>\n"
659 "<head>\n"
660 "<title>%s</title>\n"
661 "<meta charset='utf-8' />\n"
662 "<meta name='viewport' content='initial-scale=.75' />\n"
663 "<meta name='msapplication-TileColor' content='#da532c' />\n"
664 "<meta name='theme-color' content='#ffffff'/>\n"
665 "<link rel='apple-touch-icon' sizes='180x180'"
666 " href='/apple-touch-icon.png' />\n"
667 "<link rel='icon' type='image/png' sizes='32x32'"
668 " href='/favicon-32x32.png' />\n"
669 "<link rel='icon' type='image/png' sizes='16x16'"
670 " href='/favicon-16x16.png' />\n"
671 "<link rel='manifest' href='/site.webmanifest'/>\n"
672 "<link rel='mask-icon' href='/safari-pinned-tab.svg' />\n"
673 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
674 "</head>\n"
675 "<body>\n"
676 "<div id='gw_body'>\n"
677 "<div id='header'>\n"
678 "<div id='got_link'>"
679 "<a href='%s' target='_sotd'>"
680 "<img src='%s%s' alt='logo' id='logo' />"
681 "</a>\n"
682 "</div>\n" /* #got_link */
683 "</div>\n" /* #header */
684 "<div id='site_path'>\n"
685 "<div id='site_link'>\n"
686 "<a href='/%s?index_page=%d' alt='sitelink'>%s</a>",
687 srv->site_name,
688 droot, srv->custom_css,
689 srv->logo_url,
690 droot, srv->logo,
691 c->document_root, qs->index_page, srv->site_link);
692 if (r == -1)
693 goto done;
695 if (qs != NULL) {
696 if (qs->path != NULL) {
697 r = fcgi_printf(c, " / "
698 "<a href='/%s?index_page=%d&path=%s&action=summary'"
699 " alt='summlink'>%s</a>",
700 c->document_root, qs->index_page, qs->path,
701 qs->path);
702 if (r == -1)
703 goto done;
705 if (qs->action != INDEX) {
706 const char *action = "";
708 switch (qs->action) {
709 case BLAME:
710 action = "blame";
711 break;
712 case BRIEFS:
713 action = "briefs";
714 break;
715 case COMMITS:
716 action = "commits";
717 break;
718 case DIFF:
719 action = "diff";
720 break;
721 case SUMMARY:
722 action = "summary";
723 break;
724 case TAG:
725 action = "tag";
726 break;
727 case TAGS:
728 action = "tags";
729 break;
730 case TREE:
731 action = "tree";
732 break;
735 if (fcgi_printf(c, " / %s", action) == -1)
736 goto done;
740 fcgi_printf(c, "</div>\n" /* #site_path */
741 "</div>\n" /* #site_link */
742 "<div id='content'>\n");
744 done:
745 return NULL;
748 static const struct got_error *
749 gotweb_render_footer(struct request *c)
751 const struct got_error *error = NULL;
752 struct server *srv = c->srv;
753 const char *siteowner = "&nbsp;";
754 char *escaped_owner = NULL;
756 if (srv->show_site_owner) {
757 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
758 if (error)
759 return error;
760 siteowner = escaped_owner;
763 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
764 "<div id='site_owner'>%s</div>\n"
765 "</div>\n" /* #site_owner_wrapper */
766 "</div>\n" /* #content */
767 "</div>\n" /* #gw_body */
768 "</body>\n</html>\n", siteowner);
770 free(escaped_owner);
771 return NULL;
774 static const struct got_error *
775 gotweb_render_navs(struct request *c)
777 const struct got_error *error = NULL;
778 struct transport *t = c->t;
779 struct querystring *qs = t->qs;
780 struct server *srv = c->srv;
781 char *nhref = NULL, *phref = NULL;
782 int r, disp = 0;
784 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
785 if (r == -1)
786 goto done;
788 switch(qs->action) {
789 case INDEX:
790 if (qs->index_page > 0) {
791 if (asprintf(&phref, "index_page=%d",
792 qs->index_page - 1) == -1) {
793 error = got_error_from_errno2("%s: asprintf",
794 __func__);
795 goto done;
797 disp = 1;
799 break;
800 case BRIEFS:
801 if (t->prev_id && qs->commit != NULL &&
802 strcmp(qs->commit, t->prev_id) != 0) {
803 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
804 "&action=briefs&commit=%s&headref=%s",
805 qs->index_page, qs->path, qs->page - 1, t->prev_id,
806 qs->headref) == -1) {
807 error = got_error_from_errno2("%s: asprintf",
808 __func__);
809 goto done;
811 disp = 1;
813 break;
814 case COMMITS:
815 if (t->prev_id && qs->commit != NULL &&
816 strcmp(qs->commit, t->prev_id) != 0) {
817 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
818 "&action=commits&commit=%s&headref=%s&folder=%s"
819 "&file=%s",
820 qs->index_page, qs->path, qs->page - 1, t->prev_id,
821 qs->headref, qs->folder ? qs->folder : "",
822 qs->file ? qs->file : "") == -1) {
823 error = got_error_from_errno2("%s: asprintf",
824 __func__);
825 goto done;
827 disp = 1;
829 break;
830 case TAGS:
831 if (t->prev_id && qs->commit != NULL &&
832 strcmp(qs->commit, t->prev_id) != 0) {
833 if (asprintf(&phref, "index_page=%d&path=%s&page=%d"
834 "&action=tags&commit=%s&headref=%s",
835 qs->index_page, qs->path, qs->page - 1, t->prev_id,
836 qs->headref) == -1) {
837 error = got_error_from_errno2("%s: asprintf",
838 __func__);
839 goto done;
841 disp = 1;
843 break;
844 default:
845 disp = 0;
846 break;
849 if (disp) {
850 r = fcgi_printf(c, "<a href='?%s'>Previous</a>", phref);
851 if (r == -1)
852 goto done;
855 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
856 "<div id='nav_next'>");
857 if (r == -1)
858 goto done;
860 disp = 0;
861 switch(qs->action) {
862 case INDEX:
863 if (t->next_disp == srv->max_repos_display &&
864 t->repos_total != (qs->index_page + 1) *
865 srv->max_repos_display) {
866 if (asprintf(&nhref, "index_page=%d",
867 qs->index_page + 1) == -1) {
868 error = got_error_from_errno2("%s: asprintf",
869 __func__);
870 goto done;
872 disp = 1;
874 break;
875 case BRIEFS:
876 if (t->next_id) {
877 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
878 "&action=briefs&commit=%s&headref=%s",
879 qs->index_page, qs->path, qs->page + 1, t->next_id,
880 qs->headref) == -1) {
881 error = got_error_from_errno2("%s: asprintf",
882 __func__);
883 goto done;
885 disp = 1;
887 break;
888 case COMMITS:
889 if (t->next_id) {
890 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
891 "&action=commits&commit=%s&headref=%s&folder=%s"
892 "&file=%s",
893 qs->index_page, qs->path, qs->page + 1, t->next_id,
894 qs->headref, qs->folder ? qs->folder : "",
895 qs->file ? qs->file : "") == -1) {
896 error = got_error_from_errno2("%s: asprintf",
897 __func__);
898 goto done;
900 disp = 1;
902 break;
903 case TAGS:
904 if (t->next_id) {
905 if (asprintf(&nhref, "index_page=%d&path=%s&page=%d"
906 "&action=tags&commit=%s&headref=%s",
907 qs->index_page, qs->path, qs->page + 1, t->next_id,
908 qs->headref) == -1) {
909 error = got_error_from_errno2("%s: asprintf",
910 __func__);
911 goto done;
913 disp = 1;
915 break;
916 default:
917 disp = 0;
918 break;
920 if (disp) {
921 r = fcgi_printf(c, "<a href='?%s'>Next</a>", nhref);
922 if (r == -1)
923 goto done;
925 fcgi_printf(c, "</div>\n"); /* #nav_next */
926 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
927 done:
928 free(t->next_id);
929 t->next_id = NULL;
930 free(t->prev_id);
931 t->prev_id = NULL;
932 free(phref);
933 free(nhref);
934 return error;
937 static const struct got_error *
938 gotweb_render_index(struct request *c)
940 const struct got_error *error = NULL;
941 struct server *srv = c->srv;
942 struct transport *t = c->t;
943 struct querystring *qs = t->qs;
944 struct repo_dir *repo_dir = NULL;
945 DIR *d;
946 struct dirent **sd_dent;
947 const char *index_page_str;
948 char *c_path = NULL;
949 struct stat st;
950 unsigned int d_cnt, d_i, d_disp = 0;
951 int r;
953 index_page_str = qs->index_page_str ? qs->index_page_str : "";
955 d = opendir(srv->repos_path);
956 if (d == NULL) {
957 error = got_error_from_errno2("opendir", srv->repos_path);
958 return error;
961 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
962 if (d_cnt == -1) {
963 error = got_error_from_errno2("scandir", srv->repos_path);
964 goto done;
967 /* get total count of repos */
968 for (d_i = 0; d_i < d_cnt; d_i++) {
969 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
970 strcmp(sd_dent[d_i]->d_name, "..") == 0)
971 continue;
973 if (asprintf(&c_path, "%s/%s", srv->repos_path,
974 sd_dent[d_i]->d_name) == -1) {
975 error = got_error_from_errno("asprintf");
976 return error;
979 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
980 !got_path_dir_is_empty(c_path))
981 t->repos_total++;
982 free(c_path);
983 c_path = NULL;
986 r = fcgi_printf(c, "<div id='index_header'>\n"
987 "<div id='index_header_project'>Project</div>\n");
988 if (r == -1)
989 goto done;
991 if (srv->show_repo_description)
992 if (fcgi_printf(c, "<div id='index_header_description'>"
993 "Description</div>\n") == -1)
994 goto done;
995 if (srv->show_repo_owner)
996 if (fcgi_printf(c, "<div id='index_header_owner'>"
997 "Owner</div>\n") == -1)
998 goto done;
999 if (srv->show_repo_age)
1000 if (fcgi_printf(c, "<div id='index_header_age'>"
1001 "Last Change</div>\n") == -1)
1002 goto done;
1003 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1004 goto done;
1006 for (d_i = 0; d_i < d_cnt; d_i++) {
1007 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1008 break; /* account for parent and self */
1010 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1011 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1012 continue;
1014 if (qs->index_page > 0 && (qs->index_page *
1015 srv->max_repos_display) > t->prev_disp) {
1016 t->prev_disp++;
1017 continue;
1020 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1021 if (error)
1022 goto done;
1024 error = gotweb_load_got_path(c, repo_dir);
1025 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1026 error = NULL;
1027 continue;
1029 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1030 goto done;
1032 if (lstat(repo_dir->path, &st) == 0 &&
1033 S_ISDIR(st.st_mode) &&
1034 !got_path_dir_is_empty(repo_dir->path))
1035 goto render;
1036 else {
1037 gotweb_free_repo_dir(repo_dir);
1038 repo_dir = NULL;
1039 continue;
1041 render:
1042 d_disp++;
1043 t->prev_disp++;
1045 r = fcgi_printf(c, "<div class='index_wrapper'>\n"
1046 "<div class='index_project'>"
1047 "<a href='?index_page=%s&path=%s&action=summary'>"
1048 " %s "
1049 "</a>"
1050 "</div>", /* .index_project */
1051 index_page_str, repo_dir->name,
1052 repo_dir->name);
1053 if (r == -1)
1054 goto done;
1056 if (srv->show_repo_description) {
1057 r = fcgi_printf(c,
1058 "<div class='index_project_description'>\n"
1059 "%s</div>\n", repo_dir->description);
1060 if (r == -1)
1061 goto done;
1064 if (srv->show_repo_owner) {
1065 r = fcgi_printf(c, "<div class='index_project_owner'>"
1066 "%s</div>\n", repo_dir->owner);
1067 if (r == -1)
1068 goto done;
1071 if (srv->show_repo_age) {
1072 r = fcgi_printf(c, "<div class='index_project_age'>"
1073 "%s</div>\n", repo_dir->age);
1074 if (r == -1)
1075 goto done;
1078 r = fcgi_printf(c, "<div class='navs_wrapper'>"
1079 "<div class='navs'>"
1080 "<a href='?index_page=%s&path=%s&action=summary'>"
1081 "summary"
1082 "</a> | "
1083 "<a href='?index_page=%s&path=%s&action=briefs'>"
1084 "commit briefs"
1085 "</a> | "
1086 "<a href='?index_page=%s&path=%s&action=commits'>"
1087 "commits"
1088 "</a> | "
1089 "<a href='?index_page=%s&path=%s&action=tags'>"
1090 "tags"
1091 "</a> | "
1092 "<a href='?index_page=%s&path=%s&action=tree'>"
1093 "tree"
1094 "</a>"
1095 "</div>" /* .navs */
1096 "<div class='dotted_line'></div>\n"
1097 "</div>\n" /* .navs_wrapper */
1098 "</div>\n", /* .index_wrapper */
1099 index_page_str, repo_dir->name,
1100 index_page_str, repo_dir->name,
1101 index_page_str, repo_dir->name,
1102 index_page_str, repo_dir->name,
1103 index_page_str, repo_dir->name);
1104 if (r == -1)
1105 goto done;
1107 gotweb_free_repo_dir(repo_dir);
1108 repo_dir = NULL;
1109 error = got_repo_close(t->repo);
1110 if (error)
1111 goto done;
1112 t->next_disp++;
1113 if (d_disp == srv->max_repos_display)
1114 break;
1116 if (srv->max_repos_display == 0)
1117 goto done;
1118 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1119 goto done;
1120 if (t->repos_total <= srv->max_repos ||
1121 t->repos_total <= srv->max_repos_display)
1122 goto done;
1124 error = gotweb_render_navs(c);
1125 if (error)
1126 goto done;
1127 done:
1128 if (d != NULL && closedir(d) == EOF && error == NULL)
1129 error = got_error_from_errno("closedir");
1130 return error;
1133 static const struct got_error *
1134 gotweb_render_blame(struct request *c)
1136 const struct got_error *error = NULL;
1137 struct transport *t = c->t;
1138 struct repo_commit *rc = NULL;
1139 char *age = NULL;
1140 int r;
1142 error = got_get_repo_commits(c, 1);
1143 if (error)
1144 return error;
1146 rc = TAILQ_FIRST(&t->repo_commits);
1148 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1149 if (error)
1150 goto done;
1152 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1153 "<div id='blame_title'>Blame</div>\n"
1154 "</div>\n" /* #blame_title_wrapper */
1155 "<div id='blame_content'>\n"
1156 "<div id='blame_header_wrapper'>\n"
1157 "<div id='blame_header'>\n"
1158 "<div class='header_age_title'>Date:</div>\n"
1159 "<div class='header_age'>%s</div>\n"
1160 "<div id='header_commit_msg_title'>Message:</div>\n"
1161 "<div id='header_commit_msg'>%s</div>\n"
1162 "</div>\n" /* #blame_header */
1163 "</div>\n" /* #blame_header_wrapper */
1164 "<div class='dotted_line'></div>\n"
1165 "<div id='blame'>\n",
1166 age ? age : "",
1167 rc->commit_msg);
1168 if (r == -1)
1169 goto done;
1171 error = got_output_file_blame(c);
1172 if (error)
1173 goto done;
1175 fcgi_printf(c, "</div>\n" /* #blame */
1176 "</div>\n"); /* #blame_content */
1177 done:
1178 return error;
1181 static const struct got_error *
1182 gotweb_render_briefs(struct request *c)
1184 const struct got_error *error = NULL;
1185 struct repo_commit *rc = NULL;
1186 struct server *srv = c->srv;
1187 struct transport *t = c->t;
1188 struct querystring *qs = t->qs;
1189 struct repo_dir *repo_dir = t->repo_dir;
1190 const char *index_page_str;
1191 char *smallerthan, *newline;
1192 char *age = NULL;
1193 int r;
1195 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1197 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1198 "<div id='briefs_title'>Commit Briefs</div>\n"
1199 "</div>\n" /* #briefs_title_wrapper */
1200 "<div id='briefs_content'>\n");
1201 if (r == -1)
1202 goto done;
1204 if (qs->action == SUMMARY) {
1205 qs->action = BRIEFS;
1206 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1207 } else
1208 error = got_get_repo_commits(c, srv->max_commits_display);
1209 if (error)
1210 goto done;
1212 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1213 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1214 if (error)
1215 goto done;
1217 smallerthan = strchr(rc->author, '<');
1218 if (smallerthan)
1219 *smallerthan = '\0';
1221 newline = strchr(rc->commit_msg, '\n');
1222 if (newline)
1223 *newline = '\0';
1225 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1226 "<div class='briefs_author'>%s</div>\n"
1227 "<div class='briefs_log'>"
1228 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1229 "&headref=%s'>%s</a>",
1230 age ? age : "",
1231 rc->author,
1232 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1233 rc->commit_msg);
1234 if (r == -1)
1235 goto done;
1237 if (rc->refs_str) {
1238 r = fcgi_printf(c,
1239 " <span class='refs_str'>(%s)</span>",
1240 rc->refs_str);
1241 if (r == -1)
1242 goto done;
1244 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1245 goto done;
1247 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1248 "<div class='navs'>"
1249 "<a href='?index_page=%s&path=%s&action=diff&commit=%s"
1250 "&headref=%s'>diff</a>"
1251 " | "
1252 "<a href='?index_page=%s&path=%s&action=tree&commit=%s"
1253 "&headref=%s'>tree</a>"
1254 "</div>\n" /* .navs */
1255 "</div>\n" /* .navs_wrapper */
1256 "<div class='dotted_line'></div>\n",
1257 index_page_str, repo_dir->name, rc->commit_id, qs->headref,
1258 index_page_str, repo_dir->name, rc->commit_id, qs->headref);
1259 if (r == -1)
1260 goto done;
1262 free(age);
1263 age = NULL;
1266 if (t->next_id || t->prev_id) {
1267 error = gotweb_render_navs(c);
1268 if (error)
1269 goto done;
1271 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1272 done:
1273 free(age);
1274 return error;
1277 static const struct got_error *
1278 gotweb_render_commits(struct request *c)
1280 const struct got_error *error = NULL;
1281 struct repo_commit *rc = NULL;
1282 struct server *srv = c->srv;
1283 struct transport *t = c->t;
1284 struct querystring *qs = t->qs;
1285 struct repo_dir *repo_dir = t->repo_dir;
1286 const char *index_page_str;
1287 char *age = NULL, *author = NULL;
1288 int r;
1290 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1292 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1293 "<div class='commits_title'>Commits</div>\n"
1294 "</div>\n" /* .commits_title_wrapper */
1295 "<div class='commits_content'>\n");
1296 if (r == -1)
1297 goto done;
1299 error = got_get_repo_commits(c, srv->max_commits_display);
1300 if (error)
1301 goto done;
1303 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1304 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1305 if (error)
1306 goto done;
1307 error = gotweb_escape_html(&author, rc->author);
1308 if (error)
1309 goto done;
1311 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1312 "<div class='commits_header'>\n"
1313 "<div class='header_commit_title'>Commit:</div>\n"
1314 "<div class='header_commit'>%s</div>\n"
1315 "<div class='header_author_title'>Author:</div>\n"
1316 "<div class='header_author'>%s</div>\n"
1317 "<div class='header_age_title'>Date:</div>\n"
1318 "<div class='header_age'>%s</div>\n"
1319 "</div>\n" /* .commits_header */
1320 "</div>\n" /* .commits_header_wrapper */
1321 "<div class='dotted_line'></div>\n"
1322 "<div class='commit'>\n%s</div>\n",
1323 rc->commit_id,
1324 author ? author : "",
1325 age ? age : "",
1326 rc->commit_msg);
1327 if (r == -1)
1328 goto done;
1330 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1331 "<div class='navs'>"
1332 "<a href='?index_page=%s&path=%s&action=diff&commit=%s'>"
1333 "diff</a>"
1334 " | "
1335 "<a href='?index_page=%s&path=%s&action=tree&commit=%s'>"
1336 "tree</a>"
1337 "</div>\n" /* .navs */
1338 "</div>\n" /* .navs_wrapper */
1339 "<div class='dotted_line'></div>\n",
1340 index_page_str, repo_dir->name, rc->commit_id,
1341 index_page_str, repo_dir->name, rc->commit_id);
1343 free(age);
1344 age = NULL;
1345 free(author);
1346 author = NULL;
1349 if (t->next_id || t->prev_id) {
1350 error = gotweb_render_navs(c);
1351 if (error)
1352 goto done;
1354 fcgi_printf(c, "</div>\n"); /* .commits_content */
1355 done:
1356 free(age);
1357 return error;
1360 static const struct got_error *
1361 gotweb_render_branches(struct request *c)
1363 const struct got_error *error = NULL;
1364 struct got_reflist_head refs;
1365 struct got_reflist_entry *re;
1366 struct transport *t = c->t;
1367 struct querystring *qs = t->qs;
1368 struct got_repository *repo = t->repo;
1369 const char *index_page_str;
1370 char *age = NULL;
1371 int r;
1373 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1375 TAILQ_INIT(&refs);
1377 error = got_ref_list(&refs, repo, "refs/heads",
1378 got_ref_cmp_by_name, NULL);
1379 if (error)
1380 goto done;
1382 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1383 "<div id='branches_title'>Branches</div>\n"
1384 "</div>\n" /* #branches_title_wrapper */
1385 "<div id='branches_content'>\n");
1386 if (r == -1)
1387 goto done;
1389 TAILQ_FOREACH(re, &refs, entry) {
1390 char *refname = NULL;
1392 if (got_ref_is_symbolic(re->ref))
1393 continue;
1395 refname = strdup(got_ref_get_name(re->ref));
1396 if (refname == NULL) {
1397 error = got_error_from_errno("strdup");
1398 goto done;
1400 if (strncmp(refname, "refs/heads/", 11) != 0)
1401 continue;
1403 error = got_get_repo_age(&age, c, qs->path, refname,
1404 TM_DIFF);
1405 if (error)
1406 goto done;
1408 if (strncmp(refname, "refs/heads/", 11) == 0)
1409 refname += 11;
1411 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1412 "<div class='branches_age'>%s</div>\n"
1413 "<div class='branches_space'>&nbsp;</div>\n"
1414 "<div class='branch'>"
1415 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1416 "%s</a>"
1417 "</div>\n" /* .branch */
1418 "<div class='navs_wrapper'>\n"
1419 "<div class='navs'>"
1420 "<a href='?index_page=%s&path=%s&action=summary&headref=%s'>"
1421 "summary</a>"
1422 " | "
1423 "<a href='?index_page=%s&path=%s&action=briefs&headref=%s'>"
1424 "commit briefs</a>"
1425 " | "
1426 "<a href='?index_page=%s&path=%s&action=commits&headref=%s'>"
1427 "commits</a>"
1428 "</div>\n" /* .navs */
1429 "</div>\n" /* .navs_wrapper */
1430 "<div class='dotted_line'></div>\n"
1431 "</div>\n", /* .branches_wrapper */
1432 age ? age : "",
1433 index_page_str, qs->path, refname,
1434 refname,
1435 index_page_str, qs->path, refname,
1436 index_page_str, qs->path, refname,
1437 index_page_str, qs->path, refname);
1438 if (r == -1)
1439 goto done;
1441 free(age);
1442 age = NULL;
1445 fcgi_printf(c, "</div>\n"); /* #branches_content */
1446 done:
1447 return error;
1450 static const struct got_error *
1451 gotweb_render_tree(struct request *c)
1453 const struct got_error *error = NULL;
1454 struct transport *t = c->t;
1455 struct repo_commit *rc = NULL;
1456 char *age = NULL;
1457 int r;
1459 error = got_get_repo_commits(c, 1);
1460 if (error)
1461 return error;
1463 rc = TAILQ_FIRST(&t->repo_commits);
1465 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1466 if (error)
1467 goto done;
1469 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1470 "<div id='tree_title'>Tree</div>\n"
1471 "</div>\n" /* #tree_title_wrapper */
1472 "<div id='tree_content'>\n"
1473 "<div id='tree_header_wrapper'>\n"
1474 "<div id='tree_header'>\n"
1475 "<div id='header_tree_title'>Tree:</div>\n"
1476 "<div id='header_tree'>%s</div>\n"
1477 "<div class='header_age_title'>Date:</div>\n"
1478 "<div class='header_age'>%s</div>\n"
1479 "<div id='header_commit_msg_title'>Message:</div>\n"
1480 "<div id='header_commit_msg'>%s</div>\n"
1481 "</div>\n" /* #tree_header */
1482 "</div>\n" /* #tree_header_wrapper */
1483 "<div class='dotted_line'></div>\n"
1484 "<div id='tree'>\n",
1485 rc->tree_id,
1486 age ? age : "",
1487 rc->commit_msg);
1488 if (r == -1)
1489 goto done;
1491 error = got_output_repo_tree(c);
1492 if (error)
1493 goto done;
1495 fcgi_printf(c, "</div>\n"); /* #tree */
1496 fcgi_printf(c, "</div>\n"); /* #tree_content */
1497 done:
1498 return error;
1501 static const struct got_error *
1502 gotweb_render_diff(struct request *c)
1504 const struct got_error *error = NULL;
1505 struct transport *t = c->t;
1506 struct repo_commit *rc = NULL;
1507 char *age = NULL, *author = NULL;
1508 int r;
1510 error = got_get_repo_commits(c, 1);
1511 if (error)
1512 return error;
1514 rc = TAILQ_FIRST(&t->repo_commits);
1516 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1517 if (error)
1518 goto done;
1519 error = gotweb_escape_html(&author, rc->author);
1520 if (error)
1521 goto done;
1523 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1524 "<div id='diff_title'>Commit Diff</div>\n"
1525 "</div>\n" /* #diff_title_wrapper */
1526 "<div id='diff_content'>\n"
1527 "<div id='diff_header_wrapper'>\n"
1528 "<div id='diff_header'>\n"
1529 "<div id='header_diff_title'>Diff:</div>\n"
1530 "<div id='header_diff'>%s<br />%s</div>\n"
1531 "<div class='header_commit_title'>Commit:</div>\n"
1532 "<div class='header_commit'>%s</div>\n"
1533 "<div id='header_tree_title'>Tree:</div>\n"
1534 "<div id='header_tree'>%s</div>\n"
1535 "<div class='header_author_title'>Author:</div>\n"
1536 "<div class='header_author'>%s</div>\n"
1537 "<div class='header_age_title'>Date:</div>\n"
1538 "<div class='header_age'>%s</div>\n"
1539 "<div id='header_commit_msg_title'>Message:</div>\n"
1540 "<div id='header_commit_msg'>%s</div>\n"
1541 "</div>\n" /* #diff_header */
1542 "</div>\n" /* #diff_header_wrapper */
1543 "<div class='dotted_line'></div>\n"
1544 "<div id='diff'>\n",
1545 rc->parent_id, rc->commit_id,
1546 rc->commit_id,
1547 rc->tree_id,
1548 author ? author : "",
1549 age ? age : "",
1550 rc->commit_msg);
1551 if (r == -1)
1552 goto done;
1554 error = got_output_repo_diff(c);
1555 if (error)
1556 goto done;
1558 fcgi_printf(c, "</div>\n"); /* #diff */
1559 fcgi_printf(c, "</div>\n"); /* #diff_content */
1560 done:
1561 free(age);
1562 free(author);
1563 return error;
1566 static const struct got_error *
1567 gotweb_render_summary(struct request *c)
1569 const struct got_error *error = NULL;
1570 struct transport *t = c->t;
1571 struct server *srv = c->srv;
1572 int r;
1574 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1575 goto done;
1577 if (srv->show_repo_description) {
1578 r = fcgi_printf(c,
1579 "<div id='description_title'>Description:</div>\n"
1580 "<div id='description'>%s</div>\n",
1581 t->repo_dir->description);
1582 if (r == -1)
1583 goto done;
1586 if (srv->show_repo_owner) {
1587 r = fcgi_printf(c,
1588 "<div id='repo_owner_title'>Owner:</div>\n"
1589 "<div id='repo_owner'>%s</div>\n",
1590 t->repo_dir->owner);
1591 if (r == -1)
1592 goto done;
1595 if (srv->show_repo_age) {
1596 r = fcgi_printf(c,
1597 "<div id='last_change_title'>Last Change:</div>\n"
1598 "<div id='last_change'>%s</div>\n",
1599 t->repo_dir->age);
1600 if (r == -1)
1601 goto done;
1604 if (srv->show_repo_cloneurl) {
1605 r = fcgi_printf(c,
1606 "<div id='cloneurl_title'>Clone URL:</div>\n"
1607 "<div id='cloneurl'>%s</div>\n",
1608 t->repo_dir->url ? t->repo_dir->url : "");
1609 if (r == -1)
1610 goto done;
1613 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1614 if (r == -1)
1615 goto done;
1617 error = gotweb_render_briefs(c);
1618 if (error) {
1619 log_warnx("%s: %s", __func__, error->msg);
1620 goto done;
1623 error = gotweb_render_tags(c);
1624 if (error) {
1625 log_warnx("%s: %s", __func__, error->msg);
1626 goto done;
1629 error = gotweb_render_branches(c);
1630 if (error)
1631 log_warnx("%s: %s", __func__, error->msg);
1632 done:
1633 return error;
1636 static const struct got_error *
1637 gotweb_render_tag(struct request *c)
1639 const struct got_error *error = NULL;
1640 struct repo_tag *rt = NULL;
1641 struct transport *t = c->t;
1642 char *age = NULL, *author = NULL;
1644 error = got_get_repo_tags(c, 1);
1645 if (error)
1646 goto done;
1648 if (t->tag_count == 0) {
1649 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1650 "bad commit id");
1651 goto done;
1654 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1656 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1657 if (error)
1658 goto done;
1659 error = gotweb_escape_html(&author, rt->tagger);
1660 if (error)
1661 goto done;
1663 if (strncmp(rt->tag_name, "refs/", 5) == 0)
1664 rt->tag_name += 5;
1666 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1667 "<div id='tags_title'>Tag</div>\n"
1668 "</div>\n" /* #tags_title_wrapper */
1669 "<div id='tags_content'>\n"
1670 "<div id='tag_header_wrapper'>\n"
1671 "<div id='tag_header'>\n"
1672 "<div class='header_commit_title'>Commit:</div>\n"
1673 "<div class='header_commit'>%s"
1674 " <span class='refs_str'>(%s)</span></div>\n"
1675 "<div class='header_author_title'>Tagger:</div>\n"
1676 "<div class='header_author'>%s</div>\n"
1677 "<div class='header_age_title'>Date:</div>\n"
1678 "<div class='header_age'>%s</div>\n"
1679 "<div id='header_commit_msg_title'>Message:</div>\n"
1680 "<div id='header_commit_msg'>%s</div>\n"
1681 "</div>\n" /* #tag_header */
1682 "<div class='dotted_line'></div>\n"
1683 "<div id='tag_commit'>\n%s</div>"
1684 "</div>", /* tag_header_wrapper */
1685 rt->commit_id,
1686 rt->tag_name,
1687 author ? author : "",
1688 age ? age : "",
1689 rt->commit_msg,
1690 rt->tag_commit);
1692 done:
1693 free(age);
1694 free(author);
1695 return error;
1698 static const struct got_error *
1699 gotweb_render_tags(struct request *c)
1701 const struct got_error *error = NULL;
1702 struct repo_tag *rt = NULL;
1703 struct server *srv = c->srv;
1704 struct transport *t = c->t;
1705 struct querystring *qs = t->qs;
1706 struct repo_dir *repo_dir = t->repo_dir;
1707 const char *index_page_str;
1708 char *newline;
1709 char *age = NULL;
1710 int r, commit_found = 0;
1712 index_page_str = qs->index_page_str ? qs->index_page_str : "";
1714 if (qs->action == BRIEFS) {
1715 qs->action = TAGS;
1716 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1717 } else
1718 error = got_get_repo_tags(c, srv->max_commits_display);
1719 if (error)
1720 goto done;
1722 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1723 "<div id='tags_title'>Tags</div>\n"
1724 "</div>\n" /* #tags_title_wrapper */
1725 "<div id='tags_content'>\n");
1726 if (r == -1)
1727 goto done;
1729 if (t->tag_count == 0) {
1730 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1731 "This repository contains no tags");
1732 if (r == -1)
1733 goto done;
1736 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1737 if (commit_found == 0 && qs->commit != NULL) {
1738 if (strcmp(qs->commit, rt->commit_id) != 0)
1739 continue;
1740 else
1741 commit_found = 1;
1743 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1744 if (error)
1745 goto done;
1747 if (strncmp(rt->tag_name, "refs/tags/", 10) == 0)
1748 rt->tag_name += 10;
1750 if (rt->tag_commit != NULL) {
1751 newline = strchr(rt->tag_commit, '\n');
1752 if (newline)
1753 *newline = '\0';
1756 r = fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1757 "<div class='tag'>%s</div>\n"
1758 "<div class='tag_log'>"
1759 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1760 "%s</a>"
1761 "</div>\n" /* .tag_log */
1762 "<div class='navs_wrapper'>\n"
1763 "<div class='navs'>"
1764 "<a href='?index_page=%s&path=%s&action=tag&commit=%s'>"
1765 "tag</a>"
1766 " | "
1767 "<a href='?index_page=%s&path=%s&action=briefs&commit=%s'>"
1768 "commit briefs</a>"
1769 " | "
1770 "<a href='?index_page=%s&path=%s&action=commits&commit=%s'>"
1771 "commits</a>"
1772 "</div>\n" /* .navs */
1773 "</div>\n" /* .navs_wrapper */
1774 "<div class='dotted_line'></div>\n",
1775 age ? age : "",
1776 rt->tag_name,
1777 index_page_str, repo_dir->name, rt->commit_id,
1778 rt->tag_commit ? rt->tag_commit : "",
1779 index_page_str, repo_dir->name, rt->commit_id,
1780 index_page_str, repo_dir->name, rt->commit_id,
1781 index_page_str, repo_dir->name, rt->commit_id);
1782 if (r == -1)
1783 goto done;
1785 free(age);
1786 age = NULL;
1788 if (t->next_id || t->prev_id) {
1789 error = gotweb_render_navs(c);
1790 if (error)
1791 goto done;
1793 fcgi_printf(c, "</div>\n"); /* #tags_content */
1794 done:
1795 free(age);
1796 return error;
1799 const struct got_error *
1800 gotweb_escape_html(char **escaped_html, const char *orig_html)
1802 const struct got_error *error = NULL;
1803 struct escape_pair {
1804 char c;
1805 const char *s;
1806 } esc[] = {
1807 { '>', "&gt;" },
1808 { '<', "&lt;" },
1809 { '&', "&amp;" },
1810 { '"', "&quot;" },
1811 { '\'', "&apos;" },
1812 { '\n', "<br />" },
1814 size_t orig_len, len;
1815 int i, j, x;
1817 orig_len = strlen(orig_html);
1818 len = orig_len;
1819 for (i = 0; i < orig_len; i++) {
1820 for (j = 0; j < nitems(esc); j++) {
1821 if (orig_html[i] != esc[j].c)
1822 continue;
1823 len += strlen(esc[j].s) - 1 /* escaped char */;
1827 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1828 if (*escaped_html == NULL)
1829 return got_error_from_errno("calloc");
1831 x = 0;
1832 for (i = 0; i < orig_len; i++) {
1833 int escaped = 0;
1834 for (j = 0; j < nitems(esc); j++) {
1835 if (orig_html[i] != esc[j].c)
1836 continue;
1838 if (strlcat(*escaped_html, esc[j].s, len + 1)
1839 >= len + 1) {
1840 error = got_error(GOT_ERR_NO_SPACE);
1841 goto done;
1843 x += strlen(esc[j].s);
1844 escaped = 1;
1845 break;
1847 if (!escaped) {
1848 (*escaped_html)[x] = orig_html[i];
1849 x++;
1852 done:
1853 if (error) {
1854 free(*escaped_html);
1855 *escaped_html = NULL;
1856 } else {
1857 (*escaped_html)[x] = '\0';
1860 return error;
1863 static const struct got_error *
1864 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1866 const struct got_error *error = NULL;
1867 struct socket *sock = c->sock;
1868 struct server *srv = c->srv;
1869 struct transport *t = c->t;
1870 DIR *dt;
1871 char *dir_test;
1872 int opened = 0;
1874 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1875 GOTWEB_GIT_DIR) == -1)
1876 return got_error_from_errno("asprintf");
1878 dt = opendir(dir_test);
1879 if (dt == NULL) {
1880 free(dir_test);
1881 } else {
1882 repo_dir->path = strdup(dir_test);
1883 if (repo_dir->path == NULL) {
1884 opened = 1;
1885 error = got_error_from_errno("strdup");
1886 goto err;
1888 opened = 1;
1889 goto done;
1892 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1893 GOTWEB_GOT_DIR) == -1) {
1894 dir_test = NULL;
1895 error = got_error_from_errno("asprintf");
1896 goto err;
1899 dt = opendir(dir_test);
1900 if (dt == NULL)
1901 free(dir_test);
1902 else {
1903 opened = 1;
1904 error = got_error(GOT_ERR_NOT_GIT_REPO);
1905 goto err;
1908 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1909 repo_dir->name) == -1) {
1910 error = got_error_from_errno("asprintf");
1911 dir_test = NULL;
1912 goto err;
1915 repo_dir->path = strdup(dir_test);
1916 if (repo_dir->path == NULL) {
1917 opened = 1;
1918 error = got_error_from_errno("strdup");
1919 goto err;
1922 dt = opendir(dir_test);
1923 if (dt == NULL) {
1924 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1925 goto err;
1926 } else
1927 opened = 1;
1928 done:
1929 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1930 if (error)
1931 goto err;
1932 error = gotweb_get_repo_description(&repo_dir->description, srv,
1933 repo_dir->path);
1934 if (error)
1935 goto err;
1936 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
1937 if (error)
1938 goto err;
1939 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
1940 NULL, TM_DIFF);
1941 if (error)
1942 goto err;
1943 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
1944 err:
1945 free(dir_test);
1946 if (opened)
1947 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1948 error = got_error_from_errno("closedir");
1949 return error;
1952 static const struct got_error *
1953 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1955 const struct got_error *error;
1957 *repo_dir = calloc(1, sizeof(**repo_dir));
1958 if (*repo_dir == NULL)
1959 return got_error_from_errno("calloc");
1961 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1962 error = got_error_from_errno("asprintf");
1963 free(*repo_dir);
1964 *repo_dir = NULL;
1965 return error;
1967 (*repo_dir)->owner = NULL;
1968 (*repo_dir)->description = NULL;
1969 (*repo_dir)->url = NULL;
1970 (*repo_dir)->age = NULL;
1971 (*repo_dir)->path = NULL;
1973 return NULL;
1976 static const struct got_error *
1977 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
1979 const struct got_error *error = NULL;
1980 FILE *f = NULL;
1981 char *d_file = NULL;
1982 unsigned int len;
1983 size_t n;
1985 *description = NULL;
1986 if (srv->show_repo_description == 0)
1987 return NULL;
1989 if (asprintf(&d_file, "%s/description", dir) == -1)
1990 return got_error_from_errno("asprintf");
1992 f = fopen(d_file, "r");
1993 if (f == NULL) {
1994 if (errno == ENOENT || errno == EACCES)
1995 return NULL;
1996 error = got_error_from_errno2("fopen", d_file);
1997 goto done;
2000 if (fseek(f, 0, SEEK_END) == -1) {
2001 error = got_ferror(f, GOT_ERR_IO);
2002 goto done;
2004 len = ftell(f);
2005 if (len == -1) {
2006 error = got_ferror(f, GOT_ERR_IO);
2007 goto done;
2010 if (len == 0)
2011 goto done;
2013 if (fseek(f, 0, SEEK_SET) == -1) {
2014 error = got_ferror(f, GOT_ERR_IO);
2015 goto done;
2017 *description = calloc(len + 1, sizeof(**description));
2018 if (*description == NULL) {
2019 error = got_error_from_errno("calloc");
2020 goto done;
2023 n = fread(*description, 1, len, f);
2024 if (n == 0 && ferror(f))
2025 error = got_ferror(f, GOT_ERR_IO);
2026 done:
2027 if (f != NULL && fclose(f) == EOF && error == NULL)
2028 error = got_error_from_errno("fclose");
2029 free(d_file);
2030 return error;
2033 static const struct got_error *
2034 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2036 const struct got_error *error = NULL;
2037 FILE *f;
2038 char *d_file = NULL;
2039 unsigned int len;
2040 size_t n;
2042 *url = NULL;
2044 if (srv->show_repo_cloneurl == 0)
2045 return NULL;
2047 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2048 return got_error_from_errno("asprintf");
2050 f = fopen(d_file, "r");
2051 if (f == NULL) {
2052 if (errno != ENOENT && errno != EACCES)
2053 error = got_error_from_errno2("fopen", d_file);
2054 goto done;
2057 if (fseek(f, 0, SEEK_END) == -1) {
2058 error = got_ferror(f, GOT_ERR_IO);
2059 goto done;
2061 len = ftell(f);
2062 if (len == -1) {
2063 error = got_ferror(f, GOT_ERR_IO);
2064 goto done;
2066 if (len == 0)
2067 goto done;
2069 if (fseek(f, 0, SEEK_SET) == -1) {
2070 error = got_ferror(f, GOT_ERR_IO);
2071 goto done;
2074 *url = calloc(len + 1, sizeof(**url));
2075 if (*url == NULL) {
2076 error = got_error_from_errno("calloc");
2077 goto done;
2080 n = fread(*url, 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 const struct got_error *
2091 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2093 struct tm tm;
2094 long long diff_time;
2095 const char *years = "years ago", *months = "months ago";
2096 const char *weeks = "weeks ago", *days = "days ago";
2097 const char *hours = "hours ago", *minutes = "minutes ago";
2098 const char *seconds = "seconds ago", *now = "right now";
2099 char *s;
2100 char datebuf[29];
2102 *repo_age = NULL;
2104 switch (ref_tm) {
2105 case TM_DIFF:
2106 diff_time = time(NULL) - committer_time;
2107 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2108 if (asprintf(repo_age, "%lld %s",
2109 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2110 return got_error_from_errno("asprintf");
2111 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2112 if (asprintf(repo_age, "%lld %s",
2113 (diff_time / 60 / 60 / 24 / (365 / 12)),
2114 months) == -1)
2115 return got_error_from_errno("asprintf");
2116 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2117 if (asprintf(repo_age, "%lld %s",
2118 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2119 return got_error_from_errno("asprintf");
2120 } else if (diff_time > 60 * 60 * 24 * 2) {
2121 if (asprintf(repo_age, "%lld %s",
2122 (diff_time / 60 / 60 / 24), days) == -1)
2123 return got_error_from_errno("asprintf");
2124 } else if (diff_time > 60 * 60 * 2) {
2125 if (asprintf(repo_age, "%lld %s",
2126 (diff_time / 60 / 60), hours) == -1)
2127 return got_error_from_errno("asprintf");
2128 } else if (diff_time > 60 * 2) {
2129 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2130 minutes) == -1)
2131 return got_error_from_errno("asprintf");
2132 } else if (diff_time > 2) {
2133 if (asprintf(repo_age, "%lld %s", diff_time,
2134 seconds) == -1)
2135 return got_error_from_errno("asprintf");
2136 } else {
2137 if (asprintf(repo_age, "%s", now) == -1)
2138 return got_error_from_errno("asprintf");
2140 break;
2141 case TM_LONG:
2142 if (gmtime_r(&committer_time, &tm) == NULL)
2143 return got_error_from_errno("gmtime_r");
2145 s = asctime_r(&tm, datebuf);
2146 if (s == NULL)
2147 return got_error_from_errno("asctime_r");
2149 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2150 return got_error_from_errno("asprintf");
2151 break;
2153 return NULL;