remove full content comparison loop from got status
[got-portable.git] / gotwebd / gotweb.c
blob07b919d56bc153c9b9b88f815b1c1f155a14d638
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>
38 #include <vis.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "gotwebd.h"
53 #include "log.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 },
66 static const struct action_keys action_keys[] = {
67 { "blame", BLAME },
68 { "blob", BLOB },
69 { "blobraw", BLOBRAW },
70 { "briefs", BRIEFS },
71 { "commits", COMMITS },
72 { "diff", DIFF },
73 { "error", ERR },
74 { "index", INDEX },
75 { "patch", PATCH },
76 { "summary", SUMMARY },
77 { "tag", TAG },
78 { "tags", TAGS },
79 { "tree", TREE },
80 { "rss", RSS },
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 int gotweb_render_index(struct template *);
89 static const struct got_error *gotweb_load_got_path(struct repo_dir **,
90 const char *, struct request *);
91 static const struct got_error *gotweb_load_file(char **, const char *,
92 const char *, int);
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);
98 static void gotweb_free_querystring(struct querystring *);
99 static void gotweb_free_repo_dir(struct repo_dir *);
101 struct server *gotweb_get_server(const char *);
103 static int
104 gotweb_reply(struct request *c, int status, const char *ctype,
105 struct gotweb_url *location)
107 const char *csp;
109 if (status != 200 && tp_writef(c->tp, "Status: %d\r\n", status) == -1)
110 return -1;
112 if (location) {
113 if (tp_writes(c->tp, "Location: ") == -1 ||
114 gotweb_render_url(c, location) == -1 ||
115 tp_writes(c->tp, "\r\n") == -1)
116 return -1;
119 csp = "Content-Security-Policy: default-src 'self'; "
120 "script-src 'none'; object-src 'none';\r\n";
121 if (tp_writes(c->tp, csp) == -1)
122 return -1;
124 if (ctype && tp_writef(c->tp, "Content-Type: %s\r\n", ctype) == -1)
125 return -1;
127 return tp_writes(c->tp, "\r\n");
130 static int
131 gotweb_reply_file(struct request *c, const char *ctype, const char *file,
132 const char *suffix)
134 int r;
136 r = tp_writef(c->tp, "Content-Disposition: attachment; "
137 "filename=%s%s\r\n", file, suffix ? suffix : "");
138 if (r == -1)
139 return -1;
140 return gotweb_reply(c, 200, ctype, NULL);
143 void
144 gotweb_process_request(struct request *c)
146 const struct got_error *error = NULL;
147 struct server *srv = NULL;
148 struct querystring *qs = NULL;
149 struct repo_dir *repo_dir = NULL;
150 struct repo_commit *commit;
151 const char *rss_ctype = "application/rss+xml;charset=utf-8";
152 const uint8_t *buf;
153 size_t len;
154 int r, binary = 0;
156 /* init the transport */
157 error = gotweb_init_transport(&c->t);
158 if (error) {
159 log_warnx("%s: %s", __func__, error->msg);
160 return;
162 /* get the gotwebd server */
163 srv = gotweb_get_server(c->server_name);
164 if (srv == NULL) {
165 log_warnx("%s: error server is NULL", __func__);
166 goto err;
168 c->srv = srv;
169 /* parse our querystring */
170 error = gotweb_init_querystring(&qs);
171 if (error) {
172 log_warnx("%s: %s", __func__, error->msg);
173 goto err;
175 c->t->qs = qs;
176 error = gotweb_parse_querystring(qs, c->querystring);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
182 /* Log the request. */
183 if (gotwebd_env->gotwebd_verbose > 0) {
184 char *server_name = NULL;
185 char *querystring = NULL;
186 char *document_uri = NULL;
188 if (c->server_name[0] &&
189 stravis(&server_name, c->server_name, VIS_SAFE) == -1) {
190 log_warn("stravis");
191 server_name = NULL;
193 if (c->querystring[0] &&
194 stravis(&querystring, c->querystring, VIS_SAFE) == -1) {
195 log_warn("stravis");
196 querystring = NULL;
198 if (c->document_uri[0] &&
199 stravis(&document_uri, c->document_uri, VIS_SAFE) == -1) {
200 log_warn("stravis");
201 document_uri = NULL;
204 log_info("processing request: server='%s' query='%s' "
205 "document_uri='%s'",
206 server_name ? server_name : "",
207 querystring ? querystring : "",
208 document_uri ? document_uri : "");
209 free(server_name);
210 free(querystring);
211 free(document_uri);
215 * certain actions require a commit id in the querystring. this stops
216 * bad actors from exploiting this by manually manipulating the
217 * querystring.
220 if (qs->action == BLAME || qs->action == BLOB ||
221 qs->action == BLOBRAW || qs->action == DIFF ||
222 qs->action == PATCH) {
223 if (qs->commit == NULL) {
224 error = got_error(GOT_ERR_BAD_QUERYSTRING);
225 goto err;
229 if (qs->action != INDEX) {
230 if (qs->path == NULL) {
231 error = got_error(GOT_ERR_BAD_QUERYSTRING);
232 goto err;
235 error = gotweb_load_got_path(&repo_dir, qs->path, c);
236 c->t->repo_dir = repo_dir;
237 if (error)
238 goto err;
241 if (qs->action == BLOBRAW || qs->action == BLOB) {
242 if (qs->folder == NULL || qs->file == NULL) {
243 error = got_error(GOT_ERR_BAD_QUERYSTRING);
244 goto err;
247 error = got_get_repo_commits(c, 1);
248 if (error)
249 goto err;
251 error = got_open_blob_for_output(&c->t->blob, &c->t->fd,
252 &binary, c, qs->folder, qs->file, qs->commit);
253 if (error)
254 goto err;
257 switch (qs->action) {
258 case BLAME:
259 if (qs->folder == NULL || qs->file == NULL) {
260 error = got_error(GOT_ERR_BAD_QUERYSTRING);
261 goto err;
263 error = got_get_repo_commits(c, 1);
264 if (error) {
265 log_warnx("%s: %s", __func__, error->msg);
266 goto err;
268 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
269 return;
270 gotweb_render_page(c->tp, gotweb_render_blame);
271 return;
272 case BLOB:
273 if (binary) {
274 struct gotweb_url url = {
275 .index_page = -1,
276 .action = BLOBRAW,
277 .path = qs->path,
278 .commit = qs->commit,
279 .folder = qs->folder,
280 .file = qs->file,
283 gotweb_reply(c, 302, NULL, &url);
284 return;
287 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
288 return;
289 gotweb_render_page(c->tp, gotweb_render_blob);
290 return;
291 case BLOBRAW:
292 if (binary)
293 r = gotweb_reply_file(c, "application/octet-stream",
294 qs->file, NULL);
295 else
296 r = gotweb_reply(c, 200, "text/plain", NULL);
297 if (r == -1)
298 return;
299 if (template_flush(c->tp) == -1)
300 return;
302 for (;;) {
303 error = got_object_blob_read_block(&len, c->t->blob);
304 if (error)
305 break;
306 if (len == 0)
307 break;
308 buf = got_object_blob_get_read_buf(c->t->blob);
309 if (fcgi_write(c, buf, len) == -1)
310 break;
312 return;
313 case BRIEFS:
314 error = got_get_repo_commits(c, srv->max_commits_display);
315 if (error)
316 goto err;
317 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
318 return;
319 gotweb_render_page(c->tp, gotweb_render_briefs);
320 return;
321 case COMMITS:
322 error = got_get_repo_commits(c, srv->max_commits_display);
323 if (error) {
324 log_warnx("%s: %s", __func__, error->msg);
325 goto err;
327 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
328 return;
329 gotweb_render_page(c->tp, gotweb_render_commits);
330 return;
331 case DIFF:
332 error = got_get_repo_commits(c, 1);
333 if (error) {
334 log_warnx("%s: %s", __func__, error->msg);
335 goto err;
337 error = got_open_diff_for_output(&c->t->fp, c);
338 if (error) {
339 log_warnx("%s: %s", __func__, error->msg);
340 goto err;
342 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
343 return;
344 gotweb_render_page(c->tp, gotweb_render_diff);
345 return;
346 case INDEX:
347 c->t->nrepos = scandir(srv->repos_path, &c->t->repos, NULL,
348 alphasort);
349 if (c->t->nrepos == -1) {
350 c->t->repos = NULL;
351 error = got_error_from_errno2("scandir",
352 srv->repos_path);
353 goto err;
355 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
356 return;
357 gotweb_render_page(c->tp, gotweb_render_index);
358 return;
359 case PATCH:
360 error = got_get_repo_commits(c, 1);
361 if (error) {
362 log_warnx("%s: %s", __func__, error->msg);
363 goto err;
365 error = got_open_diff_for_output(&c->t->fp, c);
366 if (error) {
367 log_warnx("%s: %s", __func__, error->msg);
368 goto err;
370 if (gotweb_reply(c, 200, "text/plain", NULL) == -1)
371 return;
372 gotweb_render_patch(c->tp);
373 return;
374 case RSS:
375 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
376 if (error)
377 goto err;
378 if (gotweb_reply_file(c, rss_ctype, repo_dir->name, ".rss")
379 == -1)
380 return;
381 gotweb_render_rss(c->tp);
382 return;
383 case SUMMARY:
384 error = got_ref_list(&c->t->refs, c->t->repo, "refs/heads",
385 got_ref_cmp_by_name, NULL);
386 if (error) {
387 log_warnx("%s: got_ref_list: %s", __func__,
388 error->msg);
389 goto err;
391 error = got_get_repo_commits(c, srv->summary_commits_display);
392 if (error)
393 goto err;
394 qs->action = TAGS;
395 error = got_get_repo_tags(c, srv->summary_tags_display);
396 if (error) {
397 log_warnx("%s: got_get_repo_tags: %s", __func__,
398 error->msg);
399 goto err;
401 qs->action = SUMMARY;
402 commit = TAILQ_FIRST(&c->t->repo_commits);
403 if (commit && qs->commit == NULL) {
404 qs->commit = strdup(commit->commit_id);
405 if (qs->commit == NULL) {
406 error = got_error_from_errno("strdup");
407 log_warn("%s: strdup", __func__);
408 goto err;
411 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
412 return;
413 gotweb_render_page(c->tp, gotweb_render_summary);
414 return;
415 case TAG:
416 error = got_get_repo_tags(c, 1);
417 if (error) {
418 log_warnx("%s: %s", __func__, error->msg);
419 goto err;
421 if (TAILQ_EMPTY(&c->t->repo_tags)) {
422 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
423 "bad commit id");
424 goto err;
426 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
427 return;
428 gotweb_render_page(c->tp, gotweb_render_tag);
429 return;
430 case TAGS:
431 error = got_get_repo_tags(c, srv->max_commits_display);
432 if (error) {
433 log_warnx("%s: %s", __func__, error->msg);
434 goto err;
436 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
437 return;
438 gotweb_render_page(c->tp, gotweb_render_tags);
439 return;
440 case TREE:
441 error = got_get_repo_commits(c, 1);
442 if (error) {
443 log_warnx("%s: %s", __func__, error->msg);
444 goto err;
446 if (gotweb_reply(c, 200, "text/html", NULL) == -1)
447 return;
448 gotweb_render_page(c->tp, gotweb_render_tree);
449 return;
450 case ERR:
451 default:
452 error = got_error(GOT_ERR_BAD_QUERYSTRING);
455 err:
456 c->t->error = error;
457 if (gotweb_reply(c, 400, "text/html", NULL) == -1)
458 return;
459 gotweb_render_page(c->tp, gotweb_render_error);
462 struct server *
463 gotweb_get_server(const char *server_name)
465 struct server *srv;
467 /* check against the server name first */
468 if (*server_name != '\0')
469 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
470 if (strcmp(srv->name, server_name) == 0)
471 return srv;
473 /* otherwise, use the first server */
474 return TAILQ_FIRST(&gotwebd_env->servers);
477 const struct got_error *
478 gotweb_init_transport(struct transport **t)
480 const struct got_error *error = NULL;
482 *t = calloc(1, sizeof(**t));
483 if (*t == NULL)
484 return got_error_from_errno2(__func__, "calloc");
486 TAILQ_INIT(&(*t)->repo_commits);
487 TAILQ_INIT(&(*t)->repo_tags);
488 TAILQ_INIT(&(*t)->refs);
490 (*t)->fd = -1;
492 return error;
495 static const struct got_error *
496 gotweb_init_querystring(struct querystring **qs)
498 const struct got_error *error = NULL;
500 *qs = calloc(1, sizeof(**qs));
501 if (*qs == NULL)
502 return got_error_from_errno2(__func__, "calloc");
504 (*qs)->headref = strdup("HEAD");
505 if ((*qs)->headref == NULL) {
506 free(*qs);
507 *qs = NULL;
508 return got_error_from_errno2(__func__, "strdup");
511 (*qs)->action = INDEX;
513 return error;
516 static const struct got_error *
517 gotweb_parse_querystring(struct querystring *qs, char *qst)
519 const struct got_error *error = NULL;
520 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
521 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
523 if (qst == NULL)
524 return error;
526 tok1 = strdup(qst);
527 if (tok1 == NULL)
528 return got_error_from_errno2(__func__, "strdup");
530 tok1_pair = tok1;
531 tok1_end = tok1;
533 while (tok1_pair != NULL) {
534 strsep(&tok1_end, "&");
536 tok2 = strdup(tok1_pair);
537 if (tok2 == NULL) {
538 free(tok1);
539 return got_error_from_errno2(__func__, "strdup");
542 tok2_pair = tok2;
543 tok2_end = tok2;
545 while (tok2_pair != NULL) {
546 strsep(&tok2_end, "=");
547 if (tok2_end) {
548 error = gotweb_assign_querystring(qs, tok2_pair,
549 tok2_end);
550 if (error)
551 goto err;
553 tok2_pair = tok2_end;
555 free(tok2);
556 tok1_pair = tok1_end;
558 free(tok1);
559 return error;
560 err:
561 free(tok2);
562 free(tok1);
563 return error;
567 * Adapted from usr.sbin/httpd/httpd.c url_decode.
569 static const struct got_error *
570 gotweb_urldecode(char *url)
572 char *p, *q;
573 char hex[3];
574 unsigned long x;
576 hex[2] = '\0';
577 p = q = url;
579 while (*p != '\0') {
580 switch (*p) {
581 case '%':
582 /* Encoding character is followed by two hex chars */
583 if (!isxdigit((unsigned char)p[1]) ||
584 !isxdigit((unsigned char)p[2]) ||
585 (p[1] == '0' && p[2] == '0'))
586 return got_error(GOT_ERR_BAD_QUERYSTRING);
588 hex[0] = p[1];
589 hex[1] = p[2];
592 * We don't have to validate "hex" because it is
593 * guaranteed to include two hex chars followed by nul.
595 x = strtoul(hex, NULL, 16);
596 *q = (char)x;
597 p += 2;
598 break;
599 default:
600 *q = *p;
601 break;
603 p++;
604 q++;
606 *q = '\0';
608 return NULL;
611 static const struct got_error *
612 gotweb_assign_querystring(struct querystring *qs, char *key, char *value)
614 const struct got_error *error = NULL;
615 const char *errstr;
616 int a_cnt, el_cnt;
618 error = gotweb_urldecode(value);
619 if (error)
620 return error;
622 for (el_cnt = 0; el_cnt < nitems(querystring_keys); el_cnt++) {
623 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
624 continue;
626 switch (querystring_keys[el_cnt].element) {
627 case ACTION:
628 for (a_cnt = 0; a_cnt < nitems(action_keys); a_cnt++) {
629 if (strcmp(value, action_keys[a_cnt].name) != 0)
630 continue;
631 qs->action = action_keys[a_cnt].action;
632 goto qa_found;
634 qs->action = ERR;
635 qa_found:
636 break;
637 case COMMIT:
638 qs->commit = strdup(value);
639 if (qs->commit == NULL) {
640 error = got_error_from_errno2(__func__,
641 "strdup");
642 goto done;
644 break;
645 case RFILE:
646 qs->file = strdup(value);
647 if (qs->file == NULL) {
648 error = got_error_from_errno2(__func__,
649 "strdup");
650 goto done;
652 break;
653 case FOLDER:
654 qs->folder = strdup(value);
655 if (qs->folder == NULL) {
656 error = got_error_from_errno2(__func__,
657 "strdup");
658 goto done;
660 break;
661 case HEADREF:
662 free(qs->headref);
663 qs->headref = strdup(value);
664 if (qs->headref == NULL) {
665 error = got_error_from_errno2(__func__,
666 "strdup");
667 goto done;
669 break;
670 case INDEX_PAGE:
671 if (*value == '\0')
672 break;
673 qs->index_page = strtonum(value, INT64_MIN,
674 INT64_MAX, &errstr);
675 if (errstr) {
676 error = got_error_from_errno3(__func__,
677 "strtonum", errstr);
678 goto done;
680 if (qs->index_page < 0)
681 qs->index_page = 0;
682 break;
683 case PATH:
684 qs->path = strdup(value);
685 if (qs->path == NULL) {
686 error = got_error_from_errno2(__func__,
687 "strdup");
688 goto done;
690 break;
693 /* entry found */
694 break;
696 done:
697 return error;
700 void
701 gotweb_free_repo_tag(struct repo_tag *rt)
703 if (rt != NULL) {
704 free(rt->commit_id);
705 free(rt->tag_name);
706 free(rt->tag_commit);
707 free(rt->commit_msg);
708 free(rt->tagger);
710 free(rt);
713 void
714 gotweb_free_repo_commit(struct repo_commit *rc)
716 if (rc != NULL) {
717 free(rc->path);
718 free(rc->refs_str);
719 free(rc->commit_id);
720 free(rc->parent_id);
721 free(rc->tree_id);
722 free(rc->author);
723 free(rc->committer);
724 free(rc->commit_msg);
726 free(rc);
729 static void
730 gotweb_free_querystring(struct querystring *qs)
732 if (qs != NULL) {
733 free(qs->commit);
734 free(qs->file);
735 free(qs->folder);
736 free(qs->headref);
737 free(qs->path);
739 free(qs);
742 static void
743 gotweb_free_repo_dir(struct repo_dir *repo_dir)
745 if (repo_dir != NULL) {
746 free(repo_dir->name);
747 free(repo_dir->owner);
748 free(repo_dir->description);
749 free(repo_dir->url);
750 free(repo_dir->path);
752 free(repo_dir);
755 void
756 gotweb_free_transport(struct transport *t)
758 const struct got_error *err;
759 struct repo_commit *rc = NULL, *trc = NULL;
760 struct repo_tag *rt = NULL, *trt = NULL;
761 int i;
763 got_ref_list_free(&t->refs);
764 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
765 TAILQ_REMOVE(&t->repo_commits, rc, entry);
766 gotweb_free_repo_commit(rc);
768 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
769 TAILQ_REMOVE(&t->repo_tags, rt, entry);
770 gotweb_free_repo_tag(rt);
772 gotweb_free_repo_dir(t->repo_dir);
773 gotweb_free_querystring(t->qs);
774 free(t->more_id);
775 free(t->tags_more_id);
776 if (t->blob)
777 got_object_blob_close(t->blob);
778 if (t->fp) {
779 err = got_gotweb_closefile(t->fp);
780 if (err)
781 log_warnx("%s: got_gotweb_closefile failure: %s",
782 __func__, err->msg);
784 if (t->fd != -1 && close(t->fd) == -1)
785 log_warn("%s: close", __func__);
786 if (t->repos) {
787 for (i = 0; i < t->nrepos; ++i)
788 free(t->repos[i]);
789 free(t->repos);
791 if (t->repo)
792 got_repo_close(t->repo);
793 free(t);
796 void
797 gotweb_index_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
798 struct gotweb_url *next, int *have_next)
800 struct transport *t = c->t;
801 struct querystring *qs = t->qs;
802 struct server *srv = c->srv;
804 *have_prev = *have_next = 0;
806 if (qs->index_page > 0) {
807 *have_prev = 1;
808 *prev = (struct gotweb_url){
809 .action = -1,
810 .index_page = qs->index_page - 1,
813 if (t->next_disp == srv->max_repos_display &&
814 t->repos_total != (qs->index_page + 1) *
815 srv->max_repos_display) {
816 *have_next = 1;
817 *next = (struct gotweb_url){
818 .action = -1,
819 .index_page = qs->index_page + 1,
824 static int
825 gotweb_render_index(struct template *tp)
827 const struct got_error *error = NULL;
828 struct request *c = tp->tp_arg;
829 struct server *srv = c->srv;
830 struct transport *t = c->t;
831 struct querystring *qs = t->qs;
832 struct repo_dir *repo_dir = NULL;
833 struct dirent **sd_dent = t->repos;
834 unsigned int d_i, d_disp = 0;
835 unsigned int d_skipped = 0;
836 int type, r;
838 if (gotweb_render_repo_table_hdr(c->tp) == -1)
839 return -1;
841 for (d_i = 0; d_i < t->nrepos; d_i++) {
842 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
843 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
844 d_skipped++;
845 continue;
848 error = got_path_dirent_type(&type, srv->repos_path,
849 sd_dent[d_i]);
850 if (error)
851 continue;
852 if (type != DT_DIR) {
853 d_skipped++;
854 continue;
857 if (qs->index_page > 0 && (qs->index_page *
858 srv->max_repos_display) > t->prev_disp) {
859 t->prev_disp++;
860 continue;
863 error = gotweb_load_got_path(&repo_dir, sd_dent[d_i]->d_name,
865 if (error) {
866 if (error->code != GOT_ERR_NOT_GIT_REPO)
867 log_warnx("%s: %s: %s", __func__,
868 sd_dent[d_i]->d_name, error->msg);
869 gotweb_free_repo_dir(repo_dir);
870 repo_dir = NULL;
871 d_skipped++;
872 continue;
875 d_disp++;
876 t->prev_disp++;
878 r = gotweb_render_repo_fragment(c->tp, repo_dir);
879 gotweb_free_repo_dir(repo_dir);
880 repo_dir = NULL;
881 got_repo_close(t->repo);
882 t->repo = NULL;
883 if (r == -1)
884 return -1;
886 t->next_disp++;
887 if (d_disp == srv->max_repos_display)
888 break;
890 t->repos_total = t->nrepos - d_skipped;
892 if (srv->max_repos_display == 0 ||
893 t->repos_total <= srv->max_repos_display)
894 return 0;
896 if (gotweb_render_navs(c->tp) == -1)
897 return -1;
899 return 0;
902 static inline int
903 should_urlencode(int c)
905 if (c <= ' ' || c >= 127)
906 return 1;
908 switch (c) {
909 /* gen-delim */
910 case ':':
911 case '/':
912 case '?':
913 case '#':
914 case '[':
915 case ']':
916 case '@':
917 /* sub-delims */
918 case '!':
919 case '$':
920 case '&':
921 case '\'':
922 case '(':
923 case ')':
924 case '*':
925 case '+':
926 case ',':
927 case ';':
928 case '=':
929 /* needed because the URLs are embedded into the HTML */
930 case '\"':
931 return 1;
932 default:
933 return 0;
937 static char *
938 gotweb_urlencode(const char *str)
940 const char *s;
941 char *escaped;
942 size_t i, len;
943 int a, b;
945 len = 0;
946 for (s = str; *s; ++s) {
947 len++;
948 if (should_urlencode(*s))
949 len += 2;
952 escaped = calloc(1, len + 1);
953 if (escaped == NULL)
954 return NULL;
956 i = 0;
957 for (s = str; *s; ++s) {
958 if (should_urlencode(*s)) {
959 a = (*s & 0xF0) >> 4;
960 b = (*s & 0x0F);
962 escaped[i++] = '%';
963 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
964 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
965 } else
966 escaped[i++] = *s;
969 return escaped;
972 const char *
973 gotweb_action_name(int action)
975 switch (action) {
976 case BLAME:
977 return "blame";
978 case BLOB:
979 return "blob";
980 case BLOBRAW:
981 return "blobraw";
982 case BRIEFS:
983 return "briefs";
984 case COMMITS:
985 return "commits";
986 case DIFF:
987 return "diff";
988 case ERR:
989 return "err";
990 case INDEX:
991 return "index";
992 case PATCH:
993 return "patch";
994 case SUMMARY:
995 return "summary";
996 case TAG:
997 return "tag";
998 case TAGS:
999 return "tags";
1000 case TREE:
1001 return "tree";
1002 case RSS:
1003 return "rss";
1004 default:
1005 return NULL;
1010 gotweb_render_url(struct request *c, struct gotweb_url *url)
1012 const char *sep = "?", *action;
1013 char *tmp;
1014 int r;
1016 action = gotweb_action_name(url->action);
1017 if (action != NULL) {
1018 if (tp_writef(c->tp, "?action=%s", action) == -1)
1019 return -1;
1020 sep = "&";
1023 if (url->commit) {
1024 if (tp_writef(c->tp, "%scommit=%s", sep, url->commit) == -1)
1025 return -1;
1026 sep = "&";
1029 if (url->file) {
1030 tmp = gotweb_urlencode(url->file);
1031 if (tmp == NULL)
1032 return -1;
1033 r = tp_writef(c->tp, "%sfile=%s", sep, tmp);
1034 free(tmp);
1035 if (r == -1)
1036 return -1;
1037 sep = "&";
1040 if (url->folder) {
1041 tmp = gotweb_urlencode(url->folder);
1042 if (tmp == NULL)
1043 return -1;
1044 r = tp_writef(c->tp, "%sfolder=%s", sep, tmp);
1045 free(tmp);
1046 if (r == -1)
1047 return -1;
1048 sep = "&";
1051 if (url->headref) {
1052 tmp = gotweb_urlencode(url->headref);
1053 if (tmp == NULL)
1054 return -1;
1055 r = tp_writef(c->tp, "%sheadref=%s", sep, url->headref);
1056 free(tmp);
1057 if (r == -1)
1058 return -1;
1059 sep = "&";
1062 if (url->index_page != -1) {
1063 if (tp_writef(c->tp, "%sindex_page=%d", sep,
1064 url->index_page) == -1)
1065 return -1;
1066 sep = "&";
1069 if (url->path) {
1070 tmp = gotweb_urlencode(url->path);
1071 if (tmp == NULL)
1072 return -1;
1073 r = tp_writef(c->tp, "%spath=%s", sep, tmp);
1074 free(tmp);
1075 if (r == -1)
1076 return -1;
1077 sep = "&";
1080 return 0;
1084 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1086 struct template *tp = c->tp;
1087 const char *proto = c->https ? "https" : "http";
1089 if (tp_writes(tp, proto) == -1 ||
1090 tp_writes(tp, "://") == -1 ||
1091 tp_htmlescape(tp, c->server_name) == -1 ||
1092 tp_htmlescape(tp, c->document_uri) == -1)
1093 return -1;
1095 return gotweb_render_url(c, url);
1098 static const struct got_error *
1099 gotweb_load_got_path(struct repo_dir **rp, const char *dir,
1100 struct request *c)
1102 const struct got_error *error = NULL;
1103 struct server *srv = c->srv;
1104 struct transport *t = c->t;
1105 struct repo_dir *repo_dir;
1106 DIR *dt;
1107 char *dir_test;
1109 *rp = calloc(1, sizeof(**rp));
1110 if (*rp == NULL)
1111 return got_error_from_errno("calloc");
1112 repo_dir = *rp;
1114 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, dir,
1115 GOTWEB_GIT_DIR) == -1)
1116 return got_error_from_errno("asprintf");
1118 dt = opendir(dir_test);
1119 if (dt == NULL) {
1120 free(dir_test);
1121 if (asprintf(&dir_test, "%s/%s", srv->repos_path, dir) == -1)
1122 return got_error_from_errno("asprintf");
1123 dt = opendir(dir_test);
1124 if (dt == NULL) {
1125 free(dir_test);
1126 if (asprintf(&dir_test, "%s/%s%s", srv->repos_path,
1127 dir, GOTWEB_GIT_DIR) == -1)
1128 return got_error_from_errno("asprintf");
1129 dt = opendir(dir_test);
1130 if (dt == NULL) {
1131 free(dir_test);
1132 return got_error_path(dir,
1133 GOT_ERR_NOT_GIT_REPO);
1138 repo_dir->path = dir_test;
1139 dir_test = NULL;
1141 repo_dir->name = strdup(repo_dir->path + strlen(srv->repos_path) + 1);
1142 if (repo_dir->name == NULL) {
1143 error = got_error_from_errno("strdup");
1144 goto err;
1147 if (srv->respect_exportok &&
1148 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1149 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1150 goto err;
1153 error = got_repo_open(&t->repo, repo_dir->path, NULL,
1154 gotwebd_env->pack_fds);
1155 if (error)
1156 goto err;
1157 error = gotweb_get_repo_description(&repo_dir->description, srv,
1158 repo_dir->path, dirfd(dt));
1159 if (error)
1160 goto err;
1161 if (srv->show_repo_owner) {
1162 error = gotweb_load_file(&repo_dir->owner, repo_dir->path,
1163 "owner", dirfd(dt));
1164 if (error)
1165 goto err;
1166 if (repo_dir->owner == NULL) {
1167 error = got_get_repo_owner(&repo_dir->owner, c);
1168 if (error)
1169 goto err;
1172 if (srv->show_repo_age) {
1173 error = got_get_repo_age(&repo_dir->age, c, NULL);
1174 if (error)
1175 goto err;
1177 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1178 dirfd(dt));
1179 err:
1180 free(dir_test);
1181 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1182 error = got_error_from_errno("closedir");
1183 if (error && t->repo) {
1184 got_repo_close(t->repo);
1185 t->repo = NULL;
1187 return error;
1190 static const struct got_error *
1191 gotweb_load_file(char **str, const char *dir, const char *file, int dirfd)
1193 const struct got_error *error = NULL;
1194 struct stat sb;
1195 off_t len;
1196 int fd;
1198 *str = NULL;
1200 fd = openat(dirfd, file, O_RDONLY);
1201 if (fd == -1) {
1202 if (errno == ENOENT || errno == EACCES)
1203 return NULL;
1204 return got_error_from_errno_fmt("openat %s/%s", dir, file);
1207 if (fstat(fd, &sb) == -1) {
1208 error = got_error_from_errno_fmt("fstat %s/%s", dir, file);
1209 goto done;
1212 len = sb.st_size;
1213 if (len > GOTWEBD_MAXDESCRSZ - 1)
1214 len = GOTWEBD_MAXDESCRSZ - 1;
1216 *str = calloc(len + 1, 1);
1217 if (*str == NULL) {
1218 error = got_error_from_errno("calloc");
1219 goto done;
1222 if (read(fd, *str, len) == -1)
1223 error = got_error_from_errno("read");
1224 done:
1225 if (fd != -1 && close(fd) == -1 && error == NULL)
1226 error = got_error_from_errno("close");
1227 return error;
1230 static const struct got_error *
1231 gotweb_get_repo_description(char **description, struct server *srv,
1232 const char *dirpath, int dir)
1234 *description = NULL;
1235 if (srv->show_repo_description == 0)
1236 return NULL;
1238 return gotweb_load_file(description, dirpath, "description", dir);
1241 static const struct got_error *
1242 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1243 int dir)
1245 *url = NULL;
1246 if (srv->show_repo_cloneurl == 0)
1247 return NULL;
1249 return gotweb_load_file(url, dirpath, "cloneurl", dir);
1253 gotweb_render_age(struct template *tp, time_t committer_time)
1255 struct request *c = tp->tp_arg;
1256 long long diff_time;
1257 const char *years = "years ago", *months = "months ago";
1258 const char *weeks = "weeks ago", *days = "days ago";
1259 const char *hours = "hours ago", *minutes = "minutes ago";
1260 const char *seconds = "seconds ago", *now = "right now";
1262 diff_time = time(NULL) - committer_time;
1263 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1264 if (tp_writef(c->tp, "%lld %s",
1265 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1266 return -1;
1267 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1268 if (tp_writef(c->tp, "%lld %s",
1269 (diff_time / 60 / 60 / 24 / (365 / 12)),
1270 months) == -1)
1271 return -1;
1272 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1273 if (tp_writef(c->tp, "%lld %s",
1274 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1275 return -1;
1276 } else if (diff_time > 60 * 60 * 24 * 2) {
1277 if (tp_writef(c->tp, "%lld %s",
1278 (diff_time / 60 / 60 / 24), days) == -1)
1279 return -1;
1280 } else if (diff_time > 60 * 60 * 2) {
1281 if (tp_writef(c->tp, "%lld %s",
1282 (diff_time / 60 / 60), hours) == -1)
1283 return -1;
1284 } else if (diff_time > 60 * 2) {
1285 if (tp_writef(c->tp, "%lld %s", (diff_time / 60),
1286 minutes) == -1)
1287 return -1;
1288 } else if (diff_time > 2) {
1289 if (tp_writef(c->tp, "%lld %s", diff_time,
1290 seconds) == -1)
1291 return -1;
1292 } else {
1293 if (tp_writes(tp, now) == -1)
1294 return -1;
1296 return 0;