Blob


1 /*
2 * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
21 #include <net/if.h>
22 #include <netinet/in.h>
23 #include <sys/queue.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <fcntl.h>
32 #include <imsg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_cancel.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
50 #include "got_compat.h"
52 #include "proc.h"
53 #include "gotwebd.h"
54 #include "tmpl.h"
56 static const struct querystring_keys querystring_keys[] = {
57 { "action", ACTION },
58 { "commit", COMMIT },
59 { "file", RFILE },
60 { "folder", FOLDER },
61 { "headref", HEADREF },
62 { "index_page", INDEX_PAGE },
63 { "path", PATH },
64 { "page", PAGE },
65 };
67 static const struct action_keys action_keys[] = {
68 { "blame", BLAME },
69 { "blob", BLOB },
70 { "blobraw", BLOBRAW },
71 { "briefs", BRIEFS },
72 { "commits", COMMITS },
73 { "diff", DIFF },
74 { "error", ERR },
75 { "index", INDEX },
76 { "summary", SUMMARY },
77 { "tag", TAG },
78 { "tags", TAGS },
79 { "tree", TREE },
80 { "rss", RSS },
81 };
83 static const struct got_error *gotweb_init_querystring(struct querystring **);
84 static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 char *);
86 static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 char *, char *);
88 static const struct got_error *gotweb_render_index(struct request *);
89 static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 const char *);
91 static const struct got_error *gotweb_load_got_path(struct request *c,
92 struct repo_dir *);
93 static const struct got_error *gotweb_get_repo_description(char **,
94 struct server *, const char *, int);
95 static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 const char *, int);
97 static const struct got_error *gotweb_render_blame(struct request *);
98 static const struct got_error *gotweb_render_summary(struct request *);
99 static const struct got_error *gotweb_render_tags(struct request *);
100 static const struct got_error *gotweb_render_branches(struct request *);
102 static void gotweb_free_querystring(struct querystring *);
103 static void gotweb_free_repo_dir(struct repo_dir *);
105 struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 void
108 gotweb_process_request(struct request *c)
110 const struct got_error *error = NULL, *error2 = NULL;
111 struct got_blob_object *blob = NULL;
112 struct server *srv = NULL;
113 struct querystring *qs = NULL;
114 struct repo_dir *repo_dir = NULL;
115 FILE *fp = NULL;
116 uint8_t err[] = "gotwebd experienced an error: ";
117 int r, html = 0, fd = -1;
119 /* init the transport */
120 error = gotweb_init_transport(&c->t);
121 if (error) {
122 log_warnx("%s: %s", __func__, error->msg);
123 return;
125 /* don't process any further if client disconnected */
126 if (c->sock->client_status == CLIENT_DISCONNECT)
127 return;
128 /* get the gotwebd server */
129 srv = gotweb_get_server(c->server_name, c->http_host);
130 if (srv == NULL) {
131 log_warnx("%s: error server is NULL", __func__);
132 goto err;
134 c->srv = srv;
135 /* parse our querystring */
136 error = gotweb_init_querystring(&qs);
137 if (error) {
138 log_warnx("%s: %s", __func__, error->msg);
139 goto err;
141 c->t->qs = qs;
142 error = gotweb_parse_querystring(&qs, c->querystring);
143 if (error) {
144 log_warnx("%s: %s", __func__, error->msg);
145 goto err;
148 /*
149 * certain actions require a commit id in the querystring. this stops
150 * bad actors from exploiting this by manually manipulating the
151 * querystring.
152 */
154 if (qs->action == BLAME || qs->action == BLOB ||
155 qs->action == BLOBRAW || qs->action == DIFF) {
156 if (qs->commit == NULL) {
157 error2 = got_error(GOT_ERR_QUERYSTRING);
158 goto render;
162 if (qs->action != INDEX) {
163 error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 if (error)
165 goto done;
166 error = gotweb_load_got_path(c, repo_dir);
167 c->t->repo_dir = repo_dir;
168 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
169 goto err;
172 if (qs->action == BLOBRAW) {
173 error = got_get_repo_commits(c, 1);
174 if (error)
175 goto done;
176 error = got_output_file_blob(c);
177 if (error) {
178 log_warnx("%s: %s", __func__, error->msg);
179 goto err;
181 goto done;
184 if (qs->action == BLOB) {
185 int binary;
186 struct gotweb_url url = {
187 .index_page = -1,
188 .page = -1,
189 .action = BLOBRAW,
190 .path = qs->path,
191 .commit = qs->commit,
192 .folder = qs->folder,
193 .file = qs->file,
194 };
196 error = got_get_repo_commits(c, 1);
197 if (error)
198 goto done;
200 error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
201 if (error2)
202 goto render;
203 if (binary) {
204 fcgi_puts(c->tp, "Status: 302\r\n");
205 fcgi_puts(c->tp, "Location: ");
206 gotweb_render_url(c, &url);
207 fcgi_puts(c->tp, "\r\n\r\n");
208 goto done;
212 if (qs->action == RSS) {
213 error = gotweb_render_content_type_file(c,
214 "application/rss+xml;charset=utf-8",
215 repo_dir->name, ".rss");
216 if (error) {
217 log_warnx("%s: %s", __func__, error->msg);
218 goto err;
221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 if (error) {
223 log_warnx("%s: %s", __func__, error->msg);
224 goto err;
226 if (gotweb_render_rss(c->tp) == -1)
227 goto err;
228 goto done;
231 render:
232 error = gotweb_render_content_type(c, "text/html");
233 if (error) {
234 log_warnx("%s: %s", __func__, error->msg);
235 goto err;
237 html = 1;
239 if (gotweb_render_header(c->tp) == -1)
240 goto err;
242 if (error2) {
243 error = error2;
244 goto err;
247 switch(qs->action) {
248 case BLAME:
249 error = gotweb_render_blame(c);
250 if (error) {
251 log_warnx("%s: %s", __func__, error->msg);
252 goto err;
254 break;
255 case BLOB:
256 if (gotweb_render_blob(c->tp, blob) == -1)
257 goto err;
258 break;
259 case BRIEFS:
260 if (gotweb_render_briefs(c->tp) == -1)
261 goto err;
262 break;
263 case COMMITS:
264 error = got_get_repo_commits(c, srv->max_commits_display);
265 if (error) {
266 log_warnx("%s: %s", __func__, error->msg);
267 goto err;
269 if (gotweb_render_commits(c->tp) == -1)
270 goto err;
271 break;
272 case DIFF:
273 error = got_get_repo_commits(c, 1);
274 if (error) {
275 log_warnx("%s: %s", __func__, error->msg);
276 goto err;
278 error = got_open_diff_for_output(&fp, &fd, c);
279 if (error) {
280 log_warnx("%s: %s", __func__, error->msg);
281 goto err;
283 if (gotweb_render_diff(c->tp, fp) == -1)
284 goto err;
285 break;
286 case INDEX:
287 error = gotweb_render_index(c);
288 if (error) {
289 log_warnx("%s: %s", __func__, error->msg);
290 goto err;
292 break;
293 case SUMMARY:
294 error = gotweb_render_summary(c);
295 if (error) {
296 log_warnx("%s: %s", __func__, error->msg);
297 goto err;
299 break;
300 case TAG:
301 error = got_get_repo_tags(c, 1);
302 if (error) {
303 log_warnx("%s: %s", __func__, error->msg);
304 goto err;
306 if (c->t->tag_count == 0) {
307 error = got_error_msg(GOT_ERR_BAD_OBJ_ID,
308 "bad commit id");
309 goto err;
311 if (gotweb_render_tag(c->tp) == -1)
312 goto done;
313 break;
314 case TAGS:
315 error = gotweb_render_tags(c);
316 if (error) {
317 log_warnx("%s: %s", __func__, error->msg);
318 goto err;
320 break;
321 case TREE:
322 error = got_get_repo_commits(c, 1);
323 if (error) {
324 log_warnx("%s: %s", __func__, error->msg);
325 goto err;
327 if (gotweb_render_tree(c->tp) == -1)
328 goto err;
329 break;
330 case ERR:
331 default:
332 r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
333 "Erorr: Bad Querystring");
334 if (r == -1)
335 goto err;
336 break;
339 goto done;
340 err:
341 if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
342 return;
343 if (fcgi_printf(c, "\n%s", err) == -1)
344 return;
345 if (error) {
346 if (fcgi_printf(c, "%s", error->msg) == -1)
347 return;
348 } else {
349 if (fcgi_printf(c, "see daemon logs for details") == -1)
350 return;
352 if (html && fcgi_printf(c, "</div>\n") == -1)
353 return;
354 done:
355 if (blob)
356 got_object_blob_close(blob);
357 if (fp) {
358 error = got_gotweb_flushfile(fp, fd);
359 if (error)
360 log_warnx("%s: got_gotweb_flushfile failure: %s",
361 __func__, error->msg);
362 fd = -1;
364 if (fd != -1)
365 close(fd);
366 if (html && srv != NULL)
367 gotweb_render_footer(c->tp);
370 struct server *
371 gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
373 struct server *srv = NULL;
375 /* check against the server name first */
376 if (strlen(server_name) > 0)
377 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
378 if (strcmp(srv->name, server_name) == 0)
379 goto done;
381 /* check against subdomain second */
382 if (strlen(subdomain) > 0)
383 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
384 if (strcmp(srv->name, subdomain) == 0)
385 goto done;
387 /* if those fail, send first server */
388 TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
389 if (srv != NULL)
390 break;
391 done:
392 return srv;
393 };
395 const struct got_error *
396 gotweb_init_transport(struct transport **t)
398 const struct got_error *error = NULL;
400 *t = calloc(1, sizeof(**t));
401 if (*t == NULL)
402 return got_error_from_errno2("%s: calloc", __func__);
404 TAILQ_INIT(&(*t)->repo_commits);
405 TAILQ_INIT(&(*t)->repo_tags);
407 (*t)->repo = NULL;
408 (*t)->repo_dir = NULL;
409 (*t)->qs = NULL;
410 (*t)->next_id = NULL;
411 (*t)->prev_id = NULL;
412 (*t)->next_disp = 0;
413 (*t)->prev_disp = 0;
415 return error;
418 static const struct got_error *
419 gotweb_init_querystring(struct querystring **qs)
421 const struct got_error *error = NULL;
423 *qs = calloc(1, sizeof(**qs));
424 if (*qs == NULL)
425 return got_error_from_errno2("%s: calloc", __func__);
427 (*qs)->headref = strdup("HEAD");
428 if ((*qs)->headref == NULL) {
429 free(*qs);
430 *qs = NULL;
431 return got_error_from_errno2("%s: strdup", __func__);
434 (*qs)->action = INDEX;
435 (*qs)->commit = NULL;
436 (*qs)->file = NULL;
437 (*qs)->folder = NULL;
438 (*qs)->index_page = 0;
439 (*qs)->path = NULL;
441 return error;
444 static const struct got_error *
445 gotweb_parse_querystring(struct querystring **qs, char *qst)
447 const struct got_error *error = NULL;
448 char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
449 char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
451 if (qst == NULL)
452 return error;
454 tok1 = strdup(qst);
455 if (tok1 == NULL)
456 return got_error_from_errno2("%s: strdup", __func__);
458 tok1_pair = tok1;
459 tok1_end = tok1;
461 while (tok1_pair != NULL) {
462 strsep(&tok1_end, "&");
464 tok2 = strdup(tok1_pair);
465 if (tok2 == NULL) {
466 free(tok1);
467 return got_error_from_errno2("%s: strdup", __func__);
470 tok2_pair = tok2;
471 tok2_end = tok2;
473 while (tok2_pair != NULL) {
474 strsep(&tok2_end, "=");
475 if (tok2_end) {
476 error = gotweb_assign_querystring(qs, tok2_pair,
477 tok2_end);
478 if (error)
479 goto err;
481 tok2_pair = tok2_end;
483 free(tok2);
484 tok1_pair = tok1_end;
486 free(tok1);
487 return error;
488 err:
489 free(tok2);
490 free(tok1);
491 return error;
494 /*
495 * Adapted from usr.sbin/httpd/httpd.c url_decode.
496 */
497 static const struct got_error *
498 gotweb_urldecode(char *url)
500 char *p, *q;
501 char hex[3];
502 unsigned long x;
504 hex[2] = '\0';
505 p = q = url;
507 while (*p != '\0') {
508 switch (*p) {
509 case '%':
510 /* Encoding character is followed by two hex chars */
511 if (!isxdigit((unsigned char)p[1]) ||
512 !isxdigit((unsigned char)p[2]) ||
513 (p[1] == '0' && p[2] == '0'))
514 return got_error(GOT_ERR_BAD_QUERYSTRING);
516 hex[0] = p[1];
517 hex[1] = p[2];
519 /*
520 * We don't have to validate "hex" because it is
521 * guaranteed to include two hex chars followed by nul.
522 */
523 x = strtoul(hex, NULL, 16);
524 *q = (char)x;
525 p += 2;
526 break;
527 default:
528 *q = *p;
529 break;
531 p++;
532 q++;
534 *q = '\0';
536 return NULL;
539 static const struct got_error *
540 gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
542 const struct got_error *error = NULL;
543 const char *errstr;
544 int a_cnt, el_cnt;
546 error = gotweb_urldecode(value);
547 if (error)
548 return error;
550 for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
551 if (strcmp(key, querystring_keys[el_cnt].name) != 0)
552 continue;
554 switch (querystring_keys[el_cnt].element) {
555 case ACTION:
556 for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
557 if (strcmp(value, action_keys[a_cnt].name) != 0)
558 continue;
559 else if (strcmp(value,
560 action_keys[a_cnt].name) == 0){
561 (*qs)->action =
562 action_keys[a_cnt].action;
563 goto qa_found;
566 (*qs)->action = ERR;
567 qa_found:
568 break;
569 case COMMIT:
570 (*qs)->commit = strdup(value);
571 if ((*qs)->commit == NULL) {
572 error = got_error_from_errno2("%s: strdup",
573 __func__);
574 goto done;
576 break;
577 case RFILE:
578 (*qs)->file = strdup(value);
579 if ((*qs)->file == NULL) {
580 error = got_error_from_errno2("%s: strdup",
581 __func__);
582 goto done;
584 break;
585 case FOLDER:
586 (*qs)->folder = strdup(value);
587 if ((*qs)->folder == NULL) {
588 error = got_error_from_errno2("%s: strdup",
589 __func__);
590 goto done;
592 break;
593 case HEADREF:
594 free((*qs)->headref);
595 (*qs)->headref = strdup(value);
596 if ((*qs)->headref == NULL) {
597 error = got_error_from_errno2("%s: strdup",
598 __func__);
599 goto done;
601 break;
602 case INDEX_PAGE:
603 if (strlen(value) == 0)
604 break;
605 (*qs)->index_page = strtonum(value, INT64_MIN,
606 INT64_MAX, &errstr);
607 if (errstr) {
608 error = got_error_from_errno3("%s: strtonum %s",
609 __func__, errstr);
610 goto done;
612 if ((*qs)->index_page < 0)
613 (*qs)->index_page = 0;
614 break;
615 case PATH:
616 (*qs)->path = strdup(value);
617 if ((*qs)->path == NULL) {
618 error = got_error_from_errno2("%s: strdup",
619 __func__);
620 goto done;
622 break;
623 case PAGE:
624 if (strlen(value) == 0)
625 break;
626 (*qs)->page = strtonum(value, INT64_MIN,
627 INT64_MAX, &errstr);
628 if (errstr) {
629 error = got_error_from_errno3("%s: strtonum %s",
630 __func__, errstr);
631 goto done;
633 if ((*qs)->page < 0)
634 (*qs)->page = 0;
635 break;
636 default:
637 break;
640 done:
641 return error;
644 void
645 gotweb_free_repo_tag(struct repo_tag *rt)
647 if (rt != NULL) {
648 free(rt->commit_id);
649 free(rt->tag_name);
650 free(rt->tag_commit);
651 free(rt->commit_msg);
652 free(rt->tagger);
654 free(rt);
657 void
658 gotweb_free_repo_commit(struct repo_commit *rc)
660 if (rc != NULL) {
661 free(rc->path);
662 free(rc->refs_str);
663 free(rc->commit_id);
664 free(rc->parent_id);
665 free(rc->tree_id);
666 free(rc->author);
667 free(rc->committer);
668 free(rc->commit_msg);
670 free(rc);
673 static void
674 gotweb_free_querystring(struct querystring *qs)
676 if (qs != NULL) {
677 free(qs->commit);
678 free(qs->file);
679 free(qs->folder);
680 free(qs->headref);
681 free(qs->path);
683 free(qs);
686 static void
687 gotweb_free_repo_dir(struct repo_dir *repo_dir)
689 if (repo_dir != NULL) {
690 free(repo_dir->name);
691 free(repo_dir->owner);
692 free(repo_dir->description);
693 free(repo_dir->url);
694 free(repo_dir->age);
695 free(repo_dir->path);
697 free(repo_dir);
700 void
701 gotweb_free_transport(struct transport *t)
703 struct repo_commit *rc = NULL, *trc = NULL;
704 struct repo_tag *rt = NULL, *trt = NULL;
706 TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
707 TAILQ_REMOVE(&t->repo_commits, rc, entry);
708 gotweb_free_repo_commit(rc);
710 TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
711 TAILQ_REMOVE(&t->repo_tags, rt, entry);
712 gotweb_free_repo_tag(rt);
714 gotweb_free_repo_dir(t->repo_dir);
715 gotweb_free_querystring(t->qs);
716 free(t->next_id);
717 free(t->prev_id);
718 free(t);
721 const struct got_error *
722 gotweb_render_content_type(struct request *c, const char *type)
724 const char *csp = "default-src 'self'; script-src 'none'; "
725 "object-src 'none';";
727 fcgi_printf(c,
728 "Content-Security-Policy: %s\r\n"
729 "Content-Type: %s\r\n\r\n",
730 csp, type);
731 return NULL;
734 const struct got_error *
735 gotweb_render_content_type_file(struct request *c, const char *type,
736 const char *file, const char *suffix)
738 fcgi_printf(c, "Content-type: %s\r\n"
739 "Content-disposition: attachment; filename=%s%s\r\n\r\n",
740 type, file, suffix ? suffix : "");
741 return NULL;
744 void
745 gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
746 struct gotweb_url *next, int *have_next)
748 struct transport *t = c->t;
749 struct querystring *qs = t->qs;
750 struct server *srv = c->srv;
752 *have_prev = *have_next = 0;
754 switch(qs->action) {
755 case INDEX:
756 if (qs->index_page > 0) {
757 *have_prev = 1;
758 *prev = (struct gotweb_url){
759 .action = -1,
760 .index_page = qs->index_page - 1,
761 .page = -1,
762 };
764 if (t->next_disp == srv->max_repos_display &&
765 t->repos_total != (qs->index_page + 1) *
766 srv->max_repos_display) {
767 *have_next = 1;
768 *next = (struct gotweb_url){
769 .action = -1,
770 .index_page = qs->index_page + 1,
771 .page = -1,
772 };
774 break;
775 case BRIEFS:
776 if (t->prev_id && qs->commit != NULL &&
777 strcmp(qs->commit, t->prev_id) != 0) {
778 *have_prev = 1;
779 *prev = (struct gotweb_url){
780 .action = BRIEFS,
781 .index_page = -1,
782 .page = qs->page - 1,
783 .path = qs->path,
784 .commit = t->prev_id,
785 .headref = qs->headref,
786 };
788 if (t->next_id) {
789 *have_next = 1;
790 *next = (struct gotweb_url){
791 .action = BRIEFS,
792 .index_page = -1,
793 .page = qs->page + 1,
794 .path = qs->path,
795 .commit = t->next_id,
796 .headref = qs->headref,
797 };
799 break;
800 case COMMITS:
801 if (t->prev_id && qs->commit != NULL &&
802 strcmp(qs->commit, t->prev_id) != 0) {
803 *have_prev = 1;
804 *prev = (struct gotweb_url){
805 .action = COMMITS,
806 .index_page = -1,
807 .page = qs->page - 1,
808 .path = qs->path,
809 .commit = t->prev_id,
810 .headref = qs->headref,
811 .folder = qs->folder,
812 .file = qs->file,
813 };
815 if (t->next_id) {
816 *have_next = 1;
817 *next = (struct gotweb_url){
818 .action = COMMITS,
819 .index_page = -1,
820 .page = qs->page + 1,
821 .path = qs->path,
822 .commit = t->next_id,
823 .headref = qs->headref,
824 .folder = qs->folder,
825 .file = qs->file,
826 };
828 break;
829 case TAGS:
830 if (t->prev_id && qs->commit != NULL &&
831 strcmp(qs->commit, t->prev_id) != 0) {
832 *have_prev = 1;
833 *prev = (struct gotweb_url){
834 .action = TAGS,
835 .index_page = -1,
836 .page = qs->page - 1,
837 .path = qs->path,
838 .commit = t->prev_id,
839 .headref = qs->headref,
840 };
842 if (t->next_id) {
843 *have_next = 1;
844 *next = (struct gotweb_url){
845 .action = TAGS,
846 .index_page = -1,
847 .page = qs->page + 1,
848 .path = qs->path,
849 .commit = t->next_id,
850 .headref = qs->headref,
851 };
853 break;
857 static const struct got_error *
858 gotweb_render_index(struct request *c)
860 const struct got_error *error = NULL;
861 struct server *srv = c->srv;
862 struct transport *t = c->t;
863 struct querystring *qs = t->qs;
864 struct repo_dir *repo_dir = NULL;
865 DIR *d;
866 struct dirent **sd_dent = NULL;
867 unsigned int d_cnt, d_i, d_disp = 0;
868 unsigned int d_skipped = 0;
869 int type;
871 d = opendir(srv->repos_path);
872 if (d == NULL) {
873 error = got_error_from_errno2("opendir", srv->repos_path);
874 return error;
877 d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
878 if (d_cnt == -1) {
879 sd_dent = NULL;
880 error = got_error_from_errno2("scandir", srv->repos_path);
881 goto done;
884 if (gotweb_render_repo_table_hdr(c->tp) == -1)
885 goto done;
887 for (d_i = 0; d_i < d_cnt; d_i++) {
888 if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
889 break;
891 if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
892 strcmp(sd_dent[d_i]->d_name, "..") == 0) {
893 d_skipped++;
894 continue;
897 error = got_path_dirent_type(&type, srv->repos_path,
898 sd_dent[d_i]);
899 if (error)
900 goto done;
901 if (type != DT_DIR) {
902 d_skipped++;
903 continue;
906 if (qs->index_page > 0 && (qs->index_page *
907 srv->max_repos_display) > t->prev_disp) {
908 t->prev_disp++;
909 continue;
912 error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
913 if (error)
914 goto done;
916 error = gotweb_load_got_path(c, repo_dir);
917 if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
918 error = NULL;
919 gotweb_free_repo_dir(repo_dir);
920 repo_dir = NULL;
921 d_skipped++;
922 continue;
924 if (error && error->code != GOT_ERR_LONELY_PACKIDX)
925 goto done;
927 d_disp++;
928 t->prev_disp++;
930 if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
931 goto done;
933 gotweb_free_repo_dir(repo_dir);
934 repo_dir = NULL;
935 t->next_disp++;
936 if (d_disp == srv->max_repos_display)
937 break;
939 t->repos_total = d_cnt - d_skipped;
941 if (srv->max_repos_display == 0)
942 goto done;
943 if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
944 goto done;
945 if (t->repos_total <= srv->max_repos ||
946 t->repos_total <= srv->max_repos_display)
947 goto done;
949 if (gotweb_render_navs(c->tp) == -1)
950 goto done;
951 done:
952 if (sd_dent) {
953 for (d_i = 0; d_i < d_cnt; d_i++)
954 free(sd_dent[d_i]);
955 free(sd_dent);
957 if (d != NULL && closedir(d) == EOF && error == NULL)
958 error = got_error_from_errno("closedir");
959 return error;
962 static const struct got_error *
963 gotweb_render_blame(struct request *c)
965 const struct got_error *error = NULL;
966 struct transport *t = c->t;
967 struct repo_commit *rc = NULL;
968 char *age = NULL, *msg = NULL;
969 int r;
971 error = got_get_repo_commits(c, 1);
972 if (error)
973 return error;
975 rc = TAILQ_FIRST(&t->repo_commits);
977 error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
978 if (error)
979 goto done;
980 error = gotweb_escape_html(&msg, rc->commit_msg);
981 if (error)
982 goto done;
984 r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
985 "<div id='blame_title'>Blame</div>\n"
986 "</div>\n" /* #blame_title_wrapper */
987 "<div id='blame_content'>\n"
988 "<div id='blame_header_wrapper'>\n"
989 "<div id='blame_header'>\n"
990 "<div class='header_age_title'>Date:</div>\n"
991 "<div class='header_age'>%s</div>\n"
992 "<div id='header_commit_msg_title'>Message:</div>\n"
993 "<div id='header_commit_msg'>%s</div>\n"
994 "</div>\n" /* #blame_header */
995 "</div>\n" /* #blame_header_wrapper */
996 "<div class='dotted_line'></div>\n"
997 "<div id='blame'>\n",
998 age,
999 msg);
1000 if (r == -1)
1001 goto done;
1003 error = got_output_file_blame(c);
1004 if (error)
1005 goto done;
1007 fcgi_printf(c, "</div>\n" /* #blame */
1008 "</div>\n"); /* #blame_content */
1009 done:
1010 free(age);
1011 free(msg);
1012 return error;
1015 static const struct got_error *
1016 gotweb_render_branches(struct request *c)
1018 const struct got_error *error = NULL;
1019 struct got_reflist_head refs;
1020 struct got_reflist_entry *re;
1021 struct transport *t = c->t;
1022 struct querystring *qs = t->qs;
1023 struct got_repository *repo = t->repo;
1024 char *escaped_refname = NULL;
1025 char *age = NULL;
1026 int r;
1028 TAILQ_INIT(&refs);
1030 error = got_ref_list(&refs, repo, "refs/heads",
1031 got_ref_cmp_by_name, NULL);
1032 if (error)
1033 goto done;
1035 r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1036 "<div id='branches_title'>Branches</div>\n"
1037 "</div>\n" /* #branches_title_wrapper */
1038 "<div id='branches_content'>\n");
1039 if (r == -1)
1040 goto done;
1042 TAILQ_FOREACH(re, &refs, entry) {
1043 const char *refname = NULL;
1045 if (got_ref_is_symbolic(re->ref))
1046 continue;
1048 refname = got_ref_get_name(re->ref);
1049 if (refname == NULL) {
1050 error = got_error_from_errno("strdup");
1051 goto done;
1053 if (strncmp(refname, "refs/heads/", 11) != 0)
1054 continue;
1056 error = got_get_repo_age(&age, c, refname, TM_DIFF);
1057 if (error)
1058 goto done;
1060 if (strncmp(refname, "refs/heads/", 11) == 0)
1061 refname += 11;
1062 error = gotweb_escape_html(&escaped_refname, refname);
1063 if (error)
1064 goto done;
1066 r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1067 "<div class='branches_age'>%s</div>\n"
1068 "<div class='branches_space'>&nbsp;</div>\n"
1069 "<div class='branch'>", age);
1070 if (r == -1)
1071 goto done;
1073 r = gotweb_link(c, &(struct gotweb_url){
1074 .action = SUMMARY,
1075 .index_page = -1,
1076 .page = -1,
1077 .path = qs->path,
1078 .headref = refname,
1079 }, "%s", escaped_refname);
1080 if (r == -1)
1081 goto done;
1083 if (fcgi_printf(c, "</div>\n" /* .branch */
1084 "<div class='navs_wrapper'>\n"
1085 "<div class='navs'>") == -1)
1086 goto done;
1088 r = gotweb_link(c, &(struct gotweb_url){
1089 .action = SUMMARY,
1090 .index_page = -1,
1091 .page = -1,
1092 .path = qs->path,
1093 .headref = refname,
1094 }, "summary");
1095 if (r == -1)
1096 goto done;
1098 if (fcgi_printf(c, " | ") == -1)
1099 goto done;
1101 r = gotweb_link(c, &(struct gotweb_url){
1102 .action = BRIEFS,
1103 .index_page = -1,
1104 .page = -1,
1105 .path = qs->path,
1106 .headref = refname,
1107 }, "commit briefs");
1108 if (r == -1)
1109 goto done;
1111 if (fcgi_printf(c, " | ") == -1)
1112 goto done;
1114 r = gotweb_link(c, &(struct gotweb_url){
1115 .action = COMMITS,
1116 .index_page = -1,
1117 .page = -1,
1118 .path = qs->path,
1119 .headref = refname,
1120 }, "commits");
1121 if (r == -1)
1122 goto done;
1124 r = fcgi_printf(c, "</div>\n" /* .navs */
1125 "</div>\n" /* .navs_wrapper */
1126 "<div class='dotted_line'></div>\n"
1127 "</div>\n"); /* .branches_wrapper */
1128 if (r == -1)
1129 goto done;
1131 free(age);
1132 age = NULL;
1133 free(escaped_refname);
1134 escaped_refname = NULL;
1136 fcgi_printf(c, "</div>\n"); /* #branches_content */
1137 done:
1138 free(age);
1139 free(escaped_refname);
1140 got_ref_list_free(&refs);
1141 return error;
1144 static const struct got_error *
1145 gotweb_render_summary(struct request *c)
1147 const struct got_error *error = NULL;
1148 struct transport *t = c->t;
1149 struct server *srv = c->srv;
1150 int r;
1152 if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1153 goto done;
1155 if (srv->show_repo_description) {
1156 r = fcgi_printf(c,
1157 "<div id='description_title'>Description:</div>\n"
1158 "<div id='description'>%s</div>\n",
1159 t->repo_dir->description ? t->repo_dir->description : "");
1160 if (r == -1)
1161 goto done;
1164 if (srv->show_repo_owner) {
1165 r = fcgi_printf(c,
1166 "<div id='repo_owner_title'>Owner:</div>\n"
1167 "<div id='repo_owner'>%s</div>\n",
1168 t->repo_dir->owner ? t->repo_dir->owner : "");
1169 if (r == -1)
1170 goto done;
1173 if (srv->show_repo_age) {
1174 r = fcgi_printf(c,
1175 "<div id='last_change_title'>Last Change:</div>\n"
1176 "<div id='last_change'>%s</div>\n",
1177 t->repo_dir->age);
1178 if (r == -1)
1179 goto done;
1182 if (srv->show_repo_cloneurl) {
1183 r = fcgi_printf(c,
1184 "<div id='cloneurl_title'>Clone URL:</div>\n"
1185 "<div id='cloneurl'>%s</div>\n",
1186 t->repo_dir->url ? t->repo_dir->url : "");
1187 if (r == -1)
1188 goto done;
1191 r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1192 if (r == -1)
1193 goto done;
1195 if (gotweb_render_briefs(c->tp) == -1)
1196 goto done;
1198 error = gotweb_render_tags(c);
1199 if (error) {
1200 log_warnx("%s: %s", __func__, error->msg);
1201 goto done;
1204 error = gotweb_render_branches(c);
1205 if (error)
1206 log_warnx("%s: %s", __func__, error->msg);
1207 done:
1208 return error;
1211 static const struct got_error *
1212 gotweb_render_tags(struct request *c)
1214 const struct got_error *error = NULL;
1215 struct server *srv = c->srv;
1216 struct transport *t = c->t;
1217 struct querystring *qs = t->qs;
1219 if (qs->action == BRIEFS) {
1220 qs->action = TAGS;
1221 error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1222 } else
1223 error = got_get_repo_tags(c, srv->max_commits_display);
1224 if (error)
1225 goto done;
1227 if (gotweb_render_tags_tmpl(c->tp) == -1)
1228 goto done;
1230 done:
1231 return error;
1234 const struct got_error *
1235 gotweb_escape_html(char **escaped_html, const char *orig_html)
1237 const struct got_error *error = NULL;
1238 struct escape_pair {
1239 char c;
1240 const char *s;
1241 } esc[] = {
1242 { '>', "&gt;" },
1243 { '<', "&lt;" },
1244 { '&', "&amp;" },
1245 { '"', "&quot;" },
1246 { '\'', "&apos;" },
1247 { '\n', "<br />" },
1249 size_t orig_len, len;
1250 int i, j, x;
1252 orig_len = strlen(orig_html);
1253 len = orig_len;
1254 for (i = 0; i < orig_len; i++) {
1255 for (j = 0; j < nitems(esc); j++) {
1256 if (orig_html[i] != esc[j].c)
1257 continue;
1258 len += strlen(esc[j].s) - 1 /* escaped char */;
1262 *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1263 if (*escaped_html == NULL)
1264 return got_error_from_errno("calloc");
1266 x = 0;
1267 for (i = 0; i < orig_len; i++) {
1268 int escaped = 0;
1269 for (j = 0; j < nitems(esc); j++) {
1270 if (orig_html[i] != esc[j].c)
1271 continue;
1273 if (strlcat(*escaped_html, esc[j].s, len + 1)
1274 >= len + 1) {
1275 error = got_error(GOT_ERR_NO_SPACE);
1276 goto done;
1278 x += strlen(esc[j].s);
1279 escaped = 1;
1280 break;
1282 if (!escaped) {
1283 (*escaped_html)[x] = orig_html[i];
1284 x++;
1287 done:
1288 if (error) {
1289 free(*escaped_html);
1290 *escaped_html = NULL;
1291 } else {
1292 (*escaped_html)[x] = '\0';
1295 return error;
1298 static inline int
1299 should_urlencode(int c)
1301 if (c <= ' ' || c >= 127)
1302 return 1;
1304 switch (c) {
1305 /* gen-delim */
1306 case ':':
1307 case '/':
1308 case '?':
1309 case '#':
1310 case '[':
1311 case ']':
1312 case '@':
1313 /* sub-delims */
1314 case '!':
1315 case '$':
1316 case '&':
1317 case '\'':
1318 case '(':
1319 case ')':
1320 case '*':
1321 case '+':
1322 case ',':
1323 case ';':
1324 case '=':
1325 /* needed because the URLs are embedded into the HTML */
1326 case '\"':
1327 return 1;
1328 default:
1329 return 0;
1333 static char *
1334 gotweb_urlencode(const char *str)
1336 const char *s;
1337 char *escaped;
1338 size_t i, len;
1339 int a, b;
1341 len = 0;
1342 for (s = str; *s; ++s) {
1343 len++;
1344 if (should_urlencode(*s))
1345 len += 2;
1348 escaped = calloc(1, len + 1);
1349 if (escaped == NULL)
1350 return NULL;
1352 i = 0;
1353 for (s = str; *s; ++s) {
1354 if (should_urlencode(*s)) {
1355 a = (*s & 0xF0) >> 4;
1356 b = (*s & 0x0F);
1358 escaped[i++] = '%';
1359 escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1360 escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1361 } else
1362 escaped[i++] = *s;
1365 return escaped;
1368 const char *
1369 gotweb_action_name(int action)
1371 switch (action) {
1372 case BLAME:
1373 return "blame";
1374 case BLOB:
1375 return "blob";
1376 case BLOBRAW:
1377 return "blobraw";
1378 case BRIEFS:
1379 return "briefs";
1380 case COMMITS:
1381 return "commits";
1382 case DIFF:
1383 return "diff";
1384 case ERR:
1385 return "err";
1386 case INDEX:
1387 return "index";
1388 case SUMMARY:
1389 return "summary";
1390 case TAG:
1391 return "tag";
1392 case TAGS:
1393 return "tags";
1394 case TREE:
1395 return "tree";
1396 case RSS:
1397 return "rss";
1398 default:
1399 return NULL;
1403 int
1404 gotweb_render_url(struct request *c, struct gotweb_url *url)
1406 const char *sep = "?", *action;
1407 char *tmp;
1408 int r;
1410 action = gotweb_action_name(url->action);
1411 if (action != NULL) {
1412 if (fcgi_printf(c, "?action=%s", action) == -1)
1413 return -1;
1414 sep = "&";
1417 if (url->commit) {
1418 if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1419 return -1;
1420 sep = "&";
1423 if (url->previd) {
1424 if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1425 return -1;
1426 sep = "&";
1429 if (url->prevset) {
1430 if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1431 return -1;
1432 sep = "&";
1435 if (url->file) {
1436 tmp = gotweb_urlencode(url->file);
1437 if (tmp == NULL)
1438 return -1;
1439 r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1440 free(tmp);
1441 if (r == -1)
1442 return -1;
1443 sep = "&";
1446 if (url->folder) {
1447 tmp = gotweb_urlencode(url->folder);
1448 if (tmp == NULL)
1449 return -1;
1450 r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1451 free(tmp);
1452 if (r == -1)
1453 return -1;
1454 sep = "&";
1457 if (url->headref) {
1458 tmp = gotweb_urlencode(url->headref);
1459 if (tmp == NULL)
1460 return -1;
1461 r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1462 free(tmp);
1463 if (r == -1)
1464 return -1;
1465 sep = "&";
1468 if (url->index_page != -1) {
1469 if (fcgi_printf(c, "%sindex_page=%d", sep,
1470 url->index_page) == -1)
1471 return -1;
1472 sep = "&";
1475 if (url->path) {
1476 tmp = gotweb_urlencode(url->path);
1477 if (tmp == NULL)
1478 return -1;
1479 r = fcgi_printf(c, "%spath=%s", sep, tmp);
1480 free(tmp);
1481 if (r == -1)
1482 return -1;
1483 sep = "&";
1486 if (url->page != -1) {
1487 if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1488 return -1;
1489 sep = "&";
1492 return 0;
1495 int
1496 gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1498 struct template *tp = c->tp;
1499 const char *proto = c->https ? "https" : "http";
1501 if (fcgi_puts(tp, proto) == -1 ||
1502 fcgi_puts(tp, "://") == -1 ||
1503 tp_htmlescape(tp, c->server_name) == -1 ||
1504 tp_htmlescape(tp, c->document_uri) == -1)
1505 return -1;
1507 return gotweb_render_url(c, url);
1510 int
1511 gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1513 va_list ap;
1514 int r;
1516 if (fcgi_printf(c, "<a href='") == -1)
1517 return -1;
1519 if (gotweb_render_url(c, url) == -1)
1520 return -1;
1522 if (fcgi_printf(c, "'>") == -1)
1523 return -1;
1525 va_start(ap, fmt);
1526 r = fcgi_vprintf(c, fmt, ap);
1527 va_end(ap);
1528 if (r == -1)
1529 return -1;
1531 if (fcgi_printf(c, "</a>"))
1532 return -1;
1533 return 0;
1536 static struct got_repository *
1537 find_cached_repo(struct server *srv, const char *path)
1539 int i;
1541 for (i = 0; i < srv->ncached_repos; i++) {
1542 if (strcmp(srv->cached_repos[i].path, path) == 0)
1543 return srv->cached_repos[i].repo;
1546 return NULL;
1549 static const struct got_error *
1550 cache_repo(struct got_repository **new, struct server *srv,
1551 struct repo_dir *repo_dir, struct socket *sock)
1553 const struct got_error *error = NULL;
1554 struct got_repository *repo;
1555 struct cached_repo *cr;
1556 int evicted = 0;
1558 if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1559 cr = &srv->cached_repos[srv->ncached_repos - 1];
1560 error = got_repo_close(cr->repo);
1561 memset(cr, 0, sizeof(*cr));
1562 srv->ncached_repos--;
1563 if (error)
1564 return error;
1565 memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1566 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1567 cr = &srv->cached_repos[0];
1568 evicted = 1;
1569 } else {
1570 cr = &srv->cached_repos[srv->ncached_repos];
1573 error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1574 if (error) {
1575 if (evicted) {
1576 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1577 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1579 return error;
1582 if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1583 >= sizeof(cr->path)) {
1584 if (evicted) {
1585 memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1586 srv->ncached_repos * sizeof(srv->cached_repos[0]));
1588 return got_error(GOT_ERR_NO_SPACE);
1591 cr->repo = repo;
1592 srv->ncached_repos++;
1593 *new = repo;
1594 return NULL;
1597 static const struct got_error *
1598 gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1600 const struct got_error *error = NULL;
1601 struct socket *sock = c->sock;
1602 struct server *srv = c->srv;
1603 struct transport *t = c->t;
1604 struct got_repository *repo = NULL;
1605 DIR *dt;
1606 char *dir_test;
1608 if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1609 GOTWEB_GIT_DIR) == -1)
1610 return got_error_from_errno("asprintf");
1612 dt = opendir(dir_test);
1613 if (dt == NULL) {
1614 free(dir_test);
1615 } else {
1616 repo_dir->path = dir_test;
1617 dir_test = NULL;
1618 goto done;
1621 if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1622 repo_dir->name) == -1)
1623 return got_error_from_errno("asprintf");
1625 dt = opendir(dir_test);
1626 if (dt == NULL) {
1627 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1628 goto err;
1629 } else {
1630 repo_dir->path = dir_test;
1631 dir_test = NULL;
1634 done:
1635 if (srv->respect_exportok &&
1636 faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1637 error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1638 goto err;
1641 repo = find_cached_repo(srv, repo_dir->path);
1642 if (repo == NULL) {
1643 error = cache_repo(&repo, srv, repo_dir, sock);
1644 if (error)
1645 goto err;
1647 t->repo = repo;
1648 error = gotweb_get_repo_description(&repo_dir->description, srv,
1649 repo_dir->path, dirfd(dt));
1650 if (error)
1651 goto err;
1652 error = got_get_repo_owner(&repo_dir->owner, c);
1653 if (error)
1654 goto err;
1655 error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1656 if (error)
1657 goto err;
1658 error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1659 dirfd(dt));
1660 err:
1661 free(dir_test);
1662 if (dt != NULL && closedir(dt) == EOF && error == NULL)
1663 error = got_error_from_errno("closedir");
1664 return error;
1667 static const struct got_error *
1668 gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1670 const struct got_error *error;
1672 *repo_dir = calloc(1, sizeof(**repo_dir));
1673 if (*repo_dir == NULL)
1674 return got_error_from_errno("calloc");
1676 if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1677 error = got_error_from_errno("asprintf");
1678 free(*repo_dir);
1679 *repo_dir = NULL;
1680 return error;
1682 (*repo_dir)->owner = NULL;
1683 (*repo_dir)->description = NULL;
1684 (*repo_dir)->url = NULL;
1685 (*repo_dir)->age = NULL;
1686 (*repo_dir)->path = NULL;
1688 return NULL;
1691 static const struct got_error *
1692 gotweb_get_repo_description(char **description, struct server *srv,
1693 const char *dirpath, int dir)
1695 const struct got_error *error = NULL;
1696 struct stat sb;
1697 int fd = -1;
1698 off_t len;
1700 *description = NULL;
1701 if (srv->show_repo_description == 0)
1702 return NULL;
1704 fd = openat(dir, "description", O_RDONLY);
1705 if (fd == -1) {
1706 if (errno != ENOENT && errno != EACCES) {
1707 error = got_error_from_errno_fmt("openat %s/%s",
1708 dirpath, "description");
1710 goto done;
1713 if (fstat(fd, &sb) == -1) {
1714 error = got_error_from_errno_fmt("fstat %s/%s",
1715 dirpath, "description");
1716 goto done;
1719 len = sb.st_size;
1720 if (len > GOTWEBD_MAXDESCRSZ - 1)
1721 len = GOTWEBD_MAXDESCRSZ - 1;
1723 *description = calloc(len + 1, sizeof(**description));
1724 if (*description == NULL) {
1725 error = got_error_from_errno("calloc");
1726 goto done;
1729 if (read(fd, *description, len) == -1)
1730 error = got_error_from_errno("read");
1731 done:
1732 if (fd != -1 && close(fd) == -1 && error == NULL)
1733 error = got_error_from_errno("close");
1734 return error;
1737 static const struct got_error *
1738 gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1739 int dir)
1741 const struct got_error *error = NULL;
1742 struct stat sb;
1743 int fd = -1;
1744 off_t len;
1746 *url = NULL;
1747 if (srv->show_repo_cloneurl == 0)
1748 return NULL;
1750 fd = openat(dir, "cloneurl", O_RDONLY);
1751 if (fd == -1) {
1752 if (errno != ENOENT && errno != EACCES) {
1753 error = got_error_from_errno_fmt("openat %s/%s",
1754 dirpath, "cloneurl");
1756 goto done;
1759 if (fstat(fd, &sb) == -1) {
1760 error = got_error_from_errno_fmt("fstat %s/%s",
1761 dirpath, "cloneurl");
1762 goto done;
1765 len = sb.st_size;
1766 if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1767 len = GOTWEBD_MAXCLONEURLSZ - 1;
1769 *url = calloc(len + 1, sizeof(**url));
1770 if (*url == NULL) {
1771 error = got_error_from_errno("calloc");
1772 goto done;
1775 if (read(fd, *url, len) == -1)
1776 error = got_error_from_errno("read");
1777 done:
1778 if (fd != -1 && close(fd) == -1 && error == NULL)
1779 error = got_error_from_errno("close");
1780 return error;
1783 const struct got_error *
1784 gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1786 struct tm tm;
1787 long long diff_time;
1788 const char *years = "years ago", *months = "months ago";
1789 const char *weeks = "weeks ago", *days = "days ago";
1790 const char *hours = "hours ago", *minutes = "minutes ago";
1791 const char *seconds = "seconds ago", *now = "right now";
1792 char *s;
1793 char datebuf[64];
1794 size_t r;
1796 *repo_age = NULL;
1798 switch (ref_tm) {
1799 case TM_DIFF:
1800 diff_time = time(NULL) - committer_time;
1801 if (diff_time > 60 * 60 * 24 * 365 * 2) {
1802 if (asprintf(repo_age, "%lld %s",
1803 (diff_time / 60 / 60 / 24 / 365), years) == -1)
1804 return got_error_from_errno("asprintf");
1805 } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1806 if (asprintf(repo_age, "%lld %s",
1807 (diff_time / 60 / 60 / 24 / (365 / 12)),
1808 months) == -1)
1809 return got_error_from_errno("asprintf");
1810 } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1811 if (asprintf(repo_age, "%lld %s",
1812 (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1813 return got_error_from_errno("asprintf");
1814 } else if (diff_time > 60 * 60 * 24 * 2) {
1815 if (asprintf(repo_age, "%lld %s",
1816 (diff_time / 60 / 60 / 24), days) == -1)
1817 return got_error_from_errno("asprintf");
1818 } else if (diff_time > 60 * 60 * 2) {
1819 if (asprintf(repo_age, "%lld %s",
1820 (diff_time / 60 / 60), hours) == -1)
1821 return got_error_from_errno("asprintf");
1822 } else if (diff_time > 60 * 2) {
1823 if (asprintf(repo_age, "%lld %s", (diff_time / 60),
1824 minutes) == -1)
1825 return got_error_from_errno("asprintf");
1826 } else if (diff_time > 2) {
1827 if (asprintf(repo_age, "%lld %s", diff_time,
1828 seconds) == -1)
1829 return got_error_from_errno("asprintf");
1830 } else {
1831 if (asprintf(repo_age, "%s", now) == -1)
1832 return got_error_from_errno("asprintf");
1834 break;
1835 case TM_LONG:
1836 if (gmtime_r(&committer_time, &tm) == NULL)
1837 return got_error_from_errno("gmtime_r");
1839 s = asctime_r(&tm, datebuf);
1840 if (s == NULL)
1841 return got_error_from_errno("asctime_r");
1843 if (asprintf(repo_age, "%s UTC", datebuf) == -1)
1844 return got_error_from_errno("asprintf");
1845 break;
1846 case TM_RFC822:
1847 if (gmtime_r(&committer_time, &tm) == NULL)
1848 return got_error_from_errno("gmtime_r");
1850 r = strftime(datebuf, sizeof(datebuf),
1851 "%a, %d %b %Y %H:%M:%S GMT", &tm);
1852 if (r == 0)
1853 return got_error(GOT_ERR_NO_SPACE);
1855 *repo_age = strdup(datebuf);
1856 if (*repo_age == NULL)
1857 return got_error_from_errno("asprintf");
1858 break;
1860 return NULL;