Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <sha1.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "proc.h"
52 #include "gotwebd.h"
53 #include "tmpl.h"
55 static const struct querystring_keys querystring_keys[] = {
56 { "action", ACTION },
57 { "commit", COMMIT },
58 { "file", RFILE },
59 { "folder", FOLDER },
60 { "headref", HEADREF },
61 { "index_page", INDEX_PAGE },
62 { "path", PATH },
63 { "page", PAGE },
64 };
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 { "summary", SUMMARY },
76 { "tag", TAG },
77 { "tags", TAGS },
78 { "tree", TREE },
79 { "rss", RSS },
80 };
82 static const struct got_error *gotweb_init_querystring(struct querystring **);
83 static const struct got_error *gotweb_parse_querystring(struct querystring **,
84 char *);
85 static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 char *, char *);
87 static const struct got_error *gotweb_render_index(struct request *);
88 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 const char *);
90 static const struct got_error *gotweb_load_got_path(struct request *c,
91 struct repo_dir *);
92 static const struct got_error *gotweb_get_repo_description(char **,
93 struct server *, const char *, int);
94 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 const char *, int);
96 static const struct got_error *gotweb_render_blame(struct request *);
97 static const struct got_error *gotweb_render_summary(struct request *);
98 static const struct got_error *gotweb_render_tags(struct request *);
99 static const struct got_error *gotweb_render_branches(struct request *);
101 static void gotweb_free_querystring(struct querystring *);
102 static void gotweb_free_repo_dir(struct repo_dir *);
104 struct server *gotweb_get_server(uint8_t *, uint8_t *);
106 void
107 gotweb_process_request(struct request *c)
109 const struct got_error *error = NULL, *error2 = NULL;
110 struct got_blob_object *blob = NULL;
111 struct server *srv = NULL;
112 struct querystring *qs = NULL;
113 struct repo_dir *repo_dir = NULL;
114 FILE *fp = NULL;
115 uint8_t err[] = "gotwebd experienced an error: ";
116 int r, html = 0, fd = -1;
118 /* init the transport */
119 error = gotweb_init_transport(&c->t);
120 if (error) {
121 log_warnx("%s: %s", __func__, error->msg);
122 return;
124 /* don't process any further if client disconnected */
125 if (c->sock->client_status == CLIENT_DISCONNECT)
126 return;
127 /* get the gotwebd server */
128 srv = gotweb_get_server(c->server_name, c->http_host);
129 if (srv == NULL) {
130 log_warnx("%s: error server is NULL", __func__);
131 goto err;
133 c->srv = srv;
134 /* parse our querystring */
135 error = gotweb_init_querystring(&qs);
136 if (error) {
137 log_warnx("%s: %s", __func__, error->msg);
138 goto err;
140 c->t->qs = qs;
141 error = gotweb_parse_querystring(&qs, c->querystring);
142 if (error) {
143 log_warnx("%s: %s", __func__, error->msg);
144 goto err;
147 /*
148 * certain actions require a commit id in the querystring. this stops
149 * bad actors from exploiting this by manually manipulating the
150 * querystring.
151 */
153 if (qs->action == BLAME || qs->action == BLOB ||
154 qs->action == BLOBRAW || qs->action == DIFF) {
155 if (qs->commit == NULL) {
156 error2 = got_error(GOT_ERR_QUERYSTRING);
157 goto render;
161 if (qs->action != INDEX) {
162 error = gotweb_init_repo_dir(&repo_dir, qs->path);
163 if (error)
164 goto done;
165 error = gotweb_load_got_path(c, repo_dir);
166 c->t->repo_dir = repo_dir;
167 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
168 goto err;
171 if (qs->action == BLOBRAW) {
172 error = got_get_repo_commits(c, 1);
173 if (error)
174 goto done;
175 error = got_output_file_blob(c);
176 if (error) {
177 log_warnx("%s: %s", __func__, error->msg);
178 goto err;
180 goto done;
183 if (qs->action == BLOB) {
184 int binary;
185 struct gotweb_url url = {
186 .index_page = -1,
187 .page = -1,
188 .action = BLOBRAW,
189 .path = qs->path,
190 .commit = qs->commit,
191 .folder = qs->folder,
192 .file = qs->file,
193 };
195 error = got_get_repo_commits(c, 1);
196 if (error)
197 goto done;
199 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
200 if (error2)
201 goto render;
202 if (binary) {
203 fcgi_puts(c->tp, "Status: 302\r\n");
204 fcgi_puts(c->tp, "Location: ");
205 gotweb_render_url(c, &url);
206 fcgi_puts(c->tp, "\r\n\r\n");
207 goto done;
211 if (qs->action == RSS) {
212 error = gotweb_render_content_type_file(c,
213 "application/rss+xml;charset=utf-8",
214 repo_dir->name, ".rss");
215 if (error) {
216 log_warnx("%s: %s", __func__, error->msg);
217 goto err;
220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
221 if (error) {
222 log_warnx("%s: %s", __func__, error->msg);
223 goto err;
225 if (gotweb_render_rss(c->tp) == -1)
226 goto err;
227 goto done;
230 render:
231 error = gotweb_render_content_type(c, "text/html");
232 if (error) {
233 log_warnx("%s: %s", __func__, error->msg);
234 goto err;
236 html = 1;
238 if (gotweb_render_header(c->tp) == -1)
239 goto err;
241 if (error2) {
242 error = error2;
243 goto err;
246 switch(qs->action) {
247 case BLAME:
248 error = gotweb_render_blame(c);
249 if (error) {
250 log_warnx("%s: %s", __func__, error->msg);
251 goto err;
253 break;
254 case BLOB:
255 if (gotweb_render_blob(c->tp, blob) == -1)
256 goto err;
257 break;
258 case BRIEFS:
259 if (gotweb_render_briefs(c->tp) == -1)
260 goto err;
261 break;
262 case COMMITS:
263 error = got_get_repo_commits(c, srv->max_commits_display);
264 if (error) {
265 log_warnx("%s: %s", __func__, error->msg);
266 goto err;
268 if (gotweb_render_commits(c->tp) == -1)
269 goto err;
270 break;
271 case DIFF:
272 error = got_get_repo_commits(c, 1);
273 if (error) {
274 log_warnx("%s: %s", __func__, error->msg);
275 goto err;
277 error = got_open_diff_for_output(&fp, &fd, c);
278 if (error) {
279 log_warnx("%s: %s", __func__, error->msg);
280 goto err;
282 if (gotweb_render_diff(c->tp, fp) == -1)
283 goto err;
284 break;
285 case INDEX:
286 error = gotweb_render_index(c);
287 if (error) {
288 log_warnx("%s: %s", __func__, error->msg);
289 goto err;
291 break;
292 case SUMMARY:
293 error = gotweb_render_summary(c);
294 if (error) {
295 log_warnx("%s: %s", __func__, error->msg);
296 goto err;
298 break;
299 case TAG:
300 error = got_get_repo_tags(c, 1);
301 if (error) {
302 log_warnx("%s: %s", __func__, error->msg);
303 goto err;
305 if (c->t->tag_count == 0) {
306 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
307 "bad commit id");
308 goto err;
310 if (gotweb_render_tag(c->tp) == -1)
311 goto done;
312 break;
313 case TAGS:
314 error = gotweb_render_tags(c);
315 if (error) {
316 log_warnx("%s: %s", __func__, error->msg);
317 goto err;
319 break;
320 case TREE:
321 error = got_get_repo_commits(c, 1);
322 if (error) {
323 log_warnx("%s: %s", __func__, error->msg);
324 goto err;
326 if (gotweb_render_tree(c->tp) == -1)
327 goto err;
328 break;
329 case ERR:
330 default:
331 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
332 "Erorr: Bad Querystring");
333 if (r == -1)
334 goto err;
335 break;
338 goto done;
339 err:
340 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
341 return;
342 if (fcgi_printf(c, "\n%s", err) == -1)
343 return;
344 if (error) {
345 if (fcgi_printf(c, "%s", error->msg) == -1)
346 return;
347 } else {
348 if (fcgi_printf(c, "see daemon logs for details") == -1)
349 return;
351 if (html && fcgi_printf(c, "</div>\n") == -1)
352 return;
353 done:
354 if (blob)
355 got_object_blob_close(blob);
356 if (fp) {
357 error = got_gotweb_flushfile(fp, fd);
358 if (error)
359 log_warnx("%s: got_gotweb_flushfile failure: %s",
360 __func__, error->msg);
361 fd = -1;
363 if (fd != -1)
364 close(fd);
365 if (html && srv != NULL)
366 gotweb_render_footer(c->tp);
369 struct server *
370 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
372 struct server *srv = NULL;
374 /* check against the server name first */
375 if (strlen(server_name) > 0)
376 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
377 if (strcmp(srv->name, server_name) == 0)
378 goto done;
380 /* check against subdomain second */
381 if (strlen(subdomain) > 0)
382 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
383 if (strcmp(srv->name, subdomain) == 0)
384 goto done;
386 /* if those fail, send first server */
387 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
388 if (srv != NULL)
389 break;
390 done:
391 return srv;
392 };
394 const struct got_error *
395 gotweb_init_transport(struct transport **t)
397 const struct got_error *error = NULL;
399 *t = calloc(1, sizeof(**t));
400 if (*t == NULL)
401 return got_error_from_errno2("%s: calloc", __func__);
403 TAILQ_INIT(&(*t)->repo_commits);
404 TAILQ_INIT(&(*t)->repo_tags);
406 (*t)->repo = NULL;
407 (*t)->repo_dir = NULL;
408 (*t)->qs = NULL;
409 (*t)->next_id = NULL;
410 (*t)->prev_id = NULL;
411 (*t)->next_disp = 0;
412 (*t)->prev_disp = 0;
414 return error;
417 static const struct got_error *
418 gotweb_init_querystring(struct querystring **qs)
420 const struct got_error *error = NULL;
422 *qs = calloc(1, sizeof(**qs));
423 if (*qs == NULL)
424 return got_error_from_errno2("%s: calloc", __func__);
426 (*qs)->headref = strdup("HEAD");
427 if ((*qs)->headref == NULL) {
428 free(*qs);
429 *qs = NULL;
430 return got_error_from_errno2("%s: strdup", __func__);
433 (*qs)->action = INDEX;
434 (*qs)->commit = NULL;
435 (*qs)->file = NULL;
436 (*qs)->folder = NULL;
437 (*qs)->index_page = 0;
438 (*qs)->path = NULL;
440 return error;
443 static const struct got_error *
444 gotweb_parse_querystring(struct querystring **qs, char *qst)
446 const struct got_error *error = NULL;
447 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
448 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
450 if (qst == NULL)
451 return error;
453 tok1 = strdup(qst);
454 if (tok1 == NULL)
455 return got_error_from_errno2("%s: strdup", __func__);
457 tok1_pair = tok1;
458 tok1_end = tok1;
460 while (tok1_pair != NULL) {
461 strsep(&tok1_end, "&");
463 tok2 = strdup(tok1_pair);
464 if (tok2 == NULL) {
465 free(tok1);
466 return got_error_from_errno2("%s: strdup", __func__);
469 tok2_pair = tok2;
470 tok2_end = tok2;
472 while (tok2_pair != NULL) {
473 strsep(&tok2_end, "=");
474 if (tok2_end) {
475 error = gotweb_assign_querystring(qs, tok2_pair,
476 tok2_end);
477 if (error)
478 goto err;
480 tok2_pair = tok2_end;
482 free(tok2);
483 tok1_pair = tok1_end;
485 free(tok1);
486 return error;
487 err:
488 free(tok2);
489 free(tok1);
490 return error;
493 /*
494 * Adapted from usr.sbin/httpd/httpd.c url_decode.
495 */
496 static const struct got_error *
497 gotweb_urldecode(char *url)
499 char *p, *q;
500 char hex[3];
501 unsigned long x;
503 hex[2] = '\0';
504 p = q = url;
506 while (*p != '\0') {
507 switch (*p) {
508 case '%':
509 /* Encoding character is followed by two hex chars */
510 if (!isxdigit((unsigned char)p[1]) ||
511 !isxdigit((unsigned char)p[2]) ||
512 (p[1] == '0' && p[2] == '0'))
513 return got_error(GOT_ERR_BAD_QUERYSTRING);
515 hex[0] = p[1];
516 hex[1] = p[2];
518 /*
519 * We don't have to validate "hex" because it is
520 * guaranteed to include two hex chars followed by nul.
521 */
522 x = strtoul(hex, NULL, 16);
523 *q = (char)x;
524 p += 2;
525 break;
526 default:
527 *q = *p;
528 break;
530 p++;
531 q++;
533 *q = '\0';
535 return NULL;
538 static const struct got_error *
539 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
541 const struct got_error *error = NULL;
542 const char *errstr;
543 int a_cnt, el_cnt;
545 error = gotweb_urldecode(value);
546 if (error)
547 return error;
549 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
550 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
551 continue;
553 switch (querystring_keys[el_cnt].element) {
554 case ACTION:
555 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
556 if (strcmp(value, action_keys[a_cnt].name) != 0)
557 continue;
558 else if (strcmp(value,
559 action_keys[a_cnt].name) == 0){
560 (*qs)->action =
561 action_keys[a_cnt].action;
562 goto qa_found;
565 (*qs)->action = ERR;
566 qa_found:
567 break;
568 case COMMIT:
569 (*qs)->commit = strdup(value);
570 if ((*qs)->commit == NULL) {
571 error = got_error_from_errno2("%s: strdup",
572 __func__);
573 goto done;
575 break;
576 case RFILE:
577 (*qs)->file = strdup(value);
578 if ((*qs)->file == NULL) {
579 error = got_error_from_errno2("%s: strdup",
580 __func__);
581 goto done;
583 break;
584 case FOLDER:
585 (*qs)->folder = strdup(value);
586 if ((*qs)->folder == NULL) {
587 error = got_error_from_errno2("%s: strdup",
588 __func__);
589 goto done;
591 break;
592 case HEADREF:
593 free((*qs)->headref);
594 (*qs)->headref = strdup(value);
595 if ((*qs)->headref == NULL) {
596 error = got_error_from_errno2("%s: strdup",
597 __func__);
598 goto done;
600 break;
601 case INDEX_PAGE:
602 if (strlen(value) == 0)
603 break;
604 (*qs)->index_page = strtonum(value, INT64_MIN,
605 INT64_MAX, &errstr);
606 if (errstr) {
607 error = got_error_from_errno3("%s: strtonum %s",
608 __func__, errstr);
609 goto done;
611 if ((*qs)->index_page < 0)
612 (*qs)->index_page = 0;
613 break;
614 case PATH:
615 (*qs)->path = strdup(value);
616 if ((*qs)->path == NULL) {
617 error = got_error_from_errno2("%s: strdup",
618 __func__);
619 goto done;
621 break;
622 case PAGE:
623 if (strlen(value) == 0)
624 break;
625 (*qs)->page = strtonum(value, INT64_MIN,
626 INT64_MAX, &errstr);
627 if (errstr) {
628 error = got_error_from_errno3("%s: strtonum %s",
629 __func__, errstr);
630 goto done;
632 if ((*qs)->page < 0)
633 (*qs)->page = 0;
634 break;
635 default:
636 break;
639 done:
640 return error;
643 void
644 gotweb_free_repo_tag(struct repo_tag *rt)
646 if (rt != NULL) {
647 free(rt->commit_id);
648 free(rt->tag_name);
649 free(rt->tag_commit);
650 free(rt->commit_msg);
651 free(rt->tagger);
653 free(rt);
656 void
657 gotweb_free_repo_commit(struct repo_commit *rc)
659 if (rc != NULL) {
660 free(rc->path);
661 free(rc->refs_str);
662 free(rc->commit_id);
663 free(rc->parent_id);
664 free(rc->tree_id);
665 free(rc->author);
666 free(rc->committer);
667 free(rc->commit_msg);
669 free(rc);
672 static void
673 gotweb_free_querystring(struct querystring *qs)
675 if (qs != NULL) {
676 free(qs->commit);
677 free(qs->file);
678 free(qs->folder);
679 free(qs->headref);
680 free(qs->path);
682 free(qs);
685 static void
686 gotweb_free_repo_dir(struct repo_dir *repo_dir)
688 if (repo_dir != NULL) {
689 free(repo_dir->name);
690 free(repo_dir->owner);
691 free(repo_dir->description);
692 free(repo_dir->url);
693 free(repo_dir->age);
694 free(repo_dir->path);
696 free(repo_dir);
699 void
700 gotweb_free_transport(struct transport *t)
702 struct repo_commit *rc = NULL, *trc = NULL;
703 struct repo_tag *rt = NULL, *trt = NULL;
705 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
706 TAILQ_REMOVE(&t->repo_commits, rc, entry);
707 gotweb_free_repo_commit(rc);
709 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
710 TAILQ_REMOVE(&t->repo_tags, rt, entry);
711 gotweb_free_repo_tag(rt);
713 gotweb_free_repo_dir(t->repo_dir);
714 gotweb_free_querystring(t->qs);
715 free(t->next_id);
716 free(t->prev_id);
717 free(t);
720 const struct got_error *
721 gotweb_render_content_type(struct request *c, const char *type)
723 const char *csp = "default-src 'self'; script-src 'none'; "
724 "object-src 'none';";
726 fcgi_printf(c,
727 "Content-Security-Policy: %s\r\n"
728 "Content-Type: %s\r\n\r\n",
729 csp, type);
730 return NULL;
733 const struct got_error *
734 gotweb_render_content_type_file(struct request *c, const char *type,
735 const char *file, const char *suffix)
737 fcgi_printf(c, "Content-type: %s\r\n"
738 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
739 type, file, suffix ? suffix : "");
740 return NULL;
743 void
744 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
745 struct gotweb_url *next, int *have_next)
747 struct transport *t = c->t;
748 struct querystring *qs = t->qs;
749 struct server *srv = c->srv;
751 *have_prev = *have_next = 0;
753 switch(qs->action) {
754 case INDEX:
755 if (qs->index_page > 0) {
756 *have_prev = 1;
757 *prev = (struct gotweb_url){
758 .action = -1,
759 .index_page = qs->index_page - 1,
760 .page = -1,
761 };
763 if (t->next_disp == srv->max_repos_display &&
764 t->repos_total != (qs->index_page + 1) *
765 srv->max_repos_display) {
766 *have_next = 1;
767 *next = (struct gotweb_url){
768 .action = -1,
769 .index_page = qs->index_page + 1,
770 .page = -1,
771 };
773 break;
774 case BRIEFS:
775 if (t->prev_id && qs->commit != NULL &&
776 strcmp(qs->commit, t->prev_id) != 0) {
777 *have_prev = 1;
778 *prev = (struct gotweb_url){
779 .action = BRIEFS,
780 .index_page = -1,
781 .page = qs->page - 1,
782 .path = qs->path,
783 .commit = t->prev_id,
784 .headref = qs->headref,
785 };
787 if (t->next_id) {
788 *have_next = 1;
789 *next = (struct gotweb_url){
790 .action = BRIEFS,
791 .index_page = -1,
792 .page = qs->page + 1,
793 .path = qs->path,
794 .commit = t->next_id,
795 .headref = qs->headref,
796 };
798 break;
799 case COMMITS:
800 if (t->prev_id && qs->commit != NULL &&
801 strcmp(qs->commit, t->prev_id) != 0) {
802 *have_prev = 1;
803 *prev = (struct gotweb_url){
804 .action = COMMITS,
805 .index_page = -1,
806 .page = qs->page - 1,
807 .path = qs->path,
808 .commit = t->prev_id,
809 .headref = qs->headref,
810 .folder = qs->folder,
811 .file = qs->file,
812 };
814 if (t->next_id) {
815 *have_next = 1;
816 *next = (struct gotweb_url){
817 .action = COMMITS,
818 .index_page = -1,
819 .page = qs->page + 1,
820 .path = qs->path,
821 .commit = t->next_id,
822 .headref = qs->headref,
823 .folder = qs->folder,
824 .file = qs->file,
825 };
827 break;
828 case TAGS:
829 if (t->prev_id && qs->commit != NULL &&
830 strcmp(qs->commit, t->prev_id) != 0) {
831 *have_prev = 1;
832 *prev = (struct gotweb_url){
833 .action = TAGS,
834 .index_page = -1,
835 .page = qs->page - 1,
836 .path = qs->path,
837 .commit = t->prev_id,
838 .headref = qs->headref,
839 };
841 if (t->next_id) {
842 *have_next = 1;
843 *next = (struct gotweb_url){
844 .action = TAGS,
845 .index_page = -1,
846 .page = qs->page + 1,
847 .path = qs->path,
848 .commit = t->next_id,
849 .headref = qs->headref,
850 };
852 break;
856 static const struct got_error *
857 gotweb_render_index(struct request *c)
859 const struct got_error *error = NULL;
860 struct server *srv = c->srv;
861 struct transport *t = c->t;
862 struct querystring *qs = t->qs;
863 struct repo_dir *repo_dir = NULL;
864 DIR *d;
865 struct dirent **sd_dent = NULL;
866 unsigned int d_cnt, d_i, d_disp = 0;
867 unsigned int d_skipped = 0;
868 int type;
870 d = opendir(srv->repos_path);
871 if (d == NULL) {
872 error = got_error_from_errno2("opendir", srv->repos_path);
873 return error;
876 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
877 if (d_cnt == -1) {
878 sd_dent = NULL;
879 error = got_error_from_errno2("scandir", srv->repos_path);
880 goto done;
883 if (gotweb_render_repo_table_hdr(c->tp) == -1)
884 goto done;
886 for (d_i = 0; d_i < d_cnt; d_i++) {
887 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
888 break;
890 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
891 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
892 d_skipped++;
893 continue;
896 error = got_path_dirent_type(&type, srv->repos_path,
897 sd_dent[d_i]);
898 if (error)
899 goto done;
900 if (type != DT_DIR) {
901 d_skipped++;
902 continue;
905 if (qs->index_page > 0 && (qs->index_page *
906 srv->max_repos_display) > t->prev_disp) {
907 t->prev_disp++;
908 continue;
911 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
912 if (error)
913 goto done;
915 error = gotweb_load_got_path(c, repo_dir);
916 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
917 error = NULL;
918 gotweb_free_repo_dir(repo_dir);
919 repo_dir = NULL;
920 d_skipped++;
921 continue;
923 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
924 goto done;
926 d_disp++;
927 t->prev_disp++;
929 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
930 goto done;
932 gotweb_free_repo_dir(repo_dir);
933 repo_dir = NULL;
934 t->next_disp++;
935 if (d_disp == srv->max_repos_display)
936 break;
938 t->repos_total = d_cnt - d_skipped;
940 if (srv->max_repos_display == 0)
941 goto done;
942 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
943 goto done;
944 if (t->repos_total <= srv->max_repos ||
945 t->repos_total <= srv->max_repos_display)
946 goto done;
948 if (gotweb_render_navs(c->tp) == -1)
949 goto done;
950 done:
951 if (sd_dent) {
952 for (d_i = 0; d_i < d_cnt; d_i++)
953 free(sd_dent[d_i]);
954 free(sd_dent);
956 if (d != NULL && closedir(d) == EOF && error == NULL)
957 error = got_error_from_errno("closedir");
958 return error;
961 static const struct got_error *
962 gotweb_render_blame(struct request *c)
964 const struct got_error *error = NULL;
965 struct transport *t = c->t;
966 struct repo_commit *rc = NULL;
967 char *age = NULL, *msg = NULL;
968 int r;
970 error = got_get_repo_commits(c, 1);
971 if (error)
972 return error;
974 rc = TAILQ_FIRST(&t->repo_commits);
976 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
977 if (error)
978 goto done;
979 error = gotweb_escape_html(&msg, rc->commit_msg);
980 if (error)
981 goto done;
983 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
984 "<div id='blame_title'>Blame</div>\n"
985 "</div>\n" /* #blame_title_wrapper */
986 "<div id='blame_content'>\n"
987 "<div id='blame_header_wrapper'>\n"
988 "<div id='blame_header'>\n"
989 "<div class='header_age_title'>Date:</div>\n"
990 "<div class='header_age'>%s</div>\n"
991 "<div id='header_commit_msg_title'>Message:</div>\n"
992 "<div id='header_commit_msg'>%s</div>\n"
993 "</div>\n" /* #blame_header */
994 "</div>\n" /* #blame_header_wrapper */
995 "<div class='dotted_line'></div>\n"
996 "<div id='blame'>\n",
997 age,
998 msg);
999 if (r == -1)
1000 goto done;
1002 error = got_output_file_blame(c);
1003 if (error)
1004 goto done;
1006 fcgi_printf(c, "</div>\n" /* #blame */
1007 "</div>\n"); /* #blame_content */
1008 done:
1009 free(age);
1010 free(msg);
1011 return error;
1014 static const struct got_error *
1015 gotweb_render_branches(struct request *c)
1017 const struct got_error *error = NULL;
1018 struct got_reflist_head refs;
1019 struct got_reflist_entry *re;
1020 struct transport *t = c->t;
1021 struct querystring *qs = t->qs;
1022 struct got_repository *repo = t->repo;
1023 char *escaped_refname = NULL;
1024 char *age = NULL;
1025 int r;
1027 TAILQ_INIT(&refs);
1029 error = got_ref_list(&refs, repo, "refs/heads",
1030 got_ref_cmp_by_name, NULL);
1031 if (error)
1032 goto done;
1034 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1035 "<div id='branches_title'>Branches</div>\n"
1036 "</div>\n" /* #branches_title_wrapper */
1037 "<div id='branches_content'>\n");
1038 if (r == -1)
1039 goto done;
1041 TAILQ_FOREACH(re, &refs, entry) {
1042 const char *refname = NULL;
1044 if (got_ref_is_symbolic(re->ref))
1045 continue;
1047 refname = got_ref_get_name(re->ref);
1048 if (refname == NULL) {
1049 error = got_error_from_errno("strdup");
1050 goto done;
1052 if (strncmp(refname, "refs/heads/", 11) != 0)
1053 continue;
1055 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1056 if (error)
1057 goto done;
1059 if (strncmp(refname, "refs/heads/", 11) == 0)
1060 refname += 11;
1061 error = gotweb_escape_html(&escaped_refname, refname);
1062 if (error)
1063 goto done;
1065 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1066 "<div class='branches_age'>%s</div>\n"
1067 "<div class='branches_space'>&nbsp;</div>\n"
1068 "<div class='branch'>", age);
1069 if (r == -1)
1070 goto done;
1072 r = gotweb_link(c, &(struct gotweb_url){
1073 .action = SUMMARY,
1074 .index_page = -1,
1075 .page = -1,
1076 .path = qs->path,
1077 .headref = refname,
1078 }, "%s", escaped_refname);
1079 if (r == -1)
1080 goto done;
1082 if (fcgi_printf(c, "</div>\n" /* .branch */
1083 "<div class='navs_wrapper'>\n"
1084 "<div class='navs'>") == -1)
1085 goto done;
1087 r = gotweb_link(c, &(struct gotweb_url){
1088 .action = SUMMARY,
1089 .index_page = -1,
1090 .page = -1,
1091 .path = qs->path,
1092 .headref = refname,
1093 }, "summary");
1094 if (r == -1)
1095 goto done;
1097 if (fcgi_printf(c, " | ") == -1)
1098 goto done;
1100 r = gotweb_link(c, &(struct gotweb_url){
1101 .action = BRIEFS,
1102 .index_page = -1,
1103 .page = -1,
1104 .path = qs->path,
1105 .headref = refname,
1106 }, "commit briefs");
1107 if (r == -1)
1108 goto done;
1110 if (fcgi_printf(c, " | ") == -1)
1111 goto done;
1113 r = gotweb_link(c, &(struct gotweb_url){
1114 .action = COMMITS,
1115 .index_page = -1,
1116 .page = -1,
1117 .path = qs->path,
1118 .headref = refname,
1119 }, "commits");
1120 if (r == -1)
1121 goto done;
1123 r = fcgi_printf(c, "</div>\n" /* .navs */
1124 "</div>\n" /* .navs_wrapper */
1125 "<div class='dotted_line'></div>\n"
1126 "</div>\n"); /* .branches_wrapper */
1127 if (r == -1)
1128 goto done;
1130 free(age);
1131 age = NULL;
1132 free(escaped_refname);
1133 escaped_refname = NULL;
1135 fcgi_printf(c, "</div>\n"); /* #branches_content */
1136 done:
1137 free(age);
1138 free(escaped_refname);
1139 got_ref_list_free(&refs);
1140 return error;
1143 static const struct got_error *
1144 gotweb_render_summary(struct request *c)
1146 const struct got_error *error = NULL;
1147 struct transport *t = c->t;
1148 struct server *srv = c->srv;
1149 int r;
1151 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1152 goto done;
1154 if (srv->show_repo_description) {
1155 r = fcgi_printf(c,
1156 "<div id='description_title'>Description:</div>\n"
1157 "<div id='description'>%s</div>\n",
1158 t->repo_dir->description ? t->repo_dir->description : "");
1159 if (r == -1)
1160 goto done;
1163 if (srv->show_repo_owner) {
1164 r = fcgi_printf(c,
1165 "<div id='repo_owner_title'>Owner:</div>\n"
1166 "<div id='repo_owner'>%s</div>\n",
1167 t->repo_dir->owner ? t->repo_dir->owner : "");
1168 if (r == -1)
1169 goto done;
1172 if (srv->show_repo_age) {
1173 r = fcgi_printf(c,
1174 "<div id='last_change_title'>Last Change:</div>\n"
1175 "<div id='last_change'>%s</div>\n",
1176 t->repo_dir->age);
1177 if (r == -1)
1178 goto done;
1181 if (srv->show_repo_cloneurl) {
1182 r = fcgi_printf(c,
1183 "<div id='cloneurl_title'>Clone URL:</div>\n"
1184 "<div id='cloneurl'>%s</div>\n",
1185 t->repo_dir->url ? t->repo_dir->url : "");
1186 if (r == -1)
1187 goto done;
1190 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1191 if (r == -1)
1192 goto done;
1194 if (gotweb_render_briefs(c->tp) == -1)
1195 goto done;
1197 error = gotweb_render_tags(c);
1198 if (error) {
1199 log_warnx("%s: %s", __func__, error->msg);
1200 goto done;
1203 error = gotweb_render_branches(c);
1204 if (error)
1205 log_warnx("%s: %s", __func__, error->msg);
1206 done:
1207 return error;
1210 static const struct got_error *
1211 gotweb_render_tags(struct request *c)
1213 const struct got_error *error = NULL;
1214 struct server *srv = c->srv;
1215 struct transport *t = c->t;
1216 struct querystring *qs = t->qs;
1218 if (qs->action == BRIEFS) {
1219 qs->action = TAGS;
1220 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1221 } else
1222 error = got_get_repo_tags(c, srv->max_commits_display);
1223 if (error)
1224 goto done;
1226 if (gotweb_render_tags_tmpl(c->tp) == -1)
1227 goto done;
1229 done:
1230 return error;
1233 const struct got_error *
1234 gotweb_escape_html(char **escaped_html, const char *orig_html)
1236 const struct got_error *error = NULL;
1237 struct escape_pair {
1238 char c;
1239 const char *s;
1240 } esc[] = {
1241 { '>', "&gt;" },
1242 { '<', "&lt;" },
1243 { '&', "&amp;" },
1244 { '"', "&quot;" },
1245 { '\'', "&apos;" },
1246 { '\n', "<br />" },
1248 size_t orig_len, len;
1249 int i, j, x;
1251 orig_len = strlen(orig_html);
1252 len = orig_len;
1253 for (i = 0; i < orig_len; i++) {
1254 for (j = 0; j < nitems(esc); j++) {
1255 if (orig_html[i] != esc[j].c)
1256 continue;
1257 len += strlen(esc[j].s) - 1 /* escaped char */;
1261 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1262 if (*escaped_html == NULL)
1263 return got_error_from_errno("calloc");
1265 x = 0;
1266 for (i = 0; i < orig_len; i++) {
1267 int escaped = 0;
1268 for (j = 0; j < nitems(esc); j++) {
1269 if (orig_html[i] != esc[j].c)
1270 continue;
1272 if (strlcat(*escaped_html, esc[j].s, len + 1)
1273 >= len + 1) {
1274 error = got_error(GOT_ERR_NO_SPACE);
1275 goto done;
1277 x += strlen(esc[j].s);
1278 escaped = 1;
1279 break;
1281 if (!escaped) {
1282 (*escaped_html)[x] = orig_html[i];
1283 x++;
1286 done:
1287 if (error) {
1288 free(*escaped_html);
1289 *escaped_html = NULL;
1290 } else {
1291 (*escaped_html)[x] = '\0';
1294 return error;
1297 static inline int
1298 should_urlencode(int c)
1300 if (c <= ' ' || c >= 127)
1301 return 1;
1303 switch (c) {
1304 /* gen-delim */
1305 case ':':
1306 case '/':
1307 case '?':
1308 case '#':
1309 case '[':
1310 case ']':
1311 case '@':
1312 /* sub-delims */
1313 case '!':
1314 case '$':
1315 case '&':
1316 case '\'':
1317 case '(':
1318 case ')':
1319 case '*':
1320 case '+':
1321 case ',':
1322 case ';':
1323 case '=':
1324 /* needed because the URLs are embedded into the HTML */
1325 case '\"':
1326 return 1;
1327 default:
1328 return 0;
1332 static char *
1333 gotweb_urlencode(const char *str)
1335 const char *s;
1336 char *escaped;
1337 size_t i, len;
1338 int a, b;
1340 len = 0;
1341 for (s = str; *s; ++s) {
1342 len++;
1343 if (should_urlencode(*s))
1344 len += 2;
1347 escaped = calloc(1, len + 1);
1348 if (escaped == NULL)
1349 return NULL;
1351 i = 0;
1352 for (s = str; *s; ++s) {
1353 if (should_urlencode(*s)) {
1354 a = (*s & 0xF0) >> 4;
1355 b = (*s & 0x0F);
1357 escaped[i++] = '%';
1358 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1359 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1360 } else
1361 escaped[i++] = *s;
1364 return escaped;
1367 const char *
1368 gotweb_action_name(int action)
1370 switch (action) {
1371 case BLAME:
1372 return "blame";
1373 case BLOB:
1374 return "blob";
1375 case BLOBRAW:
1376 return "blobraw";
1377 case BRIEFS:
1378 return "briefs";
1379 case COMMITS:
1380 return "commits";
1381 case DIFF:
1382 return "diff";
1383 case ERR:
1384 return "err";
1385 case INDEX:
1386 return "index";
1387 case SUMMARY:
1388 return "summary";
1389 case TAG:
1390 return "tag";
1391 case TAGS:
1392 return "tags";
1393 case TREE:
1394 return "tree";
1395 case RSS:
1396 return "rss";
1397 default:
1398 return NULL;
1402 int
1403 gotweb_render_url(struct request *c, struct gotweb_url *url)
1405 const char *sep = "?", *action;
1406 char *tmp;
1407 int r;
1409 action = gotweb_action_name(url->action);
1410 if (action != NULL) {
1411 if (fcgi_printf(c, "?action=%s", action) == -1)
1412 return -1;
1413 sep = "&";
1416 if (url->commit) {
1417 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1418 return -1;
1419 sep = "&";
1422 if (url->previd) {
1423 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1424 return -1;
1425 sep = "&";
1428 if (url->prevset) {
1429 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1430 return -1;
1431 sep = "&";
1434 if (url->file) {
1435 tmp = gotweb_urlencode(url->file);
1436 if (tmp == NULL)
1437 return -1;
1438 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1439 free(tmp);
1440 if (r == -1)
1441 return -1;
1442 sep = "&";
1445 if (url->folder) {
1446 tmp = gotweb_urlencode(url->folder);
1447 if (tmp == NULL)
1448 return -1;
1449 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1450 free(tmp);
1451 if (r == -1)
1452 return -1;
1453 sep = "&";
1456 if (url->headref) {
1457 tmp = gotweb_urlencode(url->headref);
1458 if (tmp == NULL)
1459 return -1;
1460 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1461 free(tmp);
1462 if (r == -1)
1463 return -1;
1464 sep = "&";
1467 if (url->index_page != -1) {
1468 if (fcgi_printf(c, "%sindex_page=%d", sep,
1469 url->index_page) == -1)
1470 return -1;
1471 sep = "&";
1474 if (url->path) {
1475 tmp = gotweb_urlencode(url->path);
1476 if (tmp == NULL)
1477 return -1;
1478 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1479 free(tmp);
1480 if (r == -1)
1481 return -1;
1482 sep = "&";
1485 if (url->page != -1) {
1486 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1487 return -1;
1488 sep = "&";
1491 return 0;
1494 int
1495 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1497 struct template *tp = c->tp;
1498 const char *proto = c->https ? "https" : "http";
1500 if (fcgi_puts(tp, proto) == -1 ||
1501 fcgi_puts(tp, "://") == -1 ||
1502 tp_htmlescape(tp, c->server_name) == -1 ||
1503 tp_htmlescape(tp, c->document_uri) == -1)
1504 return -1;
1506 return gotweb_render_url(c, url);
1509 int
1510 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1512 va_list ap;
1513 int r;
1515 if (fcgi_printf(c, "<a href='") == -1)
1516 return -1;
1518 if (gotweb_render_url(c, url) == -1)
1519 return -1;
1521 if (fcgi_printf(c, "'>") == -1)
1522 return -1;
1524 va_start(ap, fmt);
1525 r = fcgi_vprintf(c, fmt, ap);
1526 va_end(ap);
1527 if (r == -1)
1528 return -1;
1530 if (fcgi_printf(c, "</a>"))
1531 return -1;
1532 return 0;
1535 static struct got_repository *
1536 find_cached_repo(struct server *srv, const char *path)
1538 int i;
1540 for (i = 0; i < srv->ncached_repos; i++) {
1541 if (strcmp(srv->cached_repos[i].path, path) == 0)
1542 return srv->cached_repos[i].repo;
1545 return NULL;
1548 static const struct got_error *
1549 cache_repo(struct got_repository **new, struct server *srv,
1550 struct repo_dir *repo_dir, struct socket *sock)
1552 const struct got_error *error = NULL;
1553 struct got_repository *repo;
1554 struct cached_repo *cr;
1555 int evicted = 0;
1557 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1558 cr = &srv->cached_repos[srv->ncached_repos - 1];
1559 error = got_repo_close(cr->repo);
1560 memset(cr, 0, sizeof(*cr));
1561 srv->ncached_repos--;
1562 if (error)
1563 return error;
1564 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1565 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1566 cr = &srv->cached_repos[0];
1567 evicted = 1;
1568 } else {
1569 cr = &srv->cached_repos[srv->ncached_repos];
1572 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1573 if (error) {
1574 if (evicted) {
1575 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1576 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1578 return error;
1581 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1582 >= sizeof(cr->path)) {
1583 if (evicted) {
1584 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1585 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1587 return got_error(GOT_ERR_NO_SPACE);
1590 cr->repo = repo;
1591 srv->ncached_repos++;
1592 *new = repo;
1593 return NULL;
1596 static const struct got_error *
1597 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1599 const struct got_error *error = NULL;
1600 struct socket *sock = c->sock;
1601 struct server *srv = c->srv;
1602 struct transport *t = c->t;
1603 struct got_repository *repo = NULL;
1604 DIR *dt;
1605 char *dir_test;
1607 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1608 GOTWEB_GIT_DIR) == -1)
1609 return got_error_from_errno("asprintf");
1611 dt = opendir(dir_test);
1612 if (dt == NULL) {
1613 free(dir_test);
1614 } else {
1615 repo_dir->path = dir_test;
1616 dir_test = NULL;
1617 goto done;
1620 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1621 repo_dir->name) == -1)
1622 return got_error_from_errno("asprintf");
1624 dt = opendir(dir_test);
1625 if (dt == NULL) {
1626 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1627 goto err;
1628 } else {
1629 repo_dir->path = dir_test;
1630 dir_test = NULL;
1633 done:
1634 if (srv->respect_exportok &&
1635 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1636 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1637 goto err;
1640 repo = find_cached_repo(srv, repo_dir->path);
1641 if (repo == NULL) {
1642 error = cache_repo(&repo, srv, repo_dir, sock);
1643 if (error)
1644 goto err;
1646 t->repo = repo;
1647 error = gotweb_get_repo_description(&repo_dir->description, srv,
1648 repo_dir->path, dirfd(dt));
1649 if (error)
1650 goto err;
1651 error = got_get_repo_owner(&repo_dir->owner, c);
1652 if (error)
1653 goto err;
1654 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1655 if (error)
1656 goto err;
1657 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1658 dirfd(dt));
1659 err:
1660 free(dir_test);
1661 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1662 error = got_error_from_errno("closedir");
1663 return error;
1666 static const struct got_error *
1667 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1669 const struct got_error *error;
1671 *repo_dir = calloc(1, sizeof(**repo_dir));
1672 if (*repo_dir == NULL)
1673 return got_error_from_errno("calloc");
1675 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1676 error = got_error_from_errno("asprintf");
1677 free(*repo_dir);
1678 *repo_dir = NULL;
1679 return error;
1681 (*repo_dir)->owner = NULL;
1682 (*repo_dir)->description = NULL;
1683 (*repo_dir)->url = NULL;
1684 (*repo_dir)->age = NULL;
1685 (*repo_dir)->path = NULL;
1687 return NULL;
1690 static const struct got_error *
1691 gotweb_get_repo_description(char **description, struct server *srv,
1692 const char *dirpath, int dir)
1694 const struct got_error *error = NULL;
1695 struct stat sb;
1696 int fd = -1;
1697 off_t len;
1699 *description = NULL;
1700 if (srv->show_repo_description == 0)
1701 return NULL;
1703 fd = openat(dir, "description", O_RDONLY);
1704 if (fd == -1) {
1705 if (errno != ENOENT && errno != EACCES) {
1706 error = got_error_from_errno_fmt("openat %s/%s",
1707 dirpath, "description");
1709 goto done;
1712 if (fstat(fd, &sb) == -1) {
1713 error = got_error_from_errno_fmt("fstat %s/%s",
1714 dirpath, "description");
1715 goto done;
1718 len = sb.st_size;
1719 if (len > GOTWEBD_MAXDESCRSZ - 1)
1720 len = GOTWEBD_MAXDESCRSZ - 1;
1722 *description = calloc(len + 1, sizeof(**description));
1723 if (*description == NULL) {
1724 error = got_error_from_errno("calloc");
1725 goto done;
1728 if (read(fd, *description, len) == -1)
1729 error = got_error_from_errno("read");
1730 done:
1731 if (fd != -1 && close(fd) == -1 && error == NULL)
1732 error = got_error_from_errno("close");
1733 return error;
1736 static const struct got_error *
1737 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1738 int dir)
1740 const struct got_error *error = NULL;
1741 struct stat sb;
1742 int fd = -1;
1743 off_t len;
1745 *url = NULL;
1746 if (srv->show_repo_cloneurl == 0)
1747 return NULL;
1749 fd = openat(dir, "cloneurl", O_RDONLY);
1750 if (fd == -1) {
1751 if (errno != ENOENT && errno != EACCES) {
1752 error = got_error_from_errno_fmt("openat %s/%s",
1753 dirpath, "cloneurl");
1755 goto done;
1758 if (fstat(fd, &sb) == -1) {
1759 error = got_error_from_errno_fmt("fstat %s/%s",
1760 dirpath, "cloneurl");
1761 goto done;
1764 len = sb.st_size;
1765 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1766 len = GOTWEBD_MAXCLONEURLSZ - 1;
1768 *url = calloc(len + 1, sizeof(**url));
1769 if (*url == NULL) {
1770 error = got_error_from_errno("calloc");
1771 goto done;
1774 if (read(fd, *url, len) == -1)
1775 error = got_error_from_errno("read");
1776 done:
1777 if (fd != -1 && close(fd) == -1 && error == NULL)
1778 error = got_error_from_errno("close");
1779 return error;
1782 const struct got_error *
1783 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1785 struct tm tm;
1786 long long diff_time;
1787 const char *years = "years ago", *months = "months ago";
1788 const char *weeks = "weeks ago", *days = "days ago";
1789 const char *hours = "hours ago", *minutes = "minutes ago";
1790 const char *seconds = "seconds ago", *now = "right now";
1791 char *s;
1792 char datebuf[64];
1793 size_t r;
1795 *repo_age = NULL;
1797 switch (ref_tm) {
1798 case TM_DIFF:
1799 diff_time = time(NULL) - committer_time;
1800 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1801 if (asprintf(repo_age, "%lld %s",
1802 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1803 return got_error_from_errno("asprintf");
1804 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1805 if (asprintf(repo_age, "%lld %s",
1806 (diff_time / 60 / 60 / 24 / (365 / 12)),
1807 months) == -1)
1808 return got_error_from_errno("asprintf");
1809 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1810 if (asprintf(repo_age, "%lld %s",
1811 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1812 return got_error_from_errno("asprintf");
1813 } else if (diff_time > 60 * 60 * 24 * 2) {
1814 if (asprintf(repo_age, "%lld %s",
1815 (diff_time / 60 / 60 / 24), days) == -1)
1816 return got_error_from_errno("asprintf");
1817 } else if (diff_time > 60 * 60 * 2) {
1818 if (asprintf(repo_age, "%lld %s",
1819 (diff_time / 60 / 60), hours) == -1)
1820 return got_error_from_errno("asprintf");
1821 } else if (diff_time > 60 * 2) {
1822 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1823 minutes) == -1)
1824 return got_error_from_errno("asprintf");
1825 } else if (diff_time > 2) {
1826 if (asprintf(repo_age, "%lld %s", diff_time,
1827 seconds) == -1)
1828 return got_error_from_errno("asprintf");
1829 } else {
1830 if (asprintf(repo_age, "%s", now) == -1)
1831 return got_error_from_errno("asprintf");
1833 break;
1834 case TM_LONG:
1835 if (gmtime_r(&committer_time, &tm) == NULL)
1836 return got_error_from_errno("gmtime_r");
1838 s = asctime_r(&tm, datebuf);
1839 if (s == NULL)
1840 return got_error_from_errno("asctime_r");
1842 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1843 return got_error_from_errno("asprintf");
1844 break;
1845 case TM_RFC822:
1846 if (gmtime_r(&committer_time, &tm) == NULL)
1847 return got_error_from_errno("gmtime_r");
1849 r = strftime(datebuf, sizeof(datebuf),
1850 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1851 if (r == 0)
1852 return got_error(GOT_ERR_NO_SPACE);
1854 *repo_age = strdup(datebuf);
1855 if (*repo_age == NULL)
1856 return got_error_from_errno("asprintf");
1857 break;
1859 return NULL;