Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "proc.h"
51 #include "gotwebd.h"
53 static const struct querystring_keys querystring_keys[] = {
54 { "action", ACTION },
55 { "commit", COMMIT },
56 { "file", RFILE },
57 { "folder", FOLDER },
58 { "headref", HEADREF },
59 { "index_page", INDEX_PAGE },
60 { "path", PATH },
61 { "page", PAGE },
62 };
64 static const struct action_keys action_keys[] = {
65 { "blame", BLAME },
66 { "blob", BLOB },
67 { "briefs", BRIEFS },
68 { "commits", COMMITS },
69 { "diff", DIFF },
70 { "error", ERR },
71 { "index", INDEX },
72 { "summary", SUMMARY },
73 { "tag", TAG },
74 { "tags", TAGS },
75 { "tree", TREE },
76 };
78 static const struct got_error *gotweb_init_querystring(struct querystring **);
79 static const struct got_error *gotweb_parse_querystring(struct querystring **,
80 char *);
81 static const struct got_error *gotweb_assign_querystring(struct querystring **,
82 char *, char *);
83 static const struct got_error *gotweb_render_index(struct request *);
84 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
85 const char *);
86 static const struct got_error *gotweb_load_got_path(struct request *c,
87 struct repo_dir *);
88 static const struct got_error *gotweb_get_repo_description(char **,
89 struct server *, const char *, int);
90 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
91 const char *, int);
92 static const struct got_error *gotweb_render_blame(struct request *);
93 static const struct got_error *gotweb_render_commits(struct request *);
94 static const struct got_error *gotweb_render_diff(struct request *);
95 static const struct got_error *gotweb_render_summary(struct request *);
96 static const struct got_error *gotweb_render_tag(struct request *);
97 static const struct got_error *gotweb_render_tags(struct request *);
98 static const struct got_error *gotweb_render_tree(struct request *);
99 static const struct got_error *gotweb_render_branches(struct request *);
101 static void gotweb_free_querystring(struct querystring *);
102 static void gotweb_free_repo_dir(struct repo_dir *);
104 struct server *gotweb_get_server(uint8_t *, uint8_t *);
106 void
107 gotweb_process_request(struct request *c)
109 const struct got_error *error = NULL, *error2 = NULL;
110 struct server *srv = NULL;
111 struct querystring *qs = NULL;
112 struct repo_dir *repo_dir = NULL;
113 uint8_t err[] = "gotwebd experienced an error: ";
114 int r, html = 0;
116 /* init the transport */
117 error = gotweb_init_transport(&c->t);
118 if (error) {
119 log_warnx("%s: %s", __func__, error->msg);
120 return;
122 /* don't process any further if client disconnected */
123 if (c->sock->client_status == CLIENT_DISCONNECT)
124 return;
125 /* get the gotwebd server */
126 srv = gotweb_get_server(c->server_name, c->http_host);
127 if (srv == NULL) {
128 log_warnx("%s: error server is NULL", __func__);
129 goto err;
131 c->srv = srv;
132 /* parse our querystring */
133 error = gotweb_init_querystring(&qs);
134 if (error) {
135 log_warnx("%s: %s", __func__, error->msg);
136 goto err;
138 c->t->qs = qs;
139 error = gotweb_parse_querystring(&qs, c->querystring);
140 if (error) {
141 log_warnx("%s: %s", __func__, error->msg);
142 goto err;
145 /*
146 * certain actions require a commit id in the querystring. this stops
147 * bad actors from exploiting this by manually manipulating the
148 * querystring.
149 */
151 if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
152 qs->action == DIFF)) {
153 error2 = got_error(GOT_ERR_QUERYSTRING);
154 goto render;
157 if (qs->action != INDEX) {
158 error = gotweb_init_repo_dir(&repo_dir, qs->path);
159 if (error)
160 goto done;
161 error = gotweb_load_got_path(c, repo_dir);
162 c->t->repo_dir = repo_dir;
163 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
164 goto err;
167 /* render top of page */
168 if (qs != NULL && qs->action == BLOB) {
169 error = got_get_repo_commits(c, 1);
170 if (error)
171 goto done;
172 error = got_output_file_blob(c);
173 if (error) {
174 log_warnx("%s: %s", __func__, error->msg);
175 goto err;
177 goto done;
180 render:
181 error = gotweb_render_content_type(c, "text/html");
182 if (error) {
183 log_warnx("%s: %s", __func__, error->msg);
184 goto err;
186 html = 1;
188 if (gotweb_render_header(c->tp) == -1)
189 goto err;
191 if (error2) {
192 error = error2;
193 goto err;
196 switch(qs->action) {
197 case BLAME:
198 error = gotweb_render_blame(c);
199 if (error) {
200 log_warnx("%s: %s", __func__, error->msg);
201 goto err;
203 break;
204 case BRIEFS:
205 if (gotweb_render_briefs(c->tp) == -1)
206 goto err;
207 break;
208 case COMMITS:
209 error = gotweb_render_commits(c);
210 if (error) {
211 log_warnx("%s: %s", __func__, error->msg);
212 goto err;
214 break;
215 case DIFF:
216 error = gotweb_render_diff(c);
217 if (error) {
218 log_warnx("%s: %s", __func__, error->msg);
219 goto err;
221 break;
222 case INDEX:
223 error = gotweb_render_index(c);
224 if (error) {
225 log_warnx("%s: %s", __func__, error->msg);
226 goto err;
228 break;
229 case SUMMARY:
230 error = gotweb_render_summary(c);
231 if (error) {
232 log_warnx("%s: %s", __func__, error->msg);
233 goto err;
235 break;
236 case TAG:
237 error = gotweb_render_tag(c);
238 if (error) {
239 log_warnx("%s: %s", __func__, error->msg);
240 goto err;
242 break;
243 case TAGS:
244 error = gotweb_render_tags(c);
245 if (error) {
246 log_warnx("%s: %s", __func__, error->msg);
247 goto err;
249 break;
250 case TREE:
251 error = gotweb_render_tree(c);
252 if (error) {
253 log_warnx("%s: %s", __func__, error->msg);
254 goto err;
256 break;
257 case ERR:
258 default:
259 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
260 "Erorr: Bad Querystring");
261 if (r == -1)
262 goto err;
263 break;
266 goto done;
267 err:
268 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
269 return;
270 if (fcgi_printf(c, "\n%s", err) == -1)
271 return;
272 if (error) {
273 if (fcgi_printf(c, "%s", error->msg) == -1)
274 return;
275 } else {
276 if (fcgi_printf(c, "see daemon logs for details") == -1)
277 return;
279 if (html && fcgi_printf(c, "</div>\n") == -1)
280 return;
281 done:
282 if (html && srv != NULL)
283 gotweb_render_footer(c->tp);
286 struct server *
287 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
289 struct server *srv = NULL;
291 /* check against the server name first */
292 if (strlen(server_name) > 0)
293 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
294 if (strcmp(srv->name, server_name) == 0)
295 goto done;
297 /* check against subdomain second */
298 if (strlen(subdomain) > 0)
299 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
300 if (strcmp(srv->name, subdomain) == 0)
301 goto done;
303 /* if those fail, send first server */
304 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
305 if (srv != NULL)
306 break;
307 done:
308 return srv;
309 };
311 const struct got_error *
312 gotweb_init_transport(struct transport **t)
314 const struct got_error *error = NULL;
316 *t = calloc(1, sizeof(**t));
317 if (*t == NULL)
318 return got_error_from_errno2("%s: calloc", __func__);
320 TAILQ_INIT(&(*t)->repo_commits);
321 TAILQ_INIT(&(*t)->repo_tags);
323 (*t)->repo = NULL;
324 (*t)->repo_dir = NULL;
325 (*t)->qs = NULL;
326 (*t)->next_id = NULL;
327 (*t)->prev_id = NULL;
328 (*t)->next_disp = 0;
329 (*t)->prev_disp = 0;
331 return error;
334 static const struct got_error *
335 gotweb_init_querystring(struct querystring **qs)
337 const struct got_error *error = NULL;
339 *qs = calloc(1, sizeof(**qs));
340 if (*qs == NULL)
341 return got_error_from_errno2("%s: calloc", __func__);
343 (*qs)->headref = strdup("HEAD");
344 if ((*qs)->headref == NULL) {
345 free(*qs);
346 *qs = NULL;
347 return got_error_from_errno2("%s: strdup", __func__);
350 (*qs)->action = INDEX;
351 (*qs)->commit = NULL;
352 (*qs)->file = NULL;
353 (*qs)->folder = NULL;
354 (*qs)->index_page = 0;
355 (*qs)->path = NULL;
357 return error;
360 static const struct got_error *
361 gotweb_parse_querystring(struct querystring **qs, char *qst)
363 const struct got_error *error = NULL;
364 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
365 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
367 if (qst == NULL)
368 return error;
370 tok1 = strdup(qst);
371 if (tok1 == NULL)
372 return got_error_from_errno2("%s: strdup", __func__);
374 tok1_pair = tok1;
375 tok1_end = tok1;
377 while (tok1_pair != NULL) {
378 strsep(&tok1_end, "&");
380 tok2 = strdup(tok1_pair);
381 if (tok2 == NULL) {
382 free(tok1);
383 return got_error_from_errno2("%s: strdup", __func__);
386 tok2_pair = tok2;
387 tok2_end = tok2;
389 while (tok2_pair != NULL) {
390 strsep(&tok2_end, "=");
391 if (tok2_end) {
392 error = gotweb_assign_querystring(qs, tok2_pair,
393 tok2_end);
394 if (error)
395 goto err;
397 tok2_pair = tok2_end;
399 free(tok2);
400 tok1_pair = tok1_end;
402 free(tok1);
403 return error;
404 err:
405 free(tok2);
406 free(tok1);
407 return error;
410 /*
411 * Adapted from usr.sbin/httpd/httpd.c url_decode.
412 */
413 static const struct got_error *
414 gotweb_urldecode(char *url)
416 char *p, *q;
417 char hex[3];
418 unsigned long x;
420 hex[2] = '\0';
421 p = q = url;
423 while (*p != '\0') {
424 switch (*p) {
425 case '%':
426 /* Encoding character is followed by two hex chars */
427 if (!isxdigit((unsigned char)p[1]) ||
428 !isxdigit((unsigned char)p[2]) ||
429 (p[1] == '0' && p[2] == '0'))
430 return got_error(GOT_ERR_BAD_QUERYSTRING);
432 hex[0] = p[1];
433 hex[1] = p[2];
435 /*
436 * We don't have to validate "hex" because it is
437 * guaranteed to include two hex chars followed by nul.
438 */
439 x = strtoul(hex, NULL, 16);
440 *q = (char)x;
441 p += 2;
442 break;
443 default:
444 *q = *p;
445 break;
447 p++;
448 q++;
450 *q = '\0';
452 return NULL;
455 static const struct got_error *
456 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
458 const struct got_error *error = NULL;
459 const char *errstr;
460 int a_cnt, el_cnt;
462 error = gotweb_urldecode(value);
463 if (error)
464 return error;
466 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
467 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
468 continue;
470 switch (querystring_keys[el_cnt].element) {
471 case ACTION:
472 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
473 if (strcmp(value, action_keys[a_cnt].name) != 0)
474 continue;
475 else if (strcmp(value,
476 action_keys[a_cnt].name) == 0){
477 (*qs)->action =
478 action_keys[a_cnt].action;
479 goto qa_found;
482 (*qs)->action = ERR;
483 qa_found:
484 break;
485 case COMMIT:
486 (*qs)->commit = strdup(value);
487 if ((*qs)->commit == NULL) {
488 error = got_error_from_errno2("%s: strdup",
489 __func__);
490 goto done;
492 break;
493 case RFILE:
494 (*qs)->file = strdup(value);
495 if ((*qs)->file == NULL) {
496 error = got_error_from_errno2("%s: strdup",
497 __func__);
498 goto done;
500 break;
501 case FOLDER:
502 (*qs)->folder = strdup(value);
503 if ((*qs)->folder == NULL) {
504 error = got_error_from_errno2("%s: strdup",
505 __func__);
506 goto done;
508 break;
509 case HEADREF:
510 free((*qs)->headref);
511 (*qs)->headref = strdup(value);
512 if ((*qs)->headref == NULL) {
513 error = got_error_from_errno2("%s: strdup",
514 __func__);
515 goto done;
517 break;
518 case INDEX_PAGE:
519 if (strlen(value) == 0)
520 break;
521 (*qs)->index_page = strtonum(value, INT64_MIN,
522 INT64_MAX, &errstr);
523 if (errstr) {
524 error = got_error_from_errno3("%s: strtonum %s",
525 __func__, errstr);
526 goto done;
528 if ((*qs)->index_page < 0)
529 (*qs)->index_page = 0;
530 break;
531 case PATH:
532 (*qs)->path = strdup(value);
533 if ((*qs)->path == NULL) {
534 error = got_error_from_errno2("%s: strdup",
535 __func__);
536 goto done;
538 break;
539 case PAGE:
540 if (strlen(value) == 0)
541 break;
542 (*qs)->page = strtonum(value, INT64_MIN,
543 INT64_MAX, &errstr);
544 if (errstr) {
545 error = got_error_from_errno3("%s: strtonum %s",
546 __func__, errstr);
547 goto done;
549 if ((*qs)->page < 0)
550 (*qs)->page = 0;
551 break;
552 default:
553 break;
556 done:
557 return error;
560 void
561 gotweb_free_repo_tag(struct repo_tag *rt)
563 if (rt != NULL) {
564 free(rt->commit_id);
565 free(rt->tag_name);
566 free(rt->tag_commit);
567 free(rt->commit_msg);
568 free(rt->tagger);
570 free(rt);
573 void
574 gotweb_free_repo_commit(struct repo_commit *rc)
576 if (rc != NULL) {
577 free(rc->path);
578 free(rc->refs_str);
579 free(rc->commit_id);
580 free(rc->parent_id);
581 free(rc->tree_id);
582 free(rc->author);
583 free(rc->committer);
584 free(rc->commit_msg);
586 free(rc);
589 static void
590 gotweb_free_querystring(struct querystring *qs)
592 if (qs != NULL) {
593 free(qs->commit);
594 free(qs->file);
595 free(qs->folder);
596 free(qs->headref);
597 free(qs->path);
599 free(qs);
602 static void
603 gotweb_free_repo_dir(struct repo_dir *repo_dir)
605 if (repo_dir != NULL) {
606 free(repo_dir->name);
607 free(repo_dir->owner);
608 free(repo_dir->description);
609 free(repo_dir->url);
610 free(repo_dir->age);
611 free(repo_dir->path);
613 free(repo_dir);
616 void
617 gotweb_free_transport(struct transport *t)
619 struct repo_commit *rc = NULL, *trc = NULL;
620 struct repo_tag *rt = NULL, *trt = NULL;
622 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
623 TAILQ_REMOVE(&t->repo_commits, rc, entry);
624 gotweb_free_repo_commit(rc);
626 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
627 TAILQ_REMOVE(&t->repo_tags, rt, entry);
628 gotweb_free_repo_tag(rt);
630 gotweb_free_repo_dir(t->repo_dir);
631 gotweb_free_querystring(t->qs);
632 free(t->next_id);
633 free(t->prev_id);
634 free(t);
637 const struct got_error *
638 gotweb_render_content_type(struct request *c, const uint8_t *type)
640 const char *csp = "default-src 'self'; script-src 'none'; "
641 "object-src 'none';";
643 fcgi_printf(c,
644 "Content-Security-Policy: %s\r\n"
645 "Content-Type: %s\r\n\r\n",
646 csp, type);
647 return NULL;
650 const struct got_error *
651 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
652 char *file)
654 fcgi_printf(c, "Content-type: %s\r\n"
655 "Content-disposition: attachment; filename=%s\r\n\r\n",
656 type, file);
657 return NULL;
660 void
661 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
662 struct gotweb_url *next, int *have_next)
664 struct transport *t = c->t;
665 struct querystring *qs = t->qs;
666 struct server *srv = c->srv;
668 *have_prev = *have_next = 0;
670 switch(qs->action) {
671 case INDEX:
672 if (qs->index_page > 0) {
673 *have_prev = 1;
674 *prev = (struct gotweb_url){
675 .action = -1,
676 .index_page = qs->index_page - 1,
677 .page = -1,
678 };
680 if (t->next_disp == srv->max_repos_display &&
681 t->repos_total != (qs->index_page + 1) *
682 srv->max_repos_display) {
683 *have_next = 1;
684 *next = (struct gotweb_url){
685 .action = -1,
686 .index_page = qs->index_page + 1,
687 .page = -1,
688 };
690 break;
691 case BRIEFS:
692 if (t->prev_id && qs->commit != NULL &&
693 strcmp(qs->commit, t->prev_id) != 0) {
694 *have_prev = 1;
695 *prev = (struct gotweb_url){
696 .action = BRIEFS,
697 .index_page = -1,
698 .page = qs->page - 1,
699 .path = qs->path,
700 .commit = t->prev_id,
701 .headref = qs->headref,
702 };
704 if (t->next_id) {
705 *have_next = 1;
706 *next = (struct gotweb_url){
707 .action = BRIEFS,
708 .index_page = -1,
709 .page = qs->page + 1,
710 .path = qs->path,
711 .commit = t->next_id,
712 .headref = qs->headref,
713 };
715 break;
716 case COMMITS:
717 if (t->prev_id && qs->commit != NULL &&
718 strcmp(qs->commit, t->prev_id) != 0) {
719 *have_prev = 1;
720 *prev = (struct gotweb_url){
721 .action = COMMITS,
722 .index_page = -1,
723 .page = qs->page - 1,
724 .path = qs->path,
725 .commit = t->prev_id,
726 .headref = qs->headref,
727 .folder = qs->folder,
728 .file = qs->file,
729 };
731 if (t->next_id) {
732 *have_next = 1;
733 *next = (struct gotweb_url){
734 .action = COMMITS,
735 .index_page = -1,
736 .page = qs->page + 1,
737 .path = qs->path,
738 .commit = t->next_id,
739 .headref = qs->headref,
740 .folder = qs->folder,
741 .file = qs->file,
742 };
744 break;
745 case TAGS:
746 if (t->prev_id && qs->commit != NULL &&
747 strcmp(qs->commit, t->prev_id) != 0) {
748 *have_prev = 1;
749 *prev = (struct gotweb_url){
750 .action = TAGS,
751 .index_page = -1,
752 .page = qs->page - 1,
753 .path = qs->path,
754 .commit = t->prev_id,
755 .headref = qs->headref,
756 };
758 if (t->next_id) {
759 *have_next = 1;
760 *next = (struct gotweb_url){
761 .action = TAGS,
762 .index_page = -1,
763 .page = qs->page + 1,
764 .path = qs->path,
765 .commit = t->next_id,
766 .headref = qs->headref,
767 };
769 break;
773 static const struct got_error *
774 gotweb_render_index(struct request *c)
776 const struct got_error *error = NULL;
777 struct server *srv = c->srv;
778 struct transport *t = c->t;
779 struct querystring *qs = t->qs;
780 struct repo_dir *repo_dir = NULL;
781 DIR *d;
782 struct dirent **sd_dent = NULL;
783 unsigned int d_cnt, d_i, d_disp = 0;
784 unsigned int d_skipped = 0;
785 int type;
787 d = opendir(srv->repos_path);
788 if (d == NULL) {
789 error = got_error_from_errno2("opendir", srv->repos_path);
790 return error;
793 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
794 if (d_cnt == -1) {
795 sd_dent = NULL;
796 error = got_error_from_errno2("scandir", srv->repos_path);
797 goto done;
800 if (gotweb_render_repo_table_hdr(c->tp) == -1)
801 goto done;
803 for (d_i = 0; d_i < d_cnt; d_i++) {
804 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
805 break;
807 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
808 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
809 d_skipped++;
810 continue;
813 error = got_path_dirent_type(&type, srv->repos_path,
814 sd_dent[d_i]);
815 if (error)
816 goto done;
817 if (type != DT_DIR) {
818 d_skipped++;
819 continue;
822 if (qs->index_page > 0 && (qs->index_page *
823 srv->max_repos_display) > t->prev_disp) {
824 t->prev_disp++;
825 continue;
828 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
829 if (error)
830 goto done;
832 error = gotweb_load_got_path(c, repo_dir);
833 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
834 error = NULL;
835 gotweb_free_repo_dir(repo_dir);
836 repo_dir = NULL;
837 d_skipped++;
838 continue;
840 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
841 goto done;
843 d_disp++;
844 t->prev_disp++;
846 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
847 goto done;
849 gotweb_free_repo_dir(repo_dir);
850 repo_dir = NULL;
851 t->next_disp++;
852 if (d_disp == srv->max_repos_display)
853 break;
855 t->repos_total = d_cnt - d_skipped;
857 if (srv->max_repos_display == 0)
858 goto done;
859 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
860 goto done;
861 if (t->repos_total <= srv->max_repos ||
862 t->repos_total <= srv->max_repos_display)
863 goto done;
865 if (gotweb_render_navs(c->tp) == -1)
866 goto done;
867 done:
868 if (sd_dent) {
869 for (d_i = 0; d_i < d_cnt; d_i++)
870 free(sd_dent[d_i]);
871 free(sd_dent);
873 if (d != NULL && closedir(d) == EOF && error == NULL)
874 error = got_error_from_errno("closedir");
875 return error;
878 static const struct got_error *
879 gotweb_render_blame(struct request *c)
881 const struct got_error *error = NULL;
882 struct transport *t = c->t;
883 struct repo_commit *rc = NULL;
884 char *age = NULL, *msg = NULL;
885 int r;
887 error = got_get_repo_commits(c, 1);
888 if (error)
889 return error;
891 rc = TAILQ_FIRST(&t->repo_commits);
893 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
894 if (error)
895 goto done;
896 error = gotweb_escape_html(&msg, rc->commit_msg);
897 if (error)
898 goto done;
900 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
901 "<div id='blame_title'>Blame</div>\n"
902 "</div>\n" /* #blame_title_wrapper */
903 "<div id='blame_content'>\n"
904 "<div id='blame_header_wrapper'>\n"
905 "<div id='blame_header'>\n"
906 "<div class='header_age_title'>Date:</div>\n"
907 "<div class='header_age'>%s</div>\n"
908 "<div id='header_commit_msg_title'>Message:</div>\n"
909 "<div id='header_commit_msg'>%s</div>\n"
910 "</div>\n" /* #blame_header */
911 "</div>\n" /* #blame_header_wrapper */
912 "<div class='dotted_line'></div>\n"
913 "<div id='blame'>\n",
914 age,
915 msg);
916 if (r == -1)
917 goto done;
919 error = got_output_file_blame(c);
920 if (error)
921 goto done;
923 fcgi_printf(c, "</div>\n" /* #blame */
924 "</div>\n"); /* #blame_content */
925 done:
926 free(age);
927 free(msg);
928 return error;
931 static const struct got_error *
932 gotweb_render_commits(struct request *c)
934 const struct got_error *error = NULL;
935 struct repo_commit *rc = NULL;
936 struct server *srv = c->srv;
937 struct transport *t = c->t;
938 struct repo_dir *repo_dir = t->repo_dir;
939 char *age = NULL, *author = NULL, *msg = NULL;
940 int r;
942 r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
943 "<div class='commits_title'>Commits</div>\n"
944 "</div>\n" /* .commits_title_wrapper */
945 "<div class='commits_content'>\n");
946 if (r == -1)
947 goto done;
949 error = got_get_repo_commits(c, srv->max_commits_display);
950 if (error)
951 goto done;
953 TAILQ_FOREACH(rc, &t->repo_commits, entry) {
954 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
955 if (error)
956 goto done;
957 error = gotweb_escape_html(&author, rc->author);
958 if (error)
959 goto done;
960 error = gotweb_escape_html(&msg, rc->commit_msg);
961 if (error)
962 goto done;
964 r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
965 "<div class='commits_header'>\n"
966 "<div class='header_commit_title'>Commit:</div>\n"
967 "<div class='header_commit'>%s</div>\n"
968 "<div class='header_author_title'>Author:</div>\n"
969 "<div class='header_author'>%s</div>\n"
970 "<div class='header_age_title'>Date:</div>\n"
971 "<div class='header_age'>%s</div>\n"
972 "</div>\n" /* .commits_header */
973 "</div>\n" /* .commits_header_wrapper */
974 "<div class='dotted_line'></div>\n"
975 "<div class='commit'>\n%s</div>\n",
976 rc->commit_id,
977 author,
978 age,
979 msg);
980 if (r == -1)
981 goto done;
983 if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
984 "<div class='navs'>") == -1)
985 goto done;
987 r = gotweb_link(c, &(struct gotweb_url){
988 .action = DIFF,
989 .index_page = -1,
990 .page = -1,
991 .path = repo_dir->name,
992 .commit = rc->commit_id,
993 }, "diff");
994 if (r == -1)
995 goto done;
997 if (fcgi_printf(c, " | ") == -1)
998 goto done;
1000 r = gotweb_link(c, &(struct gotweb_url){
1001 .action = TREE,
1002 .index_page = -1,
1003 .page = -1,
1004 .path = repo_dir->name,
1005 .commit = rc->commit_id,
1006 }, "tree");
1007 if (r == -1)
1008 goto done;
1010 if (fcgi_printf(c, "</div>\n" /* .navs */
1011 "</div>\n" /* .navs_wrapper */
1012 "<div class='dotted_line'></div>\n") == -1)
1013 goto done;
1015 free(age);
1016 age = NULL;
1017 free(author);
1018 author = NULL;
1019 free(msg);
1020 msg = NULL;
1023 if (t->next_id || t->prev_id) {
1024 if (gotweb_render_navs(c->tp) == -1)
1025 goto done;
1027 fcgi_printf(c, "</div>\n"); /* .commits_content */
1028 done:
1029 free(age);
1030 free(author);
1031 free(msg);
1032 return error;
1035 static const struct got_error *
1036 gotweb_render_branches(struct request *c)
1038 const struct got_error *error = NULL;
1039 struct got_reflist_head refs;
1040 struct got_reflist_entry *re;
1041 struct transport *t = c->t;
1042 struct querystring *qs = t->qs;
1043 struct got_repository *repo = t->repo;
1044 char *escaped_refname = NULL;
1045 char *age = NULL;
1046 int r;
1048 TAILQ_INIT(&refs);
1050 error = got_ref_list(&refs, repo, "refs/heads",
1051 got_ref_cmp_by_name, NULL);
1052 if (error)
1053 goto done;
1055 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1056 "<div id='branches_title'>Branches</div>\n"
1057 "</div>\n" /* #branches_title_wrapper */
1058 "<div id='branches_content'>\n");
1059 if (r == -1)
1060 goto done;
1062 TAILQ_FOREACH(re, &refs, entry) {
1063 const char *refname = NULL;
1065 if (got_ref_is_symbolic(re->ref))
1066 continue;
1068 refname = got_ref_get_name(re->ref);
1069 if (refname == NULL) {
1070 error = got_error_from_errno("strdup");
1071 goto done;
1073 if (strncmp(refname, "refs/heads/", 11) != 0)
1074 continue;
1076 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1077 if (error)
1078 goto done;
1080 if (strncmp(refname, "refs/heads/", 11) == 0)
1081 refname += 11;
1082 error = gotweb_escape_html(&escaped_refname, refname);
1083 if (error)
1084 goto done;
1086 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1087 "<div class='branches_age'>%s</div>\n"
1088 "<div class='branches_space'>&nbsp;</div>\n"
1089 "<div class='branch'>", age);
1090 if (r == -1)
1091 goto done;
1093 r = gotweb_link(c, &(struct gotweb_url){
1094 .action = SUMMARY,
1095 .index_page = -1,
1096 .page = -1,
1097 .path = qs->path,
1098 .headref = refname,
1099 }, "%s", escaped_refname);
1100 if (r == -1)
1101 goto done;
1103 if (fcgi_printf(c, "</div>\n" /* .branch */
1104 "<div class='navs_wrapper'>\n"
1105 "<div class='navs'>") == -1)
1106 goto done;
1108 r = gotweb_link(c, &(struct gotweb_url){
1109 .action = SUMMARY,
1110 .index_page = -1,
1111 .page = -1,
1112 .path = qs->path,
1113 .headref = refname,
1114 }, "summary");
1115 if (r == -1)
1116 goto done;
1118 if (fcgi_printf(c, " | ") == -1)
1119 goto done;
1121 r = gotweb_link(c, &(struct gotweb_url){
1122 .action = BRIEFS,
1123 .index_page = -1,
1124 .page = -1,
1125 .path = qs->path,
1126 .headref = refname,
1127 }, "commit briefs");
1128 if (r == -1)
1129 goto done;
1131 if (fcgi_printf(c, " | ") == -1)
1132 goto done;
1134 r = gotweb_link(c, &(struct gotweb_url){
1135 .action = COMMITS,
1136 .index_page = -1,
1137 .page = -1,
1138 .path = qs->path,
1139 .headref = refname,
1140 }, "commits");
1141 if (r == -1)
1142 goto done;
1144 r = fcgi_printf(c, "</div>\n" /* .navs */
1145 "</div>\n" /* .navs_wrapper */
1146 "<div class='dotted_line'></div>\n"
1147 "</div>\n"); /* .branches_wrapper */
1148 if (r == -1)
1149 goto done;
1151 free(age);
1152 age = NULL;
1153 free(escaped_refname);
1154 escaped_refname = NULL;
1156 fcgi_printf(c, "</div>\n"); /* #branches_content */
1157 done:
1158 free(age);
1159 free(escaped_refname);
1160 got_ref_list_free(&refs);
1161 return error;
1164 static const struct got_error *
1165 gotweb_render_tree(struct request *c)
1167 const struct got_error *error = NULL;
1168 struct transport *t = c->t;
1169 struct repo_commit *rc = NULL;
1170 char *age = NULL, *msg = NULL;
1171 int r;
1173 error = got_get_repo_commits(c, 1);
1174 if (error)
1175 return error;
1177 rc = TAILQ_FIRST(&t->repo_commits);
1179 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1180 if (error)
1181 goto done;
1183 error = gotweb_escape_html(&msg, rc->commit_msg);
1184 if (error)
1185 goto done;
1187 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1188 "<div id='tree_title'>Tree</div>\n"
1189 "</div>\n" /* #tree_title_wrapper */
1190 "<div id='tree_content'>\n"
1191 "<div id='tree_header_wrapper'>\n"
1192 "<div id='tree_header'>\n"
1193 "<div id='header_tree_title'>Tree:</div>\n"
1194 "<div id='header_tree'>%s</div>\n"
1195 "<div class='header_age_title'>Date:</div>\n"
1196 "<div class='header_age'>%s</div>\n"
1197 "<div id='header_commit_msg_title'>Message:</div>\n"
1198 "<div id='header_commit_msg'>%s</div>\n"
1199 "</div>\n" /* #tree_header */
1200 "</div>\n" /* #tree_header_wrapper */
1201 "<div class='dotted_line'></div>\n"
1202 "<div id='tree'>\n",
1203 rc->tree_id,
1204 age,
1205 msg);
1206 if (r == -1)
1207 goto done;
1209 error = got_output_repo_tree(c);
1210 if (error)
1211 goto done;
1213 fcgi_printf(c, "</div>\n"); /* #tree */
1214 fcgi_printf(c, "</div>\n"); /* #tree_content */
1215 done:
1216 free(age);
1217 free(msg);
1218 return error;
1221 static const struct got_error *
1222 gotweb_render_diff(struct request *c)
1224 const struct got_error *error = NULL;
1225 struct transport *t = c->t;
1226 struct repo_commit *rc = NULL;
1227 char *age = NULL, *author = NULL, *msg = NULL;
1228 int r;
1230 error = got_get_repo_commits(c, 1);
1231 if (error)
1232 return error;
1234 rc = TAILQ_FIRST(&t->repo_commits);
1236 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1237 if (error)
1238 goto done;
1239 error = gotweb_escape_html(&author, rc->author);
1240 if (error)
1241 goto done;
1242 error = gotweb_escape_html(&msg, rc->commit_msg);
1243 if (error)
1244 goto done;
1246 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1247 "<div id='diff_title'>Commit Diff</div>\n"
1248 "</div>\n" /* #diff_title_wrapper */
1249 "<div id='diff_content'>\n"
1250 "<div id='diff_header_wrapper'>\n"
1251 "<div id='diff_header'>\n"
1252 "<div id='header_diff_title'>Diff:</div>\n"
1253 "<div id='header_diff'>%s<br />%s</div>\n"
1254 "<div class='header_commit_title'>Commit:</div>\n"
1255 "<div class='header_commit'>%s</div>\n"
1256 "<div id='header_tree_title'>Tree:</div>\n"
1257 "<div id='header_tree'>%s</div>\n"
1258 "<div class='header_author_title'>Author:</div>\n"
1259 "<div class='header_author'>%s</div>\n"
1260 "<div class='header_age_title'>Date:</div>\n"
1261 "<div class='header_age'>%s</div>\n"
1262 "<div id='header_commit_msg_title'>Message:</div>\n"
1263 "<div id='header_commit_msg'>%s</div>\n"
1264 "</div>\n" /* #diff_header */
1265 "</div>\n" /* #diff_header_wrapper */
1266 "<div class='dotted_line'></div>\n"
1267 "<div id='diff'>\n",
1268 rc->parent_id, rc->commit_id,
1269 rc->commit_id,
1270 rc->tree_id,
1271 author,
1272 age,
1273 msg);
1274 if (r == -1)
1275 goto done;
1277 error = got_output_repo_diff(c);
1278 if (error)
1279 goto done;
1281 fcgi_printf(c, "</div>\n"); /* #diff */
1282 fcgi_printf(c, "</div>\n"); /* #diff_content */
1283 done:
1284 free(age);
1285 free(author);
1286 free(msg);
1287 return error;
1290 static const struct got_error *
1291 gotweb_render_summary(struct request *c)
1293 const struct got_error *error = NULL;
1294 struct transport *t = c->t;
1295 struct server *srv = c->srv;
1296 int r;
1298 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1299 goto done;
1301 if (srv->show_repo_description) {
1302 r = fcgi_printf(c,
1303 "<div id='description_title'>Description:</div>\n"
1304 "<div id='description'>%s</div>\n",
1305 t->repo_dir->description ? t->repo_dir->description : "");
1306 if (r == -1)
1307 goto done;
1310 if (srv->show_repo_owner) {
1311 r = fcgi_printf(c,
1312 "<div id='repo_owner_title'>Owner:</div>\n"
1313 "<div id='repo_owner'>%s</div>\n",
1314 t->repo_dir->owner ? t->repo_dir->owner : "");
1315 if (r == -1)
1316 goto done;
1319 if (srv->show_repo_age) {
1320 r = fcgi_printf(c,
1321 "<div id='last_change_title'>Last Change:</div>\n"
1322 "<div id='last_change'>%s</div>\n",
1323 t->repo_dir->age);
1324 if (r == -1)
1325 goto done;
1328 if (srv->show_repo_cloneurl) {
1329 r = fcgi_printf(c,
1330 "<div id='cloneurl_title'>Clone URL:</div>\n"
1331 "<div id='cloneurl'>%s</div>\n",
1332 t->repo_dir->url ? t->repo_dir->url : "");
1333 if (r == -1)
1334 goto done;
1337 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1338 if (r == -1)
1339 goto done;
1341 if (gotweb_render_briefs(c->tp) == -1)
1342 goto done;
1344 error = gotweb_render_tags(c);
1345 if (error) {
1346 log_warnx("%s: %s", __func__, error->msg);
1347 goto done;
1350 error = gotweb_render_branches(c);
1351 if (error)
1352 log_warnx("%s: %s", __func__, error->msg);
1353 done:
1354 return error;
1357 static const struct got_error *
1358 gotweb_render_tag(struct request *c)
1360 const struct got_error *error = NULL;
1361 struct repo_tag *rt = NULL;
1362 struct transport *t = c->t;
1363 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1365 error = got_get_repo_tags(c, 1);
1366 if (error)
1367 goto done;
1369 if (t->tag_count == 0) {
1370 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1371 "bad commit id");
1372 goto done;
1375 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1377 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1378 if (error)
1379 goto done;
1380 error = gotweb_escape_html(&author, rt->tagger);
1381 if (error)
1382 goto done;
1383 error = gotweb_escape_html(&msg, rt->commit_msg);
1384 if (error)
1385 goto done;
1387 tagname = rt->tag_name;
1388 if (strncmp(tagname, "refs/", 5) == 0)
1389 tagname += 5;
1390 error = gotweb_escape_html(&tagname, tagname);
1391 if (error)
1392 goto done;
1394 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1395 "<div id='tags_title'>Tag</div>\n"
1396 "</div>\n" /* #tags_title_wrapper */
1397 "<div id='tags_content'>\n"
1398 "<div id='tag_header_wrapper'>\n"
1399 "<div id='tag_header'>\n"
1400 "<div class='header_commit_title'>Commit:</div>\n"
1401 "<div class='header_commit'>%s"
1402 " <span class='refs_str'>(%s)</span></div>\n"
1403 "<div class='header_author_title'>Tagger:</div>\n"
1404 "<div class='header_author'>%s</div>\n"
1405 "<div class='header_age_title'>Date:</div>\n"
1406 "<div class='header_age'>%s</div>\n"
1407 "<div id='header_commit_msg_title'>Message:</div>\n"
1408 "<div id='header_commit_msg'>%s</div>\n"
1409 "</div>\n" /* #tag_header */
1410 "<div class='dotted_line'></div>\n"
1411 "<div id='tag_commit'>\n%s</div>"
1412 "</div>" /* #tag_header_wrapper */
1413 "</div>", /* #tags_content */
1414 rt->commit_id,
1415 tagname,
1416 author,
1417 age,
1418 msg,
1419 rt->tag_commit);
1421 done:
1422 free(age);
1423 free(author);
1424 free(msg);
1425 return error;
1428 static const struct got_error *
1429 gotweb_render_tags(struct request *c)
1431 const struct got_error *error = NULL;
1432 struct repo_tag *rt = NULL;
1433 struct server *srv = c->srv;
1434 struct transport *t = c->t;
1435 struct querystring *qs = t->qs;
1436 struct repo_dir *repo_dir = t->repo_dir;
1437 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1438 int r, commit_found = 0;
1440 if (qs->action == BRIEFS) {
1441 qs->action = TAGS;
1442 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1443 } else
1444 error = got_get_repo_tags(c, srv->max_commits_display);
1445 if (error)
1446 goto done;
1448 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1449 "<div id='tags_title'>Tags</div>\n"
1450 "</div>\n" /* #tags_title_wrapper */
1451 "<div id='tags_content'>\n");
1452 if (r == -1)
1453 goto done;
1455 if (t->tag_count == 0) {
1456 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1457 "This repository contains no tags");
1458 if (r == -1)
1459 goto done;
1462 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1463 if (commit_found == 0 && qs->commit != NULL) {
1464 if (strcmp(qs->commit, rt->commit_id) != 0)
1465 continue;
1466 else
1467 commit_found = 1;
1469 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1470 if (error)
1471 goto done;
1473 tagname = rt->tag_name;
1474 if (strncmp(tagname, "refs/tags/", 10) == 0)
1475 tagname += 10;
1476 error = gotweb_escape_html(&tagname, tagname);
1477 if (error)
1478 goto done;
1480 if (rt->tag_commit != NULL) {
1481 newline = strchr(rt->tag_commit, '\n');
1482 if (newline)
1483 *newline = '\0';
1484 error = gotweb_escape_html(&msg, rt->tag_commit);
1485 if (error)
1486 goto done;
1489 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1490 "<div class='tag'>%s</div>\n"
1491 "<div class='tag_log'>", age, tagname) == -1)
1492 goto done;
1494 r = gotweb_link(c, &(struct gotweb_url){
1495 .action = TAG,
1496 .index_page = -1,
1497 .page = -1,
1498 .path = repo_dir->name,
1499 .commit = rt->commit_id,
1500 }, "%s", msg ? msg : "");
1501 if (r == -1)
1502 goto done;
1504 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1505 "<div class='navs_wrapper'>\n"
1506 "<div class='navs'>") == -1)
1507 goto done;
1509 r = gotweb_link(c, &(struct gotweb_url){
1510 .action = TAG,
1511 .index_page = -1,
1512 .page = -1,
1513 .path = repo_dir->name,
1514 .commit = rt->commit_id,
1515 }, "tag");
1516 if (r == -1)
1517 goto done;
1519 if (fcgi_printf(c, " | ") == -1)
1520 goto done;
1522 r = gotweb_link(c, &(struct gotweb_url){
1523 .action = BRIEFS,
1524 .index_page = -1,
1525 .page = -1,
1526 .path = repo_dir->name,
1527 .commit = rt->commit_id,
1528 }, "commit briefs");
1529 if (r == -1)
1530 goto done;
1532 if (fcgi_printf(c, " | ") == -1)
1533 goto done;
1535 r = gotweb_link(c, &(struct gotweb_url){
1536 .action = COMMITS,
1537 .index_page = -1,
1538 .page = -1,
1539 .path = repo_dir->name,
1540 .commit = rt->commit_id,
1541 }, "commits");
1542 if (r == -1)
1543 goto done;
1545 r = fcgi_printf(c,
1546 "</div>\n" /* .navs */
1547 "</div>\n" /* .navs_wrapper */
1548 "<div class='dotted_line'></div>\n");
1549 if (r == -1)
1550 goto done;
1552 free(age);
1553 age = NULL;
1554 free(tagname);
1555 tagname = NULL;
1556 free(msg);
1557 msg = NULL;
1559 if (t->next_id || t->prev_id) {
1560 if (gotweb_render_navs(c->tp) == -1)
1561 goto done;
1563 fcgi_printf(c, "</div>\n"); /* #tags_content */
1564 done:
1565 free(age);
1566 free(tagname);
1567 free(msg);
1568 return error;
1571 const struct got_error *
1572 gotweb_escape_html(char **escaped_html, const char *orig_html)
1574 const struct got_error *error = NULL;
1575 struct escape_pair {
1576 char c;
1577 const char *s;
1578 } esc[] = {
1579 { '>', "&gt;" },
1580 { '<', "&lt;" },
1581 { '&', "&amp;" },
1582 { '"', "&quot;" },
1583 { '\'', "&apos;" },
1584 { '\n', "<br />" },
1586 size_t orig_len, len;
1587 int i, j, x;
1589 orig_len = strlen(orig_html);
1590 len = orig_len;
1591 for (i = 0; i < orig_len; i++) {
1592 for (j = 0; j < nitems(esc); j++) {
1593 if (orig_html[i] != esc[j].c)
1594 continue;
1595 len += strlen(esc[j].s) - 1 /* escaped char */;
1599 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1600 if (*escaped_html == NULL)
1601 return got_error_from_errno("calloc");
1603 x = 0;
1604 for (i = 0; i < orig_len; i++) {
1605 int escaped = 0;
1606 for (j = 0; j < nitems(esc); j++) {
1607 if (orig_html[i] != esc[j].c)
1608 continue;
1610 if (strlcat(*escaped_html, esc[j].s, len + 1)
1611 >= len + 1) {
1612 error = got_error(GOT_ERR_NO_SPACE);
1613 goto done;
1615 x += strlen(esc[j].s);
1616 escaped = 1;
1617 break;
1619 if (!escaped) {
1620 (*escaped_html)[x] = orig_html[i];
1621 x++;
1624 done:
1625 if (error) {
1626 free(*escaped_html);
1627 *escaped_html = NULL;
1628 } else {
1629 (*escaped_html)[x] = '\0';
1632 return error;
1635 static inline int
1636 should_urlencode(int c)
1638 if (c <= ' ' || c >= 127)
1639 return 1;
1641 switch (c) {
1642 /* gen-delim */
1643 case ':':
1644 case '/':
1645 case '?':
1646 case '#':
1647 case '[':
1648 case ']':
1649 case '@':
1650 /* sub-delims */
1651 case '!':
1652 case '$':
1653 case '&':
1654 case '\'':
1655 case '(':
1656 case ')':
1657 case '*':
1658 case '+':
1659 case ',':
1660 case ';':
1661 case '=':
1662 return 1;
1663 default:
1664 return 0;
1668 static char *
1669 gotweb_urlencode(const char *str)
1671 const char *s;
1672 char *escaped;
1673 size_t i, len;
1674 int a, b;
1676 len = 0;
1677 for (s = str; *s; ++s) {
1678 len++;
1679 if (should_urlencode(*s))
1680 len += 2;
1683 escaped = calloc(1, len + 1);
1684 if (escaped == NULL)
1685 return NULL;
1687 i = 0;
1688 for (s = str; *s; ++s) {
1689 if (should_urlencode(*s)) {
1690 a = (*s & 0xF0) >> 4;
1691 b = (*s & 0x0F);
1693 escaped[i++] = '%';
1694 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1695 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1696 } else
1697 escaped[i++] = *s;
1700 return escaped;
1703 const char *
1704 gotweb_action_name(int action)
1706 switch (action) {
1707 case BLAME:
1708 return "blame";
1709 case BLOB:
1710 return "blob";
1711 case BRIEFS:
1712 return "briefs";
1713 case COMMITS:
1714 return "commits";
1715 case DIFF:
1716 return "diff";
1717 case ERR:
1718 return "err";
1719 case INDEX:
1720 return "index";
1721 case SUMMARY:
1722 return "summary";
1723 case TAG:
1724 return "tag";
1725 case TAGS:
1726 return "tags";
1727 case TREE:
1728 return "tree";
1729 default:
1730 return NULL;
1734 int
1735 gotweb_render_url(struct request *c, struct gotweb_url *url)
1737 const char *sep = "?", *action;
1738 char *tmp;
1739 int r;
1741 action = gotweb_action_name(url->action);
1742 if (action != NULL) {
1743 if (fcgi_printf(c, "?action=%s", action) == -1)
1744 return -1;
1745 sep = "&";
1748 if (url->commit) {
1749 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1750 return -1;
1751 sep = "&";
1754 if (url->previd) {
1755 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1756 return -1;
1757 sep = "&";
1760 if (url->prevset) {
1761 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1762 return -1;
1763 sep = "&";
1766 if (url->file) {
1767 tmp = gotweb_urlencode(url->file);
1768 if (tmp == NULL)
1769 return -1;
1770 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1771 free(tmp);
1772 if (r == -1)
1773 return -1;
1774 sep = "&";
1777 if (url->folder) {
1778 tmp = gotweb_urlencode(url->folder);
1779 if (tmp == NULL)
1780 return -1;
1781 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1782 free(tmp);
1783 if (r == -1)
1784 return -1;
1785 sep = "&";
1788 if (url->headref) {
1789 tmp = gotweb_urlencode(url->headref);
1790 if (tmp == NULL)
1791 return -1;
1792 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1793 free(tmp);
1794 if (r == -1)
1795 return -1;
1796 sep = "&";
1799 if (url->index_page != -1) {
1800 if (fcgi_printf(c, "%sindex_page=%d", sep,
1801 url->index_page) == -1)
1802 return -1;
1803 sep = "&";
1806 if (url->path) {
1807 tmp = gotweb_urlencode(url->path);
1808 if (tmp == NULL)
1809 return -1;
1810 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1811 free(tmp);
1812 if (r == -1)
1813 return -1;
1814 sep = "&";
1817 if (url->page != -1) {
1818 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1819 return -1;
1820 sep = "&";
1823 return 0;
1826 int
1827 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1829 va_list ap;
1830 int r;
1832 if (fcgi_printf(c, "<a href='") == -1)
1833 return -1;
1835 if (gotweb_render_url(c, url) == -1)
1836 return -1;
1838 if (fcgi_printf(c, "'>") == -1)
1839 return -1;
1841 va_start(ap, fmt);
1842 r = fcgi_vprintf(c, fmt, ap);
1843 va_end(ap);
1844 if (r == -1)
1845 return -1;
1847 if (fcgi_printf(c, "</a>"))
1848 return -1;
1849 return 0;
1852 static struct got_repository *
1853 find_cached_repo(struct server *srv, const char *path)
1855 int i;
1857 for (i = 0; i < srv->ncached_repos; i++) {
1858 if (strcmp(srv->cached_repos[i].path, path) == 0)
1859 return srv->cached_repos[i].repo;
1862 return NULL;
1865 static const struct got_error *
1866 cache_repo(struct got_repository **new, struct server *srv,
1867 struct repo_dir *repo_dir, struct socket *sock)
1869 const struct got_error *error = NULL;
1870 struct got_repository *repo;
1871 struct cached_repo *cr;
1872 int evicted = 0;
1874 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1875 cr = &srv->cached_repos[srv->ncached_repos - 1];
1876 error = got_repo_close(cr->repo);
1877 memset(cr, 0, sizeof(*cr));
1878 srv->ncached_repos--;
1879 if (error)
1880 return error;
1881 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1882 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1883 cr = &srv->cached_repos[0];
1884 evicted = 1;
1885 } else {
1886 cr = &srv->cached_repos[srv->ncached_repos];
1889 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1890 if (error) {
1891 if (evicted) {
1892 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1893 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1895 return error;
1898 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1899 >= sizeof(cr->path)) {
1900 if (evicted) {
1901 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1902 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1904 return got_error(GOT_ERR_NO_SPACE);
1907 cr->repo = repo;
1908 srv->ncached_repos++;
1909 *new = repo;
1910 return NULL;
1913 static const struct got_error *
1914 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1916 const struct got_error *error = NULL;
1917 struct socket *sock = c->sock;
1918 struct server *srv = c->srv;
1919 struct transport *t = c->t;
1920 struct got_repository *repo = NULL;
1921 DIR *dt;
1922 char *dir_test;
1924 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1925 GOTWEB_GIT_DIR) == -1)
1926 return got_error_from_errno("asprintf");
1928 dt = opendir(dir_test);
1929 if (dt == NULL) {
1930 free(dir_test);
1931 } else {
1932 repo_dir->path = dir_test;
1933 dir_test = NULL;
1934 goto done;
1937 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1938 repo_dir->name) == -1)
1939 return got_error_from_errno("asprintf");
1941 dt = opendir(dir_test);
1942 if (dt == NULL) {
1943 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1944 goto err;
1945 } else {
1946 repo_dir->path = dir_test;
1947 dir_test = NULL;
1950 done:
1951 if (srv->respect_exportok &&
1952 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1953 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1954 goto err;
1957 repo = find_cached_repo(srv, repo_dir->path);
1958 if (repo == NULL) {
1959 error = cache_repo(&repo, srv, repo_dir, sock);
1960 if (error)
1961 goto err;
1963 t->repo = repo;
1964 error = gotweb_get_repo_description(&repo_dir->description, srv,
1965 repo_dir->path, dirfd(dt));
1966 if (error)
1967 goto err;
1968 error = got_get_repo_owner(&repo_dir->owner, c);
1969 if (error)
1970 goto err;
1971 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1972 if (error)
1973 goto err;
1974 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1975 dirfd(dt));
1976 err:
1977 free(dir_test);
1978 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1979 error = got_error_from_errno("closedir");
1980 return error;
1983 static const struct got_error *
1984 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1986 const struct got_error *error;
1988 *repo_dir = calloc(1, sizeof(**repo_dir));
1989 if (*repo_dir == NULL)
1990 return got_error_from_errno("calloc");
1992 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1993 error = got_error_from_errno("asprintf");
1994 free(*repo_dir);
1995 *repo_dir = NULL;
1996 return error;
1998 (*repo_dir)->owner = NULL;
1999 (*repo_dir)->description = NULL;
2000 (*repo_dir)->url = NULL;
2001 (*repo_dir)->age = NULL;
2002 (*repo_dir)->path = NULL;
2004 return NULL;
2007 static const struct got_error *
2008 gotweb_get_repo_description(char **description, struct server *srv,
2009 const char *dirpath, int dir)
2011 const struct got_error *error = NULL;
2012 struct stat sb;
2013 int fd = -1;
2014 off_t len;
2016 *description = NULL;
2017 if (srv->show_repo_description == 0)
2018 return NULL;
2020 fd = openat(dir, "description", O_RDONLY);
2021 if (fd == -1) {
2022 if (errno != ENOENT && errno != EACCES) {
2023 error = got_error_from_errno_fmt("openat %s/%s",
2024 dirpath, "description");
2026 goto done;
2029 if (fstat(fd, &sb) == -1) {
2030 error = got_error_from_errno_fmt("fstat %s/%s",
2031 dirpath, "description");
2032 goto done;
2035 len = sb.st_size;
2036 if (len > GOTWEBD_MAXDESCRSZ - 1)
2037 len = GOTWEBD_MAXDESCRSZ - 1;
2039 *description = calloc(len + 1, sizeof(**description));
2040 if (*description == NULL) {
2041 error = got_error_from_errno("calloc");
2042 goto done;
2045 if (read(fd, *description, len) == -1)
2046 error = got_error_from_errno("read");
2047 done:
2048 if (fd != -1 && close(fd) == -1 && error == NULL)
2049 error = got_error_from_errno("close");
2050 return error;
2053 static const struct got_error *
2054 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2055 int dir)
2057 const struct got_error *error = NULL;
2058 struct stat sb;
2059 int fd = -1;
2060 off_t len;
2062 *url = NULL;
2063 if (srv->show_repo_cloneurl == 0)
2064 return NULL;
2066 fd = openat(dir, "cloneurl", O_RDONLY);
2067 if (fd == -1) {
2068 if (errno != ENOENT && errno != EACCES) {
2069 error = got_error_from_errno_fmt("openat %s/%s",
2070 dirpath, "cloneurl");
2072 goto done;
2075 if (fstat(fd, &sb) == -1) {
2076 error = got_error_from_errno_fmt("fstat %s/%s",
2077 dirpath, "cloneurl");
2078 goto done;
2081 len = sb.st_size;
2082 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2083 len = GOTWEBD_MAXCLONEURLSZ - 1;
2085 *url = calloc(len + 1, sizeof(**url));
2086 if (*url == NULL) {
2087 error = got_error_from_errno("calloc");
2088 goto done;
2091 if (read(fd, *url, len) == -1)
2092 error = got_error_from_errno("read");
2093 done:
2094 if (fd != -1 && close(fd) == -1 && error == NULL)
2095 error = got_error_from_errno("close");
2096 return error;
2099 const struct got_error *
2100 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2102 struct tm tm;
2103 long long diff_time;
2104 const char *years = "years ago", *months = "months ago";
2105 const char *weeks = "weeks ago", *days = "days ago";
2106 const char *hours = "hours ago", *minutes = "minutes ago";
2107 const char *seconds = "seconds ago", *now = "right now";
2108 char *s;
2109 char datebuf[29];
2111 *repo_age = NULL;
2113 switch (ref_tm) {
2114 case TM_DIFF:
2115 diff_time = time(NULL) - committer_time;
2116 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2117 if (asprintf(repo_age, "%lld %s",
2118 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2119 return got_error_from_errno("asprintf");
2120 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2121 if (asprintf(repo_age, "%lld %s",
2122 (diff_time / 60 / 60 / 24 / (365 / 12)),
2123 months) == -1)
2124 return got_error_from_errno("asprintf");
2125 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2126 if (asprintf(repo_age, "%lld %s",
2127 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2128 return got_error_from_errno("asprintf");
2129 } else if (diff_time > 60 * 60 * 24 * 2) {
2130 if (asprintf(repo_age, "%lld %s",
2131 (diff_time / 60 / 60 / 24), days) == -1)
2132 return got_error_from_errno("asprintf");
2133 } else if (diff_time > 60 * 60 * 2) {
2134 if (asprintf(repo_age, "%lld %s",
2135 (diff_time / 60 / 60), hours) == -1)
2136 return got_error_from_errno("asprintf");
2137 } else if (diff_time > 60 * 2) {
2138 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2139 minutes) == -1)
2140 return got_error_from_errno("asprintf");
2141 } else if (diff_time > 2) {
2142 if (asprintf(repo_age, "%lld %s", diff_time,
2143 seconds) == -1)
2144 return got_error_from_errno("asprintf");
2145 } else {
2146 if (asprintf(repo_age, "%s", now) == -1)
2147 return got_error_from_errno("asprintf");
2149 break;
2150 case TM_LONG:
2151 if (gmtime_r(&committer_time, &tm) == NULL)
2152 return got_error_from_errno("gmtime_r");
2154 s = asctime_r(&tm, datebuf);
2155 if (s == NULL)
2156 return got_error_from_errno("asctime_r");
2158 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2159 return got_error_from_errno("asprintf");
2160 break;
2162 return NULL;