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 unsigned int d_cnt, d_i, d_disp = 0;
997 unsigned int d_skipped = 0;
998 int r, type;
1000 d = opendir(srv->repos_path);
1001 if (d == NULL) {
1002 error = got_error_from_errno2("opendir", srv->repos_path);
1003 return error;
1006 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
1007 if (d_cnt == -1) {
1008 sd_dent = NULL;
1009 error = got_error_from_errno2("scandir", srv->repos_path);
1010 goto done;
1013 r = fcgi_printf(c, "<div id='index_header'>\n"
1014 "<div id='index_header_project'>Project</div>\n");
1015 if (r == -1)
1016 goto done;
1018 if (srv->show_repo_description)
1019 if (fcgi_printf(c, "<div id='index_header_description'>"
1020 "Description</div>\n") == -1)
1021 goto done;
1022 if (srv->show_repo_owner)
1023 if (fcgi_printf(c, "<div id='index_header_owner'>"
1024 "Owner</div>\n") == -1)
1025 goto done;
1026 if (srv->show_repo_age)
1027 if (fcgi_printf(c, "<div id='index_header_age'>"
1028 "Last Change</div>\n") == -1)
1029 goto done;
1030 if (fcgi_printf(c, "</div>\n") == -1) /* #index_header */
1031 goto done;
1033 for (d_i = 0; d_i < d_cnt; d_i++) {
1034 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
1035 break;
1037 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
1038 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
1039 d_skipped++;
1040 continue;
1043 error = got_path_dirent_type(&type, srv->repos_path,
1044 sd_dent[d_i]);
1045 if (error)
1046 goto done;
1047 if (type != DT_DIR) {
1048 d_skipped++;
1049 continue;
1052 if (qs->index_page > 0 && (qs->index_page *
1053 srv->max_repos_display) > t->prev_disp) {
1054 t->prev_disp++;
1055 continue;
1058 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
1059 if (error)
1060 goto done;
1062 error = gotweb_load_got_path(c, repo_dir);
1063 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
1064 error = NULL;
1065 gotweb_free_repo_dir(repo_dir);
1066 repo_dir = NULL;
1067 d_skipped++;
1068 continue;
1070 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
1071 goto done;
1073 d_disp++;
1074 t->prev_disp++;
1076 if (fcgi_printf(c, "<div class='index_wrapper'>\n"
1077 "<div class='index_project'>") == -1)
1078 goto done;
1080 r = gotweb_link(c, &(struct gotweb_url){
1081 .action = SUMMARY,
1082 .index_page = -1,
1083 .page = -1,
1084 .path = repo_dir->name,
1085 }, "%s", repo_dir->name);
1086 if (r == -1)
1087 goto done;
1089 if (fcgi_printf(c, "</div>") == -1) /* .index_project */
1090 goto done;
1092 if (srv->show_repo_description) {
1093 r = fcgi_printf(c,
1094 "<div class='index_project_description'>\n"
1095 "%s</div>\n", repo_dir->description);
1096 if (r == -1)
1097 goto done;
1100 if (srv->show_repo_owner) {
1101 r = fcgi_printf(c, "<div class='index_project_owner'>"
1102 "%s</div>\n", repo_dir->owner);
1103 if (r == -1)
1104 goto done;
1107 if (srv->show_repo_age) {
1108 r = fcgi_printf(c, "<div class='index_project_age'>"
1109 "%s</div>\n", repo_dir->age);
1110 if (r == -1)
1111 goto done;
1114 if (fcgi_printf(c, "<div class='navs_wrapper'>"
1115 "<div class='navs'>") == -1)
1116 goto done;
1118 r = gotweb_link(c, &(struct gotweb_url){
1119 .action = SUMMARY,
1120 .index_page = -1,
1121 .page = -1,
1122 .path = repo_dir->name
1123 }, "summary");
1124 if (r == -1)
1125 goto done;
1127 if (fcgi_printf(c, " | ") == -1)
1128 goto done;
1130 r = gotweb_link(c, &(struct gotweb_url){
1131 .action = BRIEFS,
1132 .index_page = -1,
1133 .page = -1,
1134 .path = repo_dir->name
1135 }, "commit briefs");
1136 if (r == -1)
1137 goto done;
1139 if (fcgi_printf(c, " | ") == -1)
1140 goto done;
1142 r = gotweb_link(c, &(struct gotweb_url){
1143 .action = COMMITS,
1144 .index_page = -1,
1145 .page = -1,
1146 .path = repo_dir->name
1147 }, "commits");
1148 if (r == -1)
1149 goto done;
1151 if (fcgi_printf(c, " | ") == -1)
1152 goto done;
1154 r = gotweb_link(c, &(struct gotweb_url){
1155 .action = TAGS,
1156 .index_page = -1,
1157 .page = -1,
1158 .path = repo_dir->name
1159 }, "tags");
1160 if (r == -1)
1161 goto done;
1163 if (fcgi_printf(c, " | ") == -1)
1164 goto done;
1166 r = gotweb_link(c, &(struct gotweb_url){
1167 .action = TREE,
1168 .index_page = -1,
1169 .page = -1,
1170 .path = repo_dir->name
1171 }, "tree");
1172 if (r == -1)
1173 goto done;
1175 r = fcgi_printf(c, "</div>" /* .navs */
1176 "<div class='dotted_line'></div>\n"
1177 "</div>\n" /* .navs_wrapper */
1178 "</div>\n"); /* .index_wrapper */
1179 if (r == -1)
1180 goto done;
1182 gotweb_free_repo_dir(repo_dir);
1183 repo_dir = NULL;
1184 t->next_disp++;
1185 if (d_disp == srv->max_repos_display)
1186 break;
1188 t->repos_total = d_cnt - d_skipped;
1190 if (srv->max_repos_display == 0)
1191 goto done;
1192 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
1193 goto done;
1194 if (t->repos_total <= srv->max_repos ||
1195 t->repos_total <= srv->max_repos_display)
1196 goto done;
1198 error = gotweb_render_navs(c);
1199 if (error)
1200 goto done;
1201 done:
1202 if (sd_dent) {
1203 for (d_i = 0; d_i < d_cnt; d_i++)
1204 free(sd_dent[d_i]);
1205 free(sd_dent);
1207 if (d != NULL && closedir(d) == EOF && error == NULL)
1208 error = got_error_from_errno("closedir");
1209 return error;
1212 static const struct got_error *
1213 gotweb_render_blame(struct request *c)
1215 const struct got_error *error = NULL;
1216 struct transport *t = c->t;
1217 struct repo_commit *rc = NULL;
1218 char *age = NULL, *msg = NULL;
1219 int r;
1221 error = got_get_repo_commits(c, 1);
1222 if (error)
1223 return error;
1225 rc = TAILQ_FIRST(&t->repo_commits);
1227 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1228 if (error)
1229 goto done;
1230 error = gotweb_escape_html(&msg, rc->commit_msg);
1231 if (error)
1232 goto done;
1234 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
1235 "<div id='blame_title'>Blame</div>\n"
1236 "</div>\n" /* #blame_title_wrapper */
1237 "<div id='blame_content'>\n"
1238 "<div id='blame_header_wrapper'>\n"
1239 "<div id='blame_header'>\n"
1240 "<div class='header_age_title'>Date:</div>\n"
1241 "<div class='header_age'>%s</div>\n"
1242 "<div id='header_commit_msg_title'>Message:</div>\n"
1243 "<div id='header_commit_msg'>%s</div>\n"
1244 "</div>\n" /* #blame_header */
1245 "</div>\n" /* #blame_header_wrapper */
1246 "<div class='dotted_line'></div>\n"
1247 "<div id='blame'>\n",
1248 age,
1249 msg);
1250 if (r == -1)
1251 goto done;
1253 error = got_output_file_blame(c);
1254 if (error)
1255 goto done;
1257 fcgi_printf(c, "</div>\n" /* #blame */
1258 "</div>\n"); /* #blame_content */
1259 done:
1260 free(age);
1261 free(msg);
1262 return error;
1265 static const struct got_error *
1266 gotweb_render_briefs(struct request *c)
1268 const struct got_error *error = NULL;
1269 struct repo_commit *rc = NULL;
1270 struct server *srv = c->srv;
1271 struct transport *t = c->t;
1272 struct querystring *qs = t->qs;
1273 struct repo_dir *repo_dir = t->repo_dir;
1274 char *smallerthan, *newline;
1275 char *age = NULL, *author = NULL, *msg = NULL;
1276 int r;
1278 r = fcgi_printf(c, "<div id='briefs_title_wrapper'>\n"
1279 "<div id='briefs_title'>Commit Briefs</div>\n"
1280 "</div>\n" /* #briefs_title_wrapper */
1281 "<div id='briefs_content'>\n");
1282 if (r == -1)
1283 goto done;
1285 if (qs->action == SUMMARY) {
1286 qs->action = BRIEFS;
1287 error = got_get_repo_commits(c, D_MAXSLCOMMDISP);
1288 } else
1289 error = got_get_repo_commits(c, srv->max_commits_display);
1290 if (error)
1291 goto done;
1293 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1294 error = gotweb_get_time_str(&age, rc->committer_time, TM_DIFF);
1295 if (error)
1296 goto done;
1298 smallerthan = strchr(rc->author, '<');
1299 if (smallerthan)
1300 *smallerthan = '\0';
1302 newline = strchr(rc->commit_msg, '\n');
1303 if (newline)
1304 *newline = '\0';
1306 error = gotweb_escape_html(&author, rc->author);
1307 if (error)
1308 goto done;
1309 error = gotweb_escape_html(&msg, rc->commit_msg);
1310 if (error)
1311 goto done;
1313 r = fcgi_printf(c, "<div class='briefs_age'>%s</div>\n"
1314 "<div class='briefs_author'>%s</div>\n"
1315 "<div class='briefs_log'>",
1316 age, author);
1317 if (r == -1)
1318 goto done;
1320 r = gotweb_link(c, &(struct gotweb_url){
1321 .action = DIFF,
1322 .index_page = -1,
1323 .page = -1,
1324 .path = repo_dir->name,
1325 .commit = rc->commit_id,
1326 .headref = qs->headref,
1327 }, "%s", msg);
1328 if (r == -1)
1329 goto done;
1331 if (rc->refs_str) {
1332 char *refs;
1334 error = gotweb_escape_html(&refs, rc->refs_str);
1335 if (error)
1336 goto done;
1337 r = fcgi_printf(c,
1338 " <span class='refs_str'>(%s)</span>", refs);
1339 free(refs);
1340 if (r == -1)
1341 goto done;
1343 if (fcgi_printf(c, "</div>\n") == -1) /* .briefs_log */
1344 goto done;
1346 r = fcgi_printf(c, "<div class='navs_wrapper'>\n"
1347 "<div class='navs'>");
1348 if (r == -1)
1349 goto done;
1351 r = gotweb_link(c, &(struct gotweb_url){
1352 .action = DIFF,
1353 .index_page = -1,
1354 .page = -1,
1355 .path = repo_dir->name,
1356 .commit = rc->commit_id,
1357 .headref = qs->headref,
1358 }, "diff");
1359 if (r == -1)
1360 goto done;
1362 if (fcgi_printf(c, " | ") == -1)
1363 goto done;
1365 r = gotweb_link(c, &(struct gotweb_url){
1366 .action = TREE,
1367 .index_page = -1,
1368 .page = -1,
1369 .path = repo_dir->name,
1370 .commit = rc->commit_id,
1371 .headref = qs->headref,
1372 }, "tree");
1373 if (r == -1)
1374 goto done;
1376 if (fcgi_printf(c, "</div>\n" /* .navs */
1377 "</div>\n" /* .navs_wrapper */
1378 "<div class='dotted_line'></div>\n") == -1)
1379 goto done;
1381 free(age);
1382 age = NULL;
1383 free(author);
1384 author = NULL;
1385 free(msg);
1386 msg = NULL;
1389 if (t->next_id || t->prev_id) {
1390 error = gotweb_render_navs(c);
1391 if (error)
1392 goto done;
1394 fcgi_printf(c, "</div>\n"); /* #briefs_content */
1395 done:
1396 free(age);
1397 free(author);
1398 free(msg);
1399 return error;
1402 static const struct got_error *
1403 gotweb_render_commits(struct request *c)
1405 const struct got_error *error = NULL;
1406 struct repo_commit *rc = NULL;
1407 struct server *srv = c->srv;
1408 struct transport *t = c->t;
1409 struct repo_dir *repo_dir = t->repo_dir;
1410 char *age = NULL, *author = NULL, *msg = NULL;
1411 int r;
1413 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
1414 "<div class='commits_title'>Commits</div>\n"
1415 "</div>\n" /* .commits_title_wrapper */
1416 "<div class='commits_content'>\n");
1417 if (r == -1)
1418 goto done;
1420 error = got_get_repo_commits(c, srv->max_commits_display);
1421 if (error)
1422 goto done;
1424 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
1425 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1426 if (error)
1427 goto done;
1428 error = gotweb_escape_html(&author, rc->author);
1429 if (error)
1430 goto done;
1431 error = gotweb_escape_html(&msg, rc->commit_msg);
1432 if (error)
1433 goto done;
1435 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1436 "<div class='commits_header'>\n"
1437 "<div class='header_commit_title'>Commit:</div>\n"
1438 "<div class='header_commit'>%s</div>\n"
1439 "<div class='header_author_title'>Author:</div>\n"
1440 "<div class='header_author'>%s</div>\n"
1441 "<div class='header_age_title'>Date:</div>\n"
1442 "<div class='header_age'>%s</div>\n"
1443 "</div>\n" /* .commits_header */
1444 "</div>\n" /* .commits_header_wrapper */
1445 "<div class='dotted_line'></div>\n"
1446 "<div class='commit'>\n%s</div>\n",
1447 rc->commit_id,
1448 author,
1449 age,
1450 msg);
1451 if (r == -1)
1452 goto done;
1454 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1455 "<div class='navs'>") == -1)
1456 goto done;
1458 r = gotweb_link(c, &(struct gotweb_url){
1459 .action = DIFF,
1460 .index_page = -1,
1461 .page = -1,
1462 .path = repo_dir->name,
1463 .commit = rc->commit_id,
1464 }, "diff");
1465 if (r == -1)
1466 goto done;
1468 if (fcgi_printf(c, " | ") == -1)
1469 goto done;
1471 r = gotweb_link(c, &(struct gotweb_url){
1472 .action = TREE,
1473 .index_page = -1,
1474 .page = -1,
1475 .path = repo_dir->name,
1476 .commit = rc->commit_id,
1477 }, "tree");
1478 if (r == -1)
1479 goto done;
1481 if (fcgi_printf(c, "</div>\n" /* .navs */
1482 "</div>\n" /* .navs_wrapper */
1483 "<div class='dotted_line'></div>\n") == -1)
1484 goto done;
1486 free(age);
1487 age = NULL;
1488 free(author);
1489 author = NULL;
1490 free(msg);
1491 msg = NULL;
1494 if (t->next_id || t->prev_id) {
1495 error = gotweb_render_navs(c);
1496 if (error)
1497 goto done;
1499 fcgi_printf(c, "</div>\n"); /* .commits_content */
1500 done:
1501 free(age);
1502 free(author);
1503 free(msg);
1504 return error;
1507 static const struct got_error *
1508 gotweb_render_branches(struct request *c)
1510 const struct got_error *error = NULL;
1511 struct got_reflist_head refs;
1512 struct got_reflist_entry *re;
1513 struct transport *t = c->t;
1514 struct querystring *qs = t->qs;
1515 struct got_repository *repo = t->repo;
1516 char *escaped_refname = NULL;
1517 char *age = NULL;
1518 int r;
1520 TAILQ_INIT(&refs);
1522 error = got_ref_list(&refs, repo, "refs/heads",
1523 got_ref_cmp_by_name, NULL);
1524 if (error)
1525 goto done;
1527 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1528 "<div id='branches_title'>Branches</div>\n"
1529 "</div>\n" /* #branches_title_wrapper */
1530 "<div id='branches_content'>\n");
1531 if (r == -1)
1532 goto done;
1534 TAILQ_FOREACH(re, &refs, entry) {
1535 const char *refname = NULL;
1537 if (got_ref_is_symbolic(re->ref))
1538 continue;
1540 refname = got_ref_get_name(re->ref);
1541 if (refname == NULL) {
1542 error = got_error_from_errno("strdup");
1543 goto done;
1545 if (strncmp(refname, "refs/heads/", 11) != 0)
1546 continue;
1548 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1549 if (error)
1550 goto done;
1552 if (strncmp(refname, "refs/heads/", 11) == 0)
1553 refname += 11;
1554 error = gotweb_escape_html(&escaped_refname, refname);
1555 if (error)
1556 goto done;
1558 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1559 "<div class='branches_age'>%s</div>\n"
1560 "<div class='branches_space'>&nbsp;</div>\n"
1561 "<div class='branch'>", age);
1562 if (r == -1)
1563 goto done;
1565 r = gotweb_link(c, &(struct gotweb_url){
1566 .action = SUMMARY,
1567 .index_page = -1,
1568 .page = -1,
1569 .path = qs->path,
1570 .headref = refname,
1571 }, "%s", escaped_refname);
1572 if (r == -1)
1573 goto done;
1575 if (fcgi_printf(c, "</div>\n" /* .branch */
1576 "<div class='navs_wrapper'>\n"
1577 "<div class='navs'>") == -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 }, "summary");
1587 if (r == -1)
1588 goto done;
1590 if (fcgi_printf(c, " | ") == -1)
1591 goto done;
1593 r = gotweb_link(c, &(struct gotweb_url){
1594 .action = BRIEFS,
1595 .index_page = -1,
1596 .page = -1,
1597 .path = qs->path,
1598 .headref = refname,
1599 }, "commit briefs");
1600 if (r == -1)
1601 goto done;
1603 if (fcgi_printf(c, " | ") == -1)
1604 goto done;
1606 r = gotweb_link(c, &(struct gotweb_url){
1607 .action = COMMITS,
1608 .index_page = -1,
1609 .page = -1,
1610 .path = qs->path,
1611 .headref = refname,
1612 }, "commits");
1613 if (r == -1)
1614 goto done;
1616 r = fcgi_printf(c, "</div>\n" /* .navs */
1617 "</div>\n" /* .navs_wrapper */
1618 "<div class='dotted_line'></div>\n"
1619 "</div>\n"); /* .branches_wrapper */
1620 if (r == -1)
1621 goto done;
1623 free(age);
1624 age = NULL;
1625 free(escaped_refname);
1626 escaped_refname = NULL;
1628 fcgi_printf(c, "</div>\n"); /* #branches_content */
1629 done:
1630 free(age);
1631 free(escaped_refname);
1632 got_ref_list_free(&refs);
1633 return error;
1636 static const struct got_error *
1637 gotweb_render_tree(struct request *c)
1639 const struct got_error *error = NULL;
1640 struct transport *t = c->t;
1641 struct repo_commit *rc = NULL;
1642 char *age = NULL, *msg = NULL;
1643 int r;
1645 error = got_get_repo_commits(c, 1);
1646 if (error)
1647 return error;
1649 rc = TAILQ_FIRST(&t->repo_commits);
1651 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1652 if (error)
1653 goto done;
1655 error = gotweb_escape_html(&msg, rc->commit_msg);
1656 if (error)
1657 goto done;
1659 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1660 "<div id='tree_title'>Tree</div>\n"
1661 "</div>\n" /* #tree_title_wrapper */
1662 "<div id='tree_content'>\n"
1663 "<div id='tree_header_wrapper'>\n"
1664 "<div id='tree_header'>\n"
1665 "<div id='header_tree_title'>Tree:</div>\n"
1666 "<div id='header_tree'>%s</div>\n"
1667 "<div class='header_age_title'>Date:</div>\n"
1668 "<div class='header_age'>%s</div>\n"
1669 "<div id='header_commit_msg_title'>Message:</div>\n"
1670 "<div id='header_commit_msg'>%s</div>\n"
1671 "</div>\n" /* #tree_header */
1672 "</div>\n" /* #tree_header_wrapper */
1673 "<div class='dotted_line'></div>\n"
1674 "<div id='tree'>\n",
1675 rc->tree_id,
1676 age,
1677 msg);
1678 if (r == -1)
1679 goto done;
1681 error = got_output_repo_tree(c);
1682 if (error)
1683 goto done;
1685 fcgi_printf(c, "</div>\n"); /* #tree */
1686 fcgi_printf(c, "</div>\n"); /* #tree_content */
1687 done:
1688 free(age);
1689 free(msg);
1690 return error;
1693 static const struct got_error *
1694 gotweb_render_diff(struct request *c)
1696 const struct got_error *error = NULL;
1697 struct transport *t = c->t;
1698 struct repo_commit *rc = NULL;
1699 char *age = NULL, *author = NULL, *msg = NULL;
1700 int r;
1702 error = got_get_repo_commits(c, 1);
1703 if (error)
1704 return error;
1706 rc = TAILQ_FIRST(&t->repo_commits);
1708 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1709 if (error)
1710 goto done;
1711 error = gotweb_escape_html(&author, rc->author);
1712 if (error)
1713 goto done;
1714 error = gotweb_escape_html(&msg, rc->commit_msg);
1715 if (error)
1716 goto done;
1718 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1719 "<div id='diff_title'>Commit Diff</div>\n"
1720 "</div>\n" /* #diff_title_wrapper */
1721 "<div id='diff_content'>\n"
1722 "<div id='diff_header_wrapper'>\n"
1723 "<div id='diff_header'>\n"
1724 "<div id='header_diff_title'>Diff:</div>\n"
1725 "<div id='header_diff'>%s<br />%s</div>\n"
1726 "<div class='header_commit_title'>Commit:</div>\n"
1727 "<div class='header_commit'>%s</div>\n"
1728 "<div id='header_tree_title'>Tree:</div>\n"
1729 "<div id='header_tree'>%s</div>\n"
1730 "<div class='header_author_title'>Author:</div>\n"
1731 "<div class='header_author'>%s</div>\n"
1732 "<div class='header_age_title'>Date:</div>\n"
1733 "<div class='header_age'>%s</div>\n"
1734 "<div id='header_commit_msg_title'>Message:</div>\n"
1735 "<div id='header_commit_msg'>%s</div>\n"
1736 "</div>\n" /* #diff_header */
1737 "</div>\n" /* #diff_header_wrapper */
1738 "<div class='dotted_line'></div>\n"
1739 "<div id='diff'>\n",
1740 rc->parent_id, rc->commit_id,
1741 rc->commit_id,
1742 rc->tree_id,
1743 author,
1744 age,
1745 msg);
1746 if (r == -1)
1747 goto done;
1749 error = got_output_repo_diff(c);
1750 if (error)
1751 goto done;
1753 fcgi_printf(c, "</div>\n"); /* #diff */
1754 fcgi_printf(c, "</div>\n"); /* #diff_content */
1755 done:
1756 free(age);
1757 free(author);
1758 free(msg);
1759 return error;
1762 static const struct got_error *
1763 gotweb_render_summary(struct request *c)
1765 const struct got_error *error = NULL;
1766 struct transport *t = c->t;
1767 struct server *srv = c->srv;
1768 int r;
1770 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1771 goto done;
1773 if (srv->show_repo_description) {
1774 r = fcgi_printf(c,
1775 "<div id='description_title'>Description:</div>\n"
1776 "<div id='description'>%s</div>\n",
1777 t->repo_dir->description ? t->repo_dir->description : "");
1778 if (r == -1)
1779 goto done;
1782 if (srv->show_repo_owner) {
1783 r = fcgi_printf(c,
1784 "<div id='repo_owner_title'>Owner:</div>\n"
1785 "<div id='repo_owner'>%s</div>\n",
1786 t->repo_dir->owner ? t->repo_dir->owner : "");
1787 if (r == -1)
1788 goto done;
1791 if (srv->show_repo_age) {
1792 r = fcgi_printf(c,
1793 "<div id='last_change_title'>Last Change:</div>\n"
1794 "<div id='last_change'>%s</div>\n",
1795 t->repo_dir->age);
1796 if (r == -1)
1797 goto done;
1800 if (srv->show_repo_cloneurl) {
1801 r = fcgi_printf(c,
1802 "<div id='cloneurl_title'>Clone URL:</div>\n"
1803 "<div id='cloneurl'>%s</div>\n",
1804 t->repo_dir->url ? t->repo_dir->url : "");
1805 if (r == -1)
1806 goto done;
1809 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1810 if (r == -1)
1811 goto done;
1813 error = gotweb_render_briefs(c);
1814 if (error) {
1815 log_warnx("%s: %s", __func__, error->msg);
1816 goto done;
1819 error = gotweb_render_tags(c);
1820 if (error) {
1821 log_warnx("%s: %s", __func__, error->msg);
1822 goto done;
1825 error = gotweb_render_branches(c);
1826 if (error)
1827 log_warnx("%s: %s", __func__, error->msg);
1828 done:
1829 return error;
1832 static const struct got_error *
1833 gotweb_render_tag(struct request *c)
1835 const struct got_error *error = NULL;
1836 struct repo_tag *rt = NULL;
1837 struct transport *t = c->t;
1838 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1840 error = got_get_repo_tags(c, 1);
1841 if (error)
1842 goto done;
1844 if (t->tag_count == 0) {
1845 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1846 "bad commit id");
1847 goto done;
1850 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1852 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1853 if (error)
1854 goto done;
1855 error = gotweb_escape_html(&author, rt->tagger);
1856 if (error)
1857 goto done;
1858 error = gotweb_escape_html(&msg, rt->commit_msg);
1859 if (error)
1860 goto done;
1862 tagname = rt->tag_name;
1863 if (strncmp(tagname, "refs/", 5) == 0)
1864 tagname += 5;
1865 error = gotweb_escape_html(&tagname, tagname);
1866 if (error)
1867 goto done;
1869 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1870 "<div id='tags_title'>Tag</div>\n"
1871 "</div>\n" /* #tags_title_wrapper */
1872 "<div id='tags_content'>\n"
1873 "<div id='tag_header_wrapper'>\n"
1874 "<div id='tag_header'>\n"
1875 "<div class='header_commit_title'>Commit:</div>\n"
1876 "<div class='header_commit'>%s"
1877 " <span class='refs_str'>(%s)</span></div>\n"
1878 "<div class='header_author_title'>Tagger:</div>\n"
1879 "<div class='header_author'>%s</div>\n"
1880 "<div class='header_age_title'>Date:</div>\n"
1881 "<div class='header_age'>%s</div>\n"
1882 "<div id='header_commit_msg_title'>Message:</div>\n"
1883 "<div id='header_commit_msg'>%s</div>\n"
1884 "</div>\n" /* #tag_header */
1885 "<div class='dotted_line'></div>\n"
1886 "<div id='tag_commit'>\n%s</div>"
1887 "</div>" /* #tag_header_wrapper */
1888 "</div>", /* #tags_content */
1889 rt->commit_id,
1890 tagname,
1891 author,
1892 age,
1893 msg,
1894 rt->tag_commit);
1896 done:
1897 free(age);
1898 free(author);
1899 free(msg);
1900 return error;
1903 static const struct got_error *
1904 gotweb_render_tags(struct request *c)
1906 const struct got_error *error = NULL;
1907 struct repo_tag *rt = NULL;
1908 struct server *srv = c->srv;
1909 struct transport *t = c->t;
1910 struct querystring *qs = t->qs;
1911 struct repo_dir *repo_dir = t->repo_dir;
1912 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1913 int r, commit_found = 0;
1915 if (qs->action == BRIEFS) {
1916 qs->action = TAGS;
1917 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1918 } else
1919 error = got_get_repo_tags(c, srv->max_commits_display);
1920 if (error)
1921 goto done;
1923 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1924 "<div id='tags_title'>Tags</div>\n"
1925 "</div>\n" /* #tags_title_wrapper */
1926 "<div id='tags_content'>\n");
1927 if (r == -1)
1928 goto done;
1930 if (t->tag_count == 0) {
1931 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1932 "This repository contains no tags");
1933 if (r == -1)
1934 goto done;
1937 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1938 if (commit_found == 0 && qs->commit != NULL) {
1939 if (strcmp(qs->commit, rt->commit_id) != 0)
1940 continue;
1941 else
1942 commit_found = 1;
1944 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1945 if (error)
1946 goto done;
1948 tagname = rt->tag_name;
1949 if (strncmp(tagname, "refs/tags/", 10) == 0)
1950 tagname += 10;
1951 error = gotweb_escape_html(&tagname, tagname);
1952 if (error)
1953 goto done;
1955 if (rt->tag_commit != NULL) {
1956 newline = strchr(rt->tag_commit, '\n');
1957 if (newline)
1958 *newline = '\0';
1959 error = gotweb_escape_html(&msg, rt->tag_commit);
1960 if (error)
1961 goto done;
1964 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1965 "<div class='tag'>%s</div>\n"
1966 "<div class='tag_log'>", age, tagname) == -1)
1967 goto done;
1969 r = gotweb_link(c, &(struct gotweb_url){
1970 .action = TAG,
1971 .index_page = -1,
1972 .page = -1,
1973 .path = repo_dir->name,
1974 .commit = rt->commit_id,
1975 }, "%s", msg ? msg : "");
1976 if (r == -1)
1977 goto done;
1979 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1980 "<div class='navs_wrapper'>\n"
1981 "<div class='navs'>") == -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 }, "tag");
1991 if (r == -1)
1992 goto done;
1994 if (fcgi_printf(c, " | ") == -1)
1995 goto done;
1997 r = gotweb_link(c, &(struct gotweb_url){
1998 .action = BRIEFS,
1999 .index_page = -1,
2000 .page = -1,
2001 .path = repo_dir->name,
2002 .commit = rt->commit_id,
2003 }, "commit briefs");
2004 if (r == -1)
2005 goto done;
2007 if (fcgi_printf(c, " | ") == -1)
2008 goto done;
2010 r = gotweb_link(c, &(struct gotweb_url){
2011 .action = COMMITS,
2012 .index_page = -1,
2013 .page = -1,
2014 .path = repo_dir->name,
2015 .commit = rt->commit_id,
2016 }, "commits");
2017 if (r == -1)
2018 goto done;
2020 r = fcgi_printf(c,
2021 "</div>\n" /* .navs */
2022 "</div>\n" /* .navs_wrapper */
2023 "<div class='dotted_line'></div>\n");
2024 if (r == -1)
2025 goto done;
2027 free(age);
2028 age = NULL;
2029 free(tagname);
2030 tagname = NULL;
2031 free(msg);
2032 msg = NULL;
2034 if (t->next_id || t->prev_id) {
2035 error = gotweb_render_navs(c);
2036 if (error)
2037 goto done;
2039 fcgi_printf(c, "</div>\n"); /* #tags_content */
2040 done:
2041 free(age);
2042 free(tagname);
2043 free(msg);
2044 return error;
2047 const struct got_error *
2048 gotweb_escape_html(char **escaped_html, const char *orig_html)
2050 const struct got_error *error = NULL;
2051 struct escape_pair {
2052 char c;
2053 const char *s;
2054 } esc[] = {
2055 { '>', "&gt;" },
2056 { '<', "&lt;" },
2057 { '&', "&amp;" },
2058 { '"', "&quot;" },
2059 { '\'', "&apos;" },
2060 { '\n', "<br />" },
2062 size_t orig_len, len;
2063 int i, j, x;
2065 orig_len = strlen(orig_html);
2066 len = orig_len;
2067 for (i = 0; i < orig_len; i++) {
2068 for (j = 0; j < nitems(esc); j++) {
2069 if (orig_html[i] != esc[j].c)
2070 continue;
2071 len += strlen(esc[j].s) - 1 /* escaped char */;
2075 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
2076 if (*escaped_html == NULL)
2077 return got_error_from_errno("calloc");
2079 x = 0;
2080 for (i = 0; i < orig_len; i++) {
2081 int escaped = 0;
2082 for (j = 0; j < nitems(esc); j++) {
2083 if (orig_html[i] != esc[j].c)
2084 continue;
2086 if (strlcat(*escaped_html, esc[j].s, len + 1)
2087 >= len + 1) {
2088 error = got_error(GOT_ERR_NO_SPACE);
2089 goto done;
2091 x += strlen(esc[j].s);
2092 escaped = 1;
2093 break;
2095 if (!escaped) {
2096 (*escaped_html)[x] = orig_html[i];
2097 x++;
2100 done:
2101 if (error) {
2102 free(*escaped_html);
2103 *escaped_html = NULL;
2104 } else {
2105 (*escaped_html)[x] = '\0';
2108 return error;
2111 static inline int
2112 should_urlencode(int c)
2114 if (c <= ' ' || c >= 127)
2115 return 1;
2117 switch (c) {
2118 /* gen-delim */
2119 case ':':
2120 case '/':
2121 case '?':
2122 case '#':
2123 case '[':
2124 case ']':
2125 case '@':
2126 /* sub-delims */
2127 case '!':
2128 case '$':
2129 case '&':
2130 case '\'':
2131 case '(':
2132 case ')':
2133 case '*':
2134 case '+':
2135 case ',':
2136 case ';':
2137 case '=':
2138 return 1;
2139 default:
2140 return 0;
2144 static char *
2145 gotweb_urlencode(const char *str)
2147 const char *s;
2148 char *escaped;
2149 size_t i, len;
2150 int a, b;
2152 len = 0;
2153 for (s = str; *s; ++s) {
2154 len++;
2155 if (should_urlencode(*s))
2156 len += 2;
2159 escaped = calloc(1, len + 1);
2160 if (escaped == NULL)
2161 return NULL;
2163 i = 0;
2164 for (s = str; *s; ++s) {
2165 if (should_urlencode(*s)) {
2166 a = (*s & 0xF0) >> 4;
2167 b = (*s & 0x0F);
2169 escaped[i++] = '%';
2170 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
2171 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
2172 } else
2173 escaped[i++] = *s;
2176 return escaped;
2179 static inline const char *
2180 action_name(int action)
2182 switch (action) {
2183 case BLAME:
2184 return "blame";
2185 case BLOB:
2186 return "blob";
2187 case BRIEFS:
2188 return "briefs";
2189 case COMMITS:
2190 return "commits";
2191 case DIFF:
2192 return "diff";
2193 case ERR:
2194 return "err";
2195 case INDEX:
2196 return "index";
2197 case SUMMARY:
2198 return "summary";
2199 case TAG:
2200 return "tag";
2201 case TAGS:
2202 return "tags";
2203 case TREE:
2204 return "tree";
2205 default:
2206 return NULL;
2210 static int
2211 gotweb_print_url(struct request *c, struct gotweb_url *url)
2213 const char *sep = "?", *action;
2214 char *tmp;
2215 int r;
2217 action = action_name(url->action);
2218 if (action != NULL) {
2219 if (fcgi_printf(c, "?action=%s", action) == -1)
2220 return -1;
2221 sep = "&";
2224 if (url->commit) {
2225 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
2226 return -1;
2227 sep = "&";
2230 if (url->previd) {
2231 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
2232 return -1;
2233 sep = "&";
2236 if (url->prevset) {
2237 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
2238 return -1;
2239 sep = "&";
2242 if (url->file) {
2243 tmp = gotweb_urlencode(url->file);
2244 if (tmp == NULL)
2245 return -1;
2246 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
2247 free(tmp);
2248 if (r == -1)
2249 return -1;
2250 sep = "&";
2253 if (url->folder) {
2254 tmp = gotweb_urlencode(url->folder);
2255 if (tmp == NULL)
2256 return -1;
2257 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
2258 free(tmp);
2259 if (r == -1)
2260 return -1;
2261 sep = "&";
2264 if (url->headref) {
2265 tmp = gotweb_urlencode(url->headref);
2266 if (tmp == NULL)
2267 return -1;
2268 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
2269 free(tmp);
2270 if (r == -1)
2271 return -1;
2272 sep = "&";
2275 if (url->index_page != -1) {
2276 if (fcgi_printf(c, "%sindex_page=%d", sep,
2277 url->index_page) == -1)
2278 return -1;
2279 sep = "&";
2282 if (url->path) {
2283 tmp = gotweb_urlencode(url->path);
2284 if (tmp == NULL)
2285 return -1;
2286 r = fcgi_printf(c, "%spath=%s", sep, tmp);
2287 free(tmp);
2288 if (r == -1)
2289 return -1;
2290 sep = "&";
2293 if (url->page != -1) {
2294 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
2295 return -1;
2296 sep = "&";
2299 return 0;
2302 int
2303 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
2305 va_list ap;
2306 int r;
2308 if (fcgi_printf(c, "<a href='") == -1)
2309 return -1;
2311 if (gotweb_print_url(c, url) == -1)
2312 return -1;
2314 if (fcgi_printf(c, "'>") == -1)
2315 return -1;
2317 va_start(ap, fmt);
2318 r = fcgi_vprintf(c, fmt, ap);
2319 va_end(ap);
2320 if (r == -1)
2321 return -1;
2323 if (fcgi_printf(c, "</a>"))
2324 return -1;
2325 return 0;
2328 static struct got_repository *
2329 find_cached_repo(struct server *srv, const char *path)
2331 int i;
2333 for (i = 0; i < srv->ncached_repos; i++) {
2334 if (strcmp(srv->cached_repos[i].path, path) == 0)
2335 return srv->cached_repos[i].repo;
2338 return NULL;
2341 static const struct got_error *
2342 cache_repo(struct got_repository **new, struct server *srv,
2343 struct repo_dir *repo_dir, struct socket *sock)
2345 const struct got_error *error = NULL;
2346 struct got_repository *repo;
2347 struct cached_repo *cr;
2348 int evicted = 0;
2350 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
2351 cr = &srv->cached_repos[srv->ncached_repos - 1];
2352 error = got_repo_close(cr->repo);
2353 memset(cr, 0, sizeof(*cr));
2354 srv->ncached_repos--;
2355 if (error)
2356 return error;
2357 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
2358 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2359 cr = &srv->cached_repos[0];
2360 evicted = 1;
2361 } else {
2362 cr = &srv->cached_repos[srv->ncached_repos];
2365 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
2366 if (error) {
2367 if (evicted) {
2368 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2369 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2371 return error;
2374 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
2375 >= sizeof(cr->path)) {
2376 if (evicted) {
2377 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
2378 srv->ncached_repos * sizeof(srv->cached_repos[0]));
2380 return got_error(GOT_ERR_NO_SPACE);
2383 cr->repo = repo;
2384 srv->ncached_repos++;
2385 *new = repo;
2386 return NULL;
2389 static const struct got_error *
2390 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
2392 const struct got_error *error = NULL;
2393 struct socket *sock = c->sock;
2394 struct server *srv = c->srv;
2395 struct transport *t = c->t;
2396 struct got_repository *repo = NULL;
2397 DIR *dt;
2398 char *dir_test;
2400 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
2401 GOTWEB_GIT_DIR) == -1)
2402 return got_error_from_errno("asprintf");
2404 dt = opendir(dir_test);
2405 if (dt == NULL) {
2406 free(dir_test);
2407 } else {
2408 repo_dir->path = dir_test;
2409 dir_test = NULL;
2410 goto done;
2413 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
2414 repo_dir->name) == -1)
2415 return got_error_from_errno("asprintf");
2417 dt = opendir(dir_test);
2418 if (dt == NULL) {
2419 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2420 goto err;
2421 } else {
2422 repo_dir->path = dir_test;
2423 dir_test = NULL;
2426 done:
2427 if (srv->respect_exportok &&
2428 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2429 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2430 goto err;
2433 repo = find_cached_repo(srv, repo_dir->path);
2434 if (repo == NULL) {
2435 error = cache_repo(&repo, srv, repo_dir, sock);
2436 if (error)
2437 goto err;
2439 t->repo = repo;
2440 error = gotweb_get_repo_description(&repo_dir->description, srv,
2441 repo_dir->path);
2442 if (error)
2443 goto err;
2444 error = got_get_repo_owner(&repo_dir->owner, c);
2445 if (error)
2446 goto err;
2447 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2448 if (error)
2449 goto err;
2450 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path);
2451 err:
2452 free(dir_test);
2453 if (dt != NULL && closedir(dt) == EOF && error == NULL)
2454 error = got_error_from_errno("closedir");
2455 return error;
2458 static const struct got_error *
2459 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2461 const struct got_error *error;
2463 *repo_dir = calloc(1, sizeof(**repo_dir));
2464 if (*repo_dir == NULL)
2465 return got_error_from_errno("calloc");
2467 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2468 error = got_error_from_errno("asprintf");
2469 free(*repo_dir);
2470 *repo_dir = NULL;
2471 return error;
2473 (*repo_dir)->owner = NULL;
2474 (*repo_dir)->description = NULL;
2475 (*repo_dir)->url = NULL;
2476 (*repo_dir)->age = NULL;
2477 (*repo_dir)->path = NULL;
2479 return NULL;
2482 static const struct got_error *
2483 gotweb_get_repo_description(char **description, struct server *srv, char *dir)
2485 const struct got_error *error = NULL;
2486 FILE *f = NULL;
2487 char *d_file = NULL;
2488 unsigned int len;
2489 size_t n;
2491 *description = NULL;
2492 if (srv->show_repo_description == 0)
2493 return NULL;
2495 if (asprintf(&d_file, "%s/description", dir) == -1)
2496 return got_error_from_errno("asprintf");
2498 f = fopen(d_file, "r");
2499 if (f == NULL) {
2500 if (errno != ENOENT && errno != EACCES)
2501 error = got_error_from_errno2("fopen", d_file);
2502 goto done;
2505 if (fseek(f, 0, SEEK_END) == -1) {
2506 error = got_ferror(f, GOT_ERR_IO);
2507 goto done;
2509 len = ftell(f);
2510 if (len == -1) {
2511 error = got_ferror(f, GOT_ERR_IO);
2512 goto done;
2515 if (len == 0) {
2516 *description = strdup("");
2517 if (*description == NULL)
2518 error = got_error_from_errno("strdup");
2519 goto done;
2522 if (fseek(f, 0, SEEK_SET) == -1) {
2523 error = got_ferror(f, GOT_ERR_IO);
2524 goto done;
2526 *description = calloc(len + 1, sizeof(**description));
2527 if (*description == NULL) {
2528 error = got_error_from_errno("calloc");
2529 goto done;
2532 n = fread(*description, 1, len, f);
2533 if (n == 0 && ferror(f))
2534 error = got_ferror(f, GOT_ERR_IO);
2535 done:
2536 if (f != NULL && fclose(f) == EOF && error == NULL)
2537 error = got_error_from_errno("fclose");
2538 free(d_file);
2539 return error;
2542 static const struct got_error *
2543 gotweb_get_clone_url(char **url, struct server *srv, char *dir)
2545 const struct got_error *error = NULL;
2546 FILE *f;
2547 char *d_file = NULL;
2548 unsigned int len;
2549 size_t n;
2551 *url = NULL;
2553 if (srv->show_repo_cloneurl == 0)
2554 return NULL;
2556 if (asprintf(&d_file, "%s/cloneurl", dir) == -1)
2557 return got_error_from_errno("asprintf");
2559 f = fopen(d_file, "r");
2560 if (f == NULL) {
2561 if (errno != ENOENT && errno != EACCES)
2562 error = got_error_from_errno2("fopen", d_file);
2563 goto done;
2566 if (fseek(f, 0, SEEK_END) == -1) {
2567 error = got_ferror(f, GOT_ERR_IO);
2568 goto done;
2570 len = ftell(f);
2571 if (len == -1) {
2572 error = got_ferror(f, GOT_ERR_IO);
2573 goto done;
2575 if (len == 0)
2576 goto done;
2578 if (fseek(f, 0, SEEK_SET) == -1) {
2579 error = got_ferror(f, GOT_ERR_IO);
2580 goto done;
2583 *url = calloc(len + 1, sizeof(**url));
2584 if (*url == NULL) {
2585 error = got_error_from_errno("calloc");
2586 goto done;
2589 n = fread(*url, 1, len, f);
2590 if (n == 0 && ferror(f))
2591 error = got_ferror(f, GOT_ERR_IO);
2592 done:
2593 if (f != NULL && fclose(f) == EOF && error == NULL)
2594 error = got_error_from_errno("fclose");
2595 free(d_file);
2596 return error;
2599 const struct got_error *
2600 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2602 struct tm tm;
2603 long long diff_time;
2604 const char *years = "years ago", *months = "months ago";
2605 const char *weeks = "weeks ago", *days = "days ago";
2606 const char *hours = "hours ago", *minutes = "minutes ago";
2607 const char *seconds = "seconds ago", *now = "right now";
2608 char *s;
2609 char datebuf[29];
2611 *repo_age = NULL;
2613 switch (ref_tm) {
2614 case TM_DIFF:
2615 diff_time = time(NULL) - committer_time;
2616 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2617 if (asprintf(repo_age, "%lld %s",
2618 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2619 return got_error_from_errno("asprintf");
2620 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2621 if (asprintf(repo_age, "%lld %s",
2622 (diff_time / 60 / 60 / 24 / (365 / 12)),
2623 months) == -1)
2624 return got_error_from_errno("asprintf");
2625 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2626 if (asprintf(repo_age, "%lld %s",
2627 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2628 return got_error_from_errno("asprintf");
2629 } else if (diff_time > 60 * 60 * 24 * 2) {
2630 if (asprintf(repo_age, "%lld %s",
2631 (diff_time / 60 / 60 / 24), days) == -1)
2632 return got_error_from_errno("asprintf");
2633 } else if (diff_time > 60 * 60 * 2) {
2634 if (asprintf(repo_age, "%lld %s",
2635 (diff_time / 60 / 60), hours) == -1)
2636 return got_error_from_errno("asprintf");
2637 } else if (diff_time > 60 * 2) {
2638 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2639 minutes) == -1)
2640 return got_error_from_errno("asprintf");
2641 } else if (diff_time > 2) {
2642 if (asprintf(repo_age, "%lld %s", diff_time,
2643 seconds) == -1)
2644 return got_error_from_errno("asprintf");
2645 } else {
2646 if (asprintf(repo_age, "%s", now) == -1)
2647 return got_error_from_errno("asprintf");
2649 break;
2650 case TM_LONG:
2651 if (gmtime_r(&committer_time, &tm) == NULL)
2652 return got_error_from_errno("gmtime_r");
2654 s = asctime_r(&tm, datebuf);
2655 if (s == NULL)
2656 return got_error_from_errno("asctime_r");
2658 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2659 return got_error_from_errno("asprintf");
2660 break;
2662 return NULL;