2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 58381f70 2022-09-03 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
9 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
10 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
12 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 a596b957 2022-07-14 tracey #include <net/if.h>
22 a596b957 2022-07-14 tracey #include <netinet/in.h>
23 b4c20a19 2022-07-15 naddy #include <sys/queue.h>
24 a596b957 2022-07-14 tracey #include <sys/stat.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
27 58381f70 2022-09-03 op #include <ctype.h>
28 a596b957 2022-07-14 tracey #include <dirent.h>
29 a596b957 2022-07-14 tracey #include <errno.h>
30 a596b957 2022-07-14 tracey #include <event.h>
31 3b81530f 2022-11-22 op #include <fcntl.h>
32 a596b957 2022-07-14 tracey #include <imsg.h>
33 a596b957 2022-07-14 tracey #include <sha1.h>
34 a596b957 2022-07-14 tracey #include <stdio.h>
35 a596b957 2022-07-14 tracey #include <stdlib.h>
36 a596b957 2022-07-14 tracey #include <string.h>
37 a596b957 2022-07-14 tracey #include <unistd.h>
39 a596b957 2022-07-14 tracey #include "got_error.h"
40 a596b957 2022-07-14 tracey #include "got_object.h"
41 a596b957 2022-07-14 tracey #include "got_reference.h"
42 a596b957 2022-07-14 tracey #include "got_repository.h"
43 a596b957 2022-07-14 tracey #include "got_path.h"
44 a596b957 2022-07-14 tracey #include "got_cancel.h"
45 a596b957 2022-07-14 tracey #include "got_worktree.h"
46 a596b957 2022-07-14 tracey #include "got_diff.h"
47 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
48 a596b957 2022-07-14 tracey #include "got_blame.h"
49 a596b957 2022-07-14 tracey #include "got_privsep.h"
51 a596b957 2022-07-14 tracey #include "proc.h"
52 a596b957 2022-07-14 tracey #include "gotwebd.h"
54 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
55 a596b957 2022-07-14 tracey { "action", ACTION },
56 a596b957 2022-07-14 tracey { "commit", COMMIT },
57 a596b957 2022-07-14 tracey { "file", RFILE },
58 a596b957 2022-07-14 tracey { "folder", FOLDER },
59 a596b957 2022-07-14 tracey { "headref", HEADREF },
60 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
61 a596b957 2022-07-14 tracey { "path", PATH },
62 a596b957 2022-07-14 tracey { "page", PAGE },
65 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
66 a596b957 2022-07-14 tracey { "blame", BLAME },
67 a596b957 2022-07-14 tracey { "blob", BLOB },
68 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
69 a596b957 2022-07-14 tracey { "commits", COMMITS },
70 a596b957 2022-07-14 tracey { "diff", DIFF },
71 a596b957 2022-07-14 tracey { "error", ERR },
72 a596b957 2022-07-14 tracey { "index", INDEX },
73 a596b957 2022-07-14 tracey { "summary", SUMMARY },
74 a596b957 2022-07-14 tracey { "tag", TAG },
75 a596b957 2022-07-14 tracey { "tags", TAGS },
76 a596b957 2022-07-14 tracey { "tree", TREE },
79 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
80 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
82 a596b957 2022-07-14 tracey static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 a596b957 2022-07-14 tracey char *, char *);
84 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_index(struct request *);
85 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 a596b957 2022-07-14 tracey const char *);
87 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
88 a596b957 2022-07-14 tracey struct repo_dir *);
89 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
90 3b81530f 2022-11-22 op struct server *, const char *, int);
91 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 3b81530f 2022-11-22 op const char *, int);
93 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_blame(struct request *);
94 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_commits(struct request *);
95 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_diff(struct request *);
96 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_summary(struct request *);
97 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tag(struct request *);
98 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tags(struct request *);
99 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tree(struct request *);
100 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_branches(struct request *);
102 ed619ca0 2022-12-14 op const struct got_error *gotweb_render_navs(struct request *);
104 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
105 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
107 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
110 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
112 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
113 a596b957 2022-07-14 tracey struct server *srv = NULL;
114 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
115 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
116 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
117 01498c42 2022-08-19 op int r, html = 0;
119 a596b957 2022-07-14 tracey /* init the transport */
120 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
121 a596b957 2022-07-14 tracey if (error) {
122 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
125 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
126 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
128 a596b957 2022-07-14 tracey /* get the gotwebd server */
129 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
130 a596b957 2022-07-14 tracey if (srv == NULL) {
131 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
132 a596b957 2022-07-14 tracey goto err;
134 a596b957 2022-07-14 tracey c->srv = srv;
135 a596b957 2022-07-14 tracey /* parse our querystring */
136 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
137 a596b957 2022-07-14 tracey if (error) {
138 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
139 a596b957 2022-07-14 tracey goto err;
141 a596b957 2022-07-14 tracey c->t->qs = qs;
142 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
143 a596b957 2022-07-14 tracey if (error) {
144 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
145 a596b957 2022-07-14 tracey goto err;
149 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
150 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
151 a596b957 2022-07-14 tracey * querystring.
154 a596b957 2022-07-14 tracey if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
155 a596b957 2022-07-14 tracey qs->action == DIFF)) {
156 a596b957 2022-07-14 tracey error2 = got_error(GOT_ERR_QUERYSTRING);
157 a596b957 2022-07-14 tracey goto render;
160 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
161 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
162 a596b957 2022-07-14 tracey if (error)
163 a596b957 2022-07-14 tracey goto done;
164 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
165 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
166 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
167 a596b957 2022-07-14 tracey goto err;
170 a596b957 2022-07-14 tracey /* render top of page */
171 a596b957 2022-07-14 tracey if (qs != NULL && qs->action == BLOB) {
172 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
173 a596b957 2022-07-14 tracey if (error)
174 a596b957 2022-07-14 tracey goto done;
175 a596b957 2022-07-14 tracey error = got_output_file_blob(c);
176 a596b957 2022-07-14 tracey if (error) {
177 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
178 a596b957 2022-07-14 tracey goto err;
180 a596b957 2022-07-14 tracey goto done;
184 6970304f 2022-12-04 op error = gotweb_render_content_type(c, "text/html");
186 6970304f 2022-12-04 op log_warnx("%s: %s", __func__, error->msg);
191 ed619ca0 2022-12-14 op if (gotweb_render_header(c->tp) == -1)
192 a596b957 2022-07-14 tracey goto err;
194 a596b957 2022-07-14 tracey if (error2) {
195 a596b957 2022-07-14 tracey error = error2;
196 a596b957 2022-07-14 tracey goto err;
199 a596b957 2022-07-14 tracey switch(qs->action) {
200 a596b957 2022-07-14 tracey case BLAME:
201 a596b957 2022-07-14 tracey error = gotweb_render_blame(c);
202 a596b957 2022-07-14 tracey if (error) {
203 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
204 a596b957 2022-07-14 tracey goto err;
207 a596b957 2022-07-14 tracey case BRIEFS:
208 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
209 a596b957 2022-07-14 tracey goto err;
211 a596b957 2022-07-14 tracey case COMMITS:
212 a596b957 2022-07-14 tracey error = gotweb_render_commits(c);
213 a596b957 2022-07-14 tracey if (error) {
214 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
215 a596b957 2022-07-14 tracey goto err;
218 a596b957 2022-07-14 tracey case DIFF:
219 a596b957 2022-07-14 tracey error = gotweb_render_diff(c);
220 a596b957 2022-07-14 tracey if (error) {
221 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
222 a596b957 2022-07-14 tracey goto err;
225 a596b957 2022-07-14 tracey case INDEX:
226 a596b957 2022-07-14 tracey error = gotweb_render_index(c);
227 a596b957 2022-07-14 tracey if (error) {
228 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
229 a596b957 2022-07-14 tracey goto err;
232 a596b957 2022-07-14 tracey case SUMMARY:
233 a596b957 2022-07-14 tracey error = gotweb_render_summary(c);
234 a596b957 2022-07-14 tracey if (error) {
235 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
236 a596b957 2022-07-14 tracey goto err;
239 a596b957 2022-07-14 tracey case TAG:
240 a596b957 2022-07-14 tracey error = gotweb_render_tag(c);
241 a596b957 2022-07-14 tracey if (error) {
242 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
243 a596b957 2022-07-14 tracey goto err;
246 a596b957 2022-07-14 tracey case TAGS:
247 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
248 a596b957 2022-07-14 tracey if (error) {
249 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
250 a596b957 2022-07-14 tracey goto err;
253 a596b957 2022-07-14 tracey case TREE:
254 a596b957 2022-07-14 tracey error = gotweb_render_tree(c);
255 a596b957 2022-07-14 tracey if (error) {
256 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
257 a596b957 2022-07-14 tracey goto err;
260 a596b957 2022-07-14 tracey case ERR:
262 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
263 01498c42 2022-08-19 op "Erorr: Bad Querystring");
265 a596b957 2022-07-14 tracey goto err;
269 a596b957 2022-07-14 tracey goto done;
271 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
273 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
275 a596b957 2022-07-14 tracey if (error) {
276 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
279 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
282 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
285 a596b957 2022-07-14 tracey if (html && srv != NULL)
286 ed619ca0 2022-12-14 op gotweb_render_footer(c->tp);
289 a596b957 2022-07-14 tracey struct server *
290 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
292 a596b957 2022-07-14 tracey struct server *srv = NULL;
294 95a4a5a1 2022-08-30 op /* check against the server name first */
295 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
296 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
297 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
298 a596b957 2022-07-14 tracey goto done;
300 95a4a5a1 2022-08-30 op /* check against subdomain second */
301 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
302 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
303 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
304 a596b957 2022-07-14 tracey goto done;
306 a596b957 2022-07-14 tracey /* if those fail, send first server */
307 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
308 a596b957 2022-07-14 tracey if (srv != NULL)
311 a596b957 2022-07-14 tracey return srv;
314 a596b957 2022-07-14 tracey const struct got_error *
315 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
317 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
319 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
320 a596b957 2022-07-14 tracey if (*t == NULL)
321 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
323 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
324 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
326 a596b957 2022-07-14 tracey (*t)->repo = NULL;
327 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
328 a596b957 2022-07-14 tracey (*t)->qs = NULL;
329 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
330 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
331 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
332 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
334 a596b957 2022-07-14 tracey return error;
337 a596b957 2022-07-14 tracey static const struct got_error *
338 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
340 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
342 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
343 a596b957 2022-07-14 tracey if (*qs == NULL)
344 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
346 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
347 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
350 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
353 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
354 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
355 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
356 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
357 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
358 a596b957 2022-07-14 tracey (*qs)->path = NULL;
360 a596b957 2022-07-14 tracey return error;
363 a596b957 2022-07-14 tracey static const struct got_error *
364 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
366 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
367 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
368 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
370 a596b957 2022-07-14 tracey if (qst == NULL)
371 a596b957 2022-07-14 tracey return error;
373 a596b957 2022-07-14 tracey tok1 = strdup(qst);
374 a596b957 2022-07-14 tracey if (tok1 == NULL)
375 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
377 a596b957 2022-07-14 tracey tok1_pair = tok1;
378 a596b957 2022-07-14 tracey tok1_end = tok1;
380 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
381 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
383 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
384 a596b957 2022-07-14 tracey if (tok2 == NULL) {
385 a596b957 2022-07-14 tracey free(tok1);
386 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
389 a596b957 2022-07-14 tracey tok2_pair = tok2;
390 a596b957 2022-07-14 tracey tok2_end = tok2;
392 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
393 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
394 a596b957 2022-07-14 tracey if (tok2_end) {
395 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
396 a596b957 2022-07-14 tracey tok2_end);
397 a596b957 2022-07-14 tracey if (error)
398 a596b957 2022-07-14 tracey goto err;
400 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
402 a596b957 2022-07-14 tracey free(tok2);
403 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
405 a596b957 2022-07-14 tracey free(tok1);
406 a596b957 2022-07-14 tracey return error;
408 a596b957 2022-07-14 tracey free(tok2);
409 a596b957 2022-07-14 tracey free(tok1);
410 a596b957 2022-07-14 tracey return error;
414 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
416 a596b957 2022-07-14 tracey static const struct got_error *
417 58381f70 2022-09-03 op gotweb_urldecode(char *url)
421 58381f70 2022-09-03 op unsigned long x;
423 58381f70 2022-09-03 op hex[2] = '\0';
426 58381f70 2022-09-03 op while (*p != '\0') {
427 58381f70 2022-09-03 op switch (*p) {
429 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
430 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
431 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
432 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
433 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
435 58381f70 2022-09-03 op hex[0] = p[1];
436 58381f70 2022-09-03 op hex[1] = p[2];
439 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
440 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
442 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
443 58381f70 2022-09-03 op *q = (char)x;
458 58381f70 2022-09-03 op static const struct got_error *
459 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
461 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
462 a596b957 2022-07-14 tracey const char *errstr;
463 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
465 58381f70 2022-09-03 op error = gotweb_urldecode(value);
467 58381f70 2022-09-03 op return error;
469 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
470 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
471 a596b957 2022-07-14 tracey continue;
473 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
474 a596b957 2022-07-14 tracey case ACTION:
475 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
476 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
477 a596b957 2022-07-14 tracey continue;
478 a596b957 2022-07-14 tracey else if (strcmp(value,
479 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
480 a596b957 2022-07-14 tracey (*qs)->action =
481 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
482 a596b957 2022-07-14 tracey goto qa_found;
485 a596b957 2022-07-14 tracey (*qs)->action = ERR;
486 a596b957 2022-07-14 tracey qa_found:
488 a596b957 2022-07-14 tracey case COMMIT:
489 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
490 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
491 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
492 a596b957 2022-07-14 tracey __func__);
493 a596b957 2022-07-14 tracey goto done;
496 a596b957 2022-07-14 tracey case RFILE:
497 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
498 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
499 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
500 a596b957 2022-07-14 tracey __func__);
501 a596b957 2022-07-14 tracey goto done;
504 a596b957 2022-07-14 tracey case FOLDER:
505 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
506 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
507 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
508 a596b957 2022-07-14 tracey __func__);
509 a596b957 2022-07-14 tracey goto done;
512 a596b957 2022-07-14 tracey case HEADREF:
513 f8faf9f1 2022-09-01 op free((*qs)->headref);
514 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
515 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
516 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
517 a596b957 2022-07-14 tracey __func__);
518 a596b957 2022-07-14 tracey goto done;
521 a596b957 2022-07-14 tracey case INDEX_PAGE:
522 a596b957 2022-07-14 tracey if (strlen(value) == 0)
524 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
525 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
526 a596b957 2022-07-14 tracey if (errstr) {
527 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
528 a596b957 2022-07-14 tracey __func__, errstr);
529 a596b957 2022-07-14 tracey goto done;
531 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
532 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
534 a596b957 2022-07-14 tracey case PATH:
535 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
536 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
537 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
538 a596b957 2022-07-14 tracey __func__);
539 a596b957 2022-07-14 tracey goto done;
542 a596b957 2022-07-14 tracey case PAGE:
543 a596b957 2022-07-14 tracey if (strlen(value) == 0)
545 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
546 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
547 a596b957 2022-07-14 tracey if (errstr) {
548 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
549 a596b957 2022-07-14 tracey __func__, errstr);
550 a596b957 2022-07-14 tracey goto done;
552 03f6a843 2022-12-17 op if ((*qs)->page < 0)
553 a596b957 2022-07-14 tracey (*qs)->page = 0;
560 a596b957 2022-07-14 tracey return error;
564 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
566 a596b957 2022-07-14 tracey if (rt != NULL) {
567 a596b957 2022-07-14 tracey free(rt->commit_id);
568 625e5896 2022-09-01 op free(rt->tag_name);
569 625e5896 2022-09-01 op free(rt->tag_commit);
570 625e5896 2022-09-01 op free(rt->commit_msg);
571 a596b957 2022-07-14 tracey free(rt->tagger);
573 a596b957 2022-07-14 tracey free(rt);
577 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
579 a596b957 2022-07-14 tracey if (rc != NULL) {
580 a596b957 2022-07-14 tracey free(rc->path);
581 a596b957 2022-07-14 tracey free(rc->refs_str);
582 a596b957 2022-07-14 tracey free(rc->commit_id);
583 a596b957 2022-07-14 tracey free(rc->parent_id);
584 a596b957 2022-07-14 tracey free(rc->tree_id);
585 a596b957 2022-07-14 tracey free(rc->author);
586 a596b957 2022-07-14 tracey free(rc->committer);
587 a596b957 2022-07-14 tracey free(rc->commit_msg);
589 a596b957 2022-07-14 tracey free(rc);
592 a596b957 2022-07-14 tracey static void
593 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
595 a596b957 2022-07-14 tracey if (qs != NULL) {
596 a596b957 2022-07-14 tracey free(qs->commit);
597 a596b957 2022-07-14 tracey free(qs->file);
598 a596b957 2022-07-14 tracey free(qs->folder);
599 a596b957 2022-07-14 tracey free(qs->headref);
600 a596b957 2022-07-14 tracey free(qs->path);
602 a596b957 2022-07-14 tracey free(qs);
605 a596b957 2022-07-14 tracey static void
606 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
608 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
609 a596b957 2022-07-14 tracey free(repo_dir->name);
610 a596b957 2022-07-14 tracey free(repo_dir->owner);
611 a596b957 2022-07-14 tracey free(repo_dir->description);
612 a596b957 2022-07-14 tracey free(repo_dir->url);
613 a596b957 2022-07-14 tracey free(repo_dir->age);
614 a596b957 2022-07-14 tracey free(repo_dir->path);
616 a596b957 2022-07-14 tracey free(repo_dir);
620 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
622 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
623 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
625 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
626 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
627 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
629 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
630 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
631 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
633 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
634 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
635 341fa7ca 2022-09-01 op free(t->next_id);
636 341fa7ca 2022-09-01 op free(t->prev_id);
640 a596b957 2022-07-14 tracey const struct got_error *
641 a596b957 2022-07-14 tracey gotweb_render_content_type(struct request *c, const uint8_t *type)
643 4d648b92 2022-08-20 op const char *csp = "default-src 'self'; script-src 'none'; "
644 4d648b92 2022-08-20 op "object-src 'none';";
646 4d648b92 2022-08-20 op fcgi_printf(c,
647 4d648b92 2022-08-20 op "Content-Security-Policy: %s\r\n"
648 4d648b92 2022-08-20 op "Content-Type: %s\r\n\r\n",
653 a596b957 2022-07-14 tracey const struct got_error *
654 a596b957 2022-07-14 tracey gotweb_render_content_type_file(struct request *c, const uint8_t *type,
655 a596b957 2022-07-14 tracey char *file)
657 01498c42 2022-08-19 op fcgi_printf(c, "Content-type: %s\r\n"
658 a596b957 2022-07-14 tracey "Content-disposition: attachment; filename=%s\r\n\r\n",
663 ed619ca0 2022-12-14 op const struct got_error *
664 a596b957 2022-07-14 tracey gotweb_render_navs(struct request *c)
666 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
667 a596b957 2022-07-14 tracey struct transport *t = c->t;
668 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
669 a596b957 2022-07-14 tracey struct server *srv = c->srv;
672 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='np_wrapper'>\n<div id='nav_prev'>\n");
674 a596b957 2022-07-14 tracey goto done;
676 a596b957 2022-07-14 tracey switch(qs->action) {
677 a596b957 2022-07-14 tracey case INDEX:
678 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
679 8d02314f 2022-09-07 op struct gotweb_url url = {
680 8d02314f 2022-09-07 op .action = -1,
681 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
685 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Previous");
688 a596b957 2022-07-14 tracey case BRIEFS:
689 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
690 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
691 8d02314f 2022-09-07 op struct gotweb_url url = {
692 8d02314f 2022-09-07 op .action = BRIEFS,
693 8d02314f 2022-09-07 op .index_page = -1,
694 8d02314f 2022-09-07 op .page = qs->page - 1,
695 8d02314f 2022-09-07 op .path = qs->path,
696 8d02314f 2022-09-07 op .commit = t->prev_id,
697 8d02314f 2022-09-07 op .headref = qs->headref,
700 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Previous");
703 a596b957 2022-07-14 tracey case COMMITS:
704 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
705 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
706 8d02314f 2022-09-07 op struct gotweb_url url = {
707 6169d054 2022-12-17 op .action = COMMITS,
708 8d02314f 2022-09-07 op .index_page = -1,
709 8d02314f 2022-09-07 op .page = qs->page - 1,
710 8d02314f 2022-09-07 op .path = qs->path,
711 8d02314f 2022-09-07 op .commit = t->prev_id,
712 8d02314f 2022-09-07 op .headref = qs->headref,
713 8d02314f 2022-09-07 op .folder = qs->folder,
714 8d02314f 2022-09-07 op .file = qs->file,
717 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Previous");
720 a596b957 2022-07-14 tracey case TAGS:
721 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
722 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
723 8d02314f 2022-09-07 op struct gotweb_url url = {
724 8d02314f 2022-09-07 op .action = TAGS,
725 8d02314f 2022-09-07 op .index_page = -1,
726 8d02314f 2022-09-07 op .page = qs->page - 1,
727 8d02314f 2022-09-07 op .path = qs->path,
728 8d02314f 2022-09-07 op .commit = t->prev_id,
729 8d02314f 2022-09-07 op .headref = qs->headref,
732 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Previous");
740 01498c42 2022-08-19 op r = fcgi_printf(c, "</div>\n" /* #nav_prev */
741 01498c42 2022-08-19 op "<div id='nav_next'>");
743 a596b957 2022-07-14 tracey goto done;
745 a596b957 2022-07-14 tracey switch(qs->action) {
746 a596b957 2022-07-14 tracey case INDEX:
747 a596b957 2022-07-14 tracey if (t->next_disp == srv->max_repos_display &&
748 a596b957 2022-07-14 tracey t->repos_total != (qs->index_page + 1) *
749 a596b957 2022-07-14 tracey srv->max_repos_display) {
750 8d02314f 2022-09-07 op struct gotweb_url url = {
751 8d02314f 2022-09-07 op .action = -1,
752 8d02314f 2022-09-07 op .index_page = qs->index_page + 1,
756 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Next");
759 a596b957 2022-07-14 tracey case BRIEFS:
760 a596b957 2022-07-14 tracey if (t->next_id) {
761 8d02314f 2022-09-07 op struct gotweb_url url = {
762 8d02314f 2022-09-07 op .action = BRIEFS,
763 8d02314f 2022-09-07 op .index_page = -1,
764 8d02314f 2022-09-07 op .page = qs->page + 1,
765 8d02314f 2022-09-07 op .path = qs->path,
766 8d02314f 2022-09-07 op .commit = t->next_id,
767 8d02314f 2022-09-07 op .headref = qs->headref,
770 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Next");
773 a596b957 2022-07-14 tracey case COMMITS:
774 a596b957 2022-07-14 tracey if (t->next_id) {
775 8d02314f 2022-09-07 op struct gotweb_url url = {
776 6169d054 2022-12-17 op .action = COMMITS,
777 8d02314f 2022-09-07 op .index_page = -1,
778 8d02314f 2022-09-07 op .page = qs->page + 1,
779 8d02314f 2022-09-07 op .path = qs->path,
780 8d02314f 2022-09-07 op .commit = t->next_id,
781 8d02314f 2022-09-07 op .headref = qs->headref,
782 8d02314f 2022-09-07 op .folder = qs->folder,
783 8d02314f 2022-09-07 op .file = qs->file,
786 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Next");
789 a596b957 2022-07-14 tracey case TAGS:
790 a596b957 2022-07-14 tracey if (t->next_id) {
791 8d02314f 2022-09-07 op struct gotweb_url url = {
792 8d02314f 2022-09-07 op .action = TAGS,
793 8d02314f 2022-09-07 op .index_page = -1,
794 8d02314f 2022-09-07 op .page = qs->page + 1,
795 8d02314f 2022-09-07 op .path = qs->path,
796 8d02314f 2022-09-07 op .commit = t->next_id,
797 8d02314f 2022-09-07 op .headref = qs->headref,
800 8d02314f 2022-09-07 op r = gotweb_link(c, &url, "Next");
807 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #nav_next */
808 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #np_wrapper */
810 a596b957 2022-07-14 tracey free(t->next_id);
811 a596b957 2022-07-14 tracey t->next_id = NULL;
812 a596b957 2022-07-14 tracey free(t->prev_id);
813 a596b957 2022-07-14 tracey t->prev_id = NULL;
814 a596b957 2022-07-14 tracey return error;
817 a596b957 2022-07-14 tracey static const struct got_error *
818 a596b957 2022-07-14 tracey gotweb_render_index(struct request *c)
820 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
821 a596b957 2022-07-14 tracey struct server *srv = c->srv;
822 a596b957 2022-07-14 tracey struct transport *t = c->t;
823 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
824 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
826 2db401bd 2022-09-01 op struct dirent **sd_dent = NULL;
827 a596b957 2022-07-14 tracey unsigned int d_cnt, d_i, d_disp = 0;
828 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
831 a596b957 2022-07-14 tracey d = opendir(srv->repos_path);
832 a596b957 2022-07-14 tracey if (d == NULL) {
833 a596b957 2022-07-14 tracey error = got_error_from_errno2("opendir", srv->repos_path);
834 a596b957 2022-07-14 tracey return error;
837 a596b957 2022-07-14 tracey d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
838 a596b957 2022-07-14 tracey if (d_cnt == -1) {
839 2db401bd 2022-09-01 op sd_dent = NULL;
840 a596b957 2022-07-14 tracey error = got_error_from_errno2("scandir", srv->repos_path);
841 a596b957 2022-07-14 tracey goto done;
844 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
845 a596b957 2022-07-14 tracey goto done;
847 a596b957 2022-07-14 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
848 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
851 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
852 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
857 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
858 525dfdf4 2022-11-22 op sd_dent[d_i]);
861 525dfdf4 2022-11-22 op if (type != DT_DIR) {
863 a596b957 2022-07-14 tracey continue;
866 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
867 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
868 a596b957 2022-07-14 tracey t->prev_disp++;
869 a596b957 2022-07-14 tracey continue;
872 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
873 a596b957 2022-07-14 tracey if (error)
874 a596b957 2022-07-14 tracey goto done;
876 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
877 a596b957 2022-07-14 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
878 a596b957 2022-07-14 tracey error = NULL;
879 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
880 a596b957 2022-07-14 tracey repo_dir = NULL;
882 a596b957 2022-07-14 tracey continue;
884 525dfdf4 2022-11-22 op if (error && error->code != GOT_ERR_LONELY_PACKIDX)
887 a596b957 2022-07-14 tracey d_disp++;
888 a596b957 2022-07-14 tracey t->prev_disp++;
890 ed619ca0 2022-12-14 op if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
893 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
894 a596b957 2022-07-14 tracey repo_dir = NULL;
895 a596b957 2022-07-14 tracey t->next_disp++;
896 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
899 525dfdf4 2022-11-22 op t->repos_total = d_cnt - d_skipped;
901 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
903 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
905 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
906 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
909 a596b957 2022-07-14 tracey error = gotweb_render_navs(c);
910 a596b957 2022-07-14 tracey if (error)
911 a596b957 2022-07-14 tracey goto done;
913 2db401bd 2022-09-01 op if (sd_dent) {
914 2db401bd 2022-09-01 op for (d_i = 0; d_i < d_cnt; d_i++)
915 2db401bd 2022-09-01 op free(sd_dent[d_i]);
916 2db401bd 2022-09-01 op free(sd_dent);
918 a596b957 2022-07-14 tracey if (d != NULL && closedir(d) == EOF && error == NULL)
919 a596b957 2022-07-14 tracey error = got_error_from_errno("closedir");
920 a596b957 2022-07-14 tracey return error;
923 a596b957 2022-07-14 tracey static const struct got_error *
924 a596b957 2022-07-14 tracey gotweb_render_blame(struct request *c)
926 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
927 a596b957 2022-07-14 tracey struct transport *t = c->t;
928 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
929 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
932 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
933 a596b957 2022-07-14 tracey if (error)
934 a596b957 2022-07-14 tracey return error;
936 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
938 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
941 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
942 a596b957 2022-07-14 tracey if (error)
943 a596b957 2022-07-14 tracey goto done;
945 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
946 01498c42 2022-08-19 op "<div id='blame_title'>Blame</div>\n"
947 01498c42 2022-08-19 op "</div>\n" /* #blame_title_wrapper */
948 01498c42 2022-08-19 op "<div id='blame_content'>\n"
949 01498c42 2022-08-19 op "<div id='blame_header_wrapper'>\n"
950 01498c42 2022-08-19 op "<div id='blame_header'>\n"
951 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
952 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
953 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
954 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
955 01498c42 2022-08-19 op "</div>\n" /* #blame_header */
956 01498c42 2022-08-19 op "</div>\n" /* #blame_header_wrapper */
957 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
958 01498c42 2022-08-19 op "<div id='blame'>\n",
962 a596b957 2022-07-14 tracey goto done;
964 a596b957 2022-07-14 tracey error = got_output_file_blame(c);
965 a596b957 2022-07-14 tracey if (error)
966 a596b957 2022-07-14 tracey goto done;
968 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n" /* #blame */
969 01498c42 2022-08-19 op "</div>\n"); /* #blame_content */
973 a596b957 2022-07-14 tracey return error;
976 a596b957 2022-07-14 tracey static const struct got_error *
977 a596b957 2022-07-14 tracey gotweb_render_commits(struct request *c)
979 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
980 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
981 a596b957 2022-07-14 tracey struct server *srv = c->srv;
982 a596b957 2022-07-14 tracey struct transport *t = c->t;
983 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
984 d927f8c8 2022-08-20 op char *age = NULL, *author = NULL, *msg = NULL;
987 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='commits_title_wrapper'>\n"
988 01498c42 2022-08-19 op "<div class='commits_title'>Commits</div>\n"
989 01498c42 2022-08-19 op "</div>\n" /* .commits_title_wrapper */
990 01498c42 2022-08-19 op "<div class='commits_content'>\n");
992 a596b957 2022-07-14 tracey goto done;
994 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, srv->max_commits_display);
995 a596b957 2022-07-14 tracey if (error)
996 a596b957 2022-07-14 tracey goto done;
998 a596b957 2022-07-14 tracey TAILQ_FOREACH(rc, &t->repo_commits, entry) {
999 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1000 a596b957 2022-07-14 tracey if (error)
1001 a596b957 2022-07-14 tracey goto done;
1002 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rc->author);
1003 a596b957 2022-07-14 tracey if (error)
1004 a596b957 2022-07-14 tracey goto done;
1005 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
1009 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='commits_header_wrapper'>\n"
1010 01498c42 2022-08-19 op "<div class='commits_header'>\n"
1011 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1012 01498c42 2022-08-19 op "<div class='header_commit'>%s</div>\n"
1013 01498c42 2022-08-19 op "<div class='header_author_title'>Author:</div>\n"
1014 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1015 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1016 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1017 01498c42 2022-08-19 op "</div>\n" /* .commits_header */
1018 01498c42 2022-08-19 op "</div>\n" /* .commits_header_wrapper */
1019 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1020 01498c42 2022-08-19 op "<div class='commit'>\n%s</div>\n",
1021 01498c42 2022-08-19 op rc->commit_id,
1025 01498c42 2022-08-19 op if (r == -1)
1026 a596b957 2022-07-14 tracey goto done;
1028 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='navs_wrapper'>\n"
1029 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1032 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1033 8d02314f 2022-09-07 op .action = DIFF,
1034 8d02314f 2022-09-07 op .index_page = -1,
1036 8d02314f 2022-09-07 op .path = repo_dir->name,
1037 8d02314f 2022-09-07 op .commit = rc->commit_id,
1039 8cf2cdaa 2022-09-01 op if (r == -1)
1042 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1045 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1046 8d02314f 2022-09-07 op .action = TREE,
1047 8d02314f 2022-09-07 op .index_page = -1,
1049 8d02314f 2022-09-07 op .path = repo_dir->name,
1050 8d02314f 2022-09-07 op .commit = rc->commit_id,
1052 8d02314f 2022-09-07 op if (r == -1)
1055 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .navs */
1056 8d02314f 2022-09-07 op "</div>\n" /* .navs_wrapper */
1057 8d02314f 2022-09-07 op "<div class='dotted_line'></div>\n") == -1)
1060 a596b957 2022-07-14 tracey free(age);
1061 a596b957 2022-07-14 tracey age = NULL;
1062 a596b957 2022-07-14 tracey free(author);
1063 a596b957 2022-07-14 tracey author = NULL;
1068 a596b957 2022-07-14 tracey if (t->next_id || t->prev_id) {
1069 a596b957 2022-07-14 tracey error = gotweb_render_navs(c);
1070 a596b957 2022-07-14 tracey if (error)
1071 a596b957 2022-07-14 tracey goto done;
1073 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* .commits_content */
1075 a596b957 2022-07-14 tracey free(age);
1076 d927f8c8 2022-08-20 op free(author);
1078 a596b957 2022-07-14 tracey return error;
1081 a596b957 2022-07-14 tracey static const struct got_error *
1082 a596b957 2022-07-14 tracey gotweb_render_branches(struct request *c)
1084 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1085 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1086 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
1087 a596b957 2022-07-14 tracey struct transport *t = c->t;
1088 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1089 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1090 8d02314f 2022-09-07 op char *escaped_refname = NULL;
1091 a596b957 2022-07-14 tracey char *age = NULL;
1094 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1096 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
1097 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
1098 a596b957 2022-07-14 tracey if (error)
1099 a596b957 2022-07-14 tracey goto done;
1101 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1102 01498c42 2022-08-19 op "<div id='branches_title'>Branches</div>\n"
1103 01498c42 2022-08-19 op "</div>\n" /* #branches_title_wrapper */
1104 01498c42 2022-08-19 op "<div id='branches_content'>\n");
1105 01498c42 2022-08-19 op if (r == -1)
1106 a596b957 2022-07-14 tracey goto done;
1108 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
1109 d927f8c8 2022-08-20 op const char *refname = NULL;
1111 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
1112 a596b957 2022-07-14 tracey continue;
1114 d927f8c8 2022-08-20 op refname = got_ref_get_name(re->ref);
1115 a596b957 2022-07-14 tracey if (refname == NULL) {
1116 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1117 a596b957 2022-07-14 tracey goto done;
1119 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) != 0)
1120 a596b957 2022-07-14 tracey continue;
1122 c127fc49 2022-11-22 op error = got_get_repo_age(&age, c, refname, TM_DIFF);
1123 a596b957 2022-07-14 tracey if (error)
1124 a596b957 2022-07-14 tracey goto done;
1126 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
1127 a596b957 2022-07-14 tracey refname += 11;
1128 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_refname, refname);
1132 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1133 01498c42 2022-08-19 op "<div class='branches_age'>%s</div>\n"
1134 01498c42 2022-08-19 op "<div class='branches_space'> </div>\n"
1135 8d02314f 2022-09-07 op "<div class='branch'>", age);
1136 8d02314f 2022-09-07 op if (r == -1)
1139 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1140 8d02314f 2022-09-07 op .action = SUMMARY,
1141 8d02314f 2022-09-07 op .index_page = -1,
1143 8d02314f 2022-09-07 op .path = qs->path,
1144 8d02314f 2022-09-07 op .headref = refname,
1145 8d02314f 2022-09-07 op }, "%s", escaped_refname);
1146 8d02314f 2022-09-07 op if (r == -1)
1149 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .branch */
1150 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1151 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1154 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1155 8d02314f 2022-09-07 op .action = SUMMARY,
1156 8d02314f 2022-09-07 op .index_page = -1,
1158 8d02314f 2022-09-07 op .path = qs->path,
1159 8d02314f 2022-09-07 op .headref = refname,
1160 8d02314f 2022-09-07 op }, "summary");
1161 8d02314f 2022-09-07 op if (r == -1)
1164 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1167 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1168 8d02314f 2022-09-07 op .action = BRIEFS,
1169 8d02314f 2022-09-07 op .index_page = -1,
1171 8d02314f 2022-09-07 op .path = qs->path,
1172 8d02314f 2022-09-07 op .headref = refname,
1173 8d02314f 2022-09-07 op }, "commit briefs");
1174 8d02314f 2022-09-07 op if (r == -1)
1177 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1180 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1181 8d02314f 2022-09-07 op .action = COMMITS,
1182 8d02314f 2022-09-07 op .index_page = -1,
1184 8d02314f 2022-09-07 op .path = qs->path,
1185 8d02314f 2022-09-07 op .headref = refname,
1186 8d02314f 2022-09-07 op }, "commits");
1187 8d02314f 2022-09-07 op if (r == -1)
1190 8d02314f 2022-09-07 op r = fcgi_printf(c, "</div>\n" /* .navs */
1191 8d02314f 2022-09-07 op "</div>\n" /* .navs_wrapper */
1192 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1193 8d02314f 2022-09-07 op "</div>\n"); /* .branches_wrapper */
1194 01498c42 2022-08-19 op if (r == -1)
1195 a596b957 2022-07-14 tracey goto done;
1197 a596b957 2022-07-14 tracey free(age);
1198 a596b957 2022-07-14 tracey age = NULL;
1199 8d02314f 2022-09-07 op free(escaped_refname);
1200 8d02314f 2022-09-07 op escaped_refname = NULL;
1202 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #branches_content */
1205 8d02314f 2022-09-07 op free(escaped_refname);
1206 f49cdcf5 2022-09-02 op got_ref_list_free(&refs);
1207 a596b957 2022-07-14 tracey return error;
1210 a596b957 2022-07-14 tracey static const struct got_error *
1211 a596b957 2022-07-14 tracey gotweb_render_tree(struct request *c)
1213 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1214 a596b957 2022-07-14 tracey struct transport *t = c->t;
1215 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1216 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
1219 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
1220 a596b957 2022-07-14 tracey if (error)
1221 a596b957 2022-07-14 tracey return error;
1223 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1225 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1226 a596b957 2022-07-14 tracey if (error)
1227 a596b957 2022-07-14 tracey goto done;
1229 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
1233 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1234 01498c42 2022-08-19 op "<div id='tree_title'>Tree</div>\n"
1235 01498c42 2022-08-19 op "</div>\n" /* #tree_title_wrapper */
1236 01498c42 2022-08-19 op "<div id='tree_content'>\n"
1237 01498c42 2022-08-19 op "<div id='tree_header_wrapper'>\n"
1238 01498c42 2022-08-19 op "<div id='tree_header'>\n"
1239 01498c42 2022-08-19 op "<div id='header_tree_title'>Tree:</div>\n"
1240 01498c42 2022-08-19 op "<div id='header_tree'>%s</div>\n"
1241 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1242 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1243 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1244 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1245 01498c42 2022-08-19 op "</div>\n" /* #tree_header */
1246 01498c42 2022-08-19 op "</div>\n" /* #tree_header_wrapper */
1247 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1248 01498c42 2022-08-19 op "<div id='tree'>\n",
1249 01498c42 2022-08-19 op rc->tree_id,
1252 01498c42 2022-08-19 op if (r == -1)
1253 a596b957 2022-07-14 tracey goto done;
1255 a596b957 2022-07-14 tracey error = got_output_repo_tree(c);
1256 a596b957 2022-07-14 tracey if (error)
1257 a596b957 2022-07-14 tracey goto done;
1259 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tree */
1260 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tree_content */
1264 a596b957 2022-07-14 tracey return error;
1267 a596b957 2022-07-14 tracey static const struct got_error *
1268 a596b957 2022-07-14 tracey gotweb_render_diff(struct request *c)
1270 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1271 a596b957 2022-07-14 tracey struct transport *t = c->t;
1272 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1273 d927f8c8 2022-08-20 op char *age = NULL, *author = NULL, *msg = NULL;
1276 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
1277 a596b957 2022-07-14 tracey if (error)
1278 a596b957 2022-07-14 tracey return error;
1280 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1282 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1283 a596b957 2022-07-14 tracey if (error)
1284 a596b957 2022-07-14 tracey goto done;
1285 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rc->author);
1286 a596b957 2022-07-14 tracey if (error)
1287 a596b957 2022-07-14 tracey goto done;
1288 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
1292 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1293 01498c42 2022-08-19 op "<div id='diff_title'>Commit Diff</div>\n"
1294 01498c42 2022-08-19 op "</div>\n" /* #diff_title_wrapper */
1295 01498c42 2022-08-19 op "<div id='diff_content'>\n"
1296 01498c42 2022-08-19 op "<div id='diff_header_wrapper'>\n"
1297 01498c42 2022-08-19 op "<div id='diff_header'>\n"
1298 01498c42 2022-08-19 op "<div id='header_diff_title'>Diff:</div>\n"
1299 01498c42 2022-08-19 op "<div id='header_diff'>%s<br />%s</div>\n"
1300 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1301 01498c42 2022-08-19 op "<div class='header_commit'>%s</div>\n"
1302 01498c42 2022-08-19 op "<div id='header_tree_title'>Tree:</div>\n"
1303 01498c42 2022-08-19 op "<div id='header_tree'>%s</div>\n"
1304 01498c42 2022-08-19 op "<div class='header_author_title'>Author:</div>\n"
1305 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1306 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1307 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1308 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1309 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1310 01498c42 2022-08-19 op "</div>\n" /* #diff_header */
1311 01498c42 2022-08-19 op "</div>\n" /* #diff_header_wrapper */
1312 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1313 01498c42 2022-08-19 op "<div id='diff'>\n",
1314 01498c42 2022-08-19 op rc->parent_id, rc->commit_id,
1315 01498c42 2022-08-19 op rc->commit_id,
1316 01498c42 2022-08-19 op rc->tree_id,
1320 01498c42 2022-08-19 op if (r == -1)
1321 a596b957 2022-07-14 tracey goto done;
1323 a596b957 2022-07-14 tracey error = got_output_repo_diff(c);
1324 a596b957 2022-07-14 tracey if (error)
1325 a596b957 2022-07-14 tracey goto done;
1327 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #diff */
1328 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #diff_content */
1330 a596b957 2022-07-14 tracey free(age);
1331 a596b957 2022-07-14 tracey free(author);
1333 a596b957 2022-07-14 tracey return error;
1336 a596b957 2022-07-14 tracey static const struct got_error *
1337 a596b957 2022-07-14 tracey gotweb_render_summary(struct request *c)
1339 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1340 a596b957 2022-07-14 tracey struct transport *t = c->t;
1341 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1344 01498c42 2022-08-19 op if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1345 a596b957 2022-07-14 tracey goto done;
1347 01498c42 2022-08-19 op if (srv->show_repo_description) {
1348 01498c42 2022-08-19 op r = fcgi_printf(c,
1349 01498c42 2022-08-19 op "<div id='description_title'>Description:</div>\n"
1350 01498c42 2022-08-19 op "<div id='description'>%s</div>\n",
1351 f897bb24 2022-08-20 op t->repo_dir->description ? t->repo_dir->description : "");
1352 01498c42 2022-08-19 op if (r == -1)
1356 01498c42 2022-08-19 op if (srv->show_repo_owner) {
1357 01498c42 2022-08-19 op r = fcgi_printf(c,
1358 01498c42 2022-08-19 op "<div id='repo_owner_title'>Owner:</div>\n"
1359 01498c42 2022-08-19 op "<div id='repo_owner'>%s</div>\n",
1360 f897bb24 2022-08-20 op t->repo_dir->owner ? t->repo_dir->owner : "");
1361 01498c42 2022-08-19 op if (r == -1)
1365 01498c42 2022-08-19 op if (srv->show_repo_age) {
1366 01498c42 2022-08-19 op r = fcgi_printf(c,
1367 01498c42 2022-08-19 op "<div id='last_change_title'>Last Change:</div>\n"
1368 01498c42 2022-08-19 op "<div id='last_change'>%s</div>\n",
1369 01498c42 2022-08-19 op t->repo_dir->age);
1370 01498c42 2022-08-19 op if (r == -1)
1374 01498c42 2022-08-19 op if (srv->show_repo_cloneurl) {
1375 01498c42 2022-08-19 op r = fcgi_printf(c,
1376 01498c42 2022-08-19 op "<div id='cloneurl_title'>Clone URL:</div>\n"
1377 01498c42 2022-08-19 op "<div id='cloneurl'>%s</div>\n",
1378 01498c42 2022-08-19 op t->repo_dir->url ? t->repo_dir->url : "");
1379 01498c42 2022-08-19 op if (r == -1)
1383 01498c42 2022-08-19 op r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1384 01498c42 2022-08-19 op if (r == -1)
1385 a596b957 2022-07-14 tracey goto done;
1387 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
1388 a596b957 2022-07-14 tracey goto done;
1390 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
1391 a596b957 2022-07-14 tracey if (error) {
1392 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1393 a596b957 2022-07-14 tracey goto done;
1396 a596b957 2022-07-14 tracey error = gotweb_render_branches(c);
1397 a596b957 2022-07-14 tracey if (error)
1398 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1400 a596b957 2022-07-14 tracey return error;
1403 a596b957 2022-07-14 tracey static const struct got_error *
1404 a596b957 2022-07-14 tracey gotweb_render_tag(struct request *c)
1406 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1407 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1408 a596b957 2022-07-14 tracey struct transport *t = c->t;
1409 d927f8c8 2022-08-20 op char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1411 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, 1);
1412 a596b957 2022-07-14 tracey if (error)
1413 a596b957 2022-07-14 tracey goto done;
1415 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1416 a596b957 2022-07-14 tracey error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1417 a596b957 2022-07-14 tracey "bad commit id");
1418 a596b957 2022-07-14 tracey goto done;
1421 a596b957 2022-07-14 tracey rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1423 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1424 a596b957 2022-07-14 tracey if (error)
1425 a596b957 2022-07-14 tracey goto done;
1426 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rt->tagger);
1427 a596b957 2022-07-14 tracey if (error)
1428 a596b957 2022-07-14 tracey goto done;
1429 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->commit_msg);
1433 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1434 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/", 5) == 0)
1435 5fba0750 2022-09-01 stsp tagname += 5;
1436 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1440 01498c42 2022-08-19 op fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1441 01498c42 2022-08-19 op "<div id='tags_title'>Tag</div>\n"
1442 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1443 01498c42 2022-08-19 op "<div id='tags_content'>\n"
1444 01498c42 2022-08-19 op "<div id='tag_header_wrapper'>\n"
1445 01498c42 2022-08-19 op "<div id='tag_header'>\n"
1446 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1447 01498c42 2022-08-19 op "<div class='header_commit'>%s"
1448 01498c42 2022-08-19 op " <span class='refs_str'>(%s)</span></div>\n"
1449 01498c42 2022-08-19 op "<div class='header_author_title'>Tagger:</div>\n"
1450 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1451 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1452 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1453 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1454 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1455 01498c42 2022-08-19 op "</div>\n" /* #tag_header */
1456 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1457 01498c42 2022-08-19 op "<div id='tag_commit'>\n%s</div>"
1458 f864583e 2022-09-06 op "</div>" /* #tag_header_wrapper */
1459 f864583e 2022-09-06 op "</div>", /* #tags_content */
1460 01498c42 2022-08-19 op rt->commit_id,
1465 01498c42 2022-08-19 op rt->tag_commit);
1468 a596b957 2022-07-14 tracey free(age);
1469 a596b957 2022-07-14 tracey free(author);
1471 a596b957 2022-07-14 tracey return error;
1474 a596b957 2022-07-14 tracey static const struct got_error *
1475 a596b957 2022-07-14 tracey gotweb_render_tags(struct request *c)
1477 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1478 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1479 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1480 a596b957 2022-07-14 tracey struct transport *t = c->t;
1481 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1482 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1483 d927f8c8 2022-08-20 op char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1484 01498c42 2022-08-19 op int r, commit_found = 0;
1486 a596b957 2022-07-14 tracey if (qs->action == BRIEFS) {
1487 a596b957 2022-07-14 tracey qs->action = TAGS;
1488 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1490 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, srv->max_commits_display);
1491 a596b957 2022-07-14 tracey if (error)
1492 a596b957 2022-07-14 tracey goto done;
1494 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1495 01498c42 2022-08-19 op "<div id='tags_title'>Tags</div>\n"
1496 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1497 01498c42 2022-08-19 op "<div id='tags_content'>\n");
1498 01498c42 2022-08-19 op if (r == -1)
1499 a596b957 2022-07-14 tracey goto done;
1501 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1502 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1503 01498c42 2022-08-19 op "This repository contains no tags");
1504 01498c42 2022-08-19 op if (r == -1)
1505 a596b957 2022-07-14 tracey goto done;
1508 a596b957 2022-07-14 tracey TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1509 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL) {
1510 a596b957 2022-07-14 tracey if (strcmp(qs->commit, rt->commit_id) != 0)
1511 a596b957 2022-07-14 tracey continue;
1513 a596b957 2022-07-14 tracey commit_found = 1;
1515 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1516 a596b957 2022-07-14 tracey if (error)
1517 a596b957 2022-07-14 tracey goto done;
1519 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1520 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/tags/", 10) == 0)
1521 5fba0750 2022-09-01 stsp tagname += 10;
1522 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1526 a596b957 2022-07-14 tracey if (rt->tag_commit != NULL) {
1527 a596b957 2022-07-14 tracey newline = strchr(rt->tag_commit, '\n');
1528 a596b957 2022-07-14 tracey if (newline)
1529 a596b957 2022-07-14 tracey *newline = '\0';
1530 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->tag_commit);
1535 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1536 01498c42 2022-08-19 op "<div class='tag'>%s</div>\n"
1537 8d02314f 2022-09-07 op "<div class='tag_log'>", age, tagname) == -1)
1540 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1541 8d02314f 2022-09-07 op .action = TAG,
1542 8d02314f 2022-09-07 op .index_page = -1,
1544 8d02314f 2022-09-07 op .path = repo_dir->name,
1545 8d02314f 2022-09-07 op .commit = rt->commit_id,
1546 8d02314f 2022-09-07 op }, "%s", msg ? msg : "");
1547 8d02314f 2022-09-07 op if (r == -1)
1550 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .tag_log */
1551 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1552 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1555 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1556 8d02314f 2022-09-07 op .action = TAG,
1557 8d02314f 2022-09-07 op .index_page = -1,
1559 8d02314f 2022-09-07 op .path = repo_dir->name,
1560 8d02314f 2022-09-07 op .commit = rt->commit_id,
1562 8d02314f 2022-09-07 op if (r == -1)
1565 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1568 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1569 8d02314f 2022-09-07 op .action = BRIEFS,
1570 8d02314f 2022-09-07 op .index_page = -1,
1572 8d02314f 2022-09-07 op .path = repo_dir->name,
1573 8d02314f 2022-09-07 op .commit = rt->commit_id,
1574 8d02314f 2022-09-07 op }, "commit briefs");
1575 8d02314f 2022-09-07 op if (r == -1)
1578 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1581 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1582 8d02314f 2022-09-07 op .action = COMMITS,
1583 8d02314f 2022-09-07 op .index_page = -1,
1585 8d02314f 2022-09-07 op .path = repo_dir->name,
1586 8d02314f 2022-09-07 op .commit = rt->commit_id,
1587 8d02314f 2022-09-07 op }, "commits");
1588 8d02314f 2022-09-07 op if (r == -1)
1591 8d02314f 2022-09-07 op r = fcgi_printf(c,
1592 01498c42 2022-08-19 op "</div>\n" /* .navs */
1593 01498c42 2022-08-19 op "</div>\n" /* .navs_wrapper */
1594 8d02314f 2022-09-07 op "<div class='dotted_line'></div>\n");
1595 01498c42 2022-08-19 op if (r == -1)
1596 a596b957 2022-07-14 tracey goto done;
1598 a596b957 2022-07-14 tracey free(age);
1599 a596b957 2022-07-14 tracey age = NULL;
1600 d927f8c8 2022-08-20 op free(tagname);
1601 d927f8c8 2022-08-20 op tagname = NULL;
1605 a596b957 2022-07-14 tracey if (t->next_id || t->prev_id) {
1606 a596b957 2022-07-14 tracey error = gotweb_render_navs(c);
1607 a596b957 2022-07-14 tracey if (error)
1608 a596b957 2022-07-14 tracey goto done;
1610 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tags_content */
1612 a596b957 2022-07-14 tracey free(age);
1613 d927f8c8 2022-08-20 op free(tagname);
1615 a596b957 2022-07-14 tracey return error;
1618 a596b957 2022-07-14 tracey const struct got_error *
1619 a596b957 2022-07-14 tracey gotweb_escape_html(char **escaped_html, const char *orig_html)
1621 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1622 a596b957 2022-07-14 tracey struct escape_pair {
1624 a596b957 2022-07-14 tracey const char *s;
1625 a596b957 2022-07-14 tracey } esc[] = {
1626 a596b957 2022-07-14 tracey { '>', ">" },
1627 a596b957 2022-07-14 tracey { '<', "<" },
1628 a596b957 2022-07-14 tracey { '&', "&" },
1629 a596b957 2022-07-14 tracey { '"', """ },
1630 a596b957 2022-07-14 tracey { '\'', "'" },
1631 a596b957 2022-07-14 tracey { '\n', "<br />" },
1633 a596b957 2022-07-14 tracey size_t orig_len, len;
1634 a596b957 2022-07-14 tracey int i, j, x;
1636 a596b957 2022-07-14 tracey orig_len = strlen(orig_html);
1637 a596b957 2022-07-14 tracey len = orig_len;
1638 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1639 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1640 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1641 a596b957 2022-07-14 tracey continue;
1642 a596b957 2022-07-14 tracey len += strlen(esc[j].s) - 1 /* escaped char */;
1646 a596b957 2022-07-14 tracey *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1647 a596b957 2022-07-14 tracey if (*escaped_html == NULL)
1648 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1651 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1652 a596b957 2022-07-14 tracey int escaped = 0;
1653 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1654 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1655 a596b957 2022-07-14 tracey continue;
1657 a596b957 2022-07-14 tracey if (strlcat(*escaped_html, esc[j].s, len + 1)
1658 a596b957 2022-07-14 tracey >= len + 1) {
1659 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_SPACE);
1660 a596b957 2022-07-14 tracey goto done;
1662 a596b957 2022-07-14 tracey x += strlen(esc[j].s);
1663 a596b957 2022-07-14 tracey escaped = 1;
1666 a596b957 2022-07-14 tracey if (!escaped) {
1667 a596b957 2022-07-14 tracey (*escaped_html)[x] = orig_html[i];
1672 a596b957 2022-07-14 tracey if (error) {
1673 a596b957 2022-07-14 tracey free(*escaped_html);
1674 a596b957 2022-07-14 tracey *escaped_html = NULL;
1675 a596b957 2022-07-14 tracey } else {
1676 a596b957 2022-07-14 tracey (*escaped_html)[x] = '\0';
1679 a596b957 2022-07-14 tracey return error;
1682 8d02314f 2022-09-07 op static inline int
1683 8d02314f 2022-09-07 op should_urlencode(int c)
1685 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
1688 8d02314f 2022-09-07 op switch (c) {
1689 8d02314f 2022-09-07 op /* gen-delim */
1697 8d02314f 2022-09-07 op /* sub-delims */
1715 8d02314f 2022-09-07 op static char *
1716 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
1718 8d02314f 2022-09-07 op const char *s;
1719 8d02314f 2022-09-07 op char *escaped;
1720 8d02314f 2022-09-07 op size_t i, len;
1724 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1726 8d02314f 2022-09-07 op if (should_urlencode(*s))
1730 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
1731 8d02314f 2022-09-07 op if (escaped == NULL)
1732 8d02314f 2022-09-07 op return NULL;
1735 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1736 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1737 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1738 8d02314f 2022-09-07 op b = (*s & 0x0F);
1740 8d02314f 2022-09-07 op escaped[i++] = '%';
1741 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1742 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1744 8d02314f 2022-09-07 op escaped[i++] = *s;
1747 8d02314f 2022-09-07 op return escaped;
1750 ed619ca0 2022-12-14 op const char *
1751 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1753 8d02314f 2022-09-07 op switch (action) {
1755 8d02314f 2022-09-07 op return "blame";
1757 8d02314f 2022-09-07 op return "blob";
1758 8d02314f 2022-09-07 op case BRIEFS:
1759 8d02314f 2022-09-07 op return "briefs";
1760 8d02314f 2022-09-07 op case COMMITS:
1761 8d02314f 2022-09-07 op return "commits";
1763 8d02314f 2022-09-07 op return "diff";
1765 8d02314f 2022-09-07 op return "err";
1767 8d02314f 2022-09-07 op return "index";
1768 8d02314f 2022-09-07 op case SUMMARY:
1769 8d02314f 2022-09-07 op return "summary";
1771 8d02314f 2022-09-07 op return "tag";
1773 8d02314f 2022-09-07 op return "tags";
1775 8d02314f 2022-09-07 op return "tree";
1777 8d02314f 2022-09-07 op return NULL;
1782 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1784 8d02314f 2022-09-07 op const char *sep = "?", *action;
1788 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1789 8d02314f 2022-09-07 op if (action != NULL) {
1790 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1795 8d02314f 2022-09-07 op if (url->commit) {
1796 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1801 8d02314f 2022-09-07 op if (url->previd) {
1802 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1807 8d02314f 2022-09-07 op if (url->prevset) {
1808 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1813 8d02314f 2022-09-07 op if (url->file) {
1814 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1815 8d02314f 2022-09-07 op if (tmp == NULL)
1817 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1819 8d02314f 2022-09-07 op if (r == -1)
1824 8d02314f 2022-09-07 op if (url->folder) {
1825 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1826 8d02314f 2022-09-07 op if (tmp == NULL)
1828 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1830 8d02314f 2022-09-07 op if (r == -1)
1835 8d02314f 2022-09-07 op if (url->headref) {
1836 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1837 8d02314f 2022-09-07 op if (tmp == NULL)
1839 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1841 8d02314f 2022-09-07 op if (r == -1)
1846 8d02314f 2022-09-07 op if (url->index_page != -1) {
1847 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1848 8d02314f 2022-09-07 op url->index_page) == -1)
1853 8d02314f 2022-09-07 op if (url->path) {
1854 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1855 8d02314f 2022-09-07 op if (tmp == NULL)
1857 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1859 8d02314f 2022-09-07 op if (r == -1)
1864 8d02314f 2022-09-07 op if (url->page != -1) {
1865 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1874 8d02314f 2022-09-07 op gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1879 8d02314f 2022-09-07 op if (fcgi_printf(c, "<a href='") == -1)
1882 ed619ca0 2022-12-14 op if (gotweb_render_url(c, url) == -1)
1885 8d02314f 2022-09-07 op if (fcgi_printf(c, "'>") == -1)
1888 8d02314f 2022-09-07 op va_start(ap, fmt);
1889 8d02314f 2022-09-07 op r = fcgi_vprintf(c, fmt, ap);
1891 8d02314f 2022-09-07 op if (r == -1)
1894 8d02314f 2022-09-07 op if (fcgi_printf(c, "</a>"))
1899 b5c757f5 2022-09-01 stsp static struct got_repository *
1900 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1904 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1905 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1906 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1909 b5c757f5 2022-09-01 stsp return NULL;
1912 a596b957 2022-07-14 tracey static const struct got_error *
1913 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1914 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1916 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1917 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1918 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1919 b5c757f5 2022-09-01 stsp int evicted = 0;
1921 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1922 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1923 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1924 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1925 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1926 b5c757f5 2022-09-01 stsp if (error)
1927 b5c757f5 2022-09-01 stsp return error;
1928 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1929 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1930 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1931 b5c757f5 2022-09-01 stsp evicted = 1;
1933 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1936 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1937 b5c757f5 2022-09-01 stsp if (error) {
1938 b5c757f5 2022-09-01 stsp if (evicted) {
1939 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1940 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1942 b5c757f5 2022-09-01 stsp return error;
1945 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1946 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1947 b5c757f5 2022-09-01 stsp if (evicted) {
1948 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1949 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1951 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1954 b5c757f5 2022-09-01 stsp cr->repo = repo;
1955 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1956 b5c757f5 2022-09-01 stsp *new = repo;
1957 b5c757f5 2022-09-01 stsp return NULL;
1960 b5c757f5 2022-09-01 stsp static const struct got_error *
1961 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1963 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1964 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1965 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1966 a596b957 2022-07-14 tracey struct transport *t = c->t;
1967 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1968 a596b957 2022-07-14 tracey DIR *dt;
1969 a596b957 2022-07-14 tracey char *dir_test;
1971 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1972 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1973 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1975 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1976 a596b957 2022-07-14 tracey if (dt == NULL) {
1977 a596b957 2022-07-14 tracey free(dir_test);
1978 a596b957 2022-07-14 tracey } else {
1979 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1980 a596b957 2022-07-14 tracey dir_test = NULL;
1984 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1985 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1986 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1988 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1989 a596b957 2022-07-14 tracey if (dt == NULL) {
1990 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1991 a596b957 2022-07-14 tracey goto err;
1993 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1994 0fad85dd 2022-09-01 op dir_test = NULL;
1998 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1999 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
2000 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
2001 d5996b9e 2022-10-31 landry goto err;
2004 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
2005 b5c757f5 2022-09-01 stsp if (repo == NULL) {
2006 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
2007 b5c757f5 2022-09-01 stsp if (error)
2010 b5c757f5 2022-09-01 stsp t->repo = repo;
2011 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
2012 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
2013 a596b957 2022-07-14 tracey if (error)
2014 a596b957 2022-07-14 tracey goto err;
2015 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
2016 a596b957 2022-07-14 tracey if (error)
2017 a596b957 2022-07-14 tracey goto err;
2018 c127fc49 2022-11-22 op error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
2019 a596b957 2022-07-14 tracey if (error)
2020 a596b957 2022-07-14 tracey goto err;
2021 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
2024 a596b957 2022-07-14 tracey free(dir_test);
2025 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
2026 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
2027 a596b957 2022-07-14 tracey return error;
2030 a596b957 2022-07-14 tracey static const struct got_error *
2031 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
2033 a596b957 2022-07-14 tracey const struct got_error *error;
2035 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
2036 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
2037 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
2039 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
2040 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
2041 a596b957 2022-07-14 tracey free(*repo_dir);
2042 a596b957 2022-07-14 tracey *repo_dir = NULL;
2043 a596b957 2022-07-14 tracey return error;
2045 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
2046 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
2047 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
2048 a596b957 2022-07-14 tracey (*repo_dir)->age = NULL;
2049 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
2051 a596b957 2022-07-14 tracey return NULL;
2054 a596b957 2022-07-14 tracey static const struct got_error *
2055 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
2056 3b81530f 2022-11-22 op const char *dirpath, int dir)
2058 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
2059 3b81530f 2022-11-22 op struct stat sb;
2060 3b81530f 2022-11-22 op int fd = -1;
2063 a596b957 2022-07-14 tracey *description = NULL;
2064 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
2065 a596b957 2022-07-14 tracey return NULL;
2067 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
2068 3b81530f 2022-11-22 op if (fd == -1) {
2069 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
2070 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
2071 3b81530f 2022-11-22 op dirpath, "description");
2073 a596b957 2022-07-14 tracey goto done;
2076 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
2077 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
2078 3b81530f 2022-11-22 op dirpath, "description");
2079 a596b957 2022-07-14 tracey goto done;
2082 3b81530f 2022-11-22 op len = sb.st_size;
2083 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
2084 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
2086 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
2087 a596b957 2022-07-14 tracey if (*description == NULL) {
2088 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
2089 a596b957 2022-07-14 tracey goto done;
2092 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
2093 3b81530f 2022-11-22 op error = got_error_from_errno("read");
2095 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
2096 3b81530f 2022-11-22 op error = got_error_from_errno("close");
2097 a596b957 2022-07-14 tracey return error;
2100 a596b957 2022-07-14 tracey static const struct got_error *
2101 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
2104 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
2105 3b81530f 2022-11-22 op struct stat sb;
2106 3b81530f 2022-11-22 op int fd = -1;
2109 a596b957 2022-07-14 tracey *url = NULL;
2110 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
2111 a596b957 2022-07-14 tracey return NULL;
2113 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
2114 3b81530f 2022-11-22 op if (fd == -1) {
2115 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
2116 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
2117 3b81530f 2022-11-22 op dirpath, "cloneurl");
2119 a596b957 2022-07-14 tracey goto done;
2122 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
2123 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
2124 3b81530f 2022-11-22 op dirpath, "cloneurl");
2125 a596b957 2022-07-14 tracey goto done;
2128 3b81530f 2022-11-22 op len = sb.st_size;
2129 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2130 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
2132 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
2133 a596b957 2022-07-14 tracey if (*url == NULL) {
2134 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
2135 a596b957 2022-07-14 tracey goto done;
2138 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
2139 3b81530f 2022-11-22 op error = got_error_from_errno("read");
2141 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
2142 3b81530f 2022-11-22 op error = got_error_from_errno("close");
2143 a596b957 2022-07-14 tracey return error;
2146 a596b957 2022-07-14 tracey const struct got_error *
2147 a596b957 2022-07-14 tracey gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2149 a596b957 2022-07-14 tracey struct tm tm;
2150 fced5a66 2022-07-20 naddy long long diff_time;
2151 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
2152 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
2153 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
2154 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
2155 a596b957 2022-07-14 tracey char *s;
2156 a596b957 2022-07-14 tracey char datebuf[29];
2158 a596b957 2022-07-14 tracey *repo_age = NULL;
2160 a596b957 2022-07-14 tracey switch (ref_tm) {
2161 a596b957 2022-07-14 tracey case TM_DIFF:
2162 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
2163 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2164 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2165 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
2166 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2167 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2168 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2169 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2170 a596b957 2022-07-14 tracey months) == -1)
2171 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2172 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2173 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2174 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2175 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2176 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2177 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2178 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
2179 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2180 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
2181 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2182 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
2183 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2184 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
2185 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2186 a596b957 2022-07-14 tracey minutes) == -1)
2187 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2188 a596b957 2022-07-14 tracey } else if (diff_time > 2) {
2189 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", diff_time,
2190 a596b957 2022-07-14 tracey seconds) == -1)
2191 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2192 a596b957 2022-07-14 tracey } else {
2193 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s", now) == -1)
2194 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2197 a596b957 2022-07-14 tracey case TM_LONG:
2198 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2199 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
2201 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
2202 a596b957 2022-07-14 tracey if (s == NULL)
2203 a596b957 2022-07-14 tracey return got_error_from_errno("asctime_r");
2205 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2206 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2209 a596b957 2022-07-14 tracey return NULL;