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(c,
184 "application/rss+xml;charset=utf-8");
185 if (error) {
186 log_warnx("%s: %s", __func__, error->msg);
187 goto err;
190 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
191 if (error) {
192 log_warnx("%s: %s", __func__, error->msg);
193 goto err;
195 if (gotweb_render_rss(c->tp) == -1)
196 goto err;
197 goto done;
200 render:
201 error = gotweb_render_content_type(c, "text/html");
202 if (error) {
203 log_warnx("%s: %s", __func__, error->msg);
204 goto err;
206 html = 1;
208 if (gotweb_render_header(c->tp) == -1)
209 goto err;
211 if (error2) {
212 error = error2;
213 goto err;
216 switch(qs->action) {
217 case BLAME:
218 error = gotweb_render_blame(c);
219 if (error) {
220 log_warnx("%s: %s", __func__, error->msg);
221 goto err;
223 break;
224 case BRIEFS:
225 if (gotweb_render_briefs(c->tp) == -1)
226 goto err;
227 break;
228 case COMMITS:
229 error = got_get_repo_commits(c, srv->max_commits_display);
230 if (error) {
231 log_warnx("%s: %s", __func__, error->msg);
232 goto err;
234 if (gotweb_render_commits(c->tp) == -1)
235 goto err;
236 break;
237 case DIFF:
238 error = gotweb_render_diff(c);
239 if (error) {
240 log_warnx("%s: %s", __func__, error->msg);
241 goto err;
243 break;
244 case INDEX:
245 error = gotweb_render_index(c);
246 if (error) {
247 log_warnx("%s: %s", __func__, error->msg);
248 goto err;
250 break;
251 case SUMMARY:
252 error = gotweb_render_summary(c);
253 if (error) {
254 log_warnx("%s: %s", __func__, error->msg);
255 goto err;
257 break;
258 case TAG:
259 error = gotweb_render_tag(c);
260 if (error) {
261 log_warnx("%s: %s", __func__, error->msg);
262 goto err;
264 break;
265 case TAGS:
266 error = gotweb_render_tags(c);
267 if (error) {
268 log_warnx("%s: %s", __func__, error->msg);
269 goto err;
271 break;
272 case TREE:
273 error = gotweb_render_tree(c);
274 if (error) {
275 log_warnx("%s: %s", __func__, error->msg);
276 goto err;
278 break;
279 case ERR:
280 default:
281 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
282 "Erorr: Bad Querystring");
283 if (r == -1)
284 goto err;
285 break;
288 goto done;
289 err:
290 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
291 return;
292 if (fcgi_printf(c, "\n%s", err) == -1)
293 return;
294 if (error) {
295 if (fcgi_printf(c, "%s", error->msg) == -1)
296 return;
297 } else {
298 if (fcgi_printf(c, "see daemon logs for details") == -1)
299 return;
301 if (html && fcgi_printf(c, "</div>\n") == -1)
302 return;
303 done:
304 if (html && srv != NULL)
305 gotweb_render_footer(c->tp);
308 struct server *
309 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
311 struct server *srv = NULL;
313 /* check against the server name first */
314 if (strlen(server_name) > 0)
315 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
316 if (strcmp(srv->name, server_name) == 0)
317 goto done;
319 /* check against subdomain second */
320 if (strlen(subdomain) > 0)
321 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
322 if (strcmp(srv->name, subdomain) == 0)
323 goto done;
325 /* if those fail, send first server */
326 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
327 if (srv != NULL)
328 break;
329 done:
330 return srv;
331 };
333 const struct got_error *
334 gotweb_init_transport(struct transport **t)
336 const struct got_error *error = NULL;
338 *t = calloc(1, sizeof(**t));
339 if (*t == NULL)
340 return got_error_from_errno2("%s: calloc", __func__);
342 TAILQ_INIT(&(*t)->repo_commits);
343 TAILQ_INIT(&(*t)->repo_tags);
345 (*t)->repo = NULL;
346 (*t)->repo_dir = NULL;
347 (*t)->qs = NULL;
348 (*t)->next_id = NULL;
349 (*t)->prev_id = NULL;
350 (*t)->next_disp = 0;
351 (*t)->prev_disp = 0;
353 return error;
356 static const struct got_error *
357 gotweb_init_querystring(struct querystring **qs)
359 const struct got_error *error = NULL;
361 *qs = calloc(1, sizeof(**qs));
362 if (*qs == NULL)
363 return got_error_from_errno2("%s: calloc", __func__);
365 (*qs)->headref = strdup("HEAD");
366 if ((*qs)->headref == NULL) {
367 free(*qs);
368 *qs = NULL;
369 return got_error_from_errno2("%s: strdup", __func__);
372 (*qs)->action = INDEX;
373 (*qs)->commit = NULL;
374 (*qs)->file = NULL;
375 (*qs)->folder = NULL;
376 (*qs)->index_page = 0;
377 (*qs)->path = NULL;
379 return error;
382 static const struct got_error *
383 gotweb_parse_querystring(struct querystring **qs, char *qst)
385 const struct got_error *error = NULL;
386 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
387 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
389 if (qst == NULL)
390 return error;
392 tok1 = strdup(qst);
393 if (tok1 == NULL)
394 return got_error_from_errno2("%s: strdup", __func__);
396 tok1_pair = tok1;
397 tok1_end = tok1;
399 while (tok1_pair != NULL) {
400 strsep(&tok1_end, "&");
402 tok2 = strdup(tok1_pair);
403 if (tok2 == NULL) {
404 free(tok1);
405 return got_error_from_errno2("%s: strdup", __func__);
408 tok2_pair = tok2;
409 tok2_end = tok2;
411 while (tok2_pair != NULL) {
412 strsep(&tok2_end, "=");
413 if (tok2_end) {
414 error = gotweb_assign_querystring(qs, tok2_pair,
415 tok2_end);
416 if (error)
417 goto err;
419 tok2_pair = tok2_end;
421 free(tok2);
422 tok1_pair = tok1_end;
424 free(tok1);
425 return error;
426 err:
427 free(tok2);
428 free(tok1);
429 return error;
432 /*
433 * Adapted from usr.sbin/httpd/httpd.c url_decode.
434 */
435 static const struct got_error *
436 gotweb_urldecode(char *url)
438 char *p, *q;
439 char hex[3];
440 unsigned long x;
442 hex[2] = '\0';
443 p = q = url;
445 while (*p != '\0') {
446 switch (*p) {
447 case '%':
448 /* Encoding character is followed by two hex chars */
449 if (!isxdigit((unsigned char)p[1]) ||
450 !isxdigit((unsigned char)p[2]) ||
451 (p[1] == '0' && p[2] == '0'))
452 return got_error(GOT_ERR_BAD_QUERYSTRING);
454 hex[0] = p[1];
455 hex[1] = p[2];
457 /*
458 * We don't have to validate "hex" because it is
459 * guaranteed to include two hex chars followed by nul.
460 */
461 x = strtoul(hex, NULL, 16);
462 *q = (char)x;
463 p += 2;
464 break;
465 default:
466 *q = *p;
467 break;
469 p++;
470 q++;
472 *q = '\0';
474 return NULL;
477 static const struct got_error *
478 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
480 const struct got_error *error = NULL;
481 const char *errstr;
482 int a_cnt, el_cnt;
484 error = gotweb_urldecode(value);
485 if (error)
486 return error;
488 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
489 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
490 continue;
492 switch (querystring_keys[el_cnt].element) {
493 case ACTION:
494 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
495 if (strcmp(value, action_keys[a_cnt].name) != 0)
496 continue;
497 else if (strcmp(value,
498 action_keys[a_cnt].name) == 0){
499 (*qs)->action =
500 action_keys[a_cnt].action;
501 goto qa_found;
504 (*qs)->action = ERR;
505 qa_found:
506 break;
507 case COMMIT:
508 (*qs)->commit = strdup(value);
509 if ((*qs)->commit == NULL) {
510 error = got_error_from_errno2("%s: strdup",
511 __func__);
512 goto done;
514 break;
515 case RFILE:
516 (*qs)->file = strdup(value);
517 if ((*qs)->file == NULL) {
518 error = got_error_from_errno2("%s: strdup",
519 __func__);
520 goto done;
522 break;
523 case FOLDER:
524 (*qs)->folder = strdup(value);
525 if ((*qs)->folder == NULL) {
526 error = got_error_from_errno2("%s: strdup",
527 __func__);
528 goto done;
530 break;
531 case HEADREF:
532 free((*qs)->headref);
533 (*qs)->headref = strdup(value);
534 if ((*qs)->headref == NULL) {
535 error = got_error_from_errno2("%s: strdup",
536 __func__);
537 goto done;
539 break;
540 case INDEX_PAGE:
541 if (strlen(value) == 0)
542 break;
543 (*qs)->index_page = strtonum(value, INT64_MIN,
544 INT64_MAX, &errstr);
545 if (errstr) {
546 error = got_error_from_errno3("%s: strtonum %s",
547 __func__, errstr);
548 goto done;
550 if ((*qs)->index_page < 0)
551 (*qs)->index_page = 0;
552 break;
553 case PATH:
554 (*qs)->path = strdup(value);
555 if ((*qs)->path == NULL) {
556 error = got_error_from_errno2("%s: strdup",
557 __func__);
558 goto done;
560 break;
561 case PAGE:
562 if (strlen(value) == 0)
563 break;
564 (*qs)->page = strtonum(value, INT64_MIN,
565 INT64_MAX, &errstr);
566 if (errstr) {
567 error = got_error_from_errno3("%s: strtonum %s",
568 __func__, errstr);
569 goto done;
571 if ((*qs)->page < 0)
572 (*qs)->page = 0;
573 break;
574 default:
575 break;
578 done:
579 return error;
582 void
583 gotweb_free_repo_tag(struct repo_tag *rt)
585 if (rt != NULL) {
586 free(rt->commit_id);
587 free(rt->tag_name);
588 free(rt->tag_commit);
589 free(rt->commit_msg);
590 free(rt->tagger);
592 free(rt);
595 void
596 gotweb_free_repo_commit(struct repo_commit *rc)
598 if (rc != NULL) {
599 free(rc->path);
600 free(rc->refs_str);
601 free(rc->commit_id);
602 free(rc->parent_id);
603 free(rc->tree_id);
604 free(rc->author);
605 free(rc->committer);
606 free(rc->commit_msg);
608 free(rc);
611 static void
612 gotweb_free_querystring(struct querystring *qs)
614 if (qs != NULL) {
615 free(qs->commit);
616 free(qs->file);
617 free(qs->folder);
618 free(qs->headref);
619 free(qs->path);
621 free(qs);
624 static void
625 gotweb_free_repo_dir(struct repo_dir *repo_dir)
627 if (repo_dir != NULL) {
628 free(repo_dir->name);
629 free(repo_dir->owner);
630 free(repo_dir->description);
631 free(repo_dir->url);
632 free(repo_dir->age);
633 free(repo_dir->path);
635 free(repo_dir);
638 void
639 gotweb_free_transport(struct transport *t)
641 struct repo_commit *rc = NULL, *trc = NULL;
642 struct repo_tag *rt = NULL, *trt = NULL;
644 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
645 TAILQ_REMOVE(&t->repo_commits, rc, entry);
646 gotweb_free_repo_commit(rc);
648 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
649 TAILQ_REMOVE(&t->repo_tags, rt, entry);
650 gotweb_free_repo_tag(rt);
652 gotweb_free_repo_dir(t->repo_dir);
653 gotweb_free_querystring(t->qs);
654 free(t->next_id);
655 free(t->prev_id);
656 free(t);
659 const struct got_error *
660 gotweb_render_content_type(struct request *c, const uint8_t *type)
662 const char *csp = "default-src 'self'; script-src 'none'; "
663 "object-src 'none';";
665 fcgi_printf(c,
666 "Content-Security-Policy: %s\r\n"
667 "Content-Type: %s\r\n\r\n",
668 csp, type);
669 return NULL;
672 const struct got_error *
673 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
674 char *file)
676 fcgi_printf(c, "Content-type: %s\r\n"
677 "Content-disposition: attachment; filename=%s\r\n\r\n",
678 type, file);
679 return NULL;
682 void
683 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
684 struct gotweb_url *next, int *have_next)
686 struct transport *t = c->t;
687 struct querystring *qs = t->qs;
688 struct server *srv = c->srv;
690 *have_prev = *have_next = 0;
692 switch(qs->action) {
693 case INDEX:
694 if (qs->index_page > 0) {
695 *have_prev = 1;
696 *prev = (struct gotweb_url){
697 .action = -1,
698 .index_page = qs->index_page - 1,
699 .page = -1,
700 };
702 if (t->next_disp == srv->max_repos_display &&
703 t->repos_total != (qs->index_page + 1) *
704 srv->max_repos_display) {
705 *have_next = 1;
706 *next = (struct gotweb_url){
707 .action = -1,
708 .index_page = qs->index_page + 1,
709 .page = -1,
710 };
712 break;
713 case BRIEFS:
714 if (t->prev_id && qs->commit != NULL &&
715 strcmp(qs->commit, t->prev_id) != 0) {
716 *have_prev = 1;
717 *prev = (struct gotweb_url){
718 .action = BRIEFS,
719 .index_page = -1,
720 .page = qs->page - 1,
721 .path = qs->path,
722 .commit = t->prev_id,
723 .headref = qs->headref,
724 };
726 if (t->next_id) {
727 *have_next = 1;
728 *next = (struct gotweb_url){
729 .action = BRIEFS,
730 .index_page = -1,
731 .page = qs->page + 1,
732 .path = qs->path,
733 .commit = t->next_id,
734 .headref = qs->headref,
735 };
737 break;
738 case COMMITS:
739 if (t->prev_id && qs->commit != NULL &&
740 strcmp(qs->commit, t->prev_id) != 0) {
741 *have_prev = 1;
742 *prev = (struct gotweb_url){
743 .action = COMMITS,
744 .index_page = -1,
745 .page = qs->page - 1,
746 .path = qs->path,
747 .commit = t->prev_id,
748 .headref = qs->headref,
749 .folder = qs->folder,
750 .file = qs->file,
751 };
753 if (t->next_id) {
754 *have_next = 1;
755 *next = (struct gotweb_url){
756 .action = COMMITS,
757 .index_page = -1,
758 .page = qs->page + 1,
759 .path = qs->path,
760 .commit = t->next_id,
761 .headref = qs->headref,
762 .folder = qs->folder,
763 .file = qs->file,
764 };
766 break;
767 case TAGS:
768 if (t->prev_id && qs->commit != NULL &&
769 strcmp(qs->commit, t->prev_id) != 0) {
770 *have_prev = 1;
771 *prev = (struct gotweb_url){
772 .action = TAGS,
773 .index_page = -1,
774 .page = qs->page - 1,
775 .path = qs->path,
776 .commit = t->prev_id,
777 .headref = qs->headref,
778 };
780 if (t->next_id) {
781 *have_next = 1;
782 *next = (struct gotweb_url){
783 .action = TAGS,
784 .index_page = -1,
785 .page = qs->page + 1,
786 .path = qs->path,
787 .commit = t->next_id,
788 .headref = qs->headref,
789 };
791 break;
795 static const struct got_error *
796 gotweb_render_index(struct request *c)
798 const struct got_error *error = NULL;
799 struct server *srv = c->srv;
800 struct transport *t = c->t;
801 struct querystring *qs = t->qs;
802 struct repo_dir *repo_dir = NULL;
803 DIR *d;
804 struct dirent **sd_dent = NULL;
805 unsigned int d_cnt, d_i, d_disp = 0;
806 unsigned int d_skipped = 0;
807 int type;
809 d = opendir(srv->repos_path);
810 if (d == NULL) {
811 error = got_error_from_errno2("opendir", srv->repos_path);
812 return error;
815 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
816 if (d_cnt == -1) {
817 sd_dent = NULL;
818 error = got_error_from_errno2("scandir", srv->repos_path);
819 goto done;
822 if (gotweb_render_repo_table_hdr(c->tp) == -1)
823 goto done;
825 for (d_i = 0; d_i < d_cnt; d_i++) {
826 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
827 break;
829 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
830 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
831 d_skipped++;
832 continue;
835 error = got_path_dirent_type(&type, srv->repos_path,
836 sd_dent[d_i]);
837 if (error)
838 goto done;
839 if (type != DT_DIR) {
840 d_skipped++;
841 continue;
844 if (qs->index_page > 0 && (qs->index_page *
845 srv->max_repos_display) > t->prev_disp) {
846 t->prev_disp++;
847 continue;
850 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
851 if (error)
852 goto done;
854 error = gotweb_load_got_path(c, repo_dir);
855 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
856 error = NULL;
857 gotweb_free_repo_dir(repo_dir);
858 repo_dir = NULL;
859 d_skipped++;
860 continue;
862 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
863 goto done;
865 d_disp++;
866 t->prev_disp++;
868 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
869 goto done;
871 gotweb_free_repo_dir(repo_dir);
872 repo_dir = NULL;
873 t->next_disp++;
874 if (d_disp == srv->max_repos_display)
875 break;
877 t->repos_total = d_cnt - d_skipped;
879 if (srv->max_repos_display == 0)
880 goto done;
881 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
882 goto done;
883 if (t->repos_total <= srv->max_repos ||
884 t->repos_total <= srv->max_repos_display)
885 goto done;
887 if (gotweb_render_navs(c->tp) == -1)
888 goto done;
889 done:
890 if (sd_dent) {
891 for (d_i = 0; d_i < d_cnt; d_i++)
892 free(sd_dent[d_i]);
893 free(sd_dent);
895 if (d != NULL && closedir(d) == EOF && error == NULL)
896 error = got_error_from_errno("closedir");
897 return error;
900 static const struct got_error *
901 gotweb_render_blame(struct request *c)
903 const struct got_error *error = NULL;
904 struct transport *t = c->t;
905 struct repo_commit *rc = NULL;
906 char *age = NULL, *msg = NULL;
907 int r;
909 error = got_get_repo_commits(c, 1);
910 if (error)
911 return error;
913 rc = TAILQ_FIRST(&t->repo_commits);
915 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
916 if (error)
917 goto done;
918 error = gotweb_escape_html(&msg, rc->commit_msg);
919 if (error)
920 goto done;
922 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
923 "<div id='blame_title'>Blame</div>\n"
924 "</div>\n" /* #blame_title_wrapper */
925 "<div id='blame_content'>\n"
926 "<div id='blame_header_wrapper'>\n"
927 "<div id='blame_header'>\n"
928 "<div class='header_age_title'>Date:</div>\n"
929 "<div class='header_age'>%s</div>\n"
930 "<div id='header_commit_msg_title'>Message:</div>\n"
931 "<div id='header_commit_msg'>%s</div>\n"
932 "</div>\n" /* #blame_header */
933 "</div>\n" /* #blame_header_wrapper */
934 "<div class='dotted_line'></div>\n"
935 "<div id='blame'>\n",
936 age,
937 msg);
938 if (r == -1)
939 goto done;
941 error = got_output_file_blame(c);
942 if (error)
943 goto done;
945 fcgi_printf(c, "</div>\n" /* #blame */
946 "</div>\n"); /* #blame_content */
947 done:
948 free(age);
949 free(msg);
950 return error;
953 static const struct got_error *
954 gotweb_render_branches(struct request *c)
956 const struct got_error *error = NULL;
957 struct got_reflist_head refs;
958 struct got_reflist_entry *re;
959 struct transport *t = c->t;
960 struct querystring *qs = t->qs;
961 struct got_repository *repo = t->repo;
962 char *escaped_refname = NULL;
963 char *age = NULL;
964 int r;
966 TAILQ_INIT(&refs);
968 error = got_ref_list(&refs, repo, "refs/heads",
969 got_ref_cmp_by_name, NULL);
970 if (error)
971 goto done;
973 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
974 "<div id='branches_title'>Branches</div>\n"
975 "</div>\n" /* #branches_title_wrapper */
976 "<div id='branches_content'>\n");
977 if (r == -1)
978 goto done;
980 TAILQ_FOREACH(re, &refs, entry) {
981 const char *refname = NULL;
983 if (got_ref_is_symbolic(re->ref))
984 continue;
986 refname = got_ref_get_name(re->ref);
987 if (refname == NULL) {
988 error = got_error_from_errno("strdup");
989 goto done;
991 if (strncmp(refname, "refs/heads/", 11) != 0)
992 continue;
994 error = got_get_repo_age(&age, c, refname, TM_DIFF);
995 if (error)
996 goto done;
998 if (strncmp(refname, "refs/heads/", 11) == 0)
999 refname += 11;
1000 error = gotweb_escape_html(&escaped_refname, refname);
1001 if (error)
1002 goto done;
1004 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1005 "<div class='branches_age'>%s</div>\n"
1006 "<div class='branches_space'>&nbsp;</div>\n"
1007 "<div class='branch'>", age);
1008 if (r == -1)
1009 goto done;
1011 r = gotweb_link(c, &(struct gotweb_url){
1012 .action = SUMMARY,
1013 .index_page = -1,
1014 .page = -1,
1015 .path = qs->path,
1016 .headref = refname,
1017 }, "%s", escaped_refname);
1018 if (r == -1)
1019 goto done;
1021 if (fcgi_printf(c, "</div>\n" /* .branch */
1022 "<div class='navs_wrapper'>\n"
1023 "<div class='navs'>") == -1)
1024 goto done;
1026 r = gotweb_link(c, &(struct gotweb_url){
1027 .action = SUMMARY,
1028 .index_page = -1,
1029 .page = -1,
1030 .path = qs->path,
1031 .headref = refname,
1032 }, "summary");
1033 if (r == -1)
1034 goto done;
1036 if (fcgi_printf(c, " | ") == -1)
1037 goto done;
1039 r = gotweb_link(c, &(struct gotweb_url){
1040 .action = BRIEFS,
1041 .index_page = -1,
1042 .page = -1,
1043 .path = qs->path,
1044 .headref = refname,
1045 }, "commit briefs");
1046 if (r == -1)
1047 goto done;
1049 if (fcgi_printf(c, " | ") == -1)
1050 goto done;
1052 r = gotweb_link(c, &(struct gotweb_url){
1053 .action = COMMITS,
1054 .index_page = -1,
1055 .page = -1,
1056 .path = qs->path,
1057 .headref = refname,
1058 }, "commits");
1059 if (r == -1)
1060 goto done;
1062 r = fcgi_printf(c, "</div>\n" /* .navs */
1063 "</div>\n" /* .navs_wrapper */
1064 "<div class='dotted_line'></div>\n"
1065 "</div>\n"); /* .branches_wrapper */
1066 if (r == -1)
1067 goto done;
1069 free(age);
1070 age = NULL;
1071 free(escaped_refname);
1072 escaped_refname = NULL;
1074 fcgi_printf(c, "</div>\n"); /* #branches_content */
1075 done:
1076 free(age);
1077 free(escaped_refname);
1078 got_ref_list_free(&refs);
1079 return error;
1082 static const struct got_error *
1083 gotweb_render_tree(struct request *c)
1085 const struct got_error *error = NULL;
1086 struct transport *t = c->t;
1087 struct repo_commit *rc = NULL;
1088 char *age = NULL, *msg = NULL;
1089 int r;
1091 error = got_get_repo_commits(c, 1);
1092 if (error)
1093 return error;
1095 rc = TAILQ_FIRST(&t->repo_commits);
1097 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1098 if (error)
1099 goto done;
1101 error = gotweb_escape_html(&msg, rc->commit_msg);
1102 if (error)
1103 goto done;
1105 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1106 "<div id='tree_title'>Tree</div>\n"
1107 "</div>\n" /* #tree_title_wrapper */
1108 "<div id='tree_content'>\n"
1109 "<div id='tree_header_wrapper'>\n"
1110 "<div id='tree_header'>\n"
1111 "<div id='header_tree_title'>Tree:</div>\n"
1112 "<div id='header_tree'>%s</div>\n"
1113 "<div class='header_age_title'>Date:</div>\n"
1114 "<div class='header_age'>%s</div>\n"
1115 "<div id='header_commit_msg_title'>Message:</div>\n"
1116 "<div id='header_commit_msg'>%s</div>\n"
1117 "</div>\n" /* #tree_header */
1118 "</div>\n" /* #tree_header_wrapper */
1119 "<div class='dotted_line'></div>\n"
1120 "<div id='tree'>\n",
1121 rc->tree_id,
1122 age,
1123 msg);
1124 if (r == -1)
1125 goto done;
1127 error = got_output_repo_tree(c);
1128 if (error)
1129 goto done;
1131 fcgi_printf(c, "</div>\n"); /* #tree */
1132 fcgi_printf(c, "</div>\n"); /* #tree_content */
1133 done:
1134 free(age);
1135 free(msg);
1136 return error;
1139 static const struct got_error *
1140 gotweb_render_diff(struct request *c)
1142 const struct got_error *error = NULL;
1143 struct transport *t = c->t;
1144 struct repo_commit *rc = NULL;
1145 char *age = NULL, *author = NULL, *msg = NULL;
1146 int r;
1148 error = got_get_repo_commits(c, 1);
1149 if (error)
1150 return error;
1152 rc = TAILQ_FIRST(&t->repo_commits);
1154 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1155 if (error)
1156 goto done;
1157 error = gotweb_escape_html(&author, rc->author);
1158 if (error)
1159 goto done;
1160 error = gotweb_escape_html(&msg, rc->commit_msg);
1161 if (error)
1162 goto done;
1164 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1165 "<div id='diff_title'>Commit Diff</div>\n"
1166 "</div>\n" /* #diff_title_wrapper */
1167 "<div id='diff_content'>\n"
1168 "<div id='diff_header_wrapper'>\n"
1169 "<div id='diff_header'>\n"
1170 "<div id='header_diff_title'>Diff:</div>\n"
1171 "<div id='header_diff'>%s<br />%s</div>\n"
1172 "<div class='header_commit_title'>Commit:</div>\n"
1173 "<div class='header_commit'>%s</div>\n"
1174 "<div id='header_tree_title'>Tree:</div>\n"
1175 "<div id='header_tree'>%s</div>\n"
1176 "<div class='header_author_title'>Author:</div>\n"
1177 "<div class='header_author'>%s</div>\n"
1178 "<div class='header_age_title'>Date:</div>\n"
1179 "<div class='header_age'>%s</div>\n"
1180 "<div id='header_commit_msg_title'>Message:</div>\n"
1181 "<div id='header_commit_msg'>%s</div>\n"
1182 "</div>\n" /* #diff_header */
1183 "</div>\n" /* #diff_header_wrapper */
1184 "<div class='dotted_line'></div>\n"
1185 "<div id='diff'>\n",
1186 rc->parent_id, rc->commit_id,
1187 rc->commit_id,
1188 rc->tree_id,
1189 author,
1190 age,
1191 msg);
1192 if (r == -1)
1193 goto done;
1195 error = got_output_repo_diff(c);
1196 if (error)
1197 goto done;
1199 fcgi_printf(c, "</div>\n"); /* #diff */
1200 fcgi_printf(c, "</div>\n"); /* #diff_content */
1201 done:
1202 free(age);
1203 free(author);
1204 free(msg);
1205 return error;
1208 static const struct got_error *
1209 gotweb_render_summary(struct request *c)
1211 const struct got_error *error = NULL;
1212 struct transport *t = c->t;
1213 struct server *srv = c->srv;
1214 int r;
1216 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1217 goto done;
1219 if (srv->show_repo_description) {
1220 r = fcgi_printf(c,
1221 "<div id='description_title'>Description:</div>\n"
1222 "<div id='description'>%s</div>\n",
1223 t->repo_dir->description ? t->repo_dir->description : "");
1224 if (r == -1)
1225 goto done;
1228 if (srv->show_repo_owner) {
1229 r = fcgi_printf(c,
1230 "<div id='repo_owner_title'>Owner:</div>\n"
1231 "<div id='repo_owner'>%s</div>\n",
1232 t->repo_dir->owner ? t->repo_dir->owner : "");
1233 if (r == -1)
1234 goto done;
1237 if (srv->show_repo_age) {
1238 r = fcgi_printf(c,
1239 "<div id='last_change_title'>Last Change:</div>\n"
1240 "<div id='last_change'>%s</div>\n",
1241 t->repo_dir->age);
1242 if (r == -1)
1243 goto done;
1246 if (srv->show_repo_cloneurl) {
1247 r = fcgi_printf(c,
1248 "<div id='cloneurl_title'>Clone URL:</div>\n"
1249 "<div id='cloneurl'>%s</div>\n",
1250 t->repo_dir->url ? t->repo_dir->url : "");
1251 if (r == -1)
1252 goto done;
1255 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1256 if (r == -1)
1257 goto done;
1259 if (gotweb_render_briefs(c->tp) == -1)
1260 goto done;
1262 error = gotweb_render_tags(c);
1263 if (error) {
1264 log_warnx("%s: %s", __func__, error->msg);
1265 goto done;
1268 error = gotweb_render_branches(c);
1269 if (error)
1270 log_warnx("%s: %s", __func__, error->msg);
1271 done:
1272 return error;
1275 static const struct got_error *
1276 gotweb_render_tag(struct request *c)
1278 const struct got_error *error = NULL;
1279 struct repo_tag *rt = NULL;
1280 struct transport *t = c->t;
1281 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1283 error = got_get_repo_tags(c, 1);
1284 if (error)
1285 goto done;
1287 if (t->tag_count == 0) {
1288 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1289 "bad commit id");
1290 goto done;
1293 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1295 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1296 if (error)
1297 goto done;
1298 error = gotweb_escape_html(&author, rt->tagger);
1299 if (error)
1300 goto done;
1301 error = gotweb_escape_html(&msg, rt->commit_msg);
1302 if (error)
1303 goto done;
1305 tagname = rt->tag_name;
1306 if (strncmp(tagname, "refs/", 5) == 0)
1307 tagname += 5;
1308 error = gotweb_escape_html(&tagname, tagname);
1309 if (error)
1310 goto done;
1312 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1313 "<div id='tags_title'>Tag</div>\n"
1314 "</div>\n" /* #tags_title_wrapper */
1315 "<div id='tags_content'>\n"
1316 "<div id='tag_header_wrapper'>\n"
1317 "<div id='tag_header'>\n"
1318 "<div class='header_commit_title'>Commit:</div>\n"
1319 "<div class='header_commit'>%s"
1320 " <span class='refs_str'>(%s)</span></div>\n"
1321 "<div class='header_author_title'>Tagger:</div>\n"
1322 "<div class='header_author'>%s</div>\n"
1323 "<div class='header_age_title'>Date:</div>\n"
1324 "<div class='header_age'>%s</div>\n"
1325 "<div id='header_commit_msg_title'>Message:</div>\n"
1326 "<div id='header_commit_msg'>%s</div>\n"
1327 "</div>\n" /* #tag_header */
1328 "<div class='dotted_line'></div>\n"
1329 "<div id='tag_commit'>\n%s</div>"
1330 "</div>" /* #tag_header_wrapper */
1331 "</div>", /* #tags_content */
1332 rt->commit_id,
1333 tagname,
1334 author,
1335 age,
1336 msg,
1337 rt->tag_commit);
1339 done:
1340 free(age);
1341 free(author);
1342 free(msg);
1343 return error;
1346 static const struct got_error *
1347 gotweb_render_tags(struct request *c)
1349 const struct got_error *error = NULL;
1350 struct repo_tag *rt = NULL;
1351 struct server *srv = c->srv;
1352 struct transport *t = c->t;
1353 struct querystring *qs = t->qs;
1354 struct repo_dir *repo_dir = t->repo_dir;
1355 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1356 int r, commit_found = 0;
1358 if (qs->action == BRIEFS) {
1359 qs->action = TAGS;
1360 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1361 } else
1362 error = got_get_repo_tags(c, srv->max_commits_display);
1363 if (error)
1364 goto done;
1366 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1367 "<div id='tags_title'>Tags</div>\n"
1368 "</div>\n" /* #tags_title_wrapper */
1369 "<div id='tags_content'>\n");
1370 if (r == -1)
1371 goto done;
1373 if (t->tag_count == 0) {
1374 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1375 "This repository contains no tags");
1376 if (r == -1)
1377 goto done;
1380 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1381 if (commit_found == 0 && qs->commit != NULL) {
1382 if (strcmp(qs->commit, rt->commit_id) != 0)
1383 continue;
1384 else
1385 commit_found = 1;
1387 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1388 if (error)
1389 goto done;
1391 tagname = rt->tag_name;
1392 if (strncmp(tagname, "refs/tags/", 10) == 0)
1393 tagname += 10;
1394 error = gotweb_escape_html(&tagname, tagname);
1395 if (error)
1396 goto done;
1398 if (rt->tag_commit != NULL) {
1399 newline = strchr(rt->tag_commit, '\n');
1400 if (newline)
1401 *newline = '\0';
1402 error = gotweb_escape_html(&msg, rt->tag_commit);
1403 if (error)
1404 goto done;
1407 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1408 "<div class='tag'>%s</div>\n"
1409 "<div class='tag_log'>", age, tagname) == -1)
1410 goto done;
1412 r = gotweb_link(c, &(struct gotweb_url){
1413 .action = TAG,
1414 .index_page = -1,
1415 .page = -1,
1416 .path = repo_dir->name,
1417 .commit = rt->commit_id,
1418 }, "%s", msg ? msg : "");
1419 if (r == -1)
1420 goto done;
1422 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1423 "<div class='navs_wrapper'>\n"
1424 "<div class='navs'>") == -1)
1425 goto done;
1427 r = gotweb_link(c, &(struct gotweb_url){
1428 .action = TAG,
1429 .index_page = -1,
1430 .page = -1,
1431 .path = repo_dir->name,
1432 .commit = rt->commit_id,
1433 }, "tag");
1434 if (r == -1)
1435 goto done;
1437 if (fcgi_printf(c, " | ") == -1)
1438 goto done;
1440 r = gotweb_link(c, &(struct gotweb_url){
1441 .action = BRIEFS,
1442 .index_page = -1,
1443 .page = -1,
1444 .path = repo_dir->name,
1445 .commit = rt->commit_id,
1446 }, "commit briefs");
1447 if (r == -1)
1448 goto done;
1450 if (fcgi_printf(c, " | ") == -1)
1451 goto done;
1453 r = gotweb_link(c, &(struct gotweb_url){
1454 .action = COMMITS,
1455 .index_page = -1,
1456 .page = -1,
1457 .path = repo_dir->name,
1458 .commit = rt->commit_id,
1459 }, "commits");
1460 if (r == -1)
1461 goto done;
1463 r = fcgi_printf(c,
1464 "</div>\n" /* .navs */
1465 "</div>\n" /* .navs_wrapper */
1466 "<div class='dotted_line'></div>\n");
1467 if (r == -1)
1468 goto done;
1470 free(age);
1471 age = NULL;
1472 free(tagname);
1473 tagname = NULL;
1474 free(msg);
1475 msg = NULL;
1477 if (t->next_id || t->prev_id) {
1478 if (gotweb_render_navs(c->tp) == -1)
1479 goto done;
1481 fcgi_printf(c, "</div>\n"); /* #tags_content */
1482 done:
1483 free(age);
1484 free(tagname);
1485 free(msg);
1486 return error;
1489 const struct got_error *
1490 gotweb_escape_html(char **escaped_html, const char *orig_html)
1492 const struct got_error *error = NULL;
1493 struct escape_pair {
1494 char c;
1495 const char *s;
1496 } esc[] = {
1497 { '>', "&gt;" },
1498 { '<', "&lt;" },
1499 { '&', "&amp;" },
1500 { '"', "&quot;" },
1501 { '\'', "&apos;" },
1502 { '\n', "<br />" },
1504 size_t orig_len, len;
1505 int i, j, x;
1507 orig_len = strlen(orig_html);
1508 len = orig_len;
1509 for (i = 0; i < orig_len; i++) {
1510 for (j = 0; j < nitems(esc); j++) {
1511 if (orig_html[i] != esc[j].c)
1512 continue;
1513 len += strlen(esc[j].s) - 1 /* escaped char */;
1517 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1518 if (*escaped_html == NULL)
1519 return got_error_from_errno("calloc");
1521 x = 0;
1522 for (i = 0; i < orig_len; i++) {
1523 int escaped = 0;
1524 for (j = 0; j < nitems(esc); j++) {
1525 if (orig_html[i] != esc[j].c)
1526 continue;
1528 if (strlcat(*escaped_html, esc[j].s, len + 1)
1529 >= len + 1) {
1530 error = got_error(GOT_ERR_NO_SPACE);
1531 goto done;
1533 x += strlen(esc[j].s);
1534 escaped = 1;
1535 break;
1537 if (!escaped) {
1538 (*escaped_html)[x] = orig_html[i];
1539 x++;
1542 done:
1543 if (error) {
1544 free(*escaped_html);
1545 *escaped_html = NULL;
1546 } else {
1547 (*escaped_html)[x] = '\0';
1550 return error;
1553 static inline int
1554 should_urlencode(int c)
1556 if (c <= ' ' || c >= 127)
1557 return 1;
1559 switch (c) {
1560 /* gen-delim */
1561 case ':':
1562 case '/':
1563 case '?':
1564 case '#':
1565 case '[':
1566 case ']':
1567 case '@':
1568 /* sub-delims */
1569 case '!':
1570 case '$':
1571 case '&':
1572 case '\'':
1573 case '(':
1574 case ')':
1575 case '*':
1576 case '+':
1577 case ',':
1578 case ';':
1579 case '=':
1580 return 1;
1581 default:
1582 return 0;
1586 static char *
1587 gotweb_urlencode(const char *str)
1589 const char *s;
1590 char *escaped;
1591 size_t i, len;
1592 int a, b;
1594 len = 0;
1595 for (s = str; *s; ++s) {
1596 len++;
1597 if (should_urlencode(*s))
1598 len += 2;
1601 escaped = calloc(1, len + 1);
1602 if (escaped == NULL)
1603 return NULL;
1605 i = 0;
1606 for (s = str; *s; ++s) {
1607 if (should_urlencode(*s)) {
1608 a = (*s & 0xF0) >> 4;
1609 b = (*s & 0x0F);
1611 escaped[i++] = '%';
1612 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1613 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1614 } else
1615 escaped[i++] = *s;
1618 return escaped;
1621 const char *
1622 gotweb_action_name(int action)
1624 switch (action) {
1625 case BLAME:
1626 return "blame";
1627 case BLOB:
1628 return "blob";
1629 case BRIEFS:
1630 return "briefs";
1631 case COMMITS:
1632 return "commits";
1633 case DIFF:
1634 return "diff";
1635 case ERR:
1636 return "err";
1637 case INDEX:
1638 return "index";
1639 case SUMMARY:
1640 return "summary";
1641 case TAG:
1642 return "tag";
1643 case TAGS:
1644 return "tags";
1645 case TREE:
1646 return "tree";
1647 case RSS:
1648 return "rss";
1649 default:
1650 return NULL;
1654 int
1655 gotweb_render_url(struct request *c, struct gotweb_url *url)
1657 const char *sep = "?", *action;
1658 char *tmp;
1659 int r;
1661 action = gotweb_action_name(url->action);
1662 if (action != NULL) {
1663 if (fcgi_printf(c, "?action=%s", action) == -1)
1664 return -1;
1665 sep = "&";
1668 if (url->commit) {
1669 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1670 return -1;
1671 sep = "&";
1674 if (url->previd) {
1675 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1676 return -1;
1677 sep = "&";
1680 if (url->prevset) {
1681 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1682 return -1;
1683 sep = "&";
1686 if (url->file) {
1687 tmp = gotweb_urlencode(url->file);
1688 if (tmp == NULL)
1689 return -1;
1690 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1691 free(tmp);
1692 if (r == -1)
1693 return -1;
1694 sep = "&";
1697 if (url->folder) {
1698 tmp = gotweb_urlencode(url->folder);
1699 if (tmp == NULL)
1700 return -1;
1701 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1702 free(tmp);
1703 if (r == -1)
1704 return -1;
1705 sep = "&";
1708 if (url->headref) {
1709 tmp = gotweb_urlencode(url->headref);
1710 if (tmp == NULL)
1711 return -1;
1712 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1713 free(tmp);
1714 if (r == -1)
1715 return -1;
1716 sep = "&";
1719 if (url->index_page != -1) {
1720 if (fcgi_printf(c, "%sindex_page=%d", sep,
1721 url->index_page) == -1)
1722 return -1;
1723 sep = "&";
1726 if (url->path) {
1727 tmp = gotweb_urlencode(url->path);
1728 if (tmp == NULL)
1729 return -1;
1730 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1731 free(tmp);
1732 if (r == -1)
1733 return -1;
1734 sep = "&";
1737 if (url->page != -1) {
1738 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1739 return -1;
1740 sep = "&";
1743 return 0;
1746 int
1747 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1749 struct template *tp = c->tp;
1750 const char *proto = c->https ? "https" : "http";
1752 if (fcgi_puts(tp, proto) == -1 ||
1753 fcgi_puts(tp, "://") == -1 ||
1754 tp_htmlescape(tp, c->server_name) == -1 ||
1755 tp_htmlescape(tp, c->document_uri) == -1)
1756 return -1;
1758 return gotweb_render_url(c, url);
1761 int
1762 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1764 va_list ap;
1765 int r;
1767 if (fcgi_printf(c, "<a href='") == -1)
1768 return -1;
1770 if (gotweb_render_url(c, url) == -1)
1771 return -1;
1773 if (fcgi_printf(c, "'>") == -1)
1774 return -1;
1776 va_start(ap, fmt);
1777 r = fcgi_vprintf(c, fmt, ap);
1778 va_end(ap);
1779 if (r == -1)
1780 return -1;
1782 if (fcgi_printf(c, "</a>"))
1783 return -1;
1784 return 0;
1787 static struct got_repository *
1788 find_cached_repo(struct server *srv, const char *path)
1790 int i;
1792 for (i = 0; i < srv->ncached_repos; i++) {
1793 if (strcmp(srv->cached_repos[i].path, path) == 0)
1794 return srv->cached_repos[i].repo;
1797 return NULL;
1800 static const struct got_error *
1801 cache_repo(struct got_repository **new, struct server *srv,
1802 struct repo_dir *repo_dir, struct socket *sock)
1804 const struct got_error *error = NULL;
1805 struct got_repository *repo;
1806 struct cached_repo *cr;
1807 int evicted = 0;
1809 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1810 cr = &srv->cached_repos[srv->ncached_repos - 1];
1811 error = got_repo_close(cr->repo);
1812 memset(cr, 0, sizeof(*cr));
1813 srv->ncached_repos--;
1814 if (error)
1815 return error;
1816 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1817 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1818 cr = &srv->cached_repos[0];
1819 evicted = 1;
1820 } else {
1821 cr = &srv->cached_repos[srv->ncached_repos];
1824 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1825 if (error) {
1826 if (evicted) {
1827 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1828 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1830 return error;
1833 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1834 >= sizeof(cr->path)) {
1835 if (evicted) {
1836 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1837 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1839 return got_error(GOT_ERR_NO_SPACE);
1842 cr->repo = repo;
1843 srv->ncached_repos++;
1844 *new = repo;
1845 return NULL;
1848 static const struct got_error *
1849 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1851 const struct got_error *error = NULL;
1852 struct socket *sock = c->sock;
1853 struct server *srv = c->srv;
1854 struct transport *t = c->t;
1855 struct got_repository *repo = NULL;
1856 DIR *dt;
1857 char *dir_test;
1859 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1860 GOTWEB_GIT_DIR) == -1)
1861 return got_error_from_errno("asprintf");
1863 dt = opendir(dir_test);
1864 if (dt == NULL) {
1865 free(dir_test);
1866 } else {
1867 repo_dir->path = dir_test;
1868 dir_test = NULL;
1869 goto done;
1872 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1873 repo_dir->name) == -1)
1874 return got_error_from_errno("asprintf");
1876 dt = opendir(dir_test);
1877 if (dt == NULL) {
1878 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1879 goto err;
1880 } else {
1881 repo_dir->path = dir_test;
1882 dir_test = NULL;
1885 done:
1886 if (srv->respect_exportok &&
1887 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1888 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1889 goto err;
1892 repo = find_cached_repo(srv, repo_dir->path);
1893 if (repo == NULL) {
1894 error = cache_repo(&repo, srv, repo_dir, sock);
1895 if (error)
1896 goto err;
1898 t->repo = repo;
1899 error = gotweb_get_repo_description(&repo_dir->description, srv,
1900 repo_dir->path, dirfd(dt));
1901 if (error)
1902 goto err;
1903 error = got_get_repo_owner(&repo_dir->owner, c);
1904 if (error)
1905 goto err;
1906 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1907 if (error)
1908 goto err;
1909 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1910 dirfd(dt));
1911 err:
1912 free(dir_test);
1913 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1914 error = got_error_from_errno("closedir");
1915 return error;
1918 static const struct got_error *
1919 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1921 const struct got_error *error;
1923 *repo_dir = calloc(1, sizeof(**repo_dir));
1924 if (*repo_dir == NULL)
1925 return got_error_from_errno("calloc");
1927 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1928 error = got_error_from_errno("asprintf");
1929 free(*repo_dir);
1930 *repo_dir = NULL;
1931 return error;
1933 (*repo_dir)->owner = NULL;
1934 (*repo_dir)->description = NULL;
1935 (*repo_dir)->url = NULL;
1936 (*repo_dir)->age = NULL;
1937 (*repo_dir)->path = NULL;
1939 return NULL;
1942 static const struct got_error *
1943 gotweb_get_repo_description(char **description, struct server *srv,
1944 const char *dirpath, int dir)
1946 const struct got_error *error = NULL;
1947 struct stat sb;
1948 int fd = -1;
1949 off_t len;
1951 *description = NULL;
1952 if (srv->show_repo_description == 0)
1953 return NULL;
1955 fd = openat(dir, "description", O_RDONLY);
1956 if (fd == -1) {
1957 if (errno != ENOENT && errno != EACCES) {
1958 error = got_error_from_errno_fmt("openat %s/%s",
1959 dirpath, "description");
1961 goto done;
1964 if (fstat(fd, &sb) == -1) {
1965 error = got_error_from_errno_fmt("fstat %s/%s",
1966 dirpath, "description");
1967 goto done;
1970 len = sb.st_size;
1971 if (len > GOTWEBD_MAXDESCRSZ - 1)
1972 len = GOTWEBD_MAXDESCRSZ - 1;
1974 *description = calloc(len + 1, sizeof(**description));
1975 if (*description == NULL) {
1976 error = got_error_from_errno("calloc");
1977 goto done;
1980 if (read(fd, *description, len) == -1)
1981 error = got_error_from_errno("read");
1982 done:
1983 if (fd != -1 && close(fd) == -1 && error == NULL)
1984 error = got_error_from_errno("close");
1985 return error;
1988 static const struct got_error *
1989 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1990 int dir)
1992 const struct got_error *error = NULL;
1993 struct stat sb;
1994 int fd = -1;
1995 off_t len;
1997 *url = NULL;
1998 if (srv->show_repo_cloneurl == 0)
1999 return NULL;
2001 fd = openat(dir, "cloneurl", O_RDONLY);
2002 if (fd == -1) {
2003 if (errno != ENOENT && errno != EACCES) {
2004 error = got_error_from_errno_fmt("openat %s/%s",
2005 dirpath, "cloneurl");
2007 goto done;
2010 if (fstat(fd, &sb) == -1) {
2011 error = got_error_from_errno_fmt("fstat %s/%s",
2012 dirpath, "cloneurl");
2013 goto done;
2016 len = sb.st_size;
2017 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2018 len = GOTWEBD_MAXCLONEURLSZ - 1;
2020 *url = calloc(len + 1, sizeof(**url));
2021 if (*url == NULL) {
2022 error = got_error_from_errno("calloc");
2023 goto done;
2026 if (read(fd, *url, len) == -1)
2027 error = got_error_from_errno("read");
2028 done:
2029 if (fd != -1 && close(fd) == -1 && error == NULL)
2030 error = got_error_from_errno("close");
2031 return error;
2034 const struct got_error *
2035 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2037 struct tm tm;
2038 long long diff_time;
2039 const char *years = "years ago", *months = "months ago";
2040 const char *weeks = "weeks ago", *days = "days ago";
2041 const char *hours = "hours ago", *minutes = "minutes ago";
2042 const char *seconds = "seconds ago", *now = "right now";
2043 char *s;
2044 char datebuf[64];
2045 size_t r;
2047 *repo_age = NULL;
2049 switch (ref_tm) {
2050 case TM_DIFF:
2051 diff_time = time(NULL) - committer_time;
2052 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2053 if (asprintf(repo_age, "%lld %s",
2054 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2055 return got_error_from_errno("asprintf");
2056 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2057 if (asprintf(repo_age, "%lld %s",
2058 (diff_time / 60 / 60 / 24 / (365 / 12)),
2059 months) == -1)
2060 return got_error_from_errno("asprintf");
2061 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2062 if (asprintf(repo_age, "%lld %s",
2063 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2064 return got_error_from_errno("asprintf");
2065 } else if (diff_time > 60 * 60 * 24 * 2) {
2066 if (asprintf(repo_age, "%lld %s",
2067 (diff_time / 60 / 60 / 24), days) == -1)
2068 return got_error_from_errno("asprintf");
2069 } else if (diff_time > 60 * 60 * 2) {
2070 if (asprintf(repo_age, "%lld %s",
2071 (diff_time / 60 / 60), hours) == -1)
2072 return got_error_from_errno("asprintf");
2073 } else if (diff_time > 60 * 2) {
2074 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2075 minutes) == -1)
2076 return got_error_from_errno("asprintf");
2077 } else if (diff_time > 2) {
2078 if (asprintf(repo_age, "%lld %s", diff_time,
2079 seconds) == -1)
2080 return got_error_from_errno("asprintf");
2081 } else {
2082 if (asprintf(repo_age, "%s", now) == -1)
2083 return got_error_from_errno("asprintf");
2085 break;
2086 case TM_LONG:
2087 if (gmtime_r(&committer_time, &tm) == NULL)
2088 return got_error_from_errno("gmtime_r");
2090 s = asctime_r(&tm, datebuf);
2091 if (s == NULL)
2092 return got_error_from_errno("asctime_r");
2094 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2095 return got_error_from_errno("asprintf");
2096 break;
2097 case TM_RFC822:
2098 if (gmtime_r(&committer_time, &tm) == NULL)
2099 return got_error_from_errno("gmtime_r");
2101 r = strftime(datebuf, sizeof(datebuf),
2102 "%a, %d %b %Y %H:%M:%S GMT", &tm);
2103 if (r == 0)
2104 return got_error(GOT_ERR_NO_SPACE);
2106 *repo_age = strdup(datebuf);
2107 if (*repo_age == NULL)
2108 return got_error_from_errno("asprintf");
2109 break;
2111 return NULL;