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"
23 #include <netinet/in.h>
24 #include <sys/queue.h>
26 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
55 static const struct querystring_keys querystring_keys
[] = {
60 { "headref", HEADREF
},
61 { "index_page", INDEX_PAGE
},
66 static const struct action_keys action_keys
[] = {
69 { "blobraw", BLOBRAW
},
71 { "commits", COMMITS
},
75 { "summary", SUMMARY
},
82 static const struct got_error
*gotweb_init_querystring(struct querystring
**);
83 static const struct got_error
*gotweb_parse_querystring(struct querystring
**,
85 static const struct got_error
*gotweb_assign_querystring(struct querystring
**,
87 static const struct got_error
*gotweb_render_index(struct request
*);
88 static const struct got_error
*gotweb_init_repo_dir(struct repo_dir
**,
90 static const struct got_error
*gotweb_load_got_path(struct request
*c
,
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
*,
97 static void gotweb_free_querystring(struct querystring
*);
98 static void gotweb_free_repo_dir(struct repo_dir
*);
100 struct server
*gotweb_get_server(uint8_t *, uint8_t *);
103 gotweb_reply(struct request
*c
, int status
, const char *ctype
,
104 struct gotweb_url
*location
)
108 if (status
!= 200 && fcgi_printf(c
, "Status: %d\r\n", status
) == -1)
112 if (fcgi_puts(c
->tp
, "Location: ") == -1 ||
113 gotweb_render_url(c
, location
) == -1 ||
114 fcgi_puts(c
->tp
, "\r\n") == -1)
118 csp
= "Content-Security-Policy: default-src 'self'; "
119 "script-src 'none'; object-src 'none';\r\n";
120 if (fcgi_puts(c
->tp
, csp
) == -1)
123 if (ctype
&& fcgi_printf(c
, "Content-Type: %s\r\n", ctype
) == -1)
126 return fcgi_puts(c
->tp
, "\r\n");
130 gotweb_reply_file(struct request
*c
, const char *ctype
, const char *file
,
135 r
= fcgi_printf(c
, "Content-Disposition: attachment; "
136 "filename=%s%s\r\n", file
, suffix
? suffix
: "");
139 return gotweb_reply(c
, 200, ctype
, NULL
);
143 gotweb_process_request(struct request
*c
)
145 const struct got_error
*error
= NULL
, *error2
= NULL
;
146 struct got_blob_object
*blob
= NULL
;
147 struct server
*srv
= NULL
;
148 struct querystring
*qs
= NULL
;
149 struct repo_dir
*repo_dir
= NULL
;
150 struct got_reflist_head refs
;
152 uint8_t err
[] = "gotwebd experienced an error: ";
153 int r
, html
= 0, fd
= -1;
157 /* init the transport */
158 error
= gotweb_init_transport(&c
->t
);
160 log_warnx("%s: %s", __func__
, error
->msg
);
163 /* don't process any further if client disconnected */
164 if (c
->sock
->client_status
== CLIENT_DISCONNECT
)
166 /* get the gotwebd server */
167 srv
= gotweb_get_server(c
->server_name
, c
->http_host
);
169 log_warnx("%s: error server is NULL", __func__
);
173 /* parse our querystring */
174 error
= gotweb_init_querystring(&qs
);
176 log_warnx("%s: %s", __func__
, error
->msg
);
180 error
= gotweb_parse_querystring(&qs
, c
->querystring
);
182 log_warnx("%s: %s", __func__
, error
->msg
);
187 * certain actions require a commit id in the querystring. this stops
188 * bad actors from exploiting this by manually manipulating the
192 if (qs
->action
== BLAME
|| qs
->action
== BLOB
||
193 qs
->action
== BLOBRAW
|| qs
->action
== DIFF
) {
194 if (qs
->commit
== NULL
) {
195 error2
= got_error(GOT_ERR_QUERYSTRING
);
200 if (qs
->action
!= INDEX
) {
201 error
= gotweb_init_repo_dir(&repo_dir
, qs
->path
);
204 error
= gotweb_load_got_path(c
, repo_dir
);
205 c
->t
->repo_dir
= repo_dir
;
206 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
)
210 if (qs
->action
== BLOBRAW
) {
215 error
= got_get_repo_commits(c
, 1);
219 error2
= got_open_blob_for_output(&blob
, &fd
, &binary
, c
);
224 r
= gotweb_reply_file(c
, "application/octet-stream",
227 r
= gotweb_reply(c
, 200, "text/plain", NULL
);
232 error
= got_object_blob_read_block(&len
, blob
);
237 buf
= got_object_blob_get_read_buf(blob
);
238 if (fcgi_gen_binary_response(c
, buf
, len
) == -1)
245 if (qs
->action
== BLOB
) {
247 struct gotweb_url url
= {
252 .commit
= qs
->commit
,
253 .folder
= qs
->folder
,
257 error
= got_get_repo_commits(c
, 1);
261 error2
= got_open_blob_for_output(&blob
, &fd
, &binary
, c
);
265 gotweb_reply(c
, 302, NULL
, &url
);
270 if (qs
->action
== RSS
) {
271 const char *ctype
= "application/rss+xml;charset=utf-8";
273 if (gotweb_reply_file(c
, ctype
, repo_dir
->name
, ".rss") == -1)
276 error
= got_get_repo_tags(c
, D_MAXSLCOMMDISP
);
278 log_warnx("%s: %s", __func__
, error
->msg
);
281 if (gotweb_render_rss(c
->tp
) == -1)
287 if (gotweb_reply(c
, 200, "text/html", NULL
) == -1)
291 if (gotweb_render_header(c
->tp
) == -1)
301 error
= got_get_repo_commits(c
, 1);
303 log_warnx("%s: %s", __func__
, error
->msg
);
306 if (gotweb_render_blame(c
->tp
) == -1)
310 if (gotweb_render_blob(c
->tp
, blob
) == -1)
314 if (gotweb_render_briefs(c
->tp
) == -1)
318 error
= got_get_repo_commits(c
, srv
->max_commits_display
);
320 log_warnx("%s: %s", __func__
, error
->msg
);
323 if (gotweb_render_commits(c
->tp
) == -1)
327 error
= got_get_repo_commits(c
, 1);
329 log_warnx("%s: %s", __func__
, error
->msg
);
332 error
= got_open_diff_for_output(&fp
, &fd
, c
);
334 log_warnx("%s: %s", __func__
, error
->msg
);
337 if (gotweb_render_diff(c
->tp
, fp
) == -1)
341 error
= gotweb_render_index(c
);
343 log_warnx("%s: %s", __func__
, error
->msg
);
348 error
= got_ref_list(&refs
, c
->t
->repo
, "refs/heads",
349 got_ref_cmp_by_name
, NULL
);
351 log_warnx("%s: got_ref_list: %s", __func__
,
356 error
= got_get_repo_tags(c
, D_MAXSLCOMMDISP
);
358 log_warnx("%s: got_get_repo_tags: %s", __func__
,
362 qs
->action
= SUMMARY
;
363 if (gotweb_render_summary(c
->tp
, &refs
) == -1)
367 error
= got_get_repo_tags(c
, 1);
369 log_warnx("%s: %s", __func__
, error
->msg
);
372 if (c
->t
->tag_count
== 0) {
373 error
= got_error_msg(GOT_ERR_BAD_OBJ_ID
,
377 if (gotweb_render_tag(c
->tp
) == -1)
381 error
= got_get_repo_tags(c
, srv
->max_commits_display
);
383 log_warnx("%s: %s", __func__
, error
->msg
);
386 if (gotweb_render_tags(c
->tp
) == -1)
390 error
= got_get_repo_commits(c
, 1);
392 log_warnx("%s: %s", __func__
, error
->msg
);
395 if (gotweb_render_tree(c
->tp
) == -1)
400 r
= fcgi_printf(c
, "<div id='err_content'>%s</div>\n",
401 "Erorr: Bad Querystring");
409 if (html
&& fcgi_printf(c
, "<div id='err_content'>") == -1)
411 if (fcgi_printf(c
, "\n%s", err
) == -1)
414 if (fcgi_printf(c
, "%s", error
->msg
) == -1)
417 if (fcgi_printf(c
, "see daemon logs for details") == -1)
420 if (html
&& fcgi_printf(c
, "</div>\n") == -1)
424 got_object_blob_close(blob
);
426 error
= got_gotweb_flushfile(fp
, fd
);
428 log_warnx("%s: got_gotweb_flushfile failure: %s",
429 __func__
, error
->msg
);
434 if (html
&& srv
!= NULL
)
435 gotweb_render_footer(c
->tp
);
437 got_ref_list_free(&refs
);
441 gotweb_get_server(uint8_t *server_name
, uint8_t *subdomain
)
443 struct server
*srv
= NULL
;
445 /* check against the server name first */
446 if (strlen(server_name
) > 0)
447 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
448 if (strcmp(srv
->name
, server_name
) == 0)
451 /* check against subdomain second */
452 if (strlen(subdomain
) > 0)
453 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
454 if (strcmp(srv
->name
, subdomain
) == 0)
457 /* if those fail, send first server */
458 TAILQ_FOREACH(srv
, &gotwebd_env
->servers
, entry
)
465 const struct got_error
*
466 gotweb_init_transport(struct transport
**t
)
468 const struct got_error
*error
= NULL
;
470 *t
= calloc(1, sizeof(**t
));
472 return got_error_from_errno2("%s: calloc", __func__
);
474 TAILQ_INIT(&(*t
)->repo_commits
);
475 TAILQ_INIT(&(*t
)->repo_tags
);
478 (*t
)->repo_dir
= NULL
;
480 (*t
)->next_id
= NULL
;
481 (*t
)->prev_id
= NULL
;
488 static const struct got_error
*
489 gotweb_init_querystring(struct querystring
**qs
)
491 const struct got_error
*error
= NULL
;
493 *qs
= calloc(1, sizeof(**qs
));
495 return got_error_from_errno2("%s: calloc", __func__
);
497 (*qs
)->headref
= strdup("HEAD");
498 if ((*qs
)->headref
== NULL
) {
501 return got_error_from_errno2("%s: strdup", __func__
);
504 (*qs
)->action
= INDEX
;
505 (*qs
)->commit
= NULL
;
507 (*qs
)->folder
= NULL
;
508 (*qs
)->index_page
= 0;
514 static const struct got_error
*
515 gotweb_parse_querystring(struct querystring
**qs
, char *qst
)
517 const struct got_error
*error
= NULL
;
518 char *tok1
= NULL
, *tok1_pair
= NULL
, *tok1_end
= NULL
;
519 char *tok2
= NULL
, *tok2_pair
= NULL
, *tok2_end
= NULL
;
526 return got_error_from_errno2("%s: strdup", __func__
);
531 while (tok1_pair
!= NULL
) {
532 strsep(&tok1_end
, "&");
534 tok2
= strdup(tok1_pair
);
537 return got_error_from_errno2("%s: strdup", __func__
);
543 while (tok2_pair
!= NULL
) {
544 strsep(&tok2_end
, "=");
546 error
= gotweb_assign_querystring(qs
, tok2_pair
,
551 tok2_pair
= tok2_end
;
554 tok1_pair
= tok1_end
;
565 * Adapted from usr.sbin/httpd/httpd.c url_decode.
567 static const struct got_error
*
568 gotweb_urldecode(char *url
)
580 /* Encoding character is followed by two hex chars */
581 if (!isxdigit((unsigned char)p
[1]) ||
582 !isxdigit((unsigned char)p
[2]) ||
583 (p
[1] == '0' && p
[2] == '0'))
584 return got_error(GOT_ERR_BAD_QUERYSTRING
);
590 * We don't have to validate "hex" because it is
591 * guaranteed to include two hex chars followed by nul.
593 x
= strtoul(hex
, NULL
, 16);
609 static const struct got_error
*
610 gotweb_assign_querystring(struct querystring
**qs
, char *key
, char *value
)
612 const struct got_error
*error
= NULL
;
616 error
= gotweb_urldecode(value
);
620 for (el_cnt
= 0; el_cnt
< QSELEM__MAX
; el_cnt
++) {
621 if (strcmp(key
, querystring_keys
[el_cnt
].name
) != 0)
624 switch (querystring_keys
[el_cnt
].element
) {
626 for (a_cnt
= 0; a_cnt
< ACTIONS__MAX
; a_cnt
++) {
627 if (strcmp(value
, action_keys
[a_cnt
].name
) != 0)
629 else if (strcmp(value
,
630 action_keys
[a_cnt
].name
) == 0){
632 action_keys
[a_cnt
].action
;
640 (*qs
)->commit
= strdup(value
);
641 if ((*qs
)->commit
== NULL
) {
642 error
= got_error_from_errno2("%s: strdup",
648 (*qs
)->file
= strdup(value
);
649 if ((*qs
)->file
== NULL
) {
650 error
= got_error_from_errno2("%s: strdup",
656 (*qs
)->folder
= strdup(value
);
657 if ((*qs
)->folder
== NULL
) {
658 error
= got_error_from_errno2("%s: strdup",
664 free((*qs
)->headref
);
665 (*qs
)->headref
= strdup(value
);
666 if ((*qs
)->headref
== NULL
) {
667 error
= got_error_from_errno2("%s: strdup",
673 if (strlen(value
) == 0)
675 (*qs
)->index_page
= strtonum(value
, INT64_MIN
,
678 error
= got_error_from_errno3("%s: strtonum %s",
682 if ((*qs
)->index_page
< 0)
683 (*qs
)->index_page
= 0;
686 (*qs
)->path
= strdup(value
);
687 if ((*qs
)->path
== NULL
) {
688 error
= got_error_from_errno2("%s: strdup",
694 if (strlen(value
) == 0)
696 (*qs
)->page
= strtonum(value
, INT64_MIN
,
699 error
= got_error_from_errno3("%s: strtonum %s",
715 gotweb_free_repo_tag(struct repo_tag
*rt
)
720 free(rt
->tag_commit
);
721 free(rt
->commit_msg
);
728 gotweb_free_repo_commit(struct repo_commit
*rc
)
738 free(rc
->commit_msg
);
744 gotweb_free_querystring(struct querystring
*qs
)
757 gotweb_free_repo_dir(struct repo_dir
*repo_dir
)
759 if (repo_dir
!= NULL
) {
760 free(repo_dir
->name
);
761 free(repo_dir
->owner
);
762 free(repo_dir
->description
);
764 free(repo_dir
->path
);
770 gotweb_free_transport(struct transport
*t
)
772 struct repo_commit
*rc
= NULL
, *trc
= NULL
;
773 struct repo_tag
*rt
= NULL
, *trt
= NULL
;
775 TAILQ_FOREACH_SAFE(rc
, &t
->repo_commits
, entry
, trc
) {
776 TAILQ_REMOVE(&t
->repo_commits
, rc
, entry
);
777 gotweb_free_repo_commit(rc
);
779 TAILQ_FOREACH_SAFE(rt
, &t
->repo_tags
, entry
, trt
) {
780 TAILQ_REMOVE(&t
->repo_tags
, rt
, entry
);
781 gotweb_free_repo_tag(rt
);
783 gotweb_free_repo_dir(t
->repo_dir
);
784 gotweb_free_querystring(t
->qs
);
792 gotweb_get_navs(struct request
*c
, struct gotweb_url
*prev
, int *have_prev
,
793 struct gotweb_url
*next
, int *have_next
)
795 struct transport
*t
= c
->t
;
796 struct querystring
*qs
= t
->qs
;
797 struct server
*srv
= c
->srv
;
799 *have_prev
= *have_next
= 0;
803 if (qs
->index_page
> 0) {
805 *prev
= (struct gotweb_url
){
807 .index_page
= qs
->index_page
- 1,
811 if (t
->next_disp
== srv
->max_repos_display
&&
812 t
->repos_total
!= (qs
->index_page
+ 1) *
813 srv
->max_repos_display
) {
815 *next
= (struct gotweb_url
){
817 .index_page
= qs
->index_page
+ 1,
823 if (t
->prev_id
&& qs
->commit
!= NULL
&&
824 strcmp(qs
->commit
, t
->prev_id
) != 0) {
826 *prev
= (struct gotweb_url
){
829 .page
= qs
->page
- 1,
831 .commit
= t
->prev_id
,
832 .headref
= qs
->headref
,
837 *next
= (struct gotweb_url
){
840 .page
= qs
->page
+ 1,
842 .commit
= t
->next_id
,
843 .headref
= qs
->headref
,
850 static const struct got_error
*
851 gotweb_render_index(struct request
*c
)
853 const struct got_error
*error
= NULL
;
854 struct server
*srv
= c
->srv
;
855 struct transport
*t
= c
->t
;
856 struct querystring
*qs
= t
->qs
;
857 struct repo_dir
*repo_dir
= NULL
;
858 struct dirent
**sd_dent
= NULL
;
859 unsigned int d_cnt
, d_i
, d_disp
= 0;
860 unsigned int d_skipped
= 0;
863 d_cnt
= scandir(srv
->repos_path
, &sd_dent
, NULL
, alphasort
);
866 error
= got_error_from_errno2("scandir", srv
->repos_path
);
870 if (gotweb_render_repo_table_hdr(c
->tp
) == -1)
873 for (d_i
= 0; d_i
< d_cnt
; d_i
++) {
874 if (srv
->max_repos
> 0 && t
->prev_disp
== srv
->max_repos
)
877 if (strcmp(sd_dent
[d_i
]->d_name
, ".") == 0 ||
878 strcmp(sd_dent
[d_i
]->d_name
, "..") == 0) {
883 error
= got_path_dirent_type(&type
, srv
->repos_path
,
887 if (type
!= DT_DIR
) {
892 if (qs
->index_page
> 0 && (qs
->index_page
*
893 srv
->max_repos_display
) > t
->prev_disp
) {
898 error
= gotweb_init_repo_dir(&repo_dir
, sd_dent
[d_i
]->d_name
);
902 error
= gotweb_load_got_path(c
, repo_dir
);
903 if (error
&& error
->code
== GOT_ERR_NOT_GIT_REPO
) {
905 gotweb_free_repo_dir(repo_dir
);
910 if (error
&& error
->code
!= GOT_ERR_LONELY_PACKIDX
)
916 if (gotweb_render_repo_fragment(c
->tp
, repo_dir
) == -1)
919 gotweb_free_repo_dir(repo_dir
);
922 if (d_disp
== srv
->max_repos_display
)
925 t
->repos_total
= d_cnt
- d_skipped
;
927 if (srv
->max_repos_display
== 0)
929 if (srv
->max_repos
> 0 && srv
->max_repos
< srv
->max_repos_display
)
931 if (t
->repos_total
<= srv
->max_repos
||
932 t
->repos_total
<= srv
->max_repos_display
)
935 if (gotweb_render_navs(c
->tp
) == -1)
939 for (d_i
= 0; d_i
< d_cnt
; d_i
++)
947 should_urlencode(int c
)
949 if (c
<= ' ' || c
>= 127)
973 /* needed because the URLs are embedded into the HTML */
982 gotweb_urlencode(const char *str
)
990 for (s
= str
; *s
; ++s
) {
992 if (should_urlencode(*s
))
996 escaped
= calloc(1, len
+ 1);
1001 for (s
= str
; *s
; ++s
) {
1002 if (should_urlencode(*s
)) {
1003 a
= (*s
& 0xF0) >> 4;
1007 escaped
[i
++] = a
<= 9 ? ('0' + a
) : ('7' + a
);
1008 escaped
[i
++] = b
<= 9 ? ('0' + b
) : ('7' + b
);
1017 gotweb_action_name(int action
)
1052 gotweb_render_url(struct request
*c
, struct gotweb_url
*url
)
1054 const char *sep
= "?", *action
;
1058 action
= gotweb_action_name(url
->action
);
1059 if (action
!= NULL
) {
1060 if (fcgi_printf(c
, "?action=%s", action
) == -1)
1066 if (fcgi_printf(c
, "%scommit=%s", sep
, url
->commit
) == -1)
1072 if (fcgi_printf(c
, "%sprevid=%s", sep
, url
->previd
) == -1)
1078 if (fcgi_printf(c
, "%sprevset=%s", sep
, url
->prevset
) == -1)
1084 tmp
= gotweb_urlencode(url
->file
);
1087 r
= fcgi_printf(c
, "%sfile=%s", sep
, tmp
);
1095 tmp
= gotweb_urlencode(url
->folder
);
1098 r
= fcgi_printf(c
, "%sfolder=%s", sep
, tmp
);
1106 tmp
= gotweb_urlencode(url
->headref
);
1109 r
= fcgi_printf(c
, "%sheadref=%s", sep
, url
->headref
);
1116 if (url
->index_page
!= -1) {
1117 if (fcgi_printf(c
, "%sindex_page=%d", sep
,
1118 url
->index_page
) == -1)
1124 tmp
= gotweb_urlencode(url
->path
);
1127 r
= fcgi_printf(c
, "%spath=%s", sep
, tmp
);
1134 if (url
->page
!= -1) {
1135 if (fcgi_printf(c
, "%spage=%d", sep
, url
->page
) == -1)
1144 gotweb_render_absolute_url(struct request
*c
, struct gotweb_url
*url
)
1146 struct template *tp
= c
->tp
;
1147 const char *proto
= c
->https
? "https" : "http";
1149 if (fcgi_puts(tp
, proto
) == -1 ||
1150 fcgi_puts(tp
, "://") == -1 ||
1151 tp_htmlescape(tp
, c
->server_name
) == -1 ||
1152 tp_htmlescape(tp
, c
->document_uri
) == -1)
1155 return gotweb_render_url(c
, url
);
1158 static struct got_repository
*
1159 find_cached_repo(struct server
*srv
, const char *path
)
1163 for (i
= 0; i
< srv
->ncached_repos
; i
++) {
1164 if (strcmp(srv
->cached_repos
[i
].path
, path
) == 0)
1165 return srv
->cached_repos
[i
].repo
;
1171 static const struct got_error
*
1172 cache_repo(struct got_repository
**new, struct server
*srv
,
1173 struct repo_dir
*repo_dir
, struct socket
*sock
)
1175 const struct got_error
*error
= NULL
;
1176 struct got_repository
*repo
;
1177 struct cached_repo
*cr
;
1180 if (srv
->ncached_repos
>= GOTWEBD_REPO_CACHESIZE
) {
1181 cr
= &srv
->cached_repos
[srv
->ncached_repos
- 1];
1182 error
= got_repo_close(cr
->repo
);
1183 memset(cr
, 0, sizeof(*cr
));
1184 srv
->ncached_repos
--;
1187 memmove(&srv
->cached_repos
[1], &srv
->cached_repos
[0],
1188 srv
->ncached_repos
* sizeof(srv
->cached_repos
[0]));
1189 cr
= &srv
->cached_repos
[0];
1192 cr
= &srv
->cached_repos
[srv
->ncached_repos
];
1195 error
= got_repo_open(&repo
, repo_dir
->path
, NULL
, sock
->pack_fds
);
1198 memmove(&srv
->cached_repos
[0], &srv
->cached_repos
[1],
1199 srv
->ncached_repos
* sizeof(srv
->cached_repos
[0]));
1204 if (strlcpy(cr
->path
, repo_dir
->path
, sizeof(cr
->path
))
1205 >= sizeof(cr
->path
)) {
1207 memmove(&srv
->cached_repos
[0], &srv
->cached_repos
[1],
1208 srv
->ncached_repos
* sizeof(srv
->cached_repos
[0]));
1210 return got_error(GOT_ERR_NO_SPACE
);
1214 srv
->ncached_repos
++;
1219 static const struct got_error
*
1220 gotweb_load_got_path(struct request
*c
, struct repo_dir
*repo_dir
)
1222 const struct got_error
*error
= NULL
;
1223 struct socket
*sock
= c
->sock
;
1224 struct server
*srv
= c
->srv
;
1225 struct transport
*t
= c
->t
;
1226 struct got_repository
*repo
= NULL
;
1230 if (asprintf(&dir_test
, "%s/%s/%s", srv
->repos_path
, repo_dir
->name
,
1231 GOTWEB_GIT_DIR
) == -1)
1232 return got_error_from_errno("asprintf");
1234 dt
= opendir(dir_test
);
1238 repo_dir
->path
= dir_test
;
1243 if (asprintf(&dir_test
, "%s/%s", srv
->repos_path
,
1244 repo_dir
->name
) == -1)
1245 return got_error_from_errno("asprintf");
1247 dt
= opendir(dir_test
);
1249 error
= got_error_path(repo_dir
->name
, GOT_ERR_NOT_GIT_REPO
);
1252 repo_dir
->path
= dir_test
;
1257 if (srv
->respect_exportok
&&
1258 faccessat(dirfd(dt
), "git-daemon-export-ok", F_OK
, 0) == -1) {
1259 error
= got_error_path(repo_dir
->name
, GOT_ERR_NOT_GIT_REPO
);
1263 repo
= find_cached_repo(srv
, repo_dir
->path
);
1265 error
= cache_repo(&repo
, srv
, repo_dir
, sock
);
1270 error
= gotweb_get_repo_description(&repo_dir
->description
, srv
,
1271 repo_dir
->path
, dirfd(dt
));
1274 error
= got_get_repo_owner(&repo_dir
->owner
, c
);
1277 error
= got_get_repo_age(&repo_dir
->age
, c
, NULL
);
1280 error
= gotweb_get_clone_url(&repo_dir
->url
, srv
, repo_dir
->path
,
1284 if (dt
!= NULL
&& closedir(dt
) == EOF
&& error
== NULL
)
1285 error
= got_error_from_errno("closedir");
1289 static const struct got_error
*
1290 gotweb_init_repo_dir(struct repo_dir
**repo_dir
, const char *dir
)
1292 const struct got_error
*error
;
1294 *repo_dir
= calloc(1, sizeof(**repo_dir
));
1295 if (*repo_dir
== NULL
)
1296 return got_error_from_errno("calloc");
1298 if (asprintf(&(*repo_dir
)->name
, "%s", dir
) == -1) {
1299 error
= got_error_from_errno("asprintf");
1304 (*repo_dir
)->owner
= NULL
;
1305 (*repo_dir
)->description
= NULL
;
1306 (*repo_dir
)->url
= NULL
;
1307 (*repo_dir
)->path
= NULL
;
1312 static const struct got_error
*
1313 gotweb_get_repo_description(char **description
, struct server
*srv
,
1314 const char *dirpath
, int dir
)
1316 const struct got_error
*error
= NULL
;
1321 *description
= NULL
;
1322 if (srv
->show_repo_description
== 0)
1325 fd
= openat(dir
, "description", O_RDONLY
);
1327 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1328 error
= got_error_from_errno_fmt("openat %s/%s",
1329 dirpath
, "description");
1334 if (fstat(fd
, &sb
) == -1) {
1335 error
= got_error_from_errno_fmt("fstat %s/%s",
1336 dirpath
, "description");
1341 if (len
> GOTWEBD_MAXDESCRSZ
- 1)
1342 len
= GOTWEBD_MAXDESCRSZ
- 1;
1344 *description
= calloc(len
+ 1, sizeof(**description
));
1345 if (*description
== NULL
) {
1346 error
= got_error_from_errno("calloc");
1350 if (read(fd
, *description
, len
) == -1)
1351 error
= got_error_from_errno("read");
1353 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1354 error
= got_error_from_errno("close");
1358 static const struct got_error
*
1359 gotweb_get_clone_url(char **url
, struct server
*srv
, const char *dirpath
,
1362 const struct got_error
*error
= NULL
;
1368 if (srv
->show_repo_cloneurl
== 0)
1371 fd
= openat(dir
, "cloneurl", O_RDONLY
);
1373 if (errno
!= ENOENT
&& errno
!= EACCES
) {
1374 error
= got_error_from_errno_fmt("openat %s/%s",
1375 dirpath
, "cloneurl");
1380 if (fstat(fd
, &sb
) == -1) {
1381 error
= got_error_from_errno_fmt("fstat %s/%s",
1382 dirpath
, "cloneurl");
1387 if (len
> GOTWEBD_MAXCLONEURLSZ
- 1)
1388 len
= GOTWEBD_MAXCLONEURLSZ
- 1;
1390 *url
= calloc(len
+ 1, sizeof(**url
));
1392 error
= got_error_from_errno("calloc");
1396 if (read(fd
, *url
, len
) == -1)
1397 error
= got_error_from_errno("read");
1399 if (fd
!= -1 && close(fd
) == -1 && error
== NULL
)
1400 error
= got_error_from_errno("close");
1405 gotweb_render_age(struct template *tp
, time_t committer_time
, int ref_tm
)
1407 struct request
*c
= tp
->tp_arg
;
1409 long long diff_time
;
1410 const char *years
= "years ago", *months
= "months ago";
1411 const char *weeks
= "weeks ago", *days
= "days ago";
1412 const char *hours
= "hours ago", *minutes
= "minutes ago";
1413 const char *seconds
= "seconds ago", *now
= "right now";
1420 diff_time
= time(NULL
) - committer_time
;
1421 if (diff_time
> 60 * 60 * 24 * 365 * 2) {
1422 if (fcgi_printf(c
, "%lld %s",
1423 (diff_time
/ 60 / 60 / 24 / 365), years
) == -1)
1425 } else if (diff_time
> 60 * 60 * 24 * (365 / 12) * 2) {
1426 if (fcgi_printf(c
, "%lld %s",
1427 (diff_time
/ 60 / 60 / 24 / (365 / 12)),
1430 } else if (diff_time
> 60 * 60 * 24 * 7 * 2) {
1431 if (fcgi_printf(c
, "%lld %s",
1432 (diff_time
/ 60 / 60 / 24 / 7), weeks
) == -1)
1434 } else if (diff_time
> 60 * 60 * 24 * 2) {
1435 if (fcgi_printf(c
, "%lld %s",
1436 (diff_time
/ 60 / 60 / 24), days
) == -1)
1438 } else if (diff_time
> 60 * 60 * 2) {
1439 if (fcgi_printf(c
, "%lld %s",
1440 (diff_time
/ 60 / 60), hours
) == -1)
1442 } else if (diff_time
> 60 * 2) {
1443 if (fcgi_printf(c
, "%lld %s", (diff_time
/ 60),
1446 } else if (diff_time
> 2) {
1447 if (fcgi_printf(c
, "%lld %s", diff_time
,
1451 if (fcgi_puts(tp
, now
) == -1)
1456 if (gmtime_r(&committer_time
, &tm
) == NULL
)
1459 s
= asctime_r(&tm
, datebuf
);
1463 if (fcgi_puts(tp
, datebuf
) == -1 ||
1464 fcgi_puts(tp
, " UTC") == -1)
1468 if (gmtime_r(&committer_time
, &tm
) == NULL
)
1471 r
= strftime(datebuf
, sizeof(datebuf
),
1472 "%a, %d %b %Y %H:%M:%S GMT", &tm
);
1476 if (fcgi_puts(tp
, datebuf
) == -1)