Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
54 static const struct querystring_keys querystring_keys[] = {
55 { "action", ACTION },
56 { "commit", COMMIT },
57 { "file", RFILE },
58 { "folder", FOLDER },
59 { "headref", HEADREF },
60 { "index_page", INDEX_PAGE },
61 { "path", PATH },
62 { "page", PAGE },
63 };
65 static const struct action_keys action_keys[] = {
66 { "blame", BLAME },
67 { "blob", BLOB },
68 { "briefs", BRIEFS },
69 { "commits", COMMITS },
70 { "diff", DIFF },
71 { "error", ERR },
72 { "index", INDEX },
73 { "summary", SUMMARY },
74 { "tag", TAG },
75 { "tags", TAGS },
76 { "tree", TREE },
77 };
79 static const struct got_error *gotweb_init_querystring(struct querystring **);
80 static const struct got_error *gotweb_parse_querystring(struct querystring **,
81 char *);
82 static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 char *, char *);
84 static const struct got_error *gotweb_render_index(struct request *);
85 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 const char *);
87 static const struct got_error *gotweb_load_got_path(struct request *c,
88 struct repo_dir *);
89 static const struct got_error *gotweb_get_repo_description(char **,
90 struct server *, const char *, int);
91 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 const char *, int);
93 static const struct got_error *gotweb_render_blame(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 = got_get_repo_commits(c, srv->max_commits_display);
210 if (error) {
211 log_warnx("%s: %s", __func__, error->msg);
212 goto err;
214 if (gotweb_render_commits(c->tp) == -1)
215 goto err;
216 break;
217 case DIFF:
218 error = gotweb_render_diff(c);
219 if (error) {
220 log_warnx("%s: %s", __func__, error->msg);
221 goto err;
223 break;
224 case INDEX:
225 error = gotweb_render_index(c);
226 if (error) {
227 log_warnx("%s: %s", __func__, error->msg);
228 goto err;
230 break;
231 case SUMMARY:
232 error = gotweb_render_summary(c);
233 if (error) {
234 log_warnx("%s: %s", __func__, error->msg);
235 goto err;
237 break;
238 case TAG:
239 error = gotweb_render_tag(c);
240 if (error) {
241 log_warnx("%s: %s", __func__, error->msg);
242 goto err;
244 break;
245 case TAGS:
246 error = gotweb_render_tags(c);
247 if (error) {
248 log_warnx("%s: %s", __func__, error->msg);
249 goto err;
251 break;
252 case TREE:
253 error = gotweb_render_tree(c);
254 if (error) {
255 log_warnx("%s: %s", __func__, error->msg);
256 goto err;
258 break;
259 case ERR:
260 default:
261 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
262 "Erorr: Bad Querystring");
263 if (r == -1)
264 goto err;
265 break;
268 goto done;
269 err:
270 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
271 return;
272 if (fcgi_printf(c, "\n%s", err) == -1)
273 return;
274 if (error) {
275 if (fcgi_printf(c, "%s", error->msg) == -1)
276 return;
277 } else {
278 if (fcgi_printf(c, "see daemon logs for details") == -1)
279 return;
281 if (html && fcgi_printf(c, "</div>\n") == -1)
282 return;
283 done:
284 if (html && srv != NULL)
285 gotweb_render_footer(c->tp);
288 struct server *
289 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
291 struct server *srv = NULL;
293 /* check against the server name first */
294 if (strlen(server_name) > 0)
295 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
296 if (strcmp(srv->name, server_name) == 0)
297 goto done;
299 /* check against subdomain second */
300 if (strlen(subdomain) > 0)
301 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
302 if (strcmp(srv->name, subdomain) == 0)
303 goto done;
305 /* if those fail, send first server */
306 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
307 if (srv != NULL)
308 break;
309 done:
310 return srv;
311 };
313 const struct got_error *
314 gotweb_init_transport(struct transport **t)
316 const struct got_error *error = NULL;
318 *t = calloc(1, sizeof(**t));
319 if (*t == NULL)
320 return got_error_from_errno2("%s: calloc", __func__);
322 TAILQ_INIT(&(*t)->repo_commits);
323 TAILQ_INIT(&(*t)->repo_tags);
325 (*t)->repo = NULL;
326 (*t)->repo_dir = NULL;
327 (*t)->qs = NULL;
328 (*t)->next_id = NULL;
329 (*t)->prev_id = NULL;
330 (*t)->next_disp = 0;
331 (*t)->prev_disp = 0;
333 return error;
336 static const struct got_error *
337 gotweb_init_querystring(struct querystring **qs)
339 const struct got_error *error = NULL;
341 *qs = calloc(1, sizeof(**qs));
342 if (*qs == NULL)
343 return got_error_from_errno2("%s: calloc", __func__);
345 (*qs)->headref = strdup("HEAD");
346 if ((*qs)->headref == NULL) {
347 free(*qs);
348 *qs = NULL;
349 return got_error_from_errno2("%s: strdup", __func__);
352 (*qs)->action = INDEX;
353 (*qs)->commit = NULL;
354 (*qs)->file = NULL;
355 (*qs)->folder = NULL;
356 (*qs)->index_page = 0;
357 (*qs)->path = NULL;
359 return error;
362 static const struct got_error *
363 gotweb_parse_querystring(struct querystring **qs, char *qst)
365 const struct got_error *error = NULL;
366 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
367 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
369 if (qst == NULL)
370 return error;
372 tok1 = strdup(qst);
373 if (tok1 == NULL)
374 return got_error_from_errno2("%s: strdup", __func__);
376 tok1_pair = tok1;
377 tok1_end = tok1;
379 while (tok1_pair != NULL) {
380 strsep(&tok1_end, "&");
382 tok2 = strdup(tok1_pair);
383 if (tok2 == NULL) {
384 free(tok1);
385 return got_error_from_errno2("%s: strdup", __func__);
388 tok2_pair = tok2;
389 tok2_end = tok2;
391 while (tok2_pair != NULL) {
392 strsep(&tok2_end, "=");
393 if (tok2_end) {
394 error = gotweb_assign_querystring(qs, tok2_pair,
395 tok2_end);
396 if (error)
397 goto err;
399 tok2_pair = tok2_end;
401 free(tok2);
402 tok1_pair = tok1_end;
404 free(tok1);
405 return error;
406 err:
407 free(tok2);
408 free(tok1);
409 return error;
412 /*
413 * Adapted from usr.sbin/httpd/httpd.c url_decode.
414 */
415 static const struct got_error *
416 gotweb_urldecode(char *url)
418 char *p, *q;
419 char hex[3];
420 unsigned long x;
422 hex[2] = '\0';
423 p = q = url;
425 while (*p != '\0') {
426 switch (*p) {
427 case '%':
428 /* Encoding character is followed by two hex chars */
429 if (!isxdigit((unsigned char)p[1]) ||
430 !isxdigit((unsigned char)p[2]) ||
431 (p[1] == '0' && p[2] == '0'))
432 return got_error(GOT_ERR_BAD_QUERYSTRING);
434 hex[0] = p[1];
435 hex[1] = p[2];
437 /*
438 * We don't have to validate "hex" because it is
439 * guaranteed to include two hex chars followed by nul.
440 */
441 x = strtoul(hex, NULL, 16);
442 *q = (char)x;
443 p += 2;
444 break;
445 default:
446 *q = *p;
447 break;
449 p++;
450 q++;
452 *q = '\0';
454 return NULL;
457 static const struct got_error *
458 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
460 const struct got_error *error = NULL;
461 const char *errstr;
462 int a_cnt, el_cnt;
464 error = gotweb_urldecode(value);
465 if (error)
466 return error;
468 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
469 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
470 continue;
472 switch (querystring_keys[el_cnt].element) {
473 case ACTION:
474 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
475 if (strcmp(value, action_keys[a_cnt].name) != 0)
476 continue;
477 else if (strcmp(value,
478 action_keys[a_cnt].name) == 0){
479 (*qs)->action =
480 action_keys[a_cnt].action;
481 goto qa_found;
484 (*qs)->action = ERR;
485 qa_found:
486 break;
487 case COMMIT:
488 (*qs)->commit = strdup(value);
489 if ((*qs)->commit == NULL) {
490 error = got_error_from_errno2("%s: strdup",
491 __func__);
492 goto done;
494 break;
495 case RFILE:
496 (*qs)->file = strdup(value);
497 if ((*qs)->file == NULL) {
498 error = got_error_from_errno2("%s: strdup",
499 __func__);
500 goto done;
502 break;
503 case FOLDER:
504 (*qs)->folder = strdup(value);
505 if ((*qs)->folder == NULL) {
506 error = got_error_from_errno2("%s: strdup",
507 __func__);
508 goto done;
510 break;
511 case HEADREF:
512 free((*qs)->headref);
513 (*qs)->headref = strdup(value);
514 if ((*qs)->headref == NULL) {
515 error = got_error_from_errno2("%s: strdup",
516 __func__);
517 goto done;
519 break;
520 case INDEX_PAGE:
521 if (strlen(value) == 0)
522 break;
523 (*qs)->index_page = strtonum(value, INT64_MIN,
524 INT64_MAX, &errstr);
525 if (errstr) {
526 error = got_error_from_errno3("%s: strtonum %s",
527 __func__, errstr);
528 goto done;
530 if ((*qs)->index_page < 0)
531 (*qs)->index_page = 0;
532 break;
533 case PATH:
534 (*qs)->path = strdup(value);
535 if ((*qs)->path == NULL) {
536 error = got_error_from_errno2("%s: strdup",
537 __func__);
538 goto done;
540 break;
541 case PAGE:
542 if (strlen(value) == 0)
543 break;
544 (*qs)->page = strtonum(value, INT64_MIN,
545 INT64_MAX, &errstr);
546 if (errstr) {
547 error = got_error_from_errno3("%s: strtonum %s",
548 __func__, errstr);
549 goto done;
551 if ((*qs)->page < 0)
552 (*qs)->page = 0;
553 break;
554 default:
555 break;
558 done:
559 return error;
562 void
563 gotweb_free_repo_tag(struct repo_tag *rt)
565 if (rt != NULL) {
566 free(rt->commit_id);
567 free(rt->tag_name);
568 free(rt->tag_commit);
569 free(rt->commit_msg);
570 free(rt->tagger);
572 free(rt);
575 void
576 gotweb_free_repo_commit(struct repo_commit *rc)
578 if (rc != NULL) {
579 free(rc->path);
580 free(rc->refs_str);
581 free(rc->commit_id);
582 free(rc->parent_id);
583 free(rc->tree_id);
584 free(rc->author);
585 free(rc->committer);
586 free(rc->commit_msg);
588 free(rc);
591 static void
592 gotweb_free_querystring(struct querystring *qs)
594 if (qs != NULL) {
595 free(qs->commit);
596 free(qs->file);
597 free(qs->folder);
598 free(qs->headref);
599 free(qs->path);
601 free(qs);
604 static void
605 gotweb_free_repo_dir(struct repo_dir *repo_dir)
607 if (repo_dir != NULL) {
608 free(repo_dir->name);
609 free(repo_dir->owner);
610 free(repo_dir->description);
611 free(repo_dir->url);
612 free(repo_dir->age);
613 free(repo_dir->path);
615 free(repo_dir);
618 void
619 gotweb_free_transport(struct transport *t)
621 struct repo_commit *rc = NULL, *trc = NULL;
622 struct repo_tag *rt = NULL, *trt = NULL;
624 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
625 TAILQ_REMOVE(&t->repo_commits, rc, entry);
626 gotweb_free_repo_commit(rc);
628 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
629 TAILQ_REMOVE(&t->repo_tags, rt, entry);
630 gotweb_free_repo_tag(rt);
632 gotweb_free_repo_dir(t->repo_dir);
633 gotweb_free_querystring(t->qs);
634 free(t->next_id);
635 free(t->prev_id);
636 free(t);
639 const struct got_error *
640 gotweb_render_content_type(struct request *c, const uint8_t *type)
642 const char *csp = "default-src 'self'; script-src 'none'; "
643 "object-src 'none';";
645 fcgi_printf(c,
646 "Content-Security-Policy: %s\r\n"
647 "Content-Type: %s\r\n\r\n",
648 csp, type);
649 return NULL;
652 const struct got_error *
653 gotweb_render_content_type_file(struct request *c, const uint8_t *type,
654 char *file)
656 fcgi_printf(c, "Content-type: %s\r\n"
657 "Content-disposition: attachment; filename=%s\r\n\r\n",
658 type, file);
659 return NULL;
662 void
663 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
664 struct gotweb_url *next, int *have_next)
666 struct transport *t = c->t;
667 struct querystring *qs = t->qs;
668 struct server *srv = c->srv;
670 *have_prev = *have_next = 0;
672 switch(qs->action) {
673 case INDEX:
674 if (qs->index_page > 0) {
675 *have_prev = 1;
676 *prev = (struct gotweb_url){
677 .action = -1,
678 .index_page = qs->index_page - 1,
679 .page = -1,
680 };
682 if (t->next_disp == srv->max_repos_display &&
683 t->repos_total != (qs->index_page + 1) *
684 srv->max_repos_display) {
685 *have_next = 1;
686 *next = (struct gotweb_url){
687 .action = -1,
688 .index_page = qs->index_page + 1,
689 .page = -1,
690 };
692 break;
693 case BRIEFS:
694 if (t->prev_id && qs->commit != NULL &&
695 strcmp(qs->commit, t->prev_id) != 0) {
696 *have_prev = 1;
697 *prev = (struct gotweb_url){
698 .action = BRIEFS,
699 .index_page = -1,
700 .page = qs->page - 1,
701 .path = qs->path,
702 .commit = t->prev_id,
703 .headref = qs->headref,
704 };
706 if (t->next_id) {
707 *have_next = 1;
708 *next = (struct gotweb_url){
709 .action = BRIEFS,
710 .index_page = -1,
711 .page = qs->page + 1,
712 .path = qs->path,
713 .commit = t->next_id,
714 .headref = qs->headref,
715 };
717 break;
718 case COMMITS:
719 if (t->prev_id && qs->commit != NULL &&
720 strcmp(qs->commit, t->prev_id) != 0) {
721 *have_prev = 1;
722 *prev = (struct gotweb_url){
723 .action = COMMITS,
724 .index_page = -1,
725 .page = qs->page - 1,
726 .path = qs->path,
727 .commit = t->prev_id,
728 .headref = qs->headref,
729 .folder = qs->folder,
730 .file = qs->file,
731 };
733 if (t->next_id) {
734 *have_next = 1;
735 *next = (struct gotweb_url){
736 .action = COMMITS,
737 .index_page = -1,
738 .page = qs->page + 1,
739 .path = qs->path,
740 .commit = t->next_id,
741 .headref = qs->headref,
742 .folder = qs->folder,
743 .file = qs->file,
744 };
746 break;
747 case TAGS:
748 if (t->prev_id && qs->commit != NULL &&
749 strcmp(qs->commit, t->prev_id) != 0) {
750 *have_prev = 1;
751 *prev = (struct gotweb_url){
752 .action = TAGS,
753 .index_page = -1,
754 .page = qs->page - 1,
755 .path = qs->path,
756 .commit = t->prev_id,
757 .headref = qs->headref,
758 };
760 if (t->next_id) {
761 *have_next = 1;
762 *next = (struct gotweb_url){
763 .action = TAGS,
764 .index_page = -1,
765 .page = qs->page + 1,
766 .path = qs->path,
767 .commit = t->next_id,
768 .headref = qs->headref,
769 };
771 break;
775 static const struct got_error *
776 gotweb_render_index(struct request *c)
778 const struct got_error *error = NULL;
779 struct server *srv = c->srv;
780 struct transport *t = c->t;
781 struct querystring *qs = t->qs;
782 struct repo_dir *repo_dir = NULL;
783 DIR *d;
784 struct dirent **sd_dent = NULL;
785 unsigned int d_cnt, d_i, d_disp = 0;
786 unsigned int d_skipped = 0;
787 int type;
789 d = opendir(srv->repos_path);
790 if (d == NULL) {
791 error = got_error_from_errno2("opendir", srv->repos_path);
792 return error;
795 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
796 if (d_cnt == -1) {
797 sd_dent = NULL;
798 error = got_error_from_errno2("scandir", srv->repos_path);
799 goto done;
802 if (gotweb_render_repo_table_hdr(c->tp) == -1)
803 goto done;
805 for (d_i = 0; d_i < d_cnt; d_i++) {
806 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
807 break;
809 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
810 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
811 d_skipped++;
812 continue;
815 error = got_path_dirent_type(&type, srv->repos_path,
816 sd_dent[d_i]);
817 if (error)
818 goto done;
819 if (type != DT_DIR) {
820 d_skipped++;
821 continue;
824 if (qs->index_page > 0 && (qs->index_page *
825 srv->max_repos_display) > t->prev_disp) {
826 t->prev_disp++;
827 continue;
830 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
831 if (error)
832 goto done;
834 error = gotweb_load_got_path(c, repo_dir);
835 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
836 error = NULL;
837 gotweb_free_repo_dir(repo_dir);
838 repo_dir = NULL;
839 d_skipped++;
840 continue;
842 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
843 goto done;
845 d_disp++;
846 t->prev_disp++;
848 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
849 goto done;
851 gotweb_free_repo_dir(repo_dir);
852 repo_dir = NULL;
853 t->next_disp++;
854 if (d_disp == srv->max_repos_display)
855 break;
857 t->repos_total = d_cnt - d_skipped;
859 if (srv->max_repos_display == 0)
860 goto done;
861 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
862 goto done;
863 if (t->repos_total <= srv->max_repos ||
864 t->repos_total <= srv->max_repos_display)
865 goto done;
867 if (gotweb_render_navs(c->tp) == -1)
868 goto done;
869 done:
870 if (sd_dent) {
871 for (d_i = 0; d_i < d_cnt; d_i++)
872 free(sd_dent[d_i]);
873 free(sd_dent);
875 if (d != NULL && closedir(d) == EOF && error == NULL)
876 error = got_error_from_errno("closedir");
877 return error;
880 static const struct got_error *
881 gotweb_render_blame(struct request *c)
883 const struct got_error *error = NULL;
884 struct transport *t = c->t;
885 struct repo_commit *rc = NULL;
886 char *age = NULL, *msg = NULL;
887 int r;
889 error = got_get_repo_commits(c, 1);
890 if (error)
891 return error;
893 rc = TAILQ_FIRST(&t->repo_commits);
895 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
896 if (error)
897 goto done;
898 error = gotweb_escape_html(&msg, rc->commit_msg);
899 if (error)
900 goto done;
902 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
903 "<div id='blame_title'>Blame</div>\n"
904 "</div>\n" /* #blame_title_wrapper */
905 "<div id='blame_content'>\n"
906 "<div id='blame_header_wrapper'>\n"
907 "<div id='blame_header'>\n"
908 "<div class='header_age_title'>Date:</div>\n"
909 "<div class='header_age'>%s</div>\n"
910 "<div id='header_commit_msg_title'>Message:</div>\n"
911 "<div id='header_commit_msg'>%s</div>\n"
912 "</div>\n" /* #blame_header */
913 "</div>\n" /* #blame_header_wrapper */
914 "<div class='dotted_line'></div>\n"
915 "<div id='blame'>\n",
916 age,
917 msg);
918 if (r == -1)
919 goto done;
921 error = got_output_file_blame(c);
922 if (error)
923 goto done;
925 fcgi_printf(c, "</div>\n" /* #blame */
926 "</div>\n"); /* #blame_content */
927 done:
928 free(age);
929 free(msg);
930 return error;
933 static const struct got_error *
934 gotweb_render_branches(struct request *c)
936 const struct got_error *error = NULL;
937 struct got_reflist_head refs;
938 struct got_reflist_entry *re;
939 struct transport *t = c->t;
940 struct querystring *qs = t->qs;
941 struct got_repository *repo = t->repo;
942 char *escaped_refname = NULL;
943 char *age = NULL;
944 int r;
946 TAILQ_INIT(&refs);
948 error = got_ref_list(&refs, repo, "refs/heads",
949 got_ref_cmp_by_name, NULL);
950 if (error)
951 goto done;
953 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
954 "<div id='branches_title'>Branches</div>\n"
955 "</div>\n" /* #branches_title_wrapper */
956 "<div id='branches_content'>\n");
957 if (r == -1)
958 goto done;
960 TAILQ_FOREACH(re, &refs, entry) {
961 const char *refname = NULL;
963 if (got_ref_is_symbolic(re->ref))
964 continue;
966 refname = got_ref_get_name(re->ref);
967 if (refname == NULL) {
968 error = got_error_from_errno("strdup");
969 goto done;
971 if (strncmp(refname, "refs/heads/", 11) != 0)
972 continue;
974 error = got_get_repo_age(&age, c, refname, TM_DIFF);
975 if (error)
976 goto done;
978 if (strncmp(refname, "refs/heads/", 11) == 0)
979 refname += 11;
980 error = gotweb_escape_html(&escaped_refname, refname);
981 if (error)
982 goto done;
984 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
985 "<div class='branches_age'>%s</div>\n"
986 "<div class='branches_space'>&nbsp;</div>\n"
987 "<div class='branch'>", age);
988 if (r == -1)
989 goto done;
991 r = gotweb_link(c, &(struct gotweb_url){
992 .action = SUMMARY,
993 .index_page = -1,
994 .page = -1,
995 .path = qs->path,
996 .headref = refname,
997 }, "%s", escaped_refname);
998 if (r == -1)
999 goto done;
1001 if (fcgi_printf(c, "</div>\n" /* .branch */
1002 "<div class='navs_wrapper'>\n"
1003 "<div class='navs'>") == -1)
1004 goto done;
1006 r = gotweb_link(c, &(struct gotweb_url){
1007 .action = SUMMARY,
1008 .index_page = -1,
1009 .page = -1,
1010 .path = qs->path,
1011 .headref = refname,
1012 }, "summary");
1013 if (r == -1)
1014 goto done;
1016 if (fcgi_printf(c, " | ") == -1)
1017 goto done;
1019 r = gotweb_link(c, &(struct gotweb_url){
1020 .action = BRIEFS,
1021 .index_page = -1,
1022 .page = -1,
1023 .path = qs->path,
1024 .headref = refname,
1025 }, "commit briefs");
1026 if (r == -1)
1027 goto done;
1029 if (fcgi_printf(c, " | ") == -1)
1030 goto done;
1032 r = gotweb_link(c, &(struct gotweb_url){
1033 .action = COMMITS,
1034 .index_page = -1,
1035 .page = -1,
1036 .path = qs->path,
1037 .headref = refname,
1038 }, "commits");
1039 if (r == -1)
1040 goto done;
1042 r = fcgi_printf(c, "</div>\n" /* .navs */
1043 "</div>\n" /* .navs_wrapper */
1044 "<div class='dotted_line'></div>\n"
1045 "</div>\n"); /* .branches_wrapper */
1046 if (r == -1)
1047 goto done;
1049 free(age);
1050 age = NULL;
1051 free(escaped_refname);
1052 escaped_refname = NULL;
1054 fcgi_printf(c, "</div>\n"); /* #branches_content */
1055 done:
1056 free(age);
1057 free(escaped_refname);
1058 got_ref_list_free(&refs);
1059 return error;
1062 static const struct got_error *
1063 gotweb_render_tree(struct request *c)
1065 const struct got_error *error = NULL;
1066 struct transport *t = c->t;
1067 struct repo_commit *rc = NULL;
1068 char *age = NULL, *msg = NULL;
1069 int r;
1071 error = got_get_repo_commits(c, 1);
1072 if (error)
1073 return error;
1075 rc = TAILQ_FIRST(&t->repo_commits);
1077 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1078 if (error)
1079 goto done;
1081 error = gotweb_escape_html(&msg, rc->commit_msg);
1082 if (error)
1083 goto done;
1085 r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1086 "<div id='tree_title'>Tree</div>\n"
1087 "</div>\n" /* #tree_title_wrapper */
1088 "<div id='tree_content'>\n"
1089 "<div id='tree_header_wrapper'>\n"
1090 "<div id='tree_header'>\n"
1091 "<div id='header_tree_title'>Tree:</div>\n"
1092 "<div id='header_tree'>%s</div>\n"
1093 "<div class='header_age_title'>Date:</div>\n"
1094 "<div class='header_age'>%s</div>\n"
1095 "<div id='header_commit_msg_title'>Message:</div>\n"
1096 "<div id='header_commit_msg'>%s</div>\n"
1097 "</div>\n" /* #tree_header */
1098 "</div>\n" /* #tree_header_wrapper */
1099 "<div class='dotted_line'></div>\n"
1100 "<div id='tree'>\n",
1101 rc->tree_id,
1102 age,
1103 msg);
1104 if (r == -1)
1105 goto done;
1107 error = got_output_repo_tree(c);
1108 if (error)
1109 goto done;
1111 fcgi_printf(c, "</div>\n"); /* #tree */
1112 fcgi_printf(c, "</div>\n"); /* #tree_content */
1113 done:
1114 free(age);
1115 free(msg);
1116 return error;
1119 static const struct got_error *
1120 gotweb_render_diff(struct request *c)
1122 const struct got_error *error = NULL;
1123 struct transport *t = c->t;
1124 struct repo_commit *rc = NULL;
1125 char *age = NULL, *author = NULL, *msg = NULL;
1126 int r;
1128 error = got_get_repo_commits(c, 1);
1129 if (error)
1130 return error;
1132 rc = TAILQ_FIRST(&t->repo_commits);
1134 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1135 if (error)
1136 goto done;
1137 error = gotweb_escape_html(&author, rc->author);
1138 if (error)
1139 goto done;
1140 error = gotweb_escape_html(&msg, rc->commit_msg);
1141 if (error)
1142 goto done;
1144 r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1145 "<div id='diff_title'>Commit Diff</div>\n"
1146 "</div>\n" /* #diff_title_wrapper */
1147 "<div id='diff_content'>\n"
1148 "<div id='diff_header_wrapper'>\n"
1149 "<div id='diff_header'>\n"
1150 "<div id='header_diff_title'>Diff:</div>\n"
1151 "<div id='header_diff'>%s<br />%s</div>\n"
1152 "<div class='header_commit_title'>Commit:</div>\n"
1153 "<div class='header_commit'>%s</div>\n"
1154 "<div id='header_tree_title'>Tree:</div>\n"
1155 "<div id='header_tree'>%s</div>\n"
1156 "<div class='header_author_title'>Author:</div>\n"
1157 "<div class='header_author'>%s</div>\n"
1158 "<div class='header_age_title'>Date:</div>\n"
1159 "<div class='header_age'>%s</div>\n"
1160 "<div id='header_commit_msg_title'>Message:</div>\n"
1161 "<div id='header_commit_msg'>%s</div>\n"
1162 "</div>\n" /* #diff_header */
1163 "</div>\n" /* #diff_header_wrapper */
1164 "<div class='dotted_line'></div>\n"
1165 "<div id='diff'>\n",
1166 rc->parent_id, rc->commit_id,
1167 rc->commit_id,
1168 rc->tree_id,
1169 author,
1170 age,
1171 msg);
1172 if (r == -1)
1173 goto done;
1175 error = got_output_repo_diff(c);
1176 if (error)
1177 goto done;
1179 fcgi_printf(c, "</div>\n"); /* #diff */
1180 fcgi_printf(c, "</div>\n"); /* #diff_content */
1181 done:
1182 free(age);
1183 free(author);
1184 free(msg);
1185 return error;
1188 static const struct got_error *
1189 gotweb_render_summary(struct request *c)
1191 const struct got_error *error = NULL;
1192 struct transport *t = c->t;
1193 struct server *srv = c->srv;
1194 int r;
1196 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1197 goto done;
1199 if (srv->show_repo_description) {
1200 r = fcgi_printf(c,
1201 "<div id='description_title'>Description:</div>\n"
1202 "<div id='description'>%s</div>\n",
1203 t->repo_dir->description ? t->repo_dir->description : "");
1204 if (r == -1)
1205 goto done;
1208 if (srv->show_repo_owner) {
1209 r = fcgi_printf(c,
1210 "<div id='repo_owner_title'>Owner:</div>\n"
1211 "<div id='repo_owner'>%s</div>\n",
1212 t->repo_dir->owner ? t->repo_dir->owner : "");
1213 if (r == -1)
1214 goto done;
1217 if (srv->show_repo_age) {
1218 r = fcgi_printf(c,
1219 "<div id='last_change_title'>Last Change:</div>\n"
1220 "<div id='last_change'>%s</div>\n",
1221 t->repo_dir->age);
1222 if (r == -1)
1223 goto done;
1226 if (srv->show_repo_cloneurl) {
1227 r = fcgi_printf(c,
1228 "<div id='cloneurl_title'>Clone URL:</div>\n"
1229 "<div id='cloneurl'>%s</div>\n",
1230 t->repo_dir->url ? t->repo_dir->url : "");
1231 if (r == -1)
1232 goto done;
1235 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1236 if (r == -1)
1237 goto done;
1239 if (gotweb_render_briefs(c->tp) == -1)
1240 goto done;
1242 error = gotweb_render_tags(c);
1243 if (error) {
1244 log_warnx("%s: %s", __func__, error->msg);
1245 goto done;
1248 error = gotweb_render_branches(c);
1249 if (error)
1250 log_warnx("%s: %s", __func__, error->msg);
1251 done:
1252 return error;
1255 static const struct got_error *
1256 gotweb_render_tag(struct request *c)
1258 const struct got_error *error = NULL;
1259 struct repo_tag *rt = NULL;
1260 struct transport *t = c->t;
1261 char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1263 error = got_get_repo_tags(c, 1);
1264 if (error)
1265 goto done;
1267 if (t->tag_count == 0) {
1268 error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1269 "bad commit id");
1270 goto done;
1273 rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1275 error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1276 if (error)
1277 goto done;
1278 error = gotweb_escape_html(&author, rt->tagger);
1279 if (error)
1280 goto done;
1281 error = gotweb_escape_html(&msg, rt->commit_msg);
1282 if (error)
1283 goto done;
1285 tagname = rt->tag_name;
1286 if (strncmp(tagname, "refs/", 5) == 0)
1287 tagname += 5;
1288 error = gotweb_escape_html(&tagname, tagname);
1289 if (error)
1290 goto done;
1292 fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1293 "<div id='tags_title'>Tag</div>\n"
1294 "</div>\n" /* #tags_title_wrapper */
1295 "<div id='tags_content'>\n"
1296 "<div id='tag_header_wrapper'>\n"
1297 "<div id='tag_header'>\n"
1298 "<div class='header_commit_title'>Commit:</div>\n"
1299 "<div class='header_commit'>%s"
1300 " <span class='refs_str'>(%s)</span></div>\n"
1301 "<div class='header_author_title'>Tagger:</div>\n"
1302 "<div class='header_author'>%s</div>\n"
1303 "<div class='header_age_title'>Date:</div>\n"
1304 "<div class='header_age'>%s</div>\n"
1305 "<div id='header_commit_msg_title'>Message:</div>\n"
1306 "<div id='header_commit_msg'>%s</div>\n"
1307 "</div>\n" /* #tag_header */
1308 "<div class='dotted_line'></div>\n"
1309 "<div id='tag_commit'>\n%s</div>"
1310 "</div>" /* #tag_header_wrapper */
1311 "</div>", /* #tags_content */
1312 rt->commit_id,
1313 tagname,
1314 author,
1315 age,
1316 msg,
1317 rt->tag_commit);
1319 done:
1320 free(age);
1321 free(author);
1322 free(msg);
1323 return error;
1326 static const struct got_error *
1327 gotweb_render_tags(struct request *c)
1329 const struct got_error *error = NULL;
1330 struct repo_tag *rt = NULL;
1331 struct server *srv = c->srv;
1332 struct transport *t = c->t;
1333 struct querystring *qs = t->qs;
1334 struct repo_dir *repo_dir = t->repo_dir;
1335 char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1336 int r, commit_found = 0;
1338 if (qs->action == BRIEFS) {
1339 qs->action = TAGS;
1340 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1341 } else
1342 error = got_get_repo_tags(c, srv->max_commits_display);
1343 if (error)
1344 goto done;
1346 r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1347 "<div id='tags_title'>Tags</div>\n"
1348 "</div>\n" /* #tags_title_wrapper */
1349 "<div id='tags_content'>\n");
1350 if (r == -1)
1351 goto done;
1353 if (t->tag_count == 0) {
1354 r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1355 "This repository contains no tags");
1356 if (r == -1)
1357 goto done;
1360 TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1361 if (commit_found == 0 && qs->commit != NULL) {
1362 if (strcmp(qs->commit, rt->commit_id) != 0)
1363 continue;
1364 else
1365 commit_found = 1;
1367 error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1368 if (error)
1369 goto done;
1371 tagname = rt->tag_name;
1372 if (strncmp(tagname, "refs/tags/", 10) == 0)
1373 tagname += 10;
1374 error = gotweb_escape_html(&tagname, tagname);
1375 if (error)
1376 goto done;
1378 if (rt->tag_commit != NULL) {
1379 newline = strchr(rt->tag_commit, '\n');
1380 if (newline)
1381 *newline = '\0';
1382 error = gotweb_escape_html(&msg, rt->tag_commit);
1383 if (error)
1384 goto done;
1387 if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1388 "<div class='tag'>%s</div>\n"
1389 "<div class='tag_log'>", age, tagname) == -1)
1390 goto done;
1392 r = gotweb_link(c, &(struct gotweb_url){
1393 .action = TAG,
1394 .index_page = -1,
1395 .page = -1,
1396 .path = repo_dir->name,
1397 .commit = rt->commit_id,
1398 }, "%s", msg ? msg : "");
1399 if (r == -1)
1400 goto done;
1402 if (fcgi_printf(c, "</div>\n" /* .tag_log */
1403 "<div class='navs_wrapper'>\n"
1404 "<div class='navs'>") == -1)
1405 goto done;
1407 r = gotweb_link(c, &(struct gotweb_url){
1408 .action = TAG,
1409 .index_page = -1,
1410 .page = -1,
1411 .path = repo_dir->name,
1412 .commit = rt->commit_id,
1413 }, "tag");
1414 if (r == -1)
1415 goto done;
1417 if (fcgi_printf(c, " | ") == -1)
1418 goto done;
1420 r = gotweb_link(c, &(struct gotweb_url){
1421 .action = BRIEFS,
1422 .index_page = -1,
1423 .page = -1,
1424 .path = repo_dir->name,
1425 .commit = rt->commit_id,
1426 }, "commit briefs");
1427 if (r == -1)
1428 goto done;
1430 if (fcgi_printf(c, " | ") == -1)
1431 goto done;
1433 r = gotweb_link(c, &(struct gotweb_url){
1434 .action = COMMITS,
1435 .index_page = -1,
1436 .page = -1,
1437 .path = repo_dir->name,
1438 .commit = rt->commit_id,
1439 }, "commits");
1440 if (r == -1)
1441 goto done;
1443 r = fcgi_printf(c,
1444 "</div>\n" /* .navs */
1445 "</div>\n" /* .navs_wrapper */
1446 "<div class='dotted_line'></div>\n");
1447 if (r == -1)
1448 goto done;
1450 free(age);
1451 age = NULL;
1452 free(tagname);
1453 tagname = NULL;
1454 free(msg);
1455 msg = NULL;
1457 if (t->next_id || t->prev_id) {
1458 if (gotweb_render_navs(c->tp) == -1)
1459 goto done;
1461 fcgi_printf(c, "</div>\n"); /* #tags_content */
1462 done:
1463 free(age);
1464 free(tagname);
1465 free(msg);
1466 return error;
1469 const struct got_error *
1470 gotweb_escape_html(char **escaped_html, const char *orig_html)
1472 const struct got_error *error = NULL;
1473 struct escape_pair {
1474 char c;
1475 const char *s;
1476 } esc[] = {
1477 { '>', "&gt;" },
1478 { '<', "&lt;" },
1479 { '&', "&amp;" },
1480 { '"', "&quot;" },
1481 { '\'', "&apos;" },
1482 { '\n', "<br />" },
1484 size_t orig_len, len;
1485 int i, j, x;
1487 orig_len = strlen(orig_html);
1488 len = orig_len;
1489 for (i = 0; i < orig_len; i++) {
1490 for (j = 0; j < nitems(esc); j++) {
1491 if (orig_html[i] != esc[j].c)
1492 continue;
1493 len += strlen(esc[j].s) - 1 /* escaped char */;
1497 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1498 if (*escaped_html == NULL)
1499 return got_error_from_errno("calloc");
1501 x = 0;
1502 for (i = 0; i < orig_len; i++) {
1503 int escaped = 0;
1504 for (j = 0; j < nitems(esc); j++) {
1505 if (orig_html[i] != esc[j].c)
1506 continue;
1508 if (strlcat(*escaped_html, esc[j].s, len + 1)
1509 >= len + 1) {
1510 error = got_error(GOT_ERR_NO_SPACE);
1511 goto done;
1513 x += strlen(esc[j].s);
1514 escaped = 1;
1515 break;
1517 if (!escaped) {
1518 (*escaped_html)[x] = orig_html[i];
1519 x++;
1522 done:
1523 if (error) {
1524 free(*escaped_html);
1525 *escaped_html = NULL;
1526 } else {
1527 (*escaped_html)[x] = '\0';
1530 return error;
1533 static inline int
1534 should_urlencode(int c)
1536 if (c <= ' ' || c >= 127)
1537 return 1;
1539 switch (c) {
1540 /* gen-delim */
1541 case ':':
1542 case '/':
1543 case '?':
1544 case '#':
1545 case '[':
1546 case ']':
1547 case '@':
1548 /* sub-delims */
1549 case '!':
1550 case '$':
1551 case '&':
1552 case '\'':
1553 case '(':
1554 case ')':
1555 case '*':
1556 case '+':
1557 case ',':
1558 case ';':
1559 case '=':
1560 return 1;
1561 default:
1562 return 0;
1566 static char *
1567 gotweb_urlencode(const char *str)
1569 const char *s;
1570 char *escaped;
1571 size_t i, len;
1572 int a, b;
1574 len = 0;
1575 for (s = str; *s; ++s) {
1576 len++;
1577 if (should_urlencode(*s))
1578 len += 2;
1581 escaped = calloc(1, len + 1);
1582 if (escaped == NULL)
1583 return NULL;
1585 i = 0;
1586 for (s = str; *s; ++s) {
1587 if (should_urlencode(*s)) {
1588 a = (*s & 0xF0) >> 4;
1589 b = (*s & 0x0F);
1591 escaped[i++] = '%';
1592 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1593 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1594 } else
1595 escaped[i++] = *s;
1598 return escaped;
1601 const char *
1602 gotweb_action_name(int action)
1604 switch (action) {
1605 case BLAME:
1606 return "blame";
1607 case BLOB:
1608 return "blob";
1609 case BRIEFS:
1610 return "briefs";
1611 case COMMITS:
1612 return "commits";
1613 case DIFF:
1614 return "diff";
1615 case ERR:
1616 return "err";
1617 case INDEX:
1618 return "index";
1619 case SUMMARY:
1620 return "summary";
1621 case TAG:
1622 return "tag";
1623 case TAGS:
1624 return "tags";
1625 case TREE:
1626 return "tree";
1627 default:
1628 return NULL;
1632 int
1633 gotweb_render_url(struct request *c, struct gotweb_url *url)
1635 const char *sep = "?", *action;
1636 char *tmp;
1637 int r;
1639 action = gotweb_action_name(url->action);
1640 if (action != NULL) {
1641 if (fcgi_printf(c, "?action=%s", action) == -1)
1642 return -1;
1643 sep = "&";
1646 if (url->commit) {
1647 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1648 return -1;
1649 sep = "&";
1652 if (url->previd) {
1653 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1654 return -1;
1655 sep = "&";
1658 if (url->prevset) {
1659 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1660 return -1;
1661 sep = "&";
1664 if (url->file) {
1665 tmp = gotweb_urlencode(url->file);
1666 if (tmp == NULL)
1667 return -1;
1668 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1669 free(tmp);
1670 if (r == -1)
1671 return -1;
1672 sep = "&";
1675 if (url->folder) {
1676 tmp = gotweb_urlencode(url->folder);
1677 if (tmp == NULL)
1678 return -1;
1679 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1680 free(tmp);
1681 if (r == -1)
1682 return -1;
1683 sep = "&";
1686 if (url->headref) {
1687 tmp = gotweb_urlencode(url->headref);
1688 if (tmp == NULL)
1689 return -1;
1690 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1691 free(tmp);
1692 if (r == -1)
1693 return -1;
1694 sep = "&";
1697 if (url->index_page != -1) {
1698 if (fcgi_printf(c, "%sindex_page=%d", sep,
1699 url->index_page) == -1)
1700 return -1;
1701 sep = "&";
1704 if (url->path) {
1705 tmp = gotweb_urlencode(url->path);
1706 if (tmp == NULL)
1707 return -1;
1708 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1709 free(tmp);
1710 if (r == -1)
1711 return -1;
1712 sep = "&";
1715 if (url->page != -1) {
1716 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1717 return -1;
1718 sep = "&";
1721 return 0;
1724 int
1725 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1727 va_list ap;
1728 int r;
1730 if (fcgi_printf(c, "<a href='") == -1)
1731 return -1;
1733 if (gotweb_render_url(c, url) == -1)
1734 return -1;
1736 if (fcgi_printf(c, "'>") == -1)
1737 return -1;
1739 va_start(ap, fmt);
1740 r = fcgi_vprintf(c, fmt, ap);
1741 va_end(ap);
1742 if (r == -1)
1743 return -1;
1745 if (fcgi_printf(c, "</a>"))
1746 return -1;
1747 return 0;
1750 static struct got_repository *
1751 find_cached_repo(struct server *srv, const char *path)
1753 int i;
1755 for (i = 0; i < srv->ncached_repos; i++) {
1756 if (strcmp(srv->cached_repos[i].path, path) == 0)
1757 return srv->cached_repos[i].repo;
1760 return NULL;
1763 static const struct got_error *
1764 cache_repo(struct got_repository **new, struct server *srv,
1765 struct repo_dir *repo_dir, struct socket *sock)
1767 const struct got_error *error = NULL;
1768 struct got_repository *repo;
1769 struct cached_repo *cr;
1770 int evicted = 0;
1772 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1773 cr = &srv->cached_repos[srv->ncached_repos - 1];
1774 error = got_repo_close(cr->repo);
1775 memset(cr, 0, sizeof(*cr));
1776 srv->ncached_repos--;
1777 if (error)
1778 return error;
1779 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1780 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1781 cr = &srv->cached_repos[0];
1782 evicted = 1;
1783 } else {
1784 cr = &srv->cached_repos[srv->ncached_repos];
1787 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1788 if (error) {
1789 if (evicted) {
1790 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1791 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1793 return error;
1796 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1797 >= sizeof(cr->path)) {
1798 if (evicted) {
1799 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1800 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1802 return got_error(GOT_ERR_NO_SPACE);
1805 cr->repo = repo;
1806 srv->ncached_repos++;
1807 *new = repo;
1808 return NULL;
1811 static const struct got_error *
1812 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1814 const struct got_error *error = NULL;
1815 struct socket *sock = c->sock;
1816 struct server *srv = c->srv;
1817 struct transport *t = c->t;
1818 struct got_repository *repo = NULL;
1819 DIR *dt;
1820 char *dir_test;
1822 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1823 GOTWEB_GIT_DIR) == -1)
1824 return got_error_from_errno("asprintf");
1826 dt = opendir(dir_test);
1827 if (dt == NULL) {
1828 free(dir_test);
1829 } else {
1830 repo_dir->path = dir_test;
1831 dir_test = NULL;
1832 goto done;
1835 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1836 repo_dir->name) == -1)
1837 return got_error_from_errno("asprintf");
1839 dt = opendir(dir_test);
1840 if (dt == NULL) {
1841 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1842 goto err;
1843 } else {
1844 repo_dir->path = dir_test;
1845 dir_test = NULL;
1848 done:
1849 if (srv->respect_exportok &&
1850 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1851 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1852 goto err;
1855 repo = find_cached_repo(srv, repo_dir->path);
1856 if (repo == NULL) {
1857 error = cache_repo(&repo, srv, repo_dir, sock);
1858 if (error)
1859 goto err;
1861 t->repo = repo;
1862 error = gotweb_get_repo_description(&repo_dir->description, srv,
1863 repo_dir->path, dirfd(dt));
1864 if (error)
1865 goto err;
1866 error = got_get_repo_owner(&repo_dir->owner, c);
1867 if (error)
1868 goto err;
1869 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1870 if (error)
1871 goto err;
1872 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1873 dirfd(dt));
1874 err:
1875 free(dir_test);
1876 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1877 error = got_error_from_errno("closedir");
1878 return error;
1881 static const struct got_error *
1882 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1884 const struct got_error *error;
1886 *repo_dir = calloc(1, sizeof(**repo_dir));
1887 if (*repo_dir == NULL)
1888 return got_error_from_errno("calloc");
1890 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1891 error = got_error_from_errno("asprintf");
1892 free(*repo_dir);
1893 *repo_dir = NULL;
1894 return error;
1896 (*repo_dir)->owner = NULL;
1897 (*repo_dir)->description = NULL;
1898 (*repo_dir)->url = NULL;
1899 (*repo_dir)->age = NULL;
1900 (*repo_dir)->path = NULL;
1902 return NULL;
1905 static const struct got_error *
1906 gotweb_get_repo_description(char **description, struct server *srv,
1907 const char *dirpath, int dir)
1909 const struct got_error *error = NULL;
1910 struct stat sb;
1911 int fd = -1;
1912 off_t len;
1914 *description = NULL;
1915 if (srv->show_repo_description == 0)
1916 return NULL;
1918 fd = openat(dir, "description", O_RDONLY);
1919 if (fd == -1) {
1920 if (errno != ENOENT && errno != EACCES) {
1921 error = got_error_from_errno_fmt("openat %s/%s",
1922 dirpath, "description");
1924 goto done;
1927 if (fstat(fd, &sb) == -1) {
1928 error = got_error_from_errno_fmt("fstat %s/%s",
1929 dirpath, "description");
1930 goto done;
1933 len = sb.st_size;
1934 if (len > GOTWEBD_MAXDESCRSZ - 1)
1935 len = GOTWEBD_MAXDESCRSZ - 1;
1937 *description = calloc(len + 1, sizeof(**description));
1938 if (*description == NULL) {
1939 error = got_error_from_errno("calloc");
1940 goto done;
1943 if (read(fd, *description, len) == -1)
1944 error = got_error_from_errno("read");
1945 done:
1946 if (fd != -1 && close(fd) == -1 && error == NULL)
1947 error = got_error_from_errno("close");
1948 return error;
1951 static const struct got_error *
1952 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1953 int dir)
1955 const struct got_error *error = NULL;
1956 struct stat sb;
1957 int fd = -1;
1958 off_t len;
1960 *url = NULL;
1961 if (srv->show_repo_cloneurl == 0)
1962 return NULL;
1964 fd = openat(dir, "cloneurl", O_RDONLY);
1965 if (fd == -1) {
1966 if (errno != ENOENT && errno != EACCES) {
1967 error = got_error_from_errno_fmt("openat %s/%s",
1968 dirpath, "cloneurl");
1970 goto done;
1973 if (fstat(fd, &sb) == -1) {
1974 error = got_error_from_errno_fmt("fstat %s/%s",
1975 dirpath, "cloneurl");
1976 goto done;
1979 len = sb.st_size;
1980 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1981 len = GOTWEBD_MAXCLONEURLSZ - 1;
1983 *url = calloc(len + 1, sizeof(**url));
1984 if (*url == NULL) {
1985 error = got_error_from_errno("calloc");
1986 goto done;
1989 if (read(fd, *url, len) == -1)
1990 error = got_error_from_errno("read");
1991 done:
1992 if (fd != -1 && close(fd) == -1 && error == NULL)
1993 error = got_error_from_errno("close");
1994 return error;
1997 const struct got_error *
1998 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2000 struct tm tm;
2001 long long diff_time;
2002 const char *years = "years ago", *months = "months ago";
2003 const char *weeks = "weeks ago", *days = "days ago";
2004 const char *hours = "hours ago", *minutes = "minutes ago";
2005 const char *seconds = "seconds ago", *now = "right now";
2006 char *s;
2007 char datebuf[29];
2009 *repo_age = NULL;
2011 switch (ref_tm) {
2012 case TM_DIFF:
2013 diff_time = time(NULL) - committer_time;
2014 if (diff_time > 60 * 60 * 24 * 365 * 2) {
2015 if (asprintf(repo_age, "%lld %s",
2016 (diff_time / 60 / 60 / 24 / 365), years) == -1)
2017 return got_error_from_errno("asprintf");
2018 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2019 if (asprintf(repo_age, "%lld %s",
2020 (diff_time / 60 / 60 / 24 / (365 / 12)),
2021 months) == -1)
2022 return got_error_from_errno("asprintf");
2023 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2024 if (asprintf(repo_age, "%lld %s",
2025 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2026 return got_error_from_errno("asprintf");
2027 } else if (diff_time > 60 * 60 * 24 * 2) {
2028 if (asprintf(repo_age, "%lld %s",
2029 (diff_time / 60 / 60 / 24), days) == -1)
2030 return got_error_from_errno("asprintf");
2031 } else if (diff_time > 60 * 60 * 2) {
2032 if (asprintf(repo_age, "%lld %s",
2033 (diff_time / 60 / 60), hours) == -1)
2034 return got_error_from_errno("asprintf");
2035 } else if (diff_time > 60 * 2) {
2036 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2037 minutes) == -1)
2038 return got_error_from_errno("asprintf");
2039 } else if (diff_time > 2) {
2040 if (asprintf(repo_age, "%lld %s", diff_time,
2041 seconds) == -1)
2042 return got_error_from_errno("asprintf");
2043 } else {
2044 if (asprintf(repo_age, "%s", now) == -1)
2045 return got_error_from_errno("asprintf");
2047 break;
2048 case TM_LONG:
2049 if (gmtime_r(&committer_time, &tm) == NULL)
2050 return got_error_from_errno("gmtime_r");
2052 s = asctime_r(&tm, datebuf);
2053 if (s == NULL)
2054 return got_error_from_errno("asctime_r");
2056 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2057 return got_error_from_errno("asprintf");
2058 break;
2060 return NULL;