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 { "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_tree(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 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;
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->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == DIFF)) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
157 goto render;
160 if (qs->action != INDEX) {
161 error = gotweb_init_repo_dir(&repo_dir, qs->path);
162 if (error)
163 goto done;
164 error = gotweb_load_got_path(c, repo_dir);
165 c->t->repo_dir = repo_dir;
166 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 goto err;
170 if (qs->action == BLOB) {
171 error = got_get_repo_commits(c, 1);
172 if (error)
173 goto done;
174 error = got_output_file_blob(c);
175 if (error) {
176 log_warnx("%s: %s", __func__, error->msg);
177 goto err;
179 goto done;
182 if (qs->action == RSS) {
183 error = gotweb_render_content_type_file(c,
184 "application/rss+xml;charset=utf-8",
185 repo_dir->name, ".rss");
186 if (error) {
187 log_warnx("%s: %s", __func__, error->msg);
188 goto err;
191 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
192 if (error) {
193 log_warnx("%s: %s", __func__, error->msg);
194 goto err;
196 if (gotweb_render_rss(c->tp) == -1)
197 goto err;
198 goto done;
201 render:
202 error = gotweb_render_content_type(c, "text/html");
203 if (error) {
204 log_warnx("%s: %s", __func__, error->msg);
205 goto err;
207 html = 1;
209 if (gotweb_render_header(c->tp) == -1)
210 goto err;
212 if (error2) {
213 error = error2;
214 goto err;
217 switch(qs->action) {
218 case BLAME:
219 error = gotweb_render_blame(c);
220 if (error) {
221 log_warnx("%s: %s", __func__, error->msg);
222 goto err;
224 break;
225 case BRIEFS:
226 if (gotweb_render_briefs(c->tp) == -1)
227 goto err;
228 break;
229 case COMMITS:
230 error = got_get_repo_commits(c, srv->max_commits_display);
231 if (error) {
232 log_warnx("%s: %s", __func__, error->msg);
233 goto err;
235 if (gotweb_render_commits(c->tp) == -1)
236 goto err;
237 break;
238 case DIFF:
239 error = gotweb_render_diff(c);
240 if (error) {
241 log_warnx("%s: %s", __func__, error->msg);
242 goto err;
244 break;
245 case INDEX:
246 error = gotweb_render_index(c);
247 if (error) {
248 log_warnx("%s: %s", __func__, error->msg);
249 goto err;
251 break;
252 case SUMMARY:
253 error = gotweb_render_summary(c);
254 if (error) {
255 log_warnx("%s: %s", __func__, error->msg);
256 goto err;
258 break;
259 case TAG:
260 error = gotweb_render_tag(c);
261 if (error) {
262 log_warnx("%s: %s", __func__, error->msg);
263 goto err;
265 break;
266 case TAGS:
267 error = gotweb_render_tags(c);
268 if (error) {
269 log_warnx("%s: %s", __func__, error->msg);
270 goto err;
272 break;
273 case TREE:
274 error = gotweb_render_tree(c);
275 if (error) {
276 log_warnx("%s: %s", __func__, error->msg);
277 goto err;
279 break;
280 case ERR:
281 default:
282 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
283 "Erorr: Bad Querystring");
284 if (r == -1)
285 goto err;
286 break;
289 goto done;
290 err:
291 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
292 return;
293 if (fcgi_printf(c, "\n%s", err) == -1)
294 return;
295 if (error) {
296 if (fcgi_printf(c, "%s", error->msg) == -1)
297 return;
298 } else {
299 if (fcgi_printf(c, "see daemon logs for details") == -1)
300 return;
302 if (html && fcgi_printf(c, "</div>\n") == -1)
303 return;
304 done:
305 if (html && srv != NULL)
306 gotweb_render_footer(c->tp);
309 struct server *
310 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
312 struct server *srv = NULL;
314 /* check against the server name first */
315 if (strlen(server_name) > 0)
316 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
317 if (strcmp(srv->name, server_name) == 0)
318 goto done;
320 /* check against subdomain second */
321 if (strlen(subdomain) > 0)
322 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
323 if (strcmp(srv->name, subdomain) == 0)
324 goto done;
326 /* if those fail, send first server */
327 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
328 if (srv != NULL)
329 break;
330 done:
331 return srv;
332 };
334 const struct got_error *
335 gotweb_init_transport(struct transport **t)
337 const struct got_error *error = NULL;
339 *t = calloc(1, sizeof(**t));
340 if (*t == NULL)
341 return got_error_from_errno2("%s: calloc", __func__);
343 TAILQ_INIT(&(*t)->repo_commits);
344 TAILQ_INIT(&(*t)->repo_tags);
346 (*t)->repo = NULL;
347 (*t)->repo_dir = NULL;
348 (*t)->qs = NULL;
349 (*t)->next_id = NULL;
350 (*t)->prev_id = NULL;
351 (*t)->next_disp = 0;
352 (*t)->prev_disp = 0;
354 return error;
357 static const struct got_error *
358 gotweb_init_querystring(struct querystring **qs)
360 const struct got_error *error = NULL;
362 *qs = calloc(1, sizeof(**qs));
363 if (*qs == NULL)
364 return got_error_from_errno2("%s: calloc", __func__);
366 (*qs)->headref = strdup("HEAD");
367 if ((*qs)->headref == NULL) {
368 free(*qs);
369 *qs = NULL;
370 return got_error_from_errno2("%s: strdup", __func__);
373 (*qs)->action = INDEX;
374 (*qs)->commit = NULL;
375 (*qs)->file = NULL;
376 (*qs)->folder = NULL;
377 (*qs)->index_page = 0;
378 (*qs)->path = NULL;
380 return error;
383 static const struct got_error *
384 gotweb_parse_querystring(struct querystring **qs, char *qst)
386 const struct got_error *error = NULL;
387 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
388 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
390 if (qst == NULL)
391 return error;
393 tok1 = strdup(qst);
394 if (tok1 == NULL)
395 return got_error_from_errno2("%s: strdup", __func__);
397 tok1_pair = tok1;
398 tok1_end = tok1;
400 while (tok1_pair != NULL) {
401 strsep(&tok1_end, "&");
403 tok2 = strdup(tok1_pair);
404 if (tok2 == NULL) {
405 free(tok1);
406 return got_error_from_errno2("%s: strdup", __func__);
409 tok2_pair = tok2;
410 tok2_end = tok2;
412 while (tok2_pair != NULL) {
413 strsep(&tok2_end, "=");
414 if (tok2_end) {
415 error = gotweb_assign_querystring(qs, tok2_pair,
416 tok2_end);
417 if (error)
418 goto err;
420 tok2_pair = tok2_end;
422 free(tok2);
423 tok1_pair = tok1_end;
425 free(tok1);
426 return error;
427 err:
428 free(tok2);
429 free(tok1);
430 return error;
433 /*
434 * Adapted from usr.sbin/httpd/httpd.c url_decode.
435 */
436 static const struct got_error *
437 gotweb_urldecode(char *url)
439 char *p, *q;
440 char hex[3];
441 unsigned long x;
443 hex[2] = '\0';
444 p = q = url;
446 while (*p != '\0') {
447 switch (*p) {
448 case '%':
449 /* Encoding character is followed by two hex chars */
450 if (!isxdigit((unsigned char)p[1]) ||
451 !isxdigit((unsigned char)p[2]) ||
452 (p[1] == '0' && p[2] == '0'))
453 return got_error(GOT_ERR_BAD_QUERYSTRING);
455 hex[0] = p[1];
456 hex[1] = p[2];
458 /*
459 * We don't have to validate "hex" because it is
460 * guaranteed to include two hex chars followed by nul.
461 */
462 x = strtoul(hex, NULL, 16);
463 *q = (char)x;
464 p += 2;
465 break;
466 default:
467 *q = *p;
468 break;
470 p++;
471 q++;
473 *q = '\0';
475 return NULL;
478 static const struct got_error *
479 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
481 const struct got_error *error = NULL;
482 const char *errstr;
483 int a_cnt, el_cnt;
485 error = gotweb_urldecode(value);
486 if (error)
487 return error;
489 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
490 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
491 continue;
493 switch (querystring_keys[el_cnt].element) {
494 case ACTION:
495 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
496 if (strcmp(value, action_keys[a_cnt].name) != 0)
497 continue;
498 else if (strcmp(value,
499 action_keys[a_cnt].name) == 0){
500 (*qs)->action =
501 action_keys[a_cnt].action;
502 goto qa_found;
505 (*qs)->action = ERR;
506 qa_found:
507 break;
508 case COMMIT:
509 (*qs)->commit = strdup(value);
510 if ((*qs)->commit == NULL) {
511 error = got_error_from_errno2("%s: strdup",
512 __func__);
513 goto done;
515 break;
516 case RFILE:
517 (*qs)->file = strdup(value);
518 if ((*qs)->file == NULL) {
519 error = got_error_from_errno2("%s: strdup",
520 __func__);
521 goto done;
523 break;
524 case FOLDER:
525 (*qs)->folder = strdup(value);
526 if ((*qs)->folder == NULL) {
527 error = got_error_from_errno2("%s: strdup",
528 __func__);
529 goto done;
531 break;
532 case HEADREF:
533 free((*qs)->headref);
534 (*qs)->headref = strdup(value);
535 if ((*qs)->headref == NULL) {
536 error = got_error_from_errno2("%s: strdup",
537 __func__);
538 goto done;
540 break;
541 case INDEX_PAGE:
542 if (strlen(value) == 0)
543 break;
544 (*qs)->index_page = strtonum(value, INT64_MIN,
545 INT64_MAX, &errstr);
546 if (errstr) {
547 error = got_error_from_errno3("%s: strtonum %s",
548 __func__, errstr);
549 goto done;
551 if ((*qs)->index_page < 0)
552 (*qs)->index_page = 0;
553 break;
554 case PATH:
555 (*qs)->path = strdup(value);
556 if ((*qs)->path == NULL) {
557 error = got_error_from_errno2("%s: strdup",
558 __func__);
559 goto done;
561 break;
562 case PAGE:
563 if (strlen(value) == 0)
564 break;
565 (*qs)->page = strtonum(value, INT64_MIN,
566 INT64_MAX, &errstr);
567 if (errstr) {
568 error = got_error_from_errno3("%s: strtonum %s",
569 __func__, errstr);
570 goto done;
572 if ((*qs)->page < 0)
573 (*qs)->page = 0;
574 break;
575 default:
576 break;
579 done:
580 return error;
583 void
584 gotweb_free_repo_tag(struct repo_tag *rt)
586 if (rt != NULL) {
587 free(rt->commit_id);
588 free(rt->tag_name);
589 free(rt->tag_commit);
590 free(rt->commit_msg);
591 free(rt->tagger);
593 free(rt);
596 void
597 gotweb_free_repo_commit(struct repo_commit *rc)
599 if (rc != NULL) {
600 free(rc->path);
601 free(rc->refs_str);
602 free(rc->commit_id);
603 free(rc->parent_id);
604 free(rc->tree_id);
605 free(rc->author);
606 free(rc->committer);
607 free(rc->commit_msg);
609 free(rc);
612 static void
613 gotweb_free_querystring(struct querystring *qs)
615 if (qs != NULL) {
616 free(qs->commit);
617 free(qs->file);
618 free(qs->folder);
619 free(qs->headref);
620 free(qs->path);
622 free(qs);
625 static void
626 gotweb_free_repo_dir(struct repo_dir *repo_dir)
628 if (repo_dir != NULL) {
629 free(repo_dir->name);
630 free(repo_dir->owner);
631 free(repo_dir->description);
632 free(repo_dir->url);
633 free(repo_dir->age);
634 free(repo_dir->path);
636 free(repo_dir);
639 void
640 gotweb_free_transport(struct transport *t)
642 struct repo_commit *rc = NULL, *trc = NULL;
643 struct repo_tag *rt = NULL, *trt = NULL;
645 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
646 TAILQ_REMOVE(&t->repo_commits, rc, entry);
647 gotweb_free_repo_commit(rc);
649 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
650 TAILQ_REMOVE(&t->repo_tags, rt, entry);
651 gotweb_free_repo_tag(rt);
653 gotweb_free_repo_dir(t->repo_dir);
654 gotweb_free_querystring(t->qs);
655 free(t->next_id);
656 free(t->prev_id);
657 free(t);
660 const struct got_error *
661 gotweb_render_content_type(struct request *c, const char *type)
663 const char *csp = "default-src 'self'; script-src 'none'; "
664 "object-src 'none';";
666 fcgi_printf(c,
667 "Content-Security-Policy: %s\r\n"
668 "Content-Type: %s\r\n\r\n",
669 csp, type);
670 return NULL;
673 const struct got_error *
674 gotweb_render_content_type_file(struct request *c, const char *type,
675 const char *file, const char *suffix)
677 fcgi_printf(c, "Content-type: %s\r\n"
678 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
679 type, file, suffix ? suffix : "");
680 return NULL;
683 void
684 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
685 struct gotweb_url *next, int *have_next)
687 struct transport *t = c->t;
688 struct querystring *qs = t->qs;
689 struct server *srv = c->srv;
691 *have_prev = *have_next = 0;
693 switch(qs->action) {
694 case INDEX:
695 if (qs->index_page > 0) {
696 *have_prev = 1;
697 *prev = (struct gotweb_url){
698 .action = -1,
699 .index_page = qs->index_page - 1,
700 .page = -1,
701 };
703 if (t->next_disp == srv->max_repos_display &&
704 t->repos_total != (qs->index_page + 1) *
705 srv->max_repos_display) {
706 *have_next = 1;
707 *next = (struct gotweb_url){
708 .action = -1,
709 .index_page = qs->index_page + 1,
710 .page = -1,
711 };
713 break;
714 case BRIEFS:
715 if (t->prev_id && qs->commit != NULL &&
716 strcmp(qs->commit, t->prev_id) != 0) {
717 *have_prev = 1;
718 *prev = (struct gotweb_url){
719 .action = BRIEFS,
720 .index_page = -1,
721 .page = qs->page - 1,
722 .path = qs->path,
723 .commit = t->prev_id,
724 .headref = qs->headref,
725 };
727 if (t->next_id) {
728 *have_next = 1;
729 *next = (struct gotweb_url){
730 .action = BRIEFS,
731 .index_page = -1,
732 .page = qs->page + 1,
733 .path = qs->path,
734 .commit = t->next_id,
735 .headref = qs->headref,
736 };
738 break;
739 case COMMITS:
740 if (t->prev_id && qs->commit != NULL &&
741 strcmp(qs->commit, t->prev_id) != 0) {
742 *have_prev = 1;
743 *prev = (struct gotweb_url){
744 .action = COMMITS,
745 .index_page = -1,
746 .page = qs->page - 1,
747 .path = qs->path,
748 .commit = t->prev_id,
749 .headref = qs->headref,
750 .folder = qs->folder,
751 .file = qs->file,
752 };
754 if (t->next_id) {
755 *have_next = 1;
756 *next = (struct gotweb_url){
757 .action = COMMITS,
758 .index_page = -1,
759 .page = qs->page + 1,
760 .path = qs->path,
761 .commit = t->next_id,
762 .headref = qs->headref,
763 .folder = qs->folder,
764 .file = qs->file,
765 };
767 break;
768 case TAGS:
769 if (t->prev_id && qs->commit != NULL &&
770 strcmp(qs->commit, t->prev_id) != 0) {
771 *have_prev = 1;
772 *prev = (struct gotweb_url){
773 .action = TAGS,
774 .index_page = -1,
775 .page = qs->page - 1,
776 .path = qs->path,
777 .commit = t->prev_id,
778 .headref = qs->headref,
779 };
781 if (t->next_id) {
782 *have_next = 1;
783 *next = (struct gotweb_url){
784 .action = TAGS,
785 .index_page = -1,
786 .page = qs->page + 1,
787 .path = qs->path,
788 .commit = t->next_id,
789 .headref = qs->headref,
790 };
792 break;
796 static const struct got_error *
797 gotweb_render_index(struct request *c)
799 const struct got_error *error = NULL;
800 struct server *srv = c->srv;
801 struct transport *t = c->t;
802 struct querystring *qs = t->qs;
803 struct repo_dir *repo_dir = NULL;
804 DIR *d;
805 struct dirent **sd_dent = NULL;
806 unsigned int d_cnt, d_i, d_disp = 0;
807 unsigned int d_skipped = 0;
808 int type;
810 d = opendir(srv->repos_path);
811 if (d == NULL) {
812 error = got_error_from_errno2("opendir", srv->repos_path);
813 return error;
816 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
817 if (d_cnt == -1) {
818 sd_dent = NULL;
819 error = got_error_from_errno2("scandir", srv->repos_path);
820 goto done;
823 if (gotweb_render_repo_table_hdr(c->tp) == -1)
824 goto done;
826 for (d_i = 0; d_i < d_cnt; d_i++) {
827 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
828 break;
830 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
831 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
832 d_skipped++;
833 continue;
836 error = got_path_dirent_type(&type, srv->repos_path,
837 sd_dent[d_i]);
838 if (error)
839 goto done;
840 if (type != DT_DIR) {
841 d_skipped++;
842 continue;
845 if (qs->index_page > 0 && (qs->index_page *
846 srv->max_repos_display) > t->prev_disp) {
847 t->prev_disp++;
848 continue;
851 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
852 if (error)
853 goto done;
855 error = gotweb_load_got_path(c, repo_dir);
856 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
857 error = NULL;
858 gotweb_free_repo_dir(repo_dir);
859 repo_dir = NULL;
860 d_skipped++;
861 continue;
863 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
864 goto done;
866 d_disp++;
867 t->prev_disp++;
869 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
870 goto done;
872 gotweb_free_repo_dir(repo_dir);
873 repo_dir = NULL;
874 t->next_disp++;
875 if (d_disp == srv->max_repos_display)
876 break;
878 t->repos_total = d_cnt - d_skipped;
880 if (srv->max_repos_display == 0)
881 goto done;
882 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
883 goto done;
884 if (t->repos_total <= srv->max_repos ||
885 t->repos_total <= srv->max_repos_display)
886 goto done;
888 if (gotweb_render_navs(c->tp) == -1)
889 goto done;
890 done:
891 if (sd_dent) {
892 for (d_i = 0; d_i < d_cnt; d_i++)
893 free(sd_dent[d_i]);
894 free(sd_dent);
896 if (d != NULL && closedir(d) == EOF && error == NULL)
897 error = got_error_from_errno("closedir");
898 return error;
901 static const struct got_error *
902 gotweb_render_blame(struct request *c)
904 const struct got_error *error = NULL;
905 struct transport *t = c->t;
906 struct repo_commit *rc = NULL;
907 char *age = NULL, *msg = NULL;
908 int r;
910 error = got_get_repo_commits(c, 1);
911 if (error)
912 return error;
914 rc = TAILQ_FIRST(&t->repo_commits);
916 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
917 if (error)
918 goto done;
919 error = gotweb_escape_html(&msg, rc->commit_msg);
920 if (error)
921 goto done;
923 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
924 "<div id='blame_title'>Blame</div>\n"
925 "</div>\n" /* #blame_title_wrapper */
926 "<div id='blame_content'>\n"
927 "<div id='blame_header_wrapper'>\n"
928 "<div id='blame_header'>\n"
929 "<div class='header_age_title'>Date:</div>\n"
930 "<div class='header_age'>%s</div>\n"
931 "<div id='header_commit_msg_title'>Message:</div>\n"
932 "<div id='header_commit_msg'>%s</div>\n"
933 "</div>\n" /* #blame_header */
934 "</div>\n" /* #blame_header_wrapper */
935 "<div class='dotted_line'></div>\n"
936 "<div id='blame'>\n",
937 age,
938 msg);
939 if (r == -1)
940 goto done;
942 error = got_output_file_blame(c);
943 if (error)
944 goto done;
946 fcgi_printf(c, "</div>\n" /* #blame */
947 "</div>\n"); /* #blame_content */
948 done:
949 free(age);
950 free(msg);
951 return error;
954 static const struct got_error *
955 gotweb_render_branches(struct request *c)
957 const struct got_error *error = NULL;
958 struct got_reflist_head refs;
959 struct got_reflist_entry *re;
960 struct transport *t = c->t;
961 struct querystring *qs = t->qs;
962 struct got_repository *repo = t->repo;
963 char *escaped_refname = NULL;
964 char *age = NULL;
965 int r;
967 TAILQ_INIT(&refs);
969 error = got_ref_list(&refs, repo, "refs/heads",
970 got_ref_cmp_by_name, NULL);
971 if (error)
972 goto done;
974 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
975 "<div id='branches_title'>Branches</div>\n"
976 "</div>\n" /* #branches_title_wrapper */
977 "<div id='branches_content'>\n");
978 if (r == -1)
979 goto done;
981 TAILQ_FOREACH(re, &refs, entry) {
982 const char *refname = NULL;
984 if (got_ref_is_symbolic(re->ref))
985 continue;
987 refname = got_ref_get_name(re->ref);
988 if (refname == NULL) {
989 error = got_error_from_errno("strdup");
990 goto done;
992 if (strncmp(refname, "refs/heads/", 11) != 0)
993 continue;
995 error = got_get_repo_age(&age, c, refname, TM_DIFF);
996 if (error)
997 goto done;
999 if (strncmp(refname, "refs/heads/", 11) == 0)
1000 refname += 11;
1001 error = gotweb_escape_html(&escaped_refname, refname);
1002 if (error)
1003 goto done;
1005 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1006 "<div class='branches_age'>%s</div>\n"
1007 "<div class='branches_space'>&nbsp;</div>\n"
1008 "<div class='branch'>", age);
1009 if (r == -1)
1010 goto done;
1012 r = gotweb_link(c, &(struct gotweb_url){
1013 .action = SUMMARY,
1014 .index_page = -1,
1015 .page = -1,
1016 .path = qs->path,
1017 .headref = refname,
1018 }, "%s", escaped_refname);
1019 if (r == -1)
1020 goto done;
1022 if (fcgi_printf(c, "</div>\n" /* .branch */
1023 "<div class='navs_wrapper'>\n"
1024 "<div class='navs'>") == -1)
1025 goto done;
1027 r = gotweb_link(c, &(struct gotweb_url){
1028 .action = SUMMARY,
1029 .index_page = -1,
1030 .page = -1,
1031 .path = qs->path,
1032 .headref = refname,
1033 }, "summary");
1034 if (r == -1)
1035 goto done;
1037 if (fcgi_printf(c, " | ") == -1)
1038 goto done;
1040 r = gotweb_link(c, &(struct gotweb_url){
1041 .action = BRIEFS,
1042 .index_page = -1,
1043 .page = -1,
1044 .path = qs->path,
1045 .headref = refname,
1046 }, "commit briefs");
1047 if (r == -1)
1048 goto done;
1050 if (fcgi_printf(c, " | ") == -1)
1051 goto done;
1053 r = gotweb_link(c, &(struct gotweb_url){
1054 .action = COMMITS,
1055 .index_page = -1,
1056 .page = -1,
1057 .path = qs->path,
1058 .headref = refname,
1059 }, "commits");
1060 if (r == -1)
1061 goto done;
1063 r = fcgi_printf(c, "</div>\n" /* .navs */
1064 "</div>\n" /* .navs_wrapper */
1065 "<div class='dotted_line'></div>\n"
1066 "</div>\n"); /* .branches_wrapper */
1067 if (r == -1)
1068 goto done;
1070 free(age);
1071 age = NULL;
1072 free(escaped_refname);
1073 escaped_refname = NULL;
1075 fcgi_printf(c, "</div>\n"); /* #branches_content */
1076 done:
1077 free(age);
1078 free(escaped_refname);
1079 got_ref_list_free(&refs);
1080 return error;
1083 static const struct got_error *
1084 gotweb_render_tree(struct request *c)
1086 const struct got_error *error = NULL;
1087 struct transport *t = c->t;
1088 struct repo_commit *rc = NULL;
1089 char *age = NULL, *msg = NULL;
1090 int r;
1092 error = got_get_repo_commits(c, 1);
1093 if (error)
1094 return error;
1096 rc = TAILQ_FIRST(&t->repo_commits);
1098 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1099 if (error)
1100 goto done;
1102 error = gotweb_escape_html(&msg, rc->commit_msg);
1103 if (error)
1104 goto done;
1106 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1107 "<div id='tree_title'>Tree</div>\n"
1108 "</div>\n" /* #tree_title_wrapper */
1109 "<div id='tree_content'>\n"
1110 "<div id='tree_header_wrapper'>\n"
1111 "<div id='tree_header'>\n"
1112 "<div id='header_tree_title'>Tree:</div>\n"
1113 "<div id='header_tree'>%s</div>\n"
1114 "<div class='header_age_title'>Date:</div>\n"
1115 "<div class='header_age'>%s</div>\n"
1116 "<div id='header_commit_msg_title'>Message:</div>\n"
1117 "<div id='header_commit_msg'>%s</div>\n"
1118 "</div>\n" /* #tree_header */
1119 "</div>\n" /* #tree_header_wrapper */
1120 "<div class='dotted_line'></div>\n"
1121 "<div id='tree'>\n",
1122 rc->tree_id,
1123 age,
1124 msg);
1125 if (r == -1)
1126 goto done;
1128 error = got_output_repo_tree(c);
1129 if (error)
1130 goto done;
1132 fcgi_printf(c, "</div>\n"); /* #tree */
1133 fcgi_printf(c, "</div>\n"); /* #tree_content */
1134 done:
1135 free(age);
1136 free(msg);
1137 return error;
1140 static const struct got_error *
1141 gotweb_render_diff(struct request *c)
1143 const struct got_error *error = NULL;
1144 struct transport *t = c->t;
1145 struct repo_commit *rc = NULL;
1146 char *age = NULL, *author = NULL, *msg = NULL;
1147 int r;
1149 error = got_get_repo_commits(c, 1);
1150 if (error)
1151 return error;
1153 rc = TAILQ_FIRST(&t->repo_commits);
1155 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1156 if (error)
1157 goto done;
1158 error = gotweb_escape_html(&author, rc->author);
1159 if (error)
1160 goto done;
1161 error = gotweb_escape_html(&msg, rc->commit_msg);
1162 if (error)
1163 goto done;
1165 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1166 "<div id='diff_title'>Commit Diff</div>\n"
1167 "</div>\n" /* #diff_title_wrapper */
1168 "<div id='diff_content'>\n"
1169 "<div id='diff_header_wrapper'>\n"
1170 "<div id='diff_header'>\n"
1171 "<div id='header_diff_title'>Diff:</div>\n"
1172 "<div id='header_diff'>%s<br />%s</div>\n"
1173 "<div class='header_commit_title'>Commit:</div>\n"
1174 "<div class='header_commit'>%s</div>\n"
1175 "<div id='header_tree_title'>Tree:</div>\n"
1176 "<div id='header_tree'>%s</div>\n"
1177 "<div class='header_author_title'>Author:</div>\n"
1178 "<div class='header_author'>%s</div>\n"
1179 "<div class='header_age_title'>Date:</div>\n"
1180 "<div class='header_age'>%s</div>\n"
1181 "<div id='header_commit_msg_title'>Message:</div>\n"
1182 "<div id='header_commit_msg'>%s</div>\n"
1183 "</div>\n" /* #diff_header */
1184 "</div>\n" /* #diff_header_wrapper */
1185 "<div class='dotted_line'></div>\n"
1186 "<div id='diff'>\n",
1187 rc->parent_id, rc->commit_id,
1188 rc->commit_id,
1189 rc->tree_id,
1190 author,
1191 age,
1192 msg);
1193 if (r == -1)
1194 goto done;
1196 error = got_output_repo_diff(c);
1197 if (error)
1198 goto done;
1200 fcgi_printf(c, "</div>\n"); /* #diff */
1201 fcgi_printf(c, "</div>\n"); /* #diff_content */
1202 done:
1203 free(age);
1204 free(author);
1205 free(msg);
1206 return error;
1209 static const struct got_error *
1210 gotweb_render_summary(struct request *c)
1212 const struct got_error *error = NULL;
1213 struct transport *t = c->t;
1214 struct server *srv = c->srv;
1215 int r;
1217 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1218 goto done;
1220 if (srv->show_repo_description) {
1221 r = fcgi_printf(c,
1222 "<div id='description_title'>Description:</div>\n"
1223 "<div id='description'>%s</div>\n",
1224 t->repo_dir->description ? t->repo_dir->description : "");
1225 if (r == -1)
1226 goto done;
1229 if (srv->show_repo_owner) {
1230 r = fcgi_printf(c,
1231 "<div id='repo_owner_title'>Owner:</div>\n"
1232 "<div id='repo_owner'>%s</div>\n",
1233 t->repo_dir->owner ? t->repo_dir->owner : "");
1234 if (r == -1)
1235 goto done;
1238 if (srv->show_repo_age) {
1239 r = fcgi_printf(c,
1240 "<div id='last_change_title'>Last Change:</div>\n"
1241 "<div id='last_change'>%s</div>\n",
1242 t->repo_dir->age);
1243 if (r == -1)
1244 goto done;
1247 if (srv->show_repo_cloneurl) {
1248 r = fcgi_printf(c,
1249 "<div id='cloneurl_title'>Clone URL:</div>\n"
1250 "<div id='cloneurl'>%s</div>\n",
1251 t->repo_dir->url ? t->repo_dir->url : "");
1252 if (r == -1)
1253 goto done;
1256 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1257 if (r == -1)
1258 goto done;
1260 if (gotweb_render_briefs(c->tp) == -1)
1261 goto done;
1263 error = gotweb_render_tags(c);
1264 if (error) {
1265 log_warnx("%s: %s", __func__, error->msg);
1266 goto done;
1269 error = gotweb_render_branches(c);
1270 if (error)
1271 log_warnx("%s: %s", __func__, error->msg);
1272 done:
1273 return error;
1276 static const struct got_error *
1277 gotweb_render_tag(struct request *c)
1279 const struct got_error *error = NULL;
1280 struct repo_tag *rt = NULL;
1281 struct transport *t = c->t;
1282 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1284 error = got_get_repo_tags(c, 1);
1285 if (error)
1286 goto done;
1288 if (t->tag_count == 0) {
1289 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1290 "bad commit id");
1291 goto done;
1294 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1296 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1297 if (error)
1298 goto done;
1299 error = gotweb_escape_html(&author, rt->tagger);
1300 if (error)
1301 goto done;
1302 error = gotweb_escape_html(&msg, rt->commit_msg);
1303 if (error)
1304 goto done;
1306 tagname = rt->tag_name;
1307 if (strncmp(tagname, "refs/", 5) == 0)
1308 tagname += 5;
1309 error = gotweb_escape_html(&tagname, tagname);
1310 if (error)
1311 goto done;
1313 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1314 "<div id='tags_title'>Tag</div>\n"
1315 "</div>\n" /* #tags_title_wrapper */
1316 "<div id='tags_content'>\n"
1317 "<div id='tag_header_wrapper'>\n"
1318 "<div id='tag_header'>\n"
1319 "<div class='header_commit_title'>Commit:</div>\n"
1320 "<div class='header_commit'>%s"
1321 " <span class='refs_str'>(%s)</span></div>\n"
1322 "<div class='header_author_title'>Tagger:</div>\n"
1323 "<div class='header_author'>%s</div>\n"
1324 "<div class='header_age_title'>Date:</div>\n"
1325 "<div class='header_age'>%s</div>\n"
1326 "<div id='header_commit_msg_title'>Message:</div>\n"
1327 "<div id='header_commit_msg'>%s</div>\n"
1328 "</div>\n" /* #tag_header */
1329 "<div class='dotted_line'></div>\n"
1330 "<div id='tag_commit'>\n%s</div>"
1331 "</div>" /* #tag_header_wrapper */
1332 "</div>", /* #tags_content */
1333 rt->commit_id,
1334 tagname,
1335 author,
1336 age,
1337 msg,
1338 rt->tag_commit);
1340 done:
1341 free(age);
1342 free(author);
1343 free(msg);
1344 return error;
1347 static const struct got_error *
1348 gotweb_render_tags(struct request *c)
1350 const struct got_error *error = NULL;
1351 struct repo_tag *rt = NULL;
1352 struct server *srv = c->srv;
1353 struct transport *t = c->t;
1354 struct querystring *qs = t->qs;
1355 struct repo_dir *repo_dir = t->repo_dir;
1356 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1357 int r, commit_found = 0;
1359 if (qs->action == BRIEFS) {
1360 qs->action = TAGS;
1361 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1362 } else
1363 error = got_get_repo_tags(c, srv->max_commits_display);
1364 if (error)
1365 goto done;
1367 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1368 "<div id='tags_title'>Tags</div>\n"
1369 "</div>\n" /* #tags_title_wrapper */
1370 "<div id='tags_content'>\n");
1371 if (r == -1)
1372 goto done;
1374 if (t->tag_count == 0) {
1375 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1376 "This repository contains no tags");
1377 if (r == -1)
1378 goto done;
1381 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1382 if (commit_found == 0 && qs->commit != NULL) {
1383 if (strcmp(qs->commit, rt->commit_id) != 0)
1384 continue;
1385 else
1386 commit_found = 1;
1388 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1389 if (error)
1390 goto done;
1392 tagname = rt->tag_name;
1393 if (strncmp(tagname, "refs/tags/", 10) == 0)
1394 tagname += 10;
1395 error = gotweb_escape_html(&tagname, tagname);
1396 if (error)
1397 goto done;
1399 if (rt->tag_commit != NULL) {
1400 newline = strchr(rt->tag_commit, '\n');
1401 if (newline)
1402 *newline = '\0';
1403 error = gotweb_escape_html(&msg, rt->tag_commit);
1404 if (error)
1405 goto done;
1408 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1409 "<div class='tag'>%s</div>\n"
1410 "<div class='tag_log'>", age, tagname) == -1)
1411 goto done;
1413 r = gotweb_link(c, &(struct gotweb_url){
1414 .action = TAG,
1415 .index_page = -1,
1416 .page = -1,
1417 .path = repo_dir->name,
1418 .commit = rt->commit_id,
1419 }, "%s", msg ? msg : "");
1420 if (r == -1)
1421 goto done;
1423 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1424 "<div class='navs_wrapper'>\n"
1425 "<div class='navs'>") == -1)
1426 goto done;
1428 r = gotweb_link(c, &(struct gotweb_url){
1429 .action = TAG,
1430 .index_page = -1,
1431 .page = -1,
1432 .path = repo_dir->name,
1433 .commit = rt->commit_id,
1434 }, "tag");
1435 if (r == -1)
1436 goto done;
1438 if (fcgi_printf(c, " | ") == -1)
1439 goto done;
1441 r = gotweb_link(c, &(struct gotweb_url){
1442 .action = BRIEFS,
1443 .index_page = -1,
1444 .page = -1,
1445 .path = repo_dir->name,
1446 .commit = rt->commit_id,
1447 }, "commit briefs");
1448 if (r == -1)
1449 goto done;
1451 if (fcgi_printf(c, " | ") == -1)
1452 goto done;
1454 r = gotweb_link(c, &(struct gotweb_url){
1455 .action = COMMITS,
1456 .index_page = -1,
1457 .page = -1,
1458 .path = repo_dir->name,
1459 .commit = rt->commit_id,
1460 }, "commits");
1461 if (r == -1)
1462 goto done;
1464 r = fcgi_printf(c,
1465 "</div>\n" /* .navs */
1466 "</div>\n" /* .navs_wrapper */
1467 "<div class='dotted_line'></div>\n");
1468 if (r == -1)
1469 goto done;
1471 free(age);
1472 age = NULL;
1473 free(tagname);
1474 tagname = NULL;
1475 free(msg);
1476 msg = NULL;
1478 if (t->next_id || t->prev_id) {
1479 if (gotweb_render_navs(c->tp) == -1)
1480 goto done;
1482 fcgi_printf(c, "</div>\n"); /* #tags_content */
1483 done:
1484 free(age);
1485 free(tagname);
1486 free(msg);
1487 return error;
1490 const struct got_error *
1491 gotweb_escape_html(char **escaped_html, const char *orig_html)
1493 const struct got_error *error = NULL;
1494 struct escape_pair {
1495 char c;
1496 const char *s;
1497 } esc[] = {
1498 { '>', "&gt;" },
1499 { '<', "&lt;" },
1500 { '&', "&amp;" },
1501 { '"', "&quot;" },
1502 { '\'', "&apos;" },
1503 { '\n', "<br />" },
1505 size_t orig_len, len;
1506 int i, j, x;
1508 orig_len = strlen(orig_html);
1509 len = orig_len;
1510 for (i = 0; i < orig_len; i++) {
1511 for (j = 0; j < nitems(esc); j++) {
1512 if (orig_html[i] != esc[j].c)
1513 continue;
1514 len += strlen(esc[j].s) - 1 /* escaped char */;
1518 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1519 if (*escaped_html == NULL)
1520 return got_error_from_errno("calloc");
1522 x = 0;
1523 for (i = 0; i < orig_len; i++) {
1524 int escaped = 0;
1525 for (j = 0; j < nitems(esc); j++) {
1526 if (orig_html[i] != esc[j].c)
1527 continue;
1529 if (strlcat(*escaped_html, esc[j].s, len + 1)
1530 >= len + 1) {
1531 error = got_error(GOT_ERR_NO_SPACE);
1532 goto done;
1534 x += strlen(esc[j].s);
1535 escaped = 1;
1536 break;
1538 if (!escaped) {
1539 (*escaped_html)[x] = orig_html[i];
1540 x++;
1543 done:
1544 if (error) {
1545 free(*escaped_html);
1546 *escaped_html = NULL;
1547 } else {
1548 (*escaped_html)[x] = '\0';
1551 return error;
1554 static inline int
1555 should_urlencode(int c)
1557 if (c <= ' ' || c >= 127)
1558 return 1;
1560 switch (c) {
1561 /* gen-delim */
1562 case ':':
1563 case '/':
1564 case '?':
1565 case '#':
1566 case '[':
1567 case ']':
1568 case '@':
1569 /* sub-delims */
1570 case '!':
1571 case '$':
1572 case '&':
1573 case '\'':
1574 case '(':
1575 case ')':
1576 case '*':
1577 case '+':
1578 case ',':
1579 case ';':
1580 case '=':
1581 return 1;
1582 default:
1583 return 0;
1587 static char *
1588 gotweb_urlencode(const char *str)
1590 const char *s;
1591 char *escaped;
1592 size_t i, len;
1593 int a, b;
1595 len = 0;
1596 for (s = str; *s; ++s) {
1597 len++;
1598 if (should_urlencode(*s))
1599 len += 2;
1602 escaped = calloc(1, len + 1);
1603 if (escaped == NULL)
1604 return NULL;
1606 i = 0;
1607 for (s = str; *s; ++s) {
1608 if (should_urlencode(*s)) {
1609 a = (*s & 0xF0) >> 4;
1610 b = (*s & 0x0F);
1612 escaped[i++] = '%';
1613 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1614 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1615 } else
1616 escaped[i++] = *s;
1619 return escaped;
1622 const char *
1623 gotweb_action_name(int action)
1625 switch (action) {
1626 case BLAME:
1627 return "blame";
1628 case BLOB:
1629 return "blob";
1630 case BRIEFS:
1631 return "briefs";
1632 case COMMITS:
1633 return "commits";
1634 case DIFF:
1635 return "diff";
1636 case ERR:
1637 return "err";
1638 case INDEX:
1639 return "index";
1640 case SUMMARY:
1641 return "summary";
1642 case TAG:
1643 return "tag";
1644 case TAGS:
1645 return "tags";
1646 case TREE:
1647 return "tree";
1648 case RSS:
1649 return "rss";
1650 default:
1651 return NULL;
1655 int
1656 gotweb_render_url(struct request *c, struct gotweb_url *url)
1658 const char *sep = "?", *action;
1659 char *tmp;
1660 int r;
1662 action = gotweb_action_name(url->action);
1663 if (action != NULL) {
1664 if (fcgi_printf(c, "?action=%s", action) == -1)
1665 return -1;
1666 sep = "&";
1669 if (url->commit) {
1670 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1671 return -1;
1672 sep = "&";
1675 if (url->previd) {
1676 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1677 return -1;
1678 sep = "&";
1681 if (url->prevset) {
1682 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1683 return -1;
1684 sep = "&";
1687 if (url->file) {
1688 tmp = gotweb_urlencode(url->file);
1689 if (tmp == NULL)
1690 return -1;
1691 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1692 free(tmp);
1693 if (r == -1)
1694 return -1;
1695 sep = "&";
1698 if (url->folder) {
1699 tmp = gotweb_urlencode(url->folder);
1700 if (tmp == NULL)
1701 return -1;
1702 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1703 free(tmp);
1704 if (r == -1)
1705 return -1;
1706 sep = "&";
1709 if (url->headref) {
1710 tmp = gotweb_urlencode(url->headref);
1711 if (tmp == NULL)
1712 return -1;
1713 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1714 free(tmp);
1715 if (r == -1)
1716 return -1;
1717 sep = "&";
1720 if (url->index_page != -1) {
1721 if (fcgi_printf(c, "%sindex_page=%d", sep,
1722 url->index_page) == -1)
1723 return -1;
1724 sep = "&";
1727 if (url->path) {
1728 tmp = gotweb_urlencode(url->path);
1729 if (tmp == NULL)
1730 return -1;
1731 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1732 free(tmp);
1733 if (r == -1)
1734 return -1;
1735 sep = "&";
1738 if (url->page != -1) {
1739 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1740 return -1;
1741 sep = "&";
1744 return 0;
1747 int
1748 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1750 struct template *tp = c->tp;
1751 const char *proto = c->https ? "https" : "http";
1753 if (fcgi_puts(tp, proto) == -1 ||
1754 fcgi_puts(tp, "://") == -1 ||
1755 tp_htmlescape(tp, c->server_name) == -1 ||
1756 tp_htmlescape(tp, c->document_uri) == -1)
1757 return -1;
1759 return gotweb_render_url(c, url);
1762 int
1763 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1765 va_list ap;
1766 int r;
1768 if (fcgi_printf(c, "<a href='") == -1)
1769 return -1;
1771 if (gotweb_render_url(c, url) == -1)
1772 return -1;
1774 if (fcgi_printf(c, "'>") == -1)
1775 return -1;
1777 va_start(ap, fmt);
1778 r = fcgi_vprintf(c, fmt, ap);
1779 va_end(ap);
1780 if (r == -1)
1781 return -1;
1783 if (fcgi_printf(c, "</a>"))
1784 return -1;
1785 return 0;
1788 static struct got_repository *
1789 find_cached_repo(struct server *srv, const char *path)
1791 int i;
1793 for (i = 0; i < srv->ncached_repos; i++) {
1794 if (strcmp(srv->cached_repos[i].path, path) == 0)
1795 return srv->cached_repos[i].repo;
1798 return NULL;
1801 static const struct got_error *
1802 cache_repo(struct got_repository **new, struct server *srv,
1803 struct repo_dir *repo_dir, struct socket *sock)
1805 const struct got_error *error = NULL;
1806 struct got_repository *repo;
1807 struct cached_repo *cr;
1808 int evicted = 0;
1810 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1811 cr = &srv->cached_repos[srv->ncached_repos - 1];
1812 error = got_repo_close(cr->repo);
1813 memset(cr, 0, sizeof(*cr));
1814 srv->ncached_repos--;
1815 if (error)
1816 return error;
1817 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1818 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1819 cr = &srv->cached_repos[0];
1820 evicted = 1;
1821 } else {
1822 cr = &srv->cached_repos[srv->ncached_repos];
1825 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1826 if (error) {
1827 if (evicted) {
1828 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1829 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1831 return error;
1834 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1835 >= sizeof(cr->path)) {
1836 if (evicted) {
1837 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1838 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1840 return got_error(GOT_ERR_NO_SPACE);
1843 cr->repo = repo;
1844 srv->ncached_repos++;
1845 *new = repo;
1846 return NULL;
1849 static const struct got_error *
1850 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1852 const struct got_error *error = NULL;
1853 struct socket *sock = c->sock;
1854 struct server *srv = c->srv;
1855 struct transport *t = c->t;
1856 struct got_repository *repo = NULL;
1857 DIR *dt;
1858 char *dir_test;
1860 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1861 GOTWEB_GIT_DIR) == -1)
1862 return got_error_from_errno("asprintf");
1864 dt = opendir(dir_test);
1865 if (dt == NULL) {
1866 free(dir_test);
1867 } else {
1868 repo_dir->path = dir_test;
1869 dir_test = NULL;
1870 goto done;
1873 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1874 repo_dir->name) == -1)
1875 return got_error_from_errno("asprintf");
1877 dt = opendir(dir_test);
1878 if (dt == NULL) {
1879 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1880 goto err;
1881 } else {
1882 repo_dir->path = dir_test;
1883 dir_test = NULL;
1886 done:
1887 if (srv->respect_exportok &&
1888 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1889 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1890 goto err;
1893 repo = find_cached_repo(srv, repo_dir->path);
1894 if (repo == NULL) {
1895 error = cache_repo(&repo, srv, repo_dir, sock);
1896 if (error)
1897 goto err;
1899 t->repo = repo;
1900 error = gotweb_get_repo_description(&repo_dir->description, srv,
1901 repo_dir->path, dirfd(dt));
1902 if (error)
1903 goto err;
1904 error = got_get_repo_owner(&repo_dir->owner, c);
1905 if (error)
1906 goto err;
1907 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1908 if (error)
1909 goto err;
1910 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1911 dirfd(dt));
1912 err:
1913 free(dir_test);
1914 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1915 error = got_error_from_errno("closedir");
1916 return error;
1919 static const struct got_error *
1920 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1922 const struct got_error *error;
1924 *repo_dir = calloc(1, sizeof(**repo_dir));
1925 if (*repo_dir == NULL)
1926 return got_error_from_errno("calloc");
1928 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1929 error = got_error_from_errno("asprintf");
1930 free(*repo_dir);
1931 *repo_dir = NULL;
1932 return error;
1934 (*repo_dir)->owner = NULL;
1935 (*repo_dir)->description = NULL;
1936 (*repo_dir)->url = NULL;
1937 (*repo_dir)->age = NULL;
1938 (*repo_dir)->path = NULL;
1940 return NULL;
1943 static const struct got_error *
1944 gotweb_get_repo_description(char **description, struct server *srv,
1945 const char *dirpath, int dir)
1947 const struct got_error *error = NULL;
1948 struct stat sb;
1949 int fd = -1;
1950 off_t len;
1952 *description = NULL;
1953 if (srv->show_repo_description == 0)
1954 return NULL;
1956 fd = openat(dir, "description", O_RDONLY);
1957 if (fd == -1) {
1958 if (errno != ENOENT && errno != EACCES) {
1959 error = got_error_from_errno_fmt("openat %s/%s",
1960 dirpath, "description");
1962 goto done;
1965 if (fstat(fd, &sb) == -1) {
1966 error = got_error_from_errno_fmt("fstat %s/%s",
1967 dirpath, "description");
1968 goto done;
1971 len = sb.st_size;
1972 if (len > GOTWEBD_MAXDESCRSZ - 1)
1973 len = GOTWEBD_MAXDESCRSZ - 1;
1975 *description = calloc(len + 1, sizeof(**description));
1976 if (*description == NULL) {
1977 error = got_error_from_errno("calloc");
1978 goto done;
1981 if (read(fd, *description, len) == -1)
1982 error = got_error_from_errno("read");
1983 done:
1984 if (fd != -1 && close(fd) == -1 && error == NULL)
1985 error = got_error_from_errno("close");
1986 return error;
1989 static const struct got_error *
1990 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1991 int dir)
1993 const struct got_error *error = NULL;
1994 struct stat sb;
1995 int fd = -1;
1996 off_t len;
1998 *url = NULL;
1999 if (srv->show_repo_cloneurl == 0)
2000 return NULL;
2002 fd = openat(dir, "cloneurl", O_RDONLY);
2003 if (fd == -1) {
2004 if (errno != ENOENT && errno != EACCES) {
2005 error = got_error_from_errno_fmt("openat %s/%s",
2006 dirpath, "cloneurl");
2008 goto done;
2011 if (fstat(fd, &sb) == -1) {
2012 error = got_error_from_errno_fmt("fstat %s/%s",
2013 dirpath, "cloneurl");
2014 goto done;
2017 len = sb.st_size;
2018 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2019 len = GOTWEBD_MAXCLONEURLSZ - 1;
2021 *url = calloc(len + 1, sizeof(**url));
2022 if (*url == NULL) {
2023 error = got_error_from_errno("calloc");
2024 goto done;
2027 if (read(fd, *url, len) == -1)
2028 error = got_error_from_errno("read");
2029 done:
2030 if (fd != -1 && close(fd) == -1 && error == NULL)
2031 error = got_error_from_errno("close");
2032 return error;
2035 const struct got_error *
2036 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2038 struct tm tm;
2039 long long diff_time;
2040 const char *years = "years ago", *months = "months ago";
2041 const char *weeks = "weeks ago", *days = "days ago";
2042 const char *hours = "hours ago", *minutes = "minutes ago";
2043 const char *seconds = "seconds ago", *now = "right now";
2044 char *s;
2045 char datebuf[64];
2046 size_t r;
2048 *repo_age = NULL;
2050 switch (ref_tm) {
2051 case TM_DIFF:
2052 diff_time = time(NULL) - committer_time;
2053 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2054 if (asprintf(repo_age, "%lld %s",
2055 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2056 return got_error_from_errno("asprintf");
2057 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2058 if (asprintf(repo_age, "%lld %s",
2059 (diff_time / 60 / 60 / 24 / (365 / 12)),
2060 months) == -1)
2061 return got_error_from_errno("asprintf");
2062 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2063 if (asprintf(repo_age, "%lld %s",
2064 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2065 return got_error_from_errno("asprintf");
2066 } else if (diff_time > 60 * 60 * 24 * 2) {
2067 if (asprintf(repo_age, "%lld %s",
2068 (diff_time / 60 / 60 / 24), days) == -1)
2069 return got_error_from_errno("asprintf");
2070 } else if (diff_time > 60 * 60 * 2) {
2071 if (asprintf(repo_age, "%lld %s",
2072 (diff_time / 60 / 60), hours) == -1)
2073 return got_error_from_errno("asprintf");
2074 } else if (diff_time > 60 * 2) {
2075 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2076 minutes) == -1)
2077 return got_error_from_errno("asprintf");
2078 } else if (diff_time > 2) {
2079 if (asprintf(repo_age, "%lld %s", diff_time,
2080 seconds) == -1)
2081 return got_error_from_errno("asprintf");
2082 } else {
2083 if (asprintf(repo_age, "%s", now) == -1)
2084 return got_error_from_errno("asprintf");
2086 break;
2087 case TM_LONG:
2088 if (gmtime_r(&committer_time, &tm) == NULL)
2089 return got_error_from_errno("gmtime_r");
2091 s = asctime_r(&tm, datebuf);
2092 if (s == NULL)
2093 return got_error_from_errno("asctime_r");
2095 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2096 return got_error_from_errno("asprintf");
2097 break;
2098 case TM_RFC822:
2099 if (gmtime_r(&committer_time, &tm) == NULL)
2100 return got_error_from_errno("gmtime_r");
2102 r = strftime(datebuf, sizeof(datebuf),
2103 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2104 if (r == 0)
2105 return got_error(GOT_ERR_NO_SPACE);
2107 *repo_age = strdup(datebuf);
2108 if (*repo_age == NULL)
2109 return got_error_from_errno("asprintf");
2110 break;
2112 return NULL;