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 <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_object.h"
38 #include "got_reference.h"
39 #include "got_repository.h"
40 #include "got_path.h"
41 #include "got_cancel.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
48 #include "proc.h"
49 #include "gotwebd.h"
51 #include "got_compat.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, "%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 != NULL) {
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;
800 fcgi_printf(c, "</div>\n" /* #site_path */
801 "</div>\n" /* #site_link */
802 "<div id='content'>\n");
804 done:
805 return NULL;
808 static const struct got_error *
809 gotweb_render_footer(struct request *c)
811 const struct got_error *error = NULL;
812 struct server *srv = c->srv;
813 const char *siteowner = "&nbsp;";
814 char *escaped_owner = NULL;
816 if (srv->show_site_owner) {
817 error = gotweb_escape_html(&escaped_owner, srv->site_owner);
818 if (error)
819 return error;
820 siteowner = escaped_owner;
823 fcgi_printf(c, "<div id='site_owner_wrapper'>\n"
824 "<div id='site_owner'>%s</div>\n"
825 "</div>\n" /* #site_owner_wrapper */
826 "</div>\n" /* #content */
827 "</div>\n" /* #gw_body */
828 "</body>\n</html>\n", siteowner);
830 free(escaped_owner);
831 return NULL;
834 static const struct got_error *
835 gotweb_render_navs(struct request *c)
837 const struct got_error *error = NULL;
838 struct transport *t = c->t;
839 struct querystring *qs = t->qs;
840 struct server *srv = c->srv;
841 int r;
843 r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
844 if (r == -1)
845 goto done;
847 switch(qs->action) {
848 case INDEX:
849 if (qs->index_page > 0) {
850 struct gotweb_url url = {
851 .action = -1,
852 .index_page = qs->index_page - 1,
853 .page = -1,
854 };
856 r = gotweb_link(c, &url, "Previous");
858 break;
859 case BRIEFS:
860 if (t->prev_id && qs->commit != NULL &&
861 strcmp(qs->commit, t->prev_id) != 0) {
862 struct gotweb_url url = {
863 .action = BRIEFS,
864 .index_page = -1,
865 .page = qs->page - 1,
866 .path = qs->path,
867 .commit = t->prev_id,
868 .headref = qs->headref,
869 };
871 r = gotweb_link(c, &url, "Previous");
873 break;
874 case COMMITS:
875 if (t->prev_id && qs->commit != NULL &&
876 strcmp(qs->commit, t->prev_id) != 0) {
877 struct gotweb_url url = {
878 .action = COMMIT,
879 .index_page = -1,
880 .page = qs->page - 1,
881 .path = qs->path,
882 .commit = t->prev_id,
883 .headref = qs->headref,
884 .folder = qs->folder,
885 .file = qs->file,
886 };
888 r = gotweb_link(c, &url, "Previous");
890 break;
891 case TAGS:
892 if (t->prev_id && qs->commit != NULL &&
893 strcmp(qs->commit, t->prev_id) != 0) {
894 struct gotweb_url url = {
895 .action = TAGS,
896 .index_page = -1,
897 .page = qs->page - 1,
898 .path = qs->path,
899 .commit = t->prev_id,
900 .headref = qs->headref,
901 };
903 r = gotweb_link(c, &url, "Previous");
905 break;
908 if (r == -1)
909 goto done;
911 r = fcgi_printf(c, "</div>\n" /* #nav_prev */
912 "<div id='nav_next'>");
913 if (r == -1)
914 goto done;
916 switch(qs->action) {
917 case INDEX:
918 if (t->next_disp == srv->max_repos_display &&
919 t->repos_total != (qs->index_page + 1) *
920 srv->max_repos_display) {
921 struct gotweb_url url = {
922 .action = -1,
923 .index_page = qs->index_page + 1,
924 .page = -1,
925 };
927 r = gotweb_link(c, &url, "Next");
929 break;
930 case BRIEFS:
931 if (t->next_id) {
932 struct gotweb_url url = {
933 .action = BRIEFS,
934 .index_page = -1,
935 .page = qs->page + 1,
936 .path = qs->path,
937 .commit = t->next_id,
938 .headref = qs->headref,
939 };
941 r = gotweb_link(c, &url, "Next");
943 break;
944 case COMMITS:
945 if (t->next_id) {
946 struct gotweb_url url = {
947 .action = COMMIT,
948 .index_page = -1,
949 .page = qs->page + 1,
950 .path = qs->path,
951 .commit = t->next_id,
952 .headref = qs->headref,
953 .folder = qs->folder,
954 .file = qs->file,
955 };
957 r = gotweb_link(c, &url, "Next");
959 break;
960 case TAGS:
961 if (t->next_id) {
962 struct gotweb_url url = {
963 .action = TAGS,
964 .index_page = -1,
965 .page = qs->page + 1,
966 .path = qs->path,
967 .commit = t->next_id,
968 .headref = qs->headref,
969 };
971 r = gotweb_link(c, &url, "Next");
973 break;
975 if (r == -1)
976 goto done;
978 fcgi_printf(c, "</div>\n"); /* #nav_next */
979 fcgi_printf(c, "</div>\n"); /* #np_wrapper */
980 done:
981 free(t->next_id);
982 t->next_id = NULL;
983 free(t->prev_id);
984 t->prev_id = NULL;
985 return error;
988 static const struct got_error *
989 gotweb_render_index(struct request *c)
991 const struct got_error *error = NULL;
992 struct server *srv = c->srv;
993 struct transport *t = c->t;
994 struct querystring *qs = t->qs;
995 struct repo_dir *repo_dir = NULL;
996 DIR *d;
997 struct dirent **sd_dent = NULL;
998 char *c_path = NULL;
999 struct stat st;
1000 unsigned int d_cnt, d_i, d_disp = 0;
1001 int r;
1003 d = opendir(srv->repos_path);
1004 if (d == NULL) {
1005 error = got_error_from_errno2("opendir", srv->repos_path);
1006 return error;
1009 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1010 if (d_cnt == -1) {
1011 sd_dent = NULL;
1012 error = got_error_from_errno2("scandir", srv->repos_path);
1013 goto done;
1016 /* get total count of repos */
1017 for (d_i = 0; d_i < d_cnt; d_i++) {
1018 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1019 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1020 continue;
1022 if (asprintf(&c_path, "%s/%s", srv->repos_path,
1023 sd_dent[d_i]->d_name) == -1) {
1024 error = got_error_from_errno("asprintf");
1025 return error;
1028 if (lstat(c_path, &st) == 0 && S_ISDIR(st.st_mode) &&
1029 !got_path_dir_is_empty(c_path))
1030 t->repos_total++;
1031 free(c_path);
1032 c_path = NULL;
1035 r = fcgi_printf(c, "<div id='index_header'>\n"
1036 "<div id='index_header_project'>Project</div>\n");
1037 if (r == -1)
1038 goto done;
1040 if (srv->show_repo_description)
1041 if (fcgi_printf(c, "<div id='index_header_description'>"
1042 "Description</div>\n") == -1)
1043 goto done;
1044 if (srv->show_repo_owner)
1045 if (fcgi_printf(c, "<div id='index_header_owner'>"
1046 "Owner</div>\n") == -1)
1047 goto done;
1048 if (srv->show_repo_age)
1049 if (fcgi_printf(c, "<div id='index_header_age'>"
1050 "Last Change</div>\n") == -1)
1051 goto done;
1052 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1053 goto done;
1055 for (d_i = 0; d_i < d_cnt; d_i++) {
1056 if (srv->max_repos > 0 && (d_i - 2) == srv->max_repos)
1057 break; /* account for parent and self */
1059 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1060 strcmp(sd_dent[d_i]->d_name, "..") == 0)
1061 continue;
1063 if (qs->index_page > 0 && (qs->index_page *
1064 srv->max_repos_display) > t->prev_disp) {
1065 t->prev_disp++;
1066 continue;
1069 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1070 if (error)
1071 goto done;
1073 error = gotweb_load_got_path(c, repo_dir);
1074 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1075 error = NULL;
1076 continue;
1078 else if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1079 goto done;
1081 if (lstat(repo_dir->path, &st) == 0 &&
1082 S_ISDIR(st.st_mode) &&
1083 !got_path_dir_is_empty(repo_dir->path))
1084 goto render;
1085 else {
1086 gotweb_free_repo_dir(repo_dir);
1087 repo_dir = NULL;
1088 continue;
1090 render:
1091 d_disp++;
1092 t->prev_disp++;
1094 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1095 "<div class='index_project'>") == -1)
1096 goto done;
1098 r = gotweb_link(c, &(struct gotweb_url){
1099 .action = SUMMARY,
1100 .index_page = -1,
1101 .page = -1,
1102 .path = repo_dir->name,
1103 }, "%s", repo_dir->name);
1104 if (r == -1)
1105 goto done;
1107 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1108 goto done;
1110 if (srv->show_repo_description) {
1111 r = fcgi_printf(c,
1112 "<div class='index_project_description'>\n"
1113 "%s</div>\n", repo_dir->description);
1114 if (r == -1)
1115 goto done;
1118 if (srv->show_repo_owner) {
1119 r = fcgi_printf(c, "<div class='index_project_owner'>"
1120 "%s</div>\n", repo_dir->owner);
1121 if (r == -1)
1122 goto done;
1125 if (srv->show_repo_age) {
1126 r = fcgi_printf(c, "<div class='index_project_age'>"
1127 "%s</div>\n", repo_dir->age);
1128 if (r == -1)
1129 goto done;
1132 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1133 "<div class='navs'>") == -1)
1134 goto done;
1136 r = gotweb_link(c, &(struct gotweb_url){
1137 .action = SUMMARY,
1138 .index_page = -1,
1139 .page = -1,
1140 .path = repo_dir->name
1141 }, "summary");
1142 if (r == -1)
1143 goto done;
1145 if (fcgi_printf(c, " | ") == -1)
1146 goto done;
1148 r = gotweb_link(c, &(struct gotweb_url){
1149 .action = BRIEFS,
1150 .index_page = -1,
1151 .page = -1,
1152 .path = repo_dir->name
1153 }, "commit briefs");
1154 if (r == -1)
1155 goto done;
1157 if (fcgi_printf(c, " | ") == -1)
1158 goto done;
1160 r = gotweb_link(c, &(struct gotweb_url){
1161 .action = COMMITS,
1162 .index_page = -1,
1163 .page = -1,
1164 .path = repo_dir->name
1165 }, "commits");
1166 if (r == -1)
1167 goto done;
1169 if (fcgi_printf(c, " | ") == -1)
1170 goto done;
1172 r = gotweb_link(c, &(struct gotweb_url){
1173 .action = TAGS,
1174 .index_page = -1,
1175 .page = -1,
1176 .path = repo_dir->name
1177 }, "tags");
1178 if (r == -1)
1179 goto done;
1181 if (fcgi_printf(c, " | ") == -1)
1182 goto done;
1184 r = gotweb_link(c, &(struct gotweb_url){
1185 .action = TREE,
1186 .index_page = -1,
1187 .page = -1,
1188 .path = repo_dir->name
1189 }, "tree");
1190 if (r == -1)
1191 goto done;
1193 r = fcgi_printf(c, "</div>" /* .navs */
1194 "<div class='dotted_line'></div>\n"
1195 "</div>\n" /* .navs_wrapper */
1196 "</div>\n"); /* .index_wrapper */
1197 if (r == -1)
1198 goto done;
1200 gotweb_free_repo_dir(repo_dir);
1201 repo_dir = NULL;
1202 t->next_disp++;
1203 if (d_disp == srv->max_repos_display)
1204 break;
1206 if (srv->max_repos_display == 0)
1207 goto done;
1208 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1209 goto done;
1210 if (t->repos_total <= srv->max_repos ||
1211 t->repos_total <= srv->max_repos_display)
1212 goto done;
1214 error = gotweb_render_navs(c);
1215 if (error)
1216 goto done;
1217 done:
1218 if (sd_dent) {
1219 for (d_i = 0; d_i < d_cnt; d_i++)
1220 free(sd_dent[d_i]);
1221 free(sd_dent);
1223 if (d != NULL && closedir(d) == EOF && error == NULL)
1224 error = got_error_from_errno("closedir");
1225 return error;
1228 static const struct got_error *
1229 gotweb_render_blame(struct request *c)
1231 const struct got_error *error = NULL;
1232 struct transport *t = c->t;
1233 struct repo_commit *rc = NULL;
1234 char *age = NULL, *msg = NULL;
1235 int r;
1237 error = got_get_repo_commits(c, 1);
1238 if (error)
1239 return error;
1241 rc = TAILQ_FIRST(&t->repo_commits);
1243 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1244 if (error)
1245 goto done;
1246 error = gotweb_escape_html(&msg, rc->commit_msg);
1247 if (error)
1248 goto done;
1250 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1251 "<div id='blame_title'>Blame</div>\n"
1252 "</div>\n" /* #blame_title_wrapper */
1253 "<div id='blame_content'>\n"
1254 "<div id='blame_header_wrapper'>\n"
1255 "<div id='blame_header'>\n"
1256 "<div class='header_age_title'>Date:</div>\n"
1257 "<div class='header_age'>%s</div>\n"
1258 "<div id='header_commit_msg_title'>Message:</div>\n"
1259 "<div id='header_commit_msg'>%s</div>\n"
1260 "</div>\n" /* #blame_header */
1261 "</div>\n" /* #blame_header_wrapper */
1262 "<div class='dotted_line'></div>\n"
1263 "<div id='blame'>\n",
1264 age,
1265 msg);
1266 if (r == -1)
1267 goto done;
1269 error = got_output_file_blame(c);
1270 if (error)
1271 goto done;
1273 fcgi_printf(c, "</div>\n" /* #blame */
1274 "</div>\n"); /* #blame_content */
1275 done:
1276 free(age);
1277 free(msg);
1278 return error;
1281 static const struct got_error *
1282 gotweb_render_briefs(struct request *c)
1284 const struct got_error *error = NULL;
1285 struct repo_commit *rc = NULL;
1286 struct server *srv = c->srv;
1287 struct transport *t = c->t;
1288 struct querystring *qs = t->qs;
1289 struct repo_dir *repo_dir = t->repo_dir;
1290 char *smallerthan, *newline;
1291 char *age = NULL, *author = NULL, *msg = NULL;
1292 int r;
1294 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1295 "<div id='briefs_title'>Commit Briefs</div>\n"
1296 "</div>\n" /* #briefs_title_wrapper */
1297 "<div id='briefs_content'>\n");
1298 if (r == -1)
1299 goto done;
1301 if (qs->action == SUMMARY) {
1302 qs->action = BRIEFS;
1303 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1304 } else
1305 error = got_get_repo_commits(c, srv->max_commits_display);
1306 if (error)
1307 goto done;
1309 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1310 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1311 if (error)
1312 goto done;
1314 smallerthan = strchr(rc->author, '<');
1315 if (smallerthan)
1316 *smallerthan = '\0';
1318 newline = strchr(rc->commit_msg, '\n');
1319 if (newline)
1320 *newline = '\0';
1322 error = gotweb_escape_html(&author, rc->author);
1323 if (error)
1324 goto done;
1325 error = gotweb_escape_html(&msg, rc->commit_msg);
1326 if (error)
1327 goto done;
1329 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1330 "<div class='briefs_author'>%s</div>\n"
1331 "<div class='briefs_log'>",
1332 age, author);
1333 if (r == -1)
1334 goto done;
1336 r = gotweb_link(c, &(struct gotweb_url){
1337 .action = DIFF,
1338 .index_page = -1,
1339 .page = -1,
1340 .path = repo_dir->name,
1341 .commit = rc->commit_id,
1342 .headref = qs->headref,
1343 }, "%s", msg);
1344 if (r == -1)
1345 goto done;
1347 if (rc->refs_str) {
1348 char *refs;
1350 error = gotweb_escape_html(&refs, rc->refs_str);
1351 if (error)
1352 goto done;
1353 r = fcgi_printf(c,
1354 " <span class='refs_str'>(%s)</span>", refs);
1355 free(refs);
1356 if (r == -1)
1357 goto done;
1359 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1360 goto done;
1362 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1363 "<div class='navs'>");
1364 if (r == -1)
1365 goto done;
1367 r = gotweb_link(c, &(struct gotweb_url){
1368 .action = DIFF,
1369 .index_page = -1,
1370 .page = -1,
1371 .path = repo_dir->name,
1372 .commit = rc->commit_id,
1373 .headref = qs->headref,
1374 }, "diff");
1375 if (r == -1)
1376 goto done;
1378 if (fcgi_printf(c, " | ") == -1)
1379 goto done;
1381 r = gotweb_link(c, &(struct gotweb_url){
1382 .action = TREE,
1383 .index_page = -1,
1384 .page = -1,
1385 .path = repo_dir->name,
1386 .commit = rc->commit_id,
1387 .headref = qs->headref,
1388 }, "tree");
1389 if (r == -1)
1390 goto done;
1392 if (fcgi_printf(c, "</div>\n" /* .navs */
1393 "</div>\n" /* .navs_wrapper */
1394 "<div class='dotted_line'></div>\n") == -1)
1395 goto done;
1397 free(age);
1398 age = NULL;
1399 free(author);
1400 author = NULL;
1401 free(msg);
1402 msg = NULL;
1405 if (t->next_id || t->prev_id) {
1406 error = gotweb_render_navs(c);
1407 if (error)
1408 goto done;
1410 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1411 done:
1412 free(age);
1413 free(author);
1414 free(msg);
1415 return error;
1418 static const struct got_error *
1419 gotweb_render_commits(struct request *c)
1421 const struct got_error *error = NULL;
1422 struct repo_commit *rc = NULL;
1423 struct server *srv = c->srv;
1424 struct transport *t = c->t;
1425 struct repo_dir *repo_dir = t->repo_dir;
1426 char *age = NULL, *author = NULL, *msg = NULL;
1427 int r;
1429 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1430 "<div class='commits_title'>Commits</div>\n"
1431 "</div>\n" /* .commits_title_wrapper */
1432 "<div class='commits_content'>\n");
1433 if (r == -1)
1434 goto done;
1436 error = got_get_repo_commits(c, srv->max_commits_display);
1437 if (error)
1438 goto done;
1440 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1441 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1442 if (error)
1443 goto done;
1444 error = gotweb_escape_html(&author, rc->author);
1445 if (error)
1446 goto done;
1447 error = gotweb_escape_html(&msg, rc->commit_msg);
1448 if (error)
1449 goto done;
1451 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1452 "<div class='commits_header'>\n"
1453 "<div class='header_commit_title'>Commit:</div>\n"
1454 "<div class='header_commit'>%s</div>\n"
1455 "<div class='header_author_title'>Author:</div>\n"
1456 "<div class='header_author'>%s</div>\n"
1457 "<div class='header_age_title'>Date:</div>\n"
1458 "<div class='header_age'>%s</div>\n"
1459 "</div>\n" /* .commits_header */
1460 "</div>\n" /* .commits_header_wrapper */
1461 "<div class='dotted_line'></div>\n"
1462 "<div class='commit'>\n%s</div>\n",
1463 rc->commit_id,
1464 author,
1465 age,
1466 msg);
1467 if (r == -1)
1468 goto done;
1470 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1471 "<div class='navs'>") == -1)
1472 goto done;
1474 r = gotweb_link(c, &(struct gotweb_url){
1475 .action = DIFF,
1476 .index_page = -1,
1477 .page = -1,
1478 .path = repo_dir->name,
1479 .commit = rc->commit_id,
1480 }, "diff");
1481 if (r == -1)
1482 goto done;
1484 if (fcgi_printf(c, " | ") == -1)
1485 goto done;
1487 r = gotweb_link(c, &(struct gotweb_url){
1488 .action = TREE,
1489 .index_page = -1,
1490 .page = -1,
1491 .path = repo_dir->name,
1492 .commit = rc->commit_id,
1493 }, "tree");
1494 if (r == -1)
1495 goto done;
1497 if (fcgi_printf(c, "</div>\n" /* .navs */
1498 "</div>\n" /* .navs_wrapper */
1499 "<div class='dotted_line'></div>\n") == -1)
1500 goto done;
1502 free(age);
1503 age = NULL;
1504 free(author);
1505 author = NULL;
1506 free(msg);
1507 msg = NULL;
1510 if (t->next_id || t->prev_id) {
1511 error = gotweb_render_navs(c);
1512 if (error)
1513 goto done;
1515 fcgi_printf(c, "</div>\n"); /* .commits_content */
1516 done:
1517 free(age);
1518 free(author);
1519 free(msg);
1520 return error;
1523 static const struct got_error *
1524 gotweb_render_branches(struct request *c)
1526 const struct got_error *error = NULL;
1527 struct got_reflist_head refs;
1528 struct got_reflist_entry *re;
1529 struct transport *t = c->t;
1530 struct querystring *qs = t->qs;
1531 struct got_repository *repo = t->repo;
1532 char *escaped_refname = NULL;
1533 char *age = NULL;
1534 int r;
1536 TAILQ_INIT(&refs);
1538 error = got_ref_list(&refs, repo, "refs/heads",
1539 got_ref_cmp_by_name, NULL);
1540 if (error)
1541 goto done;
1543 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1544 "<div id='branches_title'>Branches</div>\n"
1545 "</div>\n" /* #branches_title_wrapper */
1546 "<div id='branches_content'>\n");
1547 if (r == -1)
1548 goto done;
1550 TAILQ_FOREACH(re, &refs, entry) {
1551 const char *refname = NULL;
1553 if (got_ref_is_symbolic(re->ref))
1554 continue;
1556 refname = got_ref_get_name(re->ref);
1557 if (refname == NULL) {
1558 error = got_error_from_errno("strdup");
1559 goto done;
1561 if (strncmp(refname, "refs/heads/", 11) != 0)
1562 continue;
1564 error = got_get_repo_age(&age, c, qs->path, refname,
1565 TM_DIFF);
1566 if (error)
1567 goto done;
1569 if (strncmp(refname, "refs/heads/", 11) == 0)
1570 refname += 11;
1571 error = gotweb_escape_html(&escaped_refname, refname);
1572 if (error)
1573 goto done;
1575 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1576 "<div class='branches_age'>%s</div>\n"
1577 "<div class='branches_space'>&nbsp;</div>\n"
1578 "<div class='branch'>", age);
1579 if (r == -1)
1580 goto done;
1582 r = gotweb_link(c, &(struct gotweb_url){
1583 .action = SUMMARY,
1584 .index_page = -1,
1585 .page = -1,
1586 .path = qs->path,
1587 .headref = refname,
1588 }, "%s", escaped_refname);
1589 if (r == -1)
1590 goto done;
1592 if (fcgi_printf(c, "</div>\n" /* .branch */
1593 "<div class='navs_wrapper'>\n"
1594 "<div class='navs'>") == -1)
1595 goto done;
1597 r = gotweb_link(c, &(struct gotweb_url){
1598 .action = SUMMARY,
1599 .index_page = -1,
1600 .page = -1,
1601 .path = qs->path,
1602 .headref = refname,
1603 }, "summary");
1604 if (r == -1)
1605 goto done;
1607 if (fcgi_printf(c, " | ") == -1)
1608 goto done;
1610 r = gotweb_link(c, &(struct gotweb_url){
1611 .action = BRIEFS,
1612 .index_page = -1,
1613 .page = -1,
1614 .path = qs->path,
1615 .headref = refname,
1616 }, "commit briefs");
1617 if (r == -1)
1618 goto done;
1620 if (fcgi_printf(c, " | ") == -1)
1621 goto done;
1623 r = gotweb_link(c, &(struct gotweb_url){
1624 .action = COMMITS,
1625 .index_page = -1,
1626 .page = -1,
1627 .path = qs->path,
1628 .headref = refname,
1629 }, "commits");
1630 if (r == -1)
1631 goto done;
1633 r = fcgi_printf(c, "</div>\n" /* .navs */
1634 "</div>\n" /* .navs_wrapper */
1635 "<div class='dotted_line'></div>\n"
1636 "</div>\n"); /* .branches_wrapper */
1637 if (r == -1)
1638 goto done;
1640 free(age);
1641 age = NULL;
1642 free(escaped_refname);
1643 escaped_refname = NULL;
1645 fcgi_printf(c, "</div>\n"); /* #branches_content */
1646 done:
1647 free(age);
1648 free(escaped_refname);
1649 got_ref_list_free(&refs);
1650 return error;
1653 static const struct got_error *
1654 gotweb_render_tree(struct request *c)
1656 const struct got_error *error = NULL;
1657 struct transport *t = c->t;
1658 struct repo_commit *rc = NULL;
1659 char *age = NULL, *msg = NULL;
1660 int r;
1662 error = got_get_repo_commits(c, 1);
1663 if (error)
1664 return error;
1666 rc = TAILQ_FIRST(&t->repo_commits);
1668 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1669 if (error)
1670 goto done;
1672 error = gotweb_escape_html(&msg, rc->commit_msg);
1673 if (error)
1674 goto done;
1676 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1677 "<div id='tree_title'>Tree</div>\n"
1678 "</div>\n" /* #tree_title_wrapper */
1679 "<div id='tree_content'>\n"
1680 "<div id='tree_header_wrapper'>\n"
1681 "<div id='tree_header'>\n"
1682 "<div id='header_tree_title'>Tree:</div>\n"
1683 "<div id='header_tree'>%s</div>\n"
1684 "<div class='header_age_title'>Date:</div>\n"
1685 "<div class='header_age'>%s</div>\n"
1686 "<div id='header_commit_msg_title'>Message:</div>\n"
1687 "<div id='header_commit_msg'>%s</div>\n"
1688 "</div>\n" /* #tree_header */
1689 "</div>\n" /* #tree_header_wrapper */
1690 "<div class='dotted_line'></div>\n"
1691 "<div id='tree'>\n",
1692 rc->tree_id,
1693 age,
1694 msg);
1695 if (r == -1)
1696 goto done;
1698 error = got_output_repo_tree(c);
1699 if (error)
1700 goto done;
1702 fcgi_printf(c, "</div>\n"); /* #tree */
1703 fcgi_printf(c, "</div>\n"); /* #tree_content */
1704 done:
1705 free(age);
1706 free(msg);
1707 return error;
1710 static const struct got_error *
1711 gotweb_render_diff(struct request *c)
1713 const struct got_error *error = NULL;
1714 struct transport *t = c->t;
1715 struct repo_commit *rc = NULL;
1716 char *age = NULL, *author = NULL, *msg = NULL;
1717 int r;
1719 error = got_get_repo_commits(c, 1);
1720 if (error)
1721 return error;
1723 rc = TAILQ_FIRST(&t->repo_commits);
1725 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1726 if (error)
1727 goto done;
1728 error = gotweb_escape_html(&author, rc->author);
1729 if (error)
1730 goto done;
1731 error = gotweb_escape_html(&msg, rc->commit_msg);
1732 if (error)
1733 goto done;
1735 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1736 "<div id='diff_title'>Commit Diff</div>\n"
1737 "</div>\n" /* #diff_title_wrapper */
1738 "<div id='diff_content'>\n"
1739 "<div id='diff_header_wrapper'>\n"
1740 "<div id='diff_header'>\n"
1741 "<div id='header_diff_title'>Diff:</div>\n"
1742 "<div id='header_diff'>%s<br />%s</div>\n"
1743 "<div class='header_commit_title'>Commit:</div>\n"
1744 "<div class='header_commit'>%s</div>\n"
1745 "<div id='header_tree_title'>Tree:</div>\n"
1746 "<div id='header_tree'>%s</div>\n"
1747 "<div class='header_author_title'>Author:</div>\n"
1748 "<div class='header_author'>%s</div>\n"
1749 "<div class='header_age_title'>Date:</div>\n"
1750 "<div class='header_age'>%s</div>\n"
1751 "<div id='header_commit_msg_title'>Message:</div>\n"
1752 "<div id='header_commit_msg'>%s</div>\n"
1753 "</div>\n" /* #diff_header */
1754 "</div>\n" /* #diff_header_wrapper */
1755 "<div class='dotted_line'></div>\n"
1756 "<div id='diff'>\n",
1757 rc->parent_id, rc->commit_id,
1758 rc->commit_id,
1759 rc->tree_id,
1760 author,
1761 age,
1762 msg);
1763 if (r == -1)
1764 goto done;
1766 error = got_output_repo_diff(c);
1767 if (error)
1768 goto done;
1770 fcgi_printf(c, "</div>\n"); /* #diff */
1771 fcgi_printf(c, "</div>\n"); /* #diff_content */
1772 done:
1773 free(age);
1774 free(author);
1775 free(msg);
1776 return error;
1779 static const struct got_error *
1780 gotweb_render_summary(struct request *c)
1782 const struct got_error *error = NULL;
1783 struct transport *t = c->t;
1784 struct server *srv = c->srv;
1785 int r;
1787 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1788 goto done;
1790 if (srv->show_repo_description) {
1791 r = fcgi_printf(c,
1792 "<div id='description_title'>Description:</div>\n"
1793 "<div id='description'>%s</div>\n",
1794 t->repo_dir->description ? t->repo_dir->description : "");
1795 if (r == -1)
1796 goto done;
1799 if (srv->show_repo_owner) {
1800 r = fcgi_printf(c,
1801 "<div id='repo_owner_title'>Owner:</div>\n"
1802 "<div id='repo_owner'>%s</div>\n",
1803 t->repo_dir->owner ? t->repo_dir->owner : "");
1804 if (r == -1)
1805 goto done;
1808 if (srv->show_repo_age) {
1809 r = fcgi_printf(c,
1810 "<div id='last_change_title'>Last Change:</div>\n"
1811 "<div id='last_change'>%s</div>\n",
1812 t->repo_dir->age);
1813 if (r == -1)
1814 goto done;
1817 if (srv->show_repo_cloneurl) {
1818 r = fcgi_printf(c,
1819 "<div id='cloneurl_title'>Clone URL:</div>\n"
1820 "<div id='cloneurl'>%s</div>\n",
1821 t->repo_dir->url ? t->repo_dir->url : "");
1822 if (r == -1)
1823 goto done;
1826 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1827 if (r == -1)
1828 goto done;
1830 error = gotweb_render_briefs(c);
1831 if (error) {
1832 log_warnx("%s: %s", __func__, error->msg);
1833 goto done;
1836 error = gotweb_render_tags(c);
1837 if (error) {
1838 log_warnx("%s: %s", __func__, error->msg);
1839 goto done;
1842 error = gotweb_render_branches(c);
1843 if (error)
1844 log_warnx("%s: %s", __func__, error->msg);
1845 done:
1846 return error;
1849 static const struct got_error *
1850 gotweb_render_tag(struct request *c)
1852 const struct got_error *error = NULL;
1853 struct repo_tag *rt = NULL;
1854 struct transport *t = c->t;
1855 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1857 error = got_get_repo_tags(c, 1);
1858 if (error)
1859 goto done;
1861 if (t->tag_count == 0) {
1862 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1863 "bad commit id");
1864 goto done;
1867 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1869 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1870 if (error)
1871 goto done;
1872 error = gotweb_escape_html(&author, rt->tagger);
1873 if (error)
1874 goto done;
1875 error = gotweb_escape_html(&msg, rt->commit_msg);
1876 if (error)
1877 goto done;
1879 tagname = rt->tag_name;
1880 if (strncmp(tagname, "refs/", 5) == 0)
1881 tagname += 5;
1882 error = gotweb_escape_html(&tagname, tagname);
1883 if (error)
1884 goto done;
1886 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1887 "<div id='tags_title'>Tag</div>\n"
1888 "</div>\n" /* #tags_title_wrapper */
1889 "<div id='tags_content'>\n"
1890 "<div id='tag_header_wrapper'>\n"
1891 "<div id='tag_header'>\n"
1892 "<div class='header_commit_title'>Commit:</div>\n"
1893 "<div class='header_commit'>%s"
1894 " <span class='refs_str'>(%s)</span></div>\n"
1895 "<div class='header_author_title'>Tagger:</div>\n"
1896 "<div class='header_author'>%s</div>\n"
1897 "<div class='header_age_title'>Date:</div>\n"
1898 "<div class='header_age'>%s</div>\n"
1899 "<div id='header_commit_msg_title'>Message:</div>\n"
1900 "<div id='header_commit_msg'>%s</div>\n"
1901 "</div>\n" /* #tag_header */
1902 "<div class='dotted_line'></div>\n"
1903 "<div id='tag_commit'>\n%s</div>"
1904 "</div>" /* #tag_header_wrapper */
1905 "</div>", /* #tags_content */
1906 rt->commit_id,
1907 tagname,
1908 author,
1909 age,
1910 msg,
1911 rt->tag_commit);
1913 done:
1914 free(age);
1915 free(author);
1916 free(msg);
1917 return error;
1920 static const struct got_error *
1921 gotweb_render_tags(struct request *c)
1923 const struct got_error *error = NULL;
1924 struct repo_tag *rt = NULL;
1925 struct server *srv = c->srv;
1926 struct transport *t = c->t;
1927 struct querystring *qs = t->qs;
1928 struct repo_dir *repo_dir = t->repo_dir;
1929 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1930 int r, commit_found = 0;
1932 if (qs->action == BRIEFS) {
1933 qs->action = TAGS;
1934 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1935 } else
1936 error = got_get_repo_tags(c, srv->max_commits_display);
1937 if (error)
1938 goto done;
1940 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1941 "<div id='tags_title'>Tags</div>\n"
1942 "</div>\n" /* #tags_title_wrapper */
1943 "<div id='tags_content'>\n");
1944 if (r == -1)
1945 goto done;
1947 if (t->tag_count == 0) {
1948 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1949 "This repository contains no tags");
1950 if (r == -1)
1951 goto done;
1954 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1955 if (commit_found == 0 && qs->commit != NULL) {
1956 if (strcmp(qs->commit, rt->commit_id) != 0)
1957 continue;
1958 else
1959 commit_found = 1;
1961 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1962 if (error)
1963 goto done;
1965 tagname = rt->tag_name;
1966 if (strncmp(tagname, "refs/tags/", 10) == 0)
1967 tagname += 10;
1968 error = gotweb_escape_html(&tagname, tagname);
1969 if (error)
1970 goto done;
1972 if (rt->tag_commit != NULL) {
1973 newline = strchr(rt->tag_commit, '\n');
1974 if (newline)
1975 *newline = '\0';
1976 error = gotweb_escape_html(&msg, rt->tag_commit);
1977 if (error)
1978 goto done;
1981 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1982 "<div class='tag'>%s</div>\n"
1983 "<div class='tag_log'>", age, tagname) == -1)
1984 goto done;
1986 r = gotweb_link(c, &(struct gotweb_url){
1987 .action = TAG,
1988 .index_page = -1,
1989 .page = -1,
1990 .path = repo_dir->name,
1991 .commit = rt->commit_id,
1992 }, "%s", msg ? msg : "");
1993 if (r == -1)
1994 goto done;
1996 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1997 "<div class='navs_wrapper'>\n"
1998 "<div class='navs'>") == -1)
1999 goto done;
2001 r = gotweb_link(c, &(struct gotweb_url){
2002 .action = TAG,
2003 .index_page = -1,
2004 .page = -1,
2005 .path = repo_dir->name,
2006 .commit = rt->commit_id,
2007 }, "tag");
2008 if (r == -1)
2009 goto done;
2011 if (fcgi_printf(c, " | ") == -1)
2012 goto done;
2014 r = gotweb_link(c, &(struct gotweb_url){
2015 .action = BRIEFS,
2016 .index_page = -1,
2017 .page = -1,
2018 .path = repo_dir->name,
2019 .commit = rt->commit_id,
2020 }, "commit briefs");
2021 if (r == -1)
2022 goto done;
2024 if (fcgi_printf(c, " | ") == -1)
2025 goto done;
2027 r = gotweb_link(c, &(struct gotweb_url){
2028 .action = COMMITS,
2029 .index_page = -1,
2030 .page = -1,
2031 .path = repo_dir->name,
2032 .commit = rt->commit_id,
2033 }, "commits");
2034 if (r == -1)
2035 goto done;
2037 r = fcgi_printf(c,
2038 "</div>\n" /* .navs */
2039 "</div>\n" /* .navs_wrapper */
2040 "<div class='dotted_line'></div>\n");
2041 if (r == -1)
2042 goto done;
2044 free(age);
2045 age = NULL;
2046 free(tagname);
2047 tagname = NULL;
2048 free(msg);
2049 msg = NULL;
2051 if (t->next_id || t->prev_id) {
2052 error = gotweb_render_navs(c);
2053 if (error)
2054 goto done;
2056 fcgi_printf(c, "</div>\n"); /* #tags_content */
2057 done:
2058 free(age);
2059 free(tagname);
2060 free(msg);
2061 return error;
2064 const struct got_error *
2065 gotweb_escape_html(char **escaped_html, const char *orig_html)
2067 const struct got_error *error = NULL;
2068 struct escape_pair {
2069 char c;
2070 const char *s;
2071 } esc[] = {
2072 { '>', "&gt;" },
2073 { '<', "&lt;" },
2074 { '&', "&amp;" },
2075 { '"', "&quot;" },
2076 { '\'', "&apos;" },
2077 { '\n', "<br />" },
2079 size_t orig_len, len;
2080 int i, j, x;
2082 orig_len = strlen(orig_html);
2083 len = orig_len;
2084 for (i = 0; i < orig_len; i++) {
2085 for (j = 0; j < nitems(esc); j++) {
2086 if (orig_html[i] != esc[j].c)
2087 continue;
2088 len += strlen(esc[j].s) - 1 /* escaped char */;
2092 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2093 if (*escaped_html == NULL)
2094 return got_error_from_errno("calloc");
2096 x = 0;
2097 for (i = 0; i < orig_len; i++) {
2098 int escaped = 0;
2099 for (j = 0; j < nitems(esc); j++) {
2100 if (orig_html[i] != esc[j].c)
2101 continue;
2103 if (strlcat(*escaped_html, esc[j].s, len + 1)
2104 >= len + 1) {
2105 error = got_error(GOT_ERR_NO_SPACE);
2106 goto done;
2108 x += strlen(esc[j].s);
2109 escaped = 1;
2110 break;
2112 if (!escaped) {
2113 (*escaped_html)[x] = orig_html[i];
2114 x++;
2117 done:
2118 if (error) {
2119 free(*escaped_html);
2120 *escaped_html = NULL;
2121 } else {
2122 (*escaped_html)[x] = '\0';
2125 return error;
2128 static inline int
2129 should_urlencode(int c)
2131 if (c <= ' ' || c >= 127)
2132 return 1;
2134 switch (c) {
2135 /* gen-delim */
2136 case ':':
2137 case '/':
2138 case '?':
2139 case '#':
2140 case '[':
2141 case ']':
2142 case '@':
2143 /* sub-delims */
2144 case '!':
2145 case '$':
2146 case '&':
2147 case '\'':
2148 case '(':
2149 case ')':
2150 case '*':
2151 case '+':
2152 case ',':
2153 case ';':
2154 case '=':
2155 return 1;
2156 default:
2157 return 0;
2161 static char *
2162 gotweb_urlencode(const char *str)
2164 const char *s;
2165 char *escaped;
2166 size_t i, len;
2167 int a, b;
2169 len = 0;
2170 for (s = str; *s; ++s) {
2171 len++;
2172 if (should_urlencode(*s))
2173 len += 2;
2176 escaped = calloc(1, len + 1);
2177 if (escaped == NULL)
2178 return NULL;
2180 i = 0;
2181 for (s = str; *s; ++s) {
2182 if (should_urlencode(*s)) {
2183 a = (*s & 0xF0) >> 4;
2184 b = (*s & 0x0F);
2186 escaped[i++] = '%';
2187 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2188 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2189 } else
2190 escaped[i++] = *s;
2193 return escaped;
2196 static inline const char *
2197 action_name(int action)
2199 switch (action) {
2200 case BLAME:
2201 return "blame";
2202 case BLOB:
2203 return "blob";
2204 case BRIEFS:
2205 return "briefs";
2206 case COMMITS:
2207 return "commits";
2208 case DIFF:
2209 return "diff";
2210 case ERR:
2211 return "err";
2212 case INDEX:
2213 return "index";
2214 case SUMMARY:
2215 return "summary";
2216 case TAG:
2217 return "tag";
2218 case TAGS:
2219 return "tags";
2220 case TREE:
2221 return "tree";
2222 default:
2223 return NULL;
2227 static int
2228 gotweb_print_url(struct request *c, struct gotweb_url *url)
2230 const char *sep = "?", *action;
2231 char *tmp;
2232 int r;
2234 action = action_name(url->action);
2235 if (action != NULL) {
2236 if (fcgi_printf(c, "?action=%s", action) == -1)
2237 return -1;
2238 sep = "&";
2241 if (url->commit) {
2242 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2243 return -1;
2244 sep = "&";
2247 if (url->previd) {
2248 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2249 return -1;
2250 sep = "&";
2253 if (url->prevset) {
2254 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2255 return -1;
2256 sep = "&";
2259 if (url->file) {
2260 tmp = gotweb_urlencode(url->file);
2261 if (tmp == NULL)
2262 return -1;
2263 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2264 free(tmp);
2265 if (r == -1)
2266 return -1;
2267 sep = "&";
2270 if (url->folder) {
2271 tmp = gotweb_urlencode(url->folder);
2272 if (tmp == NULL)
2273 return -1;
2274 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2275 free(tmp);
2276 if (r == -1)
2277 return -1;
2278 sep = "&";
2281 if (url->headref) {
2282 tmp = gotweb_urlencode(url->headref);
2283 if (tmp == NULL)
2284 return -1;
2285 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2286 free(tmp);
2287 if (r == -1)
2288 return -1;
2289 sep = "&";
2292 if (url->index_page != -1) {
2293 if (fcgi_printf(c, "%sindex_page=%d", sep,
2294 url->index_page) == -1)
2295 return -1;
2296 sep = "&";
2299 if (url->path) {
2300 tmp = gotweb_urlencode(url->path);
2301 if (tmp == NULL)
2302 return -1;
2303 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2304 free(tmp);
2305 if (r == -1)
2306 return -1;
2307 sep = "&";
2310 if (url->page != -1) {
2311 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2312 return -1;
2313 sep = "&";
2316 return 0;
2319 int
2320 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2322 va_list ap;
2323 int r;
2325 if (fcgi_printf(c, "<a href='") == -1)
2326 return -1;
2328 if (gotweb_print_url(c, url) == -1)
2329 return -1;
2331 if (fcgi_printf(c, "'>") == -1)
2332 return -1;
2334 va_start(ap, fmt);
2335 r = fcgi_vprintf(c, fmt, ap);
2336 va_end(ap);
2337 if (r == -1)
2338 return -1;
2340 if (fcgi_printf(c, "</a>"))
2341 return -1;
2342 return 0;
2345 static struct got_repository *
2346 find_cached_repo(struct server *srv, const char *path)
2348 int i;
2350 for (i = 0; i < srv->ncached_repos; i++) {
2351 if (strcmp(srv->cached_repos[i].path, path) == 0)
2352 return srv->cached_repos[i].repo;
2355 return NULL;
2358 static const struct got_error *
2359 cache_repo(struct got_repository **new, struct server *srv,
2360 struct repo_dir *repo_dir, struct socket *sock)
2362 const struct got_error *error = NULL;
2363 struct got_repository *repo;
2364 struct cached_repo *cr;
2365 int evicted = 0;
2367 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2368 cr = &srv->cached_repos[srv->ncached_repos - 1];
2369 error = got_repo_close(cr->repo);
2370 memset(cr, 0, sizeof(*cr));
2371 srv->ncached_repos--;
2372 if (error)
2373 return error;
2374 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2375 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2376 cr = &srv->cached_repos[0];
2377 evicted = 1;
2378 } else {
2379 cr = &srv->cached_repos[srv->ncached_repos];
2382 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2383 if (error) {
2384 if (evicted) {
2385 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2386 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2388 return error;
2391 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2392 >= sizeof(cr->path)) {
2393 if (evicted) {
2394 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2395 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2397 return got_error(GOT_ERR_NO_SPACE);
2400 cr->repo = repo;
2401 srv->ncached_repos++;
2402 *new = repo;
2403 return NULL;
2406 static const struct got_error *
2407 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2409 const struct got_error *error = NULL;
2410 struct socket *sock = c->sock;
2411 struct server *srv = c->srv;
2412 struct transport *t = c->t;
2413 struct got_repository *repo = NULL;
2414 DIR *dt;
2415 char *dir_test;
2417 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2418 GOTWEB_GIT_DIR) == -1)
2419 return got_error_from_errno("asprintf");
2421 dt = opendir(dir_test);
2422 if (dt == NULL) {
2423 free(dir_test);
2424 } else {
2425 repo_dir->path = dir_test;
2426 dir_test = NULL;
2427 goto done;
2430 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2431 repo_dir->name) == -1)
2432 return got_error_from_errno("asprintf");
2434 dt = opendir(dir_test);
2435 if (dt == NULL) {
2436 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2437 goto err;
2438 } else {
2439 repo_dir->path = dir_test;
2440 dir_test = NULL;
2443 done:
2444 repo = find_cached_repo(srv, repo_dir->path);
2445 if (repo == NULL) {
2446 error = cache_repo(&repo, srv, repo_dir, sock);
2447 if (error)
2448 goto err;
2450 t->repo = repo;
2451 error = gotweb_get_repo_description(&repo_dir->description, srv,
2452 repo_dir->path);
2453 if (error)
2454 goto err;
2455 error = got_get_repo_owner(&repo_dir->owner, c, repo_dir->path);
2456 if (error)
2457 goto err;
2458 error = got_get_repo_age(&repo_dir->age, c, repo_dir->path,
2459 NULL, TM_DIFF);
2460 if (error)
2461 goto err;
2462 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2463 err:
2464 free(dir_test);
2465 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2466 error = got_error_from_errno("closedir");
2467 return error;
2470 static const struct got_error *
2471 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2473 const struct got_error *error;
2475 *repo_dir = calloc(1, sizeof(**repo_dir));
2476 if (*repo_dir == NULL)
2477 return got_error_from_errno("calloc");
2479 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2480 error = got_error_from_errno("asprintf");
2481 free(*repo_dir);
2482 *repo_dir = NULL;
2483 return error;
2485 (*repo_dir)->owner = NULL;
2486 (*repo_dir)->description = NULL;
2487 (*repo_dir)->url = NULL;
2488 (*repo_dir)->age = NULL;
2489 (*repo_dir)->path = NULL;
2491 return NULL;
2494 static const struct got_error *
2495 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2497 const struct got_error *error = NULL;
2498 FILE *f = NULL;
2499 char *d_file = NULL;
2500 unsigned int len;
2501 size_t n;
2503 *description = NULL;
2504 if (srv->show_repo_description == 0)
2505 return NULL;
2507 if (asprintf(&d_file, "%s/description", dir) == -1)
2508 return got_error_from_errno("asprintf");
2510 f = fopen(d_file, "r");
2511 if (f == NULL) {
2512 if (errno != ENOENT && errno != EACCES)
2513 error = got_error_from_errno2("fopen", d_file);
2514 goto done;
2517 if (fseek(f, 0, SEEK_END) == -1) {
2518 error = got_ferror(f, GOT_ERR_IO);
2519 goto done;
2521 len = ftell(f);
2522 if (len == -1) {
2523 error = got_ferror(f, GOT_ERR_IO);
2524 goto done;
2527 if (len == 0) {
2528 *description = strdup("");
2529 if (*description == NULL)
2530 error = got_error_from_errno("strdup");
2531 goto done;
2534 if (fseek(f, 0, SEEK_SET) == -1) {
2535 error = got_ferror(f, GOT_ERR_IO);
2536 goto done;
2538 *description = calloc(len + 1, sizeof(**description));
2539 if (*description == NULL) {
2540 error = got_error_from_errno("calloc");
2541 goto done;
2544 n = fread(*description, 1, len, f);
2545 if (n == 0 && ferror(f))
2546 error = got_ferror(f, GOT_ERR_IO);
2547 done:
2548 if (f != NULL && fclose(f) == EOF && error == NULL)
2549 error = got_error_from_errno("fclose");
2550 free(d_file);
2551 return error;
2554 static const struct got_error *
2555 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2557 const struct got_error *error = NULL;
2558 FILE *f;
2559 char *d_file = NULL;
2560 unsigned int len;
2561 size_t n;
2563 *url = NULL;
2565 if (srv->show_repo_cloneurl == 0)
2566 return NULL;
2568 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2569 return got_error_from_errno("asprintf");
2571 f = fopen(d_file, "r");
2572 if (f == NULL) {
2573 if (errno != ENOENT && errno != EACCES)
2574 error = got_error_from_errno2("fopen", d_file);
2575 goto done;
2578 if (fseek(f, 0, SEEK_END) == -1) {
2579 error = got_ferror(f, GOT_ERR_IO);
2580 goto done;
2582 len = ftell(f);
2583 if (len == -1) {
2584 error = got_ferror(f, GOT_ERR_IO);
2585 goto done;
2587 if (len == 0)
2588 goto done;
2590 if (fseek(f, 0, SEEK_SET) == -1) {
2591 error = got_ferror(f, GOT_ERR_IO);
2592 goto done;
2595 *url = calloc(len + 1, sizeof(**url));
2596 if (*url == NULL) {
2597 error = got_error_from_errno("calloc");
2598 goto done;
2601 n = fread(*url, 1, len, f);
2602 if (n == 0 && ferror(f))
2603 error = got_ferror(f, GOT_ERR_IO);
2604 done:
2605 if (f != NULL && fclose(f) == EOF && error == NULL)
2606 error = got_error_from_errno("fclose");
2607 free(d_file);
2608 return error;
2611 const struct got_error *
2612 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2614 struct tm tm;
2615 long long diff_time;
2616 const char *years = "years ago", *months = "months ago";
2617 const char *weeks = "weeks ago", *days = "days ago";
2618 const char *hours = "hours ago", *minutes = "minutes ago";
2619 const char *seconds = "seconds ago", *now = "right now";
2620 char *s;
2621 char datebuf[29];
2623 *repo_age = NULL;
2625 switch (ref_tm) {
2626 case TM_DIFF:
2627 diff_time = time(NULL) - committer_time;
2628 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2629 if (asprintf(repo_age, "%lld %s",
2630 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2631 return got_error_from_errno("asprintf");
2632 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2633 if (asprintf(repo_age, "%lld %s",
2634 (diff_time / 60 / 60 / 24 / (365 / 12)),
2635 months) == -1)
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2638 if (asprintf(repo_age, "%lld %s",
2639 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2640 return got_error_from_errno("asprintf");
2641 } else if (diff_time > 60 * 60 * 24 * 2) {
2642 if (asprintf(repo_age, "%lld %s",
2643 (diff_time / 60 / 60 / 24), days) == -1)
2644 return got_error_from_errno("asprintf");
2645 } else if (diff_time > 60 * 60 * 2) {
2646 if (asprintf(repo_age, "%lld %s",
2647 (diff_time / 60 / 60), hours) == -1)
2648 return got_error_from_errno("asprintf");
2649 } else if (diff_time > 60 * 2) {
2650 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2651 minutes) == -1)
2652 return got_error_from_errno("asprintf");
2653 } else if (diff_time > 2) {
2654 if (asprintf(repo_age, "%lld %s", diff_time,
2655 seconds) == -1)
2656 return got_error_from_errno("asprintf");
2657 } else {
2658 if (asprintf(repo_age, "%s", now) == -1)
2659 return got_error_from_errno("asprintf");
2661 break;
2662 case TM_LONG:
2663 if (gmtime_r(&committer_time, &tm) == NULL)
2664 return got_error_from_errno("gmtime_r");
2666 s = asctime_r(&tm, datebuf);
2667 if (s == NULL)
2668 return got_error_from_errno("asctime_r");
2670 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2671 return got_error_from_errno("asprintf");
2672 break;
2674 return NULL;