2 a596b957 2022-07-14 tracey * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 a596b957 2022-07-14 tracey * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 58381f70 2022-09-03 op * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 a596b957 2022-07-14 tracey * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 a596b957 2022-07-14 tracey * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
8 a596b957 2022-07-14 tracey * Permission to use, copy, modify, and distribute this software for any
9 a596b957 2022-07-14 tracey * purpose with or without fee is hereby granted, provided that the above
10 a596b957 2022-07-14 tracey * copyright notice and this permission notice appear in all copies.
12 a596b957 2022-07-14 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 a596b957 2022-07-14 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 a596b957 2022-07-14 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 a596b957 2022-07-14 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 a596b957 2022-07-14 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 a596b957 2022-07-14 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 a596b957 2022-07-14 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 a596b957 2022-07-14 tracey #include <net/if.h>
22 a596b957 2022-07-14 tracey #include <netinet/in.h>
23 b4c20a19 2022-07-15 naddy #include <sys/queue.h>
24 a596b957 2022-07-14 tracey #include <sys/stat.h>
25 a596b957 2022-07-14 tracey #include <sys/types.h>
27 58381f70 2022-09-03 op #include <ctype.h>
28 a596b957 2022-07-14 tracey #include <dirent.h>
29 a596b957 2022-07-14 tracey #include <errno.h>
30 a596b957 2022-07-14 tracey #include <event.h>
31 3b81530f 2022-11-22 op #include <fcntl.h>
32 a596b957 2022-07-14 tracey #include <imsg.h>
33 a596b957 2022-07-14 tracey #include <sha1.h>
34 a596b957 2022-07-14 tracey #include <stdio.h>
35 a596b957 2022-07-14 tracey #include <stdlib.h>
36 a596b957 2022-07-14 tracey #include <string.h>
37 a596b957 2022-07-14 tracey #include <unistd.h>
39 a596b957 2022-07-14 tracey #include "got_error.h"
40 a596b957 2022-07-14 tracey #include "got_object.h"
41 a596b957 2022-07-14 tracey #include "got_reference.h"
42 a596b957 2022-07-14 tracey #include "got_repository.h"
43 a596b957 2022-07-14 tracey #include "got_path.h"
44 a596b957 2022-07-14 tracey #include "got_cancel.h"
45 a596b957 2022-07-14 tracey #include "got_worktree.h"
46 a596b957 2022-07-14 tracey #include "got_diff.h"
47 a596b957 2022-07-14 tracey #include "got_commit_graph.h"
48 a596b957 2022-07-14 tracey #include "got_blame.h"
49 a596b957 2022-07-14 tracey #include "got_privsep.h"
51 a596b957 2022-07-14 tracey #include "proc.h"
52 a596b957 2022-07-14 tracey #include "gotwebd.h"
54 a596b957 2022-07-14 tracey static const struct querystring_keys querystring_keys[] = {
55 a596b957 2022-07-14 tracey { "action", ACTION },
56 a596b957 2022-07-14 tracey { "commit", COMMIT },
57 a596b957 2022-07-14 tracey { "file", RFILE },
58 a596b957 2022-07-14 tracey { "folder", FOLDER },
59 a596b957 2022-07-14 tracey { "headref", HEADREF },
60 a596b957 2022-07-14 tracey { "index_page", INDEX_PAGE },
61 a596b957 2022-07-14 tracey { "path", PATH },
62 a596b957 2022-07-14 tracey { "page", PAGE },
65 a596b957 2022-07-14 tracey static const struct action_keys action_keys[] = {
66 a596b957 2022-07-14 tracey { "blame", BLAME },
67 a596b957 2022-07-14 tracey { "blob", BLOB },
68 a596b957 2022-07-14 tracey { "briefs", BRIEFS },
69 a596b957 2022-07-14 tracey { "commits", COMMITS },
70 a596b957 2022-07-14 tracey { "diff", DIFF },
71 a596b957 2022-07-14 tracey { "error", ERR },
72 a596b957 2022-07-14 tracey { "index", INDEX },
73 a596b957 2022-07-14 tracey { "summary", SUMMARY },
74 a596b957 2022-07-14 tracey { "tag", TAG },
75 a596b957 2022-07-14 tracey { "tags", TAGS },
76 a596b957 2022-07-14 tracey { "tree", TREE },
79 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_querystring(struct querystring **);
80 a596b957 2022-07-14 tracey static const struct got_error *gotweb_parse_querystring(struct querystring **,
82 a596b957 2022-07-14 tracey static const struct got_error *gotweb_assign_querystring(struct querystring **,
83 a596b957 2022-07-14 tracey char *, char *);
84 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_index(struct request *);
85 a596b957 2022-07-14 tracey static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
86 a596b957 2022-07-14 tracey const char *);
87 a596b957 2022-07-14 tracey static const struct got_error *gotweb_load_got_path(struct request *c,
88 a596b957 2022-07-14 tracey struct repo_dir *);
89 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_repo_description(char **,
90 3b81530f 2022-11-22 op struct server *, const char *, int);
91 a596b957 2022-07-14 tracey static const struct got_error *gotweb_get_clone_url(char **, struct server *,
92 3b81530f 2022-11-22 op const char *, int);
93 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_blame(struct request *);
94 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_diff(struct request *);
95 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_summary(struct request *);
96 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tag(struct request *);
97 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tags(struct request *);
98 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_tree(struct request *);
99 a596b957 2022-07-14 tracey static const struct got_error *gotweb_render_branches(struct request *);
101 a596b957 2022-07-14 tracey static void gotweb_free_querystring(struct querystring *);
102 a596b957 2022-07-14 tracey static void gotweb_free_repo_dir(struct repo_dir *);
104 95a4a5a1 2022-08-30 op struct server *gotweb_get_server(uint8_t *, uint8_t *);
107 a596b957 2022-07-14 tracey gotweb_process_request(struct request *c)
109 a596b957 2022-07-14 tracey const struct got_error *error = NULL, *error2 = NULL;
110 a596b957 2022-07-14 tracey struct server *srv = NULL;
111 a596b957 2022-07-14 tracey struct querystring *qs = NULL;
112 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
113 a596b957 2022-07-14 tracey uint8_t err[] = "gotwebd experienced an error: ";
114 01498c42 2022-08-19 op int r, html = 0;
116 a596b957 2022-07-14 tracey /* init the transport */
117 a596b957 2022-07-14 tracey error = gotweb_init_transport(&c->t);
118 a596b957 2022-07-14 tracey if (error) {
119 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
122 a596b957 2022-07-14 tracey /* don't process any further if client disconnected */
123 a596b957 2022-07-14 tracey if (c->sock->client_status == CLIENT_DISCONNECT)
125 a596b957 2022-07-14 tracey /* get the gotwebd server */
126 95a4a5a1 2022-08-30 op srv = gotweb_get_server(c->server_name, c->http_host);
127 a596b957 2022-07-14 tracey if (srv == NULL) {
128 a596b957 2022-07-14 tracey log_warnx("%s: error server is NULL", __func__);
129 a596b957 2022-07-14 tracey goto err;
131 a596b957 2022-07-14 tracey c->srv = srv;
132 a596b957 2022-07-14 tracey /* parse our querystring */
133 a596b957 2022-07-14 tracey error = gotweb_init_querystring(&qs);
134 a596b957 2022-07-14 tracey if (error) {
135 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
136 a596b957 2022-07-14 tracey goto err;
138 a596b957 2022-07-14 tracey c->t->qs = qs;
139 a596b957 2022-07-14 tracey error = gotweb_parse_querystring(&qs, c->querystring);
140 a596b957 2022-07-14 tracey if (error) {
141 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
142 a596b957 2022-07-14 tracey goto err;
146 a596b957 2022-07-14 tracey * certain actions require a commit id in the querystring. this stops
147 a596b957 2022-07-14 tracey * bad actors from exploiting this by manually manipulating the
148 a596b957 2022-07-14 tracey * querystring.
151 a596b957 2022-07-14 tracey if (qs->commit == NULL && (qs->action == BLAME || qs->action == BLOB ||
152 a596b957 2022-07-14 tracey qs->action == DIFF)) {
153 a596b957 2022-07-14 tracey error2 = got_error(GOT_ERR_QUERYSTRING);
154 a596b957 2022-07-14 tracey goto render;
157 a596b957 2022-07-14 tracey if (qs->action != INDEX) {
158 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, qs->path);
159 a596b957 2022-07-14 tracey if (error)
160 a596b957 2022-07-14 tracey goto done;
161 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
162 a596b957 2022-07-14 tracey c->t->repo_dir = repo_dir;
163 a596b957 2022-07-14 tracey if (error && error->code != GOT_ERR_LONELY_PACKIDX)
164 a596b957 2022-07-14 tracey goto err;
167 a596b957 2022-07-14 tracey /* render top of page */
168 a596b957 2022-07-14 tracey if (qs != NULL && qs->action == BLOB) {
169 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
170 a596b957 2022-07-14 tracey if (error)
171 a596b957 2022-07-14 tracey goto done;
172 a596b957 2022-07-14 tracey error = got_output_file_blob(c);
173 a596b957 2022-07-14 tracey if (error) {
174 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
175 a596b957 2022-07-14 tracey goto err;
177 a596b957 2022-07-14 tracey goto done;
181 6970304f 2022-12-04 op error = gotweb_render_content_type(c, "text/html");
183 6970304f 2022-12-04 op log_warnx("%s: %s", __func__, error->msg);
188 ed619ca0 2022-12-14 op if (gotweb_render_header(c->tp) == -1)
189 a596b957 2022-07-14 tracey goto err;
191 a596b957 2022-07-14 tracey if (error2) {
192 a596b957 2022-07-14 tracey error = error2;
193 a596b957 2022-07-14 tracey goto err;
196 a596b957 2022-07-14 tracey switch(qs->action) {
197 a596b957 2022-07-14 tracey case BLAME:
198 a596b957 2022-07-14 tracey error = gotweb_render_blame(c);
199 a596b957 2022-07-14 tracey if (error) {
200 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
201 a596b957 2022-07-14 tracey goto err;
204 a596b957 2022-07-14 tracey case BRIEFS:
205 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
206 a596b957 2022-07-14 tracey goto err;
208 a596b957 2022-07-14 tracey case COMMITS:
209 156a1144 2022-12-17 op error = got_get_repo_commits(c, srv->max_commits_display);
210 a596b957 2022-07-14 tracey if (error) {
211 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
212 a596b957 2022-07-14 tracey goto err;
214 156a1144 2022-12-17 op if (gotweb_render_commits(c->tp) == -1)
217 a596b957 2022-07-14 tracey case DIFF:
218 a596b957 2022-07-14 tracey error = gotweb_render_diff(c);
219 a596b957 2022-07-14 tracey if (error) {
220 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
221 a596b957 2022-07-14 tracey goto err;
224 a596b957 2022-07-14 tracey case INDEX:
225 a596b957 2022-07-14 tracey error = gotweb_render_index(c);
226 a596b957 2022-07-14 tracey if (error) {
227 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
228 a596b957 2022-07-14 tracey goto err;
231 a596b957 2022-07-14 tracey case SUMMARY:
232 a596b957 2022-07-14 tracey error = gotweb_render_summary(c);
233 a596b957 2022-07-14 tracey if (error) {
234 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
235 a596b957 2022-07-14 tracey goto err;
238 a596b957 2022-07-14 tracey case TAG:
239 a596b957 2022-07-14 tracey error = gotweb_render_tag(c);
240 a596b957 2022-07-14 tracey if (error) {
241 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
242 a596b957 2022-07-14 tracey goto err;
245 a596b957 2022-07-14 tracey case TAGS:
246 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
247 a596b957 2022-07-14 tracey if (error) {
248 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
249 a596b957 2022-07-14 tracey goto err;
252 a596b957 2022-07-14 tracey case TREE:
253 a596b957 2022-07-14 tracey error = gotweb_render_tree(c);
254 a596b957 2022-07-14 tracey if (error) {
255 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
256 a596b957 2022-07-14 tracey goto err;
259 a596b957 2022-07-14 tracey case ERR:
261 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
262 01498c42 2022-08-19 op "Erorr: Bad Querystring");
264 a596b957 2022-07-14 tracey goto err;
268 a596b957 2022-07-14 tracey goto done;
270 01498c42 2022-08-19 op if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
272 b2e7d31e 2022-10-31 landry if (fcgi_printf(c, "\n%s", err) == -1)
274 a596b957 2022-07-14 tracey if (error) {
275 01498c42 2022-08-19 op if (fcgi_printf(c, "%s", error->msg) == -1)
278 01498c42 2022-08-19 op if (fcgi_printf(c, "see daemon logs for details") == -1)
281 01498c42 2022-08-19 op if (html && fcgi_printf(c, "</div>\n") == -1)
284 a596b957 2022-07-14 tracey if (html && srv != NULL)
285 ed619ca0 2022-12-14 op gotweb_render_footer(c->tp);
288 a596b957 2022-07-14 tracey struct server *
289 95a4a5a1 2022-08-30 op gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
291 a596b957 2022-07-14 tracey struct server *srv = NULL;
293 95a4a5a1 2022-08-30 op /* check against the server name first */
294 a596b957 2022-07-14 tracey if (strlen(server_name) > 0)
295 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
296 a596b957 2022-07-14 tracey if (strcmp(srv->name, server_name) == 0)
297 a596b957 2022-07-14 tracey goto done;
299 95a4a5a1 2022-08-30 op /* check against subdomain second */
300 a596b957 2022-07-14 tracey if (strlen(subdomain) > 0)
301 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
302 a596b957 2022-07-14 tracey if (strcmp(srv->name, subdomain) == 0)
303 a596b957 2022-07-14 tracey goto done;
305 a596b957 2022-07-14 tracey /* if those fail, send first server */
306 2ad48e9a 2022-08-16 stsp TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
307 a596b957 2022-07-14 tracey if (srv != NULL)
310 a596b957 2022-07-14 tracey return srv;
313 a596b957 2022-07-14 tracey const struct got_error *
314 a596b957 2022-07-14 tracey gotweb_init_transport(struct transport **t)
316 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
318 a596b957 2022-07-14 tracey *t = calloc(1, sizeof(**t));
319 a596b957 2022-07-14 tracey if (*t == NULL)
320 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
322 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_commits);
323 a596b957 2022-07-14 tracey TAILQ_INIT(&(*t)->repo_tags);
325 a596b957 2022-07-14 tracey (*t)->repo = NULL;
326 a596b957 2022-07-14 tracey (*t)->repo_dir = NULL;
327 a596b957 2022-07-14 tracey (*t)->qs = NULL;
328 a596b957 2022-07-14 tracey (*t)->next_id = NULL;
329 a596b957 2022-07-14 tracey (*t)->prev_id = NULL;
330 a596b957 2022-07-14 tracey (*t)->next_disp = 0;
331 a596b957 2022-07-14 tracey (*t)->prev_disp = 0;
333 a596b957 2022-07-14 tracey return error;
336 a596b957 2022-07-14 tracey static const struct got_error *
337 a596b957 2022-07-14 tracey gotweb_init_querystring(struct querystring **qs)
339 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
341 a596b957 2022-07-14 tracey *qs = calloc(1, sizeof(**qs));
342 a596b957 2022-07-14 tracey if (*qs == NULL)
343 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: calloc", __func__);
345 6c37ad7b 2022-09-01 op (*qs)->headref = strdup("HEAD");
346 6c37ad7b 2022-09-01 op if ((*qs)->headref == NULL) {
349 6c37ad7b 2022-09-01 op return got_error_from_errno2("%s: strdup", __func__);
352 a596b957 2022-07-14 tracey (*qs)->action = INDEX;
353 a596b957 2022-07-14 tracey (*qs)->commit = NULL;
354 a596b957 2022-07-14 tracey (*qs)->file = NULL;
355 a596b957 2022-07-14 tracey (*qs)->folder = NULL;
356 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
357 a596b957 2022-07-14 tracey (*qs)->path = NULL;
359 a596b957 2022-07-14 tracey return error;
362 a596b957 2022-07-14 tracey static const struct got_error *
363 a596b957 2022-07-14 tracey gotweb_parse_querystring(struct querystring **qs, char *qst)
365 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
366 a596b957 2022-07-14 tracey char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
367 a596b957 2022-07-14 tracey char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
369 a596b957 2022-07-14 tracey if (qst == NULL)
370 a596b957 2022-07-14 tracey return error;
372 a596b957 2022-07-14 tracey tok1 = strdup(qst);
373 a596b957 2022-07-14 tracey if (tok1 == NULL)
374 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
376 a596b957 2022-07-14 tracey tok1_pair = tok1;
377 a596b957 2022-07-14 tracey tok1_end = tok1;
379 a596b957 2022-07-14 tracey while (tok1_pair != NULL) {
380 a596b957 2022-07-14 tracey strsep(&tok1_end, "&");
382 a596b957 2022-07-14 tracey tok2 = strdup(tok1_pair);
383 a596b957 2022-07-14 tracey if (tok2 == NULL) {
384 a596b957 2022-07-14 tracey free(tok1);
385 a596b957 2022-07-14 tracey return got_error_from_errno2("%s: strdup", __func__);
388 a596b957 2022-07-14 tracey tok2_pair = tok2;
389 a596b957 2022-07-14 tracey tok2_end = tok2;
391 a596b957 2022-07-14 tracey while (tok2_pair != NULL) {
392 a596b957 2022-07-14 tracey strsep(&tok2_end, "=");
393 a596b957 2022-07-14 tracey if (tok2_end) {
394 a596b957 2022-07-14 tracey error = gotweb_assign_querystring(qs, tok2_pair,
395 a596b957 2022-07-14 tracey tok2_end);
396 a596b957 2022-07-14 tracey if (error)
397 a596b957 2022-07-14 tracey goto err;
399 a596b957 2022-07-14 tracey tok2_pair = tok2_end;
401 a596b957 2022-07-14 tracey free(tok2);
402 a596b957 2022-07-14 tracey tok1_pair = tok1_end;
404 a596b957 2022-07-14 tracey free(tok1);
405 a596b957 2022-07-14 tracey return error;
407 a596b957 2022-07-14 tracey free(tok2);
408 a596b957 2022-07-14 tracey free(tok1);
409 a596b957 2022-07-14 tracey return error;
413 58381f70 2022-09-03 op * Adapted from usr.sbin/httpd/httpd.c url_decode.
415 a596b957 2022-07-14 tracey static const struct got_error *
416 58381f70 2022-09-03 op gotweb_urldecode(char *url)
420 58381f70 2022-09-03 op unsigned long x;
422 58381f70 2022-09-03 op hex[2] = '\0';
425 58381f70 2022-09-03 op while (*p != '\0') {
426 58381f70 2022-09-03 op switch (*p) {
428 58381f70 2022-09-03 op /* Encoding character is followed by two hex chars */
429 58381f70 2022-09-03 op if (!isxdigit((unsigned char)p[1]) ||
430 58381f70 2022-09-03 op !isxdigit((unsigned char)p[2]) ||
431 58381f70 2022-09-03 op (p[1] == '0' && p[2] == '0'))
432 58381f70 2022-09-03 op return got_error(GOT_ERR_BAD_QUERYSTRING);
434 58381f70 2022-09-03 op hex[0] = p[1];
435 58381f70 2022-09-03 op hex[1] = p[2];
438 58381f70 2022-09-03 op * We don't have to validate "hex" because it is
439 58381f70 2022-09-03 op * guaranteed to include two hex chars followed by nul.
441 58381f70 2022-09-03 op x = strtoul(hex, NULL, 16);
442 58381f70 2022-09-03 op *q = (char)x;
457 58381f70 2022-09-03 op static const struct got_error *
458 a596b957 2022-07-14 tracey gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
460 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
461 a596b957 2022-07-14 tracey const char *errstr;
462 a596b957 2022-07-14 tracey int a_cnt, el_cnt;
464 58381f70 2022-09-03 op error = gotweb_urldecode(value);
466 58381f70 2022-09-03 op return error;
468 a596b957 2022-07-14 tracey for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
469 a596b957 2022-07-14 tracey if (strcmp(key, querystring_keys[el_cnt].name) != 0)
470 a596b957 2022-07-14 tracey continue;
472 a596b957 2022-07-14 tracey switch (querystring_keys[el_cnt].element) {
473 a596b957 2022-07-14 tracey case ACTION:
474 a596b957 2022-07-14 tracey for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
475 a596b957 2022-07-14 tracey if (strcmp(value, action_keys[a_cnt].name) != 0)
476 a596b957 2022-07-14 tracey continue;
477 a596b957 2022-07-14 tracey else if (strcmp(value,
478 a596b957 2022-07-14 tracey action_keys[a_cnt].name) == 0){
479 a596b957 2022-07-14 tracey (*qs)->action =
480 a596b957 2022-07-14 tracey action_keys[a_cnt].action;
481 a596b957 2022-07-14 tracey goto qa_found;
484 a596b957 2022-07-14 tracey (*qs)->action = ERR;
485 a596b957 2022-07-14 tracey qa_found:
487 a596b957 2022-07-14 tracey case COMMIT:
488 a596b957 2022-07-14 tracey (*qs)->commit = strdup(value);
489 a596b957 2022-07-14 tracey if ((*qs)->commit == NULL) {
490 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
491 a596b957 2022-07-14 tracey __func__);
492 a596b957 2022-07-14 tracey goto done;
495 a596b957 2022-07-14 tracey case RFILE:
496 a596b957 2022-07-14 tracey (*qs)->file = strdup(value);
497 a596b957 2022-07-14 tracey if ((*qs)->file == NULL) {
498 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
499 a596b957 2022-07-14 tracey __func__);
500 a596b957 2022-07-14 tracey goto done;
503 a596b957 2022-07-14 tracey case FOLDER:
504 a596b957 2022-07-14 tracey (*qs)->folder = strdup(value);
505 a596b957 2022-07-14 tracey if ((*qs)->folder == NULL) {
506 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
507 a596b957 2022-07-14 tracey __func__);
508 a596b957 2022-07-14 tracey goto done;
511 a596b957 2022-07-14 tracey case HEADREF:
512 f8faf9f1 2022-09-01 op free((*qs)->headref);
513 a596b957 2022-07-14 tracey (*qs)->headref = strdup(value);
514 a596b957 2022-07-14 tracey if ((*qs)->headref == NULL) {
515 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
516 a596b957 2022-07-14 tracey __func__);
517 a596b957 2022-07-14 tracey goto done;
520 a596b957 2022-07-14 tracey case INDEX_PAGE:
521 a596b957 2022-07-14 tracey if (strlen(value) == 0)
523 a596b957 2022-07-14 tracey (*qs)->index_page = strtonum(value, INT64_MIN,
524 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
525 a596b957 2022-07-14 tracey if (errstr) {
526 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
527 a596b957 2022-07-14 tracey __func__, errstr);
528 a596b957 2022-07-14 tracey goto done;
530 03f6a843 2022-12-17 op if ((*qs)->index_page < 0)
531 a596b957 2022-07-14 tracey (*qs)->index_page = 0;
533 a596b957 2022-07-14 tracey case PATH:
534 a596b957 2022-07-14 tracey (*qs)->path = strdup(value);
535 a596b957 2022-07-14 tracey if ((*qs)->path == NULL) {
536 a596b957 2022-07-14 tracey error = got_error_from_errno2("%s: strdup",
537 a596b957 2022-07-14 tracey __func__);
538 a596b957 2022-07-14 tracey goto done;
541 a596b957 2022-07-14 tracey case PAGE:
542 a596b957 2022-07-14 tracey if (strlen(value) == 0)
544 a596b957 2022-07-14 tracey (*qs)->page = strtonum(value, INT64_MIN,
545 a596b957 2022-07-14 tracey INT64_MAX, &errstr);
546 a596b957 2022-07-14 tracey if (errstr) {
547 a596b957 2022-07-14 tracey error = got_error_from_errno3("%s: strtonum %s",
548 a596b957 2022-07-14 tracey __func__, errstr);
549 a596b957 2022-07-14 tracey goto done;
551 03f6a843 2022-12-17 op if ((*qs)->page < 0)
552 a596b957 2022-07-14 tracey (*qs)->page = 0;
559 a596b957 2022-07-14 tracey return error;
563 a596b957 2022-07-14 tracey gotweb_free_repo_tag(struct repo_tag *rt)
565 a596b957 2022-07-14 tracey if (rt != NULL) {
566 a596b957 2022-07-14 tracey free(rt->commit_id);
567 625e5896 2022-09-01 op free(rt->tag_name);
568 625e5896 2022-09-01 op free(rt->tag_commit);
569 625e5896 2022-09-01 op free(rt->commit_msg);
570 a596b957 2022-07-14 tracey free(rt->tagger);
572 a596b957 2022-07-14 tracey free(rt);
576 a596b957 2022-07-14 tracey gotweb_free_repo_commit(struct repo_commit *rc)
578 a596b957 2022-07-14 tracey if (rc != NULL) {
579 a596b957 2022-07-14 tracey free(rc->path);
580 a596b957 2022-07-14 tracey free(rc->refs_str);
581 a596b957 2022-07-14 tracey free(rc->commit_id);
582 a596b957 2022-07-14 tracey free(rc->parent_id);
583 a596b957 2022-07-14 tracey free(rc->tree_id);
584 a596b957 2022-07-14 tracey free(rc->author);
585 a596b957 2022-07-14 tracey free(rc->committer);
586 a596b957 2022-07-14 tracey free(rc->commit_msg);
588 a596b957 2022-07-14 tracey free(rc);
591 a596b957 2022-07-14 tracey static void
592 a596b957 2022-07-14 tracey gotweb_free_querystring(struct querystring *qs)
594 a596b957 2022-07-14 tracey if (qs != NULL) {
595 a596b957 2022-07-14 tracey free(qs->commit);
596 a596b957 2022-07-14 tracey free(qs->file);
597 a596b957 2022-07-14 tracey free(qs->folder);
598 a596b957 2022-07-14 tracey free(qs->headref);
599 a596b957 2022-07-14 tracey free(qs->path);
601 a596b957 2022-07-14 tracey free(qs);
604 a596b957 2022-07-14 tracey static void
605 a596b957 2022-07-14 tracey gotweb_free_repo_dir(struct repo_dir *repo_dir)
607 a596b957 2022-07-14 tracey if (repo_dir != NULL) {
608 a596b957 2022-07-14 tracey free(repo_dir->name);
609 a596b957 2022-07-14 tracey free(repo_dir->owner);
610 a596b957 2022-07-14 tracey free(repo_dir->description);
611 a596b957 2022-07-14 tracey free(repo_dir->url);
612 a596b957 2022-07-14 tracey free(repo_dir->age);
613 a596b957 2022-07-14 tracey free(repo_dir->path);
615 a596b957 2022-07-14 tracey free(repo_dir);
619 a596b957 2022-07-14 tracey gotweb_free_transport(struct transport *t)
621 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL, *trc = NULL;
622 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL, *trt = NULL;
624 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
625 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_commits, rc, entry);
626 a596b957 2022-07-14 tracey gotweb_free_repo_commit(rc);
628 a596b957 2022-07-14 tracey TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
629 a596b957 2022-07-14 tracey TAILQ_REMOVE(&t->repo_tags, rt, entry);
630 a596b957 2022-07-14 tracey gotweb_free_repo_tag(rt);
632 a596b957 2022-07-14 tracey gotweb_free_repo_dir(t->repo_dir);
633 a596b957 2022-07-14 tracey gotweb_free_querystring(t->qs);
634 341fa7ca 2022-09-01 op free(t->next_id);
635 341fa7ca 2022-09-01 op free(t->prev_id);
639 a596b957 2022-07-14 tracey const struct got_error *
640 a596b957 2022-07-14 tracey gotweb_render_content_type(struct request *c, const uint8_t *type)
642 4d648b92 2022-08-20 op const char *csp = "default-src 'self'; script-src 'none'; "
643 4d648b92 2022-08-20 op "object-src 'none';";
645 4d648b92 2022-08-20 op fcgi_printf(c,
646 4d648b92 2022-08-20 op "Content-Security-Policy: %s\r\n"
647 4d648b92 2022-08-20 op "Content-Type: %s\r\n\r\n",
652 a596b957 2022-07-14 tracey const struct got_error *
653 a596b957 2022-07-14 tracey gotweb_render_content_type_file(struct request *c, const uint8_t *type,
654 a596b957 2022-07-14 tracey char *file)
656 01498c42 2022-08-19 op fcgi_printf(c, "Content-type: %s\r\n"
657 a596b957 2022-07-14 tracey "Content-disposition: attachment; filename=%s\r\n\r\n",
663 b4c0bd72 2022-12-17 op gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
664 b4c0bd72 2022-12-17 op struct gotweb_url *next, int *have_next)
666 a596b957 2022-07-14 tracey struct transport *t = c->t;
667 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
668 a596b957 2022-07-14 tracey struct server *srv = c->srv;
670 b4c0bd72 2022-12-17 op *have_prev = *have_next = 0;
672 a596b957 2022-07-14 tracey switch(qs->action) {
673 a596b957 2022-07-14 tracey case INDEX:
674 a596b957 2022-07-14 tracey if (qs->index_page > 0) {
675 b4c0bd72 2022-12-17 op *have_prev = 1;
676 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
677 8d02314f 2022-09-07 op .action = -1,
678 8d02314f 2022-09-07 op .index_page = qs->index_page - 1,
682 a596b957 2022-07-14 tracey if (t->next_disp == srv->max_repos_display &&
683 a596b957 2022-07-14 tracey t->repos_total != (qs->index_page + 1) *
684 a596b957 2022-07-14 tracey srv->max_repos_display) {
685 b4c0bd72 2022-12-17 op *have_next = 1;
686 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
687 8d02314f 2022-09-07 op .action = -1,
688 8d02314f 2022-09-07 op .index_page = qs->index_page + 1,
693 a596b957 2022-07-14 tracey case BRIEFS:
694 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
695 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
696 b4c0bd72 2022-12-17 op *have_prev = 1;
697 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
698 b4c0bd72 2022-12-17 op .action = BRIEFS,
699 b4c0bd72 2022-12-17 op .index_page = -1,
700 b4c0bd72 2022-12-17 op .page = qs->page - 1,
701 b4c0bd72 2022-12-17 op .path = qs->path,
702 b4c0bd72 2022-12-17 op .commit = t->prev_id,
703 b4c0bd72 2022-12-17 op .headref = qs->headref,
706 a596b957 2022-07-14 tracey if (t->next_id) {
707 b4c0bd72 2022-12-17 op *have_next = 1;
708 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
709 8d02314f 2022-09-07 op .action = BRIEFS,
710 8d02314f 2022-09-07 op .index_page = -1,
711 8d02314f 2022-09-07 op .page = qs->page + 1,
712 8d02314f 2022-09-07 op .path = qs->path,
713 8d02314f 2022-09-07 op .commit = t->next_id,
714 8d02314f 2022-09-07 op .headref = qs->headref,
718 a596b957 2022-07-14 tracey case COMMITS:
719 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
720 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
721 b4c0bd72 2022-12-17 op *have_prev = 1;
722 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
723 b4c0bd72 2022-12-17 op .action = COMMITS,
724 b4c0bd72 2022-12-17 op .index_page = -1,
725 b4c0bd72 2022-12-17 op .page = qs->page - 1,
726 b4c0bd72 2022-12-17 op .path = qs->path,
727 b4c0bd72 2022-12-17 op .commit = t->prev_id,
728 b4c0bd72 2022-12-17 op .headref = qs->headref,
729 b4c0bd72 2022-12-17 op .folder = qs->folder,
730 b4c0bd72 2022-12-17 op .file = qs->file,
733 a596b957 2022-07-14 tracey if (t->next_id) {
734 b4c0bd72 2022-12-17 op *have_next = 1;
735 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
736 6169d054 2022-12-17 op .action = COMMITS,
737 8d02314f 2022-09-07 op .index_page = -1,
738 8d02314f 2022-09-07 op .page = qs->page + 1,
739 8d02314f 2022-09-07 op .path = qs->path,
740 8d02314f 2022-09-07 op .commit = t->next_id,
741 8d02314f 2022-09-07 op .headref = qs->headref,
742 8d02314f 2022-09-07 op .folder = qs->folder,
743 8d02314f 2022-09-07 op .file = qs->file,
747 a596b957 2022-07-14 tracey case TAGS:
748 b4c0bd72 2022-12-17 op if (t->prev_id && qs->commit != NULL &&
749 b4c0bd72 2022-12-17 op strcmp(qs->commit, t->prev_id) != 0) {
750 b4c0bd72 2022-12-17 op *have_prev = 1;
751 b4c0bd72 2022-12-17 op *prev = (struct gotweb_url){
752 b4c0bd72 2022-12-17 op .action = TAGS,
753 b4c0bd72 2022-12-17 op .index_page = -1,
754 b4c0bd72 2022-12-17 op .page = qs->page - 1,
755 b4c0bd72 2022-12-17 op .path = qs->path,
756 b4c0bd72 2022-12-17 op .commit = t->prev_id,
757 b4c0bd72 2022-12-17 op .headref = qs->headref,
760 a596b957 2022-07-14 tracey if (t->next_id) {
761 b4c0bd72 2022-12-17 op *have_next = 1;
762 b4c0bd72 2022-12-17 op *next = (struct gotweb_url){
763 8d02314f 2022-09-07 op .action = TAGS,
764 8d02314f 2022-09-07 op .index_page = -1,
765 8d02314f 2022-09-07 op .page = qs->page + 1,
766 8d02314f 2022-09-07 op .path = qs->path,
767 8d02314f 2022-09-07 op .commit = t->next_id,
768 8d02314f 2022-09-07 op .headref = qs->headref,
775 a596b957 2022-07-14 tracey static const struct got_error *
776 a596b957 2022-07-14 tracey gotweb_render_index(struct request *c)
778 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
779 a596b957 2022-07-14 tracey struct server *srv = c->srv;
780 a596b957 2022-07-14 tracey struct transport *t = c->t;
781 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
782 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = NULL;
784 2db401bd 2022-09-01 op struct dirent **sd_dent = NULL;
785 a596b957 2022-07-14 tracey unsigned int d_cnt, d_i, d_disp = 0;
786 525dfdf4 2022-11-22 op unsigned int d_skipped = 0;
789 a596b957 2022-07-14 tracey d = opendir(srv->repos_path);
790 a596b957 2022-07-14 tracey if (d == NULL) {
791 a596b957 2022-07-14 tracey error = got_error_from_errno2("opendir", srv->repos_path);
792 a596b957 2022-07-14 tracey return error;
795 a596b957 2022-07-14 tracey d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
796 a596b957 2022-07-14 tracey if (d_cnt == -1) {
797 2db401bd 2022-09-01 op sd_dent = NULL;
798 a596b957 2022-07-14 tracey error = got_error_from_errno2("scandir", srv->repos_path);
799 a596b957 2022-07-14 tracey goto done;
802 ed619ca0 2022-12-14 op if (gotweb_render_repo_table_hdr(c->tp) == -1)
803 a596b957 2022-07-14 tracey goto done;
805 a596b957 2022-07-14 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
806 659fa237 2022-11-22 op if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
809 a596b957 2022-07-14 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
810 525dfdf4 2022-11-22 op strcmp(sd_dent[d_i]->d_name, "..") == 0) {
812 a596b957 2022-07-14 tracey continue;
815 525dfdf4 2022-11-22 op error = got_path_dirent_type(&type, srv->repos_path,
816 525dfdf4 2022-11-22 op sd_dent[d_i]);
819 525dfdf4 2022-11-22 op if (type != DT_DIR) {
824 a596b957 2022-07-14 tracey if (qs->index_page > 0 && (qs->index_page *
825 a596b957 2022-07-14 tracey srv->max_repos_display) > t->prev_disp) {
826 a596b957 2022-07-14 tracey t->prev_disp++;
827 a596b957 2022-07-14 tracey continue;
830 a596b957 2022-07-14 tracey error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
831 a596b957 2022-07-14 tracey if (error)
832 a596b957 2022-07-14 tracey goto done;
834 a596b957 2022-07-14 tracey error = gotweb_load_got_path(c, repo_dir);
835 a596b957 2022-07-14 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
836 a596b957 2022-07-14 tracey error = NULL;
837 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
838 a596b957 2022-07-14 tracey repo_dir = NULL;
840 a596b957 2022-07-14 tracey continue;
842 525dfdf4 2022-11-22 op if (error && error->code != GOT_ERR_LONELY_PACKIDX)
845 a596b957 2022-07-14 tracey d_disp++;
846 a596b957 2022-07-14 tracey t->prev_disp++;
848 ed619ca0 2022-12-14 op if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
851 a596b957 2022-07-14 tracey gotweb_free_repo_dir(repo_dir);
852 a596b957 2022-07-14 tracey repo_dir = NULL;
853 a596b957 2022-07-14 tracey t->next_disp++;
854 a596b957 2022-07-14 tracey if (d_disp == srv->max_repos_display)
857 525dfdf4 2022-11-22 op t->repos_total = d_cnt - d_skipped;
859 a596b957 2022-07-14 tracey if (srv->max_repos_display == 0)
861 a596b957 2022-07-14 tracey if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
863 a596b957 2022-07-14 tracey if (t->repos_total <= srv->max_repos ||
864 a596b957 2022-07-14 tracey t->repos_total <= srv->max_repos_display)
867 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
868 a596b957 2022-07-14 tracey goto done;
870 2db401bd 2022-09-01 op if (sd_dent) {
871 2db401bd 2022-09-01 op for (d_i = 0; d_i < d_cnt; d_i++)
872 2db401bd 2022-09-01 op free(sd_dent[d_i]);
873 2db401bd 2022-09-01 op free(sd_dent);
875 a596b957 2022-07-14 tracey if (d != NULL && closedir(d) == EOF && error == NULL)
876 a596b957 2022-07-14 tracey error = got_error_from_errno("closedir");
877 a596b957 2022-07-14 tracey return error;
880 a596b957 2022-07-14 tracey static const struct got_error *
881 a596b957 2022-07-14 tracey gotweb_render_blame(struct request *c)
883 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
884 a596b957 2022-07-14 tracey struct transport *t = c->t;
885 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
886 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
889 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
890 a596b957 2022-07-14 tracey if (error)
891 a596b957 2022-07-14 tracey return error;
893 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
895 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
896 a596b957 2022-07-14 tracey if (error)
897 a596b957 2022-07-14 tracey goto done;
898 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
902 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
903 01498c42 2022-08-19 op "<div id='blame_title'>Blame</div>\n"
904 01498c42 2022-08-19 op "</div>\n" /* #blame_title_wrapper */
905 01498c42 2022-08-19 op "<div id='blame_content'>\n"
906 01498c42 2022-08-19 op "<div id='blame_header_wrapper'>\n"
907 01498c42 2022-08-19 op "<div id='blame_header'>\n"
908 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
909 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
910 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
911 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
912 01498c42 2022-08-19 op "</div>\n" /* #blame_header */
913 01498c42 2022-08-19 op "</div>\n" /* #blame_header_wrapper */
914 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
915 01498c42 2022-08-19 op "<div id='blame'>\n",
919 a596b957 2022-07-14 tracey goto done;
921 a596b957 2022-07-14 tracey error = got_output_file_blame(c);
922 a596b957 2022-07-14 tracey if (error)
923 a596b957 2022-07-14 tracey goto done;
925 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n" /* #blame */
926 01498c42 2022-08-19 op "</div>\n"); /* #blame_content */
930 a596b957 2022-07-14 tracey return error;
933 a596b957 2022-07-14 tracey static const struct got_error *
934 a596b957 2022-07-14 tracey gotweb_render_branches(struct request *c)
936 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
937 a596b957 2022-07-14 tracey struct got_reflist_head refs;
938 a596b957 2022-07-14 tracey struct got_reflist_entry *re;
939 a596b957 2022-07-14 tracey struct transport *t = c->t;
940 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
941 a596b957 2022-07-14 tracey struct got_repository *repo = t->repo;
942 8d02314f 2022-09-07 op char *escaped_refname = NULL;
943 a596b957 2022-07-14 tracey char *age = NULL;
946 a596b957 2022-07-14 tracey TAILQ_INIT(&refs);
948 a596b957 2022-07-14 tracey error = got_ref_list(&refs, repo, "refs/heads",
949 a596b957 2022-07-14 tracey got_ref_cmp_by_name, NULL);
950 a596b957 2022-07-14 tracey if (error)
951 a596b957 2022-07-14 tracey goto done;
953 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
954 01498c42 2022-08-19 op "<div id='branches_title'>Branches</div>\n"
955 01498c42 2022-08-19 op "</div>\n" /* #branches_title_wrapper */
956 01498c42 2022-08-19 op "<div id='branches_content'>\n");
958 a596b957 2022-07-14 tracey goto done;
960 a596b957 2022-07-14 tracey TAILQ_FOREACH(re, &refs, entry) {
961 d927f8c8 2022-08-20 op const char *refname = NULL;
963 a596b957 2022-07-14 tracey if (got_ref_is_symbolic(re->ref))
964 a596b957 2022-07-14 tracey continue;
966 d927f8c8 2022-08-20 op refname = got_ref_get_name(re->ref);
967 a596b957 2022-07-14 tracey if (refname == NULL) {
968 a596b957 2022-07-14 tracey error = got_error_from_errno("strdup");
969 a596b957 2022-07-14 tracey goto done;
971 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) != 0)
972 a596b957 2022-07-14 tracey continue;
974 c127fc49 2022-11-22 op error = got_get_repo_age(&age, c, refname, TM_DIFF);
975 a596b957 2022-07-14 tracey if (error)
976 a596b957 2022-07-14 tracey goto done;
978 a596b957 2022-07-14 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
979 a596b957 2022-07-14 tracey refname += 11;
980 d927f8c8 2022-08-20 op error = gotweb_escape_html(&escaped_refname, refname);
984 01498c42 2022-08-19 op r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
985 01498c42 2022-08-19 op "<div class='branches_age'>%s</div>\n"
986 01498c42 2022-08-19 op "<div class='branches_space'> </div>\n"
987 8d02314f 2022-09-07 op "<div class='branch'>", age);
991 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
992 8d02314f 2022-09-07 op .action = SUMMARY,
993 8d02314f 2022-09-07 op .index_page = -1,
995 8d02314f 2022-09-07 op .path = qs->path,
996 8d02314f 2022-09-07 op .headref = refname,
997 8d02314f 2022-09-07 op }, "%s", escaped_refname);
1001 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .branch */
1002 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1003 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1006 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1007 8d02314f 2022-09-07 op .action = SUMMARY,
1008 8d02314f 2022-09-07 op .index_page = -1,
1010 8d02314f 2022-09-07 op .path = qs->path,
1011 8d02314f 2022-09-07 op .headref = refname,
1012 8d02314f 2022-09-07 op }, "summary");
1013 8d02314f 2022-09-07 op if (r == -1)
1016 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1019 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1020 8d02314f 2022-09-07 op .action = BRIEFS,
1021 8d02314f 2022-09-07 op .index_page = -1,
1023 8d02314f 2022-09-07 op .path = qs->path,
1024 8d02314f 2022-09-07 op .headref = refname,
1025 8d02314f 2022-09-07 op }, "commit briefs");
1026 8d02314f 2022-09-07 op if (r == -1)
1029 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1032 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1033 8d02314f 2022-09-07 op .action = COMMITS,
1034 8d02314f 2022-09-07 op .index_page = -1,
1036 8d02314f 2022-09-07 op .path = qs->path,
1037 8d02314f 2022-09-07 op .headref = refname,
1038 8d02314f 2022-09-07 op }, "commits");
1039 8d02314f 2022-09-07 op if (r == -1)
1042 8d02314f 2022-09-07 op r = fcgi_printf(c, "</div>\n" /* .navs */
1043 8d02314f 2022-09-07 op "</div>\n" /* .navs_wrapper */
1044 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1045 8d02314f 2022-09-07 op "</div>\n"); /* .branches_wrapper */
1046 8d02314f 2022-09-07 op if (r == -1)
1051 d927f8c8 2022-08-20 op free(escaped_refname);
1052 8d02314f 2022-09-07 op escaped_refname = NULL;
1054 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #branches_content */
1057 8d02314f 2022-09-07 op free(escaped_refname);
1058 f49cdcf5 2022-09-02 op got_ref_list_free(&refs);
1059 a596b957 2022-07-14 tracey return error;
1062 a596b957 2022-07-14 tracey static const struct got_error *
1063 a596b957 2022-07-14 tracey gotweb_render_tree(struct request *c)
1065 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1066 a596b957 2022-07-14 tracey struct transport *t = c->t;
1067 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1068 d927f8c8 2022-08-20 op char *age = NULL, *msg = NULL;
1071 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
1072 a596b957 2022-07-14 tracey if (error)
1073 a596b957 2022-07-14 tracey return error;
1075 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1077 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1078 a596b957 2022-07-14 tracey if (error)
1079 a596b957 2022-07-14 tracey goto done;
1081 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
1085 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='tree_title_wrapper'>\n"
1086 01498c42 2022-08-19 op "<div id='tree_title'>Tree</div>\n"
1087 01498c42 2022-08-19 op "</div>\n" /* #tree_title_wrapper */
1088 01498c42 2022-08-19 op "<div id='tree_content'>\n"
1089 01498c42 2022-08-19 op "<div id='tree_header_wrapper'>\n"
1090 01498c42 2022-08-19 op "<div id='tree_header'>\n"
1091 01498c42 2022-08-19 op "<div id='header_tree_title'>Tree:</div>\n"
1092 01498c42 2022-08-19 op "<div id='header_tree'>%s</div>\n"
1093 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1094 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1095 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1096 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1097 01498c42 2022-08-19 op "</div>\n" /* #tree_header */
1098 01498c42 2022-08-19 op "</div>\n" /* #tree_header_wrapper */
1099 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1100 01498c42 2022-08-19 op "<div id='tree'>\n",
1101 01498c42 2022-08-19 op rc->tree_id,
1104 01498c42 2022-08-19 op if (r == -1)
1105 a596b957 2022-07-14 tracey goto done;
1107 a596b957 2022-07-14 tracey error = got_output_repo_tree(c);
1108 a596b957 2022-07-14 tracey if (error)
1109 a596b957 2022-07-14 tracey goto done;
1111 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tree */
1112 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tree_content */
1116 a596b957 2022-07-14 tracey return error;
1119 a596b957 2022-07-14 tracey static const struct got_error *
1120 a596b957 2022-07-14 tracey gotweb_render_diff(struct request *c)
1122 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1123 a596b957 2022-07-14 tracey struct transport *t = c->t;
1124 a596b957 2022-07-14 tracey struct repo_commit *rc = NULL;
1125 d927f8c8 2022-08-20 op char *age = NULL, *author = NULL, *msg = NULL;
1128 a596b957 2022-07-14 tracey error = got_get_repo_commits(c, 1);
1129 a596b957 2022-07-14 tracey if (error)
1130 a596b957 2022-07-14 tracey return error;
1132 a596b957 2022-07-14 tracey rc = TAILQ_FIRST(&t->repo_commits);
1134 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1135 a596b957 2022-07-14 tracey if (error)
1136 a596b957 2022-07-14 tracey goto done;
1137 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rc->author);
1138 a596b957 2022-07-14 tracey if (error)
1139 a596b957 2022-07-14 tracey goto done;
1140 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rc->commit_msg);
1144 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1145 01498c42 2022-08-19 op "<div id='diff_title'>Commit Diff</div>\n"
1146 01498c42 2022-08-19 op "</div>\n" /* #diff_title_wrapper */
1147 01498c42 2022-08-19 op "<div id='diff_content'>\n"
1148 01498c42 2022-08-19 op "<div id='diff_header_wrapper'>\n"
1149 01498c42 2022-08-19 op "<div id='diff_header'>\n"
1150 01498c42 2022-08-19 op "<div id='header_diff_title'>Diff:</div>\n"
1151 01498c42 2022-08-19 op "<div id='header_diff'>%s<br />%s</div>\n"
1152 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1153 01498c42 2022-08-19 op "<div class='header_commit'>%s</div>\n"
1154 01498c42 2022-08-19 op "<div id='header_tree_title'>Tree:</div>\n"
1155 01498c42 2022-08-19 op "<div id='header_tree'>%s</div>\n"
1156 01498c42 2022-08-19 op "<div class='header_author_title'>Author:</div>\n"
1157 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1158 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1159 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1160 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1161 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1162 01498c42 2022-08-19 op "</div>\n" /* #diff_header */
1163 01498c42 2022-08-19 op "</div>\n" /* #diff_header_wrapper */
1164 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1165 01498c42 2022-08-19 op "<div id='diff'>\n",
1166 01498c42 2022-08-19 op rc->parent_id, rc->commit_id,
1167 01498c42 2022-08-19 op rc->commit_id,
1168 01498c42 2022-08-19 op rc->tree_id,
1172 01498c42 2022-08-19 op if (r == -1)
1173 a596b957 2022-07-14 tracey goto done;
1175 a596b957 2022-07-14 tracey error = got_output_repo_diff(c);
1176 a596b957 2022-07-14 tracey if (error)
1177 a596b957 2022-07-14 tracey goto done;
1179 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #diff */
1180 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #diff_content */
1182 a596b957 2022-07-14 tracey free(age);
1183 a596b957 2022-07-14 tracey free(author);
1185 a596b957 2022-07-14 tracey return error;
1188 a596b957 2022-07-14 tracey static const struct got_error *
1189 a596b957 2022-07-14 tracey gotweb_render_summary(struct request *c)
1191 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1192 a596b957 2022-07-14 tracey struct transport *t = c->t;
1193 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1196 01498c42 2022-08-19 op if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1197 a596b957 2022-07-14 tracey goto done;
1199 01498c42 2022-08-19 op if (srv->show_repo_description) {
1200 01498c42 2022-08-19 op r = fcgi_printf(c,
1201 01498c42 2022-08-19 op "<div id='description_title'>Description:</div>\n"
1202 01498c42 2022-08-19 op "<div id='description'>%s</div>\n",
1203 f897bb24 2022-08-20 op t->repo_dir->description ? t->repo_dir->description : "");
1204 01498c42 2022-08-19 op if (r == -1)
1208 01498c42 2022-08-19 op if (srv->show_repo_owner) {
1209 01498c42 2022-08-19 op r = fcgi_printf(c,
1210 01498c42 2022-08-19 op "<div id='repo_owner_title'>Owner:</div>\n"
1211 01498c42 2022-08-19 op "<div id='repo_owner'>%s</div>\n",
1212 f897bb24 2022-08-20 op t->repo_dir->owner ? t->repo_dir->owner : "");
1213 01498c42 2022-08-19 op if (r == -1)
1217 01498c42 2022-08-19 op if (srv->show_repo_age) {
1218 01498c42 2022-08-19 op r = fcgi_printf(c,
1219 01498c42 2022-08-19 op "<div id='last_change_title'>Last Change:</div>\n"
1220 01498c42 2022-08-19 op "<div id='last_change'>%s</div>\n",
1221 01498c42 2022-08-19 op t->repo_dir->age);
1222 01498c42 2022-08-19 op if (r == -1)
1226 01498c42 2022-08-19 op if (srv->show_repo_cloneurl) {
1227 01498c42 2022-08-19 op r = fcgi_printf(c,
1228 01498c42 2022-08-19 op "<div id='cloneurl_title'>Clone URL:</div>\n"
1229 01498c42 2022-08-19 op "<div id='cloneurl'>%s</div>\n",
1230 01498c42 2022-08-19 op t->repo_dir->url ? t->repo_dir->url : "");
1231 01498c42 2022-08-19 op if (r == -1)
1235 01498c42 2022-08-19 op r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1236 01498c42 2022-08-19 op if (r == -1)
1237 a596b957 2022-07-14 tracey goto done;
1239 ed619ca0 2022-12-14 op if (gotweb_render_briefs(c->tp) == -1)
1240 a596b957 2022-07-14 tracey goto done;
1242 a596b957 2022-07-14 tracey error = gotweb_render_tags(c);
1243 a596b957 2022-07-14 tracey if (error) {
1244 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1245 a596b957 2022-07-14 tracey goto done;
1248 a596b957 2022-07-14 tracey error = gotweb_render_branches(c);
1249 a596b957 2022-07-14 tracey if (error)
1250 a596b957 2022-07-14 tracey log_warnx("%s: %s", __func__, error->msg);
1252 a596b957 2022-07-14 tracey return error;
1255 a596b957 2022-07-14 tracey static const struct got_error *
1256 a596b957 2022-07-14 tracey gotweb_render_tag(struct request *c)
1258 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1259 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1260 a596b957 2022-07-14 tracey struct transport *t = c->t;
1261 d927f8c8 2022-08-20 op char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1263 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, 1);
1264 a596b957 2022-07-14 tracey if (error)
1265 a596b957 2022-07-14 tracey goto done;
1267 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1268 a596b957 2022-07-14 tracey error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1269 a596b957 2022-07-14 tracey "bad commit id");
1270 a596b957 2022-07-14 tracey goto done;
1273 a596b957 2022-07-14 tracey rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1275 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1276 a596b957 2022-07-14 tracey if (error)
1277 a596b957 2022-07-14 tracey goto done;
1278 a596b957 2022-07-14 tracey error = gotweb_escape_html(&author, rt->tagger);
1279 a596b957 2022-07-14 tracey if (error)
1280 a596b957 2022-07-14 tracey goto done;
1281 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->commit_msg);
1285 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1286 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/", 5) == 0)
1287 5fba0750 2022-09-01 stsp tagname += 5;
1288 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1292 01498c42 2022-08-19 op fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1293 01498c42 2022-08-19 op "<div id='tags_title'>Tag</div>\n"
1294 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1295 01498c42 2022-08-19 op "<div id='tags_content'>\n"
1296 01498c42 2022-08-19 op "<div id='tag_header_wrapper'>\n"
1297 01498c42 2022-08-19 op "<div id='tag_header'>\n"
1298 01498c42 2022-08-19 op "<div class='header_commit_title'>Commit:</div>\n"
1299 01498c42 2022-08-19 op "<div class='header_commit'>%s"
1300 01498c42 2022-08-19 op " <span class='refs_str'>(%s)</span></div>\n"
1301 01498c42 2022-08-19 op "<div class='header_author_title'>Tagger:</div>\n"
1302 01498c42 2022-08-19 op "<div class='header_author'>%s</div>\n"
1303 01498c42 2022-08-19 op "<div class='header_age_title'>Date:</div>\n"
1304 01498c42 2022-08-19 op "<div class='header_age'>%s</div>\n"
1305 01498c42 2022-08-19 op "<div id='header_commit_msg_title'>Message:</div>\n"
1306 01498c42 2022-08-19 op "<div id='header_commit_msg'>%s</div>\n"
1307 01498c42 2022-08-19 op "</div>\n" /* #tag_header */
1308 01498c42 2022-08-19 op "<div class='dotted_line'></div>\n"
1309 01498c42 2022-08-19 op "<div id='tag_commit'>\n%s</div>"
1310 f864583e 2022-09-06 op "</div>" /* #tag_header_wrapper */
1311 f864583e 2022-09-06 op "</div>", /* #tags_content */
1312 01498c42 2022-08-19 op rt->commit_id,
1317 01498c42 2022-08-19 op rt->tag_commit);
1320 a596b957 2022-07-14 tracey free(age);
1321 a596b957 2022-07-14 tracey free(author);
1323 a596b957 2022-07-14 tracey return error;
1326 a596b957 2022-07-14 tracey static const struct got_error *
1327 a596b957 2022-07-14 tracey gotweb_render_tags(struct request *c)
1329 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1330 a596b957 2022-07-14 tracey struct repo_tag *rt = NULL;
1331 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1332 a596b957 2022-07-14 tracey struct transport *t = c->t;
1333 a596b957 2022-07-14 tracey struct querystring *qs = t->qs;
1334 a596b957 2022-07-14 tracey struct repo_dir *repo_dir = t->repo_dir;
1335 d927f8c8 2022-08-20 op char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1336 01498c42 2022-08-19 op int r, commit_found = 0;
1338 a596b957 2022-07-14 tracey if (qs->action == BRIEFS) {
1339 a596b957 2022-07-14 tracey qs->action = TAGS;
1340 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1342 a596b957 2022-07-14 tracey error = got_get_repo_tags(c, srv->max_commits_display);
1343 a596b957 2022-07-14 tracey if (error)
1344 a596b957 2022-07-14 tracey goto done;
1346 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1347 01498c42 2022-08-19 op "<div id='tags_title'>Tags</div>\n"
1348 01498c42 2022-08-19 op "</div>\n" /* #tags_title_wrapper */
1349 01498c42 2022-08-19 op "<div id='tags_content'>\n");
1350 01498c42 2022-08-19 op if (r == -1)
1351 a596b957 2022-07-14 tracey goto done;
1353 a596b957 2022-07-14 tracey if (t->tag_count == 0) {
1354 01498c42 2022-08-19 op r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1355 01498c42 2022-08-19 op "This repository contains no tags");
1356 01498c42 2022-08-19 op if (r == -1)
1357 a596b957 2022-07-14 tracey goto done;
1360 a596b957 2022-07-14 tracey TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1361 a596b957 2022-07-14 tracey if (commit_found == 0 && qs->commit != NULL) {
1362 a596b957 2022-07-14 tracey if (strcmp(qs->commit, rt->commit_id) != 0)
1363 a596b957 2022-07-14 tracey continue;
1365 a596b957 2022-07-14 tracey commit_found = 1;
1367 a596b957 2022-07-14 tracey error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1368 a596b957 2022-07-14 tracey if (error)
1369 a596b957 2022-07-14 tracey goto done;
1371 5fba0750 2022-09-01 stsp tagname = rt->tag_name;
1372 5fba0750 2022-09-01 stsp if (strncmp(tagname, "refs/tags/", 10) == 0)
1373 5fba0750 2022-09-01 stsp tagname += 10;
1374 5fba0750 2022-09-01 stsp error = gotweb_escape_html(&tagname, tagname);
1378 a596b957 2022-07-14 tracey if (rt->tag_commit != NULL) {
1379 a596b957 2022-07-14 tracey newline = strchr(rt->tag_commit, '\n');
1380 a596b957 2022-07-14 tracey if (newline)
1381 a596b957 2022-07-14 tracey *newline = '\0';
1382 d927f8c8 2022-08-20 op error = gotweb_escape_html(&msg, rt->tag_commit);
1387 8d02314f 2022-09-07 op if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1388 01498c42 2022-08-19 op "<div class='tag'>%s</div>\n"
1389 8d02314f 2022-09-07 op "<div class='tag_log'>", age, tagname) == -1)
1392 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1393 8d02314f 2022-09-07 op .action = TAG,
1394 8d02314f 2022-09-07 op .index_page = -1,
1396 8d02314f 2022-09-07 op .path = repo_dir->name,
1397 8d02314f 2022-09-07 op .commit = rt->commit_id,
1398 8d02314f 2022-09-07 op }, "%s", msg ? msg : "");
1399 8d02314f 2022-09-07 op if (r == -1)
1402 8d02314f 2022-09-07 op if (fcgi_printf(c, "</div>\n" /* .tag_log */
1403 01498c42 2022-08-19 op "<div class='navs_wrapper'>\n"
1404 8d02314f 2022-09-07 op "<div class='navs'>") == -1)
1407 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1408 8d02314f 2022-09-07 op .action = TAG,
1409 8d02314f 2022-09-07 op .index_page = -1,
1411 8d02314f 2022-09-07 op .path = repo_dir->name,
1412 8d02314f 2022-09-07 op .commit = rt->commit_id,
1414 8d02314f 2022-09-07 op if (r == -1)
1417 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1420 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1421 8d02314f 2022-09-07 op .action = BRIEFS,
1422 8d02314f 2022-09-07 op .index_page = -1,
1424 8d02314f 2022-09-07 op .path = repo_dir->name,
1425 8d02314f 2022-09-07 op .commit = rt->commit_id,
1426 8d02314f 2022-09-07 op }, "commit briefs");
1427 8d02314f 2022-09-07 op if (r == -1)
1430 8d02314f 2022-09-07 op if (fcgi_printf(c, " | ") == -1)
1433 8d02314f 2022-09-07 op r = gotweb_link(c, &(struct gotweb_url){
1434 8d02314f 2022-09-07 op .action = COMMITS,
1435 8d02314f 2022-09-07 op .index_page = -1,
1437 8d02314f 2022-09-07 op .path = repo_dir->name,
1438 8d02314f 2022-09-07 op .commit = rt->commit_id,
1439 8d02314f 2022-09-07 op }, "commits");
1440 8d02314f 2022-09-07 op if (r == -1)
1443 8d02314f 2022-09-07 op r = fcgi_printf(c,
1444 01498c42 2022-08-19 op "</div>\n" /* .navs */
1445 01498c42 2022-08-19 op "</div>\n" /* .navs_wrapper */
1446 8d02314f 2022-09-07 op "<div class='dotted_line'></div>\n");
1447 01498c42 2022-08-19 op if (r == -1)
1448 a596b957 2022-07-14 tracey goto done;
1450 a596b957 2022-07-14 tracey free(age);
1451 a596b957 2022-07-14 tracey age = NULL;
1452 d927f8c8 2022-08-20 op free(tagname);
1453 d927f8c8 2022-08-20 op tagname = NULL;
1457 a596b957 2022-07-14 tracey if (t->next_id || t->prev_id) {
1458 b4c0bd72 2022-12-17 op if (gotweb_render_navs(c->tp) == -1)
1459 a596b957 2022-07-14 tracey goto done;
1461 01498c42 2022-08-19 op fcgi_printf(c, "</div>\n"); /* #tags_content */
1463 a596b957 2022-07-14 tracey free(age);
1464 d927f8c8 2022-08-20 op free(tagname);
1466 a596b957 2022-07-14 tracey return error;
1469 a596b957 2022-07-14 tracey const struct got_error *
1470 a596b957 2022-07-14 tracey gotweb_escape_html(char **escaped_html, const char *orig_html)
1472 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1473 a596b957 2022-07-14 tracey struct escape_pair {
1475 a596b957 2022-07-14 tracey const char *s;
1476 a596b957 2022-07-14 tracey } esc[] = {
1477 a596b957 2022-07-14 tracey { '>', ">" },
1478 a596b957 2022-07-14 tracey { '<', "<" },
1479 a596b957 2022-07-14 tracey { '&', "&" },
1480 a596b957 2022-07-14 tracey { '"', """ },
1481 a596b957 2022-07-14 tracey { '\'', "'" },
1482 a596b957 2022-07-14 tracey { '\n', "<br />" },
1484 a596b957 2022-07-14 tracey size_t orig_len, len;
1485 a596b957 2022-07-14 tracey int i, j, x;
1487 a596b957 2022-07-14 tracey orig_len = strlen(orig_html);
1488 a596b957 2022-07-14 tracey len = orig_len;
1489 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1490 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1491 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1492 a596b957 2022-07-14 tracey continue;
1493 a596b957 2022-07-14 tracey len += strlen(esc[j].s) - 1 /* escaped char */;
1497 a596b957 2022-07-14 tracey *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1498 a596b957 2022-07-14 tracey if (*escaped_html == NULL)
1499 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1502 a596b957 2022-07-14 tracey for (i = 0; i < orig_len; i++) {
1503 a596b957 2022-07-14 tracey int escaped = 0;
1504 a596b957 2022-07-14 tracey for (j = 0; j < nitems(esc); j++) {
1505 a596b957 2022-07-14 tracey if (orig_html[i] != esc[j].c)
1506 a596b957 2022-07-14 tracey continue;
1508 a596b957 2022-07-14 tracey if (strlcat(*escaped_html, esc[j].s, len + 1)
1509 a596b957 2022-07-14 tracey >= len + 1) {
1510 a596b957 2022-07-14 tracey error = got_error(GOT_ERR_NO_SPACE);
1511 a596b957 2022-07-14 tracey goto done;
1513 a596b957 2022-07-14 tracey x += strlen(esc[j].s);
1514 a596b957 2022-07-14 tracey escaped = 1;
1517 a596b957 2022-07-14 tracey if (!escaped) {
1518 a596b957 2022-07-14 tracey (*escaped_html)[x] = orig_html[i];
1523 a596b957 2022-07-14 tracey if (error) {
1524 a596b957 2022-07-14 tracey free(*escaped_html);
1525 a596b957 2022-07-14 tracey *escaped_html = NULL;
1526 a596b957 2022-07-14 tracey } else {
1527 a596b957 2022-07-14 tracey (*escaped_html)[x] = '\0';
1530 a596b957 2022-07-14 tracey return error;
1533 8d02314f 2022-09-07 op static inline int
1534 8d02314f 2022-09-07 op should_urlencode(int c)
1536 8d02314f 2022-09-07 op if (c <= ' ' || c >= 127)
1539 8d02314f 2022-09-07 op switch (c) {
1540 8d02314f 2022-09-07 op /* gen-delim */
1548 8d02314f 2022-09-07 op /* sub-delims */
1566 8d02314f 2022-09-07 op static char *
1567 8d02314f 2022-09-07 op gotweb_urlencode(const char *str)
1569 8d02314f 2022-09-07 op const char *s;
1570 8d02314f 2022-09-07 op char *escaped;
1571 8d02314f 2022-09-07 op size_t i, len;
1575 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1577 8d02314f 2022-09-07 op if (should_urlencode(*s))
1581 8d02314f 2022-09-07 op escaped = calloc(1, len + 1);
1582 8d02314f 2022-09-07 op if (escaped == NULL)
1583 8d02314f 2022-09-07 op return NULL;
1586 8d02314f 2022-09-07 op for (s = str; *s; ++s) {
1587 8d02314f 2022-09-07 op if (should_urlencode(*s)) {
1588 8d02314f 2022-09-07 op a = (*s & 0xF0) >> 4;
1589 8d02314f 2022-09-07 op b = (*s & 0x0F);
1591 8d02314f 2022-09-07 op escaped[i++] = '%';
1592 8d02314f 2022-09-07 op escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1593 8d02314f 2022-09-07 op escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1595 8d02314f 2022-09-07 op escaped[i++] = *s;
1598 8d02314f 2022-09-07 op return escaped;
1601 ed619ca0 2022-12-14 op const char *
1602 ed619ca0 2022-12-14 op gotweb_action_name(int action)
1604 8d02314f 2022-09-07 op switch (action) {
1606 8d02314f 2022-09-07 op return "blame";
1608 8d02314f 2022-09-07 op return "blob";
1609 8d02314f 2022-09-07 op case BRIEFS:
1610 8d02314f 2022-09-07 op return "briefs";
1611 8d02314f 2022-09-07 op case COMMITS:
1612 8d02314f 2022-09-07 op return "commits";
1614 8d02314f 2022-09-07 op return "diff";
1616 8d02314f 2022-09-07 op return "err";
1618 8d02314f 2022-09-07 op return "index";
1619 8d02314f 2022-09-07 op case SUMMARY:
1620 8d02314f 2022-09-07 op return "summary";
1622 8d02314f 2022-09-07 op return "tag";
1624 8d02314f 2022-09-07 op return "tags";
1626 8d02314f 2022-09-07 op return "tree";
1628 8d02314f 2022-09-07 op return NULL;
1633 ed619ca0 2022-12-14 op gotweb_render_url(struct request *c, struct gotweb_url *url)
1635 8d02314f 2022-09-07 op const char *sep = "?", *action;
1639 ed619ca0 2022-12-14 op action = gotweb_action_name(url->action);
1640 8d02314f 2022-09-07 op if (action != NULL) {
1641 8d02314f 2022-09-07 op if (fcgi_printf(c, "?action=%s", action) == -1)
1646 8d02314f 2022-09-07 op if (url->commit) {
1647 8d02314f 2022-09-07 op if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1652 8d02314f 2022-09-07 op if (url->previd) {
1653 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1658 8d02314f 2022-09-07 op if (url->prevset) {
1659 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1664 8d02314f 2022-09-07 op if (url->file) {
1665 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->file);
1666 8d02314f 2022-09-07 op if (tmp == NULL)
1668 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1670 8d02314f 2022-09-07 op if (r == -1)
1675 8d02314f 2022-09-07 op if (url->folder) {
1676 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->folder);
1677 8d02314f 2022-09-07 op if (tmp == NULL)
1679 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1681 8d02314f 2022-09-07 op if (r == -1)
1686 8d02314f 2022-09-07 op if (url->headref) {
1687 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->headref);
1688 8d02314f 2022-09-07 op if (tmp == NULL)
1690 8d02314f 2022-09-07 op r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1692 8d02314f 2022-09-07 op if (r == -1)
1697 8d02314f 2022-09-07 op if (url->index_page != -1) {
1698 8d02314f 2022-09-07 op if (fcgi_printf(c, "%sindex_page=%d", sep,
1699 8d02314f 2022-09-07 op url->index_page) == -1)
1704 8d02314f 2022-09-07 op if (url->path) {
1705 8d02314f 2022-09-07 op tmp = gotweb_urlencode(url->path);
1706 8d02314f 2022-09-07 op if (tmp == NULL)
1708 8d02314f 2022-09-07 op r = fcgi_printf(c, "%spath=%s", sep, tmp);
1710 8d02314f 2022-09-07 op if (r == -1)
1715 8d02314f 2022-09-07 op if (url->page != -1) {
1716 8d02314f 2022-09-07 op if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1725 8d02314f 2022-09-07 op gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1730 8d02314f 2022-09-07 op if (fcgi_printf(c, "<a href='") == -1)
1733 ed619ca0 2022-12-14 op if (gotweb_render_url(c, url) == -1)
1736 8d02314f 2022-09-07 op if (fcgi_printf(c, "'>") == -1)
1739 8d02314f 2022-09-07 op va_start(ap, fmt);
1740 8d02314f 2022-09-07 op r = fcgi_vprintf(c, fmt, ap);
1742 8d02314f 2022-09-07 op if (r == -1)
1745 8d02314f 2022-09-07 op if (fcgi_printf(c, "</a>"))
1750 b5c757f5 2022-09-01 stsp static struct got_repository *
1751 b5c757f5 2022-09-01 stsp find_cached_repo(struct server *srv, const char *path)
1755 b5c757f5 2022-09-01 stsp for (i = 0; i < srv->ncached_repos; i++) {
1756 b5c757f5 2022-09-01 stsp if (strcmp(srv->cached_repos[i].path, path) == 0)
1757 b5c757f5 2022-09-01 stsp return srv->cached_repos[i].repo;
1760 b5c757f5 2022-09-01 stsp return NULL;
1763 a596b957 2022-07-14 tracey static const struct got_error *
1764 b5c757f5 2022-09-01 stsp cache_repo(struct got_repository **new, struct server *srv,
1765 b5c757f5 2022-09-01 stsp struct repo_dir *repo_dir, struct socket *sock)
1767 b5c757f5 2022-09-01 stsp const struct got_error *error = NULL;
1768 b5c757f5 2022-09-01 stsp struct got_repository *repo;
1769 b5c757f5 2022-09-01 stsp struct cached_repo *cr;
1770 b5c757f5 2022-09-01 stsp int evicted = 0;
1772 7e0ec052 2022-09-06 op if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1773 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos - 1];
1774 b5c757f5 2022-09-01 stsp error = got_repo_close(cr->repo);
1775 b5c757f5 2022-09-01 stsp memset(cr, 0, sizeof(*cr));
1776 b5c757f5 2022-09-01 stsp srv->ncached_repos--;
1777 b5c757f5 2022-09-01 stsp if (error)
1778 b5c757f5 2022-09-01 stsp return error;
1779 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1780 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1781 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[0];
1782 b5c757f5 2022-09-01 stsp evicted = 1;
1784 b5c757f5 2022-09-01 stsp cr = &srv->cached_repos[srv->ncached_repos];
1787 b5c757f5 2022-09-01 stsp error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1788 b5c757f5 2022-09-01 stsp if (error) {
1789 b5c757f5 2022-09-01 stsp if (evicted) {
1790 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1791 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1793 b5c757f5 2022-09-01 stsp return error;
1796 b5c757f5 2022-09-01 stsp if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1797 b5c757f5 2022-09-01 stsp >= sizeof(cr->path)) {
1798 b5c757f5 2022-09-01 stsp if (evicted) {
1799 b5c757f5 2022-09-01 stsp memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1800 b5c757f5 2022-09-01 stsp srv->ncached_repos * sizeof(srv->cached_repos[0]));
1802 b5c757f5 2022-09-01 stsp return got_error(GOT_ERR_NO_SPACE);
1805 b5c757f5 2022-09-01 stsp cr->repo = repo;
1806 b5c757f5 2022-09-01 stsp srv->ncached_repos++;
1807 b5c757f5 2022-09-01 stsp *new = repo;
1808 b5c757f5 2022-09-01 stsp return NULL;
1811 b5c757f5 2022-09-01 stsp static const struct got_error *
1812 a596b957 2022-07-14 tracey gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1814 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1815 a596b957 2022-07-14 tracey struct socket *sock = c->sock;
1816 a596b957 2022-07-14 tracey struct server *srv = c->srv;
1817 a596b957 2022-07-14 tracey struct transport *t = c->t;
1818 b5c757f5 2022-09-01 stsp struct got_repository *repo = NULL;
1819 a596b957 2022-07-14 tracey DIR *dt;
1820 a596b957 2022-07-14 tracey char *dir_test;
1822 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1823 a596b957 2022-07-14 tracey GOTWEB_GIT_DIR) == -1)
1824 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
1826 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1827 a596b957 2022-07-14 tracey if (dt == NULL) {
1828 a596b957 2022-07-14 tracey free(dir_test);
1829 a596b957 2022-07-14 tracey } else {
1830 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1831 a596b957 2022-07-14 tracey dir_test = NULL;
1835 a596b957 2022-07-14 tracey if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1836 0fad85dd 2022-09-01 op repo_dir->name) == -1)
1837 0fad85dd 2022-09-01 op return got_error_from_errno("asprintf");
1839 a596b957 2022-07-14 tracey dt = opendir(dir_test);
1840 a596b957 2022-07-14 tracey if (dt == NULL) {
1841 a596b957 2022-07-14 tracey error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1842 a596b957 2022-07-14 tracey goto err;
1844 0fad85dd 2022-09-01 op repo_dir->path = dir_test;
1845 0fad85dd 2022-09-01 op dir_test = NULL;
1849 d5996b9e 2022-10-31 landry if (srv->respect_exportok &&
1850 d5996b9e 2022-10-31 landry faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1851 d5996b9e 2022-10-31 landry error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1852 d5996b9e 2022-10-31 landry goto err;
1855 b5c757f5 2022-09-01 stsp repo = find_cached_repo(srv, repo_dir->path);
1856 b5c757f5 2022-09-01 stsp if (repo == NULL) {
1857 b5c757f5 2022-09-01 stsp error = cache_repo(&repo, srv, repo_dir, sock);
1858 b5c757f5 2022-09-01 stsp if (error)
1861 b5c757f5 2022-09-01 stsp t->repo = repo;
1862 a596b957 2022-07-14 tracey error = gotweb_get_repo_description(&repo_dir->description, srv,
1863 3b81530f 2022-11-22 op repo_dir->path, dirfd(dt));
1864 a596b957 2022-07-14 tracey if (error)
1865 a596b957 2022-07-14 tracey goto err;
1866 c127fc49 2022-11-22 op error = got_get_repo_owner(&repo_dir->owner, c);
1867 a596b957 2022-07-14 tracey if (error)
1868 a596b957 2022-07-14 tracey goto err;
1869 c127fc49 2022-11-22 op error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1870 a596b957 2022-07-14 tracey if (error)
1871 a596b957 2022-07-14 tracey goto err;
1872 3b81530f 2022-11-22 op error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1875 a596b957 2022-07-14 tracey free(dir_test);
1876 0fad85dd 2022-09-01 op if (dt != NULL && closedir(dt) == EOF && error == NULL)
1877 0fad85dd 2022-09-01 op error = got_error_from_errno("closedir");
1878 a596b957 2022-07-14 tracey return error;
1881 a596b957 2022-07-14 tracey static const struct got_error *
1882 a596b957 2022-07-14 tracey gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1884 a596b957 2022-07-14 tracey const struct got_error *error;
1886 a596b957 2022-07-14 tracey *repo_dir = calloc(1, sizeof(**repo_dir));
1887 a596b957 2022-07-14 tracey if (*repo_dir == NULL)
1888 a596b957 2022-07-14 tracey return got_error_from_errno("calloc");
1890 a596b957 2022-07-14 tracey if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1891 a596b957 2022-07-14 tracey error = got_error_from_errno("asprintf");
1892 a596b957 2022-07-14 tracey free(*repo_dir);
1893 a596b957 2022-07-14 tracey *repo_dir = NULL;
1894 a596b957 2022-07-14 tracey return error;
1896 a596b957 2022-07-14 tracey (*repo_dir)->owner = NULL;
1897 a596b957 2022-07-14 tracey (*repo_dir)->description = NULL;
1898 a596b957 2022-07-14 tracey (*repo_dir)->url = NULL;
1899 a596b957 2022-07-14 tracey (*repo_dir)->age = NULL;
1900 a596b957 2022-07-14 tracey (*repo_dir)->path = NULL;
1902 a596b957 2022-07-14 tracey return NULL;
1905 a596b957 2022-07-14 tracey static const struct got_error *
1906 3b81530f 2022-11-22 op gotweb_get_repo_description(char **description, struct server *srv,
1907 3b81530f 2022-11-22 op const char *dirpath, int dir)
1909 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1910 3b81530f 2022-11-22 op struct stat sb;
1911 3b81530f 2022-11-22 op int fd = -1;
1914 a596b957 2022-07-14 tracey *description = NULL;
1915 a596b957 2022-07-14 tracey if (srv->show_repo_description == 0)
1916 a596b957 2022-07-14 tracey return NULL;
1918 3b81530f 2022-11-22 op fd = openat(dir, "description", O_RDONLY);
1919 3b81530f 2022-11-22 op if (fd == -1) {
1920 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1921 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1922 3b81530f 2022-11-22 op dirpath, "description");
1924 a596b957 2022-07-14 tracey goto done;
1927 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1928 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1929 3b81530f 2022-11-22 op dirpath, "description");
1930 a596b957 2022-07-14 tracey goto done;
1933 3b81530f 2022-11-22 op len = sb.st_size;
1934 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXDESCRSZ - 1)
1935 270c41a2 2022-12-01 op len = GOTWEBD_MAXDESCRSZ - 1;
1937 a596b957 2022-07-14 tracey *description = calloc(len + 1, sizeof(**description));
1938 a596b957 2022-07-14 tracey if (*description == NULL) {
1939 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1940 a596b957 2022-07-14 tracey goto done;
1943 3b81530f 2022-11-22 op if (read(fd, *description, len) == -1)
1944 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1946 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1947 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1948 a596b957 2022-07-14 tracey return error;
1951 a596b957 2022-07-14 tracey static const struct got_error *
1952 3b81530f 2022-11-22 op gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1955 a596b957 2022-07-14 tracey const struct got_error *error = NULL;
1956 3b81530f 2022-11-22 op struct stat sb;
1957 3b81530f 2022-11-22 op int fd = -1;
1960 a596b957 2022-07-14 tracey *url = NULL;
1961 a596b957 2022-07-14 tracey if (srv->show_repo_cloneurl == 0)
1962 a596b957 2022-07-14 tracey return NULL;
1964 3b81530f 2022-11-22 op fd = openat(dir, "cloneurl", O_RDONLY);
1965 3b81530f 2022-11-22 op if (fd == -1) {
1966 3b81530f 2022-11-22 op if (errno != ENOENT && errno != EACCES) {
1967 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("openat %s/%s",
1968 3b81530f 2022-11-22 op dirpath, "cloneurl");
1970 a596b957 2022-07-14 tracey goto done;
1973 3b81530f 2022-11-22 op if (fstat(fd, &sb) == -1) {
1974 3b81530f 2022-11-22 op error = got_error_from_errno_fmt("fstat %s/%s",
1975 3b81530f 2022-11-22 op dirpath, "cloneurl");
1976 a596b957 2022-07-14 tracey goto done;
1979 3b81530f 2022-11-22 op len = sb.st_size;
1980 270c41a2 2022-12-01 op if (len > GOTWEBD_MAXCLONEURLSZ - 1)
1981 270c41a2 2022-12-01 op len = GOTWEBD_MAXCLONEURLSZ - 1;
1983 a596b957 2022-07-14 tracey *url = calloc(len + 1, sizeof(**url));
1984 a596b957 2022-07-14 tracey if (*url == NULL) {
1985 a596b957 2022-07-14 tracey error = got_error_from_errno("calloc");
1986 a596b957 2022-07-14 tracey goto done;
1989 3b81530f 2022-11-22 op if (read(fd, *url, len) == -1)
1990 3b81530f 2022-11-22 op error = got_error_from_errno("read");
1992 3b81530f 2022-11-22 op if (fd != -1 && close(fd) == -1 && error == NULL)
1993 3b81530f 2022-11-22 op error = got_error_from_errno("close");
1994 a596b957 2022-07-14 tracey return error;
1997 a596b957 2022-07-14 tracey const struct got_error *
1998 a596b957 2022-07-14 tracey gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2000 a596b957 2022-07-14 tracey struct tm tm;
2001 fced5a66 2022-07-20 naddy long long diff_time;
2002 a596b957 2022-07-14 tracey const char *years = "years ago", *months = "months ago";
2003 a596b957 2022-07-14 tracey const char *weeks = "weeks ago", *days = "days ago";
2004 a596b957 2022-07-14 tracey const char *hours = "hours ago", *minutes = "minutes ago";
2005 a596b957 2022-07-14 tracey const char *seconds = "seconds ago", *now = "right now";
2006 a596b957 2022-07-14 tracey char *s;
2007 a596b957 2022-07-14 tracey char datebuf[29];
2009 a596b957 2022-07-14 tracey *repo_age = NULL;
2011 a596b957 2022-07-14 tracey switch (ref_tm) {
2012 a596b957 2022-07-14 tracey case TM_DIFF:
2013 a596b957 2022-07-14 tracey diff_time = time(NULL) - committer_time;
2014 a596b957 2022-07-14 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
2015 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2016 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 365), years) == -1)
2017 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2018 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2019 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2020 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
2021 a596b957 2022-07-14 tracey months) == -1)
2022 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2023 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2024 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2025 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2026 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2027 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
2028 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2029 a596b957 2022-07-14 tracey (diff_time / 60 / 60 / 24), days) == -1)
2030 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2031 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 60 * 2) {
2032 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s",
2033 a596b957 2022-07-14 tracey (diff_time / 60 / 60), hours) == -1)
2034 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2035 a596b957 2022-07-14 tracey } else if (diff_time > 60 * 2) {
2036 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2037 a596b957 2022-07-14 tracey minutes) == -1)
2038 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2039 a596b957 2022-07-14 tracey } else if (diff_time > 2) {
2040 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%lld %s", diff_time,
2041 a596b957 2022-07-14 tracey seconds) == -1)
2042 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2043 a596b957 2022-07-14 tracey } else {
2044 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s", now) == -1)
2045 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2048 a596b957 2022-07-14 tracey case TM_LONG:
2049 a596b957 2022-07-14 tracey if (gmtime_r(&committer_time, &tm) == NULL)
2050 a596b957 2022-07-14 tracey return got_error_from_errno("gmtime_r");
2052 a596b957 2022-07-14 tracey s = asctime_r(&tm, datebuf);
2053 a596b957 2022-07-14 tracey if (s == NULL)
2054 a596b957 2022-07-14 tracey return got_error_from_errno("asctime_r");
2056 a596b957 2022-07-14 tracey if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2057 a596b957 2022-07-14 tracey return got_error_from_errno("asprintf");
2060 a596b957 2022-07-14 tracey return NULL;