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) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
54 enum gotweb_ref_tm {
55 TM_DIFF,
56 TM_LONG,
57 };
59 static const struct querystring_keys querystring_keys[] = {
60 { "action", ACTION },
61 { "commit", COMMIT },
62 { "file", RFILE },
63 { "folder", FOLDER },
64 { "headref", HEADREF },
65 { "index_page", INDEX_PAGE },
66 { "path", PATH },
67 { "page", PAGE },
68 };
70 static const struct action_keys action_keys[] = {
71 { "blame", BLAME },
72 { "blob", BLOB },
73 { "briefs", BRIEFS },
74 { "commits", COMMITS },
75 { "diff", DIFF },
76 { "error", ERR },
77 { "index", INDEX },
78 { "summary", SUMMARY },
79 { "tag", TAG },
80 { "tags", TAGS },
81 { "tree", TREE },
82 };
84 static const struct got_error *gotweb_init_querystring(struct querystring **);
85 static const struct got_error *gotweb_parse_querystring(struct querystring **,
86 char *);
87 static const struct got_error *gotweb_assign_querystring(struct querystring **,
88 char *, char *);
89 static const struct got_error *gotweb_render_header(struct request *);
90 static const struct got_error *gotweb_render_footer(struct request *);
91 static const struct got_error *gotweb_render_index(struct request *);
92 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
93 const char *);
94 static const struct got_error *gotweb_load_got_path(struct request *c,
95 struct repo_dir *);
96 static const struct got_error *gotweb_get_repo_description(char **,
97 struct server *, const char *, int);
98 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
99 const char *, int);
100 static const struct got_error *gotweb_render_navs(struct request *);
101 static const struct got_error *gotweb_render_blame(struct request *);
102 static const struct got_error *gotweb_render_briefs(struct request *);
103 static const struct got_error *gotweb_render_commits(struct request *);
104 static const struct got_error *gotweb_render_diff(struct request *);
105 static const struct got_error *gotweb_render_summary(struct request *);
106 static const struct got_error *gotweb_render_tag(struct request *);
107 static const struct got_error *gotweb_render_tags(struct request *);
108 static const struct got_error *gotweb_render_tree(struct request *);
109 static const struct got_error *gotweb_render_branches(struct request *);
111 static void gotweb_free_querystring(struct querystring *);
112 static void gotweb_free_repo_dir(struct repo_dir *);
114 struct server *gotweb_get_server(uint8_t *, uint8_t *);
116 void
117 gotweb_process_request(struct request *c)
119 const struct got_error *error = NULL, *error2 = NULL;
120 struct server *srv = NULL;
121 struct querystring *qs = NULL;
122 struct repo_dir *repo_dir = NULL;
123 uint8_t err[] = "gotwebd experienced an error: ";
124 int r, html = 0;
126 /* init the transport */
127 error = gotweb_init_transport(&c->t);
128 if (error) {
129 log_warnx("%s: %s", __func__, error->msg);
130 return;
132 /* don't process any further if client disconnected */
133 if (c->sock->client_status == CLIENT_DISCONNECT)
134 return;
135 /* get the gotwebd server */
136 srv = gotweb_get_server(c->server_name, c->http_host);
137 if (srv == NULL) {
138 log_warnx("%s: error server is NULL", __func__);
139 goto err;
141 c->srv = srv;
142 /* parse our querystring */
143 error = gotweb_init_querystring(&qs);
144 if (error) {
145 log_warnx("%s: %s", __func__, error->msg);
146 goto err;
148 c->t->qs = qs;
149 error = gotweb_parse_querystring(&qs, c->querystring);
150 if (error) {
151 log_warnx("%s: %s", __func__, error->msg);
152 goto err;
155 /*
156 * certain actions require a commit id in the querystring. this stops
157 * bad actors from exploiting this by manually manipulating the
158 * querystring.
159 */
161 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
162 qs->action == DIFF)) {
163 error2 = got_error(GOT_ERR_QUERYSTRING);
164 goto render;
167 if (qs->action != INDEX) {
168 error = gotweb_init_repo_dir(&repo_dir, qs->path);
169 if (error)
170 goto done;
171 error = gotweb_load_got_path(c, repo_dir);
172 c->t->repo_dir = repo_dir;
173 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
174 goto err;
177 /* render top of page */
178 if (qs != NULL && qs->action == BLOB) {
179 error = got_get_repo_commits(c, 1);
180 if (error)
181 goto done;
182 error = got_output_file_blob(c);
183 if (error) {
184 log_warnx("%s: %s", __func__, error->msg);
185 goto err;
187 goto done;
190 render:
191 error = gotweb_render_content_type(c, "text/html");
192 if (error) {
193 log_warnx("%s: %s", __func__, error->msg);
194 goto err;
196 html = 1;
198 error = gotweb_render_header(c);
199 if (error) {
200 log_warnx("%s: %s", __func__, error->msg);
201 goto err;
204 if (error2) {
205 error = error2;
206 goto err;
209 switch(qs->action) {
210 case BLAME:
211 error = gotweb_render_blame(c);
212 if (error) {
213 log_warnx("%s: %s", __func__, error->msg);
214 goto err;
216 break;
217 case BRIEFS:
218 error = gotweb_render_briefs(c);
219 if (error) {
220 log_warnx("%s: %s", __func__, error->msg);
221 goto err;
223 break;
224 case COMMITS:
225 error = gotweb_render_commits(c);
226 if (error) {
227 log_warnx("%s: %s", __func__, error->msg);
228 goto err;
230 break;
231 case DIFF:
232 error = gotweb_render_diff(c);
233 if (error) {
234 log_warnx("%s: %s", __func__, error->msg);
235 goto err;
237 break;
238 case INDEX:
239 error = gotweb_render_index(c);
240 if (error) {
241 log_warnx("%s: %s", __func__, error->msg);
242 goto err;
244 break;
245 case SUMMARY:
246 error = gotweb_render_summary(c);
247 if (error) {
248 log_warnx("%s: %s", __func__, error->msg);
249 goto err;
251 break;
252 case TAG:
253 error = gotweb_render_tag(c);
254 if (error) {
255 log_warnx("%s: %s", __func__, error->msg);
256 goto err;
258 break;
259 case TAGS:
260 error = gotweb_render_tags(c);
261 if (error) {
262 log_warnx("%s: %s", __func__, error->msg);
263 goto err;
265 break;
266 case TREE:
267 error = gotweb_render_tree(c);
268 if (error) {
269 log_warnx("%s: %s", __func__, error->msg);
270 goto err;
272 break;
273 case ERR:
274 default:
275 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
276 "Erorr: Bad Querystring");
277 if (r == -1)
278 goto err;
279 break;
282 goto done;
283 err:
284 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
285 return;
286 if (fcgi_printf(c, "\n%s", err) == -1)
287 return;
288 if (error) {
289 if (fcgi_printf(c, "%s", error->msg) == -1)
290 return;
291 } else {
292 if (fcgi_printf(c, "see daemon logs for details") == -1)
293 return;
295 if (html && fcgi_printf(c, "</div>\n") == -1)
296 return;
297 done:
298 if (html && srv != NULL)
299 gotweb_render_footer(c);
302 struct server *
303 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
305 struct server *srv = NULL;
307 /* check against the server name 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 subdomain second */
314 if (strlen(subdomain) > 0)
315 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
316 if (strcmp(srv->name, subdomain) == 0)
317 goto done;
319 /* if those fail, send first server */
320 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
321 if (srv != NULL)
322 break;
323 done:
324 return srv;
325 };
327 const struct got_error *
328 gotweb_init_transport(struct transport **t)
330 const struct got_error *error = NULL;
332 *t = calloc(1, sizeof(**t));
333 if (*t == NULL)
334 return got_error_from_errno2("%s: calloc", __func__);
336 TAILQ_INIT(&(*t)->repo_commits);
337 TAILQ_INIT(&(*t)->repo_tags);
339 (*t)->repo = NULL;
340 (*t)->repo_dir = NULL;
341 (*t)->qs = NULL;
342 (*t)->next_id = NULL;
343 (*t)->prev_id = NULL;
344 (*t)->next_disp = 0;
345 (*t)->prev_disp = 0;
347 return error;
350 static const struct got_error *
351 gotweb_init_querystring(struct querystring **qs)
353 const struct got_error *error = NULL;
355 *qs = calloc(1, sizeof(**qs));
356 if (*qs == NULL)
357 return got_error_from_errno2("%s: calloc", __func__);
359 (*qs)->headref = strdup("HEAD");
360 if ((*qs)->headref == NULL) {
361 free(*qs);
362 *qs = NULL;
363 return got_error_from_errno2("%s: strdup", __func__);
366 (*qs)->action = INDEX;
367 (*qs)->commit = NULL;
368 (*qs)->file = NULL;
369 (*qs)->folder = NULL;
370 (*qs)->index_page = 0;
371 (*qs)->index_page_str = NULL;
372 (*qs)->path = NULL;
374 return error;
377 static const struct got_error *
378 gotweb_parse_querystring(struct querystring **qs, char *qst)
380 const struct got_error *error = NULL;
381 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
382 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
384 if (qst == NULL)
385 return error;
387 tok1 = strdup(qst);
388 if (tok1 == NULL)
389 return got_error_from_errno2("%s: strdup", __func__);
391 tok1_pair = tok1;
392 tok1_end = tok1;
394 while (tok1_pair != NULL) {
395 strsep(&tok1_end, "&");
397 tok2 = strdup(tok1_pair);
398 if (tok2 == NULL) {
399 free(tok1);
400 return got_error_from_errno2("%s: strdup", __func__);
403 tok2_pair = tok2;
404 tok2_end = tok2;
406 while (tok2_pair != NULL) {
407 strsep(&tok2_end, "=");
408 if (tok2_end) {
409 error = gotweb_assign_querystring(qs, tok2_pair,
410 tok2_end);
411 if (error)
412 goto err;
414 tok2_pair = tok2_end;
416 free(tok2);
417 tok1_pair = tok1_end;
419 free(tok1);
420 return error;
421 err:
422 free(tok2);
423 free(tok1);
424 return error;
427 /*
428 * Adapted from usr.sbin/httpd/httpd.c url_decode.
429 */
430 static const struct got_error *
431 gotweb_urldecode(char *url)
433 char *p, *q;
434 char hex[3];
435 unsigned long x;
437 hex[2] = '\0';
438 p = q = url;
440 while (*p != '\0') {
441 switch (*p) {
442 case '%':
443 /* Encoding character is followed by two hex chars */
444 if (!isxdigit((unsigned char)p[1]) ||
445 !isxdigit((unsigned char)p[2]) ||
446 (p[1] == '0' && p[2] == '0'))
447 return got_error(GOT_ERR_BAD_QUERYSTRING);
449 hex[0] = p[1];
450 hex[1] = p[2];
452 /*
453 * We don't have to validate "hex" because it is
454 * guaranteed to include two hex chars followed by nul.
455 */
456 x = strtoul(hex, NULL, 16);
457 *q = (char)x;
458 p += 2;
459 break;
460 default:
461 *q = *p;
462 break;
464 p++;
465 q++;
467 *q = '\0';
469 return NULL;
472 static const struct got_error *
473 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
475 const struct got_error *error = NULL;
476 const char *errstr;
477 int a_cnt, el_cnt;
479 error = gotweb_urldecode(value);
480 if (error)
481 return error;
483 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
484 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
485 continue;
487 switch (querystring_keys[el_cnt].element) {
488 case ACTION:
489 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
490 if (strcmp(value, action_keys[a_cnt].name) != 0)
491 continue;
492 else if (strcmp(value,
493 action_keys[a_cnt].name) == 0){
494 (*qs)->action =
495 action_keys[a_cnt].action;
496 goto qa_found;
499 (*qs)->action = ERR;
500 qa_found:
501 break;
502 case COMMIT:
503 (*qs)->commit = strdup(value);
504 if ((*qs)->commit == NULL) {
505 error = got_error_from_errno2("%s: strdup",
506 __func__);
507 goto done;
509 break;
510 case RFILE:
511 (*qs)->file = strdup(value);
512 if ((*qs)->file == NULL) {
513 error = got_error_from_errno2("%s: strdup",
514 __func__);
515 goto done;
517 break;
518 case FOLDER:
519 (*qs)->folder = strdup(value);
520 if ((*qs)->folder == NULL) {
521 error = got_error_from_errno2("%s: strdup",
522 __func__);
523 goto done;
525 break;
526 case HEADREF:
527 free((*qs)->headref);
528 (*qs)->headref = strdup(value);
529 if ((*qs)->headref == NULL) {
530 error = got_error_from_errno2("%s: strdup",
531 __func__);
532 goto done;
534 break;
535 case INDEX_PAGE:
536 if (strlen(value) == 0)
537 break;
538 (*qs)->index_page_str = strdup(value);
539 if ((*qs)->index_page_str == NULL) {
540 error = got_error_from_errno2("%s: strdup",
541 __func__);
542 goto done;
544 (*qs)->index_page = strtonum(value, INT64_MIN,
545 INT64_MAX, &errstr);
546 if (errstr) {
547 error = got_error_from_errno3("%s: strtonum %s",
548 __func__, errstr);
549 goto done;
551 if ((*qs)->index_page < 0) {
552 (*qs)->index_page = 0;
553 sprintf((*qs)->index_page_str, "%d", 0);
555 break;
556 case PATH:
557 (*qs)->path = strdup(value);
558 if ((*qs)->path == NULL) {
559 error = got_error_from_errno2("%s: strdup",
560 __func__);
561 goto done;
563 break;
564 case PAGE:
565 if (strlen(value) == 0)
566 break;
567 (*qs)->page_str = strdup(value);
568 if ((*qs)->page_str == NULL) {
569 error = got_error_from_errno2("%s: strdup",
570 __func__);
571 goto done;
573 (*qs)->page = strtonum(value, INT64_MIN,
574 INT64_MAX, &errstr);
575 if (errstr) {
576 error = got_error_from_errno3("%s: strtonum %s",
577 __func__, errstr);
578 goto done;
580 if ((*qs)->page < 0) {
581 (*qs)->page = 0;
582 sprintf((*qs)->page_str, "%d", 0);
584 break;
585 default:
586 break;
589 done:
590 return error;
593 void
594 gotweb_free_repo_tag(struct repo_tag *rt)
596 if (rt != NULL) {
597 free(rt->commit_id);
598 free(rt->tag_name);
599 free(rt->tag_commit);
600 free(rt->commit_msg);
601 free(rt->tagger);
603 free(rt);
606 void
607 gotweb_free_repo_commit(struct repo_commit *rc)
609 if (rc != NULL) {
610 free(rc->path);
611 free(rc->refs_str);
612 free(rc->commit_id);
613 free(rc->parent_id);
614 free(rc->tree_id);
615 free(rc->author);
616 free(rc->committer);
617 free(rc->commit_msg);
619 free(rc);
622 static void
623 gotweb_free_querystring(struct querystring *qs)
625 if (qs != NULL) {
626 free(qs->commit);
627 free(qs->file);
628 free(qs->folder);
629 free(qs->headref);
630 free(qs->index_page_str);
631 free(qs->path);
632 free(qs->page_str);
634 free(qs);
637 static void
638 gotweb_free_repo_dir(struct repo_dir *repo_dir)
640 if (repo_dir != NULL) {
641 free(repo_dir->name);
642 free(repo_dir->owner);
643 free(repo_dir->description);
644 free(repo_dir->url);
645 free(repo_dir->age);
646 free(repo_dir->path);
648 free(repo_dir);
651 void
652 gotweb_free_transport(struct transport *t)
654 struct repo_commit *rc = NULL, *trc = NULL;
655 struct repo_tag *rt = NULL, *trt = NULL;
657 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
658 TAILQ_REMOVE(&t->repo_commits, rc, entry);
659 gotweb_free_repo_commit(rc);
661 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
662 TAILQ_REMOVE(&t->repo_tags, rt, entry);
663 gotweb_free_repo_tag(rt);
665 gotweb_free_repo_dir(t->repo_dir);
666 gotweb_free_querystring(t->qs);
667 free(t->next_id);
668 free(t->prev_id);
669 free(t);
672 const struct got_error *
673 gotweb_render_content_type(struct request *c, const uint8_t *type)
675 const char *csp = "default-src 'self'; script-src 'none'; "
676 "object-src 'none';";
678 fcgi_printf(c,
679 "Content-Security-Policy: %s\r\n"
680 "Content-Type: %s\r\n\r\n",
681 csp, type);
682 return NULL;
685 const struct got_error *
686 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
687 char *file)
689 fcgi_printf(c, "Content-type: %s\r\n"
690 "Content-disposition: attachment; filename=%s\r\n\r\n",
691 type, file);
692 return NULL;
695 static const struct got_error *
696 gotweb_render_header(struct request *c)
698 const struct got_error *err = NULL;
699 struct server *srv = c->srv;
700 struct querystring *qs = c->t->qs;
701 int r;
703 r = fcgi_printf(c, "<!doctype html>\n"
704 "<html>\n"
705 "<head>\n"
706 "<title>%s</title>\n"
707 "<meta charset='utf-8' />\n"
708 "<meta name='viewport' content='initial-scale=.75' />\n"
709 "<meta name='msapplication-TileColor' content='#da532c' />\n"
710 "<meta name='theme-color' content='#ffffff'/>\n"
711 "<link rel='apple-touch-icon' sizes='180x180'"
712 " href='%sapple-touch-icon.png' />\n"
713 "<link rel='icon' type='image/png' sizes='32x32'"
714 " href='%sfavicon-32x32.png' />\n"
715 "<link rel='icon' type='image/png' sizes='16x16'"
716 " href='%sfavicon-16x16.png' />\n"
717 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
718 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
719 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
720 "</head>\n"
721 "<body>\n"
722 "<div id='gw_body'>\n"
723 "<div id='header'>\n"
724 "<div id='got_link'>"
725 "<a href='%s' target='_blank'>"
726 "<img src='%s%s' alt='logo' id='logo' />"
727 "</a>\n"
728 "</div>\n" /* #got_link */
729 "</div>\n" /* #header */
730 "<div id='site_path'>\n"
731 "<div id='site_link'>\n"
732 "<a href='?index_page=%d'>%s</a>",
733 srv->site_name,
734 c->script_name,
735 c->script_name,
736 c->script_name,
737 c->script_name,
738 c->script_name,
739 c->script_name, srv->custom_css,
740 srv->logo_url,
741 c->script_name, srv->logo,
742 qs->index_page, srv->site_link);
743 if (r == -1)
744 goto done;
746 if (qs->path != NULL) {
747 char *epath;
749 if (fcgi_printf(c, " / ") == -1)
750 goto done;
752 err = gotweb_escape_html(&epath, qs->path);
753 if (err)
754 return err;
755 r = gotweb_link(c, &(struct gotweb_url){
756 .action = SUMMARY,
757 .index_page = -1,
758 .page = -1,
759 .path = qs->path,
760 }, "%s", epath);
761 free(epath);
762 if (r == -1)
763 goto done;
765 if (qs->action != INDEX) {
766 const char *action = "";
768 switch (qs->action) {
769 case BLAME:
770 action = "blame";
771 break;
772 case BRIEFS:
773 action = "briefs";
774 break;
775 case COMMITS:
776 action = "commits";
777 break;
778 case DIFF:
779 action = "diff";
780 break;
781 case SUMMARY:
782 action = "summary";
783 break;
784 case TAG:
785 action = "tag";
786 break;
787 case TAGS:
788 action = "tags";
789 break;
790 case TREE:
791 action = "tree";
792 break;
795 if (fcgi_printf(c, " / %s", action) == -1)
796 goto done;
799 fcgi_printf(c, "</div>\n" /* #site_path */
800 "</div>\n" /* #site_link */
801 "<div id='content'>\n");
803 done:
804 return NULL;
807 static const struct got_error *
808 gotweb_render_footer(struct request *c)
810 const struct got_error *error = NULL;
811 struct server *srv = c->srv;
812 const char *siteowner = "&nbsp;";
813 char *escaped_owner = NULL;
815 if (srv->show_site_owner) {
816 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
817 if (error)
818 return error;
819 siteowner = escaped_owner;
822 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
823 "<div id='site_owner'>%s</div>\n"
824 "</div>\n" /* #site_owner_wrapper */
825 "</div>\n" /* #content */
826 "</div>\n" /* #gw_body */
827 "</body>\n</html>\n", siteowner);
829 free(escaped_owner);
830 return NULL;
833 static const struct got_error *
834 gotweb_render_navs(struct request *c)
836 const struct got_error *error = NULL;
837 struct transport *t = c->t;
838 struct querystring *qs = t->qs;
839 struct server *srv = c->srv;
840 int r;
842 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
843 if (r == -1)
844 goto done;
846 switch(qs->action) {
847 case INDEX:
848 if (qs->index_page > 0) {
849 struct gotweb_url url = {
850 .action = -1,
851 .index_page = qs->index_page - 1,
852 .page = -1,
853 };
855 r = gotweb_link(c, &url, "Previous");
857 break;
858 case BRIEFS:
859 if (t->prev_id && qs->commit != NULL &&
860 strcmp(qs->commit, t->prev_id) != 0) {
861 struct gotweb_url url = {
862 .action = BRIEFS,
863 .index_page = -1,
864 .page = qs->page - 1,
865 .path = qs->path,
866 .commit = t->prev_id,
867 .headref = qs->headref,
868 };
870 r = gotweb_link(c, &url, "Previous");
872 break;
873 case COMMITS:
874 if (t->prev_id && qs->commit != NULL &&
875 strcmp(qs->commit, t->prev_id) != 0) {
876 struct gotweb_url url = {
877 .action = COMMIT,
878 .index_page = -1,
879 .page = qs->page - 1,
880 .path = qs->path,
881 .commit = t->prev_id,
882 .headref = qs->headref,
883 .folder = qs->folder,
884 .file = qs->file,
885 };
887 r = gotweb_link(c, &url, "Previous");
889 break;
890 case TAGS:
891 if (t->prev_id && qs->commit != NULL &&
892 strcmp(qs->commit, t->prev_id) != 0) {
893 struct gotweb_url url = {
894 .action = TAGS,
895 .index_page = -1,
896 .page = qs->page - 1,
897 .path = qs->path,
898 .commit = t->prev_id,
899 .headref = qs->headref,
900 };
902 r = gotweb_link(c, &url, "Previous");
904 break;
907 if (r == -1)
908 goto done;
910 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
911 "<div id='nav_next'>");
912 if (r == -1)
913 goto done;
915 switch(qs->action) {
916 case INDEX:
917 if (t->next_disp == srv->max_repos_display &&
918 t->repos_total != (qs->index_page + 1) *
919 srv->max_repos_display) {
920 struct gotweb_url url = {
921 .action = -1,
922 .index_page = qs->index_page + 1,
923 .page = -1,
924 };
926 r = gotweb_link(c, &url, "Next");
928 break;
929 case BRIEFS:
930 if (t->next_id) {
931 struct gotweb_url url = {
932 .action = BRIEFS,
933 .index_page = -1,
934 .page = qs->page + 1,
935 .path = qs->path,
936 .commit = t->next_id,
937 .headref = qs->headref,
938 };
940 r = gotweb_link(c, &url, "Next");
942 break;
943 case COMMITS:
944 if (t->next_id) {
945 struct gotweb_url url = {
946 .action = COMMIT,
947 .index_page = -1,
948 .page = qs->page + 1,
949 .path = qs->path,
950 .commit = t->next_id,
951 .headref = qs->headref,
952 .folder = qs->folder,
953 .file = qs->file,
954 };
956 r = gotweb_link(c, &url, "Next");
958 break;
959 case TAGS:
960 if (t->next_id) {
961 struct gotweb_url url = {
962 .action = TAGS,
963 .index_page = -1,
964 .page = qs->page + 1,
965 .path = qs->path,
966 .commit = t->next_id,
967 .headref = qs->headref,
968 };
970 r = gotweb_link(c, &url, "Next");
972 break;
974 if (r == -1)
975 goto done;
977 fcgi_printf(c, "</div>\n"); /* #nav_next */
978 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
979 done:
980 free(t->next_id);
981 t->next_id = NULL;
982 free(t->prev_id);
983 t->prev_id = NULL;
984 return error;
987 static const struct got_error *
988 gotweb_render_index(struct request *c)
990 const struct got_error *error = NULL;
991 struct server *srv = c->srv;
992 struct transport *t = c->t;
993 struct querystring *qs = t->qs;
994 struct repo_dir *repo_dir = NULL;
995 DIR *d;
996 struct dirent **sd_dent = NULL;
997 unsigned int d_cnt, d_i, d_disp = 0;
998 unsigned int d_skipped = 0;
999 int r, type;
1001 d = opendir(srv->repos_path);
1002 if (d == NULL) {
1003 error = got_error_from_errno2("opendir", srv->repos_path);
1004 return error;
1007 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1008 if (d_cnt == -1) {
1009 sd_dent = NULL;
1010 error = got_error_from_errno2("scandir", srv->repos_path);
1011 goto done;
1014 r = fcgi_printf(c, "<div id='index_header'>\n"
1015 "<div id='index_header_project'>Project</div>\n");
1016 if (r == -1)
1017 goto done;
1019 if (srv->show_repo_description)
1020 if (fcgi_printf(c, "<div id='index_header_description'>"
1021 "Description</div>\n") == -1)
1022 goto done;
1023 if (srv->show_repo_owner)
1024 if (fcgi_printf(c, "<div id='index_header_owner'>"
1025 "Owner</div>\n") == -1)
1026 goto done;
1027 if (srv->show_repo_age)
1028 if (fcgi_printf(c, "<div id='index_header_age'>"
1029 "Last Change</div>\n") == -1)
1030 goto done;
1031 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1032 goto done;
1034 for (d_i = 0; d_i < d_cnt; d_i++) {
1035 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
1036 break;
1038 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1039 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
1040 d_skipped++;
1041 continue;
1044 error = got_path_dirent_type(&type, srv->repos_path,
1045 sd_dent[d_i]);
1046 if (error)
1047 goto done;
1048 if (type != DT_DIR) {
1049 d_skipped++;
1050 continue;
1053 if (qs->index_page > 0 && (qs->index_page *
1054 srv->max_repos_display) > t->prev_disp) {
1055 t->prev_disp++;
1056 continue;
1059 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1060 if (error)
1061 goto done;
1063 error = gotweb_load_got_path(c, repo_dir);
1064 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1065 error = NULL;
1066 gotweb_free_repo_dir(repo_dir);
1067 repo_dir = NULL;
1068 d_skipped++;
1069 continue;
1071 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1072 goto done;
1074 d_disp++;
1075 t->prev_disp++;
1077 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1078 "<div class='index_project'>") == -1)
1079 goto done;
1081 r = gotweb_link(c, &(struct gotweb_url){
1082 .action = SUMMARY,
1083 .index_page = -1,
1084 .page = -1,
1085 .path = repo_dir->name,
1086 }, "%s", repo_dir->name);
1087 if (r == -1)
1088 goto done;
1090 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1091 goto done;
1093 if (srv->show_repo_description) {
1094 r = fcgi_printf(c,
1095 "<div class='index_project_description'>\n"
1096 "%s</div>\n", repo_dir->description);
1097 if (r == -1)
1098 goto done;
1101 if (srv->show_repo_owner) {
1102 r = fcgi_printf(c, "<div class='index_project_owner'>"
1103 "%s</div>\n", repo_dir->owner);
1104 if (r == -1)
1105 goto done;
1108 if (srv->show_repo_age) {
1109 r = fcgi_printf(c, "<div class='index_project_age'>"
1110 "%s</div>\n", repo_dir->age);
1111 if (r == -1)
1112 goto done;
1115 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1116 "<div class='navs'>") == -1)
1117 goto done;
1119 r = gotweb_link(c, &(struct gotweb_url){
1120 .action = SUMMARY,
1121 .index_page = -1,
1122 .page = -1,
1123 .path = repo_dir->name
1124 }, "summary");
1125 if (r == -1)
1126 goto done;
1128 if (fcgi_printf(c, " | ") == -1)
1129 goto done;
1131 r = gotweb_link(c, &(struct gotweb_url){
1132 .action = BRIEFS,
1133 .index_page = -1,
1134 .page = -1,
1135 .path = repo_dir->name
1136 }, "commit briefs");
1137 if (r == -1)
1138 goto done;
1140 if (fcgi_printf(c, " | ") == -1)
1141 goto done;
1143 r = gotweb_link(c, &(struct gotweb_url){
1144 .action = COMMITS,
1145 .index_page = -1,
1146 .page = -1,
1147 .path = repo_dir->name
1148 }, "commits");
1149 if (r == -1)
1150 goto done;
1152 if (fcgi_printf(c, " | ") == -1)
1153 goto done;
1155 r = gotweb_link(c, &(struct gotweb_url){
1156 .action = TAGS,
1157 .index_page = -1,
1158 .page = -1,
1159 .path = repo_dir->name
1160 }, "tags");
1161 if (r == -1)
1162 goto done;
1164 if (fcgi_printf(c, " | ") == -1)
1165 goto done;
1167 r = gotweb_link(c, &(struct gotweb_url){
1168 .action = TREE,
1169 .index_page = -1,
1170 .page = -1,
1171 .path = repo_dir->name
1172 }, "tree");
1173 if (r == -1)
1174 goto done;
1176 r = fcgi_printf(c, "</div>" /* .navs */
1177 "<div class='dotted_line'></div>\n"
1178 "</div>\n" /* .navs_wrapper */
1179 "</div>\n"); /* .index_wrapper */
1180 if (r == -1)
1181 goto done;
1183 gotweb_free_repo_dir(repo_dir);
1184 repo_dir = NULL;
1185 t->next_disp++;
1186 if (d_disp == srv->max_repos_display)
1187 break;
1189 t->repos_total = d_cnt - d_skipped;
1191 if (srv->max_repos_display == 0)
1192 goto done;
1193 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1194 goto done;
1195 if (t->repos_total <= srv->max_repos ||
1196 t->repos_total <= srv->max_repos_display)
1197 goto done;
1199 error = gotweb_render_navs(c);
1200 if (error)
1201 goto done;
1202 done:
1203 if (sd_dent) {
1204 for (d_i = 0; d_i < d_cnt; d_i++)
1205 free(sd_dent[d_i]);
1206 free(sd_dent);
1208 if (d != NULL && closedir(d) == EOF && error == NULL)
1209 error = got_error_from_errno("closedir");
1210 return error;
1213 static const struct got_error *
1214 gotweb_render_blame(struct request *c)
1216 const struct got_error *error = NULL;
1217 struct transport *t = c->t;
1218 struct repo_commit *rc = NULL;
1219 char *age = NULL, *msg = NULL;
1220 int r;
1222 error = got_get_repo_commits(c, 1);
1223 if (error)
1224 return error;
1226 rc = TAILQ_FIRST(&t->repo_commits);
1228 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1229 if (error)
1230 goto done;
1231 error = gotweb_escape_html(&msg, rc->commit_msg);
1232 if (error)
1233 goto done;
1235 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1236 "<div id='blame_title'>Blame</div>\n"
1237 "</div>\n" /* #blame_title_wrapper */
1238 "<div id='blame_content'>\n"
1239 "<div id='blame_header_wrapper'>\n"
1240 "<div id='blame_header'>\n"
1241 "<div class='header_age_title'>Date:</div>\n"
1242 "<div class='header_age'>%s</div>\n"
1243 "<div id='header_commit_msg_title'>Message:</div>\n"
1244 "<div id='header_commit_msg'>%s</div>\n"
1245 "</div>\n" /* #blame_header */
1246 "</div>\n" /* #blame_header_wrapper */
1247 "<div class='dotted_line'></div>\n"
1248 "<div id='blame'>\n",
1249 age,
1250 msg);
1251 if (r == -1)
1252 goto done;
1254 error = got_output_file_blame(c);
1255 if (error)
1256 goto done;
1258 fcgi_printf(c, "</div>\n" /* #blame */
1259 "</div>\n"); /* #blame_content */
1260 done:
1261 free(age);
1262 free(msg);
1263 return error;
1266 static const struct got_error *
1267 gotweb_render_briefs(struct request *c)
1269 const struct got_error *error = NULL;
1270 struct repo_commit *rc = NULL;
1271 struct server *srv = c->srv;
1272 struct transport *t = c->t;
1273 struct querystring *qs = t->qs;
1274 struct repo_dir *repo_dir = t->repo_dir;
1275 char *smallerthan, *newline;
1276 char *age = NULL, *author = NULL, *msg = NULL;
1277 int r;
1279 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1280 "<div id='briefs_title'>Commit Briefs</div>\n"
1281 "</div>\n" /* #briefs_title_wrapper */
1282 "<div id='briefs_content'>\n");
1283 if (r == -1)
1284 goto done;
1286 if (qs->action == SUMMARY) {
1287 qs->action = BRIEFS;
1288 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1289 } else
1290 error = got_get_repo_commits(c, srv->max_commits_display);
1291 if (error)
1292 goto done;
1294 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1295 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1296 if (error)
1297 goto done;
1299 smallerthan = strchr(rc->author, '<');
1300 if (smallerthan)
1301 *smallerthan = '\0';
1303 newline = strchr(rc->commit_msg, '\n');
1304 if (newline)
1305 *newline = '\0';
1307 error = gotweb_escape_html(&author, rc->author);
1308 if (error)
1309 goto done;
1310 error = gotweb_escape_html(&msg, rc->commit_msg);
1311 if (error)
1312 goto done;
1314 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1315 "<div class='briefs_author'>%s</div>\n"
1316 "<div class='briefs_log'>",
1317 age, author);
1318 if (r == -1)
1319 goto done;
1321 r = gotweb_link(c, &(struct gotweb_url){
1322 .action = DIFF,
1323 .index_page = -1,
1324 .page = -1,
1325 .path = repo_dir->name,
1326 .commit = rc->commit_id,
1327 .headref = qs->headref,
1328 }, "%s", msg);
1329 if (r == -1)
1330 goto done;
1332 if (rc->refs_str) {
1333 char *refs;
1335 error = gotweb_escape_html(&refs, rc->refs_str);
1336 if (error)
1337 goto done;
1338 r = fcgi_printf(c,
1339 " <span class='refs_str'>(%s)</span>", refs);
1340 free(refs);
1341 if (r == -1)
1342 goto done;
1344 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1345 goto done;
1347 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1348 "<div class='navs'>");
1349 if (r == -1)
1350 goto done;
1352 r = gotweb_link(c, &(struct gotweb_url){
1353 .action = DIFF,
1354 .index_page = -1,
1355 .page = -1,
1356 .path = repo_dir->name,
1357 .commit = rc->commit_id,
1358 .headref = qs->headref,
1359 }, "diff");
1360 if (r == -1)
1361 goto done;
1363 if (fcgi_printf(c, " | ") == -1)
1364 goto done;
1366 r = gotweb_link(c, &(struct gotweb_url){
1367 .action = TREE,
1368 .index_page = -1,
1369 .page = -1,
1370 .path = repo_dir->name,
1371 .commit = rc->commit_id,
1372 .headref = qs->headref,
1373 }, "tree");
1374 if (r == -1)
1375 goto done;
1377 if (fcgi_printf(c, "</div>\n" /* .navs */
1378 "</div>\n" /* .navs_wrapper */
1379 "<div class='dotted_line'></div>\n") == -1)
1380 goto done;
1382 free(age);
1383 age = NULL;
1384 free(author);
1385 author = NULL;
1386 free(msg);
1387 msg = NULL;
1390 if (t->next_id || t->prev_id) {
1391 error = gotweb_render_navs(c);
1392 if (error)
1393 goto done;
1395 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1396 done:
1397 free(age);
1398 free(author);
1399 free(msg);
1400 return error;
1403 static const struct got_error *
1404 gotweb_render_commits(struct request *c)
1406 const struct got_error *error = NULL;
1407 struct repo_commit *rc = NULL;
1408 struct server *srv = c->srv;
1409 struct transport *t = c->t;
1410 struct repo_dir *repo_dir = t->repo_dir;
1411 char *age = NULL, *author = NULL, *msg = NULL;
1412 int r;
1414 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1415 "<div class='commits_title'>Commits</div>\n"
1416 "</div>\n" /* .commits_title_wrapper */
1417 "<div class='commits_content'>\n");
1418 if (r == -1)
1419 goto done;
1421 error = got_get_repo_commits(c, srv->max_commits_display);
1422 if (error)
1423 goto done;
1425 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1426 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1427 if (error)
1428 goto done;
1429 error = gotweb_escape_html(&author, rc->author);
1430 if (error)
1431 goto done;
1432 error = gotweb_escape_html(&msg, rc->commit_msg);
1433 if (error)
1434 goto done;
1436 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1437 "<div class='commits_header'>\n"
1438 "<div class='header_commit_title'>Commit:</div>\n"
1439 "<div class='header_commit'>%s</div>\n"
1440 "<div class='header_author_title'>Author:</div>\n"
1441 "<div class='header_author'>%s</div>\n"
1442 "<div class='header_age_title'>Date:</div>\n"
1443 "<div class='header_age'>%s</div>\n"
1444 "</div>\n" /* .commits_header */
1445 "</div>\n" /* .commits_header_wrapper */
1446 "<div class='dotted_line'></div>\n"
1447 "<div class='commit'>\n%s</div>\n",
1448 rc->commit_id,
1449 author,
1450 age,
1451 msg);
1452 if (r == -1)
1453 goto done;
1455 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1456 "<div class='navs'>") == -1)
1457 goto done;
1459 r = gotweb_link(c, &(struct gotweb_url){
1460 .action = DIFF,
1461 .index_page = -1,
1462 .page = -1,
1463 .path = repo_dir->name,
1464 .commit = rc->commit_id,
1465 }, "diff");
1466 if (r == -1)
1467 goto done;
1469 if (fcgi_printf(c, " | ") == -1)
1470 goto done;
1472 r = gotweb_link(c, &(struct gotweb_url){
1473 .action = TREE,
1474 .index_page = -1,
1475 .page = -1,
1476 .path = repo_dir->name,
1477 .commit = rc->commit_id,
1478 }, "tree");
1479 if (r == -1)
1480 goto done;
1482 if (fcgi_printf(c, "</div>\n" /* .navs */
1483 "</div>\n" /* .navs_wrapper */
1484 "<div class='dotted_line'></div>\n") == -1)
1485 goto done;
1487 free(age);
1488 age = NULL;
1489 free(author);
1490 author = NULL;
1491 free(msg);
1492 msg = NULL;
1495 if (t->next_id || t->prev_id) {
1496 error = gotweb_render_navs(c);
1497 if (error)
1498 goto done;
1500 fcgi_printf(c, "</div>\n"); /* .commits_content */
1501 done:
1502 free(age);
1503 free(author);
1504 free(msg);
1505 return error;
1508 static const struct got_error *
1509 gotweb_render_branches(struct request *c)
1511 const struct got_error *error = NULL;
1512 struct got_reflist_head refs;
1513 struct got_reflist_entry *re;
1514 struct transport *t = c->t;
1515 struct querystring *qs = t->qs;
1516 struct got_repository *repo = t->repo;
1517 char *escaped_refname = NULL;
1518 char *age = NULL;
1519 int r;
1521 TAILQ_INIT(&refs);
1523 error = got_ref_list(&refs, repo, "refs/heads",
1524 got_ref_cmp_by_name, NULL);
1525 if (error)
1526 goto done;
1528 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1529 "<div id='branches_title'>Branches</div>\n"
1530 "</div>\n" /* #branches_title_wrapper */
1531 "<div id='branches_content'>\n");
1532 if (r == -1)
1533 goto done;
1535 TAILQ_FOREACH(re, &refs, entry) {
1536 const char *refname = NULL;
1538 if (got_ref_is_symbolic(re->ref))
1539 continue;
1541 refname = got_ref_get_name(re->ref);
1542 if (refname == NULL) {
1543 error = got_error_from_errno("strdup");
1544 goto done;
1546 if (strncmp(refname, "refs/heads/", 11) != 0)
1547 continue;
1549 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1550 if (error)
1551 goto done;
1553 if (strncmp(refname, "refs/heads/", 11) == 0)
1554 refname += 11;
1555 error = gotweb_escape_html(&escaped_refname, refname);
1556 if (error)
1557 goto done;
1559 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1560 "<div class='branches_age'>%s</div>\n"
1561 "<div class='branches_space'>&nbsp;</div>\n"
1562 "<div class='branch'>", age);
1563 if (r == -1)
1564 goto done;
1566 r = gotweb_link(c, &(struct gotweb_url){
1567 .action = SUMMARY,
1568 .index_page = -1,
1569 .page = -1,
1570 .path = qs->path,
1571 .headref = refname,
1572 }, "%s", escaped_refname);
1573 if (r == -1)
1574 goto done;
1576 if (fcgi_printf(c, "</div>\n" /* .branch */
1577 "<div class='navs_wrapper'>\n"
1578 "<div class='navs'>") == -1)
1579 goto done;
1581 r = gotweb_link(c, &(struct gotweb_url){
1582 .action = SUMMARY,
1583 .index_page = -1,
1584 .page = -1,
1585 .path = qs->path,
1586 .headref = refname,
1587 }, "summary");
1588 if (r == -1)
1589 goto done;
1591 if (fcgi_printf(c, " | ") == -1)
1592 goto done;
1594 r = gotweb_link(c, &(struct gotweb_url){
1595 .action = BRIEFS,
1596 .index_page = -1,
1597 .page = -1,
1598 .path = qs->path,
1599 .headref = refname,
1600 }, "commit briefs");
1601 if (r == -1)
1602 goto done;
1604 if (fcgi_printf(c, " | ") == -1)
1605 goto done;
1607 r = gotweb_link(c, &(struct gotweb_url){
1608 .action = COMMITS,
1609 .index_page = -1,
1610 .page = -1,
1611 .path = qs->path,
1612 .headref = refname,
1613 }, "commits");
1614 if (r == -1)
1615 goto done;
1617 r = fcgi_printf(c, "</div>\n" /* .navs */
1618 "</div>\n" /* .navs_wrapper */
1619 "<div class='dotted_line'></div>\n"
1620 "</div>\n"); /* .branches_wrapper */
1621 if (r == -1)
1622 goto done;
1624 free(age);
1625 age = NULL;
1626 free(escaped_refname);
1627 escaped_refname = NULL;
1629 fcgi_printf(c, "</div>\n"); /* #branches_content */
1630 done:
1631 free(age);
1632 free(escaped_refname);
1633 got_ref_list_free(&refs);
1634 return error;
1637 static const struct got_error *
1638 gotweb_render_tree(struct request *c)
1640 const struct got_error *error = NULL;
1641 struct transport *t = c->t;
1642 struct repo_commit *rc = NULL;
1643 char *age = NULL, *msg = NULL;
1644 int r;
1646 error = got_get_repo_commits(c, 1);
1647 if (error)
1648 return error;
1650 rc = TAILQ_FIRST(&t->repo_commits);
1652 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1653 if (error)
1654 goto done;
1656 error = gotweb_escape_html(&msg, rc->commit_msg);
1657 if (error)
1658 goto done;
1660 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1661 "<div id='tree_title'>Tree</div>\n"
1662 "</div>\n" /* #tree_title_wrapper */
1663 "<div id='tree_content'>\n"
1664 "<div id='tree_header_wrapper'>\n"
1665 "<div id='tree_header'>\n"
1666 "<div id='header_tree_title'>Tree:</div>\n"
1667 "<div id='header_tree'>%s</div>\n"
1668 "<div class='header_age_title'>Date:</div>\n"
1669 "<div class='header_age'>%s</div>\n"
1670 "<div id='header_commit_msg_title'>Message:</div>\n"
1671 "<div id='header_commit_msg'>%s</div>\n"
1672 "</div>\n" /* #tree_header */
1673 "</div>\n" /* #tree_header_wrapper */
1674 "<div class='dotted_line'></div>\n"
1675 "<div id='tree'>\n",
1676 rc->tree_id,
1677 age,
1678 msg);
1679 if (r == -1)
1680 goto done;
1682 error = got_output_repo_tree(c);
1683 if (error)
1684 goto done;
1686 fcgi_printf(c, "</div>\n"); /* #tree */
1687 fcgi_printf(c, "</div>\n"); /* #tree_content */
1688 done:
1689 free(age);
1690 free(msg);
1691 return error;
1694 static const struct got_error *
1695 gotweb_render_diff(struct request *c)
1697 const struct got_error *error = NULL;
1698 struct transport *t = c->t;
1699 struct repo_commit *rc = NULL;
1700 char *age = NULL, *author = NULL, *msg = NULL;
1701 int r;
1703 error = got_get_repo_commits(c, 1);
1704 if (error)
1705 return error;
1707 rc = TAILQ_FIRST(&t->repo_commits);
1709 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1710 if (error)
1711 goto done;
1712 error = gotweb_escape_html(&author, rc->author);
1713 if (error)
1714 goto done;
1715 error = gotweb_escape_html(&msg, rc->commit_msg);
1716 if (error)
1717 goto done;
1719 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1720 "<div id='diff_title'>Commit Diff</div>\n"
1721 "</div>\n" /* #diff_title_wrapper */
1722 "<div id='diff_content'>\n"
1723 "<div id='diff_header_wrapper'>\n"
1724 "<div id='diff_header'>\n"
1725 "<div id='header_diff_title'>Diff:</div>\n"
1726 "<div id='header_diff'>%s<br />%s</div>\n"
1727 "<div class='header_commit_title'>Commit:</div>\n"
1728 "<div class='header_commit'>%s</div>\n"
1729 "<div id='header_tree_title'>Tree:</div>\n"
1730 "<div id='header_tree'>%s</div>\n"
1731 "<div class='header_author_title'>Author:</div>\n"
1732 "<div class='header_author'>%s</div>\n"
1733 "<div class='header_age_title'>Date:</div>\n"
1734 "<div class='header_age'>%s</div>\n"
1735 "<div id='header_commit_msg_title'>Message:</div>\n"
1736 "<div id='header_commit_msg'>%s</div>\n"
1737 "</div>\n" /* #diff_header */
1738 "</div>\n" /* #diff_header_wrapper */
1739 "<div class='dotted_line'></div>\n"
1740 "<div id='diff'>\n",
1741 rc->parent_id, rc->commit_id,
1742 rc->commit_id,
1743 rc->tree_id,
1744 author,
1745 age,
1746 msg);
1747 if (r == -1)
1748 goto done;
1750 error = got_output_repo_diff(c);
1751 if (error)
1752 goto done;
1754 fcgi_printf(c, "</div>\n"); /* #diff */
1755 fcgi_printf(c, "</div>\n"); /* #diff_content */
1756 done:
1757 free(age);
1758 free(author);
1759 free(msg);
1760 return error;
1763 static const struct got_error *
1764 gotweb_render_summary(struct request *c)
1766 const struct got_error *error = NULL;
1767 struct transport *t = c->t;
1768 struct server *srv = c->srv;
1769 int r;
1771 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1772 goto done;
1774 if (srv->show_repo_description) {
1775 r = fcgi_printf(c,
1776 "<div id='description_title'>Description:</div>\n"
1777 "<div id='description'>%s</div>\n",
1778 t->repo_dir->description ? t->repo_dir->description : "");
1779 if (r == -1)
1780 goto done;
1783 if (srv->show_repo_owner) {
1784 r = fcgi_printf(c,
1785 "<div id='repo_owner_title'>Owner:</div>\n"
1786 "<div id='repo_owner'>%s</div>\n",
1787 t->repo_dir->owner ? t->repo_dir->owner : "");
1788 if (r == -1)
1789 goto done;
1792 if (srv->show_repo_age) {
1793 r = fcgi_printf(c,
1794 "<div id='last_change_title'>Last Change:</div>\n"
1795 "<div id='last_change'>%s</div>\n",
1796 t->repo_dir->age);
1797 if (r == -1)
1798 goto done;
1801 if (srv->show_repo_cloneurl) {
1802 r = fcgi_printf(c,
1803 "<div id='cloneurl_title'>Clone URL:</div>\n"
1804 "<div id='cloneurl'>%s</div>\n",
1805 t->repo_dir->url ? t->repo_dir->url : "");
1806 if (r == -1)
1807 goto done;
1810 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1811 if (r == -1)
1812 goto done;
1814 error = gotweb_render_briefs(c);
1815 if (error) {
1816 log_warnx("%s: %s", __func__, error->msg);
1817 goto done;
1820 error = gotweb_render_tags(c);
1821 if (error) {
1822 log_warnx("%s: %s", __func__, error->msg);
1823 goto done;
1826 error = gotweb_render_branches(c);
1827 if (error)
1828 log_warnx("%s: %s", __func__, error->msg);
1829 done:
1830 return error;
1833 static const struct got_error *
1834 gotweb_render_tag(struct request *c)
1836 const struct got_error *error = NULL;
1837 struct repo_tag *rt = NULL;
1838 struct transport *t = c->t;
1839 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1841 error = got_get_repo_tags(c, 1);
1842 if (error)
1843 goto done;
1845 if (t->tag_count == 0) {
1846 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1847 "bad commit id");
1848 goto done;
1851 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1853 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1854 if (error)
1855 goto done;
1856 error = gotweb_escape_html(&author, rt->tagger);
1857 if (error)
1858 goto done;
1859 error = gotweb_escape_html(&msg, rt->commit_msg);
1860 if (error)
1861 goto done;
1863 tagname = rt->tag_name;
1864 if (strncmp(tagname, "refs/", 5) == 0)
1865 tagname += 5;
1866 error = gotweb_escape_html(&tagname, tagname);
1867 if (error)
1868 goto done;
1870 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1871 "<div id='tags_title'>Tag</div>\n"
1872 "</div>\n" /* #tags_title_wrapper */
1873 "<div id='tags_content'>\n"
1874 "<div id='tag_header_wrapper'>\n"
1875 "<div id='tag_header'>\n"
1876 "<div class='header_commit_title'>Commit:</div>\n"
1877 "<div class='header_commit'>%s"
1878 " <span class='refs_str'>(%s)</span></div>\n"
1879 "<div class='header_author_title'>Tagger:</div>\n"
1880 "<div class='header_author'>%s</div>\n"
1881 "<div class='header_age_title'>Date:</div>\n"
1882 "<div class='header_age'>%s</div>\n"
1883 "<div id='header_commit_msg_title'>Message:</div>\n"
1884 "<div id='header_commit_msg'>%s</div>\n"
1885 "</div>\n" /* #tag_header */
1886 "<div class='dotted_line'></div>\n"
1887 "<div id='tag_commit'>\n%s</div>"
1888 "</div>" /* #tag_header_wrapper */
1889 "</div>", /* #tags_content */
1890 rt->commit_id,
1891 tagname,
1892 author,
1893 age,
1894 msg,
1895 rt->tag_commit);
1897 done:
1898 free(age);
1899 free(author);
1900 free(msg);
1901 return error;
1904 static const struct got_error *
1905 gotweb_render_tags(struct request *c)
1907 const struct got_error *error = NULL;
1908 struct repo_tag *rt = NULL;
1909 struct server *srv = c->srv;
1910 struct transport *t = c->t;
1911 struct querystring *qs = t->qs;
1912 struct repo_dir *repo_dir = t->repo_dir;
1913 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1914 int r, commit_found = 0;
1916 if (qs->action == BRIEFS) {
1917 qs->action = TAGS;
1918 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1919 } else
1920 error = got_get_repo_tags(c, srv->max_commits_display);
1921 if (error)
1922 goto done;
1924 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1925 "<div id='tags_title'>Tags</div>\n"
1926 "</div>\n" /* #tags_title_wrapper */
1927 "<div id='tags_content'>\n");
1928 if (r == -1)
1929 goto done;
1931 if (t->tag_count == 0) {
1932 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1933 "This repository contains no tags");
1934 if (r == -1)
1935 goto done;
1938 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1939 if (commit_found == 0 && qs->commit != NULL) {
1940 if (strcmp(qs->commit, rt->commit_id) != 0)
1941 continue;
1942 else
1943 commit_found = 1;
1945 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1946 if (error)
1947 goto done;
1949 tagname = rt->tag_name;
1950 if (strncmp(tagname, "refs/tags/", 10) == 0)
1951 tagname += 10;
1952 error = gotweb_escape_html(&tagname, tagname);
1953 if (error)
1954 goto done;
1956 if (rt->tag_commit != NULL) {
1957 newline = strchr(rt->tag_commit, '\n');
1958 if (newline)
1959 *newline = '\0';
1960 error = gotweb_escape_html(&msg, rt->tag_commit);
1961 if (error)
1962 goto done;
1965 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1966 "<div class='tag'>%s</div>\n"
1967 "<div class='tag_log'>", age, tagname) == -1)
1968 goto done;
1970 r = gotweb_link(c, &(struct gotweb_url){
1971 .action = TAG,
1972 .index_page = -1,
1973 .page = -1,
1974 .path = repo_dir->name,
1975 .commit = rt->commit_id,
1976 }, "%s", msg ? msg : "");
1977 if (r == -1)
1978 goto done;
1980 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1981 "<div class='navs_wrapper'>\n"
1982 "<div class='navs'>") == -1)
1983 goto done;
1985 r = gotweb_link(c, &(struct gotweb_url){
1986 .action = TAG,
1987 .index_page = -1,
1988 .page = -1,
1989 .path = repo_dir->name,
1990 .commit = rt->commit_id,
1991 }, "tag");
1992 if (r == -1)
1993 goto done;
1995 if (fcgi_printf(c, " | ") == -1)
1996 goto done;
1998 r = gotweb_link(c, &(struct gotweb_url){
1999 .action = BRIEFS,
2000 .index_page = -1,
2001 .page = -1,
2002 .path = repo_dir->name,
2003 .commit = rt->commit_id,
2004 }, "commit briefs");
2005 if (r == -1)
2006 goto done;
2008 if (fcgi_printf(c, " | ") == -1)
2009 goto done;
2011 r = gotweb_link(c, &(struct gotweb_url){
2012 .action = COMMITS,
2013 .index_page = -1,
2014 .page = -1,
2015 .path = repo_dir->name,
2016 .commit = rt->commit_id,
2017 }, "commits");
2018 if (r == -1)
2019 goto done;
2021 r = fcgi_printf(c,
2022 "</div>\n" /* .navs */
2023 "</div>\n" /* .navs_wrapper */
2024 "<div class='dotted_line'></div>\n");
2025 if (r == -1)
2026 goto done;
2028 free(age);
2029 age = NULL;
2030 free(tagname);
2031 tagname = NULL;
2032 free(msg);
2033 msg = NULL;
2035 if (t->next_id || t->prev_id) {
2036 error = gotweb_render_navs(c);
2037 if (error)
2038 goto done;
2040 fcgi_printf(c, "</div>\n"); /* #tags_content */
2041 done:
2042 free(age);
2043 free(tagname);
2044 free(msg);
2045 return error;
2048 const struct got_error *
2049 gotweb_escape_html(char **escaped_html, const char *orig_html)
2051 const struct got_error *error = NULL;
2052 struct escape_pair {
2053 char c;
2054 const char *s;
2055 } esc[] = {
2056 { '>', "&gt;" },
2057 { '<', "&lt;" },
2058 { '&', "&amp;" },
2059 { '"', "&quot;" },
2060 { '\'', "&apos;" },
2061 { '\n', "<br />" },
2063 size_t orig_len, len;
2064 int i, j, x;
2066 orig_len = strlen(orig_html);
2067 len = orig_len;
2068 for (i = 0; i < orig_len; i++) {
2069 for (j = 0; j < nitems(esc); j++) {
2070 if (orig_html[i] != esc[j].c)
2071 continue;
2072 len += strlen(esc[j].s) - 1 /* escaped char */;
2076 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2077 if (*escaped_html == NULL)
2078 return got_error_from_errno("calloc");
2080 x = 0;
2081 for (i = 0; i < orig_len; i++) {
2082 int escaped = 0;
2083 for (j = 0; j < nitems(esc); j++) {
2084 if (orig_html[i] != esc[j].c)
2085 continue;
2087 if (strlcat(*escaped_html, esc[j].s, len + 1)
2088 >= len + 1) {
2089 error = got_error(GOT_ERR_NO_SPACE);
2090 goto done;
2092 x += strlen(esc[j].s);
2093 escaped = 1;
2094 break;
2096 if (!escaped) {
2097 (*escaped_html)[x] = orig_html[i];
2098 x++;
2101 done:
2102 if (error) {
2103 free(*escaped_html);
2104 *escaped_html = NULL;
2105 } else {
2106 (*escaped_html)[x] = '\0';
2109 return error;
2112 static inline int
2113 should_urlencode(int c)
2115 if (c <= ' ' || c >= 127)
2116 return 1;
2118 switch (c) {
2119 /* gen-delim */
2120 case ':':
2121 case '/':
2122 case '?':
2123 case '#':
2124 case '[':
2125 case ']':
2126 case '@':
2127 /* sub-delims */
2128 case '!':
2129 case '$':
2130 case '&':
2131 case '\'':
2132 case '(':
2133 case ')':
2134 case '*':
2135 case '+':
2136 case ',':
2137 case ';':
2138 case '=':
2139 return 1;
2140 default:
2141 return 0;
2145 static char *
2146 gotweb_urlencode(const char *str)
2148 const char *s;
2149 char *escaped;
2150 size_t i, len;
2151 int a, b;
2153 len = 0;
2154 for (s = str; *s; ++s) {
2155 len++;
2156 if (should_urlencode(*s))
2157 len += 2;
2160 escaped = calloc(1, len + 1);
2161 if (escaped == NULL)
2162 return NULL;
2164 i = 0;
2165 for (s = str; *s; ++s) {
2166 if (should_urlencode(*s)) {
2167 a = (*s & 0xF0) >> 4;
2168 b = (*s & 0x0F);
2170 escaped[i++] = '%';
2171 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2172 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2173 } else
2174 escaped[i++] = *s;
2177 return escaped;
2180 static inline const char *
2181 action_name(int action)
2183 switch (action) {
2184 case BLAME:
2185 return "blame";
2186 case BLOB:
2187 return "blob";
2188 case BRIEFS:
2189 return "briefs";
2190 case COMMITS:
2191 return "commits";
2192 case DIFF:
2193 return "diff";
2194 case ERR:
2195 return "err";
2196 case INDEX:
2197 return "index";
2198 case SUMMARY:
2199 return "summary";
2200 case TAG:
2201 return "tag";
2202 case TAGS:
2203 return "tags";
2204 case TREE:
2205 return "tree";
2206 default:
2207 return NULL;
2211 static int
2212 gotweb_print_url(struct request *c, struct gotweb_url *url)
2214 const char *sep = "?", *action;
2215 char *tmp;
2216 int r;
2218 action = action_name(url->action);
2219 if (action != NULL) {
2220 if (fcgi_printf(c, "?action=%s", action) == -1)
2221 return -1;
2222 sep = "&";
2225 if (url->commit) {
2226 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2227 return -1;
2228 sep = "&";
2231 if (url->previd) {
2232 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2233 return -1;
2234 sep = "&";
2237 if (url->prevset) {
2238 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2239 return -1;
2240 sep = "&";
2243 if (url->file) {
2244 tmp = gotweb_urlencode(url->file);
2245 if (tmp == NULL)
2246 return -1;
2247 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2248 free(tmp);
2249 if (r == -1)
2250 return -1;
2251 sep = "&";
2254 if (url->folder) {
2255 tmp = gotweb_urlencode(url->folder);
2256 if (tmp == NULL)
2257 return -1;
2258 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2259 free(tmp);
2260 if (r == -1)
2261 return -1;
2262 sep = "&";
2265 if (url->headref) {
2266 tmp = gotweb_urlencode(url->headref);
2267 if (tmp == NULL)
2268 return -1;
2269 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2270 free(tmp);
2271 if (r == -1)
2272 return -1;
2273 sep = "&";
2276 if (url->index_page != -1) {
2277 if (fcgi_printf(c, "%sindex_page=%d", sep,
2278 url->index_page) == -1)
2279 return -1;
2280 sep = "&";
2283 if (url->path) {
2284 tmp = gotweb_urlencode(url->path);
2285 if (tmp == NULL)
2286 return -1;
2287 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2288 free(tmp);
2289 if (r == -1)
2290 return -1;
2291 sep = "&";
2294 if (url->page != -1) {
2295 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2296 return -1;
2297 sep = "&";
2300 return 0;
2303 int
2304 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2306 va_list ap;
2307 int r;
2309 if (fcgi_printf(c, "<a href='") == -1)
2310 return -1;
2312 if (gotweb_print_url(c, url) == -1)
2313 return -1;
2315 if (fcgi_printf(c, "'>") == -1)
2316 return -1;
2318 va_start(ap, fmt);
2319 r = fcgi_vprintf(c, fmt, ap);
2320 va_end(ap);
2321 if (r == -1)
2322 return -1;
2324 if (fcgi_printf(c, "</a>"))
2325 return -1;
2326 return 0;
2329 static struct got_repository *
2330 find_cached_repo(struct server *srv, const char *path)
2332 int i;
2334 for (i = 0; i < srv->ncached_repos; i++) {
2335 if (strcmp(srv->cached_repos[i].path, path) == 0)
2336 return srv->cached_repos[i].repo;
2339 return NULL;
2342 static const struct got_error *
2343 cache_repo(struct got_repository **new, struct server *srv,
2344 struct repo_dir *repo_dir, struct socket *sock)
2346 const struct got_error *error = NULL;
2347 struct got_repository *repo;
2348 struct cached_repo *cr;
2349 int evicted = 0;
2351 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2352 cr = &srv->cached_repos[srv->ncached_repos - 1];
2353 error = got_repo_close(cr->repo);
2354 memset(cr, 0, sizeof(*cr));
2355 srv->ncached_repos--;
2356 if (error)
2357 return error;
2358 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2359 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2360 cr = &srv->cached_repos[0];
2361 evicted = 1;
2362 } else {
2363 cr = &srv->cached_repos[srv->ncached_repos];
2366 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2367 if (error) {
2368 if (evicted) {
2369 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2370 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2372 return error;
2375 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2376 >= sizeof(cr->path)) {
2377 if (evicted) {
2378 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2379 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2381 return got_error(GOT_ERR_NO_SPACE);
2384 cr->repo = repo;
2385 srv->ncached_repos++;
2386 *new = repo;
2387 return NULL;
2390 static const struct got_error *
2391 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2393 const struct got_error *error = NULL;
2394 struct socket *sock = c->sock;
2395 struct server *srv = c->srv;
2396 struct transport *t = c->t;
2397 struct got_repository *repo = NULL;
2398 DIR *dt;
2399 char *dir_test;
2401 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2402 GOTWEB_GIT_DIR) == -1)
2403 return got_error_from_errno("asprintf");
2405 dt = opendir(dir_test);
2406 if (dt == NULL) {
2407 free(dir_test);
2408 } else {
2409 repo_dir->path = dir_test;
2410 dir_test = NULL;
2411 goto done;
2414 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2415 repo_dir->name) == -1)
2416 return got_error_from_errno("asprintf");
2418 dt = opendir(dir_test);
2419 if (dt == NULL) {
2420 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2421 goto err;
2422 } else {
2423 repo_dir->path = dir_test;
2424 dir_test = NULL;
2427 done:
2428 if (srv->respect_exportok &&
2429 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2430 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2431 goto err;
2434 repo = find_cached_repo(srv, repo_dir->path);
2435 if (repo == NULL) {
2436 error = cache_repo(&repo, srv, repo_dir, sock);
2437 if (error)
2438 goto err;
2440 t->repo = repo;
2441 error = gotweb_get_repo_description(&repo_dir->description, srv,
2442 repo_dir->path, dirfd(dt));
2443 if (error)
2444 goto err;
2445 error = got_get_repo_owner(&repo_dir->owner, c);
2446 if (error)
2447 goto err;
2448 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2449 if (error)
2450 goto err;
2451 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2452 dirfd(dt));
2453 err:
2454 free(dir_test);
2455 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2456 error = got_error_from_errno("closedir");
2457 return error;
2460 static const struct got_error *
2461 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2463 const struct got_error *error;
2465 *repo_dir = calloc(1, sizeof(**repo_dir));
2466 if (*repo_dir == NULL)
2467 return got_error_from_errno("calloc");
2469 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2470 error = got_error_from_errno("asprintf");
2471 free(*repo_dir);
2472 *repo_dir = NULL;
2473 return error;
2475 (*repo_dir)->owner = NULL;
2476 (*repo_dir)->description = NULL;
2477 (*repo_dir)->url = NULL;
2478 (*repo_dir)->age = NULL;
2479 (*repo_dir)->path = NULL;
2481 return NULL;
2484 static const struct got_error *
2485 gotweb_get_repo_description(char **description, struct server *srv,
2486 const char *dirpath, int dir)
2488 const struct got_error *error = NULL;
2489 struct stat sb;
2490 int fd = -1;
2491 off_t len;
2493 *description = NULL;
2494 if (srv->show_repo_description == 0)
2495 return NULL;
2497 fd = openat(dir, "description", O_RDONLY);
2498 if (fd == -1) {
2499 if (errno != ENOENT && errno != EACCES) {
2500 error = got_error_from_errno_fmt("openat %s/%s",
2501 dirpath, "description");
2503 goto done;
2506 if (fstat(fd, &sb) == -1) {
2507 error = got_error_from_errno_fmt("fstat %s/%s",
2508 dirpath, "description");
2509 goto done;
2512 len = sb.st_size;
2513 if (len > GOTWEBD_MAXDESCRSZ - 1)
2514 len = GOTWEBD_MAXDESCRSZ - 1;
2516 *description = calloc(len + 1, sizeof(**description));
2517 if (*description == NULL) {
2518 error = got_error_from_errno("calloc");
2519 goto done;
2522 if (read(fd, *description, len) == -1)
2523 error = got_error_from_errno("read");
2524 done:
2525 if (fd != -1 && close(fd) == -1 && error == NULL)
2526 error = got_error_from_errno("close");
2527 return error;
2530 static const struct got_error *
2531 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2532 int dir)
2534 const struct got_error *error = NULL;
2535 struct stat sb;
2536 int fd = -1;
2537 off_t len;
2539 *url = NULL;
2540 if (srv->show_repo_cloneurl == 0)
2541 return NULL;
2543 fd = openat(dir, "cloneurl", O_RDONLY);
2544 if (fd == -1) {
2545 if (errno != ENOENT && errno != EACCES) {
2546 error = got_error_from_errno_fmt("openat %s/%s",
2547 dirpath, "cloneurl");
2549 goto done;
2552 if (fstat(fd, &sb) == -1) {
2553 error = got_error_from_errno_fmt("fstat %s/%s",
2554 dirpath, "cloneurl");
2555 goto done;
2558 len = sb.st_size;
2559 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2560 len = GOTWEBD_MAXCLONEURLSZ - 1;
2562 *url = calloc(len + 1, sizeof(**url));
2563 if (*url == NULL) {
2564 error = got_error_from_errno("calloc");
2565 goto done;
2568 if (read(fd, *url, len) == -1)
2569 error = got_error_from_errno("read");
2570 done:
2571 if (fd != -1 && close(fd) == -1 && error == NULL)
2572 error = got_error_from_errno("close");
2573 return error;
2576 const struct got_error *
2577 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2579 struct tm tm;
2580 long long diff_time;
2581 const char *years = "years ago", *months = "months ago";
2582 const char *weeks = "weeks ago", *days = "days ago";
2583 const char *hours = "hours ago", *minutes = "minutes ago";
2584 const char *seconds = "seconds ago", *now = "right now";
2585 char *s;
2586 char datebuf[29];
2588 *repo_age = NULL;
2590 switch (ref_tm) {
2591 case TM_DIFF:
2592 diff_time = time(NULL) - committer_time;
2593 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2594 if (asprintf(repo_age, "%lld %s",
2595 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2596 return got_error_from_errno("asprintf");
2597 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2598 if (asprintf(repo_age, "%lld %s",
2599 (diff_time / 60 / 60 / 24 / (365 / 12)),
2600 months) == -1)
2601 return got_error_from_errno("asprintf");
2602 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2603 if (asprintf(repo_age, "%lld %s",
2604 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2605 return got_error_from_errno("asprintf");
2606 } else if (diff_time > 60 * 60 * 24 * 2) {
2607 if (asprintf(repo_age, "%lld %s",
2608 (diff_time / 60 / 60 / 24), days) == -1)
2609 return got_error_from_errno("asprintf");
2610 } else if (diff_time > 60 * 60 * 2) {
2611 if (asprintf(repo_age, "%lld %s",
2612 (diff_time / 60 / 60), hours) == -1)
2613 return got_error_from_errno("asprintf");
2614 } else if (diff_time > 60 * 2) {
2615 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2616 minutes) == -1)
2617 return got_error_from_errno("asprintf");
2618 } else if (diff_time > 2) {
2619 if (asprintf(repo_age, "%lld %s", diff_time,
2620 seconds) == -1)
2621 return got_error_from_errno("asprintf");
2622 } else {
2623 if (asprintf(repo_age, "%s", now) == -1)
2624 return got_error_from_errno("asprintf");
2626 break;
2627 case TM_LONG:
2628 if (gmtime_r(&committer_time, &tm) == NULL)
2629 return got_error_from_errno("gmtime_r");
2631 s = asctime_r(&tm, datebuf);
2632 if (s == NULL)
2633 return got_error_from_errno("asctime_r");
2635 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2636 return got_error_from_errno("asprintf");
2637 break;
2639 return NULL;