show hint about update -b if the user attempts to rebase a branch onto itself
[got-portable.git] / gotwebd / gotweb.c
blob97a2e9974c1d7e7456cdf2bb68005d357ff9045d
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>
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.
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.
20 #include "got_compat.h"
22 #include <net/if.h>
23 #include <netinet/in.h>
24 #include <sys/queue.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <fcntl.h>
33 #include <imsg.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 "gotwebd.h"
52 #include "tmpl.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 },
64 static const struct action_keys action_keys[] = {
65 { "blame", BLAME },
66 { "blob", BLOB },
67 { "blobraw", BLOBRAW },
68 { "briefs", BRIEFS },
69 { "commits", COMMITS },
70 { "diff", DIFF },
71 { "error", ERR },
72 { "index", INDEX },
73 { "patch", PATCH },
74 { "summary", SUMMARY },
75 { "tag", TAG },
76 { "tags", TAGS },
77 { "tree", TREE },
78 { "rss", RSS },
81 static const struct got_error *gotweb_init_querystring(struct querystring **);
82 static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 char *);
84 static const struct got_error *gotweb_assign_querystring(struct querystring **,
85 char *, char *);
86 static int gotweb_render_index(struct template *);
87 static const struct got_error *gotweb_load_got_path(struct repo_dir **,
88 const char *, struct request *);
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);
94 static void gotweb_free_querystring(struct querystring *);
95 static void gotweb_free_repo_dir(struct repo_dir *);
97 struct server *gotweb_get_server(const char *);
99 static int
100 gotweb_reply(struct request *c, int status, const char *ctype,
101 struct gotweb_url *location)
103 const char *csp;
105 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
106 return -1;
108 if (location) {
109 if (tp_writes(c->tp, "Location: ") == -1 ||
110 gotweb_render_url(c, location) == -1 ||
111 tp_writes(c->tp, "\r\n") == -1)
112 return -1;
115 csp = "Content-Security-Policy: default-src 'self'; "
116 "script-src 'none'; object-src 'none';\r\n";
117 if (tp_writes(c->tp, csp) == -1)
118 return -1;
120 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
121 return -1;
123 return tp_writes(c->tp, "\r\n");
126 static int
127 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
128 const char *suffix)
130 int r;
132 r = tp_writef(c->tp, "Content-Disposition: attachment; "
133 "filename=%s%s\r\n", file, suffix ? suffix : "");
134 if (r == -1)
135 return -1;
136 return gotweb_reply(c, 200, ctype, NULL);
139 void
140 gotweb_process_request(struct request *c)
142 const struct got_error *error = NULL;
143 struct server *srv = NULL;
144 struct querystring *qs = NULL;
145 struct repo_dir *repo_dir = NULL;
146 const char *rss_ctype = "application/rss+xml;charset=utf-8";
147 const uint8_t *buf;
148 size_t len;
149 int r, binary = 0;
151 /* init the transport */
152 error = gotweb_init_transport(&c->t);
153 if (error) {
154 log_warnx("%s: %s", __func__, error->msg);
155 return;
157 /* don't process any further if client disconnected */
158 if (c->sock->client_status == CLIENT_DISCONNECT)
159 return;
160 /* get the gotwebd server */
161 srv = gotweb_get_server(c->server_name);
162 if (srv == NULL) {
163 log_warnx("%s: error server is NULL", __func__);
164 goto err;
166 c->srv = srv;
167 /* parse our querystring */
168 error = gotweb_init_querystring(&qs);
169 if (error) {
170 log_warnx("%s: %s", __func__, error->msg);
171 goto err;
173 c->t->qs = qs;
174 error = gotweb_parse_querystring(&qs, c->querystring);
175 if (error) {
176 log_warnx("%s: %s", __func__, error->msg);
177 goto err;
181 * certain actions require a commit id in the querystring. this stops
182 * bad actors from exploiting this by manually manipulating the
183 * querystring.
186 if (qs->action == BLAME || qs->action == BLOB ||
187 qs->action == BLOBRAW || qs->action == DIFF ||
188 qs->action == PATCH) {
189 if (qs->commit == NULL) {
190 error = got_error(GOT_ERR_BAD_QUERYSTRING);
191 goto err;
195 if (qs->action != INDEX) {
196 error = gotweb_load_got_path(&repo_dir, qs->path, c);
197 c->t->repo_dir = repo_dir;
198 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
199 goto err;
202 if (qs->action == BLOBRAW || qs->action == BLOB) {
203 if (qs->folder == NULL || qs->file == NULL) {
204 error = got_error(GOT_ERR_BAD_QUERYSTRING);
205 goto err;
208 error = got_get_repo_commits(c, 1);
209 if (error)
210 goto err;
212 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
213 &binary, c, qs->folder, qs->file, qs->commit);
214 if (error)
215 goto err;
218 switch (qs->action) {
219 case BLAME:
220 if (qs->folder == NULL || qs->file == NULL) {
221 error = got_error(GOT_ERR_BAD_QUERYSTRING);
222 goto err;
224 error = got_get_repo_commits(c, 1);
225 if (error) {
226 log_warnx("%s: %s", __func__, error->msg);
227 goto err;
229 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
230 return;
231 gotweb_render_page(c->tp, gotweb_render_blame);
232 return;
233 case BLOB:
234 if (binary) {
235 struct gotweb_url url = {
236 .index_page = -1,
237 .action = BLOBRAW,
238 .path = qs->path,
239 .commit = qs->commit,
240 .folder = qs->folder,
241 .file = qs->file,
244 gotweb_reply(c, 302, NULL, &url);
245 return;
248 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
249 return;
250 gotweb_render_page(c->tp, gotweb_render_blob);
251 return;
252 case BLOBRAW:
253 if (binary)
254 r = gotweb_reply_file(c, "application/octet-stream",
255 qs->file, NULL);
256 else
257 r = gotweb_reply(c, 200, "text/plain", NULL);
258 if (r == -1)
259 return;
260 if (template_flush(c->tp) == -1)
261 return;
263 for (;;) {
264 error = got_object_blob_read_block(&len, c->t->blob);
265 if (error)
266 break;
267 if (len == 0)
268 break;
269 buf = got_object_blob_get_read_buf(c->t->blob);
270 if (fcgi_write(c, buf, len) == -1)
271 break;
273 return;
274 case BRIEFS:
275 error = got_get_repo_commits(c, srv->max_commits_display);
276 if (error)
277 goto err;
278 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
279 return;
280 gotweb_render_page(c->tp, gotweb_render_briefs);
281 return;
282 case COMMITS:
283 error = got_get_repo_commits(c, srv->max_commits_display);
284 if (error) {
285 log_warnx("%s: %s", __func__, error->msg);
286 goto err;
288 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
289 return;
290 gotweb_render_page(c->tp, gotweb_render_commits);
291 return;
292 case DIFF:
293 error = got_get_repo_commits(c, 1);
294 if (error) {
295 log_warnx("%s: %s", __func__, error->msg);
296 goto err;
298 error = got_open_diff_for_output(&c->t->fp, c);
299 if (error) {
300 log_warnx("%s: %s", __func__, error->msg);
301 goto err;
303 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
304 return;
305 gotweb_render_page(c->tp, gotweb_render_diff);
306 return;
307 case INDEX:
308 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
309 alphasort);
310 if (c->t->nrepos == -1) {
311 c->t->repos = NULL;
312 error = got_error_from_errno2("scandir",
313 srv->repos_path);
314 goto err;
316 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
317 return;
318 gotweb_render_page(c->tp, gotweb_render_index);
319 return;
320 case PATCH:
321 error = got_get_repo_commits(c, 1);
322 if (error) {
323 log_warnx("%s: %s", __func__, error->msg);
324 goto err;
326 error = got_open_diff_for_output(&c->t->fp, c);
327 if (error) {
328 log_warnx("%s: %s", __func__, error->msg);
329 goto err;
331 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
332 return;
333 gotweb_render_patch(c->tp);
334 return;
335 case RSS:
336 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
337 if (error)
338 goto err;
339 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
340 == -1)
341 return;
342 gotweb_render_rss(c->tp);
343 return;
344 case SUMMARY:
345 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
346 got_ref_cmp_by_name, NULL);
347 if (error) {
348 log_warnx("%s: got_ref_list: %s", __func__,
349 error->msg);
350 goto err;
352 error = got_get_repo_commits(c, srv->summary_commits_display);
353 if (error)
354 goto err;
355 qs->action = TAGS;
356 error = got_get_repo_tags(c, srv->summary_tags_display);
357 if (error) {
358 log_warnx("%s: got_get_repo_tags: %s", __func__,
359 error->msg);
360 goto err;
362 qs->action = SUMMARY;
363 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
364 return;
365 gotweb_render_page(c->tp, gotweb_render_summary);
366 return;
367 case TAG:
368 error = got_get_repo_tags(c, 1);
369 if (error) {
370 log_warnx("%s: %s", __func__, error->msg);
371 goto err;
373 if (c->t->tag_count == 0) {
374 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
375 "bad commit id");
376 goto err;
378 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
379 return;
380 gotweb_render_page(c->tp, gotweb_render_tag);
381 return;
382 case TAGS:
383 error = got_get_repo_tags(c, srv->max_commits_display);
384 if (error) {
385 log_warnx("%s: %s", __func__, error->msg);
386 goto err;
388 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
389 return;
390 gotweb_render_page(c->tp, gotweb_render_tags);
391 return;
392 case TREE:
393 error = got_get_repo_commits(c, 1);
394 if (error) {
395 log_warnx("%s: %s", __func__, error->msg);
396 goto err;
398 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
399 return;
400 gotweb_render_page(c->tp, gotweb_render_tree);
401 return;
402 case ERR:
403 default:
404 error = got_error(GOT_ERR_BAD_QUERYSTRING);
407 err:
408 c->t->error = error;
409 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
410 return;
411 gotweb_render_page(c->tp, gotweb_render_error);
414 struct server *
415 gotweb_get_server(const char *server_name)
417 struct server *srv;
419 /* check against the server name first */
420 if (*server_name != '\0')
421 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
422 if (strcmp(srv->name, server_name) == 0)
423 return srv;
425 /* otherwise, use the first server */
426 return TAILQ_FIRST(&gotwebd_env->servers);
429 const struct got_error *
430 gotweb_init_transport(struct transport **t)
432 const struct got_error *error = NULL;
434 *t = calloc(1, sizeof(**t));
435 if (*t == NULL)
436 return got_error_from_errno2(__func__, "calloc");
438 TAILQ_INIT(&(*t)->repo_commits);
439 TAILQ_INIT(&(*t)->repo_tags);
440 TAILQ_INIT(&(*t)->refs);
442 (*t)->fd = -1;
444 return error;
447 static const struct got_error *
448 gotweb_init_querystring(struct querystring **qs)
450 const struct got_error *error = NULL;
452 *qs = calloc(1, sizeof(**qs));
453 if (*qs == NULL)
454 return got_error_from_errno2(__func__, "calloc");
456 (*qs)->headref = strdup("HEAD");
457 if ((*qs)->headref == NULL) {
458 free(*qs);
459 *qs = NULL;
460 return got_error_from_errno2(__func__, "strdup");
463 (*qs)->action = INDEX;
465 return error;
468 static const struct got_error *
469 gotweb_parse_querystring(struct querystring **qs, char *qst)
471 const struct got_error *error = NULL;
472 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
473 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
475 if (qst == NULL)
476 return error;
478 tok1 = strdup(qst);
479 if (tok1 == NULL)
480 return got_error_from_errno2(__func__, "strdup");
482 tok1_pair = tok1;
483 tok1_end = tok1;
485 while (tok1_pair != NULL) {
486 strsep(&tok1_end, "&");
488 tok2 = strdup(tok1_pair);
489 if (tok2 == NULL) {
490 free(tok1);
491 return got_error_from_errno2(__func__, "strdup");
494 tok2_pair = tok2;
495 tok2_end = tok2;
497 while (tok2_pair != NULL) {
498 strsep(&tok2_end, "=");
499 if (tok2_end) {
500 error = gotweb_assign_querystring(qs, tok2_pair,
501 tok2_end);
502 if (error)
503 goto err;
505 tok2_pair = tok2_end;
507 free(tok2);
508 tok1_pair = tok1_end;
510 free(tok1);
511 return error;
512 err:
513 free(tok2);
514 free(tok1);
515 return error;
519 * Adapted from usr.sbin/httpd/httpd.c url_decode.
521 static const struct got_error *
522 gotweb_urldecode(char *url)
524 char *p, *q;
525 char hex[3];
526 unsigned long x;
528 hex[2] = '\0';
529 p = q = url;
531 while (*p != '\0') {
532 switch (*p) {
533 case '%':
534 /* Encoding character is followed by two hex chars */
535 if (!isxdigit((unsigned char)p[1]) ||
536 !isxdigit((unsigned char)p[2]) ||
537 (p[1] == '0' && p[2] == '0'))
538 return got_error(GOT_ERR_BAD_QUERYSTRING);
540 hex[0] = p[1];
541 hex[1] = p[2];
544 * We don't have to validate "hex" because it is
545 * guaranteed to include two hex chars followed by nul.
547 x = strtoul(hex, NULL, 16);
548 *q = (char)x;
549 p += 2;
550 break;
551 default:
552 *q = *p;
553 break;
555 p++;
556 q++;
558 *q = '\0';
560 return NULL;
563 static const struct got_error *
564 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
566 const struct got_error *error = NULL;
567 const char *errstr;
568 int a_cnt, el_cnt;
570 error = gotweb_urldecode(value);
571 if (error)
572 return error;
574 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
575 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
576 continue;
578 switch (querystring_keys[el_cnt].element) {
579 case ACTION:
580 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
581 if (strcmp(value, action_keys[a_cnt].name) != 0)
582 continue;
583 else if (strcmp(value,
584 action_keys[a_cnt].name) == 0){
585 (*qs)->action =
586 action_keys[a_cnt].action;
587 goto qa_found;
590 (*qs)->action = ERR;
591 qa_found:
592 break;
593 case COMMIT:
594 (*qs)->commit = strdup(value);
595 if ((*qs)->commit == NULL) {
596 error = got_error_from_errno2(__func__,
597 "strdup");
598 goto done;
600 break;
601 case RFILE:
602 (*qs)->file = strdup(value);
603 if ((*qs)->file == NULL) {
604 error = got_error_from_errno2(__func__,
605 "strdup");
606 goto done;
608 break;
609 case FOLDER:
610 (*qs)->folder = strdup(value);
611 if ((*qs)->folder == NULL) {
612 error = got_error_from_errno2(__func__,
613 "strdup");
614 goto done;
616 break;
617 case HEADREF:
618 free((*qs)->headref);
619 (*qs)->headref = strdup(value);
620 if ((*qs)->headref == NULL) {
621 error = got_error_from_errno2(__func__,
622 "strdup");
623 goto done;
625 break;
626 case INDEX_PAGE:
627 if (*value == '\0')
628 break;
629 (*qs)->index_page = strtonum(value, INT64_MIN,
630 INT64_MAX, &errstr);
631 if (errstr) {
632 error = got_error_from_errno3(__func__,
633 "strtonum", errstr);
634 goto done;
636 if ((*qs)->index_page < 0)
637 (*qs)->index_page = 0;
638 break;
639 case PATH:
640 (*qs)->path = strdup(value);
641 if ((*qs)->path == NULL) {
642 error = got_error_from_errno2(__func__,
643 "strdup");
644 goto done;
646 break;
649 /* entry found */
650 break;
652 done:
653 return error;
656 void
657 gotweb_free_repo_tag(struct repo_tag *rt)
659 if (rt != NULL) {
660 free(rt->commit_id);
661 free(rt->tag_name);
662 free(rt->tag_commit);
663 free(rt->commit_msg);
664 free(rt->tagger);
666 free(rt);
669 void
670 gotweb_free_repo_commit(struct repo_commit *rc)
672 if (rc != NULL) {
673 free(rc->path);
674 free(rc->refs_str);
675 free(rc->commit_id);
676 free(rc->parent_id);
677 free(rc->tree_id);
678 free(rc->author);
679 free(rc->committer);
680 free(rc->commit_msg);
682 free(rc);
685 static void
686 gotweb_free_querystring(struct querystring *qs)
688 if (qs != NULL) {
689 free(qs->commit);
690 free(qs->file);
691 free(qs->folder);
692 free(qs->headref);
693 free(qs->path);
695 free(qs);
698 static void
699 gotweb_free_repo_dir(struct repo_dir *repo_dir)
701 if (repo_dir != NULL) {
702 free(repo_dir->name);
703 free(repo_dir->owner);
704 free(repo_dir->description);
705 free(repo_dir->url);
706 free(repo_dir->path);
708 free(repo_dir);
711 void
712 gotweb_free_transport(struct transport *t)
714 const struct got_error *err;
715 struct repo_commit *rc = NULL, *trc = NULL;
716 struct repo_tag *rt = NULL, *trt = NULL;
717 int i;
719 got_ref_list_free(&t->refs);
720 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
721 TAILQ_REMOVE(&t->repo_commits, rc, entry);
722 gotweb_free_repo_commit(rc);
724 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
725 TAILQ_REMOVE(&t->repo_tags, rt, entry);
726 gotweb_free_repo_tag(rt);
728 gotweb_free_repo_dir(t->repo_dir);
729 gotweb_free_querystring(t->qs);
730 free(t->more_id);
731 free(t->tags_more_id);
732 if (t->blob)
733 got_object_blob_close(t->blob);
734 if (t->fp) {
735 err = got_gotweb_closefile(t->fp);
736 if (err)
737 log_warnx("%s: got_gotweb_closefile failure: %s",
738 __func__, err->msg);
740 if (t->fd != -1 && close(t->fd) == -1)
741 log_warn("%s: close", __func__);
742 if (t->repos) {
743 for (i = 0; i < t->nrepos; ++i)
744 free(t->repos[i]);
745 free(t->repos);
747 if (t->repo)
748 got_repo_close(t->repo);
749 free(t);
752 void
753 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
754 struct gotweb_url *next, int *have_next)
756 struct transport *t = c->t;
757 struct querystring *qs = t->qs;
758 struct server *srv = c->srv;
760 *have_prev = *have_next = 0;
762 if (qs->index_page > 0) {
763 *have_prev = 1;
764 *prev = (struct gotweb_url){
765 .action = -1,
766 .index_page = qs->index_page - 1,
769 if (t->next_disp == srv->max_repos_display &&
770 t->repos_total != (qs->index_page + 1) *
771 srv->max_repos_display) {
772 *have_next = 1;
773 *next = (struct gotweb_url){
774 .action = -1,
775 .index_page = qs->index_page + 1,
780 static int
781 gotweb_render_index(struct template *tp)
783 const struct got_error *error = NULL;
784 struct request *c = tp->tp_arg;
785 struct server *srv = c->srv;
786 struct transport *t = c->t;
787 struct querystring *qs = t->qs;
788 struct repo_dir *repo_dir = NULL;
789 struct dirent **sd_dent = t->repos;
790 unsigned int d_i, d_disp = 0;
791 unsigned int d_skipped = 0;
792 int type, r;
794 if (gotweb_render_repo_table_hdr(c->tp) == -1)
795 return -1;
797 for (d_i = 0; d_i < t->nrepos; d_i++) {
798 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
799 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
800 d_skipped++;
801 continue;
804 error = got_path_dirent_type(&type, srv->repos_path,
805 sd_dent[d_i]);
806 if (error)
807 continue;
808 if (type != DT_DIR) {
809 d_skipped++;
810 continue;
813 if (qs->index_page > 0 && (qs->index_page *
814 srv->max_repos_display) > t->prev_disp) {
815 t->prev_disp++;
816 continue;
819 error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
821 if (error && error->code != GOT_ERR_LONELY_PACKIDX) {
822 if (error->code != GOT_ERR_NOT_GIT_REPO)
823 log_warnx("%s: %s: %s", __func__,
824 sd_dent[d_i]->d_name, error->msg);
825 gotweb_free_repo_dir(repo_dir);
826 repo_dir = NULL;
827 d_skipped++;
828 continue;
831 d_disp++;
832 t->prev_disp++;
834 r = gotweb_render_repo_fragment(c->tp, repo_dir);
835 gotweb_free_repo_dir(repo_dir);
836 repo_dir = NULL;
837 got_repo_close(t->repo);
838 t->repo = NULL;
839 if (r == -1)
840 return -1;
842 t->next_disp++;
843 if (d_disp == srv->max_repos_display)
844 break;
846 t->repos_total = t->nrepos - d_skipped;
848 if (srv->max_repos_display == 0 ||
849 t->repos_total <= srv->max_repos_display)
850 return 0;
852 if (gotweb_render_navs(c->tp) == -1)
853 return -1;
855 return 0;
858 static inline int
859 should_urlencode(int c)
861 if (c <= ' ' || c >= 127)
862 return 1;
864 switch (c) {
865 /* gen-delim */
866 case ':':
867 case '/':
868 case '?':
869 case '#':
870 case '[':
871 case ']':
872 case '@':
873 /* sub-delims */
874 case '!':
875 case '$':
876 case '&':
877 case '\'':
878 case '(':
879 case ')':
880 case '*':
881 case '+':
882 case ',':
883 case ';':
884 case '=':
885 /* needed because the URLs are embedded into the HTML */
886 case '\"':
887 return 1;
888 default:
889 return 0;
893 static char *
894 gotweb_urlencode(const char *str)
896 const char *s;
897 char *escaped;
898 size_t i, len;
899 int a, b;
901 len = 0;
902 for (s = str; *s; ++s) {
903 len++;
904 if (should_urlencode(*s))
905 len += 2;
908 escaped = calloc(1, len + 1);
909 if (escaped == NULL)
910 return NULL;
912 i = 0;
913 for (s = str; *s; ++s) {
914 if (should_urlencode(*s)) {
915 a = (*s & 0xF0) >> 4;
916 b = (*s & 0x0F);
918 escaped[i++] = '%';
919 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
920 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
921 } else
922 escaped[i++] = *s;
925 return escaped;
928 const char *
929 gotweb_action_name(int action)
931 switch (action) {
932 case BLAME:
933 return "blame";
934 case BLOB:
935 return "blob";
936 case BLOBRAW:
937 return "blobraw";
938 case BRIEFS:
939 return "briefs";
940 case COMMITS:
941 return "commits";
942 case DIFF:
943 return "diff";
944 case ERR:
945 return "err";
946 case INDEX:
947 return "index";
948 case PATCH:
949 return "patch";
950 case SUMMARY:
951 return "summary";
952 case TAG:
953 return "tag";
954 case TAGS:
955 return "tags";
956 case TREE:
957 return "tree";
958 case RSS:
959 return "rss";
960 default:
961 return NULL;
966 gotweb_render_url(struct request *c, struct gotweb_url *url)
968 const char *sep = "?", *action;
969 char *tmp;
970 int r;
972 action = gotweb_action_name(url->action);
973 if (action != NULL) {
974 if (tp_writef(c->tp, "?action=%s", action) == -1)
975 return -1;
976 sep = "&";
979 if (url->commit) {
980 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
981 return -1;
982 sep = "&";
985 if (url->previd) {
986 if (tp_writef(c->tp, "%sprevid=%s", sep, url->previd) == -1)
987 return -1;
988 sep = "&";
991 if (url->prevset) {
992 if (tp_writef(c->tp, "%sprevset=%s", sep, url->prevset) == -1)
993 return -1;
994 sep = "&";
997 if (url->file) {
998 tmp = gotweb_urlencode(url->file);
999 if (tmp == NULL)
1000 return -1;
1001 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1002 free(tmp);
1003 if (r == -1)
1004 return -1;
1005 sep = "&";
1008 if (url->folder) {
1009 tmp = gotweb_urlencode(url->folder);
1010 if (tmp == NULL)
1011 return -1;
1012 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1013 free(tmp);
1014 if (r == -1)
1015 return -1;
1016 sep = "&";
1019 if (url->headref) {
1020 tmp = gotweb_urlencode(url->headref);
1021 if (tmp == NULL)
1022 return -1;
1023 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1024 free(tmp);
1025 if (r == -1)
1026 return -1;
1027 sep = "&";
1030 if (url->index_page != -1) {
1031 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1032 url->index_page) == -1)
1033 return -1;
1034 sep = "&";
1037 if (url->path) {
1038 tmp = gotweb_urlencode(url->path);
1039 if (tmp == NULL)
1040 return -1;
1041 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1042 free(tmp);
1043 if (r == -1)
1044 return -1;
1045 sep = "&";
1048 return 0;
1052 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1054 struct template *tp = c->tp;
1055 const char *proto = c->https ? "https" : "http";
1057 if (tp_writes(tp, proto) == -1 ||
1058 tp_writes(tp, "://") == -1 ||
1059 tp_htmlescape(tp, c->server_name) == -1 ||
1060 tp_htmlescape(tp, c->document_uri) == -1)
1061 return -1;
1063 return gotweb_render_url(c, url);
1066 static const struct got_error *
1067 gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1068 struct request *c)
1070 const struct got_error *error = NULL;
1071 struct socket *sock = c->sock;
1072 struct server *srv = c->srv;
1073 struct transport *t = c->t;
1074 struct repo_dir *repo_dir;
1075 DIR *dt;
1076 char *dir_test;
1078 *rp = calloc(1, sizeof(**rp));
1079 if (*rp == NULL)
1080 return got_error_from_errno("calloc");
1081 repo_dir = *rp;
1083 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
1084 GOTWEB_GIT_DIR) == -1)
1085 return got_error_from_errno("asprintf");
1087 dt = opendir(dir_test);
1088 if (dt == NULL) {
1089 free(dir_test);
1090 if (asprintf(&dir_test, "%s/%s", srv->repos_path, dir) == -1)
1091 return got_error_from_errno("asprintf");
1092 dt = opendir(dir_test);
1093 if (dt == NULL) {
1094 free(dir_test);
1095 if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1096 dir, GOTWEB_GIT_DIR) == -1)
1097 return got_error_from_errno("asprintf");
1098 dt = opendir(dir_test);
1099 if (dt == NULL) {
1100 free(dir_test);
1101 return got_error_path(dir,
1102 GOT_ERR_NOT_GIT_REPO);
1107 repo_dir->path = dir_test;
1108 dir_test = NULL;
1110 repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1111 if (repo_dir->name == NULL) {
1112 error = got_error_from_errno("strdup");
1113 goto err;
1116 if (srv->respect_exportok &&
1117 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1118 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1119 goto err;
1122 error = got_repo_open(&t->repo, repo_dir->path, NULL, sock->pack_fds);
1123 if (error)
1124 goto err;
1125 error = gotweb_get_repo_description(&repo_dir->description, srv,
1126 repo_dir->path, dirfd(dt));
1127 if (error)
1128 goto err;
1129 error = got_get_repo_owner(&repo_dir->owner, c);
1130 if (error)
1131 goto err;
1132 if (srv->show_repo_age) {
1133 error = got_get_repo_age(&repo_dir->age, c, NULL);
1134 if (error)
1135 goto err;
1137 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1138 dirfd(dt));
1139 err:
1140 free(dir_test);
1141 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1142 error = got_error_from_errno("closedir");
1143 if (error && t->repo) {
1144 got_repo_close(t->repo);
1145 t->repo = NULL;
1147 return error;
1150 static const struct got_error *
1151 gotweb_get_repo_description(char **description, struct server *srv,
1152 const char *dirpath, int dir)
1154 const struct got_error *error = NULL;
1155 struct stat sb;
1156 int fd = -1;
1157 off_t len;
1159 *description = NULL;
1160 if (srv->show_repo_description == 0)
1161 return NULL;
1163 fd = openat(dir, "description", O_RDONLY);
1164 if (fd == -1) {
1165 if (errno != ENOENT && errno != EACCES) {
1166 error = got_error_from_errno_fmt("openat %s/%s",
1167 dirpath, "description");
1169 goto done;
1172 if (fstat(fd, &sb) == -1) {
1173 error = got_error_from_errno_fmt("fstat %s/%s",
1174 dirpath, "description");
1175 goto done;
1178 len = sb.st_size;
1179 if (len > GOTWEBD_MAXDESCRSZ - 1)
1180 len = GOTWEBD_MAXDESCRSZ - 1;
1182 *description = calloc(len + 1, sizeof(**description));
1183 if (*description == NULL) {
1184 error = got_error_from_errno("calloc");
1185 goto done;
1188 if (read(fd, *description, len) == -1)
1189 error = got_error_from_errno("read");
1190 done:
1191 if (fd != -1 && close(fd) == -1 && error == NULL)
1192 error = got_error_from_errno("close");
1193 return error;
1196 static const struct got_error *
1197 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1198 int dir)
1200 const struct got_error *error = NULL;
1201 struct stat sb;
1202 int fd = -1;
1203 off_t len;
1205 *url = NULL;
1206 if (srv->show_repo_cloneurl == 0)
1207 return NULL;
1209 fd = openat(dir, "cloneurl", O_RDONLY);
1210 if (fd == -1) {
1211 if (errno != ENOENT && errno != EACCES) {
1212 error = got_error_from_errno_fmt("openat %s/%s",
1213 dirpath, "cloneurl");
1215 goto done;
1218 if (fstat(fd, &sb) == -1) {
1219 error = got_error_from_errno_fmt("fstat %s/%s",
1220 dirpath, "cloneurl");
1221 goto done;
1224 len = sb.st_size;
1225 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1226 len = GOTWEBD_MAXCLONEURLSZ - 1;
1228 *url = calloc(len + 1, sizeof(**url));
1229 if (*url == NULL) {
1230 error = got_error_from_errno("calloc");
1231 goto done;
1234 if (read(fd, *url, len) == -1)
1235 error = got_error_from_errno("read");
1236 done:
1237 if (fd != -1 && close(fd) == -1 && error == NULL)
1238 error = got_error_from_errno("close");
1239 return error;
1243 gotweb_render_age(struct template *tp, time_t committer_time)
1245 struct request *c = tp->tp_arg;
1246 long long diff_time;
1247 const char *years = "years ago", *months = "months ago";
1248 const char *weeks = "weeks ago", *days = "days ago";
1249 const char *hours = "hours ago", *minutes = "minutes ago";
1250 const char *seconds = "seconds ago", *now = "right now";
1252 diff_time = time(NULL) - committer_time;
1253 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1254 if (tp_writef(c->tp, "%lld %s",
1255 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1256 return -1;
1257 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1258 if (tp_writef(c->tp, "%lld %s",
1259 (diff_time / 60 / 60 / 24 / (365 / 12)),
1260 months) == -1)
1261 return -1;
1262 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1263 if (tp_writef(c->tp, "%lld %s",
1264 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1265 return -1;
1266 } else if (diff_time > 60 * 60 * 24 * 2) {
1267 if (tp_writef(c->tp, "%lld %s",
1268 (diff_time / 60 / 60 / 24), days) == -1)
1269 return -1;
1270 } else if (diff_time > 60 * 60 * 2) {
1271 if (tp_writef(c->tp, "%lld %s",
1272 (diff_time / 60 / 60), hours) == -1)
1273 return -1;
1274 } else if (diff_time > 60 * 2) {
1275 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1276 minutes) == -1)
1277 return -1;
1278 } else if (diff_time > 2) {
1279 if (tp_writef(c->tp, "%lld %s", diff_time,
1280 seconds) == -1)
1281 return -1;
1282 } else {
1283 if (tp_writes(tp, now) == -1)
1284 return -1;
1286 return 0;