Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 { "page", PAGE },
64 };
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "briefs", BRIEFS },
70 { "commits", COMMITS },
71 { "diff", DIFF },
72 { "error", ERR },
73 { "index", INDEX },
74 { "summary", SUMMARY },
75 { "tag", TAG },
76 { "tags", TAGS },
77 { "tree", TREE },
78 { "rss", RSS },
79 };
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 char *);
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 char *, char *);
86 static const struct got_error *gotweb_render_index(struct request *);
87 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
88 const char *);
89 static const struct got_error *gotweb_load_got_path(struct request *c,
90 struct repo_dir *);
91 static const struct got_error *gotweb_get_repo_description(char **,
92 struct server *, const char *, int);
93 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
94 const char *, int);
95 static const struct got_error *gotweb_render_blame(struct request *);
96 static const struct got_error *gotweb_render_diff(struct request *);
97 static const struct got_error *gotweb_render_summary(struct request *);
98 static const struct got_error *gotweb_render_tag(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_tree(struct request *);
101 static const struct got_error *gotweb_render_branches(struct request *);
103 static void gotweb_free_querystring(struct querystring *);
104 static void gotweb_free_repo_dir(struct repo_dir *);
106 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 void
109 gotweb_process_request(struct request *c)
111 const struct got_error *error = NULL, *error2 = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
116 int r, html = 0;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
120 if (error) {
121 log_warnx("%s: %s", __func__, error->msg);
122 return;
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
126 return;
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
129 if (srv == NULL) {
130 log_warnx("%s: error server is NULL", __func__);
131 goto err;
133 c->srv = srv;
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
136 if (error) {
137 log_warnx("%s: %s", __func__, error->msg);
138 goto err;
140 c->t->qs = qs;
141 error = gotweb_parse_querystring(&qs, c->querystring);
142 if (error) {
143 log_warnx("%s: %s", __func__, error->msg);
144 goto err;
147 /*
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
150 * querystring.
151 */
153 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == DIFF)) {
155 error2 = got_error(GOT_ERR_QUERYSTRING);
156 goto render;
159 if (qs->action != INDEX) {
160 error = gotweb_init_repo_dir(&repo_dir, qs->path);
161 if (error)
162 goto done;
163 error = gotweb_load_got_path(c, repo_dir);
164 c->t->repo_dir = repo_dir;
165 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
166 goto err;
169 if (qs->action == BLOB) {
170 error = got_get_repo_commits(c, 1);
171 if (error)
172 goto done;
173 error = got_output_file_blob(c);
174 if (error) {
175 log_warnx("%s: %s", __func__, error->msg);
176 goto err;
178 goto done;
181 if (qs->action == RSS) {
182 error = gotweb_render_content_type(c,
183 "application/rss+xml;charset=utf-8");
184 if (error) {
185 log_warnx("%s: %s", __func__, error->msg);
186 goto err;
189 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
190 if (error) {
191 log_warnx("%s: %s", __func__, error->msg);
192 goto err;
194 if (gotweb_render_rss(c->tp) == -1)
195 goto err;
196 goto done;
199 render:
200 error = gotweb_render_content_type(c, "text/html");
201 if (error) {
202 log_warnx("%s: %s", __func__, error->msg);
203 goto err;
205 html = 1;
207 if (gotweb_render_header(c->tp) == -1)
208 goto err;
210 if (error2) {
211 error = error2;
212 goto err;
215 switch(qs->action) {
216 case BLAME:
217 error = gotweb_render_blame(c);
218 if (error) {
219 log_warnx("%s: %s", __func__, error->msg);
220 goto err;
222 break;
223 case BRIEFS:
224 if (gotweb_render_briefs(c->tp) == -1)
225 goto err;
226 break;
227 case COMMITS:
228 error = got_get_repo_commits(c, srv->max_commits_display);
229 if (error) {
230 log_warnx("%s: %s", __func__, error->msg);
231 goto err;
233 if (gotweb_render_commits(c->tp) == -1)
234 goto err;
235 break;
236 case DIFF:
237 error = gotweb_render_diff(c);
238 if (error) {
239 log_warnx("%s: %s", __func__, error->msg);
240 goto err;
242 break;
243 case INDEX:
244 error = gotweb_render_index(c);
245 if (error) {
246 log_warnx("%s: %s", __func__, error->msg);
247 goto err;
249 break;
250 case SUMMARY:
251 error = gotweb_render_summary(c);
252 if (error) {
253 log_warnx("%s: %s", __func__, error->msg);
254 goto err;
256 break;
257 case TAG:
258 error = gotweb_render_tag(c);
259 if (error) {
260 log_warnx("%s: %s", __func__, error->msg);
261 goto err;
263 break;
264 case TAGS:
265 error = gotweb_render_tags(c);
266 if (error) {
267 log_warnx("%s: %s", __func__, error->msg);
268 goto err;
270 break;
271 case TREE:
272 error = gotweb_render_tree(c);
273 if (error) {
274 log_warnx("%s: %s", __func__, error->msg);
275 goto err;
277 break;
278 case ERR:
279 default:
280 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
281 "Erorr: Bad Querystring");
282 if (r == -1)
283 goto err;
284 break;
287 goto done;
288 err:
289 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
290 return;
291 if (fcgi_printf(c, "\n%s", err) == -1)
292 return;
293 if (error) {
294 if (fcgi_printf(c, "%s", error->msg) == -1)
295 return;
296 } else {
297 if (fcgi_printf(c, "see daemon logs for details") == -1)
298 return;
300 if (html && fcgi_printf(c, "</div>\n") == -1)
301 return;
302 done:
303 if (html && srv != NULL)
304 gotweb_render_footer(c->tp);
307 struct server *
308 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
310 struct server *srv = NULL;
312 /* check against the server name first */
313 if (strlen(server_name) > 0)
314 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 if (strcmp(srv->name, server_name) == 0)
316 goto done;
318 /* check against subdomain second */
319 if (strlen(subdomain) > 0)
320 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
321 if (strcmp(srv->name, subdomain) == 0)
322 goto done;
324 /* if those fail, send first server */
325 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
326 if (srv != NULL)
327 break;
328 done:
329 return srv;
330 };
332 const struct got_error *
333 gotweb_init_transport(struct transport **t)
335 const struct got_error *error = NULL;
337 *t = calloc(1, sizeof(**t));
338 if (*t == NULL)
339 return got_error_from_errno2("%s: calloc", __func__);
341 TAILQ_INIT(&(*t)->repo_commits);
342 TAILQ_INIT(&(*t)->repo_tags);
344 (*t)->repo = NULL;
345 (*t)->repo_dir = NULL;
346 (*t)->qs = NULL;
347 (*t)->next_id = NULL;
348 (*t)->prev_id = NULL;
349 (*t)->next_disp = 0;
350 (*t)->prev_disp = 0;
352 return error;
355 static const struct got_error *
356 gotweb_init_querystring(struct querystring **qs)
358 const struct got_error *error = NULL;
360 *qs = calloc(1, sizeof(**qs));
361 if (*qs == NULL)
362 return got_error_from_errno2("%s: calloc", __func__);
364 (*qs)->headref = strdup("HEAD");
365 if ((*qs)->headref == NULL) {
366 free(*qs);
367 *qs = NULL;
368 return got_error_from_errno2("%s: strdup", __func__);
371 (*qs)->action = INDEX;
372 (*qs)->commit = NULL;
373 (*qs)->file = NULL;
374 (*qs)->folder = NULL;
375 (*qs)->index_page = 0;
376 (*qs)->path = NULL;
378 return error;
381 static const struct got_error *
382 gotweb_parse_querystring(struct querystring **qs, char *qst)
384 const struct got_error *error = NULL;
385 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
386 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
388 if (qst == NULL)
389 return error;
391 tok1 = strdup(qst);
392 if (tok1 == NULL)
393 return got_error_from_errno2("%s: strdup", __func__);
395 tok1_pair = tok1;
396 tok1_end = tok1;
398 while (tok1_pair != NULL) {
399 strsep(&tok1_end, "&");
401 tok2 = strdup(tok1_pair);
402 if (tok2 == NULL) {
403 free(tok1);
404 return got_error_from_errno2("%s: strdup", __func__);
407 tok2_pair = tok2;
408 tok2_end = tok2;
410 while (tok2_pair != NULL) {
411 strsep(&tok2_end, "=");
412 if (tok2_end) {
413 error = gotweb_assign_querystring(qs, tok2_pair,
414 tok2_end);
415 if (error)
416 goto err;
418 tok2_pair = tok2_end;
420 free(tok2);
421 tok1_pair = tok1_end;
423 free(tok1);
424 return error;
425 err:
426 free(tok2);
427 free(tok1);
428 return error;
431 /*
432 * Adapted from usr.sbin/httpd/httpd.c url_decode.
433 */
434 static const struct got_error *
435 gotweb_urldecode(char *url)
437 char *p, *q;
438 char hex[3];
439 unsigned long x;
441 hex[2] = '\0';
442 p = q = url;
444 while (*p != '\0') {
445 switch (*p) {
446 case '%':
447 /* Encoding character is followed by two hex chars */
448 if (!isxdigit((unsigned char)p[1]) ||
449 !isxdigit((unsigned char)p[2]) ||
450 (p[1] == '0' && p[2] == '0'))
451 return got_error(GOT_ERR_BAD_QUERYSTRING);
453 hex[0] = p[1];
454 hex[1] = p[2];
456 /*
457 * We don't have to validate "hex" because it is
458 * guaranteed to include two hex chars followed by nul.
459 */
460 x = strtoul(hex, NULL, 16);
461 *q = (char)x;
462 p += 2;
463 break;
464 default:
465 *q = *p;
466 break;
468 p++;
469 q++;
471 *q = '\0';
473 return NULL;
476 static const struct got_error *
477 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
479 const struct got_error *error = NULL;
480 const char *errstr;
481 int a_cnt, el_cnt;
483 error = gotweb_urldecode(value);
484 if (error)
485 return error;
487 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
488 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
489 continue;
491 switch (querystring_keys[el_cnt].element) {
492 case ACTION:
493 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
494 if (strcmp(value, action_keys[a_cnt].name) != 0)
495 continue;
496 else if (strcmp(value,
497 action_keys[a_cnt].name) == 0){
498 (*qs)->action =
499 action_keys[a_cnt].action;
500 goto qa_found;
503 (*qs)->action = ERR;
504 qa_found:
505 break;
506 case COMMIT:
507 (*qs)->commit = strdup(value);
508 if ((*qs)->commit == NULL) {
509 error = got_error_from_errno2("%s: strdup",
510 __func__);
511 goto done;
513 break;
514 case RFILE:
515 (*qs)->file = strdup(value);
516 if ((*qs)->file == NULL) {
517 error = got_error_from_errno2("%s: strdup",
518 __func__);
519 goto done;
521 break;
522 case FOLDER:
523 (*qs)->folder = strdup(value);
524 if ((*qs)->folder == NULL) {
525 error = got_error_from_errno2("%s: strdup",
526 __func__);
527 goto done;
529 break;
530 case HEADREF:
531 free((*qs)->headref);
532 (*qs)->headref = strdup(value);
533 if ((*qs)->headref == NULL) {
534 error = got_error_from_errno2("%s: strdup",
535 __func__);
536 goto done;
538 break;
539 case INDEX_PAGE:
540 if (strlen(value) == 0)
541 break;
542 (*qs)->index_page = strtonum(value, INT64_MIN,
543 INT64_MAX, &errstr);
544 if (errstr) {
545 error = got_error_from_errno3("%s: strtonum %s",
546 __func__, errstr);
547 goto done;
549 if ((*qs)->index_page < 0)
550 (*qs)->index_page = 0;
551 break;
552 case PATH:
553 (*qs)->path = strdup(value);
554 if ((*qs)->path == NULL) {
555 error = got_error_from_errno2("%s: strdup",
556 __func__);
557 goto done;
559 break;
560 case PAGE:
561 if (strlen(value) == 0)
562 break;
563 (*qs)->page = strtonum(value, INT64_MIN,
564 INT64_MAX, &errstr);
565 if (errstr) {
566 error = got_error_from_errno3("%s: strtonum %s",
567 __func__, errstr);
568 goto done;
570 if ((*qs)->page < 0)
571 (*qs)->page = 0;
572 break;
573 default:
574 break;
577 done:
578 return error;
581 void
582 gotweb_free_repo_tag(struct repo_tag *rt)
584 if (rt != NULL) {
585 free(rt->commit_id);
586 free(rt->tag_name);
587 free(rt->tag_commit);
588 free(rt->commit_msg);
589 free(rt->tagger);
591 free(rt);
594 void
595 gotweb_free_repo_commit(struct repo_commit *rc)
597 if (rc != NULL) {
598 free(rc->path);
599 free(rc->refs_str);
600 free(rc->commit_id);
601 free(rc->parent_id);
602 free(rc->tree_id);
603 free(rc->author);
604 free(rc->committer);
605 free(rc->commit_msg);
607 free(rc);
610 static void
611 gotweb_free_querystring(struct querystring *qs)
613 if (qs != NULL) {
614 free(qs->commit);
615 free(qs->file);
616 free(qs->folder);
617 free(qs->headref);
618 free(qs->path);
620 free(qs);
623 static void
624 gotweb_free_repo_dir(struct repo_dir *repo_dir)
626 if (repo_dir != NULL) {
627 free(repo_dir->name);
628 free(repo_dir->owner);
629 free(repo_dir->description);
630 free(repo_dir->url);
631 free(repo_dir->age);
632 free(repo_dir->path);
634 free(repo_dir);
637 void
638 gotweb_free_transport(struct transport *t)
640 struct repo_commit *rc = NULL, *trc = NULL;
641 struct repo_tag *rt = NULL, *trt = NULL;
643 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
644 TAILQ_REMOVE(&t->repo_commits, rc, entry);
645 gotweb_free_repo_commit(rc);
647 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
648 TAILQ_REMOVE(&t->repo_tags, rt, entry);
649 gotweb_free_repo_tag(rt);
651 gotweb_free_repo_dir(t->repo_dir);
652 gotweb_free_querystring(t->qs);
653 free(t->next_id);
654 free(t->prev_id);
655 free(t);
658 const struct got_error *
659 gotweb_render_content_type(struct request *c, const uint8_t *type)
661 const char *csp = "default-src 'self'; script-src 'none'; "
662 "object-src 'none';";
664 fcgi_printf(c,
665 "Content-Security-Policy: %s\r\n"
666 "Content-Type: %s\r\n\r\n",
667 csp, type);
668 return NULL;
671 const struct got_error *
672 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
673 char *file)
675 fcgi_printf(c, "Content-type: %s\r\n"
676 "Content-disposition: attachment; filename=%s\r\n\r\n",
677 type, file);
678 return NULL;
681 void
682 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
683 struct gotweb_url *next, int *have_next)
685 struct transport *t = c->t;
686 struct querystring *qs = t->qs;
687 struct server *srv = c->srv;
689 *have_prev = *have_next = 0;
691 switch(qs->action) {
692 case INDEX:
693 if (qs->index_page > 0) {
694 *have_prev = 1;
695 *prev = (struct gotweb_url){
696 .action = -1,
697 .index_page = qs->index_page - 1,
698 .page = -1,
699 };
701 if (t->next_disp == srv->max_repos_display &&
702 t->repos_total != (qs->index_page + 1) *
703 srv->max_repos_display) {
704 *have_next = 1;
705 *next = (struct gotweb_url){
706 .action = -1,
707 .index_page = qs->index_page + 1,
708 .page = -1,
709 };
711 break;
712 case BRIEFS:
713 if (t->prev_id && qs->commit != NULL &&
714 strcmp(qs->commit, t->prev_id) != 0) {
715 *have_prev = 1;
716 *prev = (struct gotweb_url){
717 .action = BRIEFS,
718 .index_page = -1,
719 .page = qs->page - 1,
720 .path = qs->path,
721 .commit = t->prev_id,
722 .headref = qs->headref,
723 };
725 if (t->next_id) {
726 *have_next = 1;
727 *next = (struct gotweb_url){
728 .action = BRIEFS,
729 .index_page = -1,
730 .page = qs->page + 1,
731 .path = qs->path,
732 .commit = t->next_id,
733 .headref = qs->headref,
734 };
736 break;
737 case COMMITS:
738 if (t->prev_id && qs->commit != NULL &&
739 strcmp(qs->commit, t->prev_id) != 0) {
740 *have_prev = 1;
741 *prev = (struct gotweb_url){
742 .action = COMMITS,
743 .index_page = -1,
744 .page = qs->page - 1,
745 .path = qs->path,
746 .commit = t->prev_id,
747 .headref = qs->headref,
748 .folder = qs->folder,
749 .file = qs->file,
750 };
752 if (t->next_id) {
753 *have_next = 1;
754 *next = (struct gotweb_url){
755 .action = COMMITS,
756 .index_page = -1,
757 .page = qs->page + 1,
758 .path = qs->path,
759 .commit = t->next_id,
760 .headref = qs->headref,
761 .folder = qs->folder,
762 .file = qs->file,
763 };
765 break;
766 case TAGS:
767 if (t->prev_id && qs->commit != NULL &&
768 strcmp(qs->commit, t->prev_id) != 0) {
769 *have_prev = 1;
770 *prev = (struct gotweb_url){
771 .action = TAGS,
772 .index_page = -1,
773 .page = qs->page - 1,
774 .path = qs->path,
775 .commit = t->prev_id,
776 .headref = qs->headref,
777 };
779 if (t->next_id) {
780 *have_next = 1;
781 *next = (struct gotweb_url){
782 .action = TAGS,
783 .index_page = -1,
784 .page = qs->page + 1,
785 .path = qs->path,
786 .commit = t->next_id,
787 .headref = qs->headref,
788 };
790 break;
794 static const struct got_error *
795 gotweb_render_index(struct request *c)
797 const struct got_error *error = NULL;
798 struct server *srv = c->srv;
799 struct transport *t = c->t;
800 struct querystring *qs = t->qs;
801 struct repo_dir *repo_dir = NULL;
802 DIR *d;
803 struct dirent **sd_dent = NULL;
804 unsigned int d_cnt, d_i, d_disp = 0;
805 unsigned int d_skipped = 0;
806 int type;
808 d = opendir(srv->repos_path);
809 if (d == NULL) {
810 error = got_error_from_errno2("opendir", srv->repos_path);
811 return error;
814 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
815 if (d_cnt == -1) {
816 sd_dent = NULL;
817 error = got_error_from_errno2("scandir", srv->repos_path);
818 goto done;
821 if (gotweb_render_repo_table_hdr(c->tp) == -1)
822 goto done;
824 for (d_i = 0; d_i < d_cnt; d_i++) {
825 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
826 break;
828 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
829 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
830 d_skipped++;
831 continue;
834 error = got_path_dirent_type(&type, srv->repos_path,
835 sd_dent[d_i]);
836 if (error)
837 goto done;
838 if (type != DT_DIR) {
839 d_skipped++;
840 continue;
843 if (qs->index_page > 0 && (qs->index_page *
844 srv->max_repos_display) > t->prev_disp) {
845 t->prev_disp++;
846 continue;
849 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
850 if (error)
851 goto done;
853 error = gotweb_load_got_path(c, repo_dir);
854 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
855 error = NULL;
856 gotweb_free_repo_dir(repo_dir);
857 repo_dir = NULL;
858 d_skipped++;
859 continue;
861 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
862 goto done;
864 d_disp++;
865 t->prev_disp++;
867 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
868 goto done;
870 gotweb_free_repo_dir(repo_dir);
871 repo_dir = NULL;
872 t->next_disp++;
873 if (d_disp == srv->max_repos_display)
874 break;
876 t->repos_total = d_cnt - d_skipped;
878 if (srv->max_repos_display == 0)
879 goto done;
880 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
881 goto done;
882 if (t->repos_total <= srv->max_repos ||
883 t->repos_total <= srv->max_repos_display)
884 goto done;
886 if (gotweb_render_navs(c->tp) == -1)
887 goto done;
888 done:
889 if (sd_dent) {
890 for (d_i = 0; d_i < d_cnt; d_i++)
891 free(sd_dent[d_i]);
892 free(sd_dent);
894 if (d != NULL && closedir(d) == EOF && error == NULL)
895 error = got_error_from_errno("closedir");
896 return error;
899 static const struct got_error *
900 gotweb_render_blame(struct request *c)
902 const struct got_error *error = NULL;
903 struct transport *t = c->t;
904 struct repo_commit *rc = NULL;
905 char *age = NULL, *msg = NULL;
906 int r;
908 error = got_get_repo_commits(c, 1);
909 if (error)
910 return error;
912 rc = TAILQ_FIRST(&t->repo_commits);
914 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
915 if (error)
916 goto done;
917 error = gotweb_escape_html(&msg, rc->commit_msg);
918 if (error)
919 goto done;
921 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
922 "<div id='blame_title'>Blame</div>\n"
923 "</div>\n" /* #blame_title_wrapper */
924 "<div id='blame_content'>\n"
925 "<div id='blame_header_wrapper'>\n"
926 "<div id='blame_header'>\n"
927 "<div class='header_age_title'>Date:</div>\n"
928 "<div class='header_age'>%s</div>\n"
929 "<div id='header_commit_msg_title'>Message:</div>\n"
930 "<div id='header_commit_msg'>%s</div>\n"
931 "</div>\n" /* #blame_header */
932 "</div>\n" /* #blame_header_wrapper */
933 "<div class='dotted_line'></div>\n"
934 "<div id='blame'>\n",
935 age,
936 msg);
937 if (r == -1)
938 goto done;
940 error = got_output_file_blame(c);
941 if (error)
942 goto done;
944 fcgi_printf(c, "</div>\n" /* #blame */
945 "</div>\n"); /* #blame_content */
946 done:
947 free(age);
948 free(msg);
949 return error;
952 static const struct got_error *
953 gotweb_render_branches(struct request *c)
955 const struct got_error *error = NULL;
956 struct got_reflist_head refs;
957 struct got_reflist_entry *re;
958 struct transport *t = c->t;
959 struct querystring *qs = t->qs;
960 struct got_repository *repo = t->repo;
961 char *escaped_refname = NULL;
962 char *age = NULL;
963 int r;
965 TAILQ_INIT(&refs);
967 error = got_ref_list(&refs, repo, "refs/heads",
968 got_ref_cmp_by_name, NULL);
969 if (error)
970 goto done;
972 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
973 "<div id='branches_title'>Branches</div>\n"
974 "</div>\n" /* #branches_title_wrapper */
975 "<div id='branches_content'>\n");
976 if (r == -1)
977 goto done;
979 TAILQ_FOREACH(re, &refs, entry) {
980 const char *refname = NULL;
982 if (got_ref_is_symbolic(re->ref))
983 continue;
985 refname = got_ref_get_name(re->ref);
986 if (refname == NULL) {
987 error = got_error_from_errno("strdup");
988 goto done;
990 if (strncmp(refname, "refs/heads/", 11) != 0)
991 continue;
993 error = got_get_repo_age(&age, c, refname, TM_DIFF);
994 if (error)
995 goto done;
997 if (strncmp(refname, "refs/heads/", 11) == 0)
998 refname += 11;
999 error = gotweb_escape_html(&escaped_refname, refname);
1000 if (error)
1001 goto done;
1003 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1004 "<div class='branches_age'>%s</div>\n"
1005 "<div class='branches_space'>&nbsp;</div>\n"
1006 "<div class='branch'>", age);
1007 if (r == -1)
1008 goto done;
1010 r = gotweb_link(c, &(struct gotweb_url){
1011 .action = SUMMARY,
1012 .index_page = -1,
1013 .page = -1,
1014 .path = qs->path,
1015 .headref = refname,
1016 }, "%s", escaped_refname);
1017 if (r == -1)
1018 goto done;
1020 if (fcgi_printf(c, "</div>\n" /* .branch */
1021 "<div class='navs_wrapper'>\n"
1022 "<div class='navs'>") == -1)
1023 goto done;
1025 r = gotweb_link(c, &(struct gotweb_url){
1026 .action = SUMMARY,
1027 .index_page = -1,
1028 .page = -1,
1029 .path = qs->path,
1030 .headref = refname,
1031 }, "summary");
1032 if (r == -1)
1033 goto done;
1035 if (fcgi_printf(c, " | ") == -1)
1036 goto done;
1038 r = gotweb_link(c, &(struct gotweb_url){
1039 .action = BRIEFS,
1040 .index_page = -1,
1041 .page = -1,
1042 .path = qs->path,
1043 .headref = refname,
1044 }, "commit briefs");
1045 if (r == -1)
1046 goto done;
1048 if (fcgi_printf(c, " | ") == -1)
1049 goto done;
1051 r = gotweb_link(c, &(struct gotweb_url){
1052 .action = COMMITS,
1053 .index_page = -1,
1054 .page = -1,
1055 .path = qs->path,
1056 .headref = refname,
1057 }, "commits");
1058 if (r == -1)
1059 goto done;
1061 r = fcgi_printf(c, "</div>\n" /* .navs */
1062 "</div>\n" /* .navs_wrapper */
1063 "<div class='dotted_line'></div>\n"
1064 "</div>\n"); /* .branches_wrapper */
1065 if (r == -1)
1066 goto done;
1068 free(age);
1069 age = NULL;
1070 free(escaped_refname);
1071 escaped_refname = NULL;
1073 fcgi_printf(c, "</div>\n"); /* #branches_content */
1074 done:
1075 free(age);
1076 free(escaped_refname);
1077 got_ref_list_free(&refs);
1078 return error;
1081 static const struct got_error *
1082 gotweb_render_tree(struct request *c)
1084 const struct got_error *error = NULL;
1085 struct transport *t = c->t;
1086 struct repo_commit *rc = NULL;
1087 char *age = NULL, *msg = NULL;
1088 int r;
1090 error = got_get_repo_commits(c, 1);
1091 if (error)
1092 return error;
1094 rc = TAILQ_FIRST(&t->repo_commits);
1096 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1097 if (error)
1098 goto done;
1100 error = gotweb_escape_html(&msg, rc->commit_msg);
1101 if (error)
1102 goto done;
1104 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1105 "<div id='tree_title'>Tree</div>\n"
1106 "</div>\n" /* #tree_title_wrapper */
1107 "<div id='tree_content'>\n"
1108 "<div id='tree_header_wrapper'>\n"
1109 "<div id='tree_header'>\n"
1110 "<div id='header_tree_title'>Tree:</div>\n"
1111 "<div id='header_tree'>%s</div>\n"
1112 "<div class='header_age_title'>Date:</div>\n"
1113 "<div class='header_age'>%s</div>\n"
1114 "<div id='header_commit_msg_title'>Message:</div>\n"
1115 "<div id='header_commit_msg'>%s</div>\n"
1116 "</div>\n" /* #tree_header */
1117 "</div>\n" /* #tree_header_wrapper */
1118 "<div class='dotted_line'></div>\n"
1119 "<div id='tree'>\n",
1120 rc->tree_id,
1121 age,
1122 msg);
1123 if (r == -1)
1124 goto done;
1126 error = got_output_repo_tree(c);
1127 if (error)
1128 goto done;
1130 fcgi_printf(c, "</div>\n"); /* #tree */
1131 fcgi_printf(c, "</div>\n"); /* #tree_content */
1132 done:
1133 free(age);
1134 free(msg);
1135 return error;
1138 static const struct got_error *
1139 gotweb_render_diff(struct request *c)
1141 const struct got_error *error = NULL;
1142 struct transport *t = c->t;
1143 struct repo_commit *rc = NULL;
1144 char *age = NULL, *author = NULL, *msg = NULL;
1145 int r;
1147 error = got_get_repo_commits(c, 1);
1148 if (error)
1149 return error;
1151 rc = TAILQ_FIRST(&t->repo_commits);
1153 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1154 if (error)
1155 goto done;
1156 error = gotweb_escape_html(&author, rc->author);
1157 if (error)
1158 goto done;
1159 error = gotweb_escape_html(&msg, rc->commit_msg);
1160 if (error)
1161 goto done;
1163 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1164 "<div id='diff_title'>Commit Diff</div>\n"
1165 "</div>\n" /* #diff_title_wrapper */
1166 "<div id='diff_content'>\n"
1167 "<div id='diff_header_wrapper'>\n"
1168 "<div id='diff_header'>\n"
1169 "<div id='header_diff_title'>Diff:</div>\n"
1170 "<div id='header_diff'>%s<br />%s</div>\n"
1171 "<div class='header_commit_title'>Commit:</div>\n"
1172 "<div class='header_commit'>%s</div>\n"
1173 "<div id='header_tree_title'>Tree:</div>\n"
1174 "<div id='header_tree'>%s</div>\n"
1175 "<div class='header_author_title'>Author:</div>\n"
1176 "<div class='header_author'>%s</div>\n"
1177 "<div class='header_age_title'>Date:</div>\n"
1178 "<div class='header_age'>%s</div>\n"
1179 "<div id='header_commit_msg_title'>Message:</div>\n"
1180 "<div id='header_commit_msg'>%s</div>\n"
1181 "</div>\n" /* #diff_header */
1182 "</div>\n" /* #diff_header_wrapper */
1183 "<div class='dotted_line'></div>\n"
1184 "<div id='diff'>\n",
1185 rc->parent_id, rc->commit_id,
1186 rc->commit_id,
1187 rc->tree_id,
1188 author,
1189 age,
1190 msg);
1191 if (r == -1)
1192 goto done;
1194 error = got_output_repo_diff(c);
1195 if (error)
1196 goto done;
1198 fcgi_printf(c, "</div>\n"); /* #diff */
1199 fcgi_printf(c, "</div>\n"); /* #diff_content */
1200 done:
1201 free(age);
1202 free(author);
1203 free(msg);
1204 return error;
1207 static const struct got_error *
1208 gotweb_render_summary(struct request *c)
1210 const struct got_error *error = NULL;
1211 struct transport *t = c->t;
1212 struct server *srv = c->srv;
1213 int r;
1215 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1216 goto done;
1218 if (srv->show_repo_description) {
1219 r = fcgi_printf(c,
1220 "<div id='description_title'>Description:</div>\n"
1221 "<div id='description'>%s</div>\n",
1222 t->repo_dir->description ? t->repo_dir->description : "");
1223 if (r == -1)
1224 goto done;
1227 if (srv->show_repo_owner) {
1228 r = fcgi_printf(c,
1229 "<div id='repo_owner_title'>Owner:</div>\n"
1230 "<div id='repo_owner'>%s</div>\n",
1231 t->repo_dir->owner ? t->repo_dir->owner : "");
1232 if (r == -1)
1233 goto done;
1236 if (srv->show_repo_age) {
1237 r = fcgi_printf(c,
1238 "<div id='last_change_title'>Last Change:</div>\n"
1239 "<div id='last_change'>%s</div>\n",
1240 t->repo_dir->age);
1241 if (r == -1)
1242 goto done;
1245 if (srv->show_repo_cloneurl) {
1246 r = fcgi_printf(c,
1247 "<div id='cloneurl_title'>Clone URL:</div>\n"
1248 "<div id='cloneurl'>%s</div>\n",
1249 t->repo_dir->url ? t->repo_dir->url : "");
1250 if (r == -1)
1251 goto done;
1254 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1255 if (r == -1)
1256 goto done;
1258 if (gotweb_render_briefs(c->tp) == -1)
1259 goto done;
1261 error = gotweb_render_tags(c);
1262 if (error) {
1263 log_warnx("%s: %s", __func__, error->msg);
1264 goto done;
1267 error = gotweb_render_branches(c);
1268 if (error)
1269 log_warnx("%s: %s", __func__, error->msg);
1270 done:
1271 return error;
1274 static const struct got_error *
1275 gotweb_render_tag(struct request *c)
1277 const struct got_error *error = NULL;
1278 struct repo_tag *rt = NULL;
1279 struct transport *t = c->t;
1280 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1282 error = got_get_repo_tags(c, 1);
1283 if (error)
1284 goto done;
1286 if (t->tag_count == 0) {
1287 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1288 "bad commit id");
1289 goto done;
1292 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1294 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1295 if (error)
1296 goto done;
1297 error = gotweb_escape_html(&author, rt->tagger);
1298 if (error)
1299 goto done;
1300 error = gotweb_escape_html(&msg, rt->commit_msg);
1301 if (error)
1302 goto done;
1304 tagname = rt->tag_name;
1305 if (strncmp(tagname, "refs/", 5) == 0)
1306 tagname += 5;
1307 error = gotweb_escape_html(&tagname, tagname);
1308 if (error)
1309 goto done;
1311 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1312 "<div id='tags_title'>Tag</div>\n"
1313 "</div>\n" /* #tags_title_wrapper */
1314 "<div id='tags_content'>\n"
1315 "<div id='tag_header_wrapper'>\n"
1316 "<div id='tag_header'>\n"
1317 "<div class='header_commit_title'>Commit:</div>\n"
1318 "<div class='header_commit'>%s"
1319 " <span class='refs_str'>(%s)</span></div>\n"
1320 "<div class='header_author_title'>Tagger:</div>\n"
1321 "<div class='header_author'>%s</div>\n"
1322 "<div class='header_age_title'>Date:</div>\n"
1323 "<div class='header_age'>%s</div>\n"
1324 "<div id='header_commit_msg_title'>Message:</div>\n"
1325 "<div id='header_commit_msg'>%s</div>\n"
1326 "</div>\n" /* #tag_header */
1327 "<div class='dotted_line'></div>\n"
1328 "<div id='tag_commit'>\n%s</div>"
1329 "</div>" /* #tag_header_wrapper */
1330 "</div>", /* #tags_content */
1331 rt->commit_id,
1332 tagname,
1333 author,
1334 age,
1335 msg,
1336 rt->tag_commit);
1338 done:
1339 free(age);
1340 free(author);
1341 free(msg);
1342 return error;
1345 static const struct got_error *
1346 gotweb_render_tags(struct request *c)
1348 const struct got_error *error = NULL;
1349 struct repo_tag *rt = NULL;
1350 struct server *srv = c->srv;
1351 struct transport *t = c->t;
1352 struct querystring *qs = t->qs;
1353 struct repo_dir *repo_dir = t->repo_dir;
1354 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1355 int r, commit_found = 0;
1357 if (qs->action == BRIEFS) {
1358 qs->action = TAGS;
1359 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1360 } else
1361 error = got_get_repo_tags(c, srv->max_commits_display);
1362 if (error)
1363 goto done;
1365 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1366 "<div id='tags_title'>Tags</div>\n"
1367 "</div>\n" /* #tags_title_wrapper */
1368 "<div id='tags_content'>\n");
1369 if (r == -1)
1370 goto done;
1372 if (t->tag_count == 0) {
1373 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1374 "This repository contains no tags");
1375 if (r == -1)
1376 goto done;
1379 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1380 if (commit_found == 0 && qs->commit != NULL) {
1381 if (strcmp(qs->commit, rt->commit_id) != 0)
1382 continue;
1383 else
1384 commit_found = 1;
1386 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1387 if (error)
1388 goto done;
1390 tagname = rt->tag_name;
1391 if (strncmp(tagname, "refs/tags/", 10) == 0)
1392 tagname += 10;
1393 error = gotweb_escape_html(&tagname, tagname);
1394 if (error)
1395 goto done;
1397 if (rt->tag_commit != NULL) {
1398 newline = strchr(rt->tag_commit, '\n');
1399 if (newline)
1400 *newline = '\0';
1401 error = gotweb_escape_html(&msg, rt->tag_commit);
1402 if (error)
1403 goto done;
1406 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1407 "<div class='tag'>%s</div>\n"
1408 "<div class='tag_log'>", age, tagname) == -1)
1409 goto done;
1411 r = gotweb_link(c, &(struct gotweb_url){
1412 .action = TAG,
1413 .index_page = -1,
1414 .page = -1,
1415 .path = repo_dir->name,
1416 .commit = rt->commit_id,
1417 }, "%s", msg ? msg : "");
1418 if (r == -1)
1419 goto done;
1421 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1422 "<div class='navs_wrapper'>\n"
1423 "<div class='navs'>") == -1)
1424 goto done;
1426 r = gotweb_link(c, &(struct gotweb_url){
1427 .action = TAG,
1428 .index_page = -1,
1429 .page = -1,
1430 .path = repo_dir->name,
1431 .commit = rt->commit_id,
1432 }, "tag");
1433 if (r == -1)
1434 goto done;
1436 if (fcgi_printf(c, " | ") == -1)
1437 goto done;
1439 r = gotweb_link(c, &(struct gotweb_url){
1440 .action = BRIEFS,
1441 .index_page = -1,
1442 .page = -1,
1443 .path = repo_dir->name,
1444 .commit = rt->commit_id,
1445 }, "commit briefs");
1446 if (r == -1)
1447 goto done;
1449 if (fcgi_printf(c, " | ") == -1)
1450 goto done;
1452 r = gotweb_link(c, &(struct gotweb_url){
1453 .action = COMMITS,
1454 .index_page = -1,
1455 .page = -1,
1456 .path = repo_dir->name,
1457 .commit = rt->commit_id,
1458 }, "commits");
1459 if (r == -1)
1460 goto done;
1462 r = fcgi_printf(c,
1463 "</div>\n" /* .navs */
1464 "</div>\n" /* .navs_wrapper */
1465 "<div class='dotted_line'></div>\n");
1466 if (r == -1)
1467 goto done;
1469 free(age);
1470 age = NULL;
1471 free(tagname);
1472 tagname = NULL;
1473 free(msg);
1474 msg = NULL;
1476 if (t->next_id || t->prev_id) {
1477 if (gotweb_render_navs(c->tp) == -1)
1478 goto done;
1480 fcgi_printf(c, "</div>\n"); /* #tags_content */
1481 done:
1482 free(age);
1483 free(tagname);
1484 free(msg);
1485 return error;
1488 const struct got_error *
1489 gotweb_escape_html(char **escaped_html, const char *orig_html)
1491 const struct got_error *error = NULL;
1492 struct escape_pair {
1493 char c;
1494 const char *s;
1495 } esc[] = {
1496 { '>', "&gt;" },
1497 { '<', "&lt;" },
1498 { '&', "&amp;" },
1499 { '"', "&quot;" },
1500 { '\'', "&apos;" },
1501 { '\n', "<br />" },
1503 size_t orig_len, len;
1504 int i, j, x;
1506 orig_len = strlen(orig_html);
1507 len = orig_len;
1508 for (i = 0; i < orig_len; i++) {
1509 for (j = 0; j < nitems(esc); j++) {
1510 if (orig_html[i] != esc[j].c)
1511 continue;
1512 len += strlen(esc[j].s) - 1 /* escaped char */;
1516 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1517 if (*escaped_html == NULL)
1518 return got_error_from_errno("calloc");
1520 x = 0;
1521 for (i = 0; i < orig_len; i++) {
1522 int escaped = 0;
1523 for (j = 0; j < nitems(esc); j++) {
1524 if (orig_html[i] != esc[j].c)
1525 continue;
1527 if (strlcat(*escaped_html, esc[j].s, len + 1)
1528 >= len + 1) {
1529 error = got_error(GOT_ERR_NO_SPACE);
1530 goto done;
1532 x += strlen(esc[j].s);
1533 escaped = 1;
1534 break;
1536 if (!escaped) {
1537 (*escaped_html)[x] = orig_html[i];
1538 x++;
1541 done:
1542 if (error) {
1543 free(*escaped_html);
1544 *escaped_html = NULL;
1545 } else {
1546 (*escaped_html)[x] = '\0';
1549 return error;
1552 static inline int
1553 should_urlencode(int c)
1555 if (c <= ' ' || c >= 127)
1556 return 1;
1558 switch (c) {
1559 /* gen-delim */
1560 case ':':
1561 case '/':
1562 case '?':
1563 case '#':
1564 case '[':
1565 case ']':
1566 case '@':
1567 /* sub-delims */
1568 case '!':
1569 case '$':
1570 case '&':
1571 case '\'':
1572 case '(':
1573 case ')':
1574 case '*':
1575 case '+':
1576 case ',':
1577 case ';':
1578 case '=':
1579 return 1;
1580 default:
1581 return 0;
1585 static char *
1586 gotweb_urlencode(const char *str)
1588 const char *s;
1589 char *escaped;
1590 size_t i, len;
1591 int a, b;
1593 len = 0;
1594 for (s = str; *s; ++s) {
1595 len++;
1596 if (should_urlencode(*s))
1597 len += 2;
1600 escaped = calloc(1, len + 1);
1601 if (escaped == NULL)
1602 return NULL;
1604 i = 0;
1605 for (s = str; *s; ++s) {
1606 if (should_urlencode(*s)) {
1607 a = (*s & 0xF0) >> 4;
1608 b = (*s & 0x0F);
1610 escaped[i++] = '%';
1611 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1612 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1613 } else
1614 escaped[i++] = *s;
1617 return escaped;
1620 const char *
1621 gotweb_action_name(int action)
1623 switch (action) {
1624 case BLAME:
1625 return "blame";
1626 case BLOB:
1627 return "blob";
1628 case BRIEFS:
1629 return "briefs";
1630 case COMMITS:
1631 return "commits";
1632 case DIFF:
1633 return "diff";
1634 case ERR:
1635 return "err";
1636 case INDEX:
1637 return "index";
1638 case SUMMARY:
1639 return "summary";
1640 case TAG:
1641 return "tag";
1642 case TAGS:
1643 return "tags";
1644 case TREE:
1645 return "tree";
1646 case RSS:
1647 return "rss";
1648 default:
1649 return NULL;
1653 int
1654 gotweb_render_url(struct request *c, struct gotweb_url *url)
1656 const char *sep = "?", *action;
1657 char *tmp;
1658 int r;
1660 action = gotweb_action_name(url->action);
1661 if (action != NULL) {
1662 if (fcgi_printf(c, "?action=%s", action) == -1)
1663 return -1;
1664 sep = "&";
1667 if (url->commit) {
1668 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1669 return -1;
1670 sep = "&";
1673 if (url->previd) {
1674 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1675 return -1;
1676 sep = "&";
1679 if (url->prevset) {
1680 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1681 return -1;
1682 sep = "&";
1685 if (url->file) {
1686 tmp = gotweb_urlencode(url->file);
1687 if (tmp == NULL)
1688 return -1;
1689 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1690 free(tmp);
1691 if (r == -1)
1692 return -1;
1693 sep = "&";
1696 if (url->folder) {
1697 tmp = gotweb_urlencode(url->folder);
1698 if (tmp == NULL)
1699 return -1;
1700 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1701 free(tmp);
1702 if (r == -1)
1703 return -1;
1704 sep = "&";
1707 if (url->headref) {
1708 tmp = gotweb_urlencode(url->headref);
1709 if (tmp == NULL)
1710 return -1;
1711 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1712 free(tmp);
1713 if (r == -1)
1714 return -1;
1715 sep = "&";
1718 if (url->index_page != -1) {
1719 if (fcgi_printf(c, "%sindex_page=%d", sep,
1720 url->index_page) == -1)
1721 return -1;
1722 sep = "&";
1725 if (url->path) {
1726 tmp = gotweb_urlencode(url->path);
1727 if (tmp == NULL)
1728 return -1;
1729 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1730 free(tmp);
1731 if (r == -1)
1732 return -1;
1733 sep = "&";
1736 if (url->page != -1) {
1737 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1738 return -1;
1739 sep = "&";
1742 return 0;
1745 int
1746 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1748 struct template *tp = c->tp;
1749 const char *proto = c->https ? "https" : "http";
1751 if (fcgi_puts(tp, proto) == -1 ||
1752 fcgi_puts(tp, "://") == -1 ||
1753 tp_htmlescape(tp, c->server_name) == -1 ||
1754 tp_htmlescape(tp, c->document_uri) == -1)
1755 return -1;
1757 return gotweb_render_url(c, url);
1760 int
1761 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1763 va_list ap;
1764 int r;
1766 if (fcgi_printf(c, "<a href='") == -1)
1767 return -1;
1769 if (gotweb_render_url(c, url) == -1)
1770 return -1;
1772 if (fcgi_printf(c, "'>") == -1)
1773 return -1;
1775 va_start(ap, fmt);
1776 r = fcgi_vprintf(c, fmt, ap);
1777 va_end(ap);
1778 if (r == -1)
1779 return -1;
1781 if (fcgi_printf(c, "</a>"))
1782 return -1;
1783 return 0;
1786 static struct got_repository *
1787 find_cached_repo(struct server *srv, const char *path)
1789 int i;
1791 for (i = 0; i < srv->ncached_repos; i++) {
1792 if (strcmp(srv->cached_repos[i].path, path) == 0)
1793 return srv->cached_repos[i].repo;
1796 return NULL;
1799 static const struct got_error *
1800 cache_repo(struct got_repository **new, struct server *srv,
1801 struct repo_dir *repo_dir, struct socket *sock)
1803 const struct got_error *error = NULL;
1804 struct got_repository *repo;
1805 struct cached_repo *cr;
1806 int evicted = 0;
1808 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1809 cr = &srv->cached_repos[srv->ncached_repos - 1];
1810 error = got_repo_close(cr->repo);
1811 memset(cr, 0, sizeof(*cr));
1812 srv->ncached_repos--;
1813 if (error)
1814 return error;
1815 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1816 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1817 cr = &srv->cached_repos[0];
1818 evicted = 1;
1819 } else {
1820 cr = &srv->cached_repos[srv->ncached_repos];
1823 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1824 if (error) {
1825 if (evicted) {
1826 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1827 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1829 return error;
1832 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1833 >= sizeof(cr->path)) {
1834 if (evicted) {
1835 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1836 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1838 return got_error(GOT_ERR_NO_SPACE);
1841 cr->repo = repo;
1842 srv->ncached_repos++;
1843 *new = repo;
1844 return NULL;
1847 static const struct got_error *
1848 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1850 const struct got_error *error = NULL;
1851 struct socket *sock = c->sock;
1852 struct server *srv = c->srv;
1853 struct transport *t = c->t;
1854 struct got_repository *repo = NULL;
1855 DIR *dt;
1856 char *dir_test;
1858 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1859 GOTWEB_GIT_DIR) == -1)
1860 return got_error_from_errno("asprintf");
1862 dt = opendir(dir_test);
1863 if (dt == NULL) {
1864 free(dir_test);
1865 } else {
1866 repo_dir->path = dir_test;
1867 dir_test = NULL;
1868 goto done;
1871 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1872 repo_dir->name) == -1)
1873 return got_error_from_errno("asprintf");
1875 dt = opendir(dir_test);
1876 if (dt == NULL) {
1877 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1878 goto err;
1879 } else {
1880 repo_dir->path = dir_test;
1881 dir_test = NULL;
1884 done:
1885 if (srv->respect_exportok &&
1886 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1887 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1888 goto err;
1891 repo = find_cached_repo(srv, repo_dir->path);
1892 if (repo == NULL) {
1893 error = cache_repo(&repo, srv, repo_dir, sock);
1894 if (error)
1895 goto err;
1897 t->repo = repo;
1898 error = gotweb_get_repo_description(&repo_dir->description, srv,
1899 repo_dir->path, dirfd(dt));
1900 if (error)
1901 goto err;
1902 error = got_get_repo_owner(&repo_dir->owner, c);
1903 if (error)
1904 goto err;
1905 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1906 if (error)
1907 goto err;
1908 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1909 dirfd(dt));
1910 err:
1911 free(dir_test);
1912 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1913 error = got_error_from_errno("closedir");
1914 return error;
1917 static const struct got_error *
1918 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1920 const struct got_error *error;
1922 *repo_dir = calloc(1, sizeof(**repo_dir));
1923 if (*repo_dir == NULL)
1924 return got_error_from_errno("calloc");
1926 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1927 error = got_error_from_errno("asprintf");
1928 free(*repo_dir);
1929 *repo_dir = NULL;
1930 return error;
1932 (*repo_dir)->owner = NULL;
1933 (*repo_dir)->description = NULL;
1934 (*repo_dir)->url = NULL;
1935 (*repo_dir)->age = NULL;
1936 (*repo_dir)->path = NULL;
1938 return NULL;
1941 static const struct got_error *
1942 gotweb_get_repo_description(char **description, struct server *srv,
1943 const char *dirpath, int dir)
1945 const struct got_error *error = NULL;
1946 struct stat sb;
1947 int fd = -1;
1948 off_t len;
1950 *description = NULL;
1951 if (srv->show_repo_description == 0)
1952 return NULL;
1954 fd = openat(dir, "description", O_RDONLY);
1955 if (fd == -1) {
1956 if (errno != ENOENT && errno != EACCES) {
1957 error = got_error_from_errno_fmt("openat %s/%s",
1958 dirpath, "description");
1960 goto done;
1963 if (fstat(fd, &sb) == -1) {
1964 error = got_error_from_errno_fmt("fstat %s/%s",
1965 dirpath, "description");
1966 goto done;
1969 len = sb.st_size;
1970 if (len > GOTWEBD_MAXDESCRSZ - 1)
1971 len = GOTWEBD_MAXDESCRSZ - 1;
1973 *description = calloc(len + 1, sizeof(**description));
1974 if (*description == NULL) {
1975 error = got_error_from_errno("calloc");
1976 goto done;
1979 if (read(fd, *description, len) == -1)
1980 error = got_error_from_errno("read");
1981 done:
1982 if (fd != -1 && close(fd) == -1 && error == NULL)
1983 error = got_error_from_errno("close");
1984 return error;
1987 static const struct got_error *
1988 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1989 int dir)
1991 const struct got_error *error = NULL;
1992 struct stat sb;
1993 int fd = -1;
1994 off_t len;
1996 *url = NULL;
1997 if (srv->show_repo_cloneurl == 0)
1998 return NULL;
2000 fd = openat(dir, "cloneurl", O_RDONLY);
2001 if (fd == -1) {
2002 if (errno != ENOENT && errno != EACCES) {
2003 error = got_error_from_errno_fmt("openat %s/%s",
2004 dirpath, "cloneurl");
2006 goto done;
2009 if (fstat(fd, &sb) == -1) {
2010 error = got_error_from_errno_fmt("fstat %s/%s",
2011 dirpath, "cloneurl");
2012 goto done;
2015 len = sb.st_size;
2016 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2017 len = GOTWEBD_MAXCLONEURLSZ - 1;
2019 *url = calloc(len + 1, sizeof(**url));
2020 if (*url == NULL) {
2021 error = got_error_from_errno("calloc");
2022 goto done;
2025 if (read(fd, *url, len) == -1)
2026 error = got_error_from_errno("read");
2027 done:
2028 if (fd != -1 && close(fd) == -1 && error == NULL)
2029 error = got_error_from_errno("close");
2030 return error;
2033 const struct got_error *
2034 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2036 struct tm tm;
2037 long long diff_time;
2038 const char *years = "years ago", *months = "months ago";
2039 const char *weeks = "weeks ago", *days = "days ago";
2040 const char *hours = "hours ago", *minutes = "minutes ago";
2041 const char *seconds = "seconds ago", *now = "right now";
2042 char *s;
2043 char datebuf[64];
2044 size_t r;
2046 *repo_age = NULL;
2048 switch (ref_tm) {
2049 case TM_DIFF:
2050 diff_time = time(NULL) - committer_time;
2051 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2052 if (asprintf(repo_age, "%lld %s",
2053 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2054 return got_error_from_errno("asprintf");
2055 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2056 if (asprintf(repo_age, "%lld %s",
2057 (diff_time / 60 / 60 / 24 / (365 / 12)),
2058 months) == -1)
2059 return got_error_from_errno("asprintf");
2060 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2061 if (asprintf(repo_age, "%lld %s",
2062 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2063 return got_error_from_errno("asprintf");
2064 } else if (diff_time > 60 * 60 * 24 * 2) {
2065 if (asprintf(repo_age, "%lld %s",
2066 (diff_time / 60 / 60 / 24), days) == -1)
2067 return got_error_from_errno("asprintf");
2068 } else if (diff_time > 60 * 60 * 2) {
2069 if (asprintf(repo_age, "%lld %s",
2070 (diff_time / 60 / 60), hours) == -1)
2071 return got_error_from_errno("asprintf");
2072 } else if (diff_time > 60 * 2) {
2073 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2074 minutes) == -1)
2075 return got_error_from_errno("asprintf");
2076 } else if (diff_time > 2) {
2077 if (asprintf(repo_age, "%lld %s", diff_time,
2078 seconds) == -1)
2079 return got_error_from_errno("asprintf");
2080 } else {
2081 if (asprintf(repo_age, "%s", now) == -1)
2082 return got_error_from_errno("asprintf");
2084 break;
2085 case TM_LONG:
2086 if (gmtime_r(&committer_time, &tm) == NULL)
2087 return got_error_from_errno("gmtime_r");
2089 s = asctime_r(&tm, datebuf);
2090 if (s == NULL)
2091 return got_error_from_errno("asctime_r");
2093 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2094 return got_error_from_errno("asprintf");
2095 break;
2096 case TM_RFC822:
2097 if (gmtime_r(&committer_time, &tm) == NULL)
2098 return got_error_from_errno("gmtime_r");
2100 r = strftime(datebuf, sizeof(datebuf),
2101 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2102 if (r == 0)
2103 return got_error(GOT_ERR_NO_SPACE);
2105 *repo_age = strdup(datebuf);
2106 if (*repo_age == NULL)
2107 return got_error_from_errno("asprintf");
2108 break;
2110 return NULL;