Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 { "page", PAGE },
64 };
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "blobraw", BLOBRAW },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
75 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 const char *);
90 static const struct got_error *gotweb_load_got_path(struct request *c,
91 struct repo_dir *);
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 const char *, int);
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_diff(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tag(struct request *);
100 static const struct got_error *gotweb_render_tags(struct request *);
101 static const struct got_error *gotweb_render_branches(struct request *);
103 static void gotweb_free_querystring(struct querystring *);
104 static void gotweb_free_repo_dir(struct repo_dir *);
106 struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 void
109 gotweb_process_request(struct request *c)
111 const struct got_error *error = NULL, *error2 = NULL;
112 struct got_blob_object *blob = NULL;
113 struct server *srv = NULL;
114 struct querystring *qs = NULL;
115 struct repo_dir *repo_dir = NULL;
116 uint8_t err[] = "gotwebd experienced an error: ";
117 int r, html = 0, fd = -1;
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
121 if (error) {
122 log_warnx("%s: %s", __func__, error->msg);
123 return;
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
127 return;
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
130 if (srv == NULL) {
131 log_warnx("%s: error server is NULL", __func__);
132 goto err;
134 c->srv = srv;
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
137 if (error) {
138 log_warnx("%s: %s", __func__, error->msg);
139 goto err;
141 c->t->qs = qs;
142 error = gotweb_parse_querystring(&qs, c->querystring);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
148 /*
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
151 * querystring.
152 */
154 if (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == BLOBRAW || qs->action == DIFF) {
156 if (qs->commit == NULL) {
157 error2 = got_error(GOT_ERR_QUERYSTRING);
158 goto render;
162 if (qs->action != INDEX) {
163 error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 if (error)
165 goto done;
166 error = gotweb_load_got_path(c, repo_dir);
167 c->t->repo_dir = repo_dir;
168 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
169 goto err;
172 if (qs->action == BLOBRAW) {
173 error = got_get_repo_commits(c, 1);
174 if (error)
175 goto done;
176 error = got_output_file_blob(c);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
181 goto done;
184 if (qs->action == BLOB) {
185 int binary;
186 struct gotweb_url url = {
187 .index_page = -1,
188 .page = -1,
189 .action = BLOBRAW,
190 .path = qs->path,
191 .commit = qs->commit,
192 .folder = qs->folder,
193 .file = qs->file,
194 };
196 error = got_get_repo_commits(c, 1);
197 if (error)
198 goto done;
200 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
201 if (error2)
202 goto render;
203 if (binary) {
204 fcgi_puts(c->tp, "Status: 302\r\n");
205 fcgi_puts(c->tp, "Location: ");
206 gotweb_render_url(c, &url);
207 fcgi_puts(c->tp, "\r\n\r\n");
208 goto done;
212 if (qs->action == RSS) {
213 error = gotweb_render_content_type_file(c,
214 "application/rss+xml;charset=utf-8",
215 repo_dir->name, ".rss");
216 if (error) {
217 log_warnx("%s: %s", __func__, error->msg);
218 goto err;
221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 if (error) {
223 log_warnx("%s: %s", __func__, error->msg);
224 goto err;
226 if (gotweb_render_rss(c->tp) == -1)
227 goto err;
228 goto done;
231 render:
232 error = gotweb_render_content_type(c, "text/html");
233 if (error) {
234 log_warnx("%s: %s", __func__, error->msg);
235 goto err;
237 html = 1;
239 if (gotweb_render_header(c->tp) == -1)
240 goto err;
242 if (error2) {
243 error = error2;
244 goto err;
247 switch(qs->action) {
248 case BLAME:
249 error = gotweb_render_blame(c);
250 if (error) {
251 log_warnx("%s: %s", __func__, error->msg);
252 goto err;
254 break;
255 case BLOB:
256 if (gotweb_render_blob(c->tp, blob) == -1)
257 goto err;
258 break;
259 case BRIEFS:
260 if (gotweb_render_briefs(c->tp) == -1)
261 goto err;
262 break;
263 case COMMITS:
264 error = got_get_repo_commits(c, srv->max_commits_display);
265 if (error) {
266 log_warnx("%s: %s", __func__, error->msg);
267 goto err;
269 if (gotweb_render_commits(c->tp) == -1)
270 goto err;
271 break;
272 case DIFF:
273 error = gotweb_render_diff(c);
274 if (error) {
275 log_warnx("%s: %s", __func__, error->msg);
276 goto err;
278 break;
279 case INDEX:
280 error = gotweb_render_index(c);
281 if (error) {
282 log_warnx("%s: %s", __func__, error->msg);
283 goto err;
285 break;
286 case SUMMARY:
287 error = gotweb_render_summary(c);
288 if (error) {
289 log_warnx("%s: %s", __func__, error->msg);
290 goto err;
292 break;
293 case TAG:
294 error = gotweb_render_tag(c);
295 if (error) {
296 log_warnx("%s: %s", __func__, error->msg);
297 goto err;
299 break;
300 case TAGS:
301 error = gotweb_render_tags(c);
302 if (error) {
303 log_warnx("%s: %s", __func__, error->msg);
304 goto err;
306 break;
307 case TREE:
308 error = got_get_repo_commits(c, 1);
309 if (error) {
310 log_warnx("%s: %s", __func__, error->msg);
311 goto err;
313 if (gotweb_render_tree(c->tp) == -1)
314 goto err;
315 break;
316 case ERR:
317 default:
318 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
319 "Erorr: Bad Querystring");
320 if (r == -1)
321 goto err;
322 break;
325 goto done;
326 err:
327 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
328 return;
329 if (fcgi_printf(c, "\n%s", err) == -1)
330 return;
331 if (error) {
332 if (fcgi_printf(c, "%s", error->msg) == -1)
333 return;
334 } else {
335 if (fcgi_printf(c, "see daemon logs for details") == -1)
336 return;
338 if (html && fcgi_printf(c, "</div>\n") == -1)
339 return;
340 done:
341 if (blob)
342 got_object_blob_close(blob);
343 if (fd != -1)
344 close(fd);
345 if (html && srv != NULL)
346 gotweb_render_footer(c->tp);
349 struct server *
350 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
352 struct server *srv = NULL;
354 /* check against the server name first */
355 if (strlen(server_name) > 0)
356 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
357 if (strcmp(srv->name, server_name) == 0)
358 goto done;
360 /* check against subdomain second */
361 if (strlen(subdomain) > 0)
362 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
363 if (strcmp(srv->name, subdomain) == 0)
364 goto done;
366 /* if those fail, send first server */
367 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
368 if (srv != NULL)
369 break;
370 done:
371 return srv;
372 };
374 const struct got_error *
375 gotweb_init_transport(struct transport **t)
377 const struct got_error *error = NULL;
379 *t = calloc(1, sizeof(**t));
380 if (*t == NULL)
381 return got_error_from_errno2("%s: calloc", __func__);
383 TAILQ_INIT(&(*t)->repo_commits);
384 TAILQ_INIT(&(*t)->repo_tags);
386 (*t)->repo = NULL;
387 (*t)->repo_dir = NULL;
388 (*t)->qs = NULL;
389 (*t)->next_id = NULL;
390 (*t)->prev_id = NULL;
391 (*t)->next_disp = 0;
392 (*t)->prev_disp = 0;
394 return error;
397 static const struct got_error *
398 gotweb_init_querystring(struct querystring **qs)
400 const struct got_error *error = NULL;
402 *qs = calloc(1, sizeof(**qs));
403 if (*qs == NULL)
404 return got_error_from_errno2("%s: calloc", __func__);
406 (*qs)->headref = strdup("HEAD");
407 if ((*qs)->headref == NULL) {
408 free(*qs);
409 *qs = NULL;
410 return got_error_from_errno2("%s: strdup", __func__);
413 (*qs)->action = INDEX;
414 (*qs)->commit = NULL;
415 (*qs)->file = NULL;
416 (*qs)->folder = NULL;
417 (*qs)->index_page = 0;
418 (*qs)->path = NULL;
420 return error;
423 static const struct got_error *
424 gotweb_parse_querystring(struct querystring **qs, char *qst)
426 const struct got_error *error = NULL;
427 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
428 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
430 if (qst == NULL)
431 return error;
433 tok1 = strdup(qst);
434 if (tok1 == NULL)
435 return got_error_from_errno2("%s: strdup", __func__);
437 tok1_pair = tok1;
438 tok1_end = tok1;
440 while (tok1_pair != NULL) {
441 strsep(&tok1_end, "&");
443 tok2 = strdup(tok1_pair);
444 if (tok2 == NULL) {
445 free(tok1);
446 return got_error_from_errno2("%s: strdup", __func__);
449 tok2_pair = tok2;
450 tok2_end = tok2;
452 while (tok2_pair != NULL) {
453 strsep(&tok2_end, "=");
454 if (tok2_end) {
455 error = gotweb_assign_querystring(qs, tok2_pair,
456 tok2_end);
457 if (error)
458 goto err;
460 tok2_pair = tok2_end;
462 free(tok2);
463 tok1_pair = tok1_end;
465 free(tok1);
466 return error;
467 err:
468 free(tok2);
469 free(tok1);
470 return error;
473 /*
474 * Adapted from usr.sbin/httpd/httpd.c url_decode.
475 */
476 static const struct got_error *
477 gotweb_urldecode(char *url)
479 char *p, *q;
480 char hex[3];
481 unsigned long x;
483 hex[2] = '\0';
484 p = q = url;
486 while (*p != '\0') {
487 switch (*p) {
488 case '%':
489 /* Encoding character is followed by two hex chars */
490 if (!isxdigit((unsigned char)p[1]) ||
491 !isxdigit((unsigned char)p[2]) ||
492 (p[1] == '0' && p[2] == '0'))
493 return got_error(GOT_ERR_BAD_QUERYSTRING);
495 hex[0] = p[1];
496 hex[1] = p[2];
498 /*
499 * We don't have to validate "hex" because it is
500 * guaranteed to include two hex chars followed by nul.
501 */
502 x = strtoul(hex, NULL, 16);
503 *q = (char)x;
504 p += 2;
505 break;
506 default:
507 *q = *p;
508 break;
510 p++;
511 q++;
513 *q = '\0';
515 return NULL;
518 static const struct got_error *
519 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
521 const struct got_error *error = NULL;
522 const char *errstr;
523 int a_cnt, el_cnt;
525 error = gotweb_urldecode(value);
526 if (error)
527 return error;
529 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
530 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
531 continue;
533 switch (querystring_keys[el_cnt].element) {
534 case ACTION:
535 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
536 if (strcmp(value, action_keys[a_cnt].name) != 0)
537 continue;
538 else if (strcmp(value,
539 action_keys[a_cnt].name) == 0){
540 (*qs)->action =
541 action_keys[a_cnt].action;
542 goto qa_found;
545 (*qs)->action = ERR;
546 qa_found:
547 break;
548 case COMMIT:
549 (*qs)->commit = strdup(value);
550 if ((*qs)->commit == NULL) {
551 error = got_error_from_errno2("%s: strdup",
552 __func__);
553 goto done;
555 break;
556 case RFILE:
557 (*qs)->file = strdup(value);
558 if ((*qs)->file == NULL) {
559 error = got_error_from_errno2("%s: strdup",
560 __func__);
561 goto done;
563 break;
564 case FOLDER:
565 (*qs)->folder = strdup(value);
566 if ((*qs)->folder == NULL) {
567 error = got_error_from_errno2("%s: strdup",
568 __func__);
569 goto done;
571 break;
572 case HEADREF:
573 free((*qs)->headref);
574 (*qs)->headref = strdup(value);
575 if ((*qs)->headref == NULL) {
576 error = got_error_from_errno2("%s: strdup",
577 __func__);
578 goto done;
580 break;
581 case INDEX_PAGE:
582 if (strlen(value) == 0)
583 break;
584 (*qs)->index_page = strtonum(value, INT64_MIN,
585 INT64_MAX, &errstr);
586 if (errstr) {
587 error = got_error_from_errno3("%s: strtonum %s",
588 __func__, errstr);
589 goto done;
591 if ((*qs)->index_page < 0)
592 (*qs)->index_page = 0;
593 break;
594 case PATH:
595 (*qs)->path = strdup(value);
596 if ((*qs)->path == NULL) {
597 error = got_error_from_errno2("%s: strdup",
598 __func__);
599 goto done;
601 break;
602 case PAGE:
603 if (strlen(value) == 0)
604 break;
605 (*qs)->page = strtonum(value, INT64_MIN,
606 INT64_MAX, &errstr);
607 if (errstr) {
608 error = got_error_from_errno3("%s: strtonum %s",
609 __func__, errstr);
610 goto done;
612 if ((*qs)->page < 0)
613 (*qs)->page = 0;
614 break;
615 default:
616 break;
619 done:
620 return error;
623 void
624 gotweb_free_repo_tag(struct repo_tag *rt)
626 if (rt != NULL) {
627 free(rt->commit_id);
628 free(rt->tag_name);
629 free(rt->tag_commit);
630 free(rt->commit_msg);
631 free(rt->tagger);
633 free(rt);
636 void
637 gotweb_free_repo_commit(struct repo_commit *rc)
639 if (rc != NULL) {
640 free(rc->path);
641 free(rc->refs_str);
642 free(rc->commit_id);
643 free(rc->parent_id);
644 free(rc->tree_id);
645 free(rc->author);
646 free(rc->committer);
647 free(rc->commit_msg);
649 free(rc);
652 static void
653 gotweb_free_querystring(struct querystring *qs)
655 if (qs != NULL) {
656 free(qs->commit);
657 free(qs->file);
658 free(qs->folder);
659 free(qs->headref);
660 free(qs->path);
662 free(qs);
665 static void
666 gotweb_free_repo_dir(struct repo_dir *repo_dir)
668 if (repo_dir != NULL) {
669 free(repo_dir->name);
670 free(repo_dir->owner);
671 free(repo_dir->description);
672 free(repo_dir->url);
673 free(repo_dir->age);
674 free(repo_dir->path);
676 free(repo_dir);
679 void
680 gotweb_free_transport(struct transport *t)
682 struct repo_commit *rc = NULL, *trc = NULL;
683 struct repo_tag *rt = NULL, *trt = NULL;
685 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
686 TAILQ_REMOVE(&t->repo_commits, rc, entry);
687 gotweb_free_repo_commit(rc);
689 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
690 TAILQ_REMOVE(&t->repo_tags, rt, entry);
691 gotweb_free_repo_tag(rt);
693 gotweb_free_repo_dir(t->repo_dir);
694 gotweb_free_querystring(t->qs);
695 free(t->next_id);
696 free(t->prev_id);
697 free(t);
700 const struct got_error *
701 gotweb_render_content_type(struct request *c, const char *type)
703 const char *csp = "default-src 'self'; script-src 'none'; "
704 "object-src 'none';";
706 fcgi_printf(c,
707 "Content-Security-Policy: %s\r\n"
708 "Content-Type: %s\r\n\r\n",
709 csp, type);
710 return NULL;
713 const struct got_error *
714 gotweb_render_content_type_file(struct request *c, const char *type,
715 const char *file, const char *suffix)
717 fcgi_printf(c, "Content-type: %s\r\n"
718 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
719 type, file, suffix ? suffix : "");
720 return NULL;
723 void
724 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
725 struct gotweb_url *next, int *have_next)
727 struct transport *t = c->t;
728 struct querystring *qs = t->qs;
729 struct server *srv = c->srv;
731 *have_prev = *have_next = 0;
733 switch(qs->action) {
734 case INDEX:
735 if (qs->index_page > 0) {
736 *have_prev = 1;
737 *prev = (struct gotweb_url){
738 .action = -1,
739 .index_page = qs->index_page - 1,
740 .page = -1,
741 };
743 if (t->next_disp == srv->max_repos_display &&
744 t->repos_total != (qs->index_page + 1) *
745 srv->max_repos_display) {
746 *have_next = 1;
747 *next = (struct gotweb_url){
748 .action = -1,
749 .index_page = qs->index_page + 1,
750 .page = -1,
751 };
753 break;
754 case BRIEFS:
755 if (t->prev_id && qs->commit != NULL &&
756 strcmp(qs->commit, t->prev_id) != 0) {
757 *have_prev = 1;
758 *prev = (struct gotweb_url){
759 .action = BRIEFS,
760 .index_page = -1,
761 .page = qs->page - 1,
762 .path = qs->path,
763 .commit = t->prev_id,
764 .headref = qs->headref,
765 };
767 if (t->next_id) {
768 *have_next = 1;
769 *next = (struct gotweb_url){
770 .action = BRIEFS,
771 .index_page = -1,
772 .page = qs->page + 1,
773 .path = qs->path,
774 .commit = t->next_id,
775 .headref = qs->headref,
776 };
778 break;
779 case COMMITS:
780 if (t->prev_id && qs->commit != NULL &&
781 strcmp(qs->commit, t->prev_id) != 0) {
782 *have_prev = 1;
783 *prev = (struct gotweb_url){
784 .action = COMMITS,
785 .index_page = -1,
786 .page = qs->page - 1,
787 .path = qs->path,
788 .commit = t->prev_id,
789 .headref = qs->headref,
790 .folder = qs->folder,
791 .file = qs->file,
792 };
794 if (t->next_id) {
795 *have_next = 1;
796 *next = (struct gotweb_url){
797 .action = COMMITS,
798 .index_page = -1,
799 .page = qs->page + 1,
800 .path = qs->path,
801 .commit = t->next_id,
802 .headref = qs->headref,
803 .folder = qs->folder,
804 .file = qs->file,
805 };
807 break;
808 case TAGS:
809 if (t->prev_id && qs->commit != NULL &&
810 strcmp(qs->commit, t->prev_id) != 0) {
811 *have_prev = 1;
812 *prev = (struct gotweb_url){
813 .action = TAGS,
814 .index_page = -1,
815 .page = qs->page - 1,
816 .path = qs->path,
817 .commit = t->prev_id,
818 .headref = qs->headref,
819 };
821 if (t->next_id) {
822 *have_next = 1;
823 *next = (struct gotweb_url){
824 .action = TAGS,
825 .index_page = -1,
826 .page = qs->page + 1,
827 .path = qs->path,
828 .commit = t->next_id,
829 .headref = qs->headref,
830 };
832 break;
836 static const struct got_error *
837 gotweb_render_index(struct request *c)
839 const struct got_error *error = NULL;
840 struct server *srv = c->srv;
841 struct transport *t = c->t;
842 struct querystring *qs = t->qs;
843 struct repo_dir *repo_dir = NULL;
844 DIR *d;
845 struct dirent **sd_dent = NULL;
846 unsigned int d_cnt, d_i, d_disp = 0;
847 unsigned int d_skipped = 0;
848 int type;
850 d = opendir(srv->repos_path);
851 if (d == NULL) {
852 error = got_error_from_errno2("opendir", srv->repos_path);
853 return error;
856 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
857 if (d_cnt == -1) {
858 sd_dent = NULL;
859 error = got_error_from_errno2("scandir", srv->repos_path);
860 goto done;
863 if (gotweb_render_repo_table_hdr(c->tp) == -1)
864 goto done;
866 for (d_i = 0; d_i < d_cnt; d_i++) {
867 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
868 break;
870 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
871 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
872 d_skipped++;
873 continue;
876 error = got_path_dirent_type(&type, srv->repos_path,
877 sd_dent[d_i]);
878 if (error)
879 goto done;
880 if (type != DT_DIR) {
881 d_skipped++;
882 continue;
885 if (qs->index_page > 0 && (qs->index_page *
886 srv->max_repos_display) > t->prev_disp) {
887 t->prev_disp++;
888 continue;
891 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
892 if (error)
893 goto done;
895 error = gotweb_load_got_path(c, repo_dir);
896 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
897 error = NULL;
898 gotweb_free_repo_dir(repo_dir);
899 repo_dir = NULL;
900 d_skipped++;
901 continue;
903 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
904 goto done;
906 d_disp++;
907 t->prev_disp++;
909 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
910 goto done;
912 gotweb_free_repo_dir(repo_dir);
913 repo_dir = NULL;
914 t->next_disp++;
915 if (d_disp == srv->max_repos_display)
916 break;
918 t->repos_total = d_cnt - d_skipped;
920 if (srv->max_repos_display == 0)
921 goto done;
922 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
923 goto done;
924 if (t->repos_total <= srv->max_repos ||
925 t->repos_total <= srv->max_repos_display)
926 goto done;
928 if (gotweb_render_navs(c->tp) == -1)
929 goto done;
930 done:
931 if (sd_dent) {
932 for (d_i = 0; d_i < d_cnt; d_i++)
933 free(sd_dent[d_i]);
934 free(sd_dent);
936 if (d != NULL && closedir(d) == EOF && error == NULL)
937 error = got_error_from_errno("closedir");
938 return error;
941 static const struct got_error *
942 gotweb_render_blame(struct request *c)
944 const struct got_error *error = NULL;
945 struct transport *t = c->t;
946 struct repo_commit *rc = NULL;
947 char *age = NULL, *msg = NULL;
948 int r;
950 error = got_get_repo_commits(c, 1);
951 if (error)
952 return error;
954 rc = TAILQ_FIRST(&t->repo_commits);
956 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
957 if (error)
958 goto done;
959 error = gotweb_escape_html(&msg, rc->commit_msg);
960 if (error)
961 goto done;
963 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
964 "<div id='blame_title'>Blame</div>\n"
965 "</div>\n" /* #blame_title_wrapper */
966 "<div id='blame_content'>\n"
967 "<div id='blame_header_wrapper'>\n"
968 "<div id='blame_header'>\n"
969 "<div class='header_age_title'>Date:</div>\n"
970 "<div class='header_age'>%s</div>\n"
971 "<div id='header_commit_msg_title'>Message:</div>\n"
972 "<div id='header_commit_msg'>%s</div>\n"
973 "</div>\n" /* #blame_header */
974 "</div>\n" /* #blame_header_wrapper */
975 "<div class='dotted_line'></div>\n"
976 "<div id='blame'>\n",
977 age,
978 msg);
979 if (r == -1)
980 goto done;
982 error = got_output_file_blame(c);
983 if (error)
984 goto done;
986 fcgi_printf(c, "</div>\n" /* #blame */
987 "</div>\n"); /* #blame_content */
988 done:
989 free(age);
990 free(msg);
991 return error;
994 static const struct got_error *
995 gotweb_render_branches(struct request *c)
997 const struct got_error *error = NULL;
998 struct got_reflist_head refs;
999 struct got_reflist_entry *re;
1000 struct transport *t = c->t;
1001 struct querystring *qs = t->qs;
1002 struct got_repository *repo = t->repo;
1003 char *escaped_refname = NULL;
1004 char *age = NULL;
1005 int r;
1007 TAILQ_INIT(&refs);
1009 error = got_ref_list(&refs, repo, "refs/heads",
1010 got_ref_cmp_by_name, NULL);
1011 if (error)
1012 goto done;
1014 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1015 "<div id='branches_title'>Branches</div>\n"
1016 "</div>\n" /* #branches_title_wrapper */
1017 "<div id='branches_content'>\n");
1018 if (r == -1)
1019 goto done;
1021 TAILQ_FOREACH(re, &refs, entry) {
1022 const char *refname = NULL;
1024 if (got_ref_is_symbolic(re->ref))
1025 continue;
1027 refname = got_ref_get_name(re->ref);
1028 if (refname == NULL) {
1029 error = got_error_from_errno("strdup");
1030 goto done;
1032 if (strncmp(refname, "refs/heads/", 11) != 0)
1033 continue;
1035 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1036 if (error)
1037 goto done;
1039 if (strncmp(refname, "refs/heads/", 11) == 0)
1040 refname += 11;
1041 error = gotweb_escape_html(&escaped_refname, refname);
1042 if (error)
1043 goto done;
1045 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1046 "<div class='branches_age'>%s</div>\n"
1047 "<div class='branches_space'>&nbsp;</div>\n"
1048 "<div class='branch'>", age);
1049 if (r == -1)
1050 goto done;
1052 r = gotweb_link(c, &(struct gotweb_url){
1053 .action = SUMMARY,
1054 .index_page = -1,
1055 .page = -1,
1056 .path = qs->path,
1057 .headref = refname,
1058 }, "%s", escaped_refname);
1059 if (r == -1)
1060 goto done;
1062 if (fcgi_printf(c, "</div>\n" /* .branch */
1063 "<div class='navs_wrapper'>\n"
1064 "<div class='navs'>") == -1)
1065 goto done;
1067 r = gotweb_link(c, &(struct gotweb_url){
1068 .action = SUMMARY,
1069 .index_page = -1,
1070 .page = -1,
1071 .path = qs->path,
1072 .headref = refname,
1073 }, "summary");
1074 if (r == -1)
1075 goto done;
1077 if (fcgi_printf(c, " | ") == -1)
1078 goto done;
1080 r = gotweb_link(c, &(struct gotweb_url){
1081 .action = BRIEFS,
1082 .index_page = -1,
1083 .page = -1,
1084 .path = qs->path,
1085 .headref = refname,
1086 }, "commit briefs");
1087 if (r == -1)
1088 goto done;
1090 if (fcgi_printf(c, " | ") == -1)
1091 goto done;
1093 r = gotweb_link(c, &(struct gotweb_url){
1094 .action = COMMITS,
1095 .index_page = -1,
1096 .page = -1,
1097 .path = qs->path,
1098 .headref = refname,
1099 }, "commits");
1100 if (r == -1)
1101 goto done;
1103 r = fcgi_printf(c, "</div>\n" /* .navs */
1104 "</div>\n" /* .navs_wrapper */
1105 "<div class='dotted_line'></div>\n"
1106 "</div>\n"); /* .branches_wrapper */
1107 if (r == -1)
1108 goto done;
1110 free(age);
1111 age = NULL;
1112 free(escaped_refname);
1113 escaped_refname = NULL;
1115 fcgi_printf(c, "</div>\n"); /* #branches_content */
1116 done:
1117 free(age);
1118 free(escaped_refname);
1119 got_ref_list_free(&refs);
1120 return error;
1123 static const struct got_error *
1124 gotweb_render_diff(struct request *c)
1126 const struct got_error *error = NULL;
1127 struct transport *t = c->t;
1128 struct repo_commit *rc = NULL;
1129 char *age = NULL, *author = NULL, *msg = NULL;
1130 int r;
1132 error = got_get_repo_commits(c, 1);
1133 if (error)
1134 return error;
1136 rc = TAILQ_FIRST(&t->repo_commits);
1138 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1139 if (error)
1140 goto done;
1141 error = gotweb_escape_html(&author, rc->author);
1142 if (error)
1143 goto done;
1144 error = gotweb_escape_html(&msg, rc->commit_msg);
1145 if (error)
1146 goto done;
1148 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1149 "<div id='diff_title'>Commit Diff</div>\n"
1150 "</div>\n" /* #diff_title_wrapper */
1151 "<div id='diff_content'>\n"
1152 "<div id='diff_header_wrapper'>\n"
1153 "<div id='diff_header'>\n"
1154 "<div id='header_diff_title'>Diff:</div>\n"
1155 "<div id='header_diff'>%s<br />%s</div>\n"
1156 "<div class='header_commit_title'>Commit:</div>\n"
1157 "<div class='header_commit'>%s</div>\n"
1158 "<div id='header_tree_title'>Tree:</div>\n"
1159 "<div id='header_tree'>%s</div>\n"
1160 "<div class='header_author_title'>Author:</div>\n"
1161 "<div class='header_author'>%s</div>\n"
1162 "<div class='header_age_title'>Date:</div>\n"
1163 "<div class='header_age'>%s</div>\n"
1164 "<div id='header_commit_msg_title'>Message:</div>\n"
1165 "<div id='header_commit_msg'>%s</div>\n"
1166 "</div>\n" /* #diff_header */
1167 "</div>\n" /* #diff_header_wrapper */
1168 "<div class='dotted_line'></div>\n"
1169 "<div id='diff'>\n",
1170 rc->parent_id, rc->commit_id,
1171 rc->commit_id,
1172 rc->tree_id,
1173 author,
1174 age,
1175 msg);
1176 if (r == -1)
1177 goto done;
1179 error = got_output_repo_diff(c);
1180 if (error)
1181 goto done;
1183 fcgi_printf(c, "</div>\n"); /* #diff */
1184 fcgi_printf(c, "</div>\n"); /* #diff_content */
1185 done:
1186 free(age);
1187 free(author);
1188 free(msg);
1189 return error;
1192 static const struct got_error *
1193 gotweb_render_summary(struct request *c)
1195 const struct got_error *error = NULL;
1196 struct transport *t = c->t;
1197 struct server *srv = c->srv;
1198 int r;
1200 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1201 goto done;
1203 if (srv->show_repo_description) {
1204 r = fcgi_printf(c,
1205 "<div id='description_title'>Description:</div>\n"
1206 "<div id='description'>%s</div>\n",
1207 t->repo_dir->description ? t->repo_dir->description : "");
1208 if (r == -1)
1209 goto done;
1212 if (srv->show_repo_owner) {
1213 r = fcgi_printf(c,
1214 "<div id='repo_owner_title'>Owner:</div>\n"
1215 "<div id='repo_owner'>%s</div>\n",
1216 t->repo_dir->owner ? t->repo_dir->owner : "");
1217 if (r == -1)
1218 goto done;
1221 if (srv->show_repo_age) {
1222 r = fcgi_printf(c,
1223 "<div id='last_change_title'>Last Change:</div>\n"
1224 "<div id='last_change'>%s</div>\n",
1225 t->repo_dir->age);
1226 if (r == -1)
1227 goto done;
1230 if (srv->show_repo_cloneurl) {
1231 r = fcgi_printf(c,
1232 "<div id='cloneurl_title'>Clone URL:</div>\n"
1233 "<div id='cloneurl'>%s</div>\n",
1234 t->repo_dir->url ? t->repo_dir->url : "");
1235 if (r == -1)
1236 goto done;
1239 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1240 if (r == -1)
1241 goto done;
1243 if (gotweb_render_briefs(c->tp) == -1)
1244 goto done;
1246 error = gotweb_render_tags(c);
1247 if (error) {
1248 log_warnx("%s: %s", __func__, error->msg);
1249 goto done;
1252 error = gotweb_render_branches(c);
1253 if (error)
1254 log_warnx("%s: %s", __func__, error->msg);
1255 done:
1256 return error;
1259 static const struct got_error *
1260 gotweb_render_tag(struct request *c)
1262 const struct got_error *error = NULL;
1263 struct repo_tag *rt = NULL;
1264 struct transport *t = c->t;
1265 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1267 error = got_get_repo_tags(c, 1);
1268 if (error)
1269 goto done;
1271 if (t->tag_count == 0) {
1272 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1273 "bad commit id");
1274 goto done;
1277 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1279 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1280 if (error)
1281 goto done;
1282 error = gotweb_escape_html(&author, rt->tagger);
1283 if (error)
1284 goto done;
1285 error = gotweb_escape_html(&msg, rt->commit_msg);
1286 if (error)
1287 goto done;
1289 tagname = rt->tag_name;
1290 if (strncmp(tagname, "refs/", 5) == 0)
1291 tagname += 5;
1292 error = gotweb_escape_html(&tagname, tagname);
1293 if (error)
1294 goto done;
1296 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1297 "<div id='tags_title'>Tag</div>\n"
1298 "</div>\n" /* #tags_title_wrapper */
1299 "<div id='tags_content'>\n"
1300 "<div id='tag_header_wrapper'>\n"
1301 "<div id='tag_header'>\n"
1302 "<div class='header_commit_title'>Commit:</div>\n"
1303 "<div class='header_commit'>%s"
1304 " <span class='refs_str'>(%s)</span></div>\n"
1305 "<div class='header_author_title'>Tagger:</div>\n"
1306 "<div class='header_author'>%s</div>\n"
1307 "<div class='header_age_title'>Date:</div>\n"
1308 "<div class='header_age'>%s</div>\n"
1309 "<div id='header_commit_msg_title'>Message:</div>\n"
1310 "<div id='header_commit_msg'>%s</div>\n"
1311 "</div>\n" /* #tag_header */
1312 "<div class='dotted_line'></div>\n"
1313 "<div id='tag_commit'>\n%s</div>"
1314 "</div>" /* #tag_header_wrapper */
1315 "</div>", /* #tags_content */
1316 rt->commit_id,
1317 tagname,
1318 author,
1319 age,
1320 msg,
1321 rt->tag_commit);
1323 done:
1324 free(age);
1325 free(author);
1326 free(msg);
1327 return error;
1330 static const struct got_error *
1331 gotweb_render_tags(struct request *c)
1333 const struct got_error *error = NULL;
1334 struct repo_tag *rt = NULL;
1335 struct server *srv = c->srv;
1336 struct transport *t = c->t;
1337 struct querystring *qs = t->qs;
1338 struct repo_dir *repo_dir = t->repo_dir;
1339 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1340 int r, commit_found = 0;
1342 if (qs->action == BRIEFS) {
1343 qs->action = TAGS;
1344 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1345 } else
1346 error = got_get_repo_tags(c, srv->max_commits_display);
1347 if (error)
1348 goto done;
1350 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1351 "<div id='tags_title'>Tags</div>\n"
1352 "</div>\n" /* #tags_title_wrapper */
1353 "<div id='tags_content'>\n");
1354 if (r == -1)
1355 goto done;
1357 if (t->tag_count == 0) {
1358 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1359 "This repository contains no tags");
1360 if (r == -1)
1361 goto done;
1364 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1365 if (commit_found == 0 && qs->commit != NULL) {
1366 if (strcmp(qs->commit, rt->commit_id) != 0)
1367 continue;
1368 else
1369 commit_found = 1;
1371 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1372 if (error)
1373 goto done;
1375 tagname = rt->tag_name;
1376 if (strncmp(tagname, "refs/tags/", 10) == 0)
1377 tagname += 10;
1378 error = gotweb_escape_html(&tagname, tagname);
1379 if (error)
1380 goto done;
1382 if (rt->tag_commit != NULL) {
1383 newline = strchr(rt->tag_commit, '\n');
1384 if (newline)
1385 *newline = '\0';
1386 error = gotweb_escape_html(&msg, rt->tag_commit);
1387 if (error)
1388 goto done;
1391 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1392 "<div class='tag'>%s</div>\n"
1393 "<div class='tag_log'>", age, tagname) == -1)
1394 goto done;
1396 r = gotweb_link(c, &(struct gotweb_url){
1397 .action = TAG,
1398 .index_page = -1,
1399 .page = -1,
1400 .path = repo_dir->name,
1401 .commit = rt->commit_id,
1402 }, "%s", msg ? msg : "");
1403 if (r == -1)
1404 goto done;
1406 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1407 "<div class='navs_wrapper'>\n"
1408 "<div class='navs'>") == -1)
1409 goto done;
1411 r = gotweb_link(c, &(struct gotweb_url){
1412 .action = TAG,
1413 .index_page = -1,
1414 .page = -1,
1415 .path = repo_dir->name,
1416 .commit = rt->commit_id,
1417 }, "tag");
1418 if (r == -1)
1419 goto done;
1421 if (fcgi_printf(c, " | ") == -1)
1422 goto done;
1424 r = gotweb_link(c, &(struct gotweb_url){
1425 .action = BRIEFS,
1426 .index_page = -1,
1427 .page = -1,
1428 .path = repo_dir->name,
1429 .commit = rt->commit_id,
1430 }, "commit briefs");
1431 if (r == -1)
1432 goto done;
1434 if (fcgi_printf(c, " | ") == -1)
1435 goto done;
1437 r = gotweb_link(c, &(struct gotweb_url){
1438 .action = COMMITS,
1439 .index_page = -1,
1440 .page = -1,
1441 .path = repo_dir->name,
1442 .commit = rt->commit_id,
1443 }, "commits");
1444 if (r == -1)
1445 goto done;
1447 r = fcgi_printf(c,
1448 "</div>\n" /* .navs */
1449 "</div>\n" /* .navs_wrapper */
1450 "<div class='dotted_line'></div>\n");
1451 if (r == -1)
1452 goto done;
1454 free(age);
1455 age = NULL;
1456 free(tagname);
1457 tagname = NULL;
1458 free(msg);
1459 msg = NULL;
1461 if (t->next_id || t->prev_id) {
1462 if (gotweb_render_navs(c->tp) == -1)
1463 goto done;
1465 fcgi_printf(c, "</div>\n"); /* #tags_content */
1466 done:
1467 free(age);
1468 free(tagname);
1469 free(msg);
1470 return error;
1473 const struct got_error *
1474 gotweb_escape_html(char **escaped_html, const char *orig_html)
1476 const struct got_error *error = NULL;
1477 struct escape_pair {
1478 char c;
1479 const char *s;
1480 } esc[] = {
1481 { '>', "&gt;" },
1482 { '<', "&lt;" },
1483 { '&', "&amp;" },
1484 { '"', "&quot;" },
1485 { '\'', "&apos;" },
1486 { '\n', "<br />" },
1488 size_t orig_len, len;
1489 int i, j, x;
1491 orig_len = strlen(orig_html);
1492 len = orig_len;
1493 for (i = 0; i < orig_len; i++) {
1494 for (j = 0; j < nitems(esc); j++) {
1495 if (orig_html[i] != esc[j].c)
1496 continue;
1497 len += strlen(esc[j].s) - 1 /* escaped char */;
1501 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1502 if (*escaped_html == NULL)
1503 return got_error_from_errno("calloc");
1505 x = 0;
1506 for (i = 0; i < orig_len; i++) {
1507 int escaped = 0;
1508 for (j = 0; j < nitems(esc); j++) {
1509 if (orig_html[i] != esc[j].c)
1510 continue;
1512 if (strlcat(*escaped_html, esc[j].s, len + 1)
1513 >= len + 1) {
1514 error = got_error(GOT_ERR_NO_SPACE);
1515 goto done;
1517 x += strlen(esc[j].s);
1518 escaped = 1;
1519 break;
1521 if (!escaped) {
1522 (*escaped_html)[x] = orig_html[i];
1523 x++;
1526 done:
1527 if (error) {
1528 free(*escaped_html);
1529 *escaped_html = NULL;
1530 } else {
1531 (*escaped_html)[x] = '\0';
1534 return error;
1537 static inline int
1538 should_urlencode(int c)
1540 if (c <= ' ' || c >= 127)
1541 return 1;
1543 switch (c) {
1544 /* gen-delim */
1545 case ':':
1546 case '/':
1547 case '?':
1548 case '#':
1549 case '[':
1550 case ']':
1551 case '@':
1552 /* sub-delims */
1553 case '!':
1554 case '$':
1555 case '&':
1556 case '\'':
1557 case '(':
1558 case ')':
1559 case '*':
1560 case '+':
1561 case ',':
1562 case ';':
1563 case '=':
1564 /* needed because the URLs are embedded into the HTML */
1565 case '\"':
1566 return 1;
1567 default:
1568 return 0;
1572 static char *
1573 gotweb_urlencode(const char *str)
1575 const char *s;
1576 char *escaped;
1577 size_t i, len;
1578 int a, b;
1580 len = 0;
1581 for (s = str; *s; ++s) {
1582 len++;
1583 if (should_urlencode(*s))
1584 len += 2;
1587 escaped = calloc(1, len + 1);
1588 if (escaped == NULL)
1589 return NULL;
1591 i = 0;
1592 for (s = str; *s; ++s) {
1593 if (should_urlencode(*s)) {
1594 a = (*s & 0xF0) >> 4;
1595 b = (*s & 0x0F);
1597 escaped[i++] = '%';
1598 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1599 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1600 } else
1601 escaped[i++] = *s;
1604 return escaped;
1607 const char *
1608 gotweb_action_name(int action)
1610 switch (action) {
1611 case BLAME:
1612 return "blame";
1613 case BLOB:
1614 return "blob";
1615 case BLOBRAW:
1616 return "blobraw";
1617 case BRIEFS:
1618 return "briefs";
1619 case COMMITS:
1620 return "commits";
1621 case DIFF:
1622 return "diff";
1623 case ERR:
1624 return "err";
1625 case INDEX:
1626 return "index";
1627 case SUMMARY:
1628 return "summary";
1629 case TAG:
1630 return "tag";
1631 case TAGS:
1632 return "tags";
1633 case TREE:
1634 return "tree";
1635 case RSS:
1636 return "rss";
1637 default:
1638 return NULL;
1642 int
1643 gotweb_render_url(struct request *c, struct gotweb_url *url)
1645 const char *sep = "?", *action;
1646 char *tmp;
1647 int r;
1649 action = gotweb_action_name(url->action);
1650 if (action != NULL) {
1651 if (fcgi_printf(c, "?action=%s", action) == -1)
1652 return -1;
1653 sep = "&";
1656 if (url->commit) {
1657 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1658 return -1;
1659 sep = "&";
1662 if (url->previd) {
1663 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1664 return -1;
1665 sep = "&";
1668 if (url->prevset) {
1669 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1670 return -1;
1671 sep = "&";
1674 if (url->file) {
1675 tmp = gotweb_urlencode(url->file);
1676 if (tmp == NULL)
1677 return -1;
1678 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1679 free(tmp);
1680 if (r == -1)
1681 return -1;
1682 sep = "&";
1685 if (url->folder) {
1686 tmp = gotweb_urlencode(url->folder);
1687 if (tmp == NULL)
1688 return -1;
1689 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1690 free(tmp);
1691 if (r == -1)
1692 return -1;
1693 sep = "&";
1696 if (url->headref) {
1697 tmp = gotweb_urlencode(url->headref);
1698 if (tmp == NULL)
1699 return -1;
1700 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1701 free(tmp);
1702 if (r == -1)
1703 return -1;
1704 sep = "&";
1707 if (url->index_page != -1) {
1708 if (fcgi_printf(c, "%sindex_page=%d", sep,
1709 url->index_page) == -1)
1710 return -1;
1711 sep = "&";
1714 if (url->path) {
1715 tmp = gotweb_urlencode(url->path);
1716 if (tmp == NULL)
1717 return -1;
1718 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1719 free(tmp);
1720 if (r == -1)
1721 return -1;
1722 sep = "&";
1725 if (url->page != -1) {
1726 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1727 return -1;
1728 sep = "&";
1731 return 0;
1734 int
1735 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1737 struct template *tp = c->tp;
1738 const char *proto = c->https ? "https" : "http";
1740 if (fcgi_puts(tp, proto) == -1 ||
1741 fcgi_puts(tp, "://") == -1 ||
1742 tp_htmlescape(tp, c->server_name) == -1 ||
1743 tp_htmlescape(tp, c->document_uri) == -1)
1744 return -1;
1746 return gotweb_render_url(c, url);
1749 int
1750 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1752 va_list ap;
1753 int r;
1755 if (fcgi_printf(c, "<a href='") == -1)
1756 return -1;
1758 if (gotweb_render_url(c, url) == -1)
1759 return -1;
1761 if (fcgi_printf(c, "'>") == -1)
1762 return -1;
1764 va_start(ap, fmt);
1765 r = fcgi_vprintf(c, fmt, ap);
1766 va_end(ap);
1767 if (r == -1)
1768 return -1;
1770 if (fcgi_printf(c, "</a>"))
1771 return -1;
1772 return 0;
1775 static struct got_repository *
1776 find_cached_repo(struct server *srv, const char *path)
1778 int i;
1780 for (i = 0; i < srv->ncached_repos; i++) {
1781 if (strcmp(srv->cached_repos[i].path, path) == 0)
1782 return srv->cached_repos[i].repo;
1785 return NULL;
1788 static const struct got_error *
1789 cache_repo(struct got_repository **new, struct server *srv,
1790 struct repo_dir *repo_dir, struct socket *sock)
1792 const struct got_error *error = NULL;
1793 struct got_repository *repo;
1794 struct cached_repo *cr;
1795 int evicted = 0;
1797 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1798 cr = &srv->cached_repos[srv->ncached_repos - 1];
1799 error = got_repo_close(cr->repo);
1800 memset(cr, 0, sizeof(*cr));
1801 srv->ncached_repos--;
1802 if (error)
1803 return error;
1804 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1805 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1806 cr = &srv->cached_repos[0];
1807 evicted = 1;
1808 } else {
1809 cr = &srv->cached_repos[srv->ncached_repos];
1812 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1813 if (error) {
1814 if (evicted) {
1815 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1816 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1818 return error;
1821 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1822 >= sizeof(cr->path)) {
1823 if (evicted) {
1824 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1825 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1827 return got_error(GOT_ERR_NO_SPACE);
1830 cr->repo = repo;
1831 srv->ncached_repos++;
1832 *new = repo;
1833 return NULL;
1836 static const struct got_error *
1837 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1839 const struct got_error *error = NULL;
1840 struct socket *sock = c->sock;
1841 struct server *srv = c->srv;
1842 struct transport *t = c->t;
1843 struct got_repository *repo = NULL;
1844 DIR *dt;
1845 char *dir_test;
1847 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1848 GOTWEB_GIT_DIR) == -1)
1849 return got_error_from_errno("asprintf");
1851 dt = opendir(dir_test);
1852 if (dt == NULL) {
1853 free(dir_test);
1854 } else {
1855 repo_dir->path = dir_test;
1856 dir_test = NULL;
1857 goto done;
1860 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1861 repo_dir->name) == -1)
1862 return got_error_from_errno("asprintf");
1864 dt = opendir(dir_test);
1865 if (dt == NULL) {
1866 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1867 goto err;
1868 } else {
1869 repo_dir->path = dir_test;
1870 dir_test = NULL;
1873 done:
1874 if (srv->respect_exportok &&
1875 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1876 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1877 goto err;
1880 repo = find_cached_repo(srv, repo_dir->path);
1881 if (repo == NULL) {
1882 error = cache_repo(&repo, srv, repo_dir, sock);
1883 if (error)
1884 goto err;
1886 t->repo = repo;
1887 error = gotweb_get_repo_description(&repo_dir->description, srv,
1888 repo_dir->path, dirfd(dt));
1889 if (error)
1890 goto err;
1891 error = got_get_repo_owner(&repo_dir->owner, c);
1892 if (error)
1893 goto err;
1894 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1895 if (error)
1896 goto err;
1897 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1898 dirfd(dt));
1899 err:
1900 free(dir_test);
1901 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1902 error = got_error_from_errno("closedir");
1903 return error;
1906 static const struct got_error *
1907 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1909 const struct got_error *error;
1911 *repo_dir = calloc(1, sizeof(**repo_dir));
1912 if (*repo_dir == NULL)
1913 return got_error_from_errno("calloc");
1915 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1916 error = got_error_from_errno("asprintf");
1917 free(*repo_dir);
1918 *repo_dir = NULL;
1919 return error;
1921 (*repo_dir)->owner = NULL;
1922 (*repo_dir)->description = NULL;
1923 (*repo_dir)->url = NULL;
1924 (*repo_dir)->age = NULL;
1925 (*repo_dir)->path = NULL;
1927 return NULL;
1930 static const struct got_error *
1931 gotweb_get_repo_description(char **description, struct server *srv,
1932 const char *dirpath, int dir)
1934 const struct got_error *error = NULL;
1935 struct stat sb;
1936 int fd = -1;
1937 off_t len;
1939 *description = NULL;
1940 if (srv->show_repo_description == 0)
1941 return NULL;
1943 fd = openat(dir, "description", O_RDONLY);
1944 if (fd == -1) {
1945 if (errno != ENOENT && errno != EACCES) {
1946 error = got_error_from_errno_fmt("openat %s/%s",
1947 dirpath, "description");
1949 goto done;
1952 if (fstat(fd, &sb) == -1) {
1953 error = got_error_from_errno_fmt("fstat %s/%s",
1954 dirpath, "description");
1955 goto done;
1958 len = sb.st_size;
1959 if (len > GOTWEBD_MAXDESCRSZ - 1)
1960 len = GOTWEBD_MAXDESCRSZ - 1;
1962 *description = calloc(len + 1, sizeof(**description));
1963 if (*description == NULL) {
1964 error = got_error_from_errno("calloc");
1965 goto done;
1968 if (read(fd, *description, len) == -1)
1969 error = got_error_from_errno("read");
1970 done:
1971 if (fd != -1 && close(fd) == -1 && error == NULL)
1972 error = got_error_from_errno("close");
1973 return error;
1976 static const struct got_error *
1977 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1978 int dir)
1980 const struct got_error *error = NULL;
1981 struct stat sb;
1982 int fd = -1;
1983 off_t len;
1985 *url = NULL;
1986 if (srv->show_repo_cloneurl == 0)
1987 return NULL;
1989 fd = openat(dir, "cloneurl", O_RDONLY);
1990 if (fd == -1) {
1991 if (errno != ENOENT && errno != EACCES) {
1992 error = got_error_from_errno_fmt("openat %s/%s",
1993 dirpath, "cloneurl");
1995 goto done;
1998 if (fstat(fd, &sb) == -1) {
1999 error = got_error_from_errno_fmt("fstat %s/%s",
2000 dirpath, "cloneurl");
2001 goto done;
2004 len = sb.st_size;
2005 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2006 len = GOTWEBD_MAXCLONEURLSZ - 1;
2008 *url = calloc(len + 1, sizeof(**url));
2009 if (*url == NULL) {
2010 error = got_error_from_errno("calloc");
2011 goto done;
2014 if (read(fd, *url, len) == -1)
2015 error = got_error_from_errno("read");
2016 done:
2017 if (fd != -1 && close(fd) == -1 && error == NULL)
2018 error = got_error_from_errno("close");
2019 return error;
2022 const struct got_error *
2023 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2025 struct tm tm;
2026 long long diff_time;
2027 const char *years = "years ago", *months = "months ago";
2028 const char *weeks = "weeks ago", *days = "days ago";
2029 const char *hours = "hours ago", *minutes = "minutes ago";
2030 const char *seconds = "seconds ago", *now = "right now";
2031 char *s;
2032 char datebuf[64];
2033 size_t r;
2035 *repo_age = NULL;
2037 switch (ref_tm) {
2038 case TM_DIFF:
2039 diff_time = time(NULL) - committer_time;
2040 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2041 if (asprintf(repo_age, "%lld %s",
2042 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2043 return got_error_from_errno("asprintf");
2044 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2045 if (asprintf(repo_age, "%lld %s",
2046 (diff_time / 60 / 60 / 24 / (365 / 12)),
2047 months) == -1)
2048 return got_error_from_errno("asprintf");
2049 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2050 if (asprintf(repo_age, "%lld %s",
2051 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2052 return got_error_from_errno("asprintf");
2053 } else if (diff_time > 60 * 60 * 24 * 2) {
2054 if (asprintf(repo_age, "%lld %s",
2055 (diff_time / 60 / 60 / 24), days) == -1)
2056 return got_error_from_errno("asprintf");
2057 } else if (diff_time > 60 * 60 * 2) {
2058 if (asprintf(repo_age, "%lld %s",
2059 (diff_time / 60 / 60), hours) == -1)
2060 return got_error_from_errno("asprintf");
2061 } else if (diff_time > 60 * 2) {
2062 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2063 minutes) == -1)
2064 return got_error_from_errno("asprintf");
2065 } else if (diff_time > 2) {
2066 if (asprintf(repo_age, "%lld %s", diff_time,
2067 seconds) == -1)
2068 return got_error_from_errno("asprintf");
2069 } else {
2070 if (asprintf(repo_age, "%s", now) == -1)
2071 return got_error_from_errno("asprintf");
2073 break;
2074 case TM_LONG:
2075 if (gmtime_r(&committer_time, &tm) == NULL)
2076 return got_error_from_errno("gmtime_r");
2078 s = asctime_r(&tm, datebuf);
2079 if (s == NULL)
2080 return got_error_from_errno("asctime_r");
2082 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2083 return got_error_from_errno("asprintf");
2084 break;
2085 case TM_RFC822:
2086 if (gmtime_r(&committer_time, &tm) == NULL)
2087 return got_error_from_errno("gmtime_r");
2089 r = strftime(datebuf, sizeof(datebuf),
2090 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2091 if (r == 0)
2092 return got_error(GOT_ERR_NO_SPACE);
2094 *repo_age = strdup(datebuf);
2095 if (*repo_age == NULL)
2096 return got_error_from_errno("asprintf");
2097 break;
2099 return NULL;