Blob


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