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"
53 1abb18e1 2022-12-20 op #include "tmpl.h"
55 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
56 a596b957 2022-07-14 tracey { "action", ACTION },
57 a596b957 2022-07-14 tracey { "commit", COMMIT },
58 a596b957 2022-07-14 tracey { "file", RFILE },
59 a596b957 2022-07-14 tracey { "folder", FOLDER },
60 a596b957 2022-07-14 tracey { "headref", HEADREF },
61 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
62 a596b957 2022-07-14 tracey { "path", PATH },
63 a596b957 2022-07-14 tracey { "page", PAGE },
66 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
67 a596b957 2022-07-14 tracey { "blame", BLAME },
68 a596b957 2022-07-14 tracey { "blob", BLOB },
69 298f95fb 2023-01-05 op { "blobraw", BLOBRAW },
70 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
71 a596b957 2022-07-14 tracey { "commits", COMMITS },
72 a596b957 2022-07-14 tracey { "diff", DIFF },
73 a596b957 2022-07-14 tracey { "error", ERR },
74 a596b957 2022-07-14 tracey { "index", INDEX },
75 a596b957 2022-07-14 tracey { "summary", SUMMARY },
76 a596b957 2022-07-14 tracey { "tag", TAG },
77 a596b957 2022-07-14 tracey { "tags", TAGS },
78 a596b957 2022-07-14 tracey { "tree", TREE },
79 1abb18e1 2022-12-20 op { "rss", RSS },
82 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
83 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 a596b957 2022-07-14 tracey static const struct got_error *gotweb_assign_querystring(struct querystring **,
86 a596b957 2022-07-14 tracey char *, char *);
87 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_index(struct request *);
88 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
89 a596b957 2022-07-14 tracey const char *);
90 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
91 a596b957 2022-07-14 tracey struct repo_dir *);
92 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
93 3b81530f 2022-11-22 op struct server *, const char *, int);
94 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
95 3b81530f 2022-11-22 op const char *, int);
96 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_blame(struct request *);
97 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_summary(struct request *);
98 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tag(struct request *);
99 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tags(struct request *);
100 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_branches(struct request *);
102 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
103 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
105 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
110 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
111 298f95fb 2023-01-05 op struct got_blob_object *blob = NULL;
112 a596b957 2022-07-14 tracey struct server *srv = NULL;
113 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
114 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
115 169b1631 2023-01-06 op FILE *fp = NULL;
116 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
117 298f95fb 2023-01-05 op int r, html = 0, fd = -1;
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 298f95fb 2023-01-05 op if (qs->action == BLAME || qs->action == BLOB ||
155 298f95fb 2023-01-05 op qs->action == BLOBRAW || qs->action == DIFF) {
156 298f95fb 2023-01-05 op if (qs->commit == NULL) {
157 298f95fb 2023-01-05 op error2 = got_error(GOT_ERR_QUERYSTRING);
162 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
163 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
164 a596b957 2022-07-14 tracey if (error)
165 a596b957 2022-07-14 tracey goto done;
166 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
167 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
168 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
169 a596b957 2022-07-14 tracey goto err;
172 298f95fb 2023-01-05 op if (qs->action == BLOBRAW) {
173 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
174 a596b957 2022-07-14 tracey if (error)
175 a596b957 2022-07-14 tracey goto done;
176 a596b957 2022-07-14 tracey error = got_output_file_blob(c);
178 1abb18e1 2022-12-20 op log_warnx("%s: %s", __func__, error->msg);
184 298f95fb 2023-01-05 op if (qs->action == BLOB) {
186 298f95fb 2023-01-05 op struct gotweb_url url = {
187 298f95fb 2023-01-05 op .index_page = -1,
189 298f95fb 2023-01-05 op .action = BLOBRAW,
190 298f95fb 2023-01-05 op .path = qs->path,
191 298f95fb 2023-01-05 op .commit = qs->commit,
192 298f95fb 2023-01-05 op .folder = qs->folder,
193 298f95fb 2023-01-05 op .file = qs->file,
196 298f95fb 2023-01-05 op error = got_get_repo_commits(c, 1);
200 298f95fb 2023-01-05 op error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
203 298f95fb 2023-01-05 op if (binary) {
204 298f95fb 2023-01-05 op fcgi_puts(c->tp, "Status: 302\r\n");
205 298f95fb 2023-01-05 op fcgi_puts(c->tp, "Location: ");
206 298f95fb 2023-01-05 op gotweb_render_url(c, &url);
207 298f95fb 2023-01-05 op fcgi_puts(c->tp, "\r\n\r\n");
212 1abb18e1 2022-12-20 op if (qs->action == RSS) {
213 92c8ec64 2023-01-03 op error = gotweb_render_content_type_file(c,
214 92c8ec64 2023-01-03 op "application/rss+xml;charset=utf-8",
215 92c8ec64 2023-01-03 op repo_dir->name, ".rss");
217 1abb18e1 2022-12-20 op log_warnx("%s: %s", __func__, error->msg);
221 1abb18e1 2022-12-20 op error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
222 a596b957 2022-07-14 tracey if (error) {
223 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
224 a596b957 2022-07-14 tracey goto err;
226 1abb18e1 2022-12-20 op if (gotweb_render_rss(c->tp) == -1)
228 a596b957 2022-07-14 tracey goto done;
232 6970304f 2022-12-04 op error = gotweb_render_content_type(c, "text/html");
234 6970304f 2022-12-04 op log_warnx("%s: %s", __func__, error->msg);
239 ed619ca0 2022-12-14 op if (gotweb_render_header(c->tp) == -1)
240 a596b957 2022-07-14 tracey goto err;
242 a596b957 2022-07-14 tracey if (error2) {
243 a596b957 2022-07-14 tracey error = error2;
244 a596b957 2022-07-14 tracey goto err;
247 a596b957 2022-07-14 tracey switch(qs->action) {
248 a596b957 2022-07-14 tracey case BLAME:
249 a596b957 2022-07-14 tracey error = gotweb_render_blame(c);
250 a596b957 2022-07-14 tracey if (error) {
251 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
252 a596b957 2022-07-14 tracey goto err;
256 298f95fb 2023-01-05 op if (gotweb_render_blob(c->tp, blob) == -1)
259 a596b957 2022-07-14 tracey case BRIEFS:
260 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
261 a596b957 2022-07-14 tracey goto err;
263 a596b957 2022-07-14 tracey case COMMITS:
264 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
265 a596b957 2022-07-14 tracey if (error) {
266 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
267 a596b957 2022-07-14 tracey goto err;
269 156a1144 2022-12-17 op if (gotweb_render_commits(c->tp) == -1)
272 a596b957 2022-07-14 tracey case DIFF:
273 169b1631 2023-01-06 op error = got_get_repo_commits(c, 1);
274 a596b957 2022-07-14 tracey if (error) {
275 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
276 a596b957 2022-07-14 tracey goto err;
278 169b1631 2023-01-06 op error = got_open_diff_for_output(&fp, &fd, c);
280 169b1631 2023-01-06 op log_warnx("%s: %s", __func__, error->msg);
283 169b1631 2023-01-06 op if (gotweb_render_diff(c->tp, fp) == -1)
286 a596b957 2022-07-14 tracey case INDEX:
287 a596b957 2022-07-14 tracey error = gotweb_render_index(c);
288 a596b957 2022-07-14 tracey if (error) {
289 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
290 a596b957 2022-07-14 tracey goto err;
293 a596b957 2022-07-14 tracey case SUMMARY:
294 a596b957 2022-07-14 tracey error = gotweb_render_summary(c);
295 a596b957 2022-07-14 tracey if (error) {
296 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
297 a596b957 2022-07-14 tracey goto err;
300 a596b957 2022-07-14 tracey case TAG:
301 a596b957 2022-07-14 tracey error = gotweb_render_tag(c);
302 a596b957 2022-07-14 tracey if (error) {
303 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
304 a596b957 2022-07-14 tracey goto err;
307 a596b957 2022-07-14 tracey case TAGS:
308 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
309 a596b957 2022-07-14 tracey if (error) {
310 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
311 a596b957 2022-07-14 tracey goto err;
314 a596b957 2022-07-14 tracey case TREE:
315 43d421de 2023-01-05 op error = got_get_repo_commits(c, 1);
316 a596b957 2022-07-14 tracey if (error) {
317 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
318 a596b957 2022-07-14 tracey goto err;
320 43d421de 2023-01-05 op if (gotweb_render_tree(c->tp) == -1)
323 a596b957 2022-07-14 tracey case ERR:
325 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
326 01498c42 2022-08-19 op "Erorr: Bad Querystring");
328 a596b957 2022-07-14 tracey goto err;
332 a596b957 2022-07-14 tracey goto done;
334 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
336 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
338 a596b957 2022-07-14 tracey if (error) {
339 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
342 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
345 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
349 298f95fb 2023-01-05 op got_object_blob_close(blob);
351 169b1631 2023-01-06 op error = got_gotweb_flushfile(fp, fd);
353 169b1631 2023-01-06 op log_warnx("%s: got_gotweb_flushfile failure: %s",
354 169b1631 2023-01-06 op __func__, error->msg);
357 298f95fb 2023-01-05 op if (fd != -1)
359 a596b957 2022-07-14 tracey if (html && srv != NULL)
360 ed619ca0 2022-12-14 op gotweb_render_footer(c->tp);
363 a596b957 2022-07-14 tracey struct server *
364 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
366 a596b957 2022-07-14 tracey struct server *srv = NULL;
368 95a4a5a1 2022-08-30 op /* check against the server name first */
369 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
370 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
371 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
372 a596b957 2022-07-14 tracey goto done;
374 95a4a5a1 2022-08-30 op /* check against subdomain second */
375 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
376 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
377 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
378 a596b957 2022-07-14 tracey goto done;
380 a596b957 2022-07-14 tracey /* if those fail, send first server */
381 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
382 a596b957 2022-07-14 tracey if (srv != NULL)
385 a596b957 2022-07-14 tracey return srv;
388 a596b957 2022-07-14 tracey const struct got_error *
389 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
391 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
393 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
394 a596b957 2022-07-14 tracey if (*t == NULL)
395 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
397 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
398 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
400 a596b957 2022-07-14 tracey (*t)->repo = NULL;
401 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
402 a596b957 2022-07-14 tracey (*t)->qs = NULL;
403 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
404 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
405 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
406 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
408 a596b957 2022-07-14 tracey return error;
411 a596b957 2022-07-14 tracey static const struct got_error *
412 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
414 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
416 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
417 a596b957 2022-07-14 tracey if (*qs == NULL)
418 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
420 a596b957 2022-07-14 tracey (*qs)->headref = strdup("HEAD");
421 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
424 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
427 6c37ad7b 2022-09-01 op (*qs)->action = INDEX;
428 6c37ad7b 2022-09-01 op (*qs)->commit = NULL;
429 6c37ad7b 2022-09-01 op (*qs)->file = NULL;
430 6c37ad7b 2022-09-01 op (*qs)->folder = NULL;
431 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
432 a596b957 2022-07-14 tracey (*qs)->path = NULL;
434 a596b957 2022-07-14 tracey return error;
437 a596b957 2022-07-14 tracey static const struct got_error *
438 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
440 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
441 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
442 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
444 a596b957 2022-07-14 tracey if (qst == NULL)
445 a596b957 2022-07-14 tracey return error;
447 a596b957 2022-07-14 tracey tok1 = strdup(qst);
448 a596b957 2022-07-14 tracey if (tok1 == NULL)
449 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
451 a596b957 2022-07-14 tracey tok1_pair = tok1;
452 a596b957 2022-07-14 tracey tok1_end = tok1;
454 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
455 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
457 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
458 a596b957 2022-07-14 tracey if (tok2 == NULL) {
459 a596b957 2022-07-14 tracey free(tok1);
460 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
463 a596b957 2022-07-14 tracey tok2_pair = tok2;
464 a596b957 2022-07-14 tracey tok2_end = tok2;
466 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
467 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
468 a596b957 2022-07-14 tracey if (tok2_end) {
469 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
470 a596b957 2022-07-14 tracey tok2_end);
471 a596b957 2022-07-14 tracey if (error)
472 a596b957 2022-07-14 tracey goto err;
474 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
476 a596b957 2022-07-14 tracey free(tok2);
477 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
479 a596b957 2022-07-14 tracey free(tok1);
480 a596b957 2022-07-14 tracey return error;
482 a596b957 2022-07-14 tracey free(tok2);
483 a596b957 2022-07-14 tracey free(tok1);
484 a596b957 2022-07-14 tracey return error;
488 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
490 a596b957 2022-07-14 tracey static const struct got_error *
491 58381f70 2022-09-03 op gotweb_urldecode(char *url)
495 58381f70 2022-09-03 op unsigned long x;
497 58381f70 2022-09-03 op hex[2] = '\0';
500 58381f70 2022-09-03 op while (*p != '\0') {
501 58381f70 2022-09-03 op switch (*p) {
503 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
504 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
505 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
506 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
507 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
509 58381f70 2022-09-03 op hex[0] = p[1];
510 58381f70 2022-09-03 op hex[1] = p[2];
513 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
514 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
516 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
517 58381f70 2022-09-03 op *q = (char)x;
532 58381f70 2022-09-03 op static const struct got_error *
533 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
535 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
536 a596b957 2022-07-14 tracey const char *errstr;
537 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
539 58381f70 2022-09-03 op error = gotweb_urldecode(value);
541 58381f70 2022-09-03 op return error;
543 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
544 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
545 a596b957 2022-07-14 tracey continue;
547 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
548 a596b957 2022-07-14 tracey case ACTION:
549 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
550 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
551 a596b957 2022-07-14 tracey continue;
552 a596b957 2022-07-14 tracey else if (strcmp(value,
553 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
554 a596b957 2022-07-14 tracey (*qs)->action =
555 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
556 a596b957 2022-07-14 tracey goto qa_found;
559 a596b957 2022-07-14 tracey (*qs)->action = ERR;
560 a596b957 2022-07-14 tracey qa_found:
562 a596b957 2022-07-14 tracey case COMMIT:
563 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
564 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
565 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
566 a596b957 2022-07-14 tracey __func__);
567 a596b957 2022-07-14 tracey goto done;
570 a596b957 2022-07-14 tracey case RFILE:
571 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
572 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
573 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
574 a596b957 2022-07-14 tracey __func__);
575 a596b957 2022-07-14 tracey goto done;
578 a596b957 2022-07-14 tracey case FOLDER:
579 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
580 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
581 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
582 a596b957 2022-07-14 tracey __func__);
583 a596b957 2022-07-14 tracey goto done;
586 a596b957 2022-07-14 tracey case HEADREF:
587 f8faf9f1 2022-09-01 op free((*qs)->headref);
588 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
589 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
590 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
591 a596b957 2022-07-14 tracey __func__);
592 a596b957 2022-07-14 tracey goto done;
595 a596b957 2022-07-14 tracey case INDEX_PAGE:
596 a596b957 2022-07-14 tracey if (strlen(value) == 0)
598 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
599 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
600 a596b957 2022-07-14 tracey if (errstr) {
601 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
602 a596b957 2022-07-14 tracey __func__, errstr);
603 a596b957 2022-07-14 tracey goto done;
605 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
606 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
608 a596b957 2022-07-14 tracey case PATH:
609 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
610 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
611 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
612 a596b957 2022-07-14 tracey __func__);
613 a596b957 2022-07-14 tracey goto done;
616 a596b957 2022-07-14 tracey case PAGE:
617 a596b957 2022-07-14 tracey if (strlen(value) == 0)
619 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
620 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
621 a596b957 2022-07-14 tracey if (errstr) {
622 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
623 a596b957 2022-07-14 tracey __func__, errstr);
624 a596b957 2022-07-14 tracey goto done;
626 03f6a843 2022-12-17 op if ((*qs)->page < 0)
627 a596b957 2022-07-14 tracey (*qs)->page = 0;
634 a596b957 2022-07-14 tracey return error;
638 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
640 a596b957 2022-07-14 tracey if (rt != NULL) {
641 a596b957 2022-07-14 tracey free(rt->commit_id);
642 625e5896 2022-09-01 op free(rt->tag_name);
643 625e5896 2022-09-01 op free(rt->tag_commit);
644 625e5896 2022-09-01 op free(rt->commit_msg);
645 a596b957 2022-07-14 tracey free(rt->tagger);
647 a596b957 2022-07-14 tracey free(rt);
651 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
653 a596b957 2022-07-14 tracey if (rc != NULL) {
654 a596b957 2022-07-14 tracey free(rc->path);
655 a596b957 2022-07-14 tracey free(rc->refs_str);
656 a596b957 2022-07-14 tracey free(rc->commit_id);
657 a596b957 2022-07-14 tracey free(rc->parent_id);
658 a596b957 2022-07-14 tracey free(rc->tree_id);
659 a596b957 2022-07-14 tracey free(rc->author);
660 a596b957 2022-07-14 tracey free(rc->committer);
661 a596b957 2022-07-14 tracey free(rc->commit_msg);
663 a596b957 2022-07-14 tracey free(rc);
666 a596b957 2022-07-14 tracey static void
667 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
669 a596b957 2022-07-14 tracey if (qs != NULL) {
670 a596b957 2022-07-14 tracey free(qs->commit);
671 a596b957 2022-07-14 tracey free(qs->file);
672 a596b957 2022-07-14 tracey free(qs->folder);
673 a596b957 2022-07-14 tracey free(qs->headref);
674 a596b957 2022-07-14 tracey free(qs->path);
676 a596b957 2022-07-14 tracey free(qs);
679 a596b957 2022-07-14 tracey static void
680 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
682 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
683 a596b957 2022-07-14 tracey free(repo_dir->name);
684 a596b957 2022-07-14 tracey free(repo_dir->owner);
685 a596b957 2022-07-14 tracey free(repo_dir->description);
686 a596b957 2022-07-14 tracey free(repo_dir->url);
687 a596b957 2022-07-14 tracey free(repo_dir->age);
688 a596b957 2022-07-14 tracey free(repo_dir->path);
690 a596b957 2022-07-14 tracey free(repo_dir);
694 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
696 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
697 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
699 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
700 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
701 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
703 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
704 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
705 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
707 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
708 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
709 341fa7ca 2022-09-01 op free(t->next_id);
710 341fa7ca 2022-09-01 op free(t->prev_id);
714 a596b957 2022-07-14 tracey const struct got_error *
715 345b67f2 2023-01-03 op gotweb_render_content_type(struct request *c, const char *type)
717 4d648b92 2022-08-20 op const char *csp = "default-src 'self'; script-src 'none'; "
718 4d648b92 2022-08-20 op "object-src 'none';";
720 4d648b92 2022-08-20 op fcgi_printf(c,
721 4d648b92 2022-08-20 op "Content-Security-Policy: %s\r\n"
722 4d648b92 2022-08-20 op "Content-Type: %s\r\n\r\n",
727 a596b957 2022-07-14 tracey const struct got_error *
728 1b18f4cd 2023-01-03 op gotweb_render_content_type_file(struct request *c, const char *type,
729 92c8ec64 2023-01-03 op const char *file, const char *suffix)
731 01498c42 2022-08-19 op fcgi_printf(c, "Content-type: %s\r\n"
732 92c8ec64 2023-01-03 op "Content-disposition: attachment; filename=%s%s\r\n\r\n",
733 92c8ec64 2023-01-03 op type, file, suffix ? suffix : "");
738 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
739 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
741 a596b957 2022-07-14 tracey struct transport *t = c->t;
742 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
743 a596b957 2022-07-14 tracey struct server *srv = c->srv;
745 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
747 a596b957 2022-07-14 tracey switch(qs->action) {
748 a596b957 2022-07-14 tracey case INDEX:
749 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
750 b4c0bd72 2022-12-17 op *have_prev = 1;
751 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
752 8d02314f 2022-09-07 op .action = -1,
753 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
757 b4c0bd72 2022-12-17 op if (t->next_disp == srv->max_repos_display &&
758 b4c0bd72 2022-12-17 op t->repos_total != (qs->index_page + 1) *
759 b4c0bd72 2022-12-17 op srv->max_repos_display) {
760 b4c0bd72 2022-12-17 op *have_next = 1;
761 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
762 b4c0bd72 2022-12-17 op .action = -1,
763 b4c0bd72 2022-12-17 op .index_page = qs->index_page + 1,
768 a596b957 2022-07-14 tracey case BRIEFS:
769 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
770 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
771 b4c0bd72 2022-12-17 op *have_prev = 1;
772 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
773 8d02314f 2022-09-07 op .action = BRIEFS,
774 8d02314f 2022-09-07 op .index_page = -1,
775 8d02314f 2022-09-07 op .page = qs->page - 1,
776 8d02314f 2022-09-07 op .path = qs->path,
777 8d02314f 2022-09-07 op .commit = t->prev_id,
778 8d02314f 2022-09-07 op .headref = qs->headref,
781 b4c0bd72 2022-12-17 op if (t->next_id) {
782 b4c0bd72 2022-12-17 op *have_next = 1;
783 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
784 b4c0bd72 2022-12-17 op .action = BRIEFS,
785 b4c0bd72 2022-12-17 op .index_page = -1,
786 b4c0bd72 2022-12-17 op .page = qs->page + 1,
787 b4c0bd72 2022-12-17 op .path = qs->path,
788 b4c0bd72 2022-12-17 op .commit = t->next_id,
789 b4c0bd72 2022-12-17 op .headref = qs->headref,
793 a596b957 2022-07-14 tracey case COMMITS:
794 a596b957 2022-07-14 tracey if (t->prev_id && qs->commit != NULL &&
795 a596b957 2022-07-14 tracey strcmp(qs->commit, t->prev_id) != 0) {
796 b4c0bd72 2022-12-17 op *have_prev = 1;
797 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
798 6169d054 2022-12-17 op .action = COMMITS,
799 8d02314f 2022-09-07 op .index_page = -1,
800 8d02314f 2022-09-07 op .page = qs->page - 1,
801 8d02314f 2022-09-07 op .path = qs->path,
802 8d02314f 2022-09-07 op .commit = t->prev_id,
803 8d02314f 2022-09-07 op .headref = qs->headref,
804 8d02314f 2022-09-07 op .folder = qs->folder,
805 8d02314f 2022-09-07 op .file = qs->file,
808 b4c0bd72 2022-12-17 op if (t->next_id) {
809 b4c0bd72 2022-12-17 op *have_next = 1;
810 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
811 6169d054 2022-12-17 op .action = COMMITS,
812 8d02314f 2022-09-07 op .index_page = -1,
813 8d02314f 2022-09-07 op .page = qs->page + 1,
814 8d02314f 2022-09-07 op .path = qs->path,
815 8d02314f 2022-09-07 op .commit = t->next_id,
816 8d02314f 2022-09-07 op .headref = qs->headref,
817 8d02314f 2022-09-07 op .folder = qs->folder,
818 8d02314f 2022-09-07 op .file = qs->file,
822 a596b957 2022-07-14 tracey case TAGS:
823 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
824 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
825 b4c0bd72 2022-12-17 op *have_prev = 1;
826 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
827 b4c0bd72 2022-12-17 op .action = TAGS,
828 b4c0bd72 2022-12-17 op .index_page = -1,
829 b4c0bd72 2022-12-17 op .page = qs->page - 1,
830 b4c0bd72 2022-12-17 op .path = qs->path,
831 b4c0bd72 2022-12-17 op .commit = t->prev_id,
832 b4c0bd72 2022-12-17 op .headref = qs->headref,
835 a596b957 2022-07-14 tracey if (t->next_id) {
836 b4c0bd72 2022-12-17 op *have_next = 1;
837 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
838 8d02314f 2022-09-07 op .action = TAGS,
839 8d02314f 2022-09-07 op .index_page = -1,
840 8d02314f 2022-09-07 op .page = qs->page + 1,
841 8d02314f 2022-09-07 op .path = qs->path,
842 8d02314f 2022-09-07 op .commit = t->next_id,
843 8d02314f 2022-09-07 op .headref = qs->headref,
850 a596b957 2022-07-14 tracey static const struct got_error *
851 a596b957 2022-07-14 tracey gotweb_render_index(struct request *c)
853 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
854 a596b957 2022-07-14 tracey struct server *srv = c->srv;
855 a596b957 2022-07-14 tracey struct transport *t = c->t;
856 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
857 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
859 2db401bd 2022-09-01 op struct dirent **sd_dent = NULL;
860 a596b957 2022-07-14 tracey unsigned int d_cnt, d_i, d_disp = 0;
861 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
864 a596b957 2022-07-14 tracey d = opendir(srv->repos_path);
865 a596b957 2022-07-14 tracey if (d == NULL) {
866 a596b957 2022-07-14 tracey error = got_error_from_errno2("opendir", srv->repos_path);
867 a596b957 2022-07-14 tracey return error;
870 a596b957 2022-07-14 tracey d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
871 a596b957 2022-07-14 tracey if (d_cnt == -1) {
872 2db401bd 2022-09-01 op sd_dent = NULL;
873 a596b957 2022-07-14 tracey error = got_error_from_errno2("scandir", srv->repos_path);
874 a596b957 2022-07-14 tracey goto done;
877 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
878 a596b957 2022-07-14 tracey goto done;
880 a596b957 2022-07-14 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
881 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
884 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
885 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
890 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
891 525dfdf4 2022-11-22 op sd_dent[d_i]);
894 525dfdf4 2022-11-22 op if (type != DT_DIR) {
896 a596b957 2022-07-14 tracey continue;
899 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
900 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
901 a596b957 2022-07-14 tracey t->prev_disp++;
902 a596b957 2022-07-14 tracey continue;
905 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
906 a596b957 2022-07-14 tracey if (error)
907 a596b957 2022-07-14 tracey goto done;
909 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
910 a596b957 2022-07-14 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
911 a596b957 2022-07-14 tracey error = NULL;
912 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
913 a596b957 2022-07-14 tracey repo_dir = NULL;
915 a596b957 2022-07-14 tracey continue;
917 525dfdf4 2022-11-22 op if (error && error->code != GOT_ERR_LONELY_PACKIDX)
920 a596b957 2022-07-14 tracey d_disp++;
921 a596b957 2022-07-14 tracey t->prev_disp++;
923 ed619ca0 2022-12-14 op if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
926 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
927 a596b957 2022-07-14 tracey repo_dir = NULL;
928 a596b957 2022-07-14 tracey t->next_disp++;
929 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
932 525dfdf4 2022-11-22 op t->repos_total = d_cnt - d_skipped;
934 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
936 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
938 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
939 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
942 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
943 a596b957 2022-07-14 tracey goto done;
945 2db401bd 2022-09-01 op if (sd_dent) {
946 2db401bd 2022-09-01 op for (d_i = 0; d_i < d_cnt; d_i++)
947 2db401bd 2022-09-01 op free(sd_dent[d_i]);
948 2db401bd 2022-09-01 op free(sd_dent);
950 a596b957 2022-07-14 tracey if (d != NULL && closedir(d) == EOF && error == NULL)
951 a596b957 2022-07-14 tracey error = got_error_from_errno("closedir");
952 a596b957 2022-07-14 tracey return error;
955 a596b957 2022-07-14 tracey static const struct got_error *
956 a596b957 2022-07-14 tracey gotweb_render_blame(struct request *c)
958 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
959 a596b957 2022-07-14 tracey struct transport *t = c->t;
960 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
961 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
964 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
965 a596b957 2022-07-14 tracey if (error)
966 a596b957 2022-07-14 tracey return error;
968 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
970 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
973 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
974 a596b957 2022-07-14 tracey if (error)
975 a596b957 2022-07-14 tracey goto done;
977 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
978 01498c42 2022-08-19 op "<div id='blame_title'>Blame</div>\n"
979 01498c42 2022-08-19 op "</div>\n" /* #blame_title_wrapper */
980 01498c42 2022-08-19 op "<div id='blame_content'>\n"
981 01498c42 2022-08-19 op "<div id='blame_header_wrapper'>\n"
982 01498c42 2022-08-19 op "<div id='blame_header'>\n"
983 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
984 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
985 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
986 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
987 01498c42 2022-08-19 op "</div>\n" /* #blame_header */
988 01498c42 2022-08-19 op "</div>\n" /* #blame_header_wrapper */
989 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
990 01498c42 2022-08-19 op "<div id='blame'>\n",
994 a596b957 2022-07-14 tracey goto done;
996 a596b957 2022-07-14 tracey error = got_output_file_blame(c);
997 a596b957 2022-07-14 tracey if (error)
998 a596b957 2022-07-14 tracey goto done;
1000 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n" /* #blame */
1001 01498c42 2022-08-19 op "</div>\n"); /* #blame_content */
1005 a596b957 2022-07-14 tracey return error;
1008 a596b957 2022-07-14 tracey static const struct got_error *
1009 a596b957 2022-07-14 tracey gotweb_render_branches(struct request *c)
1011 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1012 a596b957 2022-07-14 tracey struct got_reflist_head refs;
1013 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
1014 a596b957 2022-07-14 tracey struct transport *t = c->t;
1015 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1016 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
1017 8d02314f 2022-09-07 op char *escaped_refname = NULL;
1018 a596b957 2022-07-14 tracey char *age = NULL;
1021 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
1023 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
1024 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
1025 a596b957 2022-07-14 tracey if (error)
1026 a596b957 2022-07-14 tracey goto done;
1028 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1029 01498c42 2022-08-19 op "<div id='branches_title'>Branches</div>\n"
1030 01498c42 2022-08-19 op "</div>\n" /* #branches_title_wrapper */
1031 01498c42 2022-08-19 op "<div id='branches_content'>\n");
1032 01498c42 2022-08-19 op if (r == -1)
1033 a596b957 2022-07-14 tracey goto done;
1035 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
1036 d927f8c8 2022-08-20 op const char *refname = NULL;
1038 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
1039 a596b957 2022-07-14 tracey continue;
1041 d927f8c8 2022-08-20 op refname = got_ref_get_name(re->ref);
1042 a596b957 2022-07-14 tracey if (refname == NULL) {
1043 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
1044 a596b957 2022-07-14 tracey goto done;
1046 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) != 0)
1047 a596b957 2022-07-14 tracey continue;
1049 c127fc49 2022-11-22 op error = got_get_repo_age(&age, c, refname, TM_DIFF);
1050 a596b957 2022-07-14 tracey if (error)
1051 a596b957 2022-07-14 tracey goto done;
1053 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
1054 a596b957 2022-07-14 tracey refname += 11;
1055 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_refname, refname);
1059 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1060 01498c42 2022-08-19 op "<div class='branches_age'>%s</div>\n"
1061 01498c42 2022-08-19 op "<div class='branches_space'> </div>\n"
1062 8d02314f 2022-09-07 op "<div class='branch'>", age);
1063 8d02314f 2022-09-07 op if (r == -1)
1066 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1067 8d02314f 2022-09-07 op .action = SUMMARY,
1068 8d02314f 2022-09-07 op .index_page = -1,
1070 8d02314f 2022-09-07 op .path = qs->path,
1071 8d02314f 2022-09-07 op .headref = refname,
1072 8d02314f 2022-09-07 op }, "%s", escaped_refname);
1073 8d02314f 2022-09-07 op if (r == -1)
1076 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .branch */
1077 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1078 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1081 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1082 8d02314f 2022-09-07 op .action = SUMMARY,
1083 8d02314f 2022-09-07 op .index_page = -1,
1085 8d02314f 2022-09-07 op .path = qs->path,
1086 8d02314f 2022-09-07 op .headref = refname,
1087 8d02314f 2022-09-07 op }, "summary");
1088 8d02314f 2022-09-07 op if (r == -1)
1091 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1094 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1095 8d02314f 2022-09-07 op .action = BRIEFS,
1096 8d02314f 2022-09-07 op .index_page = -1,
1098 8d02314f 2022-09-07 op .path = qs->path,
1099 8d02314f 2022-09-07 op .headref = refname,
1100 8d02314f 2022-09-07 op }, "commit briefs");
1101 8d02314f 2022-09-07 op if (r == -1)
1104 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1107 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1108 8d02314f 2022-09-07 op .action = COMMITS,
1109 8d02314f 2022-09-07 op .index_page = -1,
1111 8d02314f 2022-09-07 op .path = qs->path,
1112 8d02314f 2022-09-07 op .headref = refname,
1113 8d02314f 2022-09-07 op }, "commits");
1114 8d02314f 2022-09-07 op if (r == -1)
1117 8d02314f 2022-09-07 op r = fcgi_printf(c, "</div>\n" /* .navs */
1118 8d02314f 2022-09-07 op "</div>\n" /* .navs_wrapper */
1119 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1120 8d02314f 2022-09-07 op "</div>\n"); /* .branches_wrapper */
1121 01498c42 2022-08-19 op if (r == -1)
1122 a596b957 2022-07-14 tracey goto done;
1124 a596b957 2022-07-14 tracey free(age);
1125 a596b957 2022-07-14 tracey age = NULL;
1126 8d02314f 2022-09-07 op free(escaped_refname);
1127 8d02314f 2022-09-07 op escaped_refname = NULL;
1129 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #branches_content */
1132 8d02314f 2022-09-07 op free(escaped_refname);
1133 f49cdcf5 2022-09-02 op got_ref_list_free(&refs);
1134 a596b957 2022-07-14 tracey return error;
1137 a596b957 2022-07-14 tracey static const struct got_error *
1138 a596b957 2022-07-14 tracey gotweb_render_summary(struct request *c)
1140 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1141 a596b957 2022-07-14 tracey struct transport *t = c->t;
1142 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1145 01498c42 2022-08-19 op if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1146 a596b957 2022-07-14 tracey goto done;
1148 01498c42 2022-08-19 op if (srv->show_repo_description) {
1149 01498c42 2022-08-19 op r = fcgi_printf(c,
1150 01498c42 2022-08-19 op "<div id='description_title'>Description:</div>\n"
1151 01498c42 2022-08-19 op "<div id='description'>%s</div>\n",
1152 f897bb24 2022-08-20 op t->repo_dir->description ? t->repo_dir->description : "");
1153 01498c42 2022-08-19 op if (r == -1)
1157 01498c42 2022-08-19 op if (srv->show_repo_owner) {
1158 01498c42 2022-08-19 op r = fcgi_printf(c,
1159 01498c42 2022-08-19 op "<div id='repo_owner_title'>Owner:</div>\n"
1160 01498c42 2022-08-19 op "<div id='repo_owner'>%s</div>\n",
1161 f897bb24 2022-08-20 op t->repo_dir->owner ? t->repo_dir->owner : "");
1162 01498c42 2022-08-19 op if (r == -1)
1166 01498c42 2022-08-19 op if (srv->show_repo_age) {
1167 01498c42 2022-08-19 op r = fcgi_printf(c,
1168 01498c42 2022-08-19 op "<div id='last_change_title'>Last Change:</div>\n"
1169 01498c42 2022-08-19 op "<div id='last_change'>%s</div>\n",
1170 01498c42 2022-08-19 op t->repo_dir->age);
1171 01498c42 2022-08-19 op if (r == -1)
1175 01498c42 2022-08-19 op if (srv->show_repo_cloneurl) {
1176 01498c42 2022-08-19 op r = fcgi_printf(c,
1177 01498c42 2022-08-19 op "<div id='cloneurl_title'>Clone URL:</div>\n"
1178 01498c42 2022-08-19 op "<div id='cloneurl'>%s</div>\n",
1179 01498c42 2022-08-19 op t->repo_dir->url ? t->repo_dir->url : "");
1180 01498c42 2022-08-19 op if (r == -1)
1184 01498c42 2022-08-19 op r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1185 01498c42 2022-08-19 op if (r == -1)
1186 a596b957 2022-07-14 tracey goto done;
1188 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
1189 a596b957 2022-07-14 tracey goto done;
1191 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
1192 a596b957 2022-07-14 tracey if (error) {
1193 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1194 a596b957 2022-07-14 tracey goto done;
1197 a596b957 2022-07-14 tracey error = gotweb_render_branches(c);
1198 a596b957 2022-07-14 tracey if (error)
1199 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1201 a596b957 2022-07-14 tracey return error;
1204 a596b957 2022-07-14 tracey static const struct got_error *
1205 a596b957 2022-07-14 tracey gotweb_render_tag(struct request *c)
1207 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1208 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1209 a596b957 2022-07-14 tracey struct transport *t = c->t;
1210 d927f8c8 2022-08-20 op char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1212 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, 1);
1213 a596b957 2022-07-14 tracey if (error)
1214 a596b957 2022-07-14 tracey goto done;
1216 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1217 a596b957 2022-07-14 tracey error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1218 a596b957 2022-07-14 tracey "bad commit id");
1219 a596b957 2022-07-14 tracey goto done;
1222 a596b957 2022-07-14 tracey rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1224 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1225 a596b957 2022-07-14 tracey if (error)
1226 a596b957 2022-07-14 tracey goto done;
1227 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rt->tagger);
1228 a596b957 2022-07-14 tracey if (error)
1229 a596b957 2022-07-14 tracey goto done;
1230 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->commit_msg);
1234 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1235 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/", 5) == 0)
1236 5fba0750 2022-09-01 stsp tagname += 5;
1237 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1241 01498c42 2022-08-19 op fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1242 01498c42 2022-08-19 op "<div id='tags_title'>Tag</div>\n"
1243 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1244 01498c42 2022-08-19 op "<div id='tags_content'>\n"
1245 01498c42 2022-08-19 op "<div id='tag_header_wrapper'>\n"
1246 01498c42 2022-08-19 op "<div id='tag_header'>\n"
1247 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1248 01498c42 2022-08-19 op "<div class='header_commit'>%s"
1249 01498c42 2022-08-19 op " <span class='refs_str'>(%s)</span></div>\n"
1250 01498c42 2022-08-19 op "<div class='header_author_title'>Tagger:</div>\n"
1251 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1252 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1253 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1254 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1255 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1256 01498c42 2022-08-19 op "</div>\n" /* #tag_header */
1257 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1258 01498c42 2022-08-19 op "<div id='tag_commit'>\n%s</div>"
1259 f864583e 2022-09-06 op "</div>" /* #tag_header_wrapper */
1260 f864583e 2022-09-06 op "</div>", /* #tags_content */
1261 01498c42 2022-08-19 op rt->commit_id,
1266 01498c42 2022-08-19 op rt->tag_commit);
1269 a596b957 2022-07-14 tracey free(age);
1270 a596b957 2022-07-14 tracey free(author);
1272 a596b957 2022-07-14 tracey return error;
1275 a596b957 2022-07-14 tracey static const struct got_error *
1276 a596b957 2022-07-14 tracey gotweb_render_tags(struct request *c)
1278 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1279 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1280 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1281 a596b957 2022-07-14 tracey struct transport *t = c->t;
1282 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1283 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1284 d927f8c8 2022-08-20 op char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1285 01498c42 2022-08-19 op int r, commit_found = 0;
1287 a596b957 2022-07-14 tracey if (qs->action == BRIEFS) {
1288 a596b957 2022-07-14 tracey qs->action = TAGS;
1289 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1291 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, srv->max_commits_display);
1292 a596b957 2022-07-14 tracey if (error)
1293 a596b957 2022-07-14 tracey goto done;
1295 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1296 01498c42 2022-08-19 op "<div id='tags_title'>Tags</div>\n"
1297 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1298 01498c42 2022-08-19 op "<div id='tags_content'>\n");
1299 01498c42 2022-08-19 op if (r == -1)
1300 a596b957 2022-07-14 tracey goto done;
1302 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1303 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1304 01498c42 2022-08-19 op "This repository contains no tags");
1305 01498c42 2022-08-19 op if (r == -1)
1306 a596b957 2022-07-14 tracey goto done;
1309 a596b957 2022-07-14 tracey TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1310 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL) {
1311 a596b957 2022-07-14 tracey if (strcmp(qs->commit, rt->commit_id) != 0)
1312 a596b957 2022-07-14 tracey continue;
1314 a596b957 2022-07-14 tracey commit_found = 1;
1316 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1317 a596b957 2022-07-14 tracey if (error)
1318 a596b957 2022-07-14 tracey goto done;
1320 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1321 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/tags/", 10) == 0)
1322 5fba0750 2022-09-01 stsp tagname += 10;
1323 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1327 a596b957 2022-07-14 tracey if (rt->tag_commit != NULL) {
1328 a596b957 2022-07-14 tracey newline = strchr(rt->tag_commit, '\n');
1329 a596b957 2022-07-14 tracey if (newline)
1330 a596b957 2022-07-14 tracey *newline = '\0';
1331 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->tag_commit);
1336 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1337 01498c42 2022-08-19 op "<div class='tag'>%s</div>\n"
1338 8d02314f 2022-09-07 op "<div class='tag_log'>", age, tagname) == -1)
1341 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1342 8d02314f 2022-09-07 op .action = TAG,
1343 8d02314f 2022-09-07 op .index_page = -1,
1345 8d02314f 2022-09-07 op .path = repo_dir->name,
1346 8d02314f 2022-09-07 op .commit = rt->commit_id,
1347 8d02314f 2022-09-07 op }, "%s", msg ? msg : "");
1348 8d02314f 2022-09-07 op if (r == -1)
1351 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .tag_log */
1352 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1353 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1356 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1357 8d02314f 2022-09-07 op .action = TAG,
1358 8d02314f 2022-09-07 op .index_page = -1,
1360 8d02314f 2022-09-07 op .path = repo_dir->name,
1361 8d02314f 2022-09-07 op .commit = rt->commit_id,
1363 8d02314f 2022-09-07 op if (r == -1)
1366 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1369 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1370 8d02314f 2022-09-07 op .action = BRIEFS,
1371 8d02314f 2022-09-07 op .index_page = -1,
1373 8d02314f 2022-09-07 op .path = repo_dir->name,
1374 8d02314f 2022-09-07 op .commit = rt->commit_id,
1375 8d02314f 2022-09-07 op }, "commit briefs");
1376 8d02314f 2022-09-07 op if (r == -1)
1379 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1382 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1383 8d02314f 2022-09-07 op .action = COMMITS,
1384 8d02314f 2022-09-07 op .index_page = -1,
1386 8d02314f 2022-09-07 op .path = repo_dir->name,
1387 8d02314f 2022-09-07 op .commit = rt->commit_id,
1388 8d02314f 2022-09-07 op }, "commits");
1389 8d02314f 2022-09-07 op if (r == -1)
1392 8d02314f 2022-09-07 op r = fcgi_printf(c,
1393 01498c42 2022-08-19 op "</div>\n" /* .navs */
1394 01498c42 2022-08-19 op "</div>\n" /* .navs_wrapper */
1395 8d02314f 2022-09-07 op "<div class='dotted_line'></div>\n");
1396 01498c42 2022-08-19 op if (r == -1)
1397 a596b957 2022-07-14 tracey goto done;
1399 a596b957 2022-07-14 tracey free(age);
1400 a596b957 2022-07-14 tracey age = NULL;
1401 d927f8c8 2022-08-20 op free(tagname);
1402 d927f8c8 2022-08-20 op tagname = NULL;
1406 a596b957 2022-07-14 tracey if (t->next_id || t->prev_id) {
1407 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
1408 a596b957 2022-07-14 tracey goto done;
1410 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tags_content */
1412 a596b957 2022-07-14 tracey free(age);
1413 d927f8c8 2022-08-20 op free(tagname);
1415 a596b957 2022-07-14 tracey return error;
1418 a596b957 2022-07-14 tracey const struct got_error *
1419 a596b957 2022-07-14 tracey gotweb_escape_html(char **escaped_html, const char *orig_html)
1421 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1422 a596b957 2022-07-14 tracey struct escape_pair {
1424 a596b957 2022-07-14 tracey const char *s;
1425 a596b957 2022-07-14 tracey } esc[] = {
1426 a596b957 2022-07-14 tracey { '>', ">" },
1427 a596b957 2022-07-14 tracey { '<', "<" },
1428 a596b957 2022-07-14 tracey { '&', "&" },
1429 a596b957 2022-07-14 tracey { '"', """ },
1430 a596b957 2022-07-14 tracey { '\'', "'" },
1431 a596b957 2022-07-14 tracey { '\n', "<br />" },
1433 a596b957 2022-07-14 tracey size_t orig_len, len;
1434 a596b957 2022-07-14 tracey int i, j, x;
1436 a596b957 2022-07-14 tracey orig_len = strlen(orig_html);
1437 a596b957 2022-07-14 tracey len = orig_len;
1438 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1439 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1440 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1441 a596b957 2022-07-14 tracey continue;
1442 a596b957 2022-07-14 tracey len += strlen(esc[j].s) - 1 /* escaped char */;
1446 a596b957 2022-07-14 tracey *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1447 a596b957 2022-07-14 tracey if (*escaped_html == NULL)
1448 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1451 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1452 a596b957 2022-07-14 tracey int escaped = 0;
1453 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1454 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1455 a596b957 2022-07-14 tracey continue;
1457 a596b957 2022-07-14 tracey if (strlcat(*escaped_html, esc[j].s, len + 1)
1458 a596b957 2022-07-14 tracey >= len + 1) {
1459 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_SPACE);
1460 a596b957 2022-07-14 tracey goto done;
1462 a596b957 2022-07-14 tracey x += strlen(esc[j].s);
1463 a596b957 2022-07-14 tracey escaped = 1;
1466 a596b957 2022-07-14 tracey if (!escaped) {
1467 a596b957 2022-07-14 tracey (*escaped_html)[x] = orig_html[i];
1472 a596b957 2022-07-14 tracey if (error) {
1473 a596b957 2022-07-14 tracey free(*escaped_html);
1474 a596b957 2022-07-14 tracey *escaped_html = NULL;
1475 a596b957 2022-07-14 tracey } else {
1476 a596b957 2022-07-14 tracey (*escaped_html)[x] = '\0';
1479 a596b957 2022-07-14 tracey return error;
1482 8d02314f 2022-09-07 op static inline int
1483 8d02314f 2022-09-07 op should_urlencode(int c)
1485 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
1488 8d02314f 2022-09-07 op switch (c) {
1489 8d02314f 2022-09-07 op /* gen-delim */
1497 8d02314f 2022-09-07 op /* sub-delims */
1509 4a7f5bae 2023-01-05 op /* needed because the URLs are embedded into the HTML */
1517 8d02314f 2022-09-07 op static char *
1518 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
1520 8d02314f 2022-09-07 op const char *s;
1521 8d02314f 2022-09-07 op char *escaped;
1522 8d02314f 2022-09-07 op size_t i, len;
1526 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1528 8d02314f 2022-09-07 op if (should_urlencode(*s))
1532 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
1533 8d02314f 2022-09-07 op if (escaped == NULL)
1534 8d02314f 2022-09-07 op return NULL;
1537 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1538 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1539 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1540 8d02314f 2022-09-07 op b = (*s & 0x0F);
1542 8d02314f 2022-09-07 op escaped[i++] = '%';
1543 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1544 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1546 8d02314f 2022-09-07 op escaped[i++] = *s;
1549 8d02314f 2022-09-07 op return escaped;
1552 ed619ca0 2022-12-14 op const char *
1553 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1555 8d02314f 2022-09-07 op switch (action) {
1557 8d02314f 2022-09-07 op return "blame";
1559 8d02314f 2022-09-07 op return "blob";
1560 298f95fb 2023-01-05 op case BLOBRAW:
1561 298f95fb 2023-01-05 op return "blobraw";
1562 8d02314f 2022-09-07 op case BRIEFS:
1563 8d02314f 2022-09-07 op return "briefs";
1564 8d02314f 2022-09-07 op case COMMITS:
1565 8d02314f 2022-09-07 op return "commits";
1567 8d02314f 2022-09-07 op return "diff";
1569 8d02314f 2022-09-07 op return "err";
1571 8d02314f 2022-09-07 op return "index";
1572 8d02314f 2022-09-07 op case SUMMARY:
1573 8d02314f 2022-09-07 op return "summary";
1575 8d02314f 2022-09-07 op return "tag";
1577 8d02314f 2022-09-07 op return "tags";
1579 8d02314f 2022-09-07 op return "tree";
1581 1abb18e1 2022-12-20 op return "rss";
1583 8d02314f 2022-09-07 op return NULL;
1588 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1590 8d02314f 2022-09-07 op const char *sep = "?", *action;
1594 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1595 8d02314f 2022-09-07 op if (action != NULL) {
1596 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1601 8d02314f 2022-09-07 op if (url->commit) {
1602 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1607 8d02314f 2022-09-07 op if (url->previd) {
1608 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1613 8d02314f 2022-09-07 op if (url->prevset) {
1614 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1619 8d02314f 2022-09-07 op if (url->file) {
1620 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1621 8d02314f 2022-09-07 op if (tmp == NULL)
1623 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1625 8d02314f 2022-09-07 op if (r == -1)
1630 8d02314f 2022-09-07 op if (url->folder) {
1631 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1632 8d02314f 2022-09-07 op if (tmp == NULL)
1634 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1636 8d02314f 2022-09-07 op if (r == -1)
1641 8d02314f 2022-09-07 op if (url->headref) {
1642 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1643 8d02314f 2022-09-07 op if (tmp == NULL)
1645 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1647 8d02314f 2022-09-07 op if (r == -1)
1652 8d02314f 2022-09-07 op if (url->index_page != -1) {
1653 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1654 8d02314f 2022-09-07 op url->index_page) == -1)
1659 8d02314f 2022-09-07 op if (url->path) {
1660 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1661 8d02314f 2022-09-07 op if (tmp == NULL)
1663 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1665 8d02314f 2022-09-07 op if (r == -1)
1670 8d02314f 2022-09-07 op if (url->page != -1) {
1671 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1680 1abb18e1 2022-12-20 op gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1682 1abb18e1 2022-12-20 op struct template *tp = c->tp;
1683 1abb18e1 2022-12-20 op const char *proto = c->https ? "https" : "http";
1685 1abb18e1 2022-12-20 op if (fcgi_puts(tp, proto) == -1 ||
1686 1abb18e1 2022-12-20 op fcgi_puts(tp, "://") == -1 ||
1687 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->server_name) == -1 ||
1688 1abb18e1 2022-12-20 op tp_htmlescape(tp, c->document_uri) == -1)
1691 1abb18e1 2022-12-20 op return gotweb_render_url(c, url);
1695 8d02314f 2022-09-07 op gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1700 8d02314f 2022-09-07 op if (fcgi_printf(c, "<a href='") == -1)
1703 ed619ca0 2022-12-14 op if (gotweb_render_url(c, url) == -1)
1706 8d02314f 2022-09-07 op if (fcgi_printf(c, "'>") == -1)
1709 8d02314f 2022-09-07 op va_start(ap, fmt);
1710 8d02314f 2022-09-07 op r = fcgi_vprintf(c, fmt, ap);
1712 8d02314f 2022-09-07 op if (r == -1)
1715 8d02314f 2022-09-07 op if (fcgi_printf(c, "</a>"))
1720 b5c757f5 2022-09-01 stsp static struct got_repository *
1721 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1725 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1726 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1727 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1730 b5c757f5 2022-09-01 stsp return NULL;
1733 a596b957 2022-07-14 tracey static const struct got_error *
1734 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1735 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1737 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1738 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1739 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1740 b5c757f5 2022-09-01 stsp int evicted = 0;
1742 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1743 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1744 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1745 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1746 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1747 b5c757f5 2022-09-01 stsp if (error)
1748 b5c757f5 2022-09-01 stsp return error;
1749 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1750 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1751 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1752 b5c757f5 2022-09-01 stsp evicted = 1;
1754 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1757 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1758 b5c757f5 2022-09-01 stsp if (error) {
1759 b5c757f5 2022-09-01 stsp if (evicted) {
1760 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1761 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1763 b5c757f5 2022-09-01 stsp return error;
1766 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1767 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1768 b5c757f5 2022-09-01 stsp if (evicted) {
1769 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1770 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1772 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1775 b5c757f5 2022-09-01 stsp cr->repo = repo;
1776 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1777 b5c757f5 2022-09-01 stsp *new = repo;
1778 b5c757f5 2022-09-01 stsp return NULL;
1781 b5c757f5 2022-09-01 stsp static const struct got_error *
1782 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1784 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1785 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1786 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1787 a596b957 2022-07-14 tracey struct transport *t = c->t;
1788 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1789 a596b957 2022-07-14 tracey DIR *dt;
1790 a596b957 2022-07-14 tracey char *dir_test;
1792 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1793 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1794 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1796 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1797 a596b957 2022-07-14 tracey if (dt == NULL) {
1798 a596b957 2022-07-14 tracey free(dir_test);
1799 a596b957 2022-07-14 tracey } else {
1800 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1801 a596b957 2022-07-14 tracey dir_test = NULL;
1805 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1806 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1807 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1809 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1810 a596b957 2022-07-14 tracey if (dt == NULL) {
1811 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1812 a596b957 2022-07-14 tracey goto err;
1814 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1815 0fad85dd 2022-09-01 op dir_test = NULL;
1819 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1820 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1821 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1822 d5996b9e 2022-10-31 landry goto err;
1825 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1826 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1827 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1828 b5c757f5 2022-09-01 stsp if (error)
1831 b5c757f5 2022-09-01 stsp t->repo = repo;
1832 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1833 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1834 a596b957 2022-07-14 tracey if (error)
1835 a596b957 2022-07-14 tracey goto err;
1836 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1837 a596b957 2022-07-14 tracey if (error)
1838 a596b957 2022-07-14 tracey goto err;
1839 c127fc49 2022-11-22 op error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1840 a596b957 2022-07-14 tracey if (error)
1841 a596b957 2022-07-14 tracey goto err;
1842 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1845 a596b957 2022-07-14 tracey free(dir_test);
1846 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1847 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1848 a596b957 2022-07-14 tracey return error;
1851 a596b957 2022-07-14 tracey static const struct got_error *
1852 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1854 a596b957 2022-07-14 tracey const struct got_error *error;
1856 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1857 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1858 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1860 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1861 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1862 a596b957 2022-07-14 tracey free(*repo_dir);
1863 a596b957 2022-07-14 tracey *repo_dir = NULL;
1864 a596b957 2022-07-14 tracey return error;
1866 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1867 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1868 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1869 a596b957 2022-07-14 tracey (*repo_dir)->age = NULL;
1870 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1872 a596b957 2022-07-14 tracey return NULL;
1875 a596b957 2022-07-14 tracey static const struct got_error *
1876 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1877 3b81530f 2022-11-22 op const char *dirpath, int dir)
1879 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1880 3b81530f 2022-11-22 op struct stat sb;
1881 3b81530f 2022-11-22 op int fd = -1;
1884 a596b957 2022-07-14 tracey *description = NULL;
1885 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1886 a596b957 2022-07-14 tracey return NULL;
1888 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1889 3b81530f 2022-11-22 op if (fd == -1) {
1890 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1891 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1892 3b81530f 2022-11-22 op dirpath, "description");
1894 a596b957 2022-07-14 tracey goto done;
1897 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1898 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1899 3b81530f 2022-11-22 op dirpath, "description");
1900 a596b957 2022-07-14 tracey goto done;
1903 3b81530f 2022-11-22 op len = sb.st_size;
1904 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1905 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1907 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1908 a596b957 2022-07-14 tracey if (*description == NULL) {
1909 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1910 a596b957 2022-07-14 tracey goto done;
1913 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1914 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1916 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1917 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1918 a596b957 2022-07-14 tracey return error;
1921 a596b957 2022-07-14 tracey static const struct got_error *
1922 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1925 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1926 3b81530f 2022-11-22 op struct stat sb;
1927 3b81530f 2022-11-22 op int fd = -1;
1930 a596b957 2022-07-14 tracey *url = NULL;
1931 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1932 a596b957 2022-07-14 tracey return NULL;
1934 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1935 3b81530f 2022-11-22 op if (fd == -1) {
1936 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1937 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1938 3b81530f 2022-11-22 op dirpath, "cloneurl");
1940 a596b957 2022-07-14 tracey goto done;
1943 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1944 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1945 3b81530f 2022-11-22 op dirpath, "cloneurl");
1946 a596b957 2022-07-14 tracey goto done;
1949 3b81530f 2022-11-22 op len = sb.st_size;
1950 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1951 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1953 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1954 a596b957 2022-07-14 tracey if (*url == NULL) {
1955 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1956 a596b957 2022-07-14 tracey goto done;
1959 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1960 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1962 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1963 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1964 a596b957 2022-07-14 tracey return error;
1967 a596b957 2022-07-14 tracey const struct got_error *
1968 a596b957 2022-07-14 tracey gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
1970 a596b957 2022-07-14 tracey struct tm tm;
1971 fced5a66 2022-07-20 naddy long long diff_time;
1972 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
1973 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
1974 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
1975 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
1976 a596b957 2022-07-14 tracey char *s;
1977 1abb18e1 2022-12-20 op char datebuf[64];
1980 a596b957 2022-07-14 tracey *repo_age = NULL;
1982 a596b957 2022-07-14 tracey switch (ref_tm) {
1983 a596b957 2022-07-14 tracey case TM_DIFF:
1984 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
1985 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1986 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1987 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
1988 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1989 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1990 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1991 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1992 a596b957 2022-07-14 tracey months) == -1)
1993 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1994 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1995 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
1996 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
1997 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1998 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1999 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2000 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
2001 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2002 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
2003 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2004 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
2005 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2006 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
2007 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2008 a596b957 2022-07-14 tracey minutes) == -1)
2009 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2010 a596b957 2022-07-14 tracey } else if (diff_time > 2) {
2011 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", diff_time,
2012 a596b957 2022-07-14 tracey seconds) == -1)
2013 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2014 a596b957 2022-07-14 tracey } else {
2015 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s", now) == -1)
2016 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2019 a596b957 2022-07-14 tracey case TM_LONG:
2020 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2021 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
2023 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
2024 a596b957 2022-07-14 tracey if (s == NULL)
2025 a596b957 2022-07-14 tracey return got_error_from_errno("asctime_r");
2027 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2028 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2030 1abb18e1 2022-12-20 op case TM_RFC822:
2031 1abb18e1 2022-12-20 op if (gmtime_r(&committer_time, &tm) == NULL)
2032 1abb18e1 2022-12-20 op return got_error_from_errno("gmtime_r");
2034 1abb18e1 2022-12-20 op r = strftime(datebuf, sizeof(datebuf),
2035 1abb18e1 2022-12-20 op "%a, %d %b %Y %H:%M:%S GMT", &tm);
2037 1abb18e1 2022-12-20 op return got_error(GOT_ERR_NO_SPACE);
2039 1abb18e1 2022-12-20 op *repo_age = strdup(datebuf);
2040 1abb18e1 2022-12-20 op if (*repo_age == NULL)
2041 1abb18e1 2022-12-20 op return got_error_from_errno("asprintf");
2044 a596b957 2022-07-14 tracey return NULL;