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 <imsg.h>
32 #include <sha1.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "proc.h"
51 #include "gotwebd.h"
53 enum gotweb_ref_tm {
54 TM_DIFF,
55 TM_LONG,
56 };
58 static const struct querystring_keys querystring_keys[] = {
59 { "action", ACTION },
60 { "commit", COMMIT },
61 { "file", RFILE },
62 { "folder", FOLDER },
63 { "headref", HEADREF },
64 { "index_page", INDEX_PAGE },
65 { "path", PATH },
66 { "page", PAGE },
67 };
69 static const struct action_keys action_keys[] = {
70 { "blame", BLAME },
71 { "blob", BLOB },
72 { "briefs", BRIEFS },
73 { "commits", COMMITS },
74 { "diff", DIFF },
75 { "error", ERR },
76 { "index", INDEX },
77 { "summary", SUMMARY },
78 { "tag", TAG },
79 { "tags", TAGS },
80 { "tree", TREE },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_header(struct request *);
89 static const struct got_error *gotweb_render_footer(struct request *);
90 static const struct got_error *gotweb_render_index(struct request *);
91 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
92 const char *);
93 static const struct got_error *gotweb_load_got_path(struct request *c,
94 struct repo_dir *);
95 static const struct got_error *gotweb_get_repo_description(char **,
96 struct server *, char *);
97 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
98 char *);
99 static const struct got_error *gotweb_render_navs(struct request *);
100 static const struct got_error *gotweb_render_blame(struct request *);
101 static const struct got_error *gotweb_render_briefs(struct request *);
102 static const struct got_error *gotweb_render_commits(struct request *);
103 static const struct got_error *gotweb_render_diff(struct request *);
104 static const struct got_error *gotweb_render_summary(struct request *);
105 static const struct got_error *gotweb_render_tag(struct request *);
106 static const struct got_error *gotweb_render_tags(struct request *);
107 static const struct got_error *gotweb_render_tree(struct request *);
108 static const struct got_error *gotweb_render_branches(struct request *);
110 static void gotweb_free_querystring(struct querystring *);
111 static void gotweb_free_repo_dir(struct repo_dir *);
113 struct server *gotweb_get_server(uint8_t *, uint8_t *);
115 void
116 gotweb_process_request(struct request *c)
118 const struct got_error *error = NULL, *error2 = NULL;
119 struct server *srv = NULL;
120 struct querystring *qs = NULL;
121 struct repo_dir *repo_dir = NULL;
122 uint8_t err[] = "gotwebd experienced an error: ";
123 int r, html = 0;
125 /* init the transport */
126 error = gotweb_init_transport(&c->t);
127 if (error) {
128 log_warnx("%s: %s", __func__, error->msg);
129 return;
131 /* don't process any further if client disconnected */
132 if (c->sock->client_status == CLIENT_DISCONNECT)
133 return;
134 /* get the gotwebd server */
135 srv = gotweb_get_server(c->server_name, c->http_host);
136 if (srv == NULL) {
137 log_warnx("%s: error server is NULL", __func__);
138 goto err;
140 c->srv = srv;
141 /* parse our querystring */
142 error = gotweb_init_querystring(&qs);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
147 c->t->qs = qs;
148 error = gotweb_parse_querystring(&qs, c->querystring);
149 if (error) {
150 log_warnx("%s: %s", __func__, error->msg);
151 goto err;
154 /*
155 * certain actions require a commit id in the querystring. this stops
156 * bad actors from exploiting this by manually manipulating the
157 * querystring.
158 */
160 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
161 qs->action == DIFF)) {
162 error2 = got_error(GOT_ERR_QUERYSTRING);
163 goto render;
166 if (qs->action != INDEX) {
167 error = gotweb_init_repo_dir(&repo_dir, qs->path);
168 if (error)
169 goto done;
170 error = gotweb_load_got_path(c, repo_dir);
171 c->t->repo_dir = repo_dir;
172 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
173 goto err;
176 /* render top of page */
177 if (qs != NULL && qs->action == BLOB) {
178 error = got_get_repo_commits(c, 1);
179 if (error)
180 goto done;
181 error = got_output_file_blob(c);
182 if (error) {
183 log_warnx("%s: %s", __func__, error->msg);
184 goto err;
186 goto done;
187 } else {
188 render:
189 error = gotweb_render_content_type(c, "text/html");
190 if (error) {
191 log_warnx("%s: %s", __func__, error->msg);
192 goto err;
194 html = 1;
197 error = gotweb_render_header(c);
198 if (error) {
199 log_warnx("%s: %s", __func__, error->msg);
200 goto err;
203 if (error2) {
204 error = error2;
205 goto err;
208 switch(qs->action) {
209 case BLAME:
210 error = gotweb_render_blame(c);
211 if (error) {
212 log_warnx("%s: %s", __func__, error->msg);
213 goto err;
215 break;
216 case BRIEFS:
217 error = gotweb_render_briefs(c);
218 if (error) {
219 log_warnx("%s: %s", __func__, error->msg);
220 goto err;
222 break;
223 case COMMITS:
224 error = gotweb_render_commits(c);
225 if (error) {
226 log_warnx("%s: %s", __func__, error->msg);
227 goto err;
229 break;
230 case DIFF:
231 error = gotweb_render_diff(c);
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 break;
237 case INDEX:
238 error = gotweb_render_index(c);
239 if (error) {
240 log_warnx("%s: %s", __func__, error->msg);
241 goto err;
243 break;
244 case SUMMARY:
245 error = gotweb_render_summary(c);
246 if (error) {
247 log_warnx("%s: %s", __func__, error->msg);
248 goto err;
250 break;
251 case TAG:
252 error = gotweb_render_tag(c);
253 if (error) {
254 log_warnx("%s: %s", __func__, error->msg);
255 goto err;
257 break;
258 case TAGS:
259 error = gotweb_render_tags(c);
260 if (error) {
261 log_warnx("%s: %s", __func__, error->msg);
262 goto err;
264 break;
265 case TREE:
266 error = gotweb_render_tree(c);
267 if (error) {
268 log_warnx("%s: %s", __func__, error->msg);
269 goto err;
271 break;
272 case ERR:
273 default:
274 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
275 "Erorr: Bad Querystring");
276 if (r == -1)
277 goto err;
278 break;
281 goto done;
282 err:
283 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
284 return;
285 if (fcgi_printf(c, "\n%s", err) == -1)
286 return;
287 if (error) {
288 if (fcgi_printf(c, "%s", error->msg) == -1)
289 return;
290 } else {
291 if (fcgi_printf(c, "see daemon logs for details") == -1)
292 return;
294 if (html && fcgi_printf(c, "</div>\n") == -1)
295 return;
296 done:
297 if (html && srv != NULL)
298 gotweb_render_footer(c);
301 struct server *
302 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
304 struct server *srv = NULL;
306 /* check against the server name first */
307 if (strlen(server_name) > 0)
308 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
309 if (strcmp(srv->name, server_name) == 0)
310 goto done;
312 /* check against subdomain second */
313 if (strlen(subdomain) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, subdomain) == 0)
316 goto done;
318 /* if those fail, send first server */
319 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
320 if (srv != NULL)
321 break;
322 done:
323 return srv;
324 };
326 const struct got_error *
327 gotweb_init_transport(struct transport **t)
329 const struct got_error *error = NULL;
331 *t = calloc(1, sizeof(**t));
332 if (*t == NULL)
333 return got_error_from_errno2("%s: calloc", __func__);
335 TAILQ_INIT(&(*t)->repo_commits);
336 TAILQ_INIT(&(*t)->repo_tags);
338 (*t)->repo = NULL;
339 (*t)->repo_dir = NULL;
340 (*t)->qs = NULL;
341 (*t)->next_id = NULL;
342 (*t)->prev_id = NULL;
343 (*t)->next_disp = 0;
344 (*t)->prev_disp = 0;
346 return error;
349 static const struct got_error *
350 gotweb_init_querystring(struct querystring **qs)
352 const struct got_error *error = NULL;
354 *qs = calloc(1, sizeof(**qs));
355 if (*qs == NULL)
356 return got_error_from_errno2("%s: calloc", __func__);
358 (*qs)->headref = strdup("HEAD");
359 if ((*qs)->headref == NULL) {
360 free(*qs);
361 *qs = NULL;
362 return got_error_from_errno2("%s: strdup", __func__);
365 (*qs)->action = INDEX;
366 (*qs)->commit = NULL;
367 (*qs)->file = NULL;
368 (*qs)->folder = NULL;
369 (*qs)->index_page = 0;
370 (*qs)->index_page_str = NULL;
371 (*qs)->path = NULL;
373 return error;
376 static const struct got_error *
377 gotweb_parse_querystring(struct querystring **qs, char *qst)
379 const struct got_error *error = NULL;
380 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
381 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
383 if (qst == NULL)
384 return error;
386 tok1 = strdup(qst);
387 if (tok1 == NULL)
388 return got_error_from_errno2("%s: strdup", __func__);
390 tok1_pair = tok1;
391 tok1_end = tok1;
393 while (tok1_pair != NULL) {
394 strsep(&tok1_end, "&");
396 tok2 = strdup(tok1_pair);
397 if (tok2 == NULL) {
398 free(tok1);
399 return got_error_from_errno2("%s: strdup", __func__);
402 tok2_pair = tok2;
403 tok2_end = tok2;
405 while (tok2_pair != NULL) {
406 strsep(&tok2_end, "=");
407 if (tok2_end) {
408 error = gotweb_assign_querystring(qs, tok2_pair,
409 tok2_end);
410 if (error)
411 goto err;
413 tok2_pair = tok2_end;
415 free(tok2);
416 tok1_pair = tok1_end;
418 free(tok1);
419 return error;
420 err:
421 free(tok2);
422 free(tok1);
423 return error;
426 /*
427 * Adapted from usr.sbin/httpd/httpd.c url_decode.
428 */
429 static const struct got_error *
430 gotweb_urldecode(char *url)
432 char *p, *q;
433 char hex[3];
434 unsigned long x;
436 hex[2] = '\0';
437 p = q = url;
439 while (*p != '\0') {
440 switch (*p) {
441 case '%':
442 /* Encoding character is followed by two hex chars */
443 if (!isxdigit((unsigned char)p[1]) ||
444 !isxdigit((unsigned char)p[2]) ||
445 (p[1] == '0' && p[2] == '0'))
446 return got_error(GOT_ERR_BAD_QUERYSTRING);
448 hex[0] = p[1];
449 hex[1] = p[2];
451 /*
452 * We don't have to validate "hex" because it is
453 * guaranteed to include two hex chars followed by nul.
454 */
455 x = strtoul(hex, NULL, 16);
456 *q = (char)x;
457 p += 2;
458 break;
459 default:
460 *q = *p;
461 break;
463 p++;
464 q++;
466 *q = '\0';
468 return NULL;
471 static const struct got_error *
472 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
474 const struct got_error *error = NULL;
475 const char *errstr;
476 int a_cnt, el_cnt;
478 error = gotweb_urldecode(value);
479 if (error)
480 return error;
482 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
483 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
484 continue;
486 switch (querystring_keys[el_cnt].element) {
487 case ACTION:
488 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
489 if (strcmp(value, action_keys[a_cnt].name) != 0)
490 continue;
491 else if (strcmp(value,
492 action_keys[a_cnt].name) == 0){
493 (*qs)->action =
494 action_keys[a_cnt].action;
495 goto qa_found;
498 (*qs)->action = ERR;
499 qa_found:
500 break;
501 case COMMIT:
502 (*qs)->commit = strdup(value);
503 if ((*qs)->commit == NULL) {
504 error = got_error_from_errno2("%s: strdup",
505 __func__);
506 goto done;
508 break;
509 case RFILE:
510 (*qs)->file = strdup(value);
511 if ((*qs)->file == NULL) {
512 error = got_error_from_errno2("%s: strdup",
513 __func__);
514 goto done;
516 break;
517 case FOLDER:
518 (*qs)->folder = strdup(value);
519 if ((*qs)->folder == NULL) {
520 error = got_error_from_errno2("%s: strdup",
521 __func__);
522 goto done;
524 break;
525 case HEADREF:
526 free((*qs)->headref);
527 (*qs)->headref = strdup(value);
528 if ((*qs)->headref == NULL) {
529 error = got_error_from_errno2("%s: strdup",
530 __func__);
531 goto done;
533 break;
534 case INDEX_PAGE:
535 if (strlen(value) == 0)
536 break;
537 (*qs)->index_page_str = strdup(value);
538 if ((*qs)->index_page_str == NULL) {
539 error = got_error_from_errno2("%s: strdup",
540 __func__);
541 goto done;
543 (*qs)->index_page = strtonum(value, INT64_MIN,
544 INT64_MAX, &errstr);
545 if (errstr) {
546 error = got_error_from_errno3("%s: strtonum %s",
547 __func__, errstr);
548 goto done;
550 if ((*qs)->index_page < 0) {
551 (*qs)->index_page = 0;
552 sprintf((*qs)->index_page_str, "%d", 0);
554 break;
555 case PATH:
556 (*qs)->path = strdup(value);
557 if ((*qs)->path == NULL) {
558 error = got_error_from_errno2("%s: strdup",
559 __func__);
560 goto done;
562 break;
563 case PAGE:
564 if (strlen(value) == 0)
565 break;
566 (*qs)->page_str = strdup(value);
567 if ((*qs)->page_str == NULL) {
568 error = got_error_from_errno2("%s: strdup",
569 __func__);
570 goto done;
572 (*qs)->page = strtonum(value, INT64_MIN,
573 INT64_MAX, &errstr);
574 if (errstr) {
575 error = got_error_from_errno3("%s: strtonum %s",
576 __func__, errstr);
577 goto done;
579 if ((*qs)->page < 0) {
580 (*qs)->page = 0;
581 sprintf((*qs)->page_str, "%d", 0);
583 break;
584 default:
585 break;
588 done:
589 return error;
592 void
593 gotweb_free_repo_tag(struct repo_tag *rt)
595 if (rt != NULL) {
596 free(rt->commit_id);
597 free(rt->tag_name);
598 free(rt->tag_commit);
599 free(rt->commit_msg);
600 free(rt->tagger);
602 free(rt);
605 void
606 gotweb_free_repo_commit(struct repo_commit *rc)
608 if (rc != NULL) {
609 free(rc->path);
610 free(rc->refs_str);
611 free(rc->commit_id);
612 free(rc->parent_id);
613 free(rc->tree_id);
614 free(rc->author);
615 free(rc->committer);
616 free(rc->commit_msg);
618 free(rc);
621 static void
622 gotweb_free_querystring(struct querystring *qs)
624 if (qs != NULL) {
625 free(qs->commit);
626 free(qs->file);
627 free(qs->folder);
628 free(qs->headref);
629 free(qs->index_page_str);
630 free(qs->path);
631 free(qs->page_str);
633 free(qs);
636 static void
637 gotweb_free_repo_dir(struct repo_dir *repo_dir)
639 if (repo_dir != NULL) {
640 free(repo_dir->name);
641 free(repo_dir->owner);
642 free(repo_dir->description);
643 free(repo_dir->url);
644 free(repo_dir->age);
645 free(repo_dir->path);
647 free(repo_dir);
650 void
651 gotweb_free_transport(struct transport *t)
653 struct repo_commit *rc = NULL, *trc = NULL;
654 struct repo_tag *rt = NULL, *trt = NULL;
656 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
657 TAILQ_REMOVE(&t->repo_commits, rc, entry);
658 gotweb_free_repo_commit(rc);
660 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
661 TAILQ_REMOVE(&t->repo_tags, rt, entry);
662 gotweb_free_repo_tag(rt);
664 gotweb_free_repo_dir(t->repo_dir);
665 gotweb_free_querystring(t->qs);
666 free(t->next_id);
667 free(t->prev_id);
668 free(t);
671 const struct got_error *
672 gotweb_render_content_type(struct request *c, const uint8_t *type)
674 const char *csp = "default-src 'self'; script-src 'none'; "
675 "object-src 'none';";
677 fcgi_printf(c,
678 "Content-Security-Policy: %s\r\n"
679 "Content-Type: %s\r\n\r\n",
680 csp, type);
681 return NULL;
684 const struct got_error *
685 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
686 char *file)
688 fcgi_printf(c, "Content-type: %s\r\n"
689 "Content-disposition: attachment; filename=%s\r\n\r\n",
690 type, file);
691 return NULL;
694 static const struct got_error *
695 gotweb_render_header(struct request *c)
697 const struct got_error *err = NULL;
698 struct server *srv = c->srv;
699 struct querystring *qs = c->t->qs;
700 int r;
702 r = fcgi_printf(c, "<!doctype html>\n"
703 "<html>\n"
704 "<head>\n"
705 "<title>%s</title>\n"
706 "<meta charset='utf-8' />\n"
707 "<meta name='viewport' content='initial-scale=.75' />\n"
708 "<meta name='msapplication-TileColor' content='#da532c' />\n"
709 "<meta name='theme-color' content='#ffffff'/>\n"
710 "<link rel='apple-touch-icon' sizes='180x180'"
711 " href='%sapple-touch-icon.png' />\n"
712 "<link rel='icon' type='image/png' sizes='32x32'"
713 " href='%sfavicon-32x32.png' />\n"
714 "<link rel='icon' type='image/png' sizes='16x16'"
715 " href='%sfavicon-16x16.png' />\n"
716 "<link rel='manifest' href='%ssite.webmanifest'/>\n"
717 "<link rel='mask-icon' href='%ssafari-pinned-tab.svg' />\n"
718 "<link rel='stylesheet' type='text/css' href='%s%s' />\n"
719 "</head>\n"
720 "<body>\n"
721 "<div id='gw_body'>\n"
722 "<div id='header'>\n"
723 "<div id='got_link'>"
724 "<a href='%s' target='_blank'>"
725 "<img src='%s%s' alt='logo' id='logo' />"
726 "</a>\n"
727 "</div>\n" /* #got_link */
728 "</div>\n" /* #header */
729 "<div id='site_path'>\n"
730 "<div id='site_link'>\n"
731 "<a href='?index_page=%d'>%s</a>",
732 srv->site_name,
733 c->script_name,
734 c->script_name,
735 c->script_name,
736 c->script_name,
737 c->script_name,
738 c->script_name, srv->custom_css,
739 srv->logo_url,
740 c->script_name, srv->logo,
741 qs->index_page, srv->site_link);
742 if (r == -1)
743 goto done;
745 if (qs->path != NULL) {
746 char *epath;
748 if (fcgi_printf(c, " / ") == -1)
749 goto done;
751 err = gotweb_escape_html(&epath, qs->path);
752 if (err)
753 return err;
754 r = gotweb_link(c, &(struct gotweb_url){
755 .action = SUMMARY,
756 .index_page = -1,
757 .page = -1,
758 .path = qs->path,
759 }, "%s", epath);
760 free(epath);
761 if (r == -1)
762 goto done;
764 if (qs->action != INDEX) {
765 const char *action = "";
767 switch (qs->action) {
768 case BLAME:
769 action = "blame";
770 break;
771 case BRIEFS:
772 action = "briefs";
773 break;
774 case COMMITS:
775 action = "commits";
776 break;
777 case DIFF:
778 action = "diff";
779 break;
780 case SUMMARY:
781 action = "summary";
782 break;
783 case TAG:
784 action = "tag";
785 break;
786 case TAGS:
787 action = "tags";
788 break;
789 case TREE:
790 action = "tree";
791 break;
794 if (fcgi_printf(c, " / %s", action) == -1)
795 goto done;
798 fcgi_printf(c, "</div>\n" /* #site_path */
799 "</div>\n" /* #site_link */
800 "<div id='content'>\n");
802 done:
803 return NULL;
806 static const struct got_error *
807 gotweb_render_footer(struct request *c)
809 const struct got_error *error = NULL;
810 struct server *srv = c->srv;
811 const char *siteowner = "&nbsp;";
812 char *escaped_owner = NULL;
814 if (srv->show_site_owner) {
815 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
816 if (error)
817 return error;
818 siteowner = escaped_owner;
821 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
822 "<div id='site_owner'>%s</div>\n"
823 "</div>\n" /* #site_owner_wrapper */
824 "</div>\n" /* #content */
825 "</div>\n" /* #gw_body */
826 "</body>\n</html>\n", siteowner);
828 free(escaped_owner);
829 return NULL;
832 static const struct got_error *
833 gotweb_render_navs(struct request *c)
835 const struct got_error *error = NULL;
836 struct transport *t = c->t;
837 struct querystring *qs = t->qs;
838 struct server *srv = c->srv;
839 int r;
841 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
842 if (r == -1)
843 goto done;
845 switch(qs->action) {
846 case INDEX:
847 if (qs->index_page > 0) {
848 struct gotweb_url url = {
849 .action = -1,
850 .index_page = qs->index_page - 1,
851 .page = -1,
852 };
854 r = gotweb_link(c, &url, "Previous");
856 break;
857 case BRIEFS:
858 if (t->prev_id && qs->commit != NULL &&
859 strcmp(qs->commit, t->prev_id) != 0) {
860 struct gotweb_url url = {
861 .action = BRIEFS,
862 .index_page = -1,
863 .page = qs->page - 1,
864 .path = qs->path,
865 .commit = t->prev_id,
866 .headref = qs->headref,
867 };
869 r = gotweb_link(c, &url, "Previous");
871 break;
872 case COMMITS:
873 if (t->prev_id && qs->commit != NULL &&
874 strcmp(qs->commit, t->prev_id) != 0) {
875 struct gotweb_url url = {
876 .action = COMMIT,
877 .index_page = -1,
878 .page = qs->page - 1,
879 .path = qs->path,
880 .commit = t->prev_id,
881 .headref = qs->headref,
882 .folder = qs->folder,
883 .file = qs->file,
884 };
886 r = gotweb_link(c, &url, "Previous");
888 break;
889 case TAGS:
890 if (t->prev_id && qs->commit != NULL &&
891 strcmp(qs->commit, t->prev_id) != 0) {
892 struct gotweb_url url = {
893 .action = TAGS,
894 .index_page = -1,
895 .page = qs->page - 1,
896 .path = qs->path,
897 .commit = t->prev_id,
898 .headref = qs->headref,
899 };
901 r = gotweb_link(c, &url, "Previous");
903 break;
906 if (r == -1)
907 goto done;
909 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
910 "<div id='nav_next'>");
911 if (r == -1)
912 goto done;
914 switch(qs->action) {
915 case INDEX:
916 if (t->next_disp == srv->max_repos_display &&
917 t->repos_total != (qs->index_page + 1) *
918 srv->max_repos_display) {
919 struct gotweb_url url = {
920 .action = -1,
921 .index_page = qs->index_page + 1,
922 .page = -1,
923 };
925 r = gotweb_link(c, &url, "Next");
927 break;
928 case BRIEFS:
929 if (t->next_id) {
930 struct gotweb_url url = {
931 .action = BRIEFS,
932 .index_page = -1,
933 .page = qs->page + 1,
934 .path = qs->path,
935 .commit = t->next_id,
936 .headref = qs->headref,
937 };
939 r = gotweb_link(c, &url, "Next");
941 break;
942 case COMMITS:
943 if (t->next_id) {
944 struct gotweb_url url = {
945 .action = COMMIT,
946 .index_page = -1,
947 .page = qs->page + 1,
948 .path = qs->path,
949 .commit = t->next_id,
950 .headref = qs->headref,
951 .folder = qs->folder,
952 .file = qs->file,
953 };
955 r = gotweb_link(c, &url, "Next");
957 break;
958 case TAGS:
959 if (t->next_id) {
960 struct gotweb_url url = {
961 .action = TAGS,
962 .index_page = -1,
963 .page = qs->page + 1,
964 .path = qs->path,
965 .commit = t->next_id,
966 .headref = qs->headref,
967 };
969 r = gotweb_link(c, &url, "Next");
971 break;
973 if (r == -1)
974 goto done;
976 fcgi_printf(c, "</div>\n"); /* #nav_next */
977 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
978 done:
979 free(t->next_id);
980 t->next_id = NULL;
981 free(t->prev_id);
982 t->prev_id = NULL;
983 return error;
986 static const struct got_error *
987 gotweb_render_index(struct request *c)
989 const struct got_error *error = NULL;
990 struct server *srv = c->srv;
991 struct transport *t = c->t;
992 struct querystring *qs = t->qs;
993 struct repo_dir *repo_dir = NULL;
994 DIR *d;
995 struct dirent **sd_dent = NULL;
996 char *c_path = NULL;
997 struct stat st;
998 unsigned int d_cnt, d_i, d_disp = 0;
999 int r;
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 /* get total count of repos */
1015 for (d_i = 0; d_i < d_cnt; d_i++) {
1016 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1017 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1018 continue;
1020 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1021 sd_dent[d_i]->d_name) == -1) {
1022 error = got_error_from_errno("asprintf");
1023 return error;
1026 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1027 !got_path_dir_is_empty(c_path))
1028 t->repos_total++;
1029 free(c_path);
1030 c_path = NULL;
1033 r = fcgi_printf(c, "<div id='index_header'>\n"
1034 "<div id='index_header_project'>Project</div>\n");
1035 if (r == -1)
1036 goto done;
1038 if (srv->show_repo_description)
1039 if (fcgi_printf(c, "<div id='index_header_description'>"
1040 "Description</div>\n") == -1)
1041 goto done;
1042 if (srv->show_repo_owner)
1043 if (fcgi_printf(c, "<div id='index_header_owner'>"
1044 "Owner</div>\n") == -1)
1045 goto done;
1046 if (srv->show_repo_age)
1047 if (fcgi_printf(c, "<div id='index_header_age'>"
1048 "Last Change</div>\n") == -1)
1049 goto done;
1050 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1051 goto done;
1053 for (d_i = 0; d_i < d_cnt; d_i++) {
1054 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1055 break; /* account for parent and self */
1057 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1058 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1059 continue;
1061 if (qs->index_page > 0 && (qs->index_page *
1062 srv->max_repos_display) > t->prev_disp) {
1063 t->prev_disp++;
1064 continue;
1067 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1068 if (error)
1069 goto done;
1071 error = gotweb_load_got_path(c, repo_dir);
1072 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1073 error = NULL;
1074 continue;
1076 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1077 goto done;
1079 if (lstat(repo_dir->path, &st) == 0 &&
1080 S_ISDIR(st.st_mode) &&
1081 !got_path_dir_is_empty(repo_dir->path))
1082 goto render;
1083 else {
1084 gotweb_free_repo_dir(repo_dir);
1085 repo_dir = NULL;
1086 continue;
1088 render:
1089 d_disp++;
1090 t->prev_disp++;
1092 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1093 "<div class='index_project'>") == -1)
1094 goto done;
1096 r = gotweb_link(c, &(struct gotweb_url){
1097 .action = SUMMARY,
1098 .index_page = -1,
1099 .page = -1,
1100 .path = repo_dir->name,
1101 }, "%s", repo_dir->name);
1102 if (r == -1)
1103 goto done;
1105 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1106 goto done;
1108 if (srv->show_repo_description) {
1109 r = fcgi_printf(c,
1110 "<div class='index_project_description'>\n"
1111 "%s</div>\n", repo_dir->description);
1112 if (r == -1)
1113 goto done;
1116 if (srv->show_repo_owner) {
1117 r = fcgi_printf(c, "<div class='index_project_owner'>"
1118 "%s</div>\n", repo_dir->owner);
1119 if (r == -1)
1120 goto done;
1123 if (srv->show_repo_age) {
1124 r = fcgi_printf(c, "<div class='index_project_age'>"
1125 "%s</div>\n", repo_dir->age);
1126 if (r == -1)
1127 goto done;
1130 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1131 "<div class='navs'>") == -1)
1132 goto done;
1134 r = gotweb_link(c, &(struct gotweb_url){
1135 .action = SUMMARY,
1136 .index_page = -1,
1137 .page = -1,
1138 .path = repo_dir->name
1139 }, "summary");
1140 if (r == -1)
1141 goto done;
1143 if (fcgi_printf(c, " | ") == -1)
1144 goto done;
1146 r = gotweb_link(c, &(struct gotweb_url){
1147 .action = BRIEFS,
1148 .index_page = -1,
1149 .page = -1,
1150 .path = repo_dir->name
1151 }, "commit briefs");
1152 if (r == -1)
1153 goto done;
1155 if (fcgi_printf(c, " | ") == -1)
1156 goto done;
1158 r = gotweb_link(c, &(struct gotweb_url){
1159 .action = COMMITS,
1160 .index_page = -1,
1161 .page = -1,
1162 .path = repo_dir->name
1163 }, "commits");
1164 if (r == -1)
1165 goto done;
1167 if (fcgi_printf(c, " | ") == -1)
1168 goto done;
1170 r = gotweb_link(c, &(struct gotweb_url){
1171 .action = TAGS,
1172 .index_page = -1,
1173 .page = -1,
1174 .path = repo_dir->name
1175 }, "tags");
1176 if (r == -1)
1177 goto done;
1179 if (fcgi_printf(c, " | ") == -1)
1180 goto done;
1182 r = gotweb_link(c, &(struct gotweb_url){
1183 .action = TREE,
1184 .index_page = -1,
1185 .page = -1,
1186 .path = repo_dir->name
1187 }, "tree");
1188 if (r == -1)
1189 goto done;
1191 r = fcgi_printf(c, "</div>" /* .navs */
1192 "<div class='dotted_line'></div>\n"
1193 "</div>\n" /* .navs_wrapper */
1194 "</div>\n"); /* .index_wrapper */
1195 if (r == -1)
1196 goto done;
1198 gotweb_free_repo_dir(repo_dir);
1199 repo_dir = NULL;
1200 t->next_disp++;
1201 if (d_disp == srv->max_repos_display)
1202 break;
1204 if (srv->max_repos_display == 0)
1205 goto done;
1206 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1207 goto done;
1208 if (t->repos_total <= srv->max_repos ||
1209 t->repos_total <= srv->max_repos_display)
1210 goto done;
1212 error = gotweb_render_navs(c);
1213 if (error)
1214 goto done;
1215 done:
1216 if (sd_dent) {
1217 for (d_i = 0; d_i < d_cnt; d_i++)
1218 free(sd_dent[d_i]);
1219 free(sd_dent);
1221 if (d != NULL && closedir(d) == EOF && error == NULL)
1222 error = got_error_from_errno("closedir");
1223 return error;
1226 static const struct got_error *
1227 gotweb_render_blame(struct request *c)
1229 const struct got_error *error = NULL;
1230 struct transport *t = c->t;
1231 struct repo_commit *rc = NULL;
1232 char *age = NULL, *msg = NULL;
1233 int r;
1235 error = got_get_repo_commits(c, 1);
1236 if (error)
1237 return error;
1239 rc = TAILQ_FIRST(&t->repo_commits);
1241 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1242 if (error)
1243 goto done;
1244 error = gotweb_escape_html(&msg, rc->commit_msg);
1245 if (error)
1246 goto done;
1248 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1249 "<div id='blame_title'>Blame</div>\n"
1250 "</div>\n" /* #blame_title_wrapper */
1251 "<div id='blame_content'>\n"
1252 "<div id='blame_header_wrapper'>\n"
1253 "<div id='blame_header'>\n"
1254 "<div class='header_age_title'>Date:</div>\n"
1255 "<div class='header_age'>%s</div>\n"
1256 "<div id='header_commit_msg_title'>Message:</div>\n"
1257 "<div id='header_commit_msg'>%s</div>\n"
1258 "</div>\n" /* #blame_header */
1259 "</div>\n" /* #blame_header_wrapper */
1260 "<div class='dotted_line'></div>\n"
1261 "<div id='blame'>\n",
1262 age,
1263 msg);
1264 if (r == -1)
1265 goto done;
1267 error = got_output_file_blame(c);
1268 if (error)
1269 goto done;
1271 fcgi_printf(c, "</div>\n" /* #blame */
1272 "</div>\n"); /* #blame_content */
1273 done:
1274 free(age);
1275 free(msg);
1276 return error;
1279 static const struct got_error *
1280 gotweb_render_briefs(struct request *c)
1282 const struct got_error *error = NULL;
1283 struct repo_commit *rc = NULL;
1284 struct server *srv = c->srv;
1285 struct transport *t = c->t;
1286 struct querystring *qs = t->qs;
1287 struct repo_dir *repo_dir = t->repo_dir;
1288 char *smallerthan, *newline;
1289 char *age = NULL, *author = NULL, *msg = NULL;
1290 int r;
1292 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1293 "<div id='briefs_title'>Commit Briefs</div>\n"
1294 "</div>\n" /* #briefs_title_wrapper */
1295 "<div id='briefs_content'>\n");
1296 if (r == -1)
1297 goto done;
1299 if (qs->action == SUMMARY) {
1300 qs->action = BRIEFS;
1301 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1302 } else
1303 error = got_get_repo_commits(c, srv->max_commits_display);
1304 if (error)
1305 goto done;
1307 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1308 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1309 if (error)
1310 goto done;
1312 smallerthan = strchr(rc->author, '<');
1313 if (smallerthan)
1314 *smallerthan = '\0';
1316 newline = strchr(rc->commit_msg, '\n');
1317 if (newline)
1318 *newline = '\0';
1320 error = gotweb_escape_html(&author, rc->author);
1321 if (error)
1322 goto done;
1323 error = gotweb_escape_html(&msg, rc->commit_msg);
1324 if (error)
1325 goto done;
1327 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1328 "<div class='briefs_author'>%s</div>\n"
1329 "<div class='briefs_log'>",
1330 age, author);
1331 if (r == -1)
1332 goto done;
1334 r = gotweb_link(c, &(struct gotweb_url){
1335 .action = DIFF,
1336 .index_page = -1,
1337 .page = -1,
1338 .path = repo_dir->name,
1339 .commit = rc->commit_id,
1340 .headref = qs->headref,
1341 }, "%s", msg);
1342 if (r == -1)
1343 goto done;
1345 if (rc->refs_str) {
1346 char *refs;
1348 error = gotweb_escape_html(&refs, rc->refs_str);
1349 if (error)
1350 goto done;
1351 r = fcgi_printf(c,
1352 " <span class='refs_str'>(%s)</span>", refs);
1353 free(refs);
1354 if (r == -1)
1355 goto done;
1357 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1358 goto done;
1360 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1361 "<div class='navs'>");
1362 if (r == -1)
1363 goto done;
1365 r = gotweb_link(c, &(struct gotweb_url){
1366 .action = DIFF,
1367 .index_page = -1,
1368 .page = -1,
1369 .path = repo_dir->name,
1370 .commit = rc->commit_id,
1371 .headref = qs->headref,
1372 }, "diff");
1373 if (r == -1)
1374 goto done;
1376 if (fcgi_printf(c, " | ") == -1)
1377 goto done;
1379 r = gotweb_link(c, &(struct gotweb_url){
1380 .action = TREE,
1381 .index_page = -1,
1382 .page = -1,
1383 .path = repo_dir->name,
1384 .commit = rc->commit_id,
1385 .headref = qs->headref,
1386 }, "tree");
1387 if (r == -1)
1388 goto done;
1390 if (fcgi_printf(c, "</div>\n" /* .navs */
1391 "</div>\n" /* .navs_wrapper */
1392 "<div class='dotted_line'></div>\n") == -1)
1393 goto done;
1395 free(age);
1396 age = NULL;
1397 free(author);
1398 author = NULL;
1399 free(msg);
1400 msg = NULL;
1403 if (t->next_id || t->prev_id) {
1404 error = gotweb_render_navs(c);
1405 if (error)
1406 goto done;
1408 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1409 done:
1410 free(age);
1411 free(author);
1412 free(msg);
1413 return error;
1416 static const struct got_error *
1417 gotweb_render_commits(struct request *c)
1419 const struct got_error *error = NULL;
1420 struct repo_commit *rc = NULL;
1421 struct server *srv = c->srv;
1422 struct transport *t = c->t;
1423 struct repo_dir *repo_dir = t->repo_dir;
1424 char *age = NULL, *author = NULL, *msg = NULL;
1425 int r;
1427 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1428 "<div class='commits_title'>Commits</div>\n"
1429 "</div>\n" /* .commits_title_wrapper */
1430 "<div class='commits_content'>\n");
1431 if (r == -1)
1432 goto done;
1434 error = got_get_repo_commits(c, srv->max_commits_display);
1435 if (error)
1436 goto done;
1438 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1439 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1440 if (error)
1441 goto done;
1442 error = gotweb_escape_html(&author, rc->author);
1443 if (error)
1444 goto done;
1445 error = gotweb_escape_html(&msg, rc->commit_msg);
1446 if (error)
1447 goto done;
1449 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1450 "<div class='commits_header'>\n"
1451 "<div class='header_commit_title'>Commit:</div>\n"
1452 "<div class='header_commit'>%s</div>\n"
1453 "<div class='header_author_title'>Author:</div>\n"
1454 "<div class='header_author'>%s</div>\n"
1455 "<div class='header_age_title'>Date:</div>\n"
1456 "<div class='header_age'>%s</div>\n"
1457 "</div>\n" /* .commits_header */
1458 "</div>\n" /* .commits_header_wrapper */
1459 "<div class='dotted_line'></div>\n"
1460 "<div class='commit'>\n%s</div>\n",
1461 rc->commit_id,
1462 author,
1463 age,
1464 msg);
1465 if (r == -1)
1466 goto done;
1468 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1469 "<div class='navs'>") == -1)
1470 goto done;
1472 r = gotweb_link(c, &(struct gotweb_url){
1473 .action = DIFF,
1474 .index_page = -1,
1475 .page = -1,
1476 .path = repo_dir->name,
1477 .commit = rc->commit_id,
1478 }, "diff");
1479 if (r == -1)
1480 goto done;
1482 if (fcgi_printf(c, " | ") == -1)
1483 goto done;
1485 r = gotweb_link(c, &(struct gotweb_url){
1486 .action = TREE,
1487 .index_page = -1,
1488 .page = -1,
1489 .path = repo_dir->name,
1490 .commit = rc->commit_id,
1491 }, "tree");
1492 if (r == -1)
1493 goto done;
1495 if (fcgi_printf(c, "</div>\n" /* .navs */
1496 "</div>\n" /* .navs_wrapper */
1497 "<div class='dotted_line'></div>\n") == -1)
1498 goto done;
1500 free(age);
1501 age = NULL;
1502 free(author);
1503 author = NULL;
1504 free(msg);
1505 msg = NULL;
1508 if (t->next_id || t->prev_id) {
1509 error = gotweb_render_navs(c);
1510 if (error)
1511 goto done;
1513 fcgi_printf(c, "</div>\n"); /* .commits_content */
1514 done:
1515 free(age);
1516 free(author);
1517 free(msg);
1518 return error;
1521 static const struct got_error *
1522 gotweb_render_branches(struct request *c)
1524 const struct got_error *error = NULL;
1525 struct got_reflist_head refs;
1526 struct got_reflist_entry *re;
1527 struct transport *t = c->t;
1528 struct querystring *qs = t->qs;
1529 struct got_repository *repo = t->repo;
1530 char *escaped_refname = NULL;
1531 char *age = NULL;
1532 int r;
1534 TAILQ_INIT(&refs);
1536 error = got_ref_list(&refs, repo, "refs/heads",
1537 got_ref_cmp_by_name, NULL);
1538 if (error)
1539 goto done;
1541 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1542 "<div id='branches_title'>Branches</div>\n"
1543 "</div>\n" /* #branches_title_wrapper */
1544 "<div id='branches_content'>\n");
1545 if (r == -1)
1546 goto done;
1548 TAILQ_FOREACH(re, &refs, entry) {
1549 const char *refname = NULL;
1551 if (got_ref_is_symbolic(re->ref))
1552 continue;
1554 refname = got_ref_get_name(re->ref);
1555 if (refname == NULL) {
1556 error = got_error_from_errno("strdup");
1557 goto done;
1559 if (strncmp(refname, "refs/heads/", 11) != 0)
1560 continue;
1562 error = got_get_repo_age(&age, c, qs->path, refname,
1563 TM_DIFF);
1564 if (error)
1565 goto done;
1567 if (strncmp(refname, "refs/heads/", 11) == 0)
1568 refname += 11;
1569 error = gotweb_escape_html(&escaped_refname, refname);
1570 if (error)
1571 goto done;
1573 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1574 "<div class='branches_age'>%s</div>\n"
1575 "<div class='branches_space'>&nbsp;</div>\n"
1576 "<div class='branch'>", age);
1577 if (r == -1)
1578 goto done;
1580 r = gotweb_link(c, &(struct gotweb_url){
1581 .action = SUMMARY,
1582 .index_page = -1,
1583 .page = -1,
1584 .path = qs->path,
1585 .headref = refname,
1586 }, "%s", escaped_refname);
1587 if (r == -1)
1588 goto done;
1590 if (fcgi_printf(c, "</div>\n" /* .branch */
1591 "<div class='navs_wrapper'>\n"
1592 "<div class='navs'>") == -1)
1593 goto done;
1595 r = gotweb_link(c, &(struct gotweb_url){
1596 .action = SUMMARY,
1597 .index_page = -1,
1598 .page = -1,
1599 .path = qs->path,
1600 .headref = refname,
1601 }, "summary");
1602 if (r == -1)
1603 goto done;
1605 if (fcgi_printf(c, " | ") == -1)
1606 goto done;
1608 r = gotweb_link(c, &(struct gotweb_url){
1609 .action = BRIEFS,
1610 .index_page = -1,
1611 .page = -1,
1612 .path = qs->path,
1613 .headref = refname,
1614 }, "commit briefs");
1615 if (r == -1)
1616 goto done;
1618 if (fcgi_printf(c, " | ") == -1)
1619 goto done;
1621 r = gotweb_link(c, &(struct gotweb_url){
1622 .action = COMMITS,
1623 .index_page = -1,
1624 .page = -1,
1625 .path = qs->path,
1626 .headref = refname,
1627 }, "commits");
1628 if (r == -1)
1629 goto done;
1631 r = fcgi_printf(c, "</div>\n" /* .navs */
1632 "</div>\n" /* .navs_wrapper */
1633 "<div class='dotted_line'></div>\n"
1634 "</div>\n"); /* .branches_wrapper */
1635 if (r == -1)
1636 goto done;
1638 free(age);
1639 age = NULL;
1640 free(escaped_refname);
1641 escaped_refname = NULL;
1643 fcgi_printf(c, "</div>\n"); /* #branches_content */
1644 done:
1645 free(age);
1646 free(escaped_refname);
1647 got_ref_list_free(&refs);
1648 return error;
1651 static const struct got_error *
1652 gotweb_render_tree(struct request *c)
1654 const struct got_error *error = NULL;
1655 struct transport *t = c->t;
1656 struct repo_commit *rc = NULL;
1657 char *age = NULL, *msg = NULL;
1658 int r;
1660 error = got_get_repo_commits(c, 1);
1661 if (error)
1662 return error;
1664 rc = TAILQ_FIRST(&t->repo_commits);
1666 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1667 if (error)
1668 goto done;
1670 error = gotweb_escape_html(&msg, rc->commit_msg);
1671 if (error)
1672 goto done;
1674 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1675 "<div id='tree_title'>Tree</div>\n"
1676 "</div>\n" /* #tree_title_wrapper */
1677 "<div id='tree_content'>\n"
1678 "<div id='tree_header_wrapper'>\n"
1679 "<div id='tree_header'>\n"
1680 "<div id='header_tree_title'>Tree:</div>\n"
1681 "<div id='header_tree'>%s</div>\n"
1682 "<div class='header_age_title'>Date:</div>\n"
1683 "<div class='header_age'>%s</div>\n"
1684 "<div id='header_commit_msg_title'>Message:</div>\n"
1685 "<div id='header_commit_msg'>%s</div>\n"
1686 "</div>\n" /* #tree_header */
1687 "</div>\n" /* #tree_header_wrapper */
1688 "<div class='dotted_line'></div>\n"
1689 "<div id='tree'>\n",
1690 rc->tree_id,
1691 age,
1692 msg);
1693 if (r == -1)
1694 goto done;
1696 error = got_output_repo_tree(c);
1697 if (error)
1698 goto done;
1700 fcgi_printf(c, "</div>\n"); /* #tree */
1701 fcgi_printf(c, "</div>\n"); /* #tree_content */
1702 done:
1703 free(age);
1704 free(msg);
1705 return error;
1708 static const struct got_error *
1709 gotweb_render_diff(struct request *c)
1711 const struct got_error *error = NULL;
1712 struct transport *t = c->t;
1713 struct repo_commit *rc = NULL;
1714 char *age = NULL, *author = NULL, *msg = NULL;
1715 int r;
1717 error = got_get_repo_commits(c, 1);
1718 if (error)
1719 return error;
1721 rc = TAILQ_FIRST(&t->repo_commits);
1723 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1724 if (error)
1725 goto done;
1726 error = gotweb_escape_html(&author, rc->author);
1727 if (error)
1728 goto done;
1729 error = gotweb_escape_html(&msg, rc->commit_msg);
1730 if (error)
1731 goto done;
1733 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1734 "<div id='diff_title'>Commit Diff</div>\n"
1735 "</div>\n" /* #diff_title_wrapper */
1736 "<div id='diff_content'>\n"
1737 "<div id='diff_header_wrapper'>\n"
1738 "<div id='diff_header'>\n"
1739 "<div id='header_diff_title'>Diff:</div>\n"
1740 "<div id='header_diff'>%s<br />%s</div>\n"
1741 "<div class='header_commit_title'>Commit:</div>\n"
1742 "<div class='header_commit'>%s</div>\n"
1743 "<div id='header_tree_title'>Tree:</div>\n"
1744 "<div id='header_tree'>%s</div>\n"
1745 "<div class='header_author_title'>Author:</div>\n"
1746 "<div class='header_author'>%s</div>\n"
1747 "<div class='header_age_title'>Date:</div>\n"
1748 "<div class='header_age'>%s</div>\n"
1749 "<div id='header_commit_msg_title'>Message:</div>\n"
1750 "<div id='header_commit_msg'>%s</div>\n"
1751 "</div>\n" /* #diff_header */
1752 "</div>\n" /* #diff_header_wrapper */
1753 "<div class='dotted_line'></div>\n"
1754 "<div id='diff'>\n",
1755 rc->parent_id, rc->commit_id,
1756 rc->commit_id,
1757 rc->tree_id,
1758 author,
1759 age,
1760 msg);
1761 if (r == -1)
1762 goto done;
1764 error = got_output_repo_diff(c);
1765 if (error)
1766 goto done;
1768 fcgi_printf(c, "</div>\n"); /* #diff */
1769 fcgi_printf(c, "</div>\n"); /* #diff_content */
1770 done:
1771 free(age);
1772 free(author);
1773 free(msg);
1774 return error;
1777 static const struct got_error *
1778 gotweb_render_summary(struct request *c)
1780 const struct got_error *error = NULL;
1781 struct transport *t = c->t;
1782 struct server *srv = c->srv;
1783 int r;
1785 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1786 goto done;
1788 if (srv->show_repo_description) {
1789 r = fcgi_printf(c,
1790 "<div id='description_title'>Description:</div>\n"
1791 "<div id='description'>%s</div>\n",
1792 t->repo_dir->description ? t->repo_dir->description : "");
1793 if (r == -1)
1794 goto done;
1797 if (srv->show_repo_owner) {
1798 r = fcgi_printf(c,
1799 "<div id='repo_owner_title'>Owner:</div>\n"
1800 "<div id='repo_owner'>%s</div>\n",
1801 t->repo_dir->owner ? t->repo_dir->owner : "");
1802 if (r == -1)
1803 goto done;
1806 if (srv->show_repo_age) {
1807 r = fcgi_printf(c,
1808 "<div id='last_change_title'>Last Change:</div>\n"
1809 "<div id='last_change'>%s</div>\n",
1810 t->repo_dir->age);
1811 if (r == -1)
1812 goto done;
1815 if (srv->show_repo_cloneurl) {
1816 r = fcgi_printf(c,
1817 "<div id='cloneurl_title'>Clone URL:</div>\n"
1818 "<div id='cloneurl'>%s</div>\n",
1819 t->repo_dir->url ? t->repo_dir->url : "");
1820 if (r == -1)
1821 goto done;
1824 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1825 if (r == -1)
1826 goto done;
1828 error = gotweb_render_briefs(c);
1829 if (error) {
1830 log_warnx("%s: %s", __func__, error->msg);
1831 goto done;
1834 error = gotweb_render_tags(c);
1835 if (error) {
1836 log_warnx("%s: %s", __func__, error->msg);
1837 goto done;
1840 error = gotweb_render_branches(c);
1841 if (error)
1842 log_warnx("%s: %s", __func__, error->msg);
1843 done:
1844 return error;
1847 static const struct got_error *
1848 gotweb_render_tag(struct request *c)
1850 const struct got_error *error = NULL;
1851 struct repo_tag *rt = NULL;
1852 struct transport *t = c->t;
1853 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1855 error = got_get_repo_tags(c, 1);
1856 if (error)
1857 goto done;
1859 if (t->tag_count == 0) {
1860 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1861 "bad commit id");
1862 goto done;
1865 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1867 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1868 if (error)
1869 goto done;
1870 error = gotweb_escape_html(&author, rt->tagger);
1871 if (error)
1872 goto done;
1873 error = gotweb_escape_html(&msg, rt->commit_msg);
1874 if (error)
1875 goto done;
1877 tagname = rt->tag_name;
1878 if (strncmp(tagname, "refs/", 5) == 0)
1879 tagname += 5;
1880 error = gotweb_escape_html(&tagname, tagname);
1881 if (error)
1882 goto done;
1884 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1885 "<div id='tags_title'>Tag</div>\n"
1886 "</div>\n" /* #tags_title_wrapper */
1887 "<div id='tags_content'>\n"
1888 "<div id='tag_header_wrapper'>\n"
1889 "<div id='tag_header'>\n"
1890 "<div class='header_commit_title'>Commit:</div>\n"
1891 "<div class='header_commit'>%s"
1892 " <span class='refs_str'>(%s)</span></div>\n"
1893 "<div class='header_author_title'>Tagger:</div>\n"
1894 "<div class='header_author'>%s</div>\n"
1895 "<div class='header_age_title'>Date:</div>\n"
1896 "<div class='header_age'>%s</div>\n"
1897 "<div id='header_commit_msg_title'>Message:</div>\n"
1898 "<div id='header_commit_msg'>%s</div>\n"
1899 "</div>\n" /* #tag_header */
1900 "<div class='dotted_line'></div>\n"
1901 "<div id='tag_commit'>\n%s</div>"
1902 "</div>" /* #tag_header_wrapper */
1903 "</div>", /* #tags_content */
1904 rt->commit_id,
1905 tagname,
1906 author,
1907 age,
1908 msg,
1909 rt->tag_commit);
1911 done:
1912 free(age);
1913 free(author);
1914 free(msg);
1915 return error;
1918 static const struct got_error *
1919 gotweb_render_tags(struct request *c)
1921 const struct got_error *error = NULL;
1922 struct repo_tag *rt = NULL;
1923 struct server *srv = c->srv;
1924 struct transport *t = c->t;
1925 struct querystring *qs = t->qs;
1926 struct repo_dir *repo_dir = t->repo_dir;
1927 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1928 int r, commit_found = 0;
1930 if (qs->action == BRIEFS) {
1931 qs->action = TAGS;
1932 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1933 } else
1934 error = got_get_repo_tags(c, srv->max_commits_display);
1935 if (error)
1936 goto done;
1938 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1939 "<div id='tags_title'>Tags</div>\n"
1940 "</div>\n" /* #tags_title_wrapper */
1941 "<div id='tags_content'>\n");
1942 if (r == -1)
1943 goto done;
1945 if (t->tag_count == 0) {
1946 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1947 "This repository contains no tags");
1948 if (r == -1)
1949 goto done;
1952 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1953 if (commit_found == 0 && qs->commit != NULL) {
1954 if (strcmp(qs->commit, rt->commit_id) != 0)
1955 continue;
1956 else
1957 commit_found = 1;
1959 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1960 if (error)
1961 goto done;
1963 tagname = rt->tag_name;
1964 if (strncmp(tagname, "refs/tags/", 10) == 0)
1965 tagname += 10;
1966 error = gotweb_escape_html(&tagname, tagname);
1967 if (error)
1968 goto done;
1970 if (rt->tag_commit != NULL) {
1971 newline = strchr(rt->tag_commit, '\n');
1972 if (newline)
1973 *newline = '\0';
1974 error = gotweb_escape_html(&msg, rt->tag_commit);
1975 if (error)
1976 goto done;
1979 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1980 "<div class='tag'>%s</div>\n"
1981 "<div class='tag_log'>", age, tagname) == -1)
1982 goto done;
1984 r = gotweb_link(c, &(struct gotweb_url){
1985 .action = TAG,
1986 .index_page = -1,
1987 .page = -1,
1988 .path = repo_dir->name,
1989 .commit = rt->commit_id,
1990 }, "%s", msg ? msg : "");
1991 if (r == -1)
1992 goto done;
1994 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1995 "<div class='navs_wrapper'>\n"
1996 "<div class='navs'>") == -1)
1997 goto done;
1999 r = gotweb_link(c, &(struct gotweb_url){
2000 .action = TAG,
2001 .index_page = -1,
2002 .page = -1,
2003 .path = repo_dir->name,
2004 .commit = rt->commit_id,
2005 }, "tag");
2006 if (r == -1)
2007 goto done;
2009 if (fcgi_printf(c, " | ") == -1)
2010 goto done;
2012 r = gotweb_link(c, &(struct gotweb_url){
2013 .action = BRIEFS,
2014 .index_page = -1,
2015 .page = -1,
2016 .path = repo_dir->name,
2017 .commit = rt->commit_id,
2018 }, "commit briefs");
2019 if (r == -1)
2020 goto done;
2022 if (fcgi_printf(c, " | ") == -1)
2023 goto done;
2025 r = gotweb_link(c, &(struct gotweb_url){
2026 .action = COMMITS,
2027 .index_page = -1,
2028 .page = -1,
2029 .path = repo_dir->name,
2030 .commit = rt->commit_id,
2031 }, "commits");
2032 if (r == -1)
2033 goto done;
2035 r = fcgi_printf(c,
2036 "</div>\n" /* .navs */
2037 "</div>\n" /* .navs_wrapper */
2038 "<div class='dotted_line'></div>\n");
2039 if (r == -1)
2040 goto done;
2042 free(age);
2043 age = NULL;
2044 free(tagname);
2045 tagname = NULL;
2046 free(msg);
2047 msg = NULL;
2049 if (t->next_id || t->prev_id) {
2050 error = gotweb_render_navs(c);
2051 if (error)
2052 goto done;
2054 fcgi_printf(c, "</div>\n"); /* #tags_content */
2055 done:
2056 free(age);
2057 free(tagname);
2058 free(msg);
2059 return error;
2062 const struct got_error *
2063 gotweb_escape_html(char **escaped_html, const char *orig_html)
2065 const struct got_error *error = NULL;
2066 struct escape_pair {
2067 char c;
2068 const char *s;
2069 } esc[] = {
2070 { '>', "&gt;" },
2071 { '<', "&lt;" },
2072 { '&', "&amp;" },
2073 { '"', "&quot;" },
2074 { '\'', "&apos;" },
2075 { '\n', "<br />" },
2077 size_t orig_len, len;
2078 int i, j, x;
2080 orig_len = strlen(orig_html);
2081 len = orig_len;
2082 for (i = 0; i < orig_len; i++) {
2083 for (j = 0; j < nitems(esc); j++) {
2084 if (orig_html[i] != esc[j].c)
2085 continue;
2086 len += strlen(esc[j].s) - 1 /* escaped char */;
2090 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2091 if (*escaped_html == NULL)
2092 return got_error_from_errno("calloc");
2094 x = 0;
2095 for (i = 0; i < orig_len; i++) {
2096 int escaped = 0;
2097 for (j = 0; j < nitems(esc); j++) {
2098 if (orig_html[i] != esc[j].c)
2099 continue;
2101 if (strlcat(*escaped_html, esc[j].s, len + 1)
2102 >= len + 1) {
2103 error = got_error(GOT_ERR_NO_SPACE);
2104 goto done;
2106 x += strlen(esc[j].s);
2107 escaped = 1;
2108 break;
2110 if (!escaped) {
2111 (*escaped_html)[x] = orig_html[i];
2112 x++;
2115 done:
2116 if (error) {
2117 free(*escaped_html);
2118 *escaped_html = NULL;
2119 } else {
2120 (*escaped_html)[x] = '\0';
2123 return error;
2126 static inline int
2127 should_urlencode(int c)
2129 if (c <= ' ' || c >= 127)
2130 return 1;
2132 switch (c) {
2133 /* gen-delim */
2134 case ':':
2135 case '/':
2136 case '?':
2137 case '#':
2138 case '[':
2139 case ']':
2140 case '@':
2141 /* sub-delims */
2142 case '!':
2143 case '$':
2144 case '&':
2145 case '\'':
2146 case '(':
2147 case ')':
2148 case '*':
2149 case '+':
2150 case ',':
2151 case ';':
2152 case '=':
2153 return 1;
2154 default:
2155 return 0;
2159 static char *
2160 gotweb_urlencode(const char *str)
2162 const char *s;
2163 char *escaped;
2164 size_t i, len;
2165 int a, b;
2167 len = 0;
2168 for (s = str; *s; ++s) {
2169 len++;
2170 if (should_urlencode(*s))
2171 len += 2;
2174 escaped = calloc(1, len + 1);
2175 if (escaped == NULL)
2176 return NULL;
2178 i = 0;
2179 for (s = str; *s; ++s) {
2180 if (should_urlencode(*s)) {
2181 a = (*s & 0xF0) >> 4;
2182 b = (*s & 0x0F);
2184 escaped[i++] = '%';
2185 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2186 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2187 } else
2188 escaped[i++] = *s;
2191 return escaped;
2194 static inline const char *
2195 action_name(int action)
2197 switch (action) {
2198 case BLAME:
2199 return "blame";
2200 case BLOB:
2201 return "blob";
2202 case BRIEFS:
2203 return "briefs";
2204 case COMMITS:
2205 return "commits";
2206 case DIFF:
2207 return "diff";
2208 case ERR:
2209 return "err";
2210 case INDEX:
2211 return "index";
2212 case SUMMARY:
2213 return "summary";
2214 case TAG:
2215 return "tag";
2216 case TAGS:
2217 return "tags";
2218 case TREE:
2219 return "tree";
2220 default:
2221 return NULL;
2225 static int
2226 gotweb_print_url(struct request *c, struct gotweb_url *url)
2228 const char *sep = "?", *action;
2229 char *tmp;
2230 int r;
2232 action = action_name(url->action);
2233 if (action != NULL) {
2234 if (fcgi_printf(c, "?action=%s", action) == -1)
2235 return -1;
2236 sep = "&";
2239 if (url->commit) {
2240 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2241 return -1;
2242 sep = "&";
2245 if (url->previd) {
2246 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2247 return -1;
2248 sep = "&";
2251 if (url->prevset) {
2252 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2253 return -1;
2254 sep = "&";
2257 if (url->file) {
2258 tmp = gotweb_urlencode(url->file);
2259 if (tmp == NULL)
2260 return -1;
2261 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2262 free(tmp);
2263 if (r == -1)
2264 return -1;
2265 sep = "&";
2268 if (url->folder) {
2269 tmp = gotweb_urlencode(url->folder);
2270 if (tmp == NULL)
2271 return -1;
2272 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2273 free(tmp);
2274 if (r == -1)
2275 return -1;
2276 sep = "&";
2279 if (url->headref) {
2280 tmp = gotweb_urlencode(url->headref);
2281 if (tmp == NULL)
2282 return -1;
2283 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2284 free(tmp);
2285 if (r == -1)
2286 return -1;
2287 sep = "&";
2290 if (url->index_page != -1) {
2291 if (fcgi_printf(c, "%sindex_page=%d", sep,
2292 url->index_page) == -1)
2293 return -1;
2294 sep = "&";
2297 if (url->path) {
2298 tmp = gotweb_urlencode(url->path);
2299 if (tmp == NULL)
2300 return -1;
2301 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2302 free(tmp);
2303 if (r == -1)
2304 return -1;
2305 sep = "&";
2308 if (url->page != -1) {
2309 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2310 return -1;
2311 sep = "&";
2314 return 0;
2317 int
2318 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2320 va_list ap;
2321 int r;
2323 if (fcgi_printf(c, "<a href='") == -1)
2324 return -1;
2326 if (gotweb_print_url(c, url) == -1)
2327 return -1;
2329 if (fcgi_printf(c, "'>") == -1)
2330 return -1;
2332 va_start(ap, fmt);
2333 r = fcgi_vprintf(c, fmt, ap);
2334 va_end(ap);
2335 if (r == -1)
2336 return -1;
2338 if (fcgi_printf(c, "</a>"))
2339 return -1;
2340 return 0;
2343 static struct got_repository *
2344 find_cached_repo(struct server *srv, const char *path)
2346 int i;
2348 for (i = 0; i < srv->ncached_repos; i++) {
2349 if (strcmp(srv->cached_repos[i].path, path) == 0)
2350 return srv->cached_repos[i].repo;
2353 return NULL;
2356 static const struct got_error *
2357 cache_repo(struct got_repository **new, struct server *srv,
2358 struct repo_dir *repo_dir, struct socket *sock)
2360 const struct got_error *error = NULL;
2361 struct got_repository *repo;
2362 struct cached_repo *cr;
2363 int evicted = 0;
2365 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2366 cr = &srv->cached_repos[srv->ncached_repos - 1];
2367 error = got_repo_close(cr->repo);
2368 memset(cr, 0, sizeof(*cr));
2369 srv->ncached_repos--;
2370 if (error)
2371 return error;
2372 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2373 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2374 cr = &srv->cached_repos[0];
2375 evicted = 1;
2376 } else {
2377 cr = &srv->cached_repos[srv->ncached_repos];
2380 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2381 if (error) {
2382 if (evicted) {
2383 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2384 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2386 return error;
2389 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2390 >= sizeof(cr->path)) {
2391 if (evicted) {
2392 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2393 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2395 return got_error(GOT_ERR_NO_SPACE);
2398 cr->repo = repo;
2399 srv->ncached_repos++;
2400 *new = repo;
2401 return NULL;
2404 static const struct got_error *
2405 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2407 const struct got_error *error = NULL;
2408 struct socket *sock = c->sock;
2409 struct server *srv = c->srv;
2410 struct transport *t = c->t;
2411 struct got_repository *repo = NULL;
2412 DIR *dt;
2413 char *dir_test;
2415 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2416 GOTWEB_GIT_DIR) == -1)
2417 return got_error_from_errno("asprintf");
2419 dt = opendir(dir_test);
2420 if (dt == NULL) {
2421 free(dir_test);
2422 } else {
2423 repo_dir->path = dir_test;
2424 dir_test = NULL;
2425 goto done;
2428 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2429 repo_dir->name) == -1)
2430 return got_error_from_errno("asprintf");
2432 dt = opendir(dir_test);
2433 if (dt == NULL) {
2434 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2435 goto err;
2436 } else {
2437 repo_dir->path = dir_test;
2438 dir_test = NULL;
2441 done:
2442 if (srv->respect_exportok &&
2443 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2444 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2445 goto err;
2449 repo = find_cached_repo(srv, repo_dir->path);
2450 if (repo == NULL) {
2451 error = cache_repo(&repo, srv, repo_dir, sock);
2452 if (error)
2453 goto err;
2455 t->repo = repo;
2456 error = gotweb_get_repo_description(&repo_dir->description, srv,
2457 repo_dir->path);
2458 if (error)
2459 goto err;
2460 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2461 if (error)
2462 goto err;
2463 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2464 NULL, TM_DIFF);
2465 if (error)
2466 goto err;
2467 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2468 err:
2469 free(dir_test);
2470 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2471 error = got_error_from_errno("closedir");
2472 return error;
2475 static const struct got_error *
2476 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2478 const struct got_error *error;
2480 *repo_dir = calloc(1, sizeof(**repo_dir));
2481 if (*repo_dir == NULL)
2482 return got_error_from_errno("calloc");
2484 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2485 error = got_error_from_errno("asprintf");
2486 free(*repo_dir);
2487 *repo_dir = NULL;
2488 return error;
2490 (*repo_dir)->owner = NULL;
2491 (*repo_dir)->description = NULL;
2492 (*repo_dir)->url = NULL;
2493 (*repo_dir)->age = NULL;
2494 (*repo_dir)->path = NULL;
2496 return NULL;
2499 static const struct got_error *
2500 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2502 const struct got_error *error = NULL;
2503 FILE *f = NULL;
2504 char *d_file = NULL;
2505 unsigned int len;
2506 size_t n;
2508 *description = NULL;
2509 if (srv->show_repo_description == 0)
2510 return NULL;
2512 if (asprintf(&d_file, "%s/description", dir) == -1)
2513 return got_error_from_errno("asprintf");
2515 f = fopen(d_file, "r");
2516 if (f == NULL) {
2517 if (errno != ENOENT && errno != EACCES)
2518 error = got_error_from_errno2("fopen", d_file);
2519 goto done;
2522 if (fseek(f, 0, SEEK_END) == -1) {
2523 error = got_ferror(f, GOT_ERR_IO);
2524 goto done;
2526 len = ftell(f);
2527 if (len == -1) {
2528 error = got_ferror(f, GOT_ERR_IO);
2529 goto done;
2532 if (len == 0) {
2533 *description = strdup("");
2534 if (*description == NULL)
2535 error = got_error_from_errno("strdup");
2536 goto done;
2539 if (fseek(f, 0, SEEK_SET) == -1) {
2540 error = got_ferror(f, GOT_ERR_IO);
2541 goto done;
2543 *description = calloc(len + 1, sizeof(**description));
2544 if (*description == NULL) {
2545 error = got_error_from_errno("calloc");
2546 goto done;
2549 n = fread(*description, 1, len, f);
2550 if (n == 0 && ferror(f))
2551 error = got_ferror(f, GOT_ERR_IO);
2552 done:
2553 if (f != NULL && fclose(f) == EOF && error == NULL)
2554 error = got_error_from_errno("fclose");
2555 free(d_file);
2556 return error;
2559 static const struct got_error *
2560 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2562 const struct got_error *error = NULL;
2563 FILE *f;
2564 char *d_file = NULL;
2565 unsigned int len;
2566 size_t n;
2568 *url = NULL;
2570 if (srv->show_repo_cloneurl == 0)
2571 return NULL;
2573 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2574 return got_error_from_errno("asprintf");
2576 f = fopen(d_file, "r");
2577 if (f == NULL) {
2578 if (errno != ENOENT && errno != EACCES)
2579 error = got_error_from_errno2("fopen", d_file);
2580 goto done;
2583 if (fseek(f, 0, SEEK_END) == -1) {
2584 error = got_ferror(f, GOT_ERR_IO);
2585 goto done;
2587 len = ftell(f);
2588 if (len == -1) {
2589 error = got_ferror(f, GOT_ERR_IO);
2590 goto done;
2592 if (len == 0)
2593 goto done;
2595 if (fseek(f, 0, SEEK_SET) == -1) {
2596 error = got_ferror(f, GOT_ERR_IO);
2597 goto done;
2600 *url = calloc(len + 1, sizeof(**url));
2601 if (*url == NULL) {
2602 error = got_error_from_errno("calloc");
2603 goto done;
2606 n = fread(*url, 1, len, f);
2607 if (n == 0 && ferror(f))
2608 error = got_ferror(f, GOT_ERR_IO);
2609 done:
2610 if (f != NULL && fclose(f) == EOF && error == NULL)
2611 error = got_error_from_errno("fclose");
2612 free(d_file);
2613 return error;
2616 const struct got_error *
2617 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2619 struct tm tm;
2620 long long diff_time;
2621 const char *years = "years ago", *months = "months ago";
2622 const char *weeks = "weeks ago", *days = "days ago";
2623 const char *hours = "hours ago", *minutes = "minutes ago";
2624 const char *seconds = "seconds ago", *now = "right now";
2625 char *s;
2626 char datebuf[29];
2628 *repo_age = NULL;
2630 switch (ref_tm) {
2631 case TM_DIFF:
2632 diff_time = time(NULL) - committer_time;
2633 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2634 if (asprintf(repo_age, "%lld %s",
2635 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2638 if (asprintf(repo_age, "%lld %s",
2639 (diff_time / 60 / 60 / 24 / (365 / 12)),
2640 months) == -1)
2641 return got_error_from_errno("asprintf");
2642 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2643 if (asprintf(repo_age, "%lld %s",
2644 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2645 return got_error_from_errno("asprintf");
2646 } else if (diff_time > 60 * 60 * 24 * 2) {
2647 if (asprintf(repo_age, "%lld %s",
2648 (diff_time / 60 / 60 / 24), days) == -1)
2649 return got_error_from_errno("asprintf");
2650 } else if (diff_time > 60 * 60 * 2) {
2651 if (asprintf(repo_age, "%lld %s",
2652 (diff_time / 60 / 60), hours) == -1)
2653 return got_error_from_errno("asprintf");
2654 } else if (diff_time > 60 * 2) {
2655 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2656 minutes) == -1)
2657 return got_error_from_errno("asprintf");
2658 } else if (diff_time > 2) {
2659 if (asprintf(repo_age, "%lld %s", diff_time,
2660 seconds) == -1)
2661 return got_error_from_errno("asprintf");
2662 } else {
2663 if (asprintf(repo_age, "%s", now) == -1)
2664 return got_error_from_errno("asprintf");
2666 break;
2667 case TM_LONG:
2668 if (gmtime_r(&committer_time, &tm) == NULL)
2669 return got_error_from_errno("gmtime_r");
2671 s = asctime_r(&tm, datebuf);
2672 if (s == NULL)
2673 return got_error_from_errno("asctime_r");
2675 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2676 return got_error_from_errno("asprintf");
2677 break;
2679 return NULL;