2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 2ac684a4 2022-09-03 thomas * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
9 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
10 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
12 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 8a35f56c 2022-07-16 thomas #include <stdio.h>
34 8a35f56c 2022-07-16 thomas #include <stdlib.h>
35 8a35f56c 2022-07-16 thomas #include <string.h>
36 8a35f56c 2022-07-16 thomas #include <unistd.h>
38 8a35f56c 2022-07-16 thomas #include "got_error.h"
39 8a35f56c 2022-07-16 thomas #include "got_object.h"
40 8a35f56c 2022-07-16 thomas #include "got_reference.h"
41 8a35f56c 2022-07-16 thomas #include "got_repository.h"
42 8a35f56c 2022-07-16 thomas #include "got_path.h"
43 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
44 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
45 8a35f56c 2022-07-16 thomas #include "got_diff.h"
46 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
47 8a35f56c 2022-07-16 thomas #include "got_blame.h"
48 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
50 8a35f56c 2022-07-16 thomas #include "proc.h"
51 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
52 d6795e9f 2022-12-30 thomas #include "tmpl.h"
54 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
55 8a35f56c 2022-07-16 thomas { "action", ACTION },
56 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
57 8a35f56c 2022-07-16 thomas { "file", RFILE },
58 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
59 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
60 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
61 8a35f56c 2022-07-16 thomas { "path", PATH },
62 8a35f56c 2022-07-16 thomas { "page", PAGE },
65 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
66 8a35f56c 2022-07-16 thomas { "blame", BLAME },
67 8a35f56c 2022-07-16 thomas { "blob", BLOB },
68 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
69 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
70 8a35f56c 2022-07-16 thomas { "diff", DIFF },
71 8a35f56c 2022-07-16 thomas { "error", ERR },
72 8a35f56c 2022-07-16 thomas { "index", INDEX },
73 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
74 8a35f56c 2022-07-16 thomas { "tag", TAG },
75 8a35f56c 2022-07-16 thomas { "tags", TAGS },
76 8a35f56c 2022-07-16 thomas { "tree", TREE },
77 d6795e9f 2022-12-30 thomas { "rss", RSS },
80 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
81 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
84 8a35f56c 2022-07-16 thomas char *, char *);
85 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
87 8a35f56c 2022-07-16 thomas const char *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
89 8a35f56c 2022-07-16 thomas struct repo_dir *);
90 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
91 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
92 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
93 4606e6d4 2022-11-23 thomas const char *, int);
94 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_blame(struct request *);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_diff(struct request *);
96 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_summary(struct request *);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tag(struct request *);
98 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tags(struct request *);
99 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tree(struct request *);
100 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_branches(struct request *);
102 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
103 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
105 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
110 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
111 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
112 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
113 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
114 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
115 79393471 2022-08-27 thomas int r, html = 0;
117 8a35f56c 2022-07-16 thomas /* init the transport */
118 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
119 8a35f56c 2022-07-16 thomas if (error) {
120 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
123 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
124 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
126 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
127 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
128 8a35f56c 2022-07-16 thomas if (srv == NULL) {
129 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
130 8a35f56c 2022-07-16 thomas goto err;
132 8a35f56c 2022-07-16 thomas c->srv = srv;
133 8a35f56c 2022-07-16 thomas /* parse our querystring */
134 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
135 8a35f56c 2022-07-16 thomas if (error) {
136 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
137 8a35f56c 2022-07-16 thomas goto err;
139 8a35f56c 2022-07-16 thomas c->t->qs = qs;
140 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
141 8a35f56c 2022-07-16 thomas if (error) {
142 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
143 8a35f56c 2022-07-16 thomas goto err;
147 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
148 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
149 8a35f56c 2022-07-16 thomas * querystring.
152 8a35f56c 2022-07-16 thomas if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
153 8a35f56c 2022-07-16 thomas qs->action == DIFF)) {
154 8a35f56c 2022-07-16 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
155 8a35f56c 2022-07-16 thomas goto render;
158 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
159 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
160 8a35f56c 2022-07-16 thomas if (error)
161 8a35f56c 2022-07-16 thomas goto done;
162 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
163 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
164 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
165 8a35f56c 2022-07-16 thomas goto err;
168 8a35f56c 2022-07-16 thomas /* render top of page */
169 8a35f56c 2022-07-16 thomas if (qs != NULL && qs->action == BLOB) {
170 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
171 8a35f56c 2022-07-16 thomas if (error)
172 8a35f56c 2022-07-16 thomas goto done;
173 8a35f56c 2022-07-16 thomas error = got_output_file_blob(c);
174 d6795e9f 2022-12-30 thomas if (error) {
175 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
176 d6795e9f 2022-12-30 thomas goto err;
178 d6795e9f 2022-12-30 thomas goto done;
181 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
182 d6795e9f 2022-12-30 thomas error = gotweb_render_content_type(c,
183 d6795e9f 2022-12-30 thomas "application/rss+xml;charset=utf-8");
184 d6795e9f 2022-12-30 thomas if (error) {
185 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
186 d6795e9f 2022-12-30 thomas goto err;
189 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
190 8a35f56c 2022-07-16 thomas if (error) {
191 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
192 8a35f56c 2022-07-16 thomas goto err;
194 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
195 d6795e9f 2022-12-30 thomas goto err;
196 8a35f56c 2022-07-16 thomas goto done;
200 43a44bce 2022-12-04 thomas error = gotweb_render_content_type(c, "text/html");
201 43a44bce 2022-12-04 thomas if (error) {
202 43a44bce 2022-12-04 thomas log_warnx("%s: %s", __func__, error->msg);
203 43a44bce 2022-12-04 thomas goto err;
205 43a44bce 2022-12-04 thomas html = 1;
207 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
208 8a35f56c 2022-07-16 thomas goto err;
210 8a35f56c 2022-07-16 thomas if (error2) {
211 8a35f56c 2022-07-16 thomas error = error2;
212 8a35f56c 2022-07-16 thomas goto err;
215 8a35f56c 2022-07-16 thomas switch(qs->action) {
216 8a35f56c 2022-07-16 thomas case BLAME:
217 8a35f56c 2022-07-16 thomas error = gotweb_render_blame(c);
218 8a35f56c 2022-07-16 thomas if (error) {
219 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
220 8a35f56c 2022-07-16 thomas goto err;
223 8a35f56c 2022-07-16 thomas case BRIEFS:
224 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
225 8a35f56c 2022-07-16 thomas goto err;
227 8a35f56c 2022-07-16 thomas case COMMITS:
228 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
229 8a35f56c 2022-07-16 thomas if (error) {
230 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
231 8a35f56c 2022-07-16 thomas goto err;
233 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
234 7ade8b27 2022-12-30 thomas goto err;
236 8a35f56c 2022-07-16 thomas case DIFF:
237 8a35f56c 2022-07-16 thomas error = gotweb_render_diff(c);
238 8a35f56c 2022-07-16 thomas if (error) {
239 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
240 8a35f56c 2022-07-16 thomas goto err;
243 8a35f56c 2022-07-16 thomas case INDEX:
244 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
245 8a35f56c 2022-07-16 thomas if (error) {
246 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
247 8a35f56c 2022-07-16 thomas goto err;
250 8a35f56c 2022-07-16 thomas case SUMMARY:
251 8a35f56c 2022-07-16 thomas error = gotweb_render_summary(c);
252 8a35f56c 2022-07-16 thomas if (error) {
253 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
254 8a35f56c 2022-07-16 thomas goto err;
257 8a35f56c 2022-07-16 thomas case TAG:
258 8a35f56c 2022-07-16 thomas error = gotweb_render_tag(c);
259 8a35f56c 2022-07-16 thomas if (error) {
260 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
261 8a35f56c 2022-07-16 thomas goto err;
264 8a35f56c 2022-07-16 thomas case TAGS:
265 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
266 8a35f56c 2022-07-16 thomas if (error) {
267 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
268 8a35f56c 2022-07-16 thomas goto err;
271 8a35f56c 2022-07-16 thomas case TREE:
272 8a35f56c 2022-07-16 thomas error = gotweb_render_tree(c);
273 8a35f56c 2022-07-16 thomas if (error) {
274 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
275 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas case ERR:
280 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
281 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
282 79393471 2022-08-27 thomas if (r == -1)
283 8a35f56c 2022-07-16 thomas goto err;
287 8a35f56c 2022-07-16 thomas goto done;
289 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
291 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
293 8a35f56c 2022-07-16 thomas if (error) {
294 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
297 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
300 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
303 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
304 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
307 8a35f56c 2022-07-16 thomas struct server *
308 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
310 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
312 3f0158b4 2022-08-30 thomas /* check against the server name first */
313 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
314 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
315 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
316 8a35f56c 2022-07-16 thomas goto done;
318 3f0158b4 2022-08-30 thomas /* check against subdomain second */
319 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
320 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
321 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
322 8a35f56c 2022-07-16 thomas goto done;
324 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
325 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
326 8a35f56c 2022-07-16 thomas if (srv != NULL)
329 8a35f56c 2022-07-16 thomas return srv;
332 8a35f56c 2022-07-16 thomas const struct got_error *
333 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
335 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
337 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
338 8a35f56c 2022-07-16 thomas if (*t == NULL)
339 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
341 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
342 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
344 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
345 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
346 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
347 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
348 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
349 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
350 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
352 8a35f56c 2022-07-16 thomas return error;
355 8a35f56c 2022-07-16 thomas static const struct got_error *
356 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
358 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
360 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
361 8a35f56c 2022-07-16 thomas if (*qs == NULL)
362 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
364 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
365 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
366 84630254 2022-09-02 thomas free(*qs);
367 84630254 2022-09-02 thomas *qs = NULL;
368 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
371 84630254 2022-09-02 thomas (*qs)->action = INDEX;
372 84630254 2022-09-02 thomas (*qs)->commit = NULL;
373 84630254 2022-09-02 thomas (*qs)->file = NULL;
374 84630254 2022-09-02 thomas (*qs)->folder = NULL;
375 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
376 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
378 8a35f56c 2022-07-16 thomas return error;
381 8a35f56c 2022-07-16 thomas static const struct got_error *
382 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
384 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
385 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
386 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
388 8a35f56c 2022-07-16 thomas if (qst == NULL)
389 8a35f56c 2022-07-16 thomas return error;
391 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
392 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
393 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
395 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
396 8a35f56c 2022-07-16 thomas tok1_end = tok1;
398 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
399 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
401 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
402 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
403 8a35f56c 2022-07-16 thomas free(tok1);
404 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
407 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
408 8a35f56c 2022-07-16 thomas tok2_end = tok2;
410 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
411 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
412 8a35f56c 2022-07-16 thomas if (tok2_end) {
413 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
414 8a35f56c 2022-07-16 thomas tok2_end);
415 8a35f56c 2022-07-16 thomas if (error)
416 8a35f56c 2022-07-16 thomas goto err;
418 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
420 8a35f56c 2022-07-16 thomas free(tok2);
421 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
423 8a35f56c 2022-07-16 thomas free(tok1);
424 8a35f56c 2022-07-16 thomas return error;
426 8a35f56c 2022-07-16 thomas free(tok2);
427 8a35f56c 2022-07-16 thomas free(tok1);
428 8a35f56c 2022-07-16 thomas return error;
432 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
434 8a35f56c 2022-07-16 thomas static const struct got_error *
435 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
437 2ac684a4 2022-09-03 thomas char *p, *q;
438 2ac684a4 2022-09-03 thomas char hex[3];
439 2ac684a4 2022-09-03 thomas unsigned long x;
441 2ac684a4 2022-09-03 thomas hex[2] = '\0';
442 2ac684a4 2022-09-03 thomas p = q = url;
444 2ac684a4 2022-09-03 thomas while (*p != '\0') {
445 2ac684a4 2022-09-03 thomas switch (*p) {
446 2ac684a4 2022-09-03 thomas case '%':
447 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
448 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
449 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
450 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
451 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
453 2ac684a4 2022-09-03 thomas hex[0] = p[1];
454 2ac684a4 2022-09-03 thomas hex[1] = p[2];
457 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
458 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
460 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
461 2ac684a4 2022-09-03 thomas *q = (char)x;
471 2ac684a4 2022-09-03 thomas *q = '\0';
473 2ac684a4 2022-09-03 thomas return NULL;
476 2ac684a4 2022-09-03 thomas static const struct got_error *
477 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
479 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
480 8a35f56c 2022-07-16 thomas const char *errstr;
481 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
483 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
484 2ac684a4 2022-09-03 thomas if (error)
485 2ac684a4 2022-09-03 thomas return error;
487 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
488 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
489 8a35f56c 2022-07-16 thomas continue;
491 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
492 8a35f56c 2022-07-16 thomas case ACTION:
493 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
494 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
495 8a35f56c 2022-07-16 thomas continue;
496 8a35f56c 2022-07-16 thomas else if (strcmp(value,
497 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
498 8a35f56c 2022-07-16 thomas (*qs)->action =
499 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
500 8a35f56c 2022-07-16 thomas goto qa_found;
503 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
504 8a35f56c 2022-07-16 thomas qa_found:
506 8a35f56c 2022-07-16 thomas case COMMIT:
507 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
508 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
509 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
510 8a35f56c 2022-07-16 thomas __func__);
511 8a35f56c 2022-07-16 thomas goto done;
514 8a35f56c 2022-07-16 thomas case RFILE:
515 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
516 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
517 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
518 8a35f56c 2022-07-16 thomas __func__);
519 8a35f56c 2022-07-16 thomas goto done;
522 8a35f56c 2022-07-16 thomas case FOLDER:
523 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
524 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
525 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
526 8a35f56c 2022-07-16 thomas __func__);
527 8a35f56c 2022-07-16 thomas goto done;
530 8a35f56c 2022-07-16 thomas case HEADREF:
531 b0764984 2022-09-02 thomas free((*qs)->headref);
532 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
533 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
534 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
535 8a35f56c 2022-07-16 thomas __func__);
536 8a35f56c 2022-07-16 thomas goto done;
539 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
540 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
542 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
543 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
544 8a35f56c 2022-07-16 thomas if (errstr) {
545 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
546 8a35f56c 2022-07-16 thomas __func__, errstr);
547 8a35f56c 2022-07-16 thomas goto done;
549 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
550 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
552 8a35f56c 2022-07-16 thomas case PATH:
553 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
554 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
555 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
556 8a35f56c 2022-07-16 thomas __func__);
557 8a35f56c 2022-07-16 thomas goto done;
560 8a35f56c 2022-07-16 thomas case PAGE:
561 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
563 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
564 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
565 8a35f56c 2022-07-16 thomas if (errstr) {
566 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
567 8a35f56c 2022-07-16 thomas __func__, errstr);
568 8a35f56c 2022-07-16 thomas goto done;
570 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
571 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
578 8a35f56c 2022-07-16 thomas return error;
582 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
584 8a35f56c 2022-07-16 thomas if (rt != NULL) {
585 8a35f56c 2022-07-16 thomas free(rt->commit_id);
586 217813df 2022-09-02 thomas free(rt->tag_name);
587 217813df 2022-09-02 thomas free(rt->tag_commit);
588 217813df 2022-09-02 thomas free(rt->commit_msg);
589 8a35f56c 2022-07-16 thomas free(rt->tagger);
591 8a35f56c 2022-07-16 thomas free(rt);
595 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
597 8a35f56c 2022-07-16 thomas if (rc != NULL) {
598 8a35f56c 2022-07-16 thomas free(rc->path);
599 8a35f56c 2022-07-16 thomas free(rc->refs_str);
600 8a35f56c 2022-07-16 thomas free(rc->commit_id);
601 8a35f56c 2022-07-16 thomas free(rc->parent_id);
602 8a35f56c 2022-07-16 thomas free(rc->tree_id);
603 8a35f56c 2022-07-16 thomas free(rc->author);
604 8a35f56c 2022-07-16 thomas free(rc->committer);
605 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
607 8a35f56c 2022-07-16 thomas free(rc);
610 8a35f56c 2022-07-16 thomas static void
611 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
613 8a35f56c 2022-07-16 thomas if (qs != NULL) {
614 8a35f56c 2022-07-16 thomas free(qs->commit);
615 8a35f56c 2022-07-16 thomas free(qs->file);
616 8a35f56c 2022-07-16 thomas free(qs->folder);
617 8a35f56c 2022-07-16 thomas free(qs->headref);
618 8a35f56c 2022-07-16 thomas free(qs->path);
620 8a35f56c 2022-07-16 thomas free(qs);
623 8a35f56c 2022-07-16 thomas static void
624 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
626 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
627 8a35f56c 2022-07-16 thomas free(repo_dir->name);
628 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
629 8a35f56c 2022-07-16 thomas free(repo_dir->description);
630 8a35f56c 2022-07-16 thomas free(repo_dir->url);
631 8a35f56c 2022-07-16 thomas free(repo_dir->age);
632 8a35f56c 2022-07-16 thomas free(repo_dir->path);
634 8a35f56c 2022-07-16 thomas free(repo_dir);
638 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
640 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
641 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
643 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
644 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
645 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
647 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
648 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
649 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
651 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
652 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
653 6b42af1e 2022-09-02 thomas free(t->next_id);
654 6b42af1e 2022-09-02 thomas free(t->prev_id);
658 8a35f56c 2022-07-16 thomas const struct got_error *
659 8a35f56c 2022-07-16 thomas gotweb_render_content_type(struct request *c, const uint8_t *type)
661 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
662 0b75e088 2022-08-27 thomas "object-src 'none';";
664 0b75e088 2022-08-27 thomas fcgi_printf(c,
665 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
666 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
667 0b75e088 2022-08-27 thomas csp, type);
668 79393471 2022-08-27 thomas return NULL;
671 8a35f56c 2022-07-16 thomas const struct got_error *
672 8a35f56c 2022-07-16 thomas gotweb_render_content_type_file(struct request *c, const uint8_t *type,
673 8a35f56c 2022-07-16 thomas char *file)
675 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
676 8a35f56c 2022-07-16 thomas "Content-disposition: attachment; filename=%s\r\n\r\n",
677 79393471 2022-08-27 thomas type, file);
678 79393471 2022-08-27 thomas return NULL;
682 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
683 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
685 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
686 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
687 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
689 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
691 8a35f56c 2022-07-16 thomas switch(qs->action) {
692 8a35f56c 2022-07-16 thomas case INDEX:
693 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
694 2f4f0731 2022-12-30 thomas *have_prev = 1;
695 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
696 55144267 2022-09-07 thomas .action = -1,
697 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
698 55144267 2022-09-07 thomas .page = -1,
701 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
702 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
703 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
704 2f4f0731 2022-12-30 thomas *have_next = 1;
705 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
706 2f4f0731 2022-12-30 thomas .action = -1,
707 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
708 2f4f0731 2022-12-30 thomas .page = -1,
712 8a35f56c 2022-07-16 thomas case BRIEFS:
713 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
714 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
715 2f4f0731 2022-12-30 thomas *have_prev = 1;
716 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
717 55144267 2022-09-07 thomas .action = BRIEFS,
718 55144267 2022-09-07 thomas .index_page = -1,
719 55144267 2022-09-07 thomas .page = qs->page - 1,
720 55144267 2022-09-07 thomas .path = qs->path,
721 55144267 2022-09-07 thomas .commit = t->prev_id,
722 55144267 2022-09-07 thomas .headref = qs->headref,
725 2f4f0731 2022-12-30 thomas if (t->next_id) {
726 2f4f0731 2022-12-30 thomas *have_next = 1;
727 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
728 2f4f0731 2022-12-30 thomas .action = BRIEFS,
729 2f4f0731 2022-12-30 thomas .index_page = -1,
730 2f4f0731 2022-12-30 thomas .page = qs->page + 1,
731 2f4f0731 2022-12-30 thomas .path = qs->path,
732 2f4f0731 2022-12-30 thomas .commit = t->next_id,
733 2f4f0731 2022-12-30 thomas .headref = qs->headref,
737 8a35f56c 2022-07-16 thomas case COMMITS:
738 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
739 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
740 2f4f0731 2022-12-30 thomas *have_prev = 1;
741 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
742 8ea2e76e 2022-12-30 thomas .action = COMMITS,
743 55144267 2022-09-07 thomas .index_page = -1,
744 55144267 2022-09-07 thomas .page = qs->page - 1,
745 55144267 2022-09-07 thomas .path = qs->path,
746 55144267 2022-09-07 thomas .commit = t->prev_id,
747 55144267 2022-09-07 thomas .headref = qs->headref,
748 55144267 2022-09-07 thomas .folder = qs->folder,
749 55144267 2022-09-07 thomas .file = qs->file,
752 2f4f0731 2022-12-30 thomas if (t->next_id) {
753 2f4f0731 2022-12-30 thomas *have_next = 1;
754 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
755 8ea2e76e 2022-12-30 thomas .action = COMMITS,
756 55144267 2022-09-07 thomas .index_page = -1,
757 55144267 2022-09-07 thomas .page = qs->page + 1,
758 55144267 2022-09-07 thomas .path = qs->path,
759 55144267 2022-09-07 thomas .commit = t->next_id,
760 55144267 2022-09-07 thomas .headref = qs->headref,
761 55144267 2022-09-07 thomas .folder = qs->folder,
762 55144267 2022-09-07 thomas .file = qs->file,
766 8a35f56c 2022-07-16 thomas case TAGS:
767 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
768 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
769 2f4f0731 2022-12-30 thomas *have_prev = 1;
770 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
771 2f4f0731 2022-12-30 thomas .action = TAGS,
772 2f4f0731 2022-12-30 thomas .index_page = -1,
773 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
774 2f4f0731 2022-12-30 thomas .path = qs->path,
775 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
776 2f4f0731 2022-12-30 thomas .headref = qs->headref,
779 8a35f56c 2022-07-16 thomas if (t->next_id) {
780 2f4f0731 2022-12-30 thomas *have_next = 1;
781 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
782 55144267 2022-09-07 thomas .action = TAGS,
783 55144267 2022-09-07 thomas .index_page = -1,
784 55144267 2022-09-07 thomas .page = qs->page + 1,
785 55144267 2022-09-07 thomas .path = qs->path,
786 55144267 2022-09-07 thomas .commit = t->next_id,
787 55144267 2022-09-07 thomas .headref = qs->headref,
794 8a35f56c 2022-07-16 thomas static const struct got_error *
795 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
797 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
798 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
799 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
800 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
801 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
803 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
804 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
805 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
806 e7e5fa49 2022-12-30 thomas int type;
808 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
809 8a35f56c 2022-07-16 thomas if (d == NULL) {
810 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
811 8a35f56c 2022-07-16 thomas return error;
814 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
815 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
816 f530ab0e 2022-09-02 thomas sd_dent = NULL;
817 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
818 8a35f56c 2022-07-16 thomas goto done;
821 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
822 8a35f56c 2022-07-16 thomas goto done;
824 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
825 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
828 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
829 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
830 24240f6a 2022-11-23 thomas d_skipped++;
831 24240f6a 2022-11-23 thomas continue;
834 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
835 24240f6a 2022-11-23 thomas sd_dent[d_i]);
836 24240f6a 2022-11-23 thomas if (error)
837 24240f6a 2022-11-23 thomas goto done;
838 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
839 24240f6a 2022-11-23 thomas d_skipped++;
840 8a35f56c 2022-07-16 thomas continue;
843 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
844 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
845 8a35f56c 2022-07-16 thomas t->prev_disp++;
846 8a35f56c 2022-07-16 thomas continue;
849 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
850 8a35f56c 2022-07-16 thomas if (error)
851 8a35f56c 2022-07-16 thomas goto done;
853 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
854 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
855 8a35f56c 2022-07-16 thomas error = NULL;
856 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
857 8a35f56c 2022-07-16 thomas repo_dir = NULL;
858 24240f6a 2022-11-23 thomas d_skipped++;
859 8a35f56c 2022-07-16 thomas continue;
861 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
862 24240f6a 2022-11-23 thomas goto done;
864 8a35f56c 2022-07-16 thomas d_disp++;
865 8a35f56c 2022-07-16 thomas t->prev_disp++;
867 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
868 55144267 2022-09-07 thomas goto done;
870 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
871 8a35f56c 2022-07-16 thomas repo_dir = NULL;
872 8a35f56c 2022-07-16 thomas t->next_disp++;
873 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
876 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
878 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
879 79393471 2022-08-27 thomas goto done;
880 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
881 79393471 2022-08-27 thomas goto done;
882 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
883 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
884 79393471 2022-08-27 thomas goto done;
886 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
887 8a35f56c 2022-07-16 thomas goto done;
889 f530ab0e 2022-09-02 thomas if (sd_dent) {
890 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
891 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
892 f530ab0e 2022-09-02 thomas free(sd_dent);
894 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
895 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
896 8a35f56c 2022-07-16 thomas return error;
899 8a35f56c 2022-07-16 thomas static const struct got_error *
900 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
902 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
903 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
904 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
905 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
908 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
909 8a35f56c 2022-07-16 thomas if (error)
910 8a35f56c 2022-07-16 thomas return error;
912 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
914 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
915 255f4022 2022-08-27 thomas if (error)
916 255f4022 2022-08-27 thomas goto done;
917 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
918 8a35f56c 2022-07-16 thomas if (error)
919 8a35f56c 2022-07-16 thomas goto done;
921 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
922 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
923 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
924 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
925 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
926 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
927 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
928 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
929 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
930 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
931 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
932 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
933 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
934 79393471 2022-08-27 thomas "<div id='blame'>\n",
937 79393471 2022-08-27 thomas if (r == -1)
938 8a35f56c 2022-07-16 thomas goto done;
940 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
941 8a35f56c 2022-07-16 thomas if (error)
942 8a35f56c 2022-07-16 thomas goto done;
944 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
945 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
947 8a35f56c 2022-07-16 thomas free(age);
948 255f4022 2022-08-27 thomas free(msg);
949 8a35f56c 2022-07-16 thomas return error;
952 8a35f56c 2022-07-16 thomas static const struct got_error *
953 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
955 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
956 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
957 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
958 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
959 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
960 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
961 55144267 2022-09-07 thomas char *escaped_refname = NULL;
962 8a35f56c 2022-07-16 thomas char *age = NULL;
965 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
967 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
968 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
969 8a35f56c 2022-07-16 thomas if (error)
970 8a35f56c 2022-07-16 thomas goto done;
972 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
973 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
974 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
975 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
976 79393471 2022-08-27 thomas if (r == -1)
977 8a35f56c 2022-07-16 thomas goto done;
979 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
980 255f4022 2022-08-27 thomas const char *refname = NULL;
982 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
983 8a35f56c 2022-07-16 thomas continue;
985 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
986 8a35f56c 2022-07-16 thomas if (refname == NULL) {
987 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
988 8a35f56c 2022-07-16 thomas goto done;
990 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
991 8a35f56c 2022-07-16 thomas continue;
993 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&age, c, refname, TM_DIFF);
994 8a35f56c 2022-07-16 thomas if (error)
995 8a35f56c 2022-07-16 thomas goto done;
997 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
998 8a35f56c 2022-07-16 thomas refname += 11;
999 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1000 255f4022 2022-08-27 thomas if (error)
1001 255f4022 2022-08-27 thomas goto done;
1003 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1004 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1005 79393471 2022-08-27 thomas "<div class='branches_space'> </div>\n"
1006 55144267 2022-09-07 thomas "<div class='branch'>", age);
1007 55144267 2022-09-07 thomas if (r == -1)
1008 55144267 2022-09-07 thomas goto done;
1010 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1011 55144267 2022-09-07 thomas .action = SUMMARY,
1012 55144267 2022-09-07 thomas .index_page = -1,
1013 55144267 2022-09-07 thomas .page = -1,
1014 55144267 2022-09-07 thomas .path = qs->path,
1015 55144267 2022-09-07 thomas .headref = refname,
1016 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1017 55144267 2022-09-07 thomas if (r == -1)
1018 55144267 2022-09-07 thomas goto done;
1020 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1021 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1022 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1023 55144267 2022-09-07 thomas goto done;
1025 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1026 55144267 2022-09-07 thomas .action = SUMMARY,
1027 55144267 2022-09-07 thomas .index_page = -1,
1028 55144267 2022-09-07 thomas .page = -1,
1029 55144267 2022-09-07 thomas .path = qs->path,
1030 55144267 2022-09-07 thomas .headref = refname,
1031 55144267 2022-09-07 thomas }, "summary");
1032 55144267 2022-09-07 thomas if (r == -1)
1033 55144267 2022-09-07 thomas goto done;
1035 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1036 55144267 2022-09-07 thomas goto done;
1038 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1039 55144267 2022-09-07 thomas .action = BRIEFS,
1040 55144267 2022-09-07 thomas .index_page = -1,
1041 55144267 2022-09-07 thomas .page = -1,
1042 55144267 2022-09-07 thomas .path = qs->path,
1043 55144267 2022-09-07 thomas .headref = refname,
1044 55144267 2022-09-07 thomas }, "commit briefs");
1045 55144267 2022-09-07 thomas if (r == -1)
1046 55144267 2022-09-07 thomas goto done;
1048 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1049 55144267 2022-09-07 thomas goto done;
1051 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1052 55144267 2022-09-07 thomas .action = COMMITS,
1053 55144267 2022-09-07 thomas .index_page = -1,
1054 55144267 2022-09-07 thomas .page = -1,
1055 55144267 2022-09-07 thomas .path = qs->path,
1056 55144267 2022-09-07 thomas .headref = refname,
1057 55144267 2022-09-07 thomas }, "commits");
1058 55144267 2022-09-07 thomas if (r == -1)
1059 55144267 2022-09-07 thomas goto done;
1061 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1062 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1063 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1064 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1065 79393471 2022-08-27 thomas if (r == -1)
1066 8a35f56c 2022-07-16 thomas goto done;
1068 8a35f56c 2022-07-16 thomas free(age);
1069 8a35f56c 2022-07-16 thomas age = NULL;
1070 55144267 2022-09-07 thomas free(escaped_refname);
1071 55144267 2022-09-07 thomas escaped_refname = NULL;
1073 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1075 f95e65a1 2022-09-02 thomas free(age);
1076 55144267 2022-09-07 thomas free(escaped_refname);
1077 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1078 8a35f56c 2022-07-16 thomas return error;
1081 8a35f56c 2022-07-16 thomas static const struct got_error *
1082 8a35f56c 2022-07-16 thomas gotweb_render_tree(struct request *c)
1084 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1085 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1086 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1087 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
1090 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1091 8a35f56c 2022-07-16 thomas if (error)
1092 8a35f56c 2022-07-16 thomas return error;
1094 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1096 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1097 8a35f56c 2022-07-16 thomas if (error)
1098 8a35f56c 2022-07-16 thomas goto done;
1100 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1101 255f4022 2022-08-27 thomas if (error)
1102 255f4022 2022-08-27 thomas goto done;
1104 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1105 79393471 2022-08-27 thomas "<div id='tree_title'>Tree</div>\n"
1106 79393471 2022-08-27 thomas "</div>\n" /* #tree_title_wrapper */
1107 79393471 2022-08-27 thomas "<div id='tree_content'>\n"
1108 79393471 2022-08-27 thomas "<div id='tree_header_wrapper'>\n"
1109 79393471 2022-08-27 thomas "<div id='tree_header'>\n"
1110 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1111 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1112 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1113 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1114 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1115 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1116 79393471 2022-08-27 thomas "</div>\n" /* #tree_header */
1117 79393471 2022-08-27 thomas "</div>\n" /* #tree_header_wrapper */
1118 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1119 79393471 2022-08-27 thomas "<div id='tree'>\n",
1120 79393471 2022-08-27 thomas rc->tree_id,
1123 79393471 2022-08-27 thomas if (r == -1)
1124 8a35f56c 2022-07-16 thomas goto done;
1126 8a35f56c 2022-07-16 thomas error = got_output_repo_tree(c);
1127 8a35f56c 2022-07-16 thomas if (error)
1128 8a35f56c 2022-07-16 thomas goto done;
1130 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree */
1131 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tree_content */
1133 2ad628aa 2022-08-31 thomas free(age);
1134 255f4022 2022-08-27 thomas free(msg);
1135 8a35f56c 2022-07-16 thomas return error;
1138 8a35f56c 2022-07-16 thomas static const struct got_error *
1139 8a35f56c 2022-07-16 thomas gotweb_render_diff(struct request *c)
1141 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1142 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1143 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1144 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1147 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1148 8a35f56c 2022-07-16 thomas if (error)
1149 8a35f56c 2022-07-16 thomas return error;
1151 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1153 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1154 8a35f56c 2022-07-16 thomas if (error)
1155 8a35f56c 2022-07-16 thomas goto done;
1156 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1157 8a35f56c 2022-07-16 thomas if (error)
1158 8a35f56c 2022-07-16 thomas goto done;
1159 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1160 255f4022 2022-08-27 thomas if (error)
1161 255f4022 2022-08-27 thomas goto done;
1163 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1164 79393471 2022-08-27 thomas "<div id='diff_title'>Commit Diff</div>\n"
1165 79393471 2022-08-27 thomas "</div>\n" /* #diff_title_wrapper */
1166 79393471 2022-08-27 thomas "<div id='diff_content'>\n"
1167 79393471 2022-08-27 thomas "<div id='diff_header_wrapper'>\n"
1168 79393471 2022-08-27 thomas "<div id='diff_header'>\n"
1169 79393471 2022-08-27 thomas "<div id='header_diff_title'>Diff:</div>\n"
1170 79393471 2022-08-27 thomas "<div id='header_diff'>%s<br />%s</div>\n"
1171 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1172 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1173 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1174 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1175 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1176 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1177 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1178 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1179 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1180 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1181 79393471 2022-08-27 thomas "</div>\n" /* #diff_header */
1182 79393471 2022-08-27 thomas "</div>\n" /* #diff_header_wrapper */
1183 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1184 79393471 2022-08-27 thomas "<div id='diff'>\n",
1185 79393471 2022-08-27 thomas rc->parent_id, rc->commit_id,
1186 79393471 2022-08-27 thomas rc->commit_id,
1187 79393471 2022-08-27 thomas rc->tree_id,
1191 79393471 2022-08-27 thomas if (r == -1)
1192 8a35f56c 2022-07-16 thomas goto done;
1194 8a35f56c 2022-07-16 thomas error = got_output_repo_diff(c);
1195 8a35f56c 2022-07-16 thomas if (error)
1196 8a35f56c 2022-07-16 thomas goto done;
1198 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff */
1199 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff_content */
1201 8a35f56c 2022-07-16 thomas free(age);
1202 8a35f56c 2022-07-16 thomas free(author);
1203 255f4022 2022-08-27 thomas free(msg);
1204 8a35f56c 2022-07-16 thomas return error;
1207 8a35f56c 2022-07-16 thomas static const struct got_error *
1208 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1210 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1211 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1212 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1215 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1216 8a35f56c 2022-07-16 thomas goto done;
1218 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1219 79393471 2022-08-27 thomas r = fcgi_printf(c,
1220 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1221 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1222 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1223 79393471 2022-08-27 thomas if (r == -1)
1224 79393471 2022-08-27 thomas goto done;
1227 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1228 79393471 2022-08-27 thomas r = fcgi_printf(c,
1229 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1230 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1231 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1232 79393471 2022-08-27 thomas if (r == -1)
1233 79393471 2022-08-27 thomas goto done;
1236 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1237 79393471 2022-08-27 thomas r = fcgi_printf(c,
1238 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1239 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1240 79393471 2022-08-27 thomas t->repo_dir->age);
1241 79393471 2022-08-27 thomas if (r == -1)
1242 79393471 2022-08-27 thomas goto done;
1245 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1246 79393471 2022-08-27 thomas r = fcgi_printf(c,
1247 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1248 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1249 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1250 79393471 2022-08-27 thomas if (r == -1)
1251 79393471 2022-08-27 thomas goto done;
1254 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1255 79393471 2022-08-27 thomas if (r == -1)
1256 8a35f56c 2022-07-16 thomas goto done;
1258 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
1259 8a35f56c 2022-07-16 thomas goto done;
1261 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1262 8a35f56c 2022-07-16 thomas if (error) {
1263 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1264 8a35f56c 2022-07-16 thomas goto done;
1267 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1268 8a35f56c 2022-07-16 thomas if (error)
1269 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1271 8a35f56c 2022-07-16 thomas return error;
1274 8a35f56c 2022-07-16 thomas static const struct got_error *
1275 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1277 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1278 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1279 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1280 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1282 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1283 8a35f56c 2022-07-16 thomas if (error)
1284 8a35f56c 2022-07-16 thomas goto done;
1286 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1287 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1288 8a35f56c 2022-07-16 thomas "bad commit id");
1289 8a35f56c 2022-07-16 thomas goto done;
1292 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1294 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1295 8a35f56c 2022-07-16 thomas if (error)
1296 8a35f56c 2022-07-16 thomas goto done;
1297 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1298 8a35f56c 2022-07-16 thomas if (error)
1299 8a35f56c 2022-07-16 thomas goto done;
1300 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1301 255f4022 2022-08-27 thomas if (error)
1302 255f4022 2022-08-27 thomas goto done;
1304 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1305 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1306 c8d0196f 2022-09-02 thomas tagname += 5;
1307 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1308 255f4022 2022-08-27 thomas if (error)
1309 255f4022 2022-08-27 thomas goto done;
1311 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1312 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1313 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1314 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1315 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1316 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1317 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1318 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1319 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1320 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1321 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1322 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1323 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1324 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1325 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1326 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1327 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1328 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1329 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1330 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1331 79393471 2022-08-27 thomas rt->commit_id,
1332 255f4022 2022-08-27 thomas tagname,
1336 79393471 2022-08-27 thomas rt->tag_commit);
1339 8a35f56c 2022-07-16 thomas free(age);
1340 8a35f56c 2022-07-16 thomas free(author);
1341 255f4022 2022-08-27 thomas free(msg);
1342 8a35f56c 2022-07-16 thomas return error;
1345 8a35f56c 2022-07-16 thomas static const struct got_error *
1346 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1348 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1349 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1350 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1351 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1352 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1353 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1354 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1355 79393471 2022-08-27 thomas int r, commit_found = 0;
1357 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1358 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1359 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1361 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1362 8a35f56c 2022-07-16 thomas if (error)
1363 8a35f56c 2022-07-16 thomas goto done;
1365 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1366 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1367 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1368 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1369 79393471 2022-08-27 thomas if (r == -1)
1370 8a35f56c 2022-07-16 thomas goto done;
1372 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1373 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1374 79393471 2022-08-27 thomas "This repository contains no tags");
1375 79393471 2022-08-27 thomas if (r == -1)
1376 8a35f56c 2022-07-16 thomas goto done;
1379 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1380 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1381 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1382 8a35f56c 2022-07-16 thomas continue;
1384 8a35f56c 2022-07-16 thomas commit_found = 1;
1386 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1387 8a35f56c 2022-07-16 thomas if (error)
1388 8a35f56c 2022-07-16 thomas goto done;
1390 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1391 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1392 c8d0196f 2022-09-02 thomas tagname += 10;
1393 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1394 255f4022 2022-08-27 thomas if (error)
1395 255f4022 2022-08-27 thomas goto done;
1397 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1398 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1399 8a35f56c 2022-07-16 thomas if (newline)
1400 8a35f56c 2022-07-16 thomas *newline = '\0';
1401 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1402 255f4022 2022-08-27 thomas if (error)
1403 255f4022 2022-08-27 thomas goto done;
1406 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1407 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1408 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1409 55144267 2022-09-07 thomas goto done;
1411 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1412 55144267 2022-09-07 thomas .action = TAG,
1413 55144267 2022-09-07 thomas .index_page = -1,
1414 55144267 2022-09-07 thomas .page = -1,
1415 55144267 2022-09-07 thomas .path = repo_dir->name,
1416 55144267 2022-09-07 thomas .commit = rt->commit_id,
1417 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1418 55144267 2022-09-07 thomas if (r == -1)
1419 55144267 2022-09-07 thomas goto done;
1421 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1422 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1423 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1424 55144267 2022-09-07 thomas goto done;
1426 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1427 55144267 2022-09-07 thomas .action = TAG,
1428 55144267 2022-09-07 thomas .index_page = -1,
1429 55144267 2022-09-07 thomas .page = -1,
1430 55144267 2022-09-07 thomas .path = repo_dir->name,
1431 55144267 2022-09-07 thomas .commit = rt->commit_id,
1432 55144267 2022-09-07 thomas }, "tag");
1433 55144267 2022-09-07 thomas if (r == -1)
1434 55144267 2022-09-07 thomas goto done;
1436 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1437 55144267 2022-09-07 thomas goto done;
1439 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1440 55144267 2022-09-07 thomas .action = BRIEFS,
1441 55144267 2022-09-07 thomas .index_page = -1,
1442 55144267 2022-09-07 thomas .page = -1,
1443 55144267 2022-09-07 thomas .path = repo_dir->name,
1444 55144267 2022-09-07 thomas .commit = rt->commit_id,
1445 55144267 2022-09-07 thomas }, "commit briefs");
1446 55144267 2022-09-07 thomas if (r == -1)
1447 55144267 2022-09-07 thomas goto done;
1449 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1450 55144267 2022-09-07 thomas goto done;
1452 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1453 55144267 2022-09-07 thomas .action = COMMITS,
1454 55144267 2022-09-07 thomas .index_page = -1,
1455 55144267 2022-09-07 thomas .page = -1,
1456 55144267 2022-09-07 thomas .path = repo_dir->name,
1457 55144267 2022-09-07 thomas .commit = rt->commit_id,
1458 55144267 2022-09-07 thomas }, "commits");
1459 55144267 2022-09-07 thomas if (r == -1)
1460 55144267 2022-09-07 thomas goto done;
1462 55144267 2022-09-07 thomas r = fcgi_printf(c,
1463 79393471 2022-08-27 thomas "</div>\n" /* .navs */
1464 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
1465 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
1466 79393471 2022-08-27 thomas if (r == -1)
1467 8a35f56c 2022-07-16 thomas goto done;
1469 8a35f56c 2022-07-16 thomas free(age);
1470 8a35f56c 2022-07-16 thomas age = NULL;
1471 255f4022 2022-08-27 thomas free(tagname);
1472 255f4022 2022-08-27 thomas tagname = NULL;
1473 255f4022 2022-08-27 thomas free(msg);
1474 255f4022 2022-08-27 thomas msg = NULL;
1476 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1477 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
1478 8a35f56c 2022-07-16 thomas goto done;
1480 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
1482 8a35f56c 2022-07-16 thomas free(age);
1483 255f4022 2022-08-27 thomas free(tagname);
1484 255f4022 2022-08-27 thomas free(msg);
1485 8a35f56c 2022-07-16 thomas return error;
1488 8a35f56c 2022-07-16 thomas const struct got_error *
1489 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
1491 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1492 8a35f56c 2022-07-16 thomas struct escape_pair {
1494 8a35f56c 2022-07-16 thomas const char *s;
1495 8a35f56c 2022-07-16 thomas } esc[] = {
1496 8a35f56c 2022-07-16 thomas { '>', ">" },
1497 8a35f56c 2022-07-16 thomas { '<', "<" },
1498 8a35f56c 2022-07-16 thomas { '&', "&" },
1499 8a35f56c 2022-07-16 thomas { '"', """ },
1500 8a35f56c 2022-07-16 thomas { '\'', "'" },
1501 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
1503 8a35f56c 2022-07-16 thomas size_t orig_len, len;
1504 8a35f56c 2022-07-16 thomas int i, j, x;
1506 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
1507 8a35f56c 2022-07-16 thomas len = orig_len;
1508 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1509 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1510 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1511 8a35f56c 2022-07-16 thomas continue;
1512 8a35f56c 2022-07-16 thomas len += strlen(esc[j].s) - 1 /* escaped char */;
1516 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1517 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
1518 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1521 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1522 8a35f56c 2022-07-16 thomas int escaped = 0;
1523 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1524 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1525 8a35f56c 2022-07-16 thomas continue;
1527 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
1528 8a35f56c 2022-07-16 thomas >= len + 1) {
1529 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
1530 8a35f56c 2022-07-16 thomas goto done;
1532 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
1533 8a35f56c 2022-07-16 thomas escaped = 1;
1536 8a35f56c 2022-07-16 thomas if (!escaped) {
1537 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
1542 8a35f56c 2022-07-16 thomas if (error) {
1543 8a35f56c 2022-07-16 thomas free(*escaped_html);
1544 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
1545 8a35f56c 2022-07-16 thomas } else {
1546 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
1549 8a35f56c 2022-07-16 thomas return error;
1552 55144267 2022-09-07 thomas static inline int
1553 55144267 2022-09-07 thomas should_urlencode(int c)
1555 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
1556 55144267 2022-09-07 thomas return 1;
1558 55144267 2022-09-07 thomas switch (c) {
1559 55144267 2022-09-07 thomas /* gen-delim */
1560 55144267 2022-09-07 thomas case ':':
1561 55144267 2022-09-07 thomas case '/':
1562 55144267 2022-09-07 thomas case '?':
1563 55144267 2022-09-07 thomas case '#':
1564 55144267 2022-09-07 thomas case '[':
1565 55144267 2022-09-07 thomas case ']':
1566 55144267 2022-09-07 thomas case '@':
1567 55144267 2022-09-07 thomas /* sub-delims */
1568 55144267 2022-09-07 thomas case '!':
1569 55144267 2022-09-07 thomas case '$':
1570 55144267 2022-09-07 thomas case '&':
1571 55144267 2022-09-07 thomas case '\'':
1572 55144267 2022-09-07 thomas case '(':
1573 55144267 2022-09-07 thomas case ')':
1574 55144267 2022-09-07 thomas case '*':
1575 55144267 2022-09-07 thomas case '+':
1576 55144267 2022-09-07 thomas case ',':
1577 55144267 2022-09-07 thomas case ';':
1578 55144267 2022-09-07 thomas case '=':
1579 55144267 2022-09-07 thomas return 1;
1580 55144267 2022-09-07 thomas default:
1581 55144267 2022-09-07 thomas return 0;
1585 55144267 2022-09-07 thomas static char *
1586 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
1588 55144267 2022-09-07 thomas const char *s;
1589 55144267 2022-09-07 thomas char *escaped;
1590 55144267 2022-09-07 thomas size_t i, len;
1591 55144267 2022-09-07 thomas int a, b;
1593 55144267 2022-09-07 thomas len = 0;
1594 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1596 55144267 2022-09-07 thomas if (should_urlencode(*s))
1597 55144267 2022-09-07 thomas len += 2;
1600 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1601 55144267 2022-09-07 thomas if (escaped == NULL)
1602 55144267 2022-09-07 thomas return NULL;
1605 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1606 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1607 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1608 55144267 2022-09-07 thomas b = (*s & 0x0F);
1610 55144267 2022-09-07 thomas escaped[i++] = '%';
1611 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1612 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1614 55144267 2022-09-07 thomas escaped[i++] = *s;
1617 55144267 2022-09-07 thomas return escaped;
1620 e7e5fa49 2022-12-30 thomas const char *
1621 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1623 55144267 2022-09-07 thomas switch (action) {
1624 55144267 2022-09-07 thomas case BLAME:
1625 55144267 2022-09-07 thomas return "blame";
1626 55144267 2022-09-07 thomas case BLOB:
1627 55144267 2022-09-07 thomas return "blob";
1628 55144267 2022-09-07 thomas case BRIEFS:
1629 55144267 2022-09-07 thomas return "briefs";
1630 55144267 2022-09-07 thomas case COMMITS:
1631 55144267 2022-09-07 thomas return "commits";
1632 55144267 2022-09-07 thomas case DIFF:
1633 55144267 2022-09-07 thomas return "diff";
1634 55144267 2022-09-07 thomas case ERR:
1635 55144267 2022-09-07 thomas return "err";
1636 55144267 2022-09-07 thomas case INDEX:
1637 55144267 2022-09-07 thomas return "index";
1638 55144267 2022-09-07 thomas case SUMMARY:
1639 55144267 2022-09-07 thomas return "summary";
1640 55144267 2022-09-07 thomas case TAG:
1641 55144267 2022-09-07 thomas return "tag";
1642 55144267 2022-09-07 thomas case TAGS:
1643 55144267 2022-09-07 thomas return "tags";
1644 55144267 2022-09-07 thomas case TREE:
1645 55144267 2022-09-07 thomas return "tree";
1646 d6795e9f 2022-12-30 thomas case RSS:
1647 d6795e9f 2022-12-30 thomas return "rss";
1648 55144267 2022-09-07 thomas default:
1649 55144267 2022-09-07 thomas return NULL;
1654 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1656 55144267 2022-09-07 thomas const char *sep = "?", *action;
1657 55144267 2022-09-07 thomas char *tmp;
1660 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1661 55144267 2022-09-07 thomas if (action != NULL) {
1662 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1663 55144267 2022-09-07 thomas return -1;
1664 55144267 2022-09-07 thomas sep = "&";
1667 55144267 2022-09-07 thomas if (url->commit) {
1668 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1669 55144267 2022-09-07 thomas return -1;
1670 55144267 2022-09-07 thomas sep = "&";
1673 55144267 2022-09-07 thomas if (url->previd) {
1674 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1675 55144267 2022-09-07 thomas return -1;
1676 55144267 2022-09-07 thomas sep = "&";
1679 55144267 2022-09-07 thomas if (url->prevset) {
1680 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1681 55144267 2022-09-07 thomas return -1;
1682 55144267 2022-09-07 thomas sep = "&";
1685 55144267 2022-09-07 thomas if (url->file) {
1686 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1687 55144267 2022-09-07 thomas if (tmp == NULL)
1688 55144267 2022-09-07 thomas return -1;
1689 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1690 55144267 2022-09-07 thomas free(tmp);
1691 55144267 2022-09-07 thomas if (r == -1)
1692 55144267 2022-09-07 thomas return -1;
1693 55144267 2022-09-07 thomas sep = "&";
1696 55144267 2022-09-07 thomas if (url->folder) {
1697 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1698 55144267 2022-09-07 thomas if (tmp == NULL)
1699 55144267 2022-09-07 thomas return -1;
1700 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1701 55144267 2022-09-07 thomas free(tmp);
1702 55144267 2022-09-07 thomas if (r == -1)
1703 55144267 2022-09-07 thomas return -1;
1704 55144267 2022-09-07 thomas sep = "&";
1707 55144267 2022-09-07 thomas if (url->headref) {
1708 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1709 55144267 2022-09-07 thomas if (tmp == NULL)
1710 55144267 2022-09-07 thomas return -1;
1711 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1712 55144267 2022-09-07 thomas free(tmp);
1713 55144267 2022-09-07 thomas if (r == -1)
1714 55144267 2022-09-07 thomas return -1;
1715 55144267 2022-09-07 thomas sep = "&";
1718 55144267 2022-09-07 thomas if (url->index_page != -1) {
1719 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1720 55144267 2022-09-07 thomas url->index_page) == -1)
1721 55144267 2022-09-07 thomas return -1;
1722 55144267 2022-09-07 thomas sep = "&";
1725 55144267 2022-09-07 thomas if (url->path) {
1726 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1727 55144267 2022-09-07 thomas if (tmp == NULL)
1728 55144267 2022-09-07 thomas return -1;
1729 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1730 55144267 2022-09-07 thomas free(tmp);
1731 55144267 2022-09-07 thomas if (r == -1)
1732 55144267 2022-09-07 thomas return -1;
1733 55144267 2022-09-07 thomas sep = "&";
1736 55144267 2022-09-07 thomas if (url->page != -1) {
1737 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1738 55144267 2022-09-07 thomas return -1;
1739 55144267 2022-09-07 thomas sep = "&";
1742 55144267 2022-09-07 thomas return 0;
1746 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1748 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1749 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1751 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1752 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1753 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1754 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1755 d6795e9f 2022-12-30 thomas return -1;
1757 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1761 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1763 55144267 2022-09-07 thomas va_list ap;
1766 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
1767 55144267 2022-09-07 thomas return -1;
1769 e7e5fa49 2022-12-30 thomas if (gotweb_render_url(c, url) == -1)
1770 55144267 2022-09-07 thomas return -1;
1772 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
1773 55144267 2022-09-07 thomas return -1;
1775 55144267 2022-09-07 thomas va_start(ap, fmt);
1776 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
1777 55144267 2022-09-07 thomas va_end(ap);
1778 55144267 2022-09-07 thomas if (r == -1)
1779 55144267 2022-09-07 thomas return -1;
1781 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
1782 55144267 2022-09-07 thomas return -1;
1783 55144267 2022-09-07 thomas return 0;
1786 55e6cffd 2022-09-01 thomas static struct got_repository *
1787 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1791 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1792 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1793 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1796 55e6cffd 2022-09-01 thomas return NULL;
1799 8a35f56c 2022-07-16 thomas static const struct got_error *
1800 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1801 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1803 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1804 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1805 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1806 55e6cffd 2022-09-01 thomas int evicted = 0;
1808 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1809 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1810 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1811 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1812 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1813 55e6cffd 2022-09-01 thomas if (error)
1814 55e6cffd 2022-09-01 thomas return error;
1815 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1816 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1817 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1818 55e6cffd 2022-09-01 thomas evicted = 1;
1819 55e6cffd 2022-09-01 thomas } else {
1820 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1823 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1824 55e6cffd 2022-09-01 thomas if (error) {
1825 55e6cffd 2022-09-01 thomas if (evicted) {
1826 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1827 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1829 55e6cffd 2022-09-01 thomas return error;
1832 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1833 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1834 55e6cffd 2022-09-01 thomas if (evicted) {
1835 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1836 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1838 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1841 55e6cffd 2022-09-01 thomas cr->repo = repo;
1842 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1843 55e6cffd 2022-09-01 thomas *new = repo;
1844 55e6cffd 2022-09-01 thomas return NULL;
1847 55e6cffd 2022-09-01 thomas static const struct got_error *
1848 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1850 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1851 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1852 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1853 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1854 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1855 8a35f56c 2022-07-16 thomas DIR *dt;
1856 8a35f56c 2022-07-16 thomas char *dir_test;
1858 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1859 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1860 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1862 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1863 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1864 8a35f56c 2022-07-16 thomas free(dir_test);
1865 8a35f56c 2022-07-16 thomas } else {
1866 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1867 8a35f56c 2022-07-16 thomas dir_test = NULL;
1868 c2d3d9a0 2022-09-01 thomas goto done;
1871 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1872 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1873 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1875 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1876 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1877 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1878 8a35f56c 2022-07-16 thomas goto err;
1879 c2d3d9a0 2022-09-01 thomas } else {
1880 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1881 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1885 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1886 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1887 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1888 3991b2a5 2022-10-31 thomas goto err;
1891 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1892 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1893 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1894 55e6cffd 2022-09-01 thomas if (error)
1895 55e6cffd 2022-09-01 thomas goto err;
1897 55e6cffd 2022-09-01 thomas t->repo = repo;
1898 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1899 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1900 8a35f56c 2022-07-16 thomas if (error)
1901 8a35f56c 2022-07-16 thomas goto err;
1902 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1903 8a35f56c 2022-07-16 thomas if (error)
1904 8a35f56c 2022-07-16 thomas goto err;
1905 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1906 8a35f56c 2022-07-16 thomas if (error)
1907 8a35f56c 2022-07-16 thomas goto err;
1908 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1909 4606e6d4 2022-11-23 thomas dirfd(dt));
1911 8a35f56c 2022-07-16 thomas free(dir_test);
1912 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1913 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1914 8a35f56c 2022-07-16 thomas return error;
1917 8a35f56c 2022-07-16 thomas static const struct got_error *
1918 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1920 8a35f56c 2022-07-16 thomas const struct got_error *error;
1922 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1923 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1924 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1926 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1927 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1928 8a35f56c 2022-07-16 thomas free(*repo_dir);
1929 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1930 8a35f56c 2022-07-16 thomas return error;
1932 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1933 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1934 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1935 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
1936 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1938 8a35f56c 2022-07-16 thomas return NULL;
1941 8a35f56c 2022-07-16 thomas static const struct got_error *
1942 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1943 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1945 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1946 4606e6d4 2022-11-23 thomas struct stat sb;
1947 4606e6d4 2022-11-23 thomas int fd = -1;
1948 4606e6d4 2022-11-23 thomas off_t len;
1950 8a35f56c 2022-07-16 thomas *description = NULL;
1951 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1952 8a35f56c 2022-07-16 thomas return NULL;
1954 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1955 4606e6d4 2022-11-23 thomas if (fd == -1) {
1956 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1957 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1958 4606e6d4 2022-11-23 thomas dirpath, "description");
1960 8a35f56c 2022-07-16 thomas goto done;
1963 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1964 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1965 4606e6d4 2022-11-23 thomas dirpath, "description");
1966 8a35f56c 2022-07-16 thomas goto done;
1969 4606e6d4 2022-11-23 thomas len = sb.st_size;
1970 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1971 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1973 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1974 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1975 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1976 8a35f56c 2022-07-16 thomas goto done;
1979 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1980 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1982 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1983 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1984 8a35f56c 2022-07-16 thomas return error;
1987 8a35f56c 2022-07-16 thomas static const struct got_error *
1988 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1989 4606e6d4 2022-11-23 thomas int dir)
1991 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1992 4606e6d4 2022-11-23 thomas struct stat sb;
1993 4606e6d4 2022-11-23 thomas int fd = -1;
1994 4606e6d4 2022-11-23 thomas off_t len;
1996 8a35f56c 2022-07-16 thomas *url = NULL;
1997 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1998 8a35f56c 2022-07-16 thomas return NULL;
2000 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
2001 4606e6d4 2022-11-23 thomas if (fd == -1) {
2002 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
2003 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
2004 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
2006 8a35f56c 2022-07-16 thomas goto done;
2009 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
2010 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
2011 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
2012 8a35f56c 2022-07-16 thomas goto done;
2015 4606e6d4 2022-11-23 thomas len = sb.st_size;
2016 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2017 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
2019 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
2020 8a35f56c 2022-07-16 thomas if (*url == NULL) {
2021 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2022 8a35f56c 2022-07-16 thomas goto done;
2025 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
2026 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
2028 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
2029 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
2030 8a35f56c 2022-07-16 thomas return error;
2033 8a35f56c 2022-07-16 thomas const struct got_error *
2034 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2036 8a35f56c 2022-07-16 thomas struct tm tm;
2037 399ea8e4 2022-07-20 thomas long long diff_time;
2038 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
2039 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
2040 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
2041 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
2042 8a35f56c 2022-07-16 thomas char *s;
2043 d6795e9f 2022-12-30 thomas char datebuf[64];
2044 d6795e9f 2022-12-30 thomas size_t r;
2046 8a35f56c 2022-07-16 thomas *repo_age = NULL;
2048 8a35f56c 2022-07-16 thomas switch (ref_tm) {
2049 8a35f56c 2022-07-16 thomas case TM_DIFF:
2050 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
2051 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
2052 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2053 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
2054 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2055 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2056 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2057 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
2058 8a35f56c 2022-07-16 thomas months) == -1)
2059 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2060 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2061 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2062 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2063 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2064 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2065 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2066 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2067 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2068 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2069 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2070 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2071 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2072 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2073 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2074 8a35f56c 2022-07-16 thomas minutes) == -1)
2075 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2076 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2077 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2078 8a35f56c 2022-07-16 thomas seconds) == -1)
2079 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2080 8a35f56c 2022-07-16 thomas } else {
2081 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2082 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2085 8a35f56c 2022-07-16 thomas case TM_LONG:
2086 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2087 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2089 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2090 8a35f56c 2022-07-16 thomas if (s == NULL)
2091 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2093 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2094 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2096 d6795e9f 2022-12-30 thomas case TM_RFC822:
2097 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2098 d6795e9f 2022-12-30 thomas return got_error_from_errno("gmtime_r");
2100 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
2101 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
2102 d6795e9f 2022-12-30 thomas if (r == 0)
2103 d6795e9f 2022-12-30 thomas return got_error(GOT_ERR_NO_SPACE);
2105 d6795e9f 2022-12-30 thomas *repo_age = strdup(datebuf);
2106 d6795e9f 2022-12-30 thomas if (*repo_age == NULL)
2107 d6795e9f 2022-12-30 thomas return got_error_from_errno("asprintf");
2110 8a35f56c 2022-07-16 thomas return NULL;