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 <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 static const struct querystring_keys querystring_keys[] = {
54 { "action", ACTION },
55 { "commit", COMMIT },
56 { "file", RFILE },
57 { "folder", FOLDER },
58 { "headref", HEADREF },
59 { "index_page", INDEX_PAGE },
60 { "path", PATH },
61 { "page", PAGE },
62 };
64 static const struct action_keys action_keys[] = {
65 { "blame", BLAME },
66 { "blob", BLOB },
67 { "briefs", BRIEFS },
68 { "commits", COMMITS },
69 { "diff", DIFF },
70 { "error", ERR },
71 { "index", INDEX },
72 { "summary", SUMMARY },
73 { "tag", TAG },
74 { "tags", TAGS },
75 { "tree", TREE },
76 };
78 static const struct got_error *gotweb_init_querystring(struct querystring **);
79 static const struct got_error *gotweb_parse_querystring(struct querystring **,
80 char *);
81 static const struct got_error *gotweb_assign_querystring(struct querystring **,
82 char *, char *);
83 static const struct got_error *gotweb_render_index(struct request *);
84 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
85 const char *);
86 static const struct got_error *gotweb_load_got_path(struct request *c,
87 struct repo_dir *);
88 static const struct got_error *gotweb_get_repo_description(char **,
89 struct server *, const char *, int);
90 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
91 const char *, int);
92 static const struct got_error *gotweb_render_blame(struct request *);
93 static const struct got_error *gotweb_render_diff(struct request *);
94 static const struct got_error *gotweb_render_summary(struct request *);
95 static const struct got_error *gotweb_render_tag(struct request *);
96 static const struct got_error *gotweb_render_tags(struct request *);
97 static const struct got_error *gotweb_render_tree(struct request *);
98 static const struct got_error *gotweb_render_branches(struct request *);
100 static void gotweb_free_querystring(struct querystring *);
101 static void gotweb_free_repo_dir(struct repo_dir *);
103 struct server *gotweb_get_server(uint8_t *, uint8_t *);
105 void
106 gotweb_process_request(struct request *c)
108 const struct got_error *error = NULL, *error2 = NULL;
109 struct server *srv = NULL;
110 struct querystring *qs = NULL;
111 struct repo_dir *repo_dir = NULL;
112 uint8_t err[] = "gotwebd experienced an error: ";
113 int r, html = 0;
115 /* init the transport */
116 error = gotweb_init_transport(&c->t);
117 if (error) {
118 log_warnx("%s: %s", __func__, error->msg);
119 return;
121 /* don't process any further if client disconnected */
122 if (c->sock->client_status == CLIENT_DISCONNECT)
123 return;
124 /* get the gotwebd server */
125 srv = gotweb_get_server(c->server_name, c->http_host);
126 if (srv == NULL) {
127 log_warnx("%s: error server is NULL", __func__);
128 goto err;
130 c->srv = srv;
131 /* parse our querystring */
132 error = gotweb_init_querystring(&qs);
133 if (error) {
134 log_warnx("%s: %s", __func__, error->msg);
135 goto err;
137 c->t->qs = qs;
138 error = gotweb_parse_querystring(&qs, c->querystring);
139 if (error) {
140 log_warnx("%s: %s", __func__, error->msg);
141 goto err;
144 /*
145 * certain actions require a commit id in the querystring. this stops
146 * bad actors from exploiting this by manually manipulating the
147 * querystring.
148 */
150 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
151 qs->action == DIFF)) {
152 error2 = got_error(GOT_ERR_QUERYSTRING);
153 goto render;
156 if (qs->action != INDEX) {
157 error = gotweb_init_repo_dir(&repo_dir, qs->path);
158 if (error)
159 goto done;
160 error = gotweb_load_got_path(c, repo_dir);
161 c->t->repo_dir = repo_dir;
162 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
163 goto err;
166 /* render top of page */
167 if (qs != NULL && qs->action == BLOB) {
168 error = got_get_repo_commits(c, 1);
169 if (error)
170 goto done;
171 error = got_output_file_blob(c);
172 if (error) {
173 log_warnx("%s: %s", __func__, error->msg);
174 goto err;
176 goto done;
179 render:
180 error = gotweb_render_content_type(c, "text/html");
181 if (error) {
182 log_warnx("%s: %s", __func__, error->msg);
183 goto err;
185 html = 1;
187 if (gotweb_render_header(c->tp) == -1)
188 goto err;
190 if (error2) {
191 error = error2;
192 goto err;
195 switch(qs->action) {
196 case BLAME:
197 error = gotweb_render_blame(c);
198 if (error) {
199 log_warnx("%s: %s", __func__, error->msg);
200 goto err;
202 break;
203 case BRIEFS:
204 if (gotweb_render_briefs(c->tp) == -1)
205 goto err;
206 break;
207 case COMMITS:
208 error = got_get_repo_commits(c, srv->max_commits_display);
209 if (error) {
210 log_warnx("%s: %s", __func__, error->msg);
211 goto err;
213 if (gotweb_render_commits(c->tp) == -1)
214 goto err;
215 break;
216 case DIFF:
217 error = gotweb_render_diff(c);
218 if (error) {
219 log_warnx("%s: %s", __func__, error->msg);
220 goto err;
222 break;
223 case INDEX:
224 error = gotweb_render_index(c);
225 if (error) {
226 log_warnx("%s: %s", __func__, error->msg);
227 goto err;
229 break;
230 case SUMMARY:
231 error = gotweb_render_summary(c);
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 break;
237 case TAG:
238 error = gotweb_render_tag(c);
239 if (error) {
240 log_warnx("%s: %s", __func__, error->msg);
241 goto err;
243 break;
244 case TAGS:
245 error = gotweb_render_tags(c);
246 if (error) {
247 log_warnx("%s: %s", __func__, error->msg);
248 goto err;
250 break;
251 case TREE:
252 error = gotweb_render_tree(c);
253 if (error) {
254 log_warnx("%s: %s", __func__, error->msg);
255 goto err;
257 break;
258 case ERR:
259 default:
260 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
261 "Erorr: Bad Querystring");
262 if (r == -1)
263 goto err;
264 break;
267 goto done;
268 err:
269 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
270 return;
271 if (fcgi_printf(c, "\n%s", err) == -1)
272 return;
273 if (error) {
274 if (fcgi_printf(c, "%s", error->msg) == -1)
275 return;
276 } else {
277 if (fcgi_printf(c, "see daemon logs for details") == -1)
278 return;
280 if (html && fcgi_printf(c, "</div>\n") == -1)
281 return;
282 done:
283 if (html && srv != NULL)
284 gotweb_render_footer(c->tp);
287 struct server *
288 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
290 struct server *srv = NULL;
292 /* check against the server name first */
293 if (strlen(server_name) > 0)
294 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
295 if (strcmp(srv->name, server_name) == 0)
296 goto done;
298 /* check against subdomain second */
299 if (strlen(subdomain) > 0)
300 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
301 if (strcmp(srv->name, subdomain) == 0)
302 goto done;
304 /* if those fail, send first server */
305 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
306 if (srv != NULL)
307 break;
308 done:
309 return srv;
310 };
312 const struct got_error *
313 gotweb_init_transport(struct transport **t)
315 const struct got_error *error = NULL;
317 *t = calloc(1, sizeof(**t));
318 if (*t == NULL)
319 return got_error_from_errno2("%s: calloc", __func__);
321 TAILQ_INIT(&(*t)->repo_commits);
322 TAILQ_INIT(&(*t)->repo_tags);
324 (*t)->repo = NULL;
325 (*t)->repo_dir = NULL;
326 (*t)->qs = NULL;
327 (*t)->next_id = NULL;
328 (*t)->prev_id = NULL;
329 (*t)->next_disp = 0;
330 (*t)->prev_disp = 0;
332 return error;
335 static const struct got_error *
336 gotweb_init_querystring(struct querystring **qs)
338 const struct got_error *error = NULL;
340 *qs = calloc(1, sizeof(**qs));
341 if (*qs == NULL)
342 return got_error_from_errno2("%s: calloc", __func__);
344 (*qs)->headref = strdup("HEAD");
345 if ((*qs)->headref == NULL) {
346 free(*qs);
347 *qs = NULL;
348 return got_error_from_errno2("%s: strdup", __func__);
351 (*qs)->action = INDEX;
352 (*qs)->commit = NULL;
353 (*qs)->file = NULL;
354 (*qs)->folder = NULL;
355 (*qs)->index_page = 0;
356 (*qs)->path = NULL;
358 return error;
361 static const struct got_error *
362 gotweb_parse_querystring(struct querystring **qs, char *qst)
364 const struct got_error *error = NULL;
365 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
366 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
368 if (qst == NULL)
369 return error;
371 tok1 = strdup(qst);
372 if (tok1 == NULL)
373 return got_error_from_errno2("%s: strdup", __func__);
375 tok1_pair = tok1;
376 tok1_end = tok1;
378 while (tok1_pair != NULL) {
379 strsep(&tok1_end, "&");
381 tok2 = strdup(tok1_pair);
382 if (tok2 == NULL) {
383 free(tok1);
384 return got_error_from_errno2("%s: strdup", __func__);
387 tok2_pair = tok2;
388 tok2_end = tok2;
390 while (tok2_pair != NULL) {
391 strsep(&tok2_end, "=");
392 if (tok2_end) {
393 error = gotweb_assign_querystring(qs, tok2_pair,
394 tok2_end);
395 if (error)
396 goto err;
398 tok2_pair = tok2_end;
400 free(tok2);
401 tok1_pair = tok1_end;
403 free(tok1);
404 return error;
405 err:
406 free(tok2);
407 free(tok1);
408 return error;
411 /*
412 * Adapted from usr.sbin/httpd/httpd.c url_decode.
413 */
414 static const struct got_error *
415 gotweb_urldecode(char *url)
417 char *p, *q;
418 char hex[3];
419 unsigned long x;
421 hex[2] = '\0';
422 p = q = url;
424 while (*p != '\0') {
425 switch (*p) {
426 case '%':
427 /* Encoding character is followed by two hex chars */
428 if (!isxdigit((unsigned char)p[1]) ||
429 !isxdigit((unsigned char)p[2]) ||
430 (p[1] == '0' && p[2] == '0'))
431 return got_error(GOT_ERR_BAD_QUERYSTRING);
433 hex[0] = p[1];
434 hex[1] = p[2];
436 /*
437 * We don't have to validate "hex" because it is
438 * guaranteed to include two hex chars followed by nul.
439 */
440 x = strtoul(hex, NULL, 16);
441 *q = (char)x;
442 p += 2;
443 break;
444 default:
445 *q = *p;
446 break;
448 p++;
449 q++;
451 *q = '\0';
453 return NULL;
456 static const struct got_error *
457 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
459 const struct got_error *error = NULL;
460 const char *errstr;
461 int a_cnt, el_cnt;
463 error = gotweb_urldecode(value);
464 if (error)
465 return error;
467 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
468 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
469 continue;
471 switch (querystring_keys[el_cnt].element) {
472 case ACTION:
473 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
474 if (strcmp(value, action_keys[a_cnt].name) != 0)
475 continue;
476 else if (strcmp(value,
477 action_keys[a_cnt].name) == 0){
478 (*qs)->action =
479 action_keys[a_cnt].action;
480 goto qa_found;
483 (*qs)->action = ERR;
484 qa_found:
485 break;
486 case COMMIT:
487 (*qs)->commit = strdup(value);
488 if ((*qs)->commit == NULL) {
489 error = got_error_from_errno2("%s: strdup",
490 __func__);
491 goto done;
493 break;
494 case RFILE:
495 (*qs)->file = strdup(value);
496 if ((*qs)->file == NULL) {
497 error = got_error_from_errno2("%s: strdup",
498 __func__);
499 goto done;
501 break;
502 case FOLDER:
503 (*qs)->folder = strdup(value);
504 if ((*qs)->folder == NULL) {
505 error = got_error_from_errno2("%s: strdup",
506 __func__);
507 goto done;
509 break;
510 case HEADREF:
511 free((*qs)->headref);
512 (*qs)->headref = strdup(value);
513 if ((*qs)->headref == NULL) {
514 error = got_error_from_errno2("%s: strdup",
515 __func__);
516 goto done;
518 break;
519 case INDEX_PAGE:
520 if (strlen(value) == 0)
521 break;
522 (*qs)->index_page = strtonum(value, INT64_MIN,
523 INT64_MAX, &errstr);
524 if (errstr) {
525 error = got_error_from_errno3("%s: strtonum %s",
526 __func__, errstr);
527 goto done;
529 if ((*qs)->index_page < 0)
530 (*qs)->index_page = 0;
531 break;
532 case PATH:
533 (*qs)->path = strdup(value);
534 if ((*qs)->path == NULL) {
535 error = got_error_from_errno2("%s: strdup",
536 __func__);
537 goto done;
539 break;
540 case PAGE:
541 if (strlen(value) == 0)
542 break;
543 (*qs)->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)->page < 0)
551 (*qs)->page = 0;
552 break;
553 default:
554 break;
557 done:
558 return error;
561 void
562 gotweb_free_repo_tag(struct repo_tag *rt)
564 if (rt != NULL) {
565 free(rt->commit_id);
566 free(rt->tag_name);
567 free(rt->tag_commit);
568 free(rt->commit_msg);
569 free(rt->tagger);
571 free(rt);
574 void
575 gotweb_free_repo_commit(struct repo_commit *rc)
577 if (rc != NULL) {
578 free(rc->path);
579 free(rc->refs_str);
580 free(rc->commit_id);
581 free(rc->parent_id);
582 free(rc->tree_id);
583 free(rc->author);
584 free(rc->committer);
585 free(rc->commit_msg);
587 free(rc);
590 static void
591 gotweb_free_querystring(struct querystring *qs)
593 if (qs != NULL) {
594 free(qs->commit);
595 free(qs->file);
596 free(qs->folder);
597 free(qs->headref);
598 free(qs->path);
600 free(qs);
603 static void
604 gotweb_free_repo_dir(struct repo_dir *repo_dir)
606 if (repo_dir != NULL) {
607 free(repo_dir->name);
608 free(repo_dir->owner);
609 free(repo_dir->description);
610 free(repo_dir->url);
611 free(repo_dir->age);
612 free(repo_dir->path);
614 free(repo_dir);
617 void
618 gotweb_free_transport(struct transport *t)
620 struct repo_commit *rc = NULL, *trc = NULL;
621 struct repo_tag *rt = NULL, *trt = NULL;
623 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
624 TAILQ_REMOVE(&t->repo_commits, rc, entry);
625 gotweb_free_repo_commit(rc);
627 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
628 TAILQ_REMOVE(&t->repo_tags, rt, entry);
629 gotweb_free_repo_tag(rt);
631 gotweb_free_repo_dir(t->repo_dir);
632 gotweb_free_querystring(t->qs);
633 free(t->next_id);
634 free(t->prev_id);
635 free(t);
638 const struct got_error *
639 gotweb_render_content_type(struct request *c, const uint8_t *type)
641 const char *csp = "default-src 'self'; script-src 'none'; "
642 "object-src 'none';";
644 fcgi_printf(c,
645 "Content-Security-Policy: %s\r\n"
646 "Content-Type: %s\r\n\r\n",
647 csp, type);
648 return NULL;
651 const struct got_error *
652 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
653 char *file)
655 fcgi_printf(c, "Content-type: %s\r\n"
656 "Content-disposition: attachment; filename=%s\r\n\r\n",
657 type, file);
658 return NULL;
661 void
662 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
663 struct gotweb_url *next, int *have_next)
665 struct transport *t = c->t;
666 struct querystring *qs = t->qs;
667 struct server *srv = c->srv;
669 *have_prev = *have_next = 0;
671 switch(qs->action) {
672 case INDEX:
673 if (qs->index_page > 0) {
674 *have_prev = 1;
675 *prev = (struct gotweb_url){
676 .action = -1,
677 .index_page = qs->index_page - 1,
678 .page = -1,
679 };
681 if (t->next_disp == srv->max_repos_display &&
682 t->repos_total != (qs->index_page + 1) *
683 srv->max_repos_display) {
684 *have_next = 1;
685 *next = (struct gotweb_url){
686 .action = -1,
687 .index_page = qs->index_page + 1,
688 .page = -1,
689 };
691 break;
692 case BRIEFS:
693 if (t->prev_id && qs->commit != NULL &&
694 strcmp(qs->commit, t->prev_id) != 0) {
695 *have_prev = 1;
696 *prev = (struct gotweb_url){
697 .action = BRIEFS,
698 .index_page = -1,
699 .page = qs->page - 1,
700 .path = qs->path,
701 .commit = t->prev_id,
702 .headref = qs->headref,
703 };
705 if (t->next_id) {
706 *have_next = 1;
707 *next = (struct gotweb_url){
708 .action = BRIEFS,
709 .index_page = -1,
710 .page = qs->page + 1,
711 .path = qs->path,
712 .commit = t->next_id,
713 .headref = qs->headref,
714 };
716 break;
717 case COMMITS:
718 if (t->prev_id && qs->commit != NULL &&
719 strcmp(qs->commit, t->prev_id) != 0) {
720 *have_prev = 1;
721 *prev = (struct gotweb_url){
722 .action = COMMITS,
723 .index_page = -1,
724 .page = qs->page - 1,
725 .path = qs->path,
726 .commit = t->prev_id,
727 .headref = qs->headref,
728 .folder = qs->folder,
729 .file = qs->file,
730 };
732 if (t->next_id) {
733 *have_next = 1;
734 *next = (struct gotweb_url){
735 .action = COMMITS,
736 .index_page = -1,
737 .page = qs->page + 1,
738 .path = qs->path,
739 .commit = t->next_id,
740 .headref = qs->headref,
741 .folder = qs->folder,
742 .file = qs->file,
743 };
745 break;
746 case TAGS:
747 if (t->prev_id && qs->commit != NULL &&
748 strcmp(qs->commit, t->prev_id) != 0) {
749 *have_prev = 1;
750 *prev = (struct gotweb_url){
751 .action = TAGS,
752 .index_page = -1,
753 .page = qs->page - 1,
754 .path = qs->path,
755 .commit = t->prev_id,
756 .headref = qs->headref,
757 };
759 if (t->next_id) {
760 *have_next = 1;
761 *next = (struct gotweb_url){
762 .action = TAGS,
763 .index_page = -1,
764 .page = qs->page + 1,
765 .path = qs->path,
766 .commit = t->next_id,
767 .headref = qs->headref,
768 };
770 break;
774 static const struct got_error *
775 gotweb_render_index(struct request *c)
777 const struct got_error *error = NULL;
778 struct server *srv = c->srv;
779 struct transport *t = c->t;
780 struct querystring *qs = t->qs;
781 struct repo_dir *repo_dir = NULL;
782 DIR *d;
783 struct dirent **sd_dent = NULL;
784 unsigned int d_cnt, d_i, d_disp = 0;
785 unsigned int d_skipped = 0;
786 int type;
788 d = opendir(srv->repos_path);
789 if (d == NULL) {
790 error = got_error_from_errno2("opendir", srv->repos_path);
791 return error;
794 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
795 if (d_cnt == -1) {
796 sd_dent = NULL;
797 error = got_error_from_errno2("scandir", srv->repos_path);
798 goto done;
801 if (gotweb_render_repo_table_hdr(c->tp) == -1)
802 goto done;
804 for (d_i = 0; d_i < d_cnt; d_i++) {
805 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
806 break;
808 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
809 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
810 d_skipped++;
811 continue;
814 error = got_path_dirent_type(&type, srv->repos_path,
815 sd_dent[d_i]);
816 if (error)
817 goto done;
818 if (type != DT_DIR) {
819 d_skipped++;
820 continue;
823 if (qs->index_page > 0 && (qs->index_page *
824 srv->max_repos_display) > t->prev_disp) {
825 t->prev_disp++;
826 continue;
829 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
830 if (error)
831 goto done;
833 error = gotweb_load_got_path(c, repo_dir);
834 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
835 error = NULL;
836 gotweb_free_repo_dir(repo_dir);
837 repo_dir = NULL;
838 d_skipped++;
839 continue;
841 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
842 goto done;
844 d_disp++;
845 t->prev_disp++;
847 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
848 goto done;
850 gotweb_free_repo_dir(repo_dir);
851 repo_dir = NULL;
852 t->next_disp++;
853 if (d_disp == srv->max_repos_display)
854 break;
856 t->repos_total = d_cnt - d_skipped;
858 if (srv->max_repos_display == 0)
859 goto done;
860 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
861 goto done;
862 if (t->repos_total <= srv->max_repos ||
863 t->repos_total <= srv->max_repos_display)
864 goto done;
866 if (gotweb_render_navs(c->tp) == -1)
867 goto done;
868 done:
869 if (sd_dent) {
870 for (d_i = 0; d_i < d_cnt; d_i++)
871 free(sd_dent[d_i]);
872 free(sd_dent);
874 if (d != NULL && closedir(d) == EOF && error == NULL)
875 error = got_error_from_errno("closedir");
876 return error;
879 static const struct got_error *
880 gotweb_render_blame(struct request *c)
882 const struct got_error *error = NULL;
883 struct transport *t = c->t;
884 struct repo_commit *rc = NULL;
885 char *age = NULL, *msg = NULL;
886 int r;
888 error = got_get_repo_commits(c, 1);
889 if (error)
890 return error;
892 rc = TAILQ_FIRST(&t->repo_commits);
894 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
895 if (error)
896 goto done;
897 error = gotweb_escape_html(&msg, rc->commit_msg);
898 if (error)
899 goto done;
901 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
902 "<div id='blame_title'>Blame</div>\n"
903 "</div>\n" /* #blame_title_wrapper */
904 "<div id='blame_content'>\n"
905 "<div id='blame_header_wrapper'>\n"
906 "<div id='blame_header'>\n"
907 "<div class='header_age_title'>Date:</div>\n"
908 "<div class='header_age'>%s</div>\n"
909 "<div id='header_commit_msg_title'>Message:</div>\n"
910 "<div id='header_commit_msg'>%s</div>\n"
911 "</div>\n" /* #blame_header */
912 "</div>\n" /* #blame_header_wrapper */
913 "<div class='dotted_line'></div>\n"
914 "<div id='blame'>\n",
915 age,
916 msg);
917 if (r == -1)
918 goto done;
920 error = got_output_file_blame(c);
921 if (error)
922 goto done;
924 fcgi_printf(c, "</div>\n" /* #blame */
925 "</div>\n"); /* #blame_content */
926 done:
927 free(age);
928 free(msg);
929 return error;
932 static const struct got_error *
933 gotweb_render_branches(struct request *c)
935 const struct got_error *error = NULL;
936 struct got_reflist_head refs;
937 struct got_reflist_entry *re;
938 struct transport *t = c->t;
939 struct querystring *qs = t->qs;
940 struct got_repository *repo = t->repo;
941 char *escaped_refname = NULL;
942 char *age = NULL;
943 int r;
945 TAILQ_INIT(&refs);
947 error = got_ref_list(&refs, repo, "refs/heads",
948 got_ref_cmp_by_name, NULL);
949 if (error)
950 goto done;
952 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
953 "<div id='branches_title'>Branches</div>\n"
954 "</div>\n" /* #branches_title_wrapper */
955 "<div id='branches_content'>\n");
956 if (r == -1)
957 goto done;
959 TAILQ_FOREACH(re, &refs, entry) {
960 const char *refname = NULL;
962 if (got_ref_is_symbolic(re->ref))
963 continue;
965 refname = got_ref_get_name(re->ref);
966 if (refname == NULL) {
967 error = got_error_from_errno("strdup");
968 goto done;
970 if (strncmp(refname, "refs/heads/", 11) != 0)
971 continue;
973 error = got_get_repo_age(&age, c, refname, TM_DIFF);
974 if (error)
975 goto done;
977 if (strncmp(refname, "refs/heads/", 11) == 0)
978 refname += 11;
979 error = gotweb_escape_html(&escaped_refname, refname);
980 if (error)
981 goto done;
983 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
984 "<div class='branches_age'>%s</div>\n"
985 "<div class='branches_space'>&nbsp;</div>\n"
986 "<div class='branch'>", age);
987 if (r == -1)
988 goto done;
990 r = gotweb_link(c, &(struct gotweb_url){
991 .action = SUMMARY,
992 .index_page = -1,
993 .page = -1,
994 .path = qs->path,
995 .headref = refname,
996 }, "%s", escaped_refname);
997 if (r == -1)
998 goto done;
1000 if (fcgi_printf(c, "</div>\n" /* .branch */
1001 "<div class='navs_wrapper'>\n"
1002 "<div class='navs'>") == -1)
1003 goto done;
1005 r = gotweb_link(c, &(struct gotweb_url){
1006 .action = SUMMARY,
1007 .index_page = -1,
1008 .page = -1,
1009 .path = qs->path,
1010 .headref = refname,
1011 }, "summary");
1012 if (r == -1)
1013 goto done;
1015 if (fcgi_printf(c, " | ") == -1)
1016 goto done;
1018 r = gotweb_link(c, &(struct gotweb_url){
1019 .action = BRIEFS,
1020 .index_page = -1,
1021 .page = -1,
1022 .path = qs->path,
1023 .headref = refname,
1024 }, "commit briefs");
1025 if (r == -1)
1026 goto done;
1028 if (fcgi_printf(c, " | ") == -1)
1029 goto done;
1031 r = gotweb_link(c, &(struct gotweb_url){
1032 .action = COMMITS,
1033 .index_page = -1,
1034 .page = -1,
1035 .path = qs->path,
1036 .headref = refname,
1037 }, "commits");
1038 if (r == -1)
1039 goto done;
1041 r = fcgi_printf(c, "</div>\n" /* .navs */
1042 "</div>\n" /* .navs_wrapper */
1043 "<div class='dotted_line'></div>\n"
1044 "</div>\n"); /* .branches_wrapper */
1045 if (r == -1)
1046 goto done;
1048 free(age);
1049 age = NULL;
1050 free(escaped_refname);
1051 escaped_refname = NULL;
1053 fcgi_printf(c, "</div>\n"); /* #branches_content */
1054 done:
1055 free(age);
1056 free(escaped_refname);
1057 got_ref_list_free(&refs);
1058 return error;
1061 static const struct got_error *
1062 gotweb_render_tree(struct request *c)
1064 const struct got_error *error = NULL;
1065 struct transport *t = c->t;
1066 struct repo_commit *rc = NULL;
1067 char *age = NULL, *msg = NULL;
1068 int r;
1070 error = got_get_repo_commits(c, 1);
1071 if (error)
1072 return error;
1074 rc = TAILQ_FIRST(&t->repo_commits);
1076 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1077 if (error)
1078 goto done;
1080 error = gotweb_escape_html(&msg, rc->commit_msg);
1081 if (error)
1082 goto done;
1084 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1085 "<div id='tree_title'>Tree</div>\n"
1086 "</div>\n" /* #tree_title_wrapper */
1087 "<div id='tree_content'>\n"
1088 "<div id='tree_header_wrapper'>\n"
1089 "<div id='tree_header'>\n"
1090 "<div id='header_tree_title'>Tree:</div>\n"
1091 "<div id='header_tree'>%s</div>\n"
1092 "<div class='header_age_title'>Date:</div>\n"
1093 "<div class='header_age'>%s</div>\n"
1094 "<div id='header_commit_msg_title'>Message:</div>\n"
1095 "<div id='header_commit_msg'>%s</div>\n"
1096 "</div>\n" /* #tree_header */
1097 "</div>\n" /* #tree_header_wrapper */
1098 "<div class='dotted_line'></div>\n"
1099 "<div id='tree'>\n",
1100 rc->tree_id,
1101 age,
1102 msg);
1103 if (r == -1)
1104 goto done;
1106 error = got_output_repo_tree(c);
1107 if (error)
1108 goto done;
1110 fcgi_printf(c, "</div>\n"); /* #tree */
1111 fcgi_printf(c, "</div>\n"); /* #tree_content */
1112 done:
1113 free(age);
1114 free(msg);
1115 return error;
1118 static const struct got_error *
1119 gotweb_render_diff(struct request *c)
1121 const struct got_error *error = NULL;
1122 struct transport *t = c->t;
1123 struct repo_commit *rc = NULL;
1124 char *age = NULL, *author = NULL, *msg = NULL;
1125 int r;
1127 error = got_get_repo_commits(c, 1);
1128 if (error)
1129 return error;
1131 rc = TAILQ_FIRST(&t->repo_commits);
1133 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1134 if (error)
1135 goto done;
1136 error = gotweb_escape_html(&author, rc->author);
1137 if (error)
1138 goto done;
1139 error = gotweb_escape_html(&msg, rc->commit_msg);
1140 if (error)
1141 goto done;
1143 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1144 "<div id='diff_title'>Commit Diff</div>\n"
1145 "</div>\n" /* #diff_title_wrapper */
1146 "<div id='diff_content'>\n"
1147 "<div id='diff_header_wrapper'>\n"
1148 "<div id='diff_header'>\n"
1149 "<div id='header_diff_title'>Diff:</div>\n"
1150 "<div id='header_diff'>%s<br />%s</div>\n"
1151 "<div class='header_commit_title'>Commit:</div>\n"
1152 "<div class='header_commit'>%s</div>\n"
1153 "<div id='header_tree_title'>Tree:</div>\n"
1154 "<div id='header_tree'>%s</div>\n"
1155 "<div class='header_author_title'>Author:</div>\n"
1156 "<div class='header_author'>%s</div>\n"
1157 "<div class='header_age_title'>Date:</div>\n"
1158 "<div class='header_age'>%s</div>\n"
1159 "<div id='header_commit_msg_title'>Message:</div>\n"
1160 "<div id='header_commit_msg'>%s</div>\n"
1161 "</div>\n" /* #diff_header */
1162 "</div>\n" /* #diff_header_wrapper */
1163 "<div class='dotted_line'></div>\n"
1164 "<div id='diff'>\n",
1165 rc->parent_id, rc->commit_id,
1166 rc->commit_id,
1167 rc->tree_id,
1168 author,
1169 age,
1170 msg);
1171 if (r == -1)
1172 goto done;
1174 error = got_output_repo_diff(c);
1175 if (error)
1176 goto done;
1178 fcgi_printf(c, "</div>\n"); /* #diff */
1179 fcgi_printf(c, "</div>\n"); /* #diff_content */
1180 done:
1181 free(age);
1182 free(author);
1183 free(msg);
1184 return error;
1187 static const struct got_error *
1188 gotweb_render_summary(struct request *c)
1190 const struct got_error *error = NULL;
1191 struct transport *t = c->t;
1192 struct server *srv = c->srv;
1193 int r;
1195 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1196 goto done;
1198 if (srv->show_repo_description) {
1199 r = fcgi_printf(c,
1200 "<div id='description_title'>Description:</div>\n"
1201 "<div id='description'>%s</div>\n",
1202 t->repo_dir->description ? t->repo_dir->description : "");
1203 if (r == -1)
1204 goto done;
1207 if (srv->show_repo_owner) {
1208 r = fcgi_printf(c,
1209 "<div id='repo_owner_title'>Owner:</div>\n"
1210 "<div id='repo_owner'>%s</div>\n",
1211 t->repo_dir->owner ? t->repo_dir->owner : "");
1212 if (r == -1)
1213 goto done;
1216 if (srv->show_repo_age) {
1217 r = fcgi_printf(c,
1218 "<div id='last_change_title'>Last Change:</div>\n"
1219 "<div id='last_change'>%s</div>\n",
1220 t->repo_dir->age);
1221 if (r == -1)
1222 goto done;
1225 if (srv->show_repo_cloneurl) {
1226 r = fcgi_printf(c,
1227 "<div id='cloneurl_title'>Clone URL:</div>\n"
1228 "<div id='cloneurl'>%s</div>\n",
1229 t->repo_dir->url ? t->repo_dir->url : "");
1230 if (r == -1)
1231 goto done;
1234 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1235 if (r == -1)
1236 goto done;
1238 if (gotweb_render_briefs(c->tp) == -1)
1239 goto done;
1241 error = gotweb_render_tags(c);
1242 if (error) {
1243 log_warnx("%s: %s", __func__, error->msg);
1244 goto done;
1247 error = gotweb_render_branches(c);
1248 if (error)
1249 log_warnx("%s: %s", __func__, error->msg);
1250 done:
1251 return error;
1254 static const struct got_error *
1255 gotweb_render_tag(struct request *c)
1257 const struct got_error *error = NULL;
1258 struct repo_tag *rt = NULL;
1259 struct transport *t = c->t;
1260 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1262 error = got_get_repo_tags(c, 1);
1263 if (error)
1264 goto done;
1266 if (t->tag_count == 0) {
1267 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1268 "bad commit id");
1269 goto done;
1272 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1274 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1275 if (error)
1276 goto done;
1277 error = gotweb_escape_html(&author, rt->tagger);
1278 if (error)
1279 goto done;
1280 error = gotweb_escape_html(&msg, rt->commit_msg);
1281 if (error)
1282 goto done;
1284 tagname = rt->tag_name;
1285 if (strncmp(tagname, "refs/", 5) == 0)
1286 tagname += 5;
1287 error = gotweb_escape_html(&tagname, tagname);
1288 if (error)
1289 goto done;
1291 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1292 "<div id='tags_title'>Tag</div>\n"
1293 "</div>\n" /* #tags_title_wrapper */
1294 "<div id='tags_content'>\n"
1295 "<div id='tag_header_wrapper'>\n"
1296 "<div id='tag_header'>\n"
1297 "<div class='header_commit_title'>Commit:</div>\n"
1298 "<div class='header_commit'>%s"
1299 " <span class='refs_str'>(%s)</span></div>\n"
1300 "<div class='header_author_title'>Tagger:</div>\n"
1301 "<div class='header_author'>%s</div>\n"
1302 "<div class='header_age_title'>Date:</div>\n"
1303 "<div class='header_age'>%s</div>\n"
1304 "<div id='header_commit_msg_title'>Message:</div>\n"
1305 "<div id='header_commit_msg'>%s</div>\n"
1306 "</div>\n" /* #tag_header */
1307 "<div class='dotted_line'></div>\n"
1308 "<div id='tag_commit'>\n%s</div>"
1309 "</div>" /* #tag_header_wrapper */
1310 "</div>", /* #tags_content */
1311 rt->commit_id,
1312 tagname,
1313 author,
1314 age,
1315 msg,
1316 rt->tag_commit);
1318 done:
1319 free(age);
1320 free(author);
1321 free(msg);
1322 return error;
1325 static const struct got_error *
1326 gotweb_render_tags(struct request *c)
1328 const struct got_error *error = NULL;
1329 struct repo_tag *rt = NULL;
1330 struct server *srv = c->srv;
1331 struct transport *t = c->t;
1332 struct querystring *qs = t->qs;
1333 struct repo_dir *repo_dir = t->repo_dir;
1334 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1335 int r, commit_found = 0;
1337 if (qs->action == BRIEFS) {
1338 qs->action = TAGS;
1339 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1340 } else
1341 error = got_get_repo_tags(c, srv->max_commits_display);
1342 if (error)
1343 goto done;
1345 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1346 "<div id='tags_title'>Tags</div>\n"
1347 "</div>\n" /* #tags_title_wrapper */
1348 "<div id='tags_content'>\n");
1349 if (r == -1)
1350 goto done;
1352 if (t->tag_count == 0) {
1353 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1354 "This repository contains no tags");
1355 if (r == -1)
1356 goto done;
1359 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1360 if (commit_found == 0 && qs->commit != NULL) {
1361 if (strcmp(qs->commit, rt->commit_id) != 0)
1362 continue;
1363 else
1364 commit_found = 1;
1366 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1367 if (error)
1368 goto done;
1370 tagname = rt->tag_name;
1371 if (strncmp(tagname, "refs/tags/", 10) == 0)
1372 tagname += 10;
1373 error = gotweb_escape_html(&tagname, tagname);
1374 if (error)
1375 goto done;
1377 if (rt->tag_commit != NULL) {
1378 newline = strchr(rt->tag_commit, '\n');
1379 if (newline)
1380 *newline = '\0';
1381 error = gotweb_escape_html(&msg, rt->tag_commit);
1382 if (error)
1383 goto done;
1386 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1387 "<div class='tag'>%s</div>\n"
1388 "<div class='tag_log'>", age, tagname) == -1)
1389 goto done;
1391 r = gotweb_link(c, &(struct gotweb_url){
1392 .action = TAG,
1393 .index_page = -1,
1394 .page = -1,
1395 .path = repo_dir->name,
1396 .commit = rt->commit_id,
1397 }, "%s", msg ? msg : "");
1398 if (r == -1)
1399 goto done;
1401 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1402 "<div class='navs_wrapper'>\n"
1403 "<div class='navs'>") == -1)
1404 goto done;
1406 r = gotweb_link(c, &(struct gotweb_url){
1407 .action = TAG,
1408 .index_page = -1,
1409 .page = -1,
1410 .path = repo_dir->name,
1411 .commit = rt->commit_id,
1412 }, "tag");
1413 if (r == -1)
1414 goto done;
1416 if (fcgi_printf(c, " | ") == -1)
1417 goto done;
1419 r = gotweb_link(c, &(struct gotweb_url){
1420 .action = BRIEFS,
1421 .index_page = -1,
1422 .page = -1,
1423 .path = repo_dir->name,
1424 .commit = rt->commit_id,
1425 }, "commit briefs");
1426 if (r == -1)
1427 goto done;
1429 if (fcgi_printf(c, " | ") == -1)
1430 goto done;
1432 r = gotweb_link(c, &(struct gotweb_url){
1433 .action = COMMITS,
1434 .index_page = -1,
1435 .page = -1,
1436 .path = repo_dir->name,
1437 .commit = rt->commit_id,
1438 }, "commits");
1439 if (r == -1)
1440 goto done;
1442 r = fcgi_printf(c,
1443 "</div>\n" /* .navs */
1444 "</div>\n" /* .navs_wrapper */
1445 "<div class='dotted_line'></div>\n");
1446 if (r == -1)
1447 goto done;
1449 free(age);
1450 age = NULL;
1451 free(tagname);
1452 tagname = NULL;
1453 free(msg);
1454 msg = NULL;
1456 if (t->next_id || t->prev_id) {
1457 if (gotweb_render_navs(c->tp) == -1)
1458 goto done;
1460 fcgi_printf(c, "</div>\n"); /* #tags_content */
1461 done:
1462 free(age);
1463 free(tagname);
1464 free(msg);
1465 return error;
1468 const struct got_error *
1469 gotweb_escape_html(char **escaped_html, const char *orig_html)
1471 const struct got_error *error = NULL;
1472 struct escape_pair {
1473 char c;
1474 const char *s;
1475 } esc[] = {
1476 { '>', "&gt;" },
1477 { '<', "&lt;" },
1478 { '&', "&amp;" },
1479 { '"', "&quot;" },
1480 { '\'', "&apos;" },
1481 { '\n', "<br />" },
1483 size_t orig_len, len;
1484 int i, j, x;
1486 orig_len = strlen(orig_html);
1487 len = orig_len;
1488 for (i = 0; i < orig_len; i++) {
1489 for (j = 0; j < nitems(esc); j++) {
1490 if (orig_html[i] != esc[j].c)
1491 continue;
1492 len += strlen(esc[j].s) - 1 /* escaped char */;
1496 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1497 if (*escaped_html == NULL)
1498 return got_error_from_errno("calloc");
1500 x = 0;
1501 for (i = 0; i < orig_len; i++) {
1502 int escaped = 0;
1503 for (j = 0; j < nitems(esc); j++) {
1504 if (orig_html[i] != esc[j].c)
1505 continue;
1507 if (strlcat(*escaped_html, esc[j].s, len + 1)
1508 >= len + 1) {
1509 error = got_error(GOT_ERR_NO_SPACE);
1510 goto done;
1512 x += strlen(esc[j].s);
1513 escaped = 1;
1514 break;
1516 if (!escaped) {
1517 (*escaped_html)[x] = orig_html[i];
1518 x++;
1521 done:
1522 if (error) {
1523 free(*escaped_html);
1524 *escaped_html = NULL;
1525 } else {
1526 (*escaped_html)[x] = '\0';
1529 return error;
1532 static inline int
1533 should_urlencode(int c)
1535 if (c <= ' ' || c >= 127)
1536 return 1;
1538 switch (c) {
1539 /* gen-delim */
1540 case ':':
1541 case '/':
1542 case '?':
1543 case '#':
1544 case '[':
1545 case ']':
1546 case '@':
1547 /* sub-delims */
1548 case '!':
1549 case '$':
1550 case '&':
1551 case '\'':
1552 case '(':
1553 case ')':
1554 case '*':
1555 case '+':
1556 case ',':
1557 case ';':
1558 case '=':
1559 return 1;
1560 default:
1561 return 0;
1565 static char *
1566 gotweb_urlencode(const char *str)
1568 const char *s;
1569 char *escaped;
1570 size_t i, len;
1571 int a, b;
1573 len = 0;
1574 for (s = str; *s; ++s) {
1575 len++;
1576 if (should_urlencode(*s))
1577 len += 2;
1580 escaped = calloc(1, len + 1);
1581 if (escaped == NULL)
1582 return NULL;
1584 i = 0;
1585 for (s = str; *s; ++s) {
1586 if (should_urlencode(*s)) {
1587 a = (*s & 0xF0) >> 4;
1588 b = (*s & 0x0F);
1590 escaped[i++] = '%';
1591 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1592 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1593 } else
1594 escaped[i++] = *s;
1597 return escaped;
1600 const char *
1601 gotweb_action_name(int action)
1603 switch (action) {
1604 case BLAME:
1605 return "blame";
1606 case BLOB:
1607 return "blob";
1608 case BRIEFS:
1609 return "briefs";
1610 case COMMITS:
1611 return "commits";
1612 case DIFF:
1613 return "diff";
1614 case ERR:
1615 return "err";
1616 case INDEX:
1617 return "index";
1618 case SUMMARY:
1619 return "summary";
1620 case TAG:
1621 return "tag";
1622 case TAGS:
1623 return "tags";
1624 case TREE:
1625 return "tree";
1626 default:
1627 return NULL;
1631 int
1632 gotweb_render_url(struct request *c, struct gotweb_url *url)
1634 const char *sep = "?", *action;
1635 char *tmp;
1636 int r;
1638 action = gotweb_action_name(url->action);
1639 if (action != NULL) {
1640 if (fcgi_printf(c, "?action=%s", action) == -1)
1641 return -1;
1642 sep = "&";
1645 if (url->commit) {
1646 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1647 return -1;
1648 sep = "&";
1651 if (url->previd) {
1652 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1653 return -1;
1654 sep = "&";
1657 if (url->prevset) {
1658 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1659 return -1;
1660 sep = "&";
1663 if (url->file) {
1664 tmp = gotweb_urlencode(url->file);
1665 if (tmp == NULL)
1666 return -1;
1667 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1668 free(tmp);
1669 if (r == -1)
1670 return -1;
1671 sep = "&";
1674 if (url->folder) {
1675 tmp = gotweb_urlencode(url->folder);
1676 if (tmp == NULL)
1677 return -1;
1678 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1679 free(tmp);
1680 if (r == -1)
1681 return -1;
1682 sep = "&";
1685 if (url->headref) {
1686 tmp = gotweb_urlencode(url->headref);
1687 if (tmp == NULL)
1688 return -1;
1689 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1690 free(tmp);
1691 if (r == -1)
1692 return -1;
1693 sep = "&";
1696 if (url->index_page != -1) {
1697 if (fcgi_printf(c, "%sindex_page=%d", sep,
1698 url->index_page) == -1)
1699 return -1;
1700 sep = "&";
1703 if (url->path) {
1704 tmp = gotweb_urlencode(url->path);
1705 if (tmp == NULL)
1706 return -1;
1707 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1708 free(tmp);
1709 if (r == -1)
1710 return -1;
1711 sep = "&";
1714 if (url->page != -1) {
1715 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1716 return -1;
1717 sep = "&";
1720 return 0;
1723 int
1724 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1726 va_list ap;
1727 int r;
1729 if (fcgi_printf(c, "<a href='") == -1)
1730 return -1;
1732 if (gotweb_render_url(c, url) == -1)
1733 return -1;
1735 if (fcgi_printf(c, "'>") == -1)
1736 return -1;
1738 va_start(ap, fmt);
1739 r = fcgi_vprintf(c, fmt, ap);
1740 va_end(ap);
1741 if (r == -1)
1742 return -1;
1744 if (fcgi_printf(c, "</a>"))
1745 return -1;
1746 return 0;
1749 static struct got_repository *
1750 find_cached_repo(struct server *srv, const char *path)
1752 int i;
1754 for (i = 0; i < srv->ncached_repos; i++) {
1755 if (strcmp(srv->cached_repos[i].path, path) == 0)
1756 return srv->cached_repos[i].repo;
1759 return NULL;
1762 static const struct got_error *
1763 cache_repo(struct got_repository **new, struct server *srv,
1764 struct repo_dir *repo_dir, struct socket *sock)
1766 const struct got_error *error = NULL;
1767 struct got_repository *repo;
1768 struct cached_repo *cr;
1769 int evicted = 0;
1771 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1772 cr = &srv->cached_repos[srv->ncached_repos - 1];
1773 error = got_repo_close(cr->repo);
1774 memset(cr, 0, sizeof(*cr));
1775 srv->ncached_repos--;
1776 if (error)
1777 return error;
1778 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1779 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1780 cr = &srv->cached_repos[0];
1781 evicted = 1;
1782 } else {
1783 cr = &srv->cached_repos[srv->ncached_repos];
1786 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1787 if (error) {
1788 if (evicted) {
1789 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1790 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1792 return error;
1795 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1796 >= sizeof(cr->path)) {
1797 if (evicted) {
1798 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1799 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1801 return got_error(GOT_ERR_NO_SPACE);
1804 cr->repo = repo;
1805 srv->ncached_repos++;
1806 *new = repo;
1807 return NULL;
1810 static const struct got_error *
1811 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1813 const struct got_error *error = NULL;
1814 struct socket *sock = c->sock;
1815 struct server *srv = c->srv;
1816 struct transport *t = c->t;
1817 struct got_repository *repo = NULL;
1818 DIR *dt;
1819 char *dir_test;
1821 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1822 GOTWEB_GIT_DIR) == -1)
1823 return got_error_from_errno("asprintf");
1825 dt = opendir(dir_test);
1826 if (dt == NULL) {
1827 free(dir_test);
1828 } else {
1829 repo_dir->path = dir_test;
1830 dir_test = NULL;
1831 goto done;
1834 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1835 repo_dir->name) == -1)
1836 return got_error_from_errno("asprintf");
1838 dt = opendir(dir_test);
1839 if (dt == NULL) {
1840 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1841 goto err;
1842 } else {
1843 repo_dir->path = dir_test;
1844 dir_test = NULL;
1847 done:
1848 if (srv->respect_exportok &&
1849 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1850 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1851 goto err;
1854 repo = find_cached_repo(srv, repo_dir->path);
1855 if (repo == NULL) {
1856 error = cache_repo(&repo, srv, repo_dir, sock);
1857 if (error)
1858 goto err;
1860 t->repo = repo;
1861 error = gotweb_get_repo_description(&repo_dir->description, srv,
1862 repo_dir->path, dirfd(dt));
1863 if (error)
1864 goto err;
1865 error = got_get_repo_owner(&repo_dir->owner, c);
1866 if (error)
1867 goto err;
1868 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1869 if (error)
1870 goto err;
1871 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1872 dirfd(dt));
1873 err:
1874 free(dir_test);
1875 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1876 error = got_error_from_errno("closedir");
1877 return error;
1880 static const struct got_error *
1881 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1883 const struct got_error *error;
1885 *repo_dir = calloc(1, sizeof(**repo_dir));
1886 if (*repo_dir == NULL)
1887 return got_error_from_errno("calloc");
1889 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1890 error = got_error_from_errno("asprintf");
1891 free(*repo_dir);
1892 *repo_dir = NULL;
1893 return error;
1895 (*repo_dir)->owner = NULL;
1896 (*repo_dir)->description = NULL;
1897 (*repo_dir)->url = NULL;
1898 (*repo_dir)->age = NULL;
1899 (*repo_dir)->path = NULL;
1901 return NULL;
1904 static const struct got_error *
1905 gotweb_get_repo_description(char **description, struct server *srv,
1906 const char *dirpath, int dir)
1908 const struct got_error *error = NULL;
1909 struct stat sb;
1910 int fd = -1;
1911 off_t len;
1913 *description = NULL;
1914 if (srv->show_repo_description == 0)
1915 return NULL;
1917 fd = openat(dir, "description", O_RDONLY);
1918 if (fd == -1) {
1919 if (errno != ENOENT && errno != EACCES) {
1920 error = got_error_from_errno_fmt("openat %s/%s",
1921 dirpath, "description");
1923 goto done;
1926 if (fstat(fd, &sb) == -1) {
1927 error = got_error_from_errno_fmt("fstat %s/%s",
1928 dirpath, "description");
1929 goto done;
1932 len = sb.st_size;
1933 if (len > GOTWEBD_MAXDESCRSZ - 1)
1934 len = GOTWEBD_MAXDESCRSZ - 1;
1936 *description = calloc(len + 1, sizeof(**description));
1937 if (*description == NULL) {
1938 error = got_error_from_errno("calloc");
1939 goto done;
1942 if (read(fd, *description, len) == -1)
1943 error = got_error_from_errno("read");
1944 done:
1945 if (fd != -1 && close(fd) == -1 && error == NULL)
1946 error = got_error_from_errno("close");
1947 return error;
1950 static const struct got_error *
1951 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1952 int dir)
1954 const struct got_error *error = NULL;
1955 struct stat sb;
1956 int fd = -1;
1957 off_t len;
1959 *url = NULL;
1960 if (srv->show_repo_cloneurl == 0)
1961 return NULL;
1963 fd = openat(dir, "cloneurl", O_RDONLY);
1964 if (fd == -1) {
1965 if (errno != ENOENT && errno != EACCES) {
1966 error = got_error_from_errno_fmt("openat %s/%s",
1967 dirpath, "cloneurl");
1969 goto done;
1972 if (fstat(fd, &sb) == -1) {
1973 error = got_error_from_errno_fmt("fstat %s/%s",
1974 dirpath, "cloneurl");
1975 goto done;
1978 len = sb.st_size;
1979 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1980 len = GOTWEBD_MAXCLONEURLSZ - 1;
1982 *url = calloc(len + 1, sizeof(**url));
1983 if (*url == NULL) {
1984 error = got_error_from_errno("calloc");
1985 goto done;
1988 if (read(fd, *url, len) == -1)
1989 error = got_error_from_errno("read");
1990 done:
1991 if (fd != -1 && close(fd) == -1 && error == NULL)
1992 error = got_error_from_errno("close");
1993 return error;
1996 const struct got_error *
1997 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1999 struct tm tm;
2000 long long diff_time;
2001 const char *years = "years ago", *months = "months ago";
2002 const char *weeks = "weeks ago", *days = "days ago";
2003 const char *hours = "hours ago", *minutes = "minutes ago";
2004 const char *seconds = "seconds ago", *now = "right now";
2005 char *s;
2006 char datebuf[29];
2008 *repo_age = NULL;
2010 switch (ref_tm) {
2011 case TM_DIFF:
2012 diff_time = time(NULL) - committer_time;
2013 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2014 if (asprintf(repo_age, "%lld %s",
2015 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2016 return got_error_from_errno("asprintf");
2017 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2018 if (asprintf(repo_age, "%lld %s",
2019 (diff_time / 60 / 60 / 24 / (365 / 12)),
2020 months) == -1)
2021 return got_error_from_errno("asprintf");
2022 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2023 if (asprintf(repo_age, "%lld %s",
2024 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2025 return got_error_from_errno("asprintf");
2026 } else if (diff_time > 60 * 60 * 24 * 2) {
2027 if (asprintf(repo_age, "%lld %s",
2028 (diff_time / 60 / 60 / 24), days) == -1)
2029 return got_error_from_errno("asprintf");
2030 } else if (diff_time > 60 * 60 * 2) {
2031 if (asprintf(repo_age, "%lld %s",
2032 (diff_time / 60 / 60), hours) == -1)
2033 return got_error_from_errno("asprintf");
2034 } else if (diff_time > 60 * 2) {
2035 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2036 minutes) == -1)
2037 return got_error_from_errno("asprintf");
2038 } else if (diff_time > 2) {
2039 if (asprintf(repo_age, "%lld %s", diff_time,
2040 seconds) == -1)
2041 return got_error_from_errno("asprintf");
2042 } else {
2043 if (asprintf(repo_age, "%s", now) == -1)
2044 return got_error_from_errno("asprintf");
2046 break;
2047 case TM_LONG:
2048 if (gmtime_r(&committer_time, &tm) == NULL)
2049 return got_error_from_errno("gmtime_r");
2051 s = asctime_r(&tm, datebuf);
2052 if (s == NULL)
2053 return got_error_from_errno("asctime_r");
2055 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2056 return got_error_from_errno("asprintf");
2057 break;
2059 return NULL;