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 "got_compat.h"
52 #include "proc.h"
53 #include "gotwebd.h"
54 #include "tmpl.h"
56 static const struct querystring_keys querystring_keys[] = {
57 { "action", ACTION },
58 { "commit", COMMIT },
59 { "file", RFILE },
60 { "folder", FOLDER },
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
63 { "path", PATH },
64 { "page", PAGE },
65 };
67 static const struct action_keys action_keys[] = {
68 { "blame", BLAME },
69 { "blob", BLOB },
70 { "blobraw", BLOBRAW },
71 { "briefs", BRIEFS },
72 { "commits", COMMITS },
73 { "diff", DIFF },
74 { "error", ERR },
75 { "index", INDEX },
76 { "summary", SUMMARY },
77 { "tag", TAG },
78 { "tags", TAGS },
79 { "tree", TREE },
80 { "rss", RSS },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 const char *);
91 static const struct got_error *gotweb_load_got_path(struct request *c,
92 struct repo_dir *);
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 const char *, int);
97 static const struct got_error *gotweb_render_blame(struct request *);
98 static const struct got_error *gotweb_render_diff(struct request *);
99 static const struct got_error *gotweb_render_summary(struct request *);
100 static const struct got_error *gotweb_render_tag(struct request *);
101 static const struct got_error *gotweb_render_tags(struct request *);
102 static const struct got_error *gotweb_render_branches(struct request *);
104 static void gotweb_free_querystring(struct querystring *);
105 static void gotweb_free_repo_dir(struct repo_dir *);
107 struct server *gotweb_get_server(uint8_t *, uint8_t *);
109 void
110 gotweb_process_request(struct request *c)
112 const struct got_error *error = NULL, *error2 = NULL;
113 struct got_blob_object *blob = NULL;
114 struct server *srv = NULL;
115 struct querystring *qs = NULL;
116 struct repo_dir *repo_dir = NULL;
117 uint8_t err[] = "gotwebd experienced an error: ";
118 int r, html = 0, fd = -1;
120 /* init the transport */
121 error = gotweb_init_transport(&c->t);
122 if (error) {
123 log_warnx("%s: %s", __func__, error->msg);
124 return;
126 /* don't process any further if client disconnected */
127 if (c->sock->client_status == CLIENT_DISCONNECT)
128 return;
129 /* get the gotwebd server */
130 srv = gotweb_get_server(c->server_name, c->http_host);
131 if (srv == NULL) {
132 log_warnx("%s: error server is NULL", __func__);
133 goto err;
135 c->srv = srv;
136 /* parse our querystring */
137 error = gotweb_init_querystring(&qs);
138 if (error) {
139 log_warnx("%s: %s", __func__, error->msg);
140 goto err;
142 c->t->qs = qs;
143 error = gotweb_parse_querystring(&qs, c->querystring);
144 if (error) {
145 log_warnx("%s: %s", __func__, error->msg);
146 goto err;
149 /*
150 * certain actions require a commit id in the querystring. this stops
151 * bad actors from exploiting this by manually manipulating the
152 * querystring.
153 */
155 if (qs->action == BLAME || qs->action == BLOB ||
156 qs->action == BLOBRAW || qs->action == DIFF) {
157 if (qs->commit == NULL) {
158 error2 = got_error(GOT_ERR_QUERYSTRING);
159 goto render;
163 if (qs->action != INDEX) {
164 error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 if (error)
166 goto done;
167 error = gotweb_load_got_path(c, repo_dir);
168 c->t->repo_dir = repo_dir;
169 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
170 goto err;
173 if (qs->action == BLOBRAW) {
174 error = got_get_repo_commits(c, 1);
175 if (error)
176 goto done;
177 error = got_output_file_blob(c);
178 if (error) {
179 log_warnx("%s: %s", __func__, error->msg);
180 goto err;
182 goto done;
185 if (qs->action == BLOB) {
186 int binary;
187 struct gotweb_url url = {
188 .index_page = -1,
189 .page = -1,
190 .action = BLOBRAW,
191 .path = qs->path,
192 .commit = qs->commit,
193 .folder = qs->folder,
194 .file = qs->file,
195 };
197 error = got_get_repo_commits(c, 1);
198 if (error)
199 goto done;
201 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
202 if (error2)
203 goto render;
204 if (binary) {
205 fcgi_puts(c->tp, "Status: 302\r\n");
206 fcgi_puts(c->tp, "Location: ");
207 gotweb_render_url(c, &url);
208 fcgi_puts(c->tp, "\r\n\r\n");
209 goto done;
213 if (qs->action == RSS) {
214 error = gotweb_render_content_type_file(c,
215 "application/rss+xml;charset=utf-8",
216 repo_dir->name, ".rss");
217 if (error) {
218 log_warnx("%s: %s", __func__, error->msg);
219 goto err;
222 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
223 if (error) {
224 log_warnx("%s: %s", __func__, error->msg);
225 goto err;
227 if (gotweb_render_rss(c->tp) == -1)
228 goto err;
229 goto done;
232 render:
233 error = gotweb_render_content_type(c, "text/html");
234 if (error) {
235 log_warnx("%s: %s", __func__, error->msg);
236 goto err;
238 html = 1;
240 if (gotweb_render_header(c->tp) == -1)
241 goto err;
243 if (error2) {
244 error = error2;
245 goto err;
248 switch(qs->action) {
249 case BLAME:
250 error = gotweb_render_blame(c);
251 if (error) {
252 log_warnx("%s: %s", __func__, error->msg);
253 goto err;
255 break;
256 case BLOB:
257 if (gotweb_render_blob(c->tp, blob) == -1)
258 goto err;
259 break;
260 case BRIEFS:
261 if (gotweb_render_briefs(c->tp) == -1)
262 goto err;
263 break;
264 case COMMITS:
265 error = got_get_repo_commits(c, srv->max_commits_display);
266 if (error) {
267 log_warnx("%s: %s", __func__, error->msg);
268 goto err;
270 if (gotweb_render_commits(c->tp) == -1)
271 goto err;
272 break;
273 case DIFF:
274 error = gotweb_render_diff(c);
275 if (error) {
276 log_warnx("%s: %s", __func__, error->msg);
277 goto err;
279 break;
280 case INDEX:
281 error = gotweb_render_index(c);
282 if (error) {
283 log_warnx("%s: %s", __func__, error->msg);
284 goto err;
286 break;
287 case SUMMARY:
288 error = gotweb_render_summary(c);
289 if (error) {
290 log_warnx("%s: %s", __func__, error->msg);
291 goto err;
293 break;
294 case TAG:
295 error = gotweb_render_tag(c);
296 if (error) {
297 log_warnx("%s: %s", __func__, error->msg);
298 goto err;
300 break;
301 case TAGS:
302 error = gotweb_render_tags(c);
303 if (error) {
304 log_warnx("%s: %s", __func__, error->msg);
305 goto err;
307 break;
308 case TREE:
309 error = got_get_repo_commits(c, 1);
310 if (error) {
311 log_warnx("%s: %s", __func__, error->msg);
312 goto err;
314 if (gotweb_render_tree(c->tp) == -1)
315 goto err;
316 break;
317 case ERR:
318 default:
319 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
320 "Erorr: Bad Querystring");
321 if (r == -1)
322 goto err;
323 break;
326 goto done;
327 err:
328 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
329 return;
330 if (fcgi_printf(c, "\n%s", err) == -1)
331 return;
332 if (error) {
333 if (fcgi_printf(c, "%s", error->msg) == -1)
334 return;
335 } else {
336 if (fcgi_printf(c, "see daemon logs for details") == -1)
337 return;
339 if (html && fcgi_printf(c, "</div>\n") == -1)
340 return;
341 done:
342 if (blob)
343 got_object_blob_close(blob);
344 if (fd != -1)
345 close(fd);
346 if (html && srv != NULL)
347 gotweb_render_footer(c->tp);
350 struct server *
351 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
353 struct server *srv = NULL;
355 /* check against the server name first */
356 if (strlen(server_name) > 0)
357 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
358 if (strcmp(srv->name, server_name) == 0)
359 goto done;
361 /* check against subdomain second */
362 if (strlen(subdomain) > 0)
363 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
364 if (strcmp(srv->name, subdomain) == 0)
365 goto done;
367 /* if those fail, send first server */
368 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
369 if (srv != NULL)
370 break;
371 done:
372 return srv;
373 };
375 const struct got_error *
376 gotweb_init_transport(struct transport **t)
378 const struct got_error *error = NULL;
380 *t = calloc(1, sizeof(**t));
381 if (*t == NULL)
382 return got_error_from_errno2("%s: calloc", __func__);
384 TAILQ_INIT(&(*t)->repo_commits);
385 TAILQ_INIT(&(*t)->repo_tags);
387 (*t)->repo = NULL;
388 (*t)->repo_dir = NULL;
389 (*t)->qs = NULL;
390 (*t)->next_id = NULL;
391 (*t)->prev_id = NULL;
392 (*t)->next_disp = 0;
393 (*t)->prev_disp = 0;
395 return error;
398 static const struct got_error *
399 gotweb_init_querystring(struct querystring **qs)
401 const struct got_error *error = NULL;
403 *qs = calloc(1, sizeof(**qs));
404 if (*qs == NULL)
405 return got_error_from_errno2("%s: calloc", __func__);
407 (*qs)->headref = strdup("HEAD");
408 if ((*qs)->headref == NULL) {
409 free(*qs);
410 *qs = NULL;
411 return got_error_from_errno2("%s: strdup", __func__);
414 (*qs)->action = INDEX;
415 (*qs)->commit = NULL;
416 (*qs)->file = NULL;
417 (*qs)->folder = NULL;
418 (*qs)->index_page = 0;
419 (*qs)->path = NULL;
421 return error;
424 static const struct got_error *
425 gotweb_parse_querystring(struct querystring **qs, char *qst)
427 const struct got_error *error = NULL;
428 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
429 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
431 if (qst == NULL)
432 return error;
434 tok1 = strdup(qst);
435 if (tok1 == NULL)
436 return got_error_from_errno2("%s: strdup", __func__);
438 tok1_pair = tok1;
439 tok1_end = tok1;
441 while (tok1_pair != NULL) {
442 strsep(&tok1_end, "&");
444 tok2 = strdup(tok1_pair);
445 if (tok2 == NULL) {
446 free(tok1);
447 return got_error_from_errno2("%s: strdup", __func__);
450 tok2_pair = tok2;
451 tok2_end = tok2;
453 while (tok2_pair != NULL) {
454 strsep(&tok2_end, "=");
455 if (tok2_end) {
456 error = gotweb_assign_querystring(qs, tok2_pair,
457 tok2_end);
458 if (error)
459 goto err;
461 tok2_pair = tok2_end;
463 free(tok2);
464 tok1_pair = tok1_end;
466 free(tok1);
467 return error;
468 err:
469 free(tok2);
470 free(tok1);
471 return error;
474 /*
475 * Adapted from usr.sbin/httpd/httpd.c url_decode.
476 */
477 static const struct got_error *
478 gotweb_urldecode(char *url)
480 char *p, *q;
481 char hex[3];
482 unsigned long x;
484 hex[2] = '\0';
485 p = q = url;
487 while (*p != '\0') {
488 switch (*p) {
489 case '%':
490 /* Encoding character is followed by two hex chars */
491 if (!isxdigit((unsigned char)p[1]) ||
492 !isxdigit((unsigned char)p[2]) ||
493 (p[1] == '0' && p[2] == '0'))
494 return got_error(GOT_ERR_BAD_QUERYSTRING);
496 hex[0] = p[1];
497 hex[1] = p[2];
499 /*
500 * We don't have to validate "hex" because it is
501 * guaranteed to include two hex chars followed by nul.
502 */
503 x = strtoul(hex, NULL, 16);
504 *q = (char)x;
505 p += 2;
506 break;
507 default:
508 *q = *p;
509 break;
511 p++;
512 q++;
514 *q = '\0';
516 return NULL;
519 static const struct got_error *
520 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
522 const struct got_error *error = NULL;
523 const char *errstr;
524 int a_cnt, el_cnt;
526 error = gotweb_urldecode(value);
527 if (error)
528 return error;
530 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
531 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
532 continue;
534 switch (querystring_keys[el_cnt].element) {
535 case ACTION:
536 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
537 if (strcmp(value, action_keys[a_cnt].name) != 0)
538 continue;
539 else if (strcmp(value,
540 action_keys[a_cnt].name) == 0){
541 (*qs)->action =
542 action_keys[a_cnt].action;
543 goto qa_found;
546 (*qs)->action = ERR;
547 qa_found:
548 break;
549 case COMMIT:
550 (*qs)->commit = strdup(value);
551 if ((*qs)->commit == NULL) {
552 error = got_error_from_errno2("%s: strdup",
553 __func__);
554 goto done;
556 break;
557 case RFILE:
558 (*qs)->file = strdup(value);
559 if ((*qs)->file == NULL) {
560 error = got_error_from_errno2("%s: strdup",
561 __func__);
562 goto done;
564 break;
565 case FOLDER:
566 (*qs)->folder = strdup(value);
567 if ((*qs)->folder == NULL) {
568 error = got_error_from_errno2("%s: strdup",
569 __func__);
570 goto done;
572 break;
573 case HEADREF:
574 free((*qs)->headref);
575 (*qs)->headref = strdup(value);
576 if ((*qs)->headref == NULL) {
577 error = got_error_from_errno2("%s: strdup",
578 __func__);
579 goto done;
581 break;
582 case INDEX_PAGE:
583 if (strlen(value) == 0)
584 break;
585 (*qs)->index_page = strtonum(value, INT64_MIN,
586 INT64_MAX, &errstr);
587 if (errstr) {
588 error = got_error_from_errno3("%s: strtonum %s",
589 __func__, errstr);
590 goto done;
592 if ((*qs)->index_page < 0)
593 (*qs)->index_page = 0;
594 break;
595 case PATH:
596 (*qs)->path = strdup(value);
597 if ((*qs)->path == NULL) {
598 error = got_error_from_errno2("%s: strdup",
599 __func__);
600 goto done;
602 break;
603 case PAGE:
604 if (strlen(value) == 0)
605 break;
606 (*qs)->page = strtonum(value, INT64_MIN,
607 INT64_MAX, &errstr);
608 if (errstr) {
609 error = got_error_from_errno3("%s: strtonum %s",
610 __func__, errstr);
611 goto done;
613 if ((*qs)->page < 0)
614 (*qs)->page = 0;
615 break;
616 default:
617 break;
620 done:
621 return error;
624 void
625 gotweb_free_repo_tag(struct repo_tag *rt)
627 if (rt != NULL) {
628 free(rt->commit_id);
629 free(rt->tag_name);
630 free(rt->tag_commit);
631 free(rt->commit_msg);
632 free(rt->tagger);
634 free(rt);
637 void
638 gotweb_free_repo_commit(struct repo_commit *rc)
640 if (rc != NULL) {
641 free(rc->path);
642 free(rc->refs_str);
643 free(rc->commit_id);
644 free(rc->parent_id);
645 free(rc->tree_id);
646 free(rc->author);
647 free(rc->committer);
648 free(rc->commit_msg);
650 free(rc);
653 static void
654 gotweb_free_querystring(struct querystring *qs)
656 if (qs != NULL) {
657 free(qs->commit);
658 free(qs->file);
659 free(qs->folder);
660 free(qs->headref);
661 free(qs->path);
663 free(qs);
666 static void
667 gotweb_free_repo_dir(struct repo_dir *repo_dir)
669 if (repo_dir != NULL) {
670 free(repo_dir->name);
671 free(repo_dir->owner);
672 free(repo_dir->description);
673 free(repo_dir->url);
674 free(repo_dir->age);
675 free(repo_dir->path);
677 free(repo_dir);
680 void
681 gotweb_free_transport(struct transport *t)
683 struct repo_commit *rc = NULL, *trc = NULL;
684 struct repo_tag *rt = NULL, *trt = NULL;
686 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
687 TAILQ_REMOVE(&t->repo_commits, rc, entry);
688 gotweb_free_repo_commit(rc);
690 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
691 TAILQ_REMOVE(&t->repo_tags, rt, entry);
692 gotweb_free_repo_tag(rt);
694 gotweb_free_repo_dir(t->repo_dir);
695 gotweb_free_querystring(t->qs);
696 free(t->next_id);
697 free(t->prev_id);
698 free(t);
701 const struct got_error *
702 gotweb_render_content_type(struct request *c, const char *type)
704 const char *csp = "default-src 'self'; script-src 'none'; "
705 "object-src 'none';";
707 fcgi_printf(c,
708 "Content-Security-Policy: %s\r\n"
709 "Content-Type: %s\r\n\r\n",
710 csp, type);
711 return NULL;
714 const struct got_error *
715 gotweb_render_content_type_file(struct request *c, const char *type,
716 const char *file, const char *suffix)
718 fcgi_printf(c, "Content-type: %s\r\n"
719 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
720 type, file, suffix ? suffix : "");
721 return NULL;
724 void
725 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
726 struct gotweb_url *next, int *have_next)
728 struct transport *t = c->t;
729 struct querystring *qs = t->qs;
730 struct server *srv = c->srv;
732 *have_prev = *have_next = 0;
734 switch(qs->action) {
735 case INDEX:
736 if (qs->index_page > 0) {
737 *have_prev = 1;
738 *prev = (struct gotweb_url){
739 .action = -1,
740 .index_page = qs->index_page - 1,
741 .page = -1,
742 };
744 if (t->next_disp == srv->max_repos_display &&
745 t->repos_total != (qs->index_page + 1) *
746 srv->max_repos_display) {
747 *have_next = 1;
748 *next = (struct gotweb_url){
749 .action = -1,
750 .index_page = qs->index_page + 1,
751 .page = -1,
752 };
754 break;
755 case BRIEFS:
756 if (t->prev_id && qs->commit != NULL &&
757 strcmp(qs->commit, t->prev_id) != 0) {
758 *have_prev = 1;
759 *prev = (struct gotweb_url){
760 .action = BRIEFS,
761 .index_page = -1,
762 .page = qs->page - 1,
763 .path = qs->path,
764 .commit = t->prev_id,
765 .headref = qs->headref,
766 };
768 if (t->next_id) {
769 *have_next = 1;
770 *next = (struct gotweb_url){
771 .action = BRIEFS,
772 .index_page = -1,
773 .page = qs->page + 1,
774 .path = qs->path,
775 .commit = t->next_id,
776 .headref = qs->headref,
777 };
779 break;
780 case COMMITS:
781 if (t->prev_id && qs->commit != NULL &&
782 strcmp(qs->commit, t->prev_id) != 0) {
783 *have_prev = 1;
784 *prev = (struct gotweb_url){
785 .action = COMMITS,
786 .index_page = -1,
787 .page = qs->page - 1,
788 .path = qs->path,
789 .commit = t->prev_id,
790 .headref = qs->headref,
791 .folder = qs->folder,
792 .file = qs->file,
793 };
795 if (t->next_id) {
796 *have_next = 1;
797 *next = (struct gotweb_url){
798 .action = COMMITS,
799 .index_page = -1,
800 .page = qs->page + 1,
801 .path = qs->path,
802 .commit = t->next_id,
803 .headref = qs->headref,
804 .folder = qs->folder,
805 .file = qs->file,
806 };
808 break;
809 case TAGS:
810 if (t->prev_id && qs->commit != NULL &&
811 strcmp(qs->commit, t->prev_id) != 0) {
812 *have_prev = 1;
813 *prev = (struct gotweb_url){
814 .action = TAGS,
815 .index_page = -1,
816 .page = qs->page - 1,
817 .path = qs->path,
818 .commit = t->prev_id,
819 .headref = qs->headref,
820 };
822 if (t->next_id) {
823 *have_next = 1;
824 *next = (struct gotweb_url){
825 .action = TAGS,
826 .index_page = -1,
827 .page = qs->page + 1,
828 .path = qs->path,
829 .commit = t->next_id,
830 .headref = qs->headref,
831 };
833 break;
837 static const struct got_error *
838 gotweb_render_index(struct request *c)
840 const struct got_error *error = NULL;
841 struct server *srv = c->srv;
842 struct transport *t = c->t;
843 struct querystring *qs = t->qs;
844 struct repo_dir *repo_dir = NULL;
845 DIR *d;
846 struct dirent **sd_dent = NULL;
847 unsigned int d_cnt, d_i, d_disp = 0;
848 unsigned int d_skipped = 0;
849 int type;
851 d = opendir(srv->repos_path);
852 if (d == NULL) {
853 error = got_error_from_errno2("opendir", srv->repos_path);
854 return error;
857 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
858 if (d_cnt == -1) {
859 sd_dent = NULL;
860 error = got_error_from_errno2("scandir", srv->repos_path);
861 goto done;
864 if (gotweb_render_repo_table_hdr(c->tp) == -1)
865 goto done;
867 for (d_i = 0; d_i < d_cnt; d_i++) {
868 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
869 break;
871 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
872 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
873 d_skipped++;
874 continue;
877 error = got_path_dirent_type(&type, srv->repos_path,
878 sd_dent[d_i]);
879 if (error)
880 goto done;
881 if (type != DT_DIR) {
882 d_skipped++;
883 continue;
886 if (qs->index_page > 0 && (qs->index_page *
887 srv->max_repos_display) > t->prev_disp) {
888 t->prev_disp++;
889 continue;
892 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
893 if (error)
894 goto done;
896 error = gotweb_load_got_path(c, repo_dir);
897 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
898 error = NULL;
899 gotweb_free_repo_dir(repo_dir);
900 repo_dir = NULL;
901 d_skipped++;
902 continue;
904 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
905 goto done;
907 d_disp++;
908 t->prev_disp++;
910 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
911 goto done;
913 gotweb_free_repo_dir(repo_dir);
914 repo_dir = NULL;
915 t->next_disp++;
916 if (d_disp == srv->max_repos_display)
917 break;
919 t->repos_total = d_cnt - d_skipped;
921 if (srv->max_repos_display == 0)
922 goto done;
923 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
924 goto done;
925 if (t->repos_total <= srv->max_repos ||
926 t->repos_total <= srv->max_repos_display)
927 goto done;
929 if (gotweb_render_navs(c->tp) == -1)
930 goto done;
931 done:
932 if (sd_dent) {
933 for (d_i = 0; d_i < d_cnt; d_i++)
934 free(sd_dent[d_i]);
935 free(sd_dent);
937 if (d != NULL && closedir(d) == EOF && error == NULL)
938 error = got_error_from_errno("closedir");
939 return error;
942 static const struct got_error *
943 gotweb_render_blame(struct request *c)
945 const struct got_error *error = NULL;
946 struct transport *t = c->t;
947 struct repo_commit *rc = NULL;
948 char *age = NULL, *msg = NULL;
949 int r;
951 error = got_get_repo_commits(c, 1);
952 if (error)
953 return error;
955 rc = TAILQ_FIRST(&t->repo_commits);
957 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
958 if (error)
959 goto done;
960 error = gotweb_escape_html(&msg, rc->commit_msg);
961 if (error)
962 goto done;
964 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
965 "<div id='blame_title'>Blame</div>\n"
966 "</div>\n" /* #blame_title_wrapper */
967 "<div id='blame_content'>\n"
968 "<div id='blame_header_wrapper'>\n"
969 "<div id='blame_header'>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "<div id='header_commit_msg_title'>Message:</div>\n"
973 "<div id='header_commit_msg'>%s</div>\n"
974 "</div>\n" /* #blame_header */
975 "</div>\n" /* #blame_header_wrapper */
976 "<div class='dotted_line'></div>\n"
977 "<div id='blame'>\n",
978 age,
979 msg);
980 if (r == -1)
981 goto done;
983 error = got_output_file_blame(c);
984 if (error)
985 goto done;
987 fcgi_printf(c, "</div>\n" /* #blame */
988 "</div>\n"); /* #blame_content */
989 done:
990 free(age);
991 free(msg);
992 return error;
995 static const struct got_error *
996 gotweb_render_branches(struct request *c)
998 const struct got_error *error = NULL;
999 struct got_reflist_head refs;
1000 struct got_reflist_entry *re;
1001 struct transport *t = c->t;
1002 struct querystring *qs = t->qs;
1003 struct got_repository *repo = t->repo;
1004 char *escaped_refname = NULL;
1005 char *age = NULL;
1006 int r;
1008 TAILQ_INIT(&refs);
1010 error = got_ref_list(&refs, repo, "refs/heads",
1011 got_ref_cmp_by_name, NULL);
1012 if (error)
1013 goto done;
1015 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1016 "<div id='branches_title'>Branches</div>\n"
1017 "</div>\n" /* #branches_title_wrapper */
1018 "<div id='branches_content'>\n");
1019 if (r == -1)
1020 goto done;
1022 TAILQ_FOREACH(re, &refs, entry) {
1023 const char *refname = NULL;
1025 if (got_ref_is_symbolic(re->ref))
1026 continue;
1028 refname = got_ref_get_name(re->ref);
1029 if (refname == NULL) {
1030 error = got_error_from_errno("strdup");
1031 goto done;
1033 if (strncmp(refname, "refs/heads/", 11) != 0)
1034 continue;
1036 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1037 if (error)
1038 goto done;
1040 if (strncmp(refname, "refs/heads/", 11) == 0)
1041 refname += 11;
1042 error = gotweb_escape_html(&escaped_refname, refname);
1043 if (error)
1044 goto done;
1046 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1047 "<div class='branches_age'>%s</div>\n"
1048 "<div class='branches_space'>&nbsp;</div>\n"
1049 "<div class='branch'>", age);
1050 if (r == -1)
1051 goto done;
1053 r = gotweb_link(c, &(struct gotweb_url){
1054 .action = SUMMARY,
1055 .index_page = -1,
1056 .page = -1,
1057 .path = qs->path,
1058 .headref = refname,
1059 }, "%s", escaped_refname);
1060 if (r == -1)
1061 goto done;
1063 if (fcgi_printf(c, "</div>\n" /* .branch */
1064 "<div class='navs_wrapper'>\n"
1065 "<div class='navs'>") == -1)
1066 goto done;
1068 r = gotweb_link(c, &(struct gotweb_url){
1069 .action = SUMMARY,
1070 .index_page = -1,
1071 .page = -1,
1072 .path = qs->path,
1073 .headref = refname,
1074 }, "summary");
1075 if (r == -1)
1076 goto done;
1078 if (fcgi_printf(c, " | ") == -1)
1079 goto done;
1081 r = gotweb_link(c, &(struct gotweb_url){
1082 .action = BRIEFS,
1083 .index_page = -1,
1084 .page = -1,
1085 .path = qs->path,
1086 .headref = refname,
1087 }, "commit briefs");
1088 if (r == -1)
1089 goto done;
1091 if (fcgi_printf(c, " | ") == -1)
1092 goto done;
1094 r = gotweb_link(c, &(struct gotweb_url){
1095 .action = COMMITS,
1096 .index_page = -1,
1097 .page = -1,
1098 .path = qs->path,
1099 .headref = refname,
1100 }, "commits");
1101 if (r == -1)
1102 goto done;
1104 r = fcgi_printf(c, "</div>\n" /* .navs */
1105 "</div>\n" /* .navs_wrapper */
1106 "<div class='dotted_line'></div>\n"
1107 "</div>\n"); /* .branches_wrapper */
1108 if (r == -1)
1109 goto done;
1111 free(age);
1112 age = NULL;
1113 free(escaped_refname);
1114 escaped_refname = NULL;
1116 fcgi_printf(c, "</div>\n"); /* #branches_content */
1117 done:
1118 free(age);
1119 free(escaped_refname);
1120 got_ref_list_free(&refs);
1121 return error;
1124 static const struct got_error *
1125 gotweb_render_diff(struct request *c)
1127 const struct got_error *error = NULL;
1128 struct transport *t = c->t;
1129 struct repo_commit *rc = NULL;
1130 char *age = NULL, *author = NULL, *msg = NULL;
1131 int r;
1133 error = got_get_repo_commits(c, 1);
1134 if (error)
1135 return error;
1137 rc = TAILQ_FIRST(&t->repo_commits);
1139 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1140 if (error)
1141 goto done;
1142 error = gotweb_escape_html(&author, rc->author);
1143 if (error)
1144 goto done;
1145 error = gotweb_escape_html(&msg, rc->commit_msg);
1146 if (error)
1147 goto done;
1149 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1150 "<div id='diff_title'>Commit Diff</div>\n"
1151 "</div>\n" /* #diff_title_wrapper */
1152 "<div id='diff_content'>\n"
1153 "<div id='diff_header_wrapper'>\n"
1154 "<div id='diff_header'>\n"
1155 "<div id='header_diff_title'>Diff:</div>\n"
1156 "<div id='header_diff'>%s<br />%s</div>\n"
1157 "<div class='header_commit_title'>Commit:</div>\n"
1158 "<div class='header_commit'>%s</div>\n"
1159 "<div id='header_tree_title'>Tree:</div>\n"
1160 "<div id='header_tree'>%s</div>\n"
1161 "<div class='header_author_title'>Author:</div>\n"
1162 "<div class='header_author'>%s</div>\n"
1163 "<div class='header_age_title'>Date:</div>\n"
1164 "<div class='header_age'>%s</div>\n"
1165 "<div id='header_commit_msg_title'>Message:</div>\n"
1166 "<div id='header_commit_msg'>%s</div>\n"
1167 "</div>\n" /* #diff_header */
1168 "</div>\n" /* #diff_header_wrapper */
1169 "<div class='dotted_line'></div>\n"
1170 "<div id='diff'>\n",
1171 rc->parent_id, rc->commit_id,
1172 rc->commit_id,
1173 rc->tree_id,
1174 author,
1175 age,
1176 msg);
1177 if (r == -1)
1178 goto done;
1180 error = got_output_repo_diff(c);
1181 if (error)
1182 goto done;
1184 fcgi_printf(c, "</div>\n"); /* #diff */
1185 fcgi_printf(c, "</div>\n"); /* #diff_content */
1186 done:
1187 free(age);
1188 free(author);
1189 free(msg);
1190 return error;
1193 static const struct got_error *
1194 gotweb_render_summary(struct request *c)
1196 const struct got_error *error = NULL;
1197 struct transport *t = c->t;
1198 struct server *srv = c->srv;
1199 int r;
1201 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1202 goto done;
1204 if (srv->show_repo_description) {
1205 r = fcgi_printf(c,
1206 "<div id='description_title'>Description:</div>\n"
1207 "<div id='description'>%s</div>\n",
1208 t->repo_dir->description ? t->repo_dir->description : "");
1209 if (r == -1)
1210 goto done;
1213 if (srv->show_repo_owner) {
1214 r = fcgi_printf(c,
1215 "<div id='repo_owner_title'>Owner:</div>\n"
1216 "<div id='repo_owner'>%s</div>\n",
1217 t->repo_dir->owner ? t->repo_dir->owner : "");
1218 if (r == -1)
1219 goto done;
1222 if (srv->show_repo_age) {
1223 r = fcgi_printf(c,
1224 "<div id='last_change_title'>Last Change:</div>\n"
1225 "<div id='last_change'>%s</div>\n",
1226 t->repo_dir->age);
1227 if (r == -1)
1228 goto done;
1231 if (srv->show_repo_cloneurl) {
1232 r = fcgi_printf(c,
1233 "<div id='cloneurl_title'>Clone URL:</div>\n"
1234 "<div id='cloneurl'>%s</div>\n",
1235 t->repo_dir->url ? t->repo_dir->url : "");
1236 if (r == -1)
1237 goto done;
1240 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1241 if (r == -1)
1242 goto done;
1244 if (gotweb_render_briefs(c->tp) == -1)
1245 goto done;
1247 error = gotweb_render_tags(c);
1248 if (error) {
1249 log_warnx("%s: %s", __func__, error->msg);
1250 goto done;
1253 error = gotweb_render_branches(c);
1254 if (error)
1255 log_warnx("%s: %s", __func__, error->msg);
1256 done:
1257 return error;
1260 static const struct got_error *
1261 gotweb_render_tag(struct request *c)
1263 const struct got_error *error = NULL;
1264 struct repo_tag *rt = NULL;
1265 struct transport *t = c->t;
1266 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1268 error = got_get_repo_tags(c, 1);
1269 if (error)
1270 goto done;
1272 if (t->tag_count == 0) {
1273 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1274 "bad commit id");
1275 goto done;
1278 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1280 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1281 if (error)
1282 goto done;
1283 error = gotweb_escape_html(&author, rt->tagger);
1284 if (error)
1285 goto done;
1286 error = gotweb_escape_html(&msg, rt->commit_msg);
1287 if (error)
1288 goto done;
1290 tagname = rt->tag_name;
1291 if (strncmp(tagname, "refs/", 5) == 0)
1292 tagname += 5;
1293 error = gotweb_escape_html(&tagname, tagname);
1294 if (error)
1295 goto done;
1297 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1298 "<div id='tags_title'>Tag</div>\n"
1299 "</div>\n" /* #tags_title_wrapper */
1300 "<div id='tags_content'>\n"
1301 "<div id='tag_header_wrapper'>\n"
1302 "<div id='tag_header'>\n"
1303 "<div class='header_commit_title'>Commit:</div>\n"
1304 "<div class='header_commit'>%s"
1305 " <span class='refs_str'>(%s)</span></div>\n"
1306 "<div class='header_author_title'>Tagger:</div>\n"
1307 "<div class='header_author'>%s</div>\n"
1308 "<div class='header_age_title'>Date:</div>\n"
1309 "<div class='header_age'>%s</div>\n"
1310 "<div id='header_commit_msg_title'>Message:</div>\n"
1311 "<div id='header_commit_msg'>%s</div>\n"
1312 "</div>\n" /* #tag_header */
1313 "<div class='dotted_line'></div>\n"
1314 "<div id='tag_commit'>\n%s</div>"
1315 "</div>" /* #tag_header_wrapper */
1316 "</div>", /* #tags_content */
1317 rt->commit_id,
1318 tagname,
1319 author,
1320 age,
1321 msg,
1322 rt->tag_commit);
1324 done:
1325 free(age);
1326 free(author);
1327 free(msg);
1328 return error;
1331 static const struct got_error *
1332 gotweb_render_tags(struct request *c)
1334 const struct got_error *error = NULL;
1335 struct repo_tag *rt = NULL;
1336 struct server *srv = c->srv;
1337 struct transport *t = c->t;
1338 struct querystring *qs = t->qs;
1339 struct repo_dir *repo_dir = t->repo_dir;
1340 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1341 int r, commit_found = 0;
1343 if (qs->action == BRIEFS) {
1344 qs->action = TAGS;
1345 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1346 } else
1347 error = got_get_repo_tags(c, srv->max_commits_display);
1348 if (error)
1349 goto done;
1351 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1352 "<div id='tags_title'>Tags</div>\n"
1353 "</div>\n" /* #tags_title_wrapper */
1354 "<div id='tags_content'>\n");
1355 if (r == -1)
1356 goto done;
1358 if (t->tag_count == 0) {
1359 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1360 "This repository contains no tags");
1361 if (r == -1)
1362 goto done;
1365 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1366 if (commit_found == 0 && qs->commit != NULL) {
1367 if (strcmp(qs->commit, rt->commit_id) != 0)
1368 continue;
1369 else
1370 commit_found = 1;
1372 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1373 if (error)
1374 goto done;
1376 tagname = rt->tag_name;
1377 if (strncmp(tagname, "refs/tags/", 10) == 0)
1378 tagname += 10;
1379 error = gotweb_escape_html(&tagname, tagname);
1380 if (error)
1381 goto done;
1383 if (rt->tag_commit != NULL) {
1384 newline = strchr(rt->tag_commit, '\n');
1385 if (newline)
1386 *newline = '\0';
1387 error = gotweb_escape_html(&msg, rt->tag_commit);
1388 if (error)
1389 goto done;
1392 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1393 "<div class='tag'>%s</div>\n"
1394 "<div class='tag_log'>", age, tagname) == -1)
1395 goto done;
1397 r = gotweb_link(c, &(struct gotweb_url){
1398 .action = TAG,
1399 .index_page = -1,
1400 .page = -1,
1401 .path = repo_dir->name,
1402 .commit = rt->commit_id,
1403 }, "%s", msg ? msg : "");
1404 if (r == -1)
1405 goto done;
1407 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1408 "<div class='navs_wrapper'>\n"
1409 "<div class='navs'>") == -1)
1410 goto done;
1412 r = gotweb_link(c, &(struct gotweb_url){
1413 .action = TAG,
1414 .index_page = -1,
1415 .page = -1,
1416 .path = repo_dir->name,
1417 .commit = rt->commit_id,
1418 }, "tag");
1419 if (r == -1)
1420 goto done;
1422 if (fcgi_printf(c, " | ") == -1)
1423 goto done;
1425 r = gotweb_link(c, &(struct gotweb_url){
1426 .action = BRIEFS,
1427 .index_page = -1,
1428 .page = -1,
1429 .path = repo_dir->name,
1430 .commit = rt->commit_id,
1431 }, "commit briefs");
1432 if (r == -1)
1433 goto done;
1435 if (fcgi_printf(c, " | ") == -1)
1436 goto done;
1438 r = gotweb_link(c, &(struct gotweb_url){
1439 .action = COMMITS,
1440 .index_page = -1,
1441 .page = -1,
1442 .path = repo_dir->name,
1443 .commit = rt->commit_id,
1444 }, "commits");
1445 if (r == -1)
1446 goto done;
1448 r = fcgi_printf(c,
1449 "</div>\n" /* .navs */
1450 "</div>\n" /* .navs_wrapper */
1451 "<div class='dotted_line'></div>\n");
1452 if (r == -1)
1453 goto done;
1455 free(age);
1456 age = NULL;
1457 free(tagname);
1458 tagname = NULL;
1459 free(msg);
1460 msg = NULL;
1462 if (t->next_id || t->prev_id) {
1463 if (gotweb_render_navs(c->tp) == -1)
1464 goto done;
1466 fcgi_printf(c, "</div>\n"); /* #tags_content */
1467 done:
1468 free(age);
1469 free(tagname);
1470 free(msg);
1471 return error;
1474 const struct got_error *
1475 gotweb_escape_html(char **escaped_html, const char *orig_html)
1477 const struct got_error *error = NULL;
1478 struct escape_pair {
1479 char c;
1480 const char *s;
1481 } esc[] = {
1482 { '>', "&gt;" },
1483 { '<', "&lt;" },
1484 { '&', "&amp;" },
1485 { '"', "&quot;" },
1486 { '\'', "&apos;" },
1487 { '\n', "<br />" },
1489 size_t orig_len, len;
1490 int i, j, x;
1492 orig_len = strlen(orig_html);
1493 len = orig_len;
1494 for (i = 0; i < orig_len; i++) {
1495 for (j = 0; j < nitems(esc); j++) {
1496 if (orig_html[i] != esc[j].c)
1497 continue;
1498 len += strlen(esc[j].s) - 1 /* escaped char */;
1502 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1503 if (*escaped_html == NULL)
1504 return got_error_from_errno("calloc");
1506 x = 0;
1507 for (i = 0; i < orig_len; i++) {
1508 int escaped = 0;
1509 for (j = 0; j < nitems(esc); j++) {
1510 if (orig_html[i] != esc[j].c)
1511 continue;
1513 if (strlcat(*escaped_html, esc[j].s, len + 1)
1514 >= len + 1) {
1515 error = got_error(GOT_ERR_NO_SPACE);
1516 goto done;
1518 x += strlen(esc[j].s);
1519 escaped = 1;
1520 break;
1522 if (!escaped) {
1523 (*escaped_html)[x] = orig_html[i];
1524 x++;
1527 done:
1528 if (error) {
1529 free(*escaped_html);
1530 *escaped_html = NULL;
1531 } else {
1532 (*escaped_html)[x] = '\0';
1535 return error;
1538 static inline int
1539 should_urlencode(int c)
1541 if (c <= ' ' || c >= 127)
1542 return 1;
1544 switch (c) {
1545 /* gen-delim */
1546 case ':':
1547 case '/':
1548 case '?':
1549 case '#':
1550 case '[':
1551 case ']':
1552 case '@':
1553 /* sub-delims */
1554 case '!':
1555 case '$':
1556 case '&':
1557 case '\'':
1558 case '(':
1559 case ')':
1560 case '*':
1561 case '+':
1562 case ',':
1563 case ';':
1564 case '=':
1565 /* needed because the URLs are embedded into the HTML */
1566 case '\"':
1567 return 1;
1568 default:
1569 return 0;
1573 static char *
1574 gotweb_urlencode(const char *str)
1576 const char *s;
1577 char *escaped;
1578 size_t i, len;
1579 int a, b;
1581 len = 0;
1582 for (s = str; *s; ++s) {
1583 len++;
1584 if (should_urlencode(*s))
1585 len += 2;
1588 escaped = calloc(1, len + 1);
1589 if (escaped == NULL)
1590 return NULL;
1592 i = 0;
1593 for (s = str; *s; ++s) {
1594 if (should_urlencode(*s)) {
1595 a = (*s & 0xF0) >> 4;
1596 b = (*s & 0x0F);
1598 escaped[i++] = '%';
1599 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1600 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1601 } else
1602 escaped[i++] = *s;
1605 return escaped;
1608 const char *
1609 gotweb_action_name(int action)
1611 switch (action) {
1612 case BLAME:
1613 return "blame";
1614 case BLOB:
1615 return "blob";
1616 case BLOBRAW:
1617 return "blobraw";
1618 case BRIEFS:
1619 return "briefs";
1620 case COMMITS:
1621 return "commits";
1622 case DIFF:
1623 return "diff";
1624 case ERR:
1625 return "err";
1626 case INDEX:
1627 return "index";
1628 case SUMMARY:
1629 return "summary";
1630 case TAG:
1631 return "tag";
1632 case TAGS:
1633 return "tags";
1634 case TREE:
1635 return "tree";
1636 case RSS:
1637 return "rss";
1638 default:
1639 return NULL;
1643 int
1644 gotweb_render_url(struct request *c, struct gotweb_url *url)
1646 const char *sep = "?", *action;
1647 char *tmp;
1648 int r;
1650 action = gotweb_action_name(url->action);
1651 if (action != NULL) {
1652 if (fcgi_printf(c, "?action=%s", action) == -1)
1653 return -1;
1654 sep = "&";
1657 if (url->commit) {
1658 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1659 return -1;
1660 sep = "&";
1663 if (url->previd) {
1664 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1665 return -1;
1666 sep = "&";
1669 if (url->prevset) {
1670 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1671 return -1;
1672 sep = "&";
1675 if (url->file) {
1676 tmp = gotweb_urlencode(url->file);
1677 if (tmp == NULL)
1678 return -1;
1679 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1680 free(tmp);
1681 if (r == -1)
1682 return -1;
1683 sep = "&";
1686 if (url->folder) {
1687 tmp = gotweb_urlencode(url->folder);
1688 if (tmp == NULL)
1689 return -1;
1690 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1691 free(tmp);
1692 if (r == -1)
1693 return -1;
1694 sep = "&";
1697 if (url->headref) {
1698 tmp = gotweb_urlencode(url->headref);
1699 if (tmp == NULL)
1700 return -1;
1701 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1702 free(tmp);
1703 if (r == -1)
1704 return -1;
1705 sep = "&";
1708 if (url->index_page != -1) {
1709 if (fcgi_printf(c, "%sindex_page=%d", sep,
1710 url->index_page) == -1)
1711 return -1;
1712 sep = "&";
1715 if (url->path) {
1716 tmp = gotweb_urlencode(url->path);
1717 if (tmp == NULL)
1718 return -1;
1719 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1720 free(tmp);
1721 if (r == -1)
1722 return -1;
1723 sep = "&";
1726 if (url->page != -1) {
1727 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1728 return -1;
1729 sep = "&";
1732 return 0;
1735 int
1736 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1738 struct template *tp = c->tp;
1739 const char *proto = c->https ? "https" : "http";
1741 if (fcgi_puts(tp, proto) == -1 ||
1742 fcgi_puts(tp, "://") == -1 ||
1743 tp_htmlescape(tp, c->server_name) == -1 ||
1744 tp_htmlescape(tp, c->document_uri) == -1)
1745 return -1;
1747 return gotweb_render_url(c, url);
1750 int
1751 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1753 va_list ap;
1754 int r;
1756 if (fcgi_printf(c, "<a href='") == -1)
1757 return -1;
1759 if (gotweb_render_url(c, url) == -1)
1760 return -1;
1762 if (fcgi_printf(c, "'>") == -1)
1763 return -1;
1765 va_start(ap, fmt);
1766 r = fcgi_vprintf(c, fmt, ap);
1767 va_end(ap);
1768 if (r == -1)
1769 return -1;
1771 if (fcgi_printf(c, "</a>"))
1772 return -1;
1773 return 0;
1776 static struct got_repository *
1777 find_cached_repo(struct server *srv, const char *path)
1779 int i;
1781 for (i = 0; i < srv->ncached_repos; i++) {
1782 if (strcmp(srv->cached_repos[i].path, path) == 0)
1783 return srv->cached_repos[i].repo;
1786 return NULL;
1789 static const struct got_error *
1790 cache_repo(struct got_repository **new, struct server *srv,
1791 struct repo_dir *repo_dir, struct socket *sock)
1793 const struct got_error *error = NULL;
1794 struct got_repository *repo;
1795 struct cached_repo *cr;
1796 int evicted = 0;
1798 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1799 cr = &srv->cached_repos[srv->ncached_repos - 1];
1800 error = got_repo_close(cr->repo);
1801 memset(cr, 0, sizeof(*cr));
1802 srv->ncached_repos--;
1803 if (error)
1804 return error;
1805 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1806 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1807 cr = &srv->cached_repos[0];
1808 evicted = 1;
1809 } else {
1810 cr = &srv->cached_repos[srv->ncached_repos];
1813 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1814 if (error) {
1815 if (evicted) {
1816 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1817 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1819 return error;
1822 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1823 >= sizeof(cr->path)) {
1824 if (evicted) {
1825 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1826 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1828 return got_error(GOT_ERR_NO_SPACE);
1831 cr->repo = repo;
1832 srv->ncached_repos++;
1833 *new = repo;
1834 return NULL;
1837 static const struct got_error *
1838 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1840 const struct got_error *error = NULL;
1841 struct socket *sock = c->sock;
1842 struct server *srv = c->srv;
1843 struct transport *t = c->t;
1844 struct got_repository *repo = NULL;
1845 DIR *dt;
1846 char *dir_test;
1848 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1849 GOTWEB_GIT_DIR) == -1)
1850 return got_error_from_errno("asprintf");
1852 dt = opendir(dir_test);
1853 if (dt == NULL) {
1854 free(dir_test);
1855 } else {
1856 repo_dir->path = dir_test;
1857 dir_test = NULL;
1858 goto done;
1861 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1862 repo_dir->name) == -1)
1863 return got_error_from_errno("asprintf");
1865 dt = opendir(dir_test);
1866 if (dt == NULL) {
1867 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1868 goto err;
1869 } else {
1870 repo_dir->path = dir_test;
1871 dir_test = NULL;
1874 done:
1875 if (srv->respect_exportok &&
1876 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1877 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1878 goto err;
1881 repo = find_cached_repo(srv, repo_dir->path);
1882 if (repo == NULL) {
1883 error = cache_repo(&repo, srv, repo_dir, sock);
1884 if (error)
1885 goto err;
1887 t->repo = repo;
1888 error = gotweb_get_repo_description(&repo_dir->description, srv,
1889 repo_dir->path, dirfd(dt));
1890 if (error)
1891 goto err;
1892 error = got_get_repo_owner(&repo_dir->owner, c);
1893 if (error)
1894 goto err;
1895 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1896 if (error)
1897 goto err;
1898 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1899 dirfd(dt));
1900 err:
1901 free(dir_test);
1902 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1903 error = got_error_from_errno("closedir");
1904 return error;
1907 static const struct got_error *
1908 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1910 const struct got_error *error;
1912 *repo_dir = calloc(1, sizeof(**repo_dir));
1913 if (*repo_dir == NULL)
1914 return got_error_from_errno("calloc");
1916 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1917 error = got_error_from_errno("asprintf");
1918 free(*repo_dir);
1919 *repo_dir = NULL;
1920 return error;
1922 (*repo_dir)->owner = NULL;
1923 (*repo_dir)->description = NULL;
1924 (*repo_dir)->url = NULL;
1925 (*repo_dir)->age = NULL;
1926 (*repo_dir)->path = NULL;
1928 return NULL;
1931 static const struct got_error *
1932 gotweb_get_repo_description(char **description, struct server *srv,
1933 const char *dirpath, int dir)
1935 const struct got_error *error = NULL;
1936 struct stat sb;
1937 int fd = -1;
1938 off_t len;
1940 *description = NULL;
1941 if (srv->show_repo_description == 0)
1942 return NULL;
1944 fd = openat(dir, "description", O_RDONLY);
1945 if (fd == -1) {
1946 if (errno != ENOENT && errno != EACCES) {
1947 error = got_error_from_errno_fmt("openat %s/%s",
1948 dirpath, "description");
1950 goto done;
1953 if (fstat(fd, &sb) == -1) {
1954 error = got_error_from_errno_fmt("fstat %s/%s",
1955 dirpath, "description");
1956 goto done;
1959 len = sb.st_size;
1960 if (len > GOTWEBD_MAXDESCRSZ - 1)
1961 len = GOTWEBD_MAXDESCRSZ - 1;
1963 *description = calloc(len + 1, sizeof(**description));
1964 if (*description == NULL) {
1965 error = got_error_from_errno("calloc");
1966 goto done;
1969 if (read(fd, *description, len) == -1)
1970 error = got_error_from_errno("read");
1971 done:
1972 if (fd != -1 && close(fd) == -1 && error == NULL)
1973 error = got_error_from_errno("close");
1974 return error;
1977 static const struct got_error *
1978 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1979 int dir)
1981 const struct got_error *error = NULL;
1982 struct stat sb;
1983 int fd = -1;
1984 off_t len;
1986 *url = NULL;
1987 if (srv->show_repo_cloneurl == 0)
1988 return NULL;
1990 fd = openat(dir, "cloneurl", O_RDONLY);
1991 if (fd == -1) {
1992 if (errno != ENOENT && errno != EACCES) {
1993 error = got_error_from_errno_fmt("openat %s/%s",
1994 dirpath, "cloneurl");
1996 goto done;
1999 if (fstat(fd, &sb) == -1) {
2000 error = got_error_from_errno_fmt("fstat %s/%s",
2001 dirpath, "cloneurl");
2002 goto done;
2005 len = sb.st_size;
2006 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2007 len = GOTWEBD_MAXCLONEURLSZ - 1;
2009 *url = calloc(len + 1, sizeof(**url));
2010 if (*url == NULL) {
2011 error = got_error_from_errno("calloc");
2012 goto done;
2015 if (read(fd, *url, len) == -1)
2016 error = got_error_from_errno("read");
2017 done:
2018 if (fd != -1 && close(fd) == -1 && error == NULL)
2019 error = got_error_from_errno("close");
2020 return error;
2023 const struct got_error *
2024 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2026 struct tm tm;
2027 long long diff_time;
2028 const char *years = "years ago", *months = "months ago";
2029 const char *weeks = "weeks ago", *days = "days ago";
2030 const char *hours = "hours ago", *minutes = "minutes ago";
2031 const char *seconds = "seconds ago", *now = "right now";
2032 char *s;
2033 char datebuf[64];
2034 size_t r;
2036 *repo_age = NULL;
2038 switch (ref_tm) {
2039 case TM_DIFF:
2040 diff_time = time(NULL) - committer_time;
2041 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2042 if (asprintf(repo_age, "%lld %s",
2043 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2044 return got_error_from_errno("asprintf");
2045 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2046 if (asprintf(repo_age, "%lld %s",
2047 (diff_time / 60 / 60 / 24 / (365 / 12)),
2048 months) == -1)
2049 return got_error_from_errno("asprintf");
2050 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2051 if (asprintf(repo_age, "%lld %s",
2052 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2053 return got_error_from_errno("asprintf");
2054 } else if (diff_time > 60 * 60 * 24 * 2) {
2055 if (asprintf(repo_age, "%lld %s",
2056 (diff_time / 60 / 60 / 24), days) == -1)
2057 return got_error_from_errno("asprintf");
2058 } else if (diff_time > 60 * 60 * 2) {
2059 if (asprintf(repo_age, "%lld %s",
2060 (diff_time / 60 / 60), hours) == -1)
2061 return got_error_from_errno("asprintf");
2062 } else if (diff_time > 60 * 2) {
2063 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2064 minutes) == -1)
2065 return got_error_from_errno("asprintf");
2066 } else if (diff_time > 2) {
2067 if (asprintf(repo_age, "%lld %s", diff_time,
2068 seconds) == -1)
2069 return got_error_from_errno("asprintf");
2070 } else {
2071 if (asprintf(repo_age, "%s", now) == -1)
2072 return got_error_from_errno("asprintf");
2074 break;
2075 case TM_LONG:
2076 if (gmtime_r(&committer_time, &tm) == NULL)
2077 return got_error_from_errno("gmtime_r");
2079 s = asctime_r(&tm, datebuf);
2080 if (s == NULL)
2081 return got_error_from_errno("asctime_r");
2083 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2084 return got_error_from_errno("asprintf");
2085 break;
2086 case TM_RFC822:
2087 if (gmtime_r(&committer_time, &tm) == NULL)
2088 return got_error_from_errno("gmtime_r");
2090 r = strftime(datebuf, sizeof(datebuf),
2091 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2092 if (r == 0)
2093 return got_error(GOT_ERR_NO_SPACE);
2095 *repo_age = strdup(datebuf);
2096 if (*repo_age == NULL)
2097 return got_error_from_errno("asprintf");
2098 break;
2100 return NULL;