Blame


1 8a35f56c 2022-07-16 thomas /*
2 8a35f56c 2022-07-16 thomas * Copyright (c) 2016, 2019, 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 8a35f56c 2022-07-16 thomas * Copyright (c) 2015 Mike Larkin <mlarkin@openbsd.org>
4 2ac684a4 2022-09-03 thomas * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
5 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
6 8a35f56c 2022-07-16 thomas * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
7 8a35f56c 2022-07-16 thomas *
8 8a35f56c 2022-07-16 thomas * Permission to use, copy, modify, and distribute this software for any
9 8a35f56c 2022-07-16 thomas * purpose with or without fee is hereby granted, provided that the above
10 8a35f56c 2022-07-16 thomas * copyright notice and this permission notice appear in all copies.
11 8a35f56c 2022-07-16 thomas *
12 8a35f56c 2022-07-16 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 8a35f56c 2022-07-16 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 8a35f56c 2022-07-16 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 8a35f56c 2022-07-16 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 8a35f56c 2022-07-16 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 8a35f56c 2022-07-16 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 8a35f56c 2022-07-16 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 8a35f56c 2022-07-16 thomas */
20 8a35f56c 2022-07-16 thomas
21 8a35f56c 2022-07-16 thomas #include <net/if.h>
22 8a35f56c 2022-07-16 thomas #include <netinet/in.h>
23 3e12c168 2022-07-16 thomas #include <sys/queue.h>
24 8a35f56c 2022-07-16 thomas #include <sys/stat.h>
25 8a35f56c 2022-07-16 thomas #include <sys/types.h>
26 8a35f56c 2022-07-16 thomas
27 2ac684a4 2022-09-03 thomas #include <ctype.h>
28 8a35f56c 2022-07-16 thomas #include <dirent.h>
29 8a35f56c 2022-07-16 thomas #include <errno.h>
30 8a35f56c 2022-07-16 thomas #include <event.h>
31 4606e6d4 2022-11-23 thomas #include <fcntl.h>
32 4606e6d4 2022-11-23 thomas #include <imsg.h>
33 8a35f56c 2022-07-16 thomas #include <stdio.h>
34 8a35f56c 2022-07-16 thomas #include <stdlib.h>
35 8a35f56c 2022-07-16 thomas #include <string.h>
36 8a35f56c 2022-07-16 thomas #include <unistd.h>
37 8a35f56c 2022-07-16 thomas
38 8a35f56c 2022-07-16 thomas #include "got_error.h"
39 8a35f56c 2022-07-16 thomas #include "got_object.h"
40 8a35f56c 2022-07-16 thomas #include "got_reference.h"
41 8a35f56c 2022-07-16 thomas #include "got_repository.h"
42 8a35f56c 2022-07-16 thomas #include "got_path.h"
43 8a35f56c 2022-07-16 thomas #include "got_cancel.h"
44 8a35f56c 2022-07-16 thomas #include "got_worktree.h"
45 8a35f56c 2022-07-16 thomas #include "got_diff.h"
46 8a35f56c 2022-07-16 thomas #include "got_commit_graph.h"
47 8a35f56c 2022-07-16 thomas #include "got_blame.h"
48 8a35f56c 2022-07-16 thomas #include "got_privsep.h"
49 cb11302c 2022-12-30 thomas
50 cb11302c 2022-12-30 thomas #include "got_compat.h"
51 8a35f56c 2022-07-16 thomas
52 8a35f56c 2022-07-16 thomas #include "proc.h"
53 8a35f56c 2022-07-16 thomas #include "gotwebd.h"
54 d6795e9f 2022-12-30 thomas #include "tmpl.h"
55 ff36aeea 2022-07-16 thomas
56 8a35f56c 2022-07-16 thomas static const struct querystring_keys querystring_keys[] = {
57 8a35f56c 2022-07-16 thomas { "action", ACTION },
58 8a35f56c 2022-07-16 thomas { "commit", COMMIT },
59 8a35f56c 2022-07-16 thomas { "file", RFILE },
60 8a35f56c 2022-07-16 thomas { "folder", FOLDER },
61 8a35f56c 2022-07-16 thomas { "headref", HEADREF },
62 8a35f56c 2022-07-16 thomas { "index_page", INDEX_PAGE },
63 8a35f56c 2022-07-16 thomas { "path", PATH },
64 8a35f56c 2022-07-16 thomas { "page", PAGE },
65 8a35f56c 2022-07-16 thomas };
66 8a35f56c 2022-07-16 thomas
67 8a35f56c 2022-07-16 thomas static const struct action_keys action_keys[] = {
68 8a35f56c 2022-07-16 thomas { "blame", BLAME },
69 8a35f56c 2022-07-16 thomas { "blob", BLOB },
70 b82440e1 2023-01-06 thomas { "blobraw", BLOBRAW },
71 8a35f56c 2022-07-16 thomas { "briefs", BRIEFS },
72 8a35f56c 2022-07-16 thomas { "commits", COMMITS },
73 8a35f56c 2022-07-16 thomas { "diff", DIFF },
74 8a35f56c 2022-07-16 thomas { "error", ERR },
75 8a35f56c 2022-07-16 thomas { "index", INDEX },
76 8a35f56c 2022-07-16 thomas { "summary", SUMMARY },
77 8a35f56c 2022-07-16 thomas { "tag", TAG },
78 8a35f56c 2022-07-16 thomas { "tags", TAGS },
79 8a35f56c 2022-07-16 thomas { "tree", TREE },
80 d6795e9f 2022-12-30 thomas { "rss", RSS },
81 8a35f56c 2022-07-16 thomas };
82 8a35f56c 2022-07-16 thomas
83 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_querystring(struct querystring **);
84 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_parse_querystring(struct querystring **,
85 8a35f56c 2022-07-16 thomas char *);
86 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_assign_querystring(struct querystring **,
87 8a35f56c 2022-07-16 thomas char *, char *);
88 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_index(struct request *);
89 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_init_repo_dir(struct repo_dir **,
90 8a35f56c 2022-07-16 thomas const char *);
91 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_load_got_path(struct request *c,
92 8a35f56c 2022-07-16 thomas struct repo_dir *);
93 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_repo_description(char **,
94 4606e6d4 2022-11-23 thomas struct server *, const char *, int);
95 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_get_clone_url(char **, struct server *,
96 4606e6d4 2022-11-23 thomas const char *, int);
97 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_blame(struct request *);
98 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_diff(struct request *);
99 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_summary(struct request *);
100 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tag(struct request *);
101 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_tags(struct request *);
102 8a35f56c 2022-07-16 thomas static const struct got_error *gotweb_render_branches(struct request *);
103 e7e5fa49 2022-12-30 thomas
104 8a35f56c 2022-07-16 thomas static void gotweb_free_querystring(struct querystring *);
105 8a35f56c 2022-07-16 thomas static void gotweb_free_repo_dir(struct repo_dir *);
106 8a35f56c 2022-07-16 thomas
107 3f0158b4 2022-08-30 thomas struct server *gotweb_get_server(uint8_t *, uint8_t *);
108 8a35f56c 2022-07-16 thomas
109 8a35f56c 2022-07-16 thomas void
110 8a35f56c 2022-07-16 thomas gotweb_process_request(struct request *c)
111 8a35f56c 2022-07-16 thomas {
112 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL, *error2 = NULL;
113 b82440e1 2023-01-06 thomas struct got_blob_object *blob = NULL;
114 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
115 8a35f56c 2022-07-16 thomas struct querystring *qs = NULL;
116 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
117 8a35f56c 2022-07-16 thomas uint8_t err[] = "gotwebd experienced an error: ";
118 b82440e1 2023-01-06 thomas int r, html = 0, fd = -1;
119 8a35f56c 2022-07-16 thomas
120 8a35f56c 2022-07-16 thomas /* init the transport */
121 8a35f56c 2022-07-16 thomas error = gotweb_init_transport(&c->t);
122 8a35f56c 2022-07-16 thomas if (error) {
123 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
124 46aeda9a 2022-08-27 thomas return;
125 8a35f56c 2022-07-16 thomas }
126 8a35f56c 2022-07-16 thomas /* don't process any further if client disconnected */
127 8a35f56c 2022-07-16 thomas if (c->sock->client_status == CLIENT_DISCONNECT)
128 8a35f56c 2022-07-16 thomas return;
129 8a35f56c 2022-07-16 thomas /* get the gotwebd server */
130 3f0158b4 2022-08-30 thomas srv = gotweb_get_server(c->server_name, c->http_host);
131 8a35f56c 2022-07-16 thomas if (srv == NULL) {
132 8a35f56c 2022-07-16 thomas log_warnx("%s: error server is NULL", __func__);
133 8a35f56c 2022-07-16 thomas goto err;
134 8a35f56c 2022-07-16 thomas }
135 8a35f56c 2022-07-16 thomas c->srv = srv;
136 8a35f56c 2022-07-16 thomas /* parse our querystring */
137 8a35f56c 2022-07-16 thomas error = gotweb_init_querystring(&qs);
138 8a35f56c 2022-07-16 thomas if (error) {
139 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
140 8a35f56c 2022-07-16 thomas goto err;
141 8a35f56c 2022-07-16 thomas }
142 8a35f56c 2022-07-16 thomas c->t->qs = qs;
143 8a35f56c 2022-07-16 thomas error = gotweb_parse_querystring(&qs, c->querystring);
144 8a35f56c 2022-07-16 thomas if (error) {
145 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
146 8a35f56c 2022-07-16 thomas goto err;
147 8a35f56c 2022-07-16 thomas }
148 8a35f56c 2022-07-16 thomas
149 8a35f56c 2022-07-16 thomas /*
150 8a35f56c 2022-07-16 thomas * certain actions require a commit id in the querystring. this stops
151 8a35f56c 2022-07-16 thomas * bad actors from exploiting this by manually manipulating the
152 8a35f56c 2022-07-16 thomas * querystring.
153 8a35f56c 2022-07-16 thomas */
154 8a35f56c 2022-07-16 thomas
155 b82440e1 2023-01-06 thomas if (qs->action == BLAME || qs->action == BLOB ||
156 b82440e1 2023-01-06 thomas qs->action == BLOBRAW || qs->action == DIFF) {
157 b82440e1 2023-01-06 thomas if (qs->commit == NULL) {
158 b82440e1 2023-01-06 thomas error2 = got_error(GOT_ERR_QUERYSTRING);
159 b82440e1 2023-01-06 thomas goto render;
160 b82440e1 2023-01-06 thomas }
161 8a35f56c 2022-07-16 thomas }
162 8a35f56c 2022-07-16 thomas
163 8a35f56c 2022-07-16 thomas if (qs->action != INDEX) {
164 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, qs->path);
165 8a35f56c 2022-07-16 thomas if (error)
166 8a35f56c 2022-07-16 thomas goto done;
167 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
168 8a35f56c 2022-07-16 thomas c->t->repo_dir = repo_dir;
169 8a35f56c 2022-07-16 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
170 8a35f56c 2022-07-16 thomas goto err;
171 8a35f56c 2022-07-16 thomas }
172 8a35f56c 2022-07-16 thomas
173 b82440e1 2023-01-06 thomas if (qs->action == BLOBRAW) {
174 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
175 8a35f56c 2022-07-16 thomas if (error)
176 8a35f56c 2022-07-16 thomas goto done;
177 8a35f56c 2022-07-16 thomas error = got_output_file_blob(c);
178 d6795e9f 2022-12-30 thomas if (error) {
179 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
180 d6795e9f 2022-12-30 thomas goto err;
181 d6795e9f 2022-12-30 thomas }
182 d6795e9f 2022-12-30 thomas goto done;
183 b82440e1 2023-01-06 thomas }
184 b82440e1 2023-01-06 thomas
185 b82440e1 2023-01-06 thomas if (qs->action == BLOB) {
186 b82440e1 2023-01-06 thomas int binary;
187 b82440e1 2023-01-06 thomas struct gotweb_url url = {
188 b82440e1 2023-01-06 thomas .index_page = -1,
189 b82440e1 2023-01-06 thomas .page = -1,
190 b82440e1 2023-01-06 thomas .action = BLOBRAW,
191 b82440e1 2023-01-06 thomas .path = qs->path,
192 b82440e1 2023-01-06 thomas .commit = qs->commit,
193 b82440e1 2023-01-06 thomas .folder = qs->folder,
194 b82440e1 2023-01-06 thomas .file = qs->file,
195 b82440e1 2023-01-06 thomas };
196 b82440e1 2023-01-06 thomas
197 b82440e1 2023-01-06 thomas error = got_get_repo_commits(c, 1);
198 b82440e1 2023-01-06 thomas if (error)
199 b82440e1 2023-01-06 thomas goto done;
200 b82440e1 2023-01-06 thomas
201 b82440e1 2023-01-06 thomas error2 = got_open_blob_for_output(&blob, &fd, &binary, c);
202 b82440e1 2023-01-06 thomas if (error2)
203 b82440e1 2023-01-06 thomas goto render;
204 b82440e1 2023-01-06 thomas if (binary) {
205 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Status: 302\r\n");
206 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "Location: ");
207 b82440e1 2023-01-06 thomas gotweb_render_url(c, &url);
208 b82440e1 2023-01-06 thomas fcgi_puts(c->tp, "\r\n\r\n");
209 b82440e1 2023-01-06 thomas goto done;
210 b82440e1 2023-01-06 thomas }
211 d6795e9f 2022-12-30 thomas }
212 d6795e9f 2022-12-30 thomas
213 d6795e9f 2022-12-30 thomas if (qs->action == RSS) {
214 d7034a4e 2023-01-06 thomas error = gotweb_render_content_type_file(c,
215 d7034a4e 2023-01-06 thomas "application/rss+xml;charset=utf-8",
216 d7034a4e 2023-01-06 thomas repo_dir->name, ".rss");
217 d6795e9f 2022-12-30 thomas if (error) {
218 d6795e9f 2022-12-30 thomas log_warnx("%s: %s", __func__, error->msg);
219 d6795e9f 2022-12-30 thomas goto err;
220 d6795e9f 2022-12-30 thomas }
221 d6795e9f 2022-12-30 thomas
222 d6795e9f 2022-12-30 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
223 8a35f56c 2022-07-16 thomas if (error) {
224 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
225 8a35f56c 2022-07-16 thomas goto err;
226 8a35f56c 2022-07-16 thomas }
227 d6795e9f 2022-12-30 thomas if (gotweb_render_rss(c->tp) == -1)
228 d6795e9f 2022-12-30 thomas goto err;
229 8a35f56c 2022-07-16 thomas goto done;
230 43a44bce 2022-12-04 thomas }
231 43a44bce 2022-12-04 thomas
232 43a44bce 2022-12-04 thomas render:
233 43a44bce 2022-12-04 thomas error = gotweb_render_content_type(c, "text/html");
234 43a44bce 2022-12-04 thomas if (error) {
235 43a44bce 2022-12-04 thomas log_warnx("%s: %s", __func__, error->msg);
236 43a44bce 2022-12-04 thomas goto err;
237 8a35f56c 2022-07-16 thomas }
238 43a44bce 2022-12-04 thomas html = 1;
239 8a35f56c 2022-07-16 thomas
240 e7e5fa49 2022-12-30 thomas if (gotweb_render_header(c->tp) == -1)
241 8a35f56c 2022-07-16 thomas goto err;
242 8a35f56c 2022-07-16 thomas
243 8a35f56c 2022-07-16 thomas if (error2) {
244 8a35f56c 2022-07-16 thomas error = error2;
245 8a35f56c 2022-07-16 thomas goto err;
246 8a35f56c 2022-07-16 thomas }
247 8a35f56c 2022-07-16 thomas
248 8a35f56c 2022-07-16 thomas switch(qs->action) {
249 8a35f56c 2022-07-16 thomas case BLAME:
250 8a35f56c 2022-07-16 thomas error = gotweb_render_blame(c);
251 8a35f56c 2022-07-16 thomas if (error) {
252 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
253 8a35f56c 2022-07-16 thomas goto err;
254 8a35f56c 2022-07-16 thomas }
255 b82440e1 2023-01-06 thomas break;
256 b82440e1 2023-01-06 thomas case BLOB:
257 b82440e1 2023-01-06 thomas if (gotweb_render_blob(c->tp, blob) == -1)
258 b82440e1 2023-01-06 thomas goto err;
259 8a35f56c 2022-07-16 thomas break;
260 8a35f56c 2022-07-16 thomas case BRIEFS:
261 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
262 8a35f56c 2022-07-16 thomas goto err;
263 8a35f56c 2022-07-16 thomas break;
264 8a35f56c 2022-07-16 thomas case COMMITS:
265 7ade8b27 2022-12-30 thomas error = got_get_repo_commits(c, srv->max_commits_display);
266 8a35f56c 2022-07-16 thomas if (error) {
267 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
268 8a35f56c 2022-07-16 thomas goto err;
269 8a35f56c 2022-07-16 thomas }
270 7ade8b27 2022-12-30 thomas if (gotweb_render_commits(c->tp) == -1)
271 7ade8b27 2022-12-30 thomas goto err;
272 8a35f56c 2022-07-16 thomas break;
273 8a35f56c 2022-07-16 thomas case DIFF:
274 8a35f56c 2022-07-16 thomas error = gotweb_render_diff(c);
275 8a35f56c 2022-07-16 thomas if (error) {
276 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
277 8a35f56c 2022-07-16 thomas goto err;
278 8a35f56c 2022-07-16 thomas }
279 8a35f56c 2022-07-16 thomas break;
280 8a35f56c 2022-07-16 thomas case INDEX:
281 8a35f56c 2022-07-16 thomas error = gotweb_render_index(c);
282 8a35f56c 2022-07-16 thomas if (error) {
283 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
284 8a35f56c 2022-07-16 thomas goto err;
285 8a35f56c 2022-07-16 thomas }
286 8a35f56c 2022-07-16 thomas break;
287 8a35f56c 2022-07-16 thomas case SUMMARY:
288 8a35f56c 2022-07-16 thomas error = gotweb_render_summary(c);
289 8a35f56c 2022-07-16 thomas if (error) {
290 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
291 8a35f56c 2022-07-16 thomas goto err;
292 8a35f56c 2022-07-16 thomas }
293 8a35f56c 2022-07-16 thomas break;
294 8a35f56c 2022-07-16 thomas case TAG:
295 8a35f56c 2022-07-16 thomas error = gotweb_render_tag(c);
296 8a35f56c 2022-07-16 thomas if (error) {
297 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
298 8a35f56c 2022-07-16 thomas goto err;
299 8a35f56c 2022-07-16 thomas }
300 8a35f56c 2022-07-16 thomas break;
301 8a35f56c 2022-07-16 thomas case TAGS:
302 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
303 8a35f56c 2022-07-16 thomas if (error) {
304 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
305 8a35f56c 2022-07-16 thomas goto err;
306 8a35f56c 2022-07-16 thomas }
307 8a35f56c 2022-07-16 thomas break;
308 8a35f56c 2022-07-16 thomas case TREE:
309 3c14c1f2 2023-01-06 thomas error = got_get_repo_commits(c, 1);
310 8a35f56c 2022-07-16 thomas if (error) {
311 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
312 8a35f56c 2022-07-16 thomas goto err;
313 8a35f56c 2022-07-16 thomas }
314 3c14c1f2 2023-01-06 thomas if (gotweb_render_tree(c->tp) == -1)
315 3c14c1f2 2023-01-06 thomas goto err;
316 8a35f56c 2022-07-16 thomas break;
317 8a35f56c 2022-07-16 thomas case ERR:
318 8a35f56c 2022-07-16 thomas default:
319 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s</div>\n",
320 79393471 2022-08-27 thomas "Erorr: Bad Querystring");
321 79393471 2022-08-27 thomas if (r == -1)
322 8a35f56c 2022-07-16 thomas goto err;
323 8a35f56c 2022-07-16 thomas break;
324 8a35f56c 2022-07-16 thomas }
325 8a35f56c 2022-07-16 thomas
326 8a35f56c 2022-07-16 thomas goto done;
327 8a35f56c 2022-07-16 thomas err:
328 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "<div id='err_content'>") == -1)
329 8a35f56c 2022-07-16 thomas return;
330 a5f25a12 2022-10-31 thomas if (fcgi_printf(c, "\n%s", err) == -1)
331 8a35f56c 2022-07-16 thomas return;
332 8a35f56c 2022-07-16 thomas if (error) {
333 79393471 2022-08-27 thomas if (fcgi_printf(c, "%s", error->msg) == -1)
334 8a35f56c 2022-07-16 thomas return;
335 8a35f56c 2022-07-16 thomas } else {
336 79393471 2022-08-27 thomas if (fcgi_printf(c, "see daemon logs for details") == -1)
337 8a35f56c 2022-07-16 thomas return;
338 8a35f56c 2022-07-16 thomas }
339 79393471 2022-08-27 thomas if (html && fcgi_printf(c, "</div>\n") == -1)
340 8a35f56c 2022-07-16 thomas return;
341 8a35f56c 2022-07-16 thomas done:
342 b82440e1 2023-01-06 thomas if (blob)
343 b82440e1 2023-01-06 thomas got_object_blob_close(blob);
344 b82440e1 2023-01-06 thomas if (fd != -1)
345 b82440e1 2023-01-06 thomas close(fd);
346 8a35f56c 2022-07-16 thomas if (html && srv != NULL)
347 e7e5fa49 2022-12-30 thomas gotweb_render_footer(c->tp);
348 8a35f56c 2022-07-16 thomas }
349 8a35f56c 2022-07-16 thomas
350 8a35f56c 2022-07-16 thomas struct server *
351 3f0158b4 2022-08-30 thomas gotweb_get_server(uint8_t *server_name, uint8_t *subdomain)
352 8a35f56c 2022-07-16 thomas {
353 8a35f56c 2022-07-16 thomas struct server *srv = NULL;
354 8a35f56c 2022-07-16 thomas
355 3f0158b4 2022-08-30 thomas /* check against the server name first */
356 8a35f56c 2022-07-16 thomas if (strlen(server_name) > 0)
357 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
358 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, server_name) == 0)
359 8a35f56c 2022-07-16 thomas goto done;
360 8a35f56c 2022-07-16 thomas
361 3f0158b4 2022-08-30 thomas /* check against subdomain second */
362 8a35f56c 2022-07-16 thomas if (strlen(subdomain) > 0)
363 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
364 8a35f56c 2022-07-16 thomas if (strcmp(srv->name, subdomain) == 0)
365 8a35f56c 2022-07-16 thomas goto done;
366 8a35f56c 2022-07-16 thomas
367 8a35f56c 2022-07-16 thomas /* if those fail, send first server */
368 90d63d47 2022-08-16 thomas TAILQ_FOREACH(srv, &gotwebd_env->servers, entry)
369 8a35f56c 2022-07-16 thomas if (srv != NULL)
370 8a35f56c 2022-07-16 thomas break;
371 8a35f56c 2022-07-16 thomas done:
372 8a35f56c 2022-07-16 thomas return srv;
373 8a35f56c 2022-07-16 thomas };
374 8a35f56c 2022-07-16 thomas
375 8a35f56c 2022-07-16 thomas const struct got_error *
376 8a35f56c 2022-07-16 thomas gotweb_init_transport(struct transport **t)
377 8a35f56c 2022-07-16 thomas {
378 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
379 8a35f56c 2022-07-16 thomas
380 8a35f56c 2022-07-16 thomas *t = calloc(1, sizeof(**t));
381 8a35f56c 2022-07-16 thomas if (*t == NULL)
382 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
383 8a35f56c 2022-07-16 thomas
384 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_commits);
385 8a35f56c 2022-07-16 thomas TAILQ_INIT(&(*t)->repo_tags);
386 8a35f56c 2022-07-16 thomas
387 8a35f56c 2022-07-16 thomas (*t)->repo = NULL;
388 8a35f56c 2022-07-16 thomas (*t)->repo_dir = NULL;
389 8a35f56c 2022-07-16 thomas (*t)->qs = NULL;
390 8a35f56c 2022-07-16 thomas (*t)->next_id = NULL;
391 8a35f56c 2022-07-16 thomas (*t)->prev_id = NULL;
392 8a35f56c 2022-07-16 thomas (*t)->next_disp = 0;
393 8a35f56c 2022-07-16 thomas (*t)->prev_disp = 0;
394 8a35f56c 2022-07-16 thomas
395 8a35f56c 2022-07-16 thomas return error;
396 8a35f56c 2022-07-16 thomas }
397 8a35f56c 2022-07-16 thomas
398 8a35f56c 2022-07-16 thomas static const struct got_error *
399 8a35f56c 2022-07-16 thomas gotweb_init_querystring(struct querystring **qs)
400 8a35f56c 2022-07-16 thomas {
401 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
402 8a35f56c 2022-07-16 thomas
403 8a35f56c 2022-07-16 thomas *qs = calloc(1, sizeof(**qs));
404 8a35f56c 2022-07-16 thomas if (*qs == NULL)
405 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: calloc", __func__);
406 8a35f56c 2022-07-16 thomas
407 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup("HEAD");
408 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
409 84630254 2022-09-02 thomas free(*qs);
410 84630254 2022-09-02 thomas *qs = NULL;
411 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
412 8a35f56c 2022-07-16 thomas }
413 84630254 2022-09-02 thomas
414 84630254 2022-09-02 thomas (*qs)->action = INDEX;
415 84630254 2022-09-02 thomas (*qs)->commit = NULL;
416 84630254 2022-09-02 thomas (*qs)->file = NULL;
417 84630254 2022-09-02 thomas (*qs)->folder = NULL;
418 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
419 8a35f56c 2022-07-16 thomas (*qs)->path = NULL;
420 8a35f56c 2022-07-16 thomas
421 8a35f56c 2022-07-16 thomas return error;
422 8a35f56c 2022-07-16 thomas }
423 8a35f56c 2022-07-16 thomas
424 8a35f56c 2022-07-16 thomas static const struct got_error *
425 8a35f56c 2022-07-16 thomas gotweb_parse_querystring(struct querystring **qs, char *qst)
426 8a35f56c 2022-07-16 thomas {
427 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
428 8a35f56c 2022-07-16 thomas char *tok1 = NULL, *tok1_pair = NULL, *tok1_end = NULL;
429 8a35f56c 2022-07-16 thomas char *tok2 = NULL, *tok2_pair = NULL, *tok2_end = NULL;
430 8a35f56c 2022-07-16 thomas
431 8a35f56c 2022-07-16 thomas if (qst == NULL)
432 8a35f56c 2022-07-16 thomas return error;
433 8a35f56c 2022-07-16 thomas
434 8a35f56c 2022-07-16 thomas tok1 = strdup(qst);
435 8a35f56c 2022-07-16 thomas if (tok1 == NULL)
436 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
437 8a35f56c 2022-07-16 thomas
438 8a35f56c 2022-07-16 thomas tok1_pair = tok1;
439 8a35f56c 2022-07-16 thomas tok1_end = tok1;
440 8a35f56c 2022-07-16 thomas
441 8a35f56c 2022-07-16 thomas while (tok1_pair != NULL) {
442 8a35f56c 2022-07-16 thomas strsep(&tok1_end, "&");
443 8a35f56c 2022-07-16 thomas
444 8a35f56c 2022-07-16 thomas tok2 = strdup(tok1_pair);
445 8a35f56c 2022-07-16 thomas if (tok2 == NULL) {
446 8a35f56c 2022-07-16 thomas free(tok1);
447 8a35f56c 2022-07-16 thomas return got_error_from_errno2("%s: strdup", __func__);
448 8a35f56c 2022-07-16 thomas }
449 8a35f56c 2022-07-16 thomas
450 8a35f56c 2022-07-16 thomas tok2_pair = tok2;
451 8a35f56c 2022-07-16 thomas tok2_end = tok2;
452 8a35f56c 2022-07-16 thomas
453 8a35f56c 2022-07-16 thomas while (tok2_pair != NULL) {
454 8a35f56c 2022-07-16 thomas strsep(&tok2_end, "=");
455 8a35f56c 2022-07-16 thomas if (tok2_end) {
456 8a35f56c 2022-07-16 thomas error = gotweb_assign_querystring(qs, tok2_pair,
457 8a35f56c 2022-07-16 thomas tok2_end);
458 8a35f56c 2022-07-16 thomas if (error)
459 8a35f56c 2022-07-16 thomas goto err;
460 8a35f56c 2022-07-16 thomas }
461 8a35f56c 2022-07-16 thomas tok2_pair = tok2_end;
462 8a35f56c 2022-07-16 thomas }
463 8a35f56c 2022-07-16 thomas free(tok2);
464 8a35f56c 2022-07-16 thomas tok1_pair = tok1_end;
465 8a35f56c 2022-07-16 thomas }
466 8a35f56c 2022-07-16 thomas free(tok1);
467 8a35f56c 2022-07-16 thomas return error;
468 8a35f56c 2022-07-16 thomas err:
469 8a35f56c 2022-07-16 thomas free(tok2);
470 8a35f56c 2022-07-16 thomas free(tok1);
471 8a35f56c 2022-07-16 thomas return error;
472 8a35f56c 2022-07-16 thomas }
473 8a35f56c 2022-07-16 thomas
474 2ac684a4 2022-09-03 thomas /*
475 2ac684a4 2022-09-03 thomas * Adapted from usr.sbin/httpd/httpd.c url_decode.
476 2ac684a4 2022-09-03 thomas */
477 8a35f56c 2022-07-16 thomas static const struct got_error *
478 2ac684a4 2022-09-03 thomas gotweb_urldecode(char *url)
479 2ac684a4 2022-09-03 thomas {
480 2ac684a4 2022-09-03 thomas char *p, *q;
481 2ac684a4 2022-09-03 thomas char hex[3];
482 2ac684a4 2022-09-03 thomas unsigned long x;
483 2ac684a4 2022-09-03 thomas
484 2ac684a4 2022-09-03 thomas hex[2] = '\0';
485 2ac684a4 2022-09-03 thomas p = q = url;
486 2ac684a4 2022-09-03 thomas
487 2ac684a4 2022-09-03 thomas while (*p != '\0') {
488 2ac684a4 2022-09-03 thomas switch (*p) {
489 2ac684a4 2022-09-03 thomas case '%':
490 2ac684a4 2022-09-03 thomas /* Encoding character is followed by two hex chars */
491 2ac684a4 2022-09-03 thomas if (!isxdigit((unsigned char)p[1]) ||
492 2ac684a4 2022-09-03 thomas !isxdigit((unsigned char)p[2]) ||
493 2ac684a4 2022-09-03 thomas (p[1] == '0' && p[2] == '0'))
494 2ac684a4 2022-09-03 thomas return got_error(GOT_ERR_BAD_QUERYSTRING);
495 2ac684a4 2022-09-03 thomas
496 2ac684a4 2022-09-03 thomas hex[0] = p[1];
497 2ac684a4 2022-09-03 thomas hex[1] = p[2];
498 2ac684a4 2022-09-03 thomas
499 2ac684a4 2022-09-03 thomas /*
500 2ac684a4 2022-09-03 thomas * We don't have to validate "hex" because it is
501 2ac684a4 2022-09-03 thomas * guaranteed to include two hex chars followed by nul.
502 2ac684a4 2022-09-03 thomas */
503 2ac684a4 2022-09-03 thomas x = strtoul(hex, NULL, 16);
504 2ac684a4 2022-09-03 thomas *q = (char)x;
505 2ac684a4 2022-09-03 thomas p += 2;
506 2ac684a4 2022-09-03 thomas break;
507 2ac684a4 2022-09-03 thomas default:
508 2ac684a4 2022-09-03 thomas *q = *p;
509 2ac684a4 2022-09-03 thomas break;
510 2ac684a4 2022-09-03 thomas }
511 2ac684a4 2022-09-03 thomas p++;
512 2ac684a4 2022-09-03 thomas q++;
513 2ac684a4 2022-09-03 thomas }
514 2ac684a4 2022-09-03 thomas *q = '\0';
515 2ac684a4 2022-09-03 thomas
516 2ac684a4 2022-09-03 thomas return NULL;
517 2ac684a4 2022-09-03 thomas }
518 2ac684a4 2022-09-03 thomas
519 2ac684a4 2022-09-03 thomas static const struct got_error *
520 8a35f56c 2022-07-16 thomas gotweb_assign_querystring(struct querystring **qs, char *key, char *value)
521 8a35f56c 2022-07-16 thomas {
522 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
523 8a35f56c 2022-07-16 thomas const char *errstr;
524 8a35f56c 2022-07-16 thomas int a_cnt, el_cnt;
525 8a35f56c 2022-07-16 thomas
526 2ac684a4 2022-09-03 thomas error = gotweb_urldecode(value);
527 2ac684a4 2022-09-03 thomas if (error)
528 2ac684a4 2022-09-03 thomas return error;
529 2ac684a4 2022-09-03 thomas
530 8a35f56c 2022-07-16 thomas for (el_cnt = 0; el_cnt < QSELEM__MAX; el_cnt++) {
531 8a35f56c 2022-07-16 thomas if (strcmp(key, querystring_keys[el_cnt].name) != 0)
532 8a35f56c 2022-07-16 thomas continue;
533 8a35f56c 2022-07-16 thomas
534 8a35f56c 2022-07-16 thomas switch (querystring_keys[el_cnt].element) {
535 8a35f56c 2022-07-16 thomas case ACTION:
536 8a35f56c 2022-07-16 thomas for (a_cnt = 0; a_cnt < ACTIONS__MAX; a_cnt++) {
537 8a35f56c 2022-07-16 thomas if (strcmp(value, action_keys[a_cnt].name) != 0)
538 8a35f56c 2022-07-16 thomas continue;
539 8a35f56c 2022-07-16 thomas else if (strcmp(value,
540 8a35f56c 2022-07-16 thomas action_keys[a_cnt].name) == 0){
541 8a35f56c 2022-07-16 thomas (*qs)->action =
542 8a35f56c 2022-07-16 thomas action_keys[a_cnt].action;
543 8a35f56c 2022-07-16 thomas goto qa_found;
544 8a35f56c 2022-07-16 thomas }
545 8a35f56c 2022-07-16 thomas }
546 8a35f56c 2022-07-16 thomas (*qs)->action = ERR;
547 8a35f56c 2022-07-16 thomas qa_found:
548 8a35f56c 2022-07-16 thomas break;
549 8a35f56c 2022-07-16 thomas case COMMIT:
550 8a35f56c 2022-07-16 thomas (*qs)->commit = strdup(value);
551 8a35f56c 2022-07-16 thomas if ((*qs)->commit == NULL) {
552 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
553 8a35f56c 2022-07-16 thomas __func__);
554 8a35f56c 2022-07-16 thomas goto done;
555 8a35f56c 2022-07-16 thomas }
556 8a35f56c 2022-07-16 thomas break;
557 8a35f56c 2022-07-16 thomas case RFILE:
558 8a35f56c 2022-07-16 thomas (*qs)->file = strdup(value);
559 8a35f56c 2022-07-16 thomas if ((*qs)->file == NULL) {
560 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
561 8a35f56c 2022-07-16 thomas __func__);
562 8a35f56c 2022-07-16 thomas goto done;
563 8a35f56c 2022-07-16 thomas }
564 8a35f56c 2022-07-16 thomas break;
565 8a35f56c 2022-07-16 thomas case FOLDER:
566 8a35f56c 2022-07-16 thomas (*qs)->folder = strdup(value);
567 8a35f56c 2022-07-16 thomas if ((*qs)->folder == NULL) {
568 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
569 8a35f56c 2022-07-16 thomas __func__);
570 8a35f56c 2022-07-16 thomas goto done;
571 8a35f56c 2022-07-16 thomas }
572 8a35f56c 2022-07-16 thomas break;
573 8a35f56c 2022-07-16 thomas case HEADREF:
574 b0764984 2022-09-02 thomas free((*qs)->headref);
575 8a35f56c 2022-07-16 thomas (*qs)->headref = strdup(value);
576 8a35f56c 2022-07-16 thomas if ((*qs)->headref == NULL) {
577 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
578 8a35f56c 2022-07-16 thomas __func__);
579 8a35f56c 2022-07-16 thomas goto done;
580 8a35f56c 2022-07-16 thomas }
581 8a35f56c 2022-07-16 thomas break;
582 8a35f56c 2022-07-16 thomas case INDEX_PAGE:
583 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
584 8a35f56c 2022-07-16 thomas break;
585 8a35f56c 2022-07-16 thomas (*qs)->index_page = strtonum(value, INT64_MIN,
586 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
587 8a35f56c 2022-07-16 thomas if (errstr) {
588 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
589 8a35f56c 2022-07-16 thomas __func__, errstr);
590 8a35f56c 2022-07-16 thomas goto done;
591 8a35f56c 2022-07-16 thomas }
592 3d6d1fb0 2022-12-30 thomas if ((*qs)->index_page < 0)
593 8a35f56c 2022-07-16 thomas (*qs)->index_page = 0;
594 8a35f56c 2022-07-16 thomas break;
595 8a35f56c 2022-07-16 thomas case PATH:
596 8a35f56c 2022-07-16 thomas (*qs)->path = strdup(value);
597 8a35f56c 2022-07-16 thomas if ((*qs)->path == NULL) {
598 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("%s: strdup",
599 8a35f56c 2022-07-16 thomas __func__);
600 8a35f56c 2022-07-16 thomas goto done;
601 8a35f56c 2022-07-16 thomas }
602 8a35f56c 2022-07-16 thomas break;
603 8a35f56c 2022-07-16 thomas case PAGE:
604 8a35f56c 2022-07-16 thomas if (strlen(value) == 0)
605 8a35f56c 2022-07-16 thomas break;
606 8a35f56c 2022-07-16 thomas (*qs)->page = strtonum(value, INT64_MIN,
607 8a35f56c 2022-07-16 thomas INT64_MAX, &errstr);
608 8a35f56c 2022-07-16 thomas if (errstr) {
609 8a35f56c 2022-07-16 thomas error = got_error_from_errno3("%s: strtonum %s",
610 8a35f56c 2022-07-16 thomas __func__, errstr);
611 8a35f56c 2022-07-16 thomas goto done;
612 8a35f56c 2022-07-16 thomas }
613 3d6d1fb0 2022-12-30 thomas if ((*qs)->page < 0)
614 8a35f56c 2022-07-16 thomas (*qs)->page = 0;
615 8a35f56c 2022-07-16 thomas break;
616 8a35f56c 2022-07-16 thomas default:
617 8a35f56c 2022-07-16 thomas break;
618 8a35f56c 2022-07-16 thomas }
619 8a35f56c 2022-07-16 thomas }
620 8a35f56c 2022-07-16 thomas done:
621 8a35f56c 2022-07-16 thomas return error;
622 8a35f56c 2022-07-16 thomas }
623 8a35f56c 2022-07-16 thomas
624 8a35f56c 2022-07-16 thomas void
625 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(struct repo_tag *rt)
626 8a35f56c 2022-07-16 thomas {
627 8a35f56c 2022-07-16 thomas if (rt != NULL) {
628 8a35f56c 2022-07-16 thomas free(rt->commit_id);
629 217813df 2022-09-02 thomas free(rt->tag_name);
630 217813df 2022-09-02 thomas free(rt->tag_commit);
631 217813df 2022-09-02 thomas free(rt->commit_msg);
632 8a35f56c 2022-07-16 thomas free(rt->tagger);
633 8a35f56c 2022-07-16 thomas }
634 8a35f56c 2022-07-16 thomas free(rt);
635 8a35f56c 2022-07-16 thomas }
636 8a35f56c 2022-07-16 thomas
637 8a35f56c 2022-07-16 thomas void
638 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(struct repo_commit *rc)
639 8a35f56c 2022-07-16 thomas {
640 8a35f56c 2022-07-16 thomas if (rc != NULL) {
641 8a35f56c 2022-07-16 thomas free(rc->path);
642 8a35f56c 2022-07-16 thomas free(rc->refs_str);
643 8a35f56c 2022-07-16 thomas free(rc->commit_id);
644 8a35f56c 2022-07-16 thomas free(rc->parent_id);
645 8a35f56c 2022-07-16 thomas free(rc->tree_id);
646 8a35f56c 2022-07-16 thomas free(rc->author);
647 8a35f56c 2022-07-16 thomas free(rc->committer);
648 8a35f56c 2022-07-16 thomas free(rc->commit_msg);
649 8a35f56c 2022-07-16 thomas }
650 8a35f56c 2022-07-16 thomas free(rc);
651 8a35f56c 2022-07-16 thomas }
652 8a35f56c 2022-07-16 thomas
653 8a35f56c 2022-07-16 thomas static void
654 8a35f56c 2022-07-16 thomas gotweb_free_querystring(struct querystring *qs)
655 8a35f56c 2022-07-16 thomas {
656 8a35f56c 2022-07-16 thomas if (qs != NULL) {
657 8a35f56c 2022-07-16 thomas free(qs->commit);
658 8a35f56c 2022-07-16 thomas free(qs->file);
659 8a35f56c 2022-07-16 thomas free(qs->folder);
660 8a35f56c 2022-07-16 thomas free(qs->headref);
661 8a35f56c 2022-07-16 thomas free(qs->path);
662 8a35f56c 2022-07-16 thomas }
663 8a35f56c 2022-07-16 thomas free(qs);
664 8a35f56c 2022-07-16 thomas }
665 8a35f56c 2022-07-16 thomas
666 8a35f56c 2022-07-16 thomas static void
667 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(struct repo_dir *repo_dir)
668 8a35f56c 2022-07-16 thomas {
669 8a35f56c 2022-07-16 thomas if (repo_dir != NULL) {
670 8a35f56c 2022-07-16 thomas free(repo_dir->name);
671 8a35f56c 2022-07-16 thomas free(repo_dir->owner);
672 8a35f56c 2022-07-16 thomas free(repo_dir->description);
673 8a35f56c 2022-07-16 thomas free(repo_dir->url);
674 8a35f56c 2022-07-16 thomas free(repo_dir->age);
675 8a35f56c 2022-07-16 thomas free(repo_dir->path);
676 8a35f56c 2022-07-16 thomas }
677 8a35f56c 2022-07-16 thomas free(repo_dir);
678 8a35f56c 2022-07-16 thomas }
679 8a35f56c 2022-07-16 thomas
680 8a35f56c 2022-07-16 thomas void
681 8a35f56c 2022-07-16 thomas gotweb_free_transport(struct transport *t)
682 8a35f56c 2022-07-16 thomas {
683 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL, *trc = NULL;
684 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL, *trt = NULL;
685 8a35f56c 2022-07-16 thomas
686 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rc, &t->repo_commits, entry, trc) {
687 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_commits, rc, entry);
688 8a35f56c 2022-07-16 thomas gotweb_free_repo_commit(rc);
689 8a35f56c 2022-07-16 thomas }
690 8a35f56c 2022-07-16 thomas TAILQ_FOREACH_SAFE(rt, &t->repo_tags, entry, trt) {
691 8a35f56c 2022-07-16 thomas TAILQ_REMOVE(&t->repo_tags, rt, entry);
692 8a35f56c 2022-07-16 thomas gotweb_free_repo_tag(rt);
693 8a35f56c 2022-07-16 thomas }
694 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(t->repo_dir);
695 8a35f56c 2022-07-16 thomas gotweb_free_querystring(t->qs);
696 6b42af1e 2022-09-02 thomas free(t->next_id);
697 6b42af1e 2022-09-02 thomas free(t->prev_id);
698 8a35f56c 2022-07-16 thomas free(t);
699 8a35f56c 2022-07-16 thomas }
700 8a35f56c 2022-07-16 thomas
701 8a35f56c 2022-07-16 thomas const struct got_error *
702 0cef9478 2023-01-06 thomas gotweb_render_content_type(struct request *c, const char *type)
703 8a35f56c 2022-07-16 thomas {
704 0b75e088 2022-08-27 thomas const char *csp = "default-src 'self'; script-src 'none'; "
705 0b75e088 2022-08-27 thomas "object-src 'none';";
706 0b75e088 2022-08-27 thomas
707 0b75e088 2022-08-27 thomas fcgi_printf(c,
708 0b75e088 2022-08-27 thomas "Content-Security-Policy: %s\r\n"
709 0b75e088 2022-08-27 thomas "Content-Type: %s\r\n\r\n",
710 0b75e088 2022-08-27 thomas csp, type);
711 79393471 2022-08-27 thomas return NULL;
712 8a35f56c 2022-07-16 thomas }
713 8a35f56c 2022-07-16 thomas
714 8a35f56c 2022-07-16 thomas const struct got_error *
715 c7c38295 2023-01-06 thomas gotweb_render_content_type_file(struct request *c, const char *type,
716 d7034a4e 2023-01-06 thomas const char *file, const char *suffix)
717 8a35f56c 2022-07-16 thomas {
718 79393471 2022-08-27 thomas fcgi_printf(c, "Content-type: %s\r\n"
719 d7034a4e 2023-01-06 thomas "Content-disposition: attachment; filename=%s%s\r\n\r\n",
720 d7034a4e 2023-01-06 thomas type, file, suffix ? suffix : "");
721 79393471 2022-08-27 thomas return NULL;
722 8a35f56c 2022-07-16 thomas }
723 8a35f56c 2022-07-16 thomas
724 2f4f0731 2022-12-30 thomas void
725 2f4f0731 2022-12-30 thomas gotweb_get_navs(struct request *c, struct gotweb_url *prev, int *have_prev,
726 2f4f0731 2022-12-30 thomas struct gotweb_url *next, int *have_next)
727 8a35f56c 2022-07-16 thomas {
728 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
729 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
730 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
731 8a35f56c 2022-07-16 thomas
732 2f4f0731 2022-12-30 thomas *have_prev = *have_next = 0;
733 8a35f56c 2022-07-16 thomas
734 8a35f56c 2022-07-16 thomas switch(qs->action) {
735 8a35f56c 2022-07-16 thomas case INDEX:
736 8a35f56c 2022-07-16 thomas if (qs->index_page > 0) {
737 2f4f0731 2022-12-30 thomas *have_prev = 1;
738 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
739 55144267 2022-09-07 thomas .action = -1,
740 55144267 2022-09-07 thomas .index_page = qs->index_page - 1,
741 55144267 2022-09-07 thomas .page = -1,
742 55144267 2022-09-07 thomas };
743 8a35f56c 2022-07-16 thomas }
744 2f4f0731 2022-12-30 thomas if (t->next_disp == srv->max_repos_display &&
745 2f4f0731 2022-12-30 thomas t->repos_total != (qs->index_page + 1) *
746 2f4f0731 2022-12-30 thomas srv->max_repos_display) {
747 2f4f0731 2022-12-30 thomas *have_next = 1;
748 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
749 2f4f0731 2022-12-30 thomas .action = -1,
750 2f4f0731 2022-12-30 thomas .index_page = qs->index_page + 1,
751 2f4f0731 2022-12-30 thomas .page = -1,
752 2f4f0731 2022-12-30 thomas };
753 2f4f0731 2022-12-30 thomas }
754 8a35f56c 2022-07-16 thomas break;
755 8a35f56c 2022-07-16 thomas case BRIEFS:
756 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
757 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
758 2f4f0731 2022-12-30 thomas *have_prev = 1;
759 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
760 55144267 2022-09-07 thomas .action = BRIEFS,
761 55144267 2022-09-07 thomas .index_page = -1,
762 55144267 2022-09-07 thomas .page = qs->page - 1,
763 55144267 2022-09-07 thomas .path = qs->path,
764 55144267 2022-09-07 thomas .commit = t->prev_id,
765 55144267 2022-09-07 thomas .headref = qs->headref,
766 55144267 2022-09-07 thomas };
767 8a35f56c 2022-07-16 thomas }
768 2f4f0731 2022-12-30 thomas if (t->next_id) {
769 2f4f0731 2022-12-30 thomas *have_next = 1;
770 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
771 2f4f0731 2022-12-30 thomas .action = BRIEFS,
772 2f4f0731 2022-12-30 thomas .index_page = -1,
773 2f4f0731 2022-12-30 thomas .page = qs->page + 1,
774 2f4f0731 2022-12-30 thomas .path = qs->path,
775 2f4f0731 2022-12-30 thomas .commit = t->next_id,
776 2f4f0731 2022-12-30 thomas .headref = qs->headref,
777 2f4f0731 2022-12-30 thomas };
778 2f4f0731 2022-12-30 thomas }
779 8a35f56c 2022-07-16 thomas break;
780 8a35f56c 2022-07-16 thomas case COMMITS:
781 8a35f56c 2022-07-16 thomas if (t->prev_id && qs->commit != NULL &&
782 8a35f56c 2022-07-16 thomas strcmp(qs->commit, t->prev_id) != 0) {
783 2f4f0731 2022-12-30 thomas *have_prev = 1;
784 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
785 8ea2e76e 2022-12-30 thomas .action = COMMITS,
786 55144267 2022-09-07 thomas .index_page = -1,
787 55144267 2022-09-07 thomas .page = qs->page - 1,
788 55144267 2022-09-07 thomas .path = qs->path,
789 55144267 2022-09-07 thomas .commit = t->prev_id,
790 55144267 2022-09-07 thomas .headref = qs->headref,
791 55144267 2022-09-07 thomas .folder = qs->folder,
792 55144267 2022-09-07 thomas .file = qs->file,
793 55144267 2022-09-07 thomas };
794 8a35f56c 2022-07-16 thomas }
795 2f4f0731 2022-12-30 thomas if (t->next_id) {
796 2f4f0731 2022-12-30 thomas *have_next = 1;
797 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
798 8ea2e76e 2022-12-30 thomas .action = COMMITS,
799 55144267 2022-09-07 thomas .index_page = -1,
800 55144267 2022-09-07 thomas .page = qs->page + 1,
801 55144267 2022-09-07 thomas .path = qs->path,
802 55144267 2022-09-07 thomas .commit = t->next_id,
803 55144267 2022-09-07 thomas .headref = qs->headref,
804 55144267 2022-09-07 thomas .folder = qs->folder,
805 55144267 2022-09-07 thomas .file = qs->file,
806 55144267 2022-09-07 thomas };
807 8a35f56c 2022-07-16 thomas }
808 8a35f56c 2022-07-16 thomas break;
809 8a35f56c 2022-07-16 thomas case TAGS:
810 2f4f0731 2022-12-30 thomas if (t->prev_id && qs->commit != NULL &&
811 2f4f0731 2022-12-30 thomas strcmp(qs->commit, t->prev_id) != 0) {
812 2f4f0731 2022-12-30 thomas *have_prev = 1;
813 2f4f0731 2022-12-30 thomas *prev = (struct gotweb_url){
814 2f4f0731 2022-12-30 thomas .action = TAGS,
815 2f4f0731 2022-12-30 thomas .index_page = -1,
816 2f4f0731 2022-12-30 thomas .page = qs->page - 1,
817 2f4f0731 2022-12-30 thomas .path = qs->path,
818 2f4f0731 2022-12-30 thomas .commit = t->prev_id,
819 2f4f0731 2022-12-30 thomas .headref = qs->headref,
820 2f4f0731 2022-12-30 thomas };
821 2f4f0731 2022-12-30 thomas }
822 8a35f56c 2022-07-16 thomas if (t->next_id) {
823 2f4f0731 2022-12-30 thomas *have_next = 1;
824 2f4f0731 2022-12-30 thomas *next = (struct gotweb_url){
825 55144267 2022-09-07 thomas .action = TAGS,
826 55144267 2022-09-07 thomas .index_page = -1,
827 55144267 2022-09-07 thomas .page = qs->page + 1,
828 55144267 2022-09-07 thomas .path = qs->path,
829 55144267 2022-09-07 thomas .commit = t->next_id,
830 55144267 2022-09-07 thomas .headref = qs->headref,
831 55144267 2022-09-07 thomas };
832 8a35f56c 2022-07-16 thomas }
833 8a35f56c 2022-07-16 thomas break;
834 8a35f56c 2022-07-16 thomas }
835 8a35f56c 2022-07-16 thomas }
836 8a35f56c 2022-07-16 thomas
837 8a35f56c 2022-07-16 thomas static const struct got_error *
838 8a35f56c 2022-07-16 thomas gotweb_render_index(struct request *c)
839 8a35f56c 2022-07-16 thomas {
840 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
841 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
842 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
843 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
844 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = NULL;
845 8a35f56c 2022-07-16 thomas DIR *d;
846 f530ab0e 2022-09-02 thomas struct dirent **sd_dent = NULL;
847 8a35f56c 2022-07-16 thomas unsigned int d_cnt, d_i, d_disp = 0;
848 24240f6a 2022-11-23 thomas unsigned int d_skipped = 0;
849 e7e5fa49 2022-12-30 thomas int type;
850 8a35f56c 2022-07-16 thomas
851 8a35f56c 2022-07-16 thomas d = opendir(srv->repos_path);
852 8a35f56c 2022-07-16 thomas if (d == NULL) {
853 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("opendir", srv->repos_path);
854 8a35f56c 2022-07-16 thomas return error;
855 8a35f56c 2022-07-16 thomas }
856 8a35f56c 2022-07-16 thomas
857 8a35f56c 2022-07-16 thomas d_cnt = scandir(srv->repos_path, &sd_dent, NULL, alphasort);
858 8a35f56c 2022-07-16 thomas if (d_cnt == -1) {
859 f530ab0e 2022-09-02 thomas sd_dent = NULL;
860 8a35f56c 2022-07-16 thomas error = got_error_from_errno2("scandir", srv->repos_path);
861 8a35f56c 2022-07-16 thomas goto done;
862 8a35f56c 2022-07-16 thomas }
863 8a35f56c 2022-07-16 thomas
864 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_table_hdr(c->tp) == -1)
865 8a35f56c 2022-07-16 thomas goto done;
866 79393471 2022-08-27 thomas
867 8a35f56c 2022-07-16 thomas for (d_i = 0; d_i < d_cnt; d_i++) {
868 57e88d7c 2022-11-23 thomas if (srv->max_repos > 0 && t->prev_disp == srv->max_repos)
869 57e88d7c 2022-11-23 thomas break;
870 8a35f56c 2022-07-16 thomas
871 8a35f56c 2022-07-16 thomas if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
872 24240f6a 2022-11-23 thomas strcmp(sd_dent[d_i]->d_name, "..") == 0) {
873 24240f6a 2022-11-23 thomas d_skipped++;
874 24240f6a 2022-11-23 thomas continue;
875 24240f6a 2022-11-23 thomas }
876 24240f6a 2022-11-23 thomas
877 24240f6a 2022-11-23 thomas error = got_path_dirent_type(&type, srv->repos_path,
878 24240f6a 2022-11-23 thomas sd_dent[d_i]);
879 24240f6a 2022-11-23 thomas if (error)
880 24240f6a 2022-11-23 thomas goto done;
881 24240f6a 2022-11-23 thomas if (type != DT_DIR) {
882 24240f6a 2022-11-23 thomas d_skipped++;
883 8a35f56c 2022-07-16 thomas continue;
884 24240f6a 2022-11-23 thomas }
885 8a35f56c 2022-07-16 thomas
886 8a35f56c 2022-07-16 thomas if (qs->index_page > 0 && (qs->index_page *
887 8a35f56c 2022-07-16 thomas srv->max_repos_display) > t->prev_disp) {
888 8a35f56c 2022-07-16 thomas t->prev_disp++;
889 8a35f56c 2022-07-16 thomas continue;
890 8a35f56c 2022-07-16 thomas }
891 8a35f56c 2022-07-16 thomas
892 8a35f56c 2022-07-16 thomas error = gotweb_init_repo_dir(&repo_dir, sd_dent[d_i]->d_name);
893 8a35f56c 2022-07-16 thomas if (error)
894 8a35f56c 2022-07-16 thomas goto done;
895 8a35f56c 2022-07-16 thomas
896 8a35f56c 2022-07-16 thomas error = gotweb_load_got_path(c, repo_dir);
897 8a35f56c 2022-07-16 thomas if (error && error->code == GOT_ERR_NOT_GIT_REPO) {
898 8a35f56c 2022-07-16 thomas error = NULL;
899 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
900 8a35f56c 2022-07-16 thomas repo_dir = NULL;
901 24240f6a 2022-11-23 thomas d_skipped++;
902 8a35f56c 2022-07-16 thomas continue;
903 8a35f56c 2022-07-16 thomas }
904 24240f6a 2022-11-23 thomas if (error && error->code != GOT_ERR_LONELY_PACKIDX)
905 24240f6a 2022-11-23 thomas goto done;
906 24240f6a 2022-11-23 thomas
907 8a35f56c 2022-07-16 thomas d_disp++;
908 8a35f56c 2022-07-16 thomas t->prev_disp++;
909 8a35f56c 2022-07-16 thomas
910 e7e5fa49 2022-12-30 thomas if (gotweb_render_repo_fragment(c->tp, repo_dir) == -1)
911 55144267 2022-09-07 thomas goto done;
912 55144267 2022-09-07 thomas
913 8a35f56c 2022-07-16 thomas gotweb_free_repo_dir(repo_dir);
914 8a35f56c 2022-07-16 thomas repo_dir = NULL;
915 8a35f56c 2022-07-16 thomas t->next_disp++;
916 8a35f56c 2022-07-16 thomas if (d_disp == srv->max_repos_display)
917 8a35f56c 2022-07-16 thomas break;
918 8a35f56c 2022-07-16 thomas }
919 24240f6a 2022-11-23 thomas t->repos_total = d_cnt - d_skipped;
920 24240f6a 2022-11-23 thomas
921 8a35f56c 2022-07-16 thomas if (srv->max_repos_display == 0)
922 79393471 2022-08-27 thomas goto done;
923 8a35f56c 2022-07-16 thomas if (srv->max_repos > 0 && srv->max_repos < srv->max_repos_display)
924 79393471 2022-08-27 thomas goto done;
925 8a35f56c 2022-07-16 thomas if (t->repos_total <= srv->max_repos ||
926 8a35f56c 2022-07-16 thomas t->repos_total <= srv->max_repos_display)
927 79393471 2022-08-27 thomas goto done;
928 8a35f56c 2022-07-16 thomas
929 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
930 8a35f56c 2022-07-16 thomas goto done;
931 8a35f56c 2022-07-16 thomas done:
932 f530ab0e 2022-09-02 thomas if (sd_dent) {
933 f530ab0e 2022-09-02 thomas for (d_i = 0; d_i < d_cnt; d_i++)
934 f530ab0e 2022-09-02 thomas free(sd_dent[d_i]);
935 f530ab0e 2022-09-02 thomas free(sd_dent);
936 f530ab0e 2022-09-02 thomas }
937 8a35f56c 2022-07-16 thomas if (d != NULL && closedir(d) == EOF && error == NULL)
938 8a35f56c 2022-07-16 thomas error = got_error_from_errno("closedir");
939 8a35f56c 2022-07-16 thomas return error;
940 8a35f56c 2022-07-16 thomas }
941 8a35f56c 2022-07-16 thomas
942 8a35f56c 2022-07-16 thomas static const struct got_error *
943 8a35f56c 2022-07-16 thomas gotweb_render_blame(struct request *c)
944 8a35f56c 2022-07-16 thomas {
945 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
946 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
947 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
948 255f4022 2022-08-27 thomas char *age = NULL, *msg = NULL;
949 79393471 2022-08-27 thomas int r;
950 8a35f56c 2022-07-16 thomas
951 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
952 8a35f56c 2022-07-16 thomas if (error)
953 8a35f56c 2022-07-16 thomas return error;
954 8a35f56c 2022-07-16 thomas
955 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
956 8a35f56c 2022-07-16 thomas
957 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
958 255f4022 2022-08-27 thomas if (error)
959 255f4022 2022-08-27 thomas goto done;
960 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
961 8a35f56c 2022-07-16 thomas if (error)
962 8a35f56c 2022-07-16 thomas goto done;
963 8a35f56c 2022-07-16 thomas
964 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='blame_title_wrapper'>\n"
965 79393471 2022-08-27 thomas "<div id='blame_title'>Blame</div>\n"
966 79393471 2022-08-27 thomas "</div>\n" /* #blame_title_wrapper */
967 79393471 2022-08-27 thomas "<div id='blame_content'>\n"
968 79393471 2022-08-27 thomas "<div id='blame_header_wrapper'>\n"
969 79393471 2022-08-27 thomas "<div id='blame_header'>\n"
970 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
971 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
972 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
973 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
974 79393471 2022-08-27 thomas "</div>\n" /* #blame_header */
975 79393471 2022-08-27 thomas "</div>\n" /* #blame_header_wrapper */
976 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
977 79393471 2022-08-27 thomas "<div id='blame'>\n",
978 2ad628aa 2022-08-31 thomas age,
979 255f4022 2022-08-27 thomas msg);
980 79393471 2022-08-27 thomas if (r == -1)
981 8a35f56c 2022-07-16 thomas goto done;
982 8a35f56c 2022-07-16 thomas
983 8a35f56c 2022-07-16 thomas error = got_output_file_blame(c);
984 8a35f56c 2022-07-16 thomas if (error)
985 8a35f56c 2022-07-16 thomas goto done;
986 8a35f56c 2022-07-16 thomas
987 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n" /* #blame */
988 79393471 2022-08-27 thomas "</div>\n"); /* #blame_content */
989 8a35f56c 2022-07-16 thomas done:
990 8a35f56c 2022-07-16 thomas free(age);
991 255f4022 2022-08-27 thomas free(msg);
992 8a35f56c 2022-07-16 thomas return error;
993 8a35f56c 2022-07-16 thomas }
994 8a35f56c 2022-07-16 thomas
995 8a35f56c 2022-07-16 thomas static const struct got_error *
996 8a35f56c 2022-07-16 thomas gotweb_render_branches(struct request *c)
997 8a35f56c 2022-07-16 thomas {
998 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
999 8a35f56c 2022-07-16 thomas struct got_reflist_head refs;
1000 8a35f56c 2022-07-16 thomas struct got_reflist_entry *re;
1001 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1002 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1003 8a35f56c 2022-07-16 thomas struct got_repository *repo = t->repo;
1004 55144267 2022-09-07 thomas char *escaped_refname = NULL;
1005 8a35f56c 2022-07-16 thomas char *age = NULL;
1006 79393471 2022-08-27 thomas int r;
1007 8a35f56c 2022-07-16 thomas
1008 8a35f56c 2022-07-16 thomas TAILQ_INIT(&refs);
1009 8a35f56c 2022-07-16 thomas
1010 8a35f56c 2022-07-16 thomas error = got_ref_list(&refs, repo, "refs/heads",
1011 8a35f56c 2022-07-16 thomas got_ref_cmp_by_name, NULL);
1012 8a35f56c 2022-07-16 thomas if (error)
1013 8a35f56c 2022-07-16 thomas goto done;
1014 8a35f56c 2022-07-16 thomas
1015 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='branches_title_wrapper'>\n"
1016 79393471 2022-08-27 thomas "<div id='branches_title'>Branches</div>\n"
1017 79393471 2022-08-27 thomas "</div>\n" /* #branches_title_wrapper */
1018 79393471 2022-08-27 thomas "<div id='branches_content'>\n");
1019 79393471 2022-08-27 thomas if (r == -1)
1020 8a35f56c 2022-07-16 thomas goto done;
1021 8a35f56c 2022-07-16 thomas
1022 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(re, &refs, entry) {
1023 255f4022 2022-08-27 thomas const char *refname = NULL;
1024 8a35f56c 2022-07-16 thomas
1025 8a35f56c 2022-07-16 thomas if (got_ref_is_symbolic(re->ref))
1026 8a35f56c 2022-07-16 thomas continue;
1027 8a35f56c 2022-07-16 thomas
1028 255f4022 2022-08-27 thomas refname = got_ref_get_name(re->ref);
1029 8a35f56c 2022-07-16 thomas if (refname == NULL) {
1030 8a35f56c 2022-07-16 thomas error = got_error_from_errno("strdup");
1031 8a35f56c 2022-07-16 thomas goto done;
1032 8a35f56c 2022-07-16 thomas }
1033 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) != 0)
1034 8a35f56c 2022-07-16 thomas continue;
1035 8a35f56c 2022-07-16 thomas
1036 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&age, c, refname, TM_DIFF);
1037 8a35f56c 2022-07-16 thomas if (error)
1038 8a35f56c 2022-07-16 thomas goto done;
1039 8a35f56c 2022-07-16 thomas
1040 8a35f56c 2022-07-16 thomas if (strncmp(refname, "refs/heads/", 11) == 0)
1041 8a35f56c 2022-07-16 thomas refname += 11;
1042 255f4022 2022-08-27 thomas error = gotweb_escape_html(&escaped_refname, refname);
1043 255f4022 2022-08-27 thomas if (error)
1044 255f4022 2022-08-27 thomas goto done;
1045 8a35f56c 2022-07-16 thomas
1046 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div class='branches_wrapper'>\n"
1047 79393471 2022-08-27 thomas "<div class='branches_age'>%s</div>\n"
1048 79393471 2022-08-27 thomas "<div class='branches_space'>&nbsp;</div>\n"
1049 55144267 2022-09-07 thomas "<div class='branch'>", age);
1050 55144267 2022-09-07 thomas if (r == -1)
1051 55144267 2022-09-07 thomas goto done;
1052 55144267 2022-09-07 thomas
1053 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1054 55144267 2022-09-07 thomas .action = SUMMARY,
1055 55144267 2022-09-07 thomas .index_page = -1,
1056 55144267 2022-09-07 thomas .page = -1,
1057 55144267 2022-09-07 thomas .path = qs->path,
1058 55144267 2022-09-07 thomas .headref = refname,
1059 55144267 2022-09-07 thomas }, "%s", escaped_refname);
1060 55144267 2022-09-07 thomas if (r == -1)
1061 55144267 2022-09-07 thomas goto done;
1062 55144267 2022-09-07 thomas
1063 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .branch */
1064 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1065 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1066 55144267 2022-09-07 thomas goto done;
1067 55144267 2022-09-07 thomas
1068 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1069 55144267 2022-09-07 thomas .action = SUMMARY,
1070 55144267 2022-09-07 thomas .index_page = -1,
1071 55144267 2022-09-07 thomas .page = -1,
1072 55144267 2022-09-07 thomas .path = qs->path,
1073 55144267 2022-09-07 thomas .headref = refname,
1074 55144267 2022-09-07 thomas }, "summary");
1075 55144267 2022-09-07 thomas if (r == -1)
1076 55144267 2022-09-07 thomas goto done;
1077 55144267 2022-09-07 thomas
1078 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1079 55144267 2022-09-07 thomas goto done;
1080 55144267 2022-09-07 thomas
1081 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1082 55144267 2022-09-07 thomas .action = BRIEFS,
1083 55144267 2022-09-07 thomas .index_page = -1,
1084 55144267 2022-09-07 thomas .page = -1,
1085 55144267 2022-09-07 thomas .path = qs->path,
1086 55144267 2022-09-07 thomas .headref = refname,
1087 55144267 2022-09-07 thomas }, "commit briefs");
1088 55144267 2022-09-07 thomas if (r == -1)
1089 55144267 2022-09-07 thomas goto done;
1090 55144267 2022-09-07 thomas
1091 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1092 55144267 2022-09-07 thomas goto done;
1093 55144267 2022-09-07 thomas
1094 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1095 55144267 2022-09-07 thomas .action = COMMITS,
1096 55144267 2022-09-07 thomas .index_page = -1,
1097 55144267 2022-09-07 thomas .page = -1,
1098 55144267 2022-09-07 thomas .path = qs->path,
1099 55144267 2022-09-07 thomas .headref = refname,
1100 55144267 2022-09-07 thomas }, "commits");
1101 55144267 2022-09-07 thomas if (r == -1)
1102 55144267 2022-09-07 thomas goto done;
1103 55144267 2022-09-07 thomas
1104 55144267 2022-09-07 thomas r = fcgi_printf(c, "</div>\n" /* .navs */
1105 55144267 2022-09-07 thomas "</div>\n" /* .navs_wrapper */
1106 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1107 55144267 2022-09-07 thomas "</div>\n"); /* .branches_wrapper */
1108 79393471 2022-08-27 thomas if (r == -1)
1109 8a35f56c 2022-07-16 thomas goto done;
1110 8a35f56c 2022-07-16 thomas
1111 8a35f56c 2022-07-16 thomas free(age);
1112 8a35f56c 2022-07-16 thomas age = NULL;
1113 55144267 2022-09-07 thomas free(escaped_refname);
1114 55144267 2022-09-07 thomas escaped_refname = NULL;
1115 8a35f56c 2022-07-16 thomas }
1116 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #branches_content */
1117 8a35f56c 2022-07-16 thomas done:
1118 f95e65a1 2022-09-02 thomas free(age);
1119 55144267 2022-09-07 thomas free(escaped_refname);
1120 f95e65a1 2022-09-02 thomas got_ref_list_free(&refs);
1121 8a35f56c 2022-07-16 thomas return error;
1122 8a35f56c 2022-07-16 thomas }
1123 8a35f56c 2022-07-16 thomas
1124 8a35f56c 2022-07-16 thomas static const struct got_error *
1125 8a35f56c 2022-07-16 thomas gotweb_render_diff(struct request *c)
1126 8a35f56c 2022-07-16 thomas {
1127 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1128 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1129 8a35f56c 2022-07-16 thomas struct repo_commit *rc = NULL;
1130 255f4022 2022-08-27 thomas char *age = NULL, *author = NULL, *msg = NULL;
1131 79393471 2022-08-27 thomas int r;
1132 8a35f56c 2022-07-16 thomas
1133 8a35f56c 2022-07-16 thomas error = got_get_repo_commits(c, 1);
1134 8a35f56c 2022-07-16 thomas if (error)
1135 8a35f56c 2022-07-16 thomas return error;
1136 8a35f56c 2022-07-16 thomas
1137 8a35f56c 2022-07-16 thomas rc = TAILQ_FIRST(&t->repo_commits);
1138 8a35f56c 2022-07-16 thomas
1139 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rc->committer_time, TM_LONG);
1140 8a35f56c 2022-07-16 thomas if (error)
1141 8a35f56c 2022-07-16 thomas goto done;
1142 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rc->author);
1143 8a35f56c 2022-07-16 thomas if (error)
1144 8a35f56c 2022-07-16 thomas goto done;
1145 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rc->commit_msg);
1146 255f4022 2022-08-27 thomas if (error)
1147 255f4022 2022-08-27 thomas goto done;
1148 8a35f56c 2022-07-16 thomas
1149 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='diff_title_wrapper'>\n"
1150 79393471 2022-08-27 thomas "<div id='diff_title'>Commit Diff</div>\n"
1151 79393471 2022-08-27 thomas "</div>\n" /* #diff_title_wrapper */
1152 79393471 2022-08-27 thomas "<div id='diff_content'>\n"
1153 79393471 2022-08-27 thomas "<div id='diff_header_wrapper'>\n"
1154 79393471 2022-08-27 thomas "<div id='diff_header'>\n"
1155 79393471 2022-08-27 thomas "<div id='header_diff_title'>Diff:</div>\n"
1156 79393471 2022-08-27 thomas "<div id='header_diff'>%s<br />%s</div>\n"
1157 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1158 79393471 2022-08-27 thomas "<div class='header_commit'>%s</div>\n"
1159 79393471 2022-08-27 thomas "<div id='header_tree_title'>Tree:</div>\n"
1160 79393471 2022-08-27 thomas "<div id='header_tree'>%s</div>\n"
1161 79393471 2022-08-27 thomas "<div class='header_author_title'>Author:</div>\n"
1162 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1163 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1164 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1165 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1166 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1167 79393471 2022-08-27 thomas "</div>\n" /* #diff_header */
1168 79393471 2022-08-27 thomas "</div>\n" /* #diff_header_wrapper */
1169 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1170 79393471 2022-08-27 thomas "<div id='diff'>\n",
1171 79393471 2022-08-27 thomas rc->parent_id, rc->commit_id,
1172 79393471 2022-08-27 thomas rc->commit_id,
1173 79393471 2022-08-27 thomas rc->tree_id,
1174 255f4022 2022-08-27 thomas author,
1175 2ad628aa 2022-08-31 thomas age,
1176 255f4022 2022-08-27 thomas msg);
1177 79393471 2022-08-27 thomas if (r == -1)
1178 8a35f56c 2022-07-16 thomas goto done;
1179 8a35f56c 2022-07-16 thomas
1180 8a35f56c 2022-07-16 thomas error = got_output_repo_diff(c);
1181 8a35f56c 2022-07-16 thomas if (error)
1182 8a35f56c 2022-07-16 thomas goto done;
1183 8a35f56c 2022-07-16 thomas
1184 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff */
1185 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #diff_content */
1186 8a35f56c 2022-07-16 thomas done:
1187 8a35f56c 2022-07-16 thomas free(age);
1188 8a35f56c 2022-07-16 thomas free(author);
1189 255f4022 2022-08-27 thomas free(msg);
1190 8a35f56c 2022-07-16 thomas return error;
1191 8a35f56c 2022-07-16 thomas }
1192 8a35f56c 2022-07-16 thomas
1193 8a35f56c 2022-07-16 thomas static const struct got_error *
1194 8a35f56c 2022-07-16 thomas gotweb_render_summary(struct request *c)
1195 8a35f56c 2022-07-16 thomas {
1196 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1197 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1198 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1199 79393471 2022-08-27 thomas int r;
1200 8a35f56c 2022-07-16 thomas
1201 79393471 2022-08-27 thomas if (fcgi_printf(c, "<div id='summary_wrapper'>\n") == -1)
1202 8a35f56c 2022-07-16 thomas goto done;
1203 8a35f56c 2022-07-16 thomas
1204 79393471 2022-08-27 thomas if (srv->show_repo_description) {
1205 79393471 2022-08-27 thomas r = fcgi_printf(c,
1206 79393471 2022-08-27 thomas "<div id='description_title'>Description:</div>\n"
1207 79393471 2022-08-27 thomas "<div id='description'>%s</div>\n",
1208 ddf2e5c2 2022-08-27 thomas t->repo_dir->description ? t->repo_dir->description : "");
1209 79393471 2022-08-27 thomas if (r == -1)
1210 79393471 2022-08-27 thomas goto done;
1211 79393471 2022-08-27 thomas }
1212 8a35f56c 2022-07-16 thomas
1213 79393471 2022-08-27 thomas if (srv->show_repo_owner) {
1214 79393471 2022-08-27 thomas r = fcgi_printf(c,
1215 79393471 2022-08-27 thomas "<div id='repo_owner_title'>Owner:</div>\n"
1216 79393471 2022-08-27 thomas "<div id='repo_owner'>%s</div>\n",
1217 ddf2e5c2 2022-08-27 thomas t->repo_dir->owner ? t->repo_dir->owner : "");
1218 79393471 2022-08-27 thomas if (r == -1)
1219 79393471 2022-08-27 thomas goto done;
1220 79393471 2022-08-27 thomas }
1221 8a35f56c 2022-07-16 thomas
1222 79393471 2022-08-27 thomas if (srv->show_repo_age) {
1223 79393471 2022-08-27 thomas r = fcgi_printf(c,
1224 79393471 2022-08-27 thomas "<div id='last_change_title'>Last Change:</div>\n"
1225 79393471 2022-08-27 thomas "<div id='last_change'>%s</div>\n",
1226 79393471 2022-08-27 thomas t->repo_dir->age);
1227 79393471 2022-08-27 thomas if (r == -1)
1228 79393471 2022-08-27 thomas goto done;
1229 79393471 2022-08-27 thomas }
1230 8a35f56c 2022-07-16 thomas
1231 79393471 2022-08-27 thomas if (srv->show_repo_cloneurl) {
1232 79393471 2022-08-27 thomas r = fcgi_printf(c,
1233 79393471 2022-08-27 thomas "<div id='cloneurl_title'>Clone URL:</div>\n"
1234 79393471 2022-08-27 thomas "<div id='cloneurl'>%s</div>\n",
1235 79393471 2022-08-27 thomas t->repo_dir->url ? t->repo_dir->url : "");
1236 79393471 2022-08-27 thomas if (r == -1)
1237 79393471 2022-08-27 thomas goto done;
1238 79393471 2022-08-27 thomas }
1239 8a35f56c 2022-07-16 thomas
1240 79393471 2022-08-27 thomas r = fcgi_printf(c, "</div>\n"); /* #summary_wrapper */
1241 79393471 2022-08-27 thomas if (r == -1)
1242 8a35f56c 2022-07-16 thomas goto done;
1243 8a35f56c 2022-07-16 thomas
1244 e7e5fa49 2022-12-30 thomas if (gotweb_render_briefs(c->tp) == -1)
1245 8a35f56c 2022-07-16 thomas goto done;
1246 8a35f56c 2022-07-16 thomas
1247 8a35f56c 2022-07-16 thomas error = gotweb_render_tags(c);
1248 8a35f56c 2022-07-16 thomas if (error) {
1249 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1250 8a35f56c 2022-07-16 thomas goto done;
1251 8a35f56c 2022-07-16 thomas }
1252 8a35f56c 2022-07-16 thomas
1253 8a35f56c 2022-07-16 thomas error = gotweb_render_branches(c);
1254 8a35f56c 2022-07-16 thomas if (error)
1255 8a35f56c 2022-07-16 thomas log_warnx("%s: %s", __func__, error->msg);
1256 8a35f56c 2022-07-16 thomas done:
1257 8a35f56c 2022-07-16 thomas return error;
1258 8a35f56c 2022-07-16 thomas }
1259 8a35f56c 2022-07-16 thomas
1260 8a35f56c 2022-07-16 thomas static const struct got_error *
1261 8a35f56c 2022-07-16 thomas gotweb_render_tag(struct request *c)
1262 8a35f56c 2022-07-16 thomas {
1263 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1264 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1265 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1266 255f4022 2022-08-27 thomas char *tagname = NULL, *age = NULL, *author = NULL, *msg = NULL;
1267 8a35f56c 2022-07-16 thomas
1268 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, 1);
1269 8a35f56c 2022-07-16 thomas if (error)
1270 8a35f56c 2022-07-16 thomas goto done;
1271 8a35f56c 2022-07-16 thomas
1272 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1273 8a35f56c 2022-07-16 thomas error = got_error_set_errno(GOT_ERR_BAD_OBJ_ID,
1274 8a35f56c 2022-07-16 thomas "bad commit id");
1275 8a35f56c 2022-07-16 thomas goto done;
1276 8a35f56c 2022-07-16 thomas }
1277 8a35f56c 2022-07-16 thomas
1278 8a35f56c 2022-07-16 thomas rt = TAILQ_LAST(&t->repo_tags, repo_tags_head);
1279 8a35f56c 2022-07-16 thomas
1280 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_LONG);
1281 8a35f56c 2022-07-16 thomas if (error)
1282 8a35f56c 2022-07-16 thomas goto done;
1283 8a35f56c 2022-07-16 thomas error = gotweb_escape_html(&author, rt->tagger);
1284 8a35f56c 2022-07-16 thomas if (error)
1285 8a35f56c 2022-07-16 thomas goto done;
1286 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->commit_msg);
1287 255f4022 2022-08-27 thomas if (error)
1288 255f4022 2022-08-27 thomas goto done;
1289 8a35f56c 2022-07-16 thomas
1290 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1291 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/", 5) == 0)
1292 c8d0196f 2022-09-02 thomas tagname += 5;
1293 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1294 255f4022 2022-08-27 thomas if (error)
1295 255f4022 2022-08-27 thomas goto done;
1296 8a35f56c 2022-07-16 thomas
1297 79393471 2022-08-27 thomas fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1298 79393471 2022-08-27 thomas "<div id='tags_title'>Tag</div>\n"
1299 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1300 79393471 2022-08-27 thomas "<div id='tags_content'>\n"
1301 79393471 2022-08-27 thomas "<div id='tag_header_wrapper'>\n"
1302 79393471 2022-08-27 thomas "<div id='tag_header'>\n"
1303 79393471 2022-08-27 thomas "<div class='header_commit_title'>Commit:</div>\n"
1304 79393471 2022-08-27 thomas "<div class='header_commit'>%s"
1305 79393471 2022-08-27 thomas " <span class='refs_str'>(%s)</span></div>\n"
1306 79393471 2022-08-27 thomas "<div class='header_author_title'>Tagger:</div>\n"
1307 79393471 2022-08-27 thomas "<div class='header_author'>%s</div>\n"
1308 79393471 2022-08-27 thomas "<div class='header_age_title'>Date:</div>\n"
1309 79393471 2022-08-27 thomas "<div class='header_age'>%s</div>\n"
1310 79393471 2022-08-27 thomas "<div id='header_commit_msg_title'>Message:</div>\n"
1311 79393471 2022-08-27 thomas "<div id='header_commit_msg'>%s</div>\n"
1312 79393471 2022-08-27 thomas "</div>\n" /* #tag_header */
1313 79393471 2022-08-27 thomas "<div class='dotted_line'></div>\n"
1314 79393471 2022-08-27 thomas "<div id='tag_commit'>\n%s</div>"
1315 80ca8b0f 2022-09-06 thomas "</div>" /* #tag_header_wrapper */
1316 80ca8b0f 2022-09-06 thomas "</div>", /* #tags_content */
1317 79393471 2022-08-27 thomas rt->commit_id,
1318 255f4022 2022-08-27 thomas tagname,
1319 255f4022 2022-08-27 thomas author,
1320 2ad628aa 2022-08-31 thomas age,
1321 255f4022 2022-08-27 thomas msg,
1322 79393471 2022-08-27 thomas rt->tag_commit);
1323 8a35f56c 2022-07-16 thomas
1324 8a35f56c 2022-07-16 thomas done:
1325 8a35f56c 2022-07-16 thomas free(age);
1326 8a35f56c 2022-07-16 thomas free(author);
1327 255f4022 2022-08-27 thomas free(msg);
1328 8a35f56c 2022-07-16 thomas return error;
1329 8a35f56c 2022-07-16 thomas }
1330 8a35f56c 2022-07-16 thomas
1331 8a35f56c 2022-07-16 thomas static const struct got_error *
1332 8a35f56c 2022-07-16 thomas gotweb_render_tags(struct request *c)
1333 8a35f56c 2022-07-16 thomas {
1334 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1335 8a35f56c 2022-07-16 thomas struct repo_tag *rt = NULL;
1336 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1337 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1338 8a35f56c 2022-07-16 thomas struct querystring *qs = t->qs;
1339 8a35f56c 2022-07-16 thomas struct repo_dir *repo_dir = t->repo_dir;
1340 255f4022 2022-08-27 thomas char *age = NULL, *tagname = NULL, *msg = NULL, *newline;
1341 79393471 2022-08-27 thomas int r, commit_found = 0;
1342 8a35f56c 2022-07-16 thomas
1343 8a35f56c 2022-07-16 thomas if (qs->action == BRIEFS) {
1344 8a35f56c 2022-07-16 thomas qs->action = TAGS;
1345 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, D_MAXSLCOMMDISP);
1346 8a35f56c 2022-07-16 thomas } else
1347 8a35f56c 2022-07-16 thomas error = got_get_repo_tags(c, srv->max_commits_display);
1348 8a35f56c 2022-07-16 thomas if (error)
1349 8a35f56c 2022-07-16 thomas goto done;
1350 8a35f56c 2022-07-16 thomas
1351 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='tags_title_wrapper'>\n"
1352 79393471 2022-08-27 thomas "<div id='tags_title'>Tags</div>\n"
1353 79393471 2022-08-27 thomas "</div>\n" /* #tags_title_wrapper */
1354 79393471 2022-08-27 thomas "<div id='tags_content'>\n");
1355 79393471 2022-08-27 thomas if (r == -1)
1356 8a35f56c 2022-07-16 thomas goto done;
1357 8a35f56c 2022-07-16 thomas
1358 8a35f56c 2022-07-16 thomas if (t->tag_count == 0) {
1359 79393471 2022-08-27 thomas r = fcgi_printf(c, "<div id='err_content'>%s\n</div>\n",
1360 79393471 2022-08-27 thomas "This repository contains no tags");
1361 79393471 2022-08-27 thomas if (r == -1)
1362 8a35f56c 2022-07-16 thomas goto done;
1363 8a35f56c 2022-07-16 thomas }
1364 8a35f56c 2022-07-16 thomas
1365 8a35f56c 2022-07-16 thomas TAILQ_FOREACH(rt, &t->repo_tags, entry) {
1366 8a35f56c 2022-07-16 thomas if (commit_found == 0 && qs->commit != NULL) {
1367 8a35f56c 2022-07-16 thomas if (strcmp(qs->commit, rt->commit_id) != 0)
1368 8a35f56c 2022-07-16 thomas continue;
1369 8a35f56c 2022-07-16 thomas else
1370 8a35f56c 2022-07-16 thomas commit_found = 1;
1371 8a35f56c 2022-07-16 thomas }
1372 8a35f56c 2022-07-16 thomas error = gotweb_get_time_str(&age, rt->tagger_time, TM_DIFF);
1373 8a35f56c 2022-07-16 thomas if (error)
1374 8a35f56c 2022-07-16 thomas goto done;
1375 8a35f56c 2022-07-16 thomas
1376 c8d0196f 2022-09-02 thomas tagname = rt->tag_name;
1377 c8d0196f 2022-09-02 thomas if (strncmp(tagname, "refs/tags/", 10) == 0)
1378 c8d0196f 2022-09-02 thomas tagname += 10;
1379 c8d0196f 2022-09-02 thomas error = gotweb_escape_html(&tagname, tagname);
1380 255f4022 2022-08-27 thomas if (error)
1381 255f4022 2022-08-27 thomas goto done;
1382 8a35f56c 2022-07-16 thomas
1383 8a35f56c 2022-07-16 thomas if (rt->tag_commit != NULL) {
1384 8a35f56c 2022-07-16 thomas newline = strchr(rt->tag_commit, '\n');
1385 8a35f56c 2022-07-16 thomas if (newline)
1386 8a35f56c 2022-07-16 thomas *newline = '\0';
1387 255f4022 2022-08-27 thomas error = gotweb_escape_html(&msg, rt->tag_commit);
1388 255f4022 2022-08-27 thomas if (error)
1389 255f4022 2022-08-27 thomas goto done;
1390 8a35f56c 2022-07-16 thomas }
1391 8a35f56c 2022-07-16 thomas
1392 55144267 2022-09-07 thomas if (fcgi_printf(c, "<div class='tag_age'>%s</div>\n"
1393 79393471 2022-08-27 thomas "<div class='tag'>%s</div>\n"
1394 55144267 2022-09-07 thomas "<div class='tag_log'>", age, tagname) == -1)
1395 55144267 2022-09-07 thomas goto done;
1396 55144267 2022-09-07 thomas
1397 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1398 55144267 2022-09-07 thomas .action = TAG,
1399 55144267 2022-09-07 thomas .index_page = -1,
1400 55144267 2022-09-07 thomas .page = -1,
1401 55144267 2022-09-07 thomas .path = repo_dir->name,
1402 55144267 2022-09-07 thomas .commit = rt->commit_id,
1403 55144267 2022-09-07 thomas }, "%s", msg ? msg : "");
1404 55144267 2022-09-07 thomas if (r == -1)
1405 55144267 2022-09-07 thomas goto done;
1406 55144267 2022-09-07 thomas
1407 55144267 2022-09-07 thomas if (fcgi_printf(c, "</div>\n" /* .tag_log */
1408 79393471 2022-08-27 thomas "<div class='navs_wrapper'>\n"
1409 55144267 2022-09-07 thomas "<div class='navs'>") == -1)
1410 55144267 2022-09-07 thomas goto done;
1411 55144267 2022-09-07 thomas
1412 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1413 55144267 2022-09-07 thomas .action = TAG,
1414 55144267 2022-09-07 thomas .index_page = -1,
1415 55144267 2022-09-07 thomas .page = -1,
1416 55144267 2022-09-07 thomas .path = repo_dir->name,
1417 55144267 2022-09-07 thomas .commit = rt->commit_id,
1418 55144267 2022-09-07 thomas }, "tag");
1419 55144267 2022-09-07 thomas if (r == -1)
1420 55144267 2022-09-07 thomas goto done;
1421 55144267 2022-09-07 thomas
1422 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1423 55144267 2022-09-07 thomas goto done;
1424 55144267 2022-09-07 thomas
1425 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1426 55144267 2022-09-07 thomas .action = BRIEFS,
1427 55144267 2022-09-07 thomas .index_page = -1,
1428 55144267 2022-09-07 thomas .page = -1,
1429 55144267 2022-09-07 thomas .path = repo_dir->name,
1430 55144267 2022-09-07 thomas .commit = rt->commit_id,
1431 55144267 2022-09-07 thomas }, "commit briefs");
1432 55144267 2022-09-07 thomas if (r == -1)
1433 55144267 2022-09-07 thomas goto done;
1434 55144267 2022-09-07 thomas
1435 55144267 2022-09-07 thomas if (fcgi_printf(c, " | ") == -1)
1436 55144267 2022-09-07 thomas goto done;
1437 55144267 2022-09-07 thomas
1438 55144267 2022-09-07 thomas r = gotweb_link(c, &(struct gotweb_url){
1439 55144267 2022-09-07 thomas .action = COMMITS,
1440 55144267 2022-09-07 thomas .index_page = -1,
1441 55144267 2022-09-07 thomas .page = -1,
1442 55144267 2022-09-07 thomas .path = repo_dir->name,
1443 55144267 2022-09-07 thomas .commit = rt->commit_id,
1444 55144267 2022-09-07 thomas }, "commits");
1445 55144267 2022-09-07 thomas if (r == -1)
1446 55144267 2022-09-07 thomas goto done;
1447 55144267 2022-09-07 thomas
1448 55144267 2022-09-07 thomas r = fcgi_printf(c,
1449 79393471 2022-08-27 thomas "</div>\n" /* .navs */
1450 79393471 2022-08-27 thomas "</div>\n" /* .navs_wrapper */
1451 55144267 2022-09-07 thomas "<div class='dotted_line'></div>\n");
1452 79393471 2022-08-27 thomas if (r == -1)
1453 8a35f56c 2022-07-16 thomas goto done;
1454 8a35f56c 2022-07-16 thomas
1455 8a35f56c 2022-07-16 thomas free(age);
1456 8a35f56c 2022-07-16 thomas age = NULL;
1457 255f4022 2022-08-27 thomas free(tagname);
1458 255f4022 2022-08-27 thomas tagname = NULL;
1459 255f4022 2022-08-27 thomas free(msg);
1460 255f4022 2022-08-27 thomas msg = NULL;
1461 8a35f56c 2022-07-16 thomas }
1462 8a35f56c 2022-07-16 thomas if (t->next_id || t->prev_id) {
1463 2f4f0731 2022-12-30 thomas if (gotweb_render_navs(c->tp) == -1)
1464 8a35f56c 2022-07-16 thomas goto done;
1465 8a35f56c 2022-07-16 thomas }
1466 79393471 2022-08-27 thomas fcgi_printf(c, "</div>\n"); /* #tags_content */
1467 8a35f56c 2022-07-16 thomas done:
1468 8a35f56c 2022-07-16 thomas free(age);
1469 255f4022 2022-08-27 thomas free(tagname);
1470 255f4022 2022-08-27 thomas free(msg);
1471 8a35f56c 2022-07-16 thomas return error;
1472 8a35f56c 2022-07-16 thomas }
1473 8a35f56c 2022-07-16 thomas
1474 8a35f56c 2022-07-16 thomas const struct got_error *
1475 8a35f56c 2022-07-16 thomas gotweb_escape_html(char **escaped_html, const char *orig_html)
1476 8a35f56c 2022-07-16 thomas {
1477 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1478 8a35f56c 2022-07-16 thomas struct escape_pair {
1479 8a35f56c 2022-07-16 thomas char c;
1480 8a35f56c 2022-07-16 thomas const char *s;
1481 8a35f56c 2022-07-16 thomas } esc[] = {
1482 8a35f56c 2022-07-16 thomas { '>', "&gt;" },
1483 8a35f56c 2022-07-16 thomas { '<', "&lt;" },
1484 8a35f56c 2022-07-16 thomas { '&', "&amp;" },
1485 8a35f56c 2022-07-16 thomas { '"', "&quot;" },
1486 8a35f56c 2022-07-16 thomas { '\'', "&apos;" },
1487 8a35f56c 2022-07-16 thomas { '\n', "<br />" },
1488 8a35f56c 2022-07-16 thomas };
1489 8a35f56c 2022-07-16 thomas size_t orig_len, len;
1490 8a35f56c 2022-07-16 thomas int i, j, x;
1491 8a35f56c 2022-07-16 thomas
1492 8a35f56c 2022-07-16 thomas orig_len = strlen(orig_html);
1493 8a35f56c 2022-07-16 thomas len = orig_len;
1494 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1495 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1496 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1497 8a35f56c 2022-07-16 thomas continue;
1498 8a35f56c 2022-07-16 thomas len += strlen(esc[j].s) - 1 /* escaped char */;
1499 8a35f56c 2022-07-16 thomas }
1500 8a35f56c 2022-07-16 thomas }
1501 8a35f56c 2022-07-16 thomas
1502 8a35f56c 2022-07-16 thomas *escaped_html = calloc(len + 1 /* NUL */, sizeof(**escaped_html));
1503 8a35f56c 2022-07-16 thomas if (*escaped_html == NULL)
1504 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1505 8a35f56c 2022-07-16 thomas
1506 8a35f56c 2022-07-16 thomas x = 0;
1507 8a35f56c 2022-07-16 thomas for (i = 0; i < orig_len; i++) {
1508 8a35f56c 2022-07-16 thomas int escaped = 0;
1509 8a35f56c 2022-07-16 thomas for (j = 0; j < nitems(esc); j++) {
1510 8a35f56c 2022-07-16 thomas if (orig_html[i] != esc[j].c)
1511 8a35f56c 2022-07-16 thomas continue;
1512 8a35f56c 2022-07-16 thomas
1513 8a35f56c 2022-07-16 thomas if (strlcat(*escaped_html, esc[j].s, len + 1)
1514 8a35f56c 2022-07-16 thomas >= len + 1) {
1515 8a35f56c 2022-07-16 thomas error = got_error(GOT_ERR_NO_SPACE);
1516 8a35f56c 2022-07-16 thomas goto done;
1517 8a35f56c 2022-07-16 thomas }
1518 8a35f56c 2022-07-16 thomas x += strlen(esc[j].s);
1519 8a35f56c 2022-07-16 thomas escaped = 1;
1520 8a35f56c 2022-07-16 thomas break;
1521 8a35f56c 2022-07-16 thomas }
1522 8a35f56c 2022-07-16 thomas if (!escaped) {
1523 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = orig_html[i];
1524 8a35f56c 2022-07-16 thomas x++;
1525 8a35f56c 2022-07-16 thomas }
1526 8a35f56c 2022-07-16 thomas }
1527 8a35f56c 2022-07-16 thomas done:
1528 8a35f56c 2022-07-16 thomas if (error) {
1529 8a35f56c 2022-07-16 thomas free(*escaped_html);
1530 8a35f56c 2022-07-16 thomas *escaped_html = NULL;
1531 8a35f56c 2022-07-16 thomas } else {
1532 8a35f56c 2022-07-16 thomas (*escaped_html)[x] = '\0';
1533 8a35f56c 2022-07-16 thomas }
1534 8a35f56c 2022-07-16 thomas
1535 8a35f56c 2022-07-16 thomas return error;
1536 8a35f56c 2022-07-16 thomas }
1537 8a35f56c 2022-07-16 thomas
1538 55144267 2022-09-07 thomas static inline int
1539 55144267 2022-09-07 thomas should_urlencode(int c)
1540 55144267 2022-09-07 thomas {
1541 55144267 2022-09-07 thomas if (c <= ' ' || c >= 127)
1542 55144267 2022-09-07 thomas return 1;
1543 55144267 2022-09-07 thomas
1544 55144267 2022-09-07 thomas switch (c) {
1545 55144267 2022-09-07 thomas /* gen-delim */
1546 55144267 2022-09-07 thomas case ':':
1547 55144267 2022-09-07 thomas case '/':
1548 55144267 2022-09-07 thomas case '?':
1549 55144267 2022-09-07 thomas case '#':
1550 55144267 2022-09-07 thomas case '[':
1551 55144267 2022-09-07 thomas case ']':
1552 55144267 2022-09-07 thomas case '@':
1553 55144267 2022-09-07 thomas /* sub-delims */
1554 55144267 2022-09-07 thomas case '!':
1555 55144267 2022-09-07 thomas case '$':
1556 55144267 2022-09-07 thomas case '&':
1557 55144267 2022-09-07 thomas case '\'':
1558 55144267 2022-09-07 thomas case '(':
1559 55144267 2022-09-07 thomas case ')':
1560 55144267 2022-09-07 thomas case '*':
1561 55144267 2022-09-07 thomas case '+':
1562 55144267 2022-09-07 thomas case ',':
1563 55144267 2022-09-07 thomas case ';':
1564 55144267 2022-09-07 thomas case '=':
1565 e1a9403a 2023-01-06 thomas /* needed because the URLs are embedded into the HTML */
1566 e1a9403a 2023-01-06 thomas case '\"':
1567 55144267 2022-09-07 thomas return 1;
1568 55144267 2022-09-07 thomas default:
1569 55144267 2022-09-07 thomas return 0;
1570 55144267 2022-09-07 thomas }
1571 55144267 2022-09-07 thomas }
1572 55144267 2022-09-07 thomas
1573 55144267 2022-09-07 thomas static char *
1574 55144267 2022-09-07 thomas gotweb_urlencode(const char *str)
1575 55144267 2022-09-07 thomas {
1576 55144267 2022-09-07 thomas const char *s;
1577 55144267 2022-09-07 thomas char *escaped;
1578 55144267 2022-09-07 thomas size_t i, len;
1579 55144267 2022-09-07 thomas int a, b;
1580 55144267 2022-09-07 thomas
1581 55144267 2022-09-07 thomas len = 0;
1582 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1583 55144267 2022-09-07 thomas len++;
1584 55144267 2022-09-07 thomas if (should_urlencode(*s))
1585 55144267 2022-09-07 thomas len += 2;
1586 55144267 2022-09-07 thomas }
1587 55144267 2022-09-07 thomas
1588 55144267 2022-09-07 thomas escaped = calloc(1, len + 1);
1589 55144267 2022-09-07 thomas if (escaped == NULL)
1590 55144267 2022-09-07 thomas return NULL;
1591 55144267 2022-09-07 thomas
1592 55144267 2022-09-07 thomas i = 0;
1593 55144267 2022-09-07 thomas for (s = str; *s; ++s) {
1594 55144267 2022-09-07 thomas if (should_urlencode(*s)) {
1595 55144267 2022-09-07 thomas a = (*s & 0xF0) >> 4;
1596 55144267 2022-09-07 thomas b = (*s & 0x0F);
1597 55144267 2022-09-07 thomas
1598 55144267 2022-09-07 thomas escaped[i++] = '%';
1599 55144267 2022-09-07 thomas escaped[i++] = a <= 9 ? ('0' + a) : ('7' + a);
1600 55144267 2022-09-07 thomas escaped[i++] = b <= 9 ? ('0' + b) : ('7' + b);
1601 55144267 2022-09-07 thomas } else
1602 55144267 2022-09-07 thomas escaped[i++] = *s;
1603 55144267 2022-09-07 thomas }
1604 55144267 2022-09-07 thomas
1605 55144267 2022-09-07 thomas return escaped;
1606 55144267 2022-09-07 thomas }
1607 55144267 2022-09-07 thomas
1608 e7e5fa49 2022-12-30 thomas const char *
1609 e7e5fa49 2022-12-30 thomas gotweb_action_name(int action)
1610 55144267 2022-09-07 thomas {
1611 55144267 2022-09-07 thomas switch (action) {
1612 55144267 2022-09-07 thomas case BLAME:
1613 55144267 2022-09-07 thomas return "blame";
1614 55144267 2022-09-07 thomas case BLOB:
1615 55144267 2022-09-07 thomas return "blob";
1616 b82440e1 2023-01-06 thomas case BLOBRAW:
1617 b82440e1 2023-01-06 thomas return "blobraw";
1618 55144267 2022-09-07 thomas case BRIEFS:
1619 55144267 2022-09-07 thomas return "briefs";
1620 55144267 2022-09-07 thomas case COMMITS:
1621 55144267 2022-09-07 thomas return "commits";
1622 55144267 2022-09-07 thomas case DIFF:
1623 55144267 2022-09-07 thomas return "diff";
1624 55144267 2022-09-07 thomas case ERR:
1625 55144267 2022-09-07 thomas return "err";
1626 55144267 2022-09-07 thomas case INDEX:
1627 55144267 2022-09-07 thomas return "index";
1628 55144267 2022-09-07 thomas case SUMMARY:
1629 55144267 2022-09-07 thomas return "summary";
1630 55144267 2022-09-07 thomas case TAG:
1631 55144267 2022-09-07 thomas return "tag";
1632 55144267 2022-09-07 thomas case TAGS:
1633 55144267 2022-09-07 thomas return "tags";
1634 55144267 2022-09-07 thomas case TREE:
1635 55144267 2022-09-07 thomas return "tree";
1636 d6795e9f 2022-12-30 thomas case RSS:
1637 d6795e9f 2022-12-30 thomas return "rss";
1638 55144267 2022-09-07 thomas default:
1639 55144267 2022-09-07 thomas return NULL;
1640 55144267 2022-09-07 thomas }
1641 55144267 2022-09-07 thomas }
1642 55144267 2022-09-07 thomas
1643 e7e5fa49 2022-12-30 thomas int
1644 e7e5fa49 2022-12-30 thomas gotweb_render_url(struct request *c, struct gotweb_url *url)
1645 55144267 2022-09-07 thomas {
1646 55144267 2022-09-07 thomas const char *sep = "?", *action;
1647 55144267 2022-09-07 thomas char *tmp;
1648 55144267 2022-09-07 thomas int r;
1649 55144267 2022-09-07 thomas
1650 e7e5fa49 2022-12-30 thomas action = gotweb_action_name(url->action);
1651 55144267 2022-09-07 thomas if (action != NULL) {
1652 55144267 2022-09-07 thomas if (fcgi_printf(c, "?action=%s", action) == -1)
1653 55144267 2022-09-07 thomas return -1;
1654 55144267 2022-09-07 thomas sep = "&";
1655 55144267 2022-09-07 thomas }
1656 55144267 2022-09-07 thomas
1657 55144267 2022-09-07 thomas if (url->commit) {
1658 55144267 2022-09-07 thomas if (fcgi_printf(c, "%scommit=%s", sep, url->commit) == -1)
1659 55144267 2022-09-07 thomas return -1;
1660 55144267 2022-09-07 thomas sep = "&";
1661 55144267 2022-09-07 thomas }
1662 55144267 2022-09-07 thomas
1663 55144267 2022-09-07 thomas if (url->previd) {
1664 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevid=%s", sep, url->previd) == -1)
1665 55144267 2022-09-07 thomas return -1;
1666 55144267 2022-09-07 thomas sep = "&";
1667 55144267 2022-09-07 thomas }
1668 55144267 2022-09-07 thomas
1669 55144267 2022-09-07 thomas if (url->prevset) {
1670 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sprevset=%s", sep, url->prevset) == -1)
1671 55144267 2022-09-07 thomas return -1;
1672 55144267 2022-09-07 thomas sep = "&";
1673 55144267 2022-09-07 thomas }
1674 55144267 2022-09-07 thomas
1675 55144267 2022-09-07 thomas if (url->file) {
1676 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->file);
1677 55144267 2022-09-07 thomas if (tmp == NULL)
1678 55144267 2022-09-07 thomas return -1;
1679 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfile=%s", sep, tmp);
1680 55144267 2022-09-07 thomas free(tmp);
1681 55144267 2022-09-07 thomas if (r == -1)
1682 55144267 2022-09-07 thomas return -1;
1683 55144267 2022-09-07 thomas sep = "&";
1684 55144267 2022-09-07 thomas }
1685 55144267 2022-09-07 thomas
1686 55144267 2022-09-07 thomas if (url->folder) {
1687 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->folder);
1688 55144267 2022-09-07 thomas if (tmp == NULL)
1689 55144267 2022-09-07 thomas return -1;
1690 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sfolder=%s", sep, tmp);
1691 55144267 2022-09-07 thomas free(tmp);
1692 55144267 2022-09-07 thomas if (r == -1)
1693 55144267 2022-09-07 thomas return -1;
1694 55144267 2022-09-07 thomas sep = "&";
1695 55144267 2022-09-07 thomas }
1696 55144267 2022-09-07 thomas
1697 55144267 2022-09-07 thomas if (url->headref) {
1698 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->headref);
1699 55144267 2022-09-07 thomas if (tmp == NULL)
1700 55144267 2022-09-07 thomas return -1;
1701 55144267 2022-09-07 thomas r = fcgi_printf(c, "%sheadref=%s", sep, url->headref);
1702 55144267 2022-09-07 thomas free(tmp);
1703 55144267 2022-09-07 thomas if (r == -1)
1704 55144267 2022-09-07 thomas return -1;
1705 55144267 2022-09-07 thomas sep = "&";
1706 55144267 2022-09-07 thomas }
1707 55144267 2022-09-07 thomas
1708 55144267 2022-09-07 thomas if (url->index_page != -1) {
1709 55144267 2022-09-07 thomas if (fcgi_printf(c, "%sindex_page=%d", sep,
1710 55144267 2022-09-07 thomas url->index_page) == -1)
1711 55144267 2022-09-07 thomas return -1;
1712 55144267 2022-09-07 thomas sep = "&";
1713 55144267 2022-09-07 thomas }
1714 55144267 2022-09-07 thomas
1715 55144267 2022-09-07 thomas if (url->path) {
1716 55144267 2022-09-07 thomas tmp = gotweb_urlencode(url->path);
1717 55144267 2022-09-07 thomas if (tmp == NULL)
1718 55144267 2022-09-07 thomas return -1;
1719 55144267 2022-09-07 thomas r = fcgi_printf(c, "%spath=%s", sep, tmp);
1720 55144267 2022-09-07 thomas free(tmp);
1721 55144267 2022-09-07 thomas if (r == -1)
1722 55144267 2022-09-07 thomas return -1;
1723 55144267 2022-09-07 thomas sep = "&";
1724 55144267 2022-09-07 thomas }
1725 55144267 2022-09-07 thomas
1726 55144267 2022-09-07 thomas if (url->page != -1) {
1727 55144267 2022-09-07 thomas if (fcgi_printf(c, "%spage=%d", sep, url->page) == -1)
1728 55144267 2022-09-07 thomas return -1;
1729 55144267 2022-09-07 thomas sep = "&";
1730 55144267 2022-09-07 thomas }
1731 55144267 2022-09-07 thomas
1732 55144267 2022-09-07 thomas return 0;
1733 55144267 2022-09-07 thomas }
1734 55144267 2022-09-07 thomas
1735 55144267 2022-09-07 thomas int
1736 d6795e9f 2022-12-30 thomas gotweb_render_absolute_url(struct request *c, struct gotweb_url *url)
1737 d6795e9f 2022-12-30 thomas {
1738 d6795e9f 2022-12-30 thomas struct template *tp = c->tp;
1739 d6795e9f 2022-12-30 thomas const char *proto = c->https ? "https" : "http";
1740 d6795e9f 2022-12-30 thomas
1741 d6795e9f 2022-12-30 thomas if (fcgi_puts(tp, proto) == -1 ||
1742 d6795e9f 2022-12-30 thomas fcgi_puts(tp, "://") == -1 ||
1743 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->server_name) == -1 ||
1744 d6795e9f 2022-12-30 thomas tp_htmlescape(tp, c->document_uri) == -1)
1745 d6795e9f 2022-12-30 thomas return -1;
1746 d6795e9f 2022-12-30 thomas
1747 d6795e9f 2022-12-30 thomas return gotweb_render_url(c, url);
1748 d6795e9f 2022-12-30 thomas }
1749 d6795e9f 2022-12-30 thomas
1750 d6795e9f 2022-12-30 thomas int
1751 55144267 2022-09-07 thomas gotweb_link(struct request *c, struct gotweb_url *url, const char *fmt, ...)
1752 55144267 2022-09-07 thomas {
1753 55144267 2022-09-07 thomas va_list ap;
1754 55144267 2022-09-07 thomas int r;
1755 55144267 2022-09-07 thomas
1756 55144267 2022-09-07 thomas if (fcgi_printf(c, "<a href='") == -1)
1757 55144267 2022-09-07 thomas return -1;
1758 55144267 2022-09-07 thomas
1759 e7e5fa49 2022-12-30 thomas if (gotweb_render_url(c, url) == -1)
1760 55144267 2022-09-07 thomas return -1;
1761 55144267 2022-09-07 thomas
1762 55144267 2022-09-07 thomas if (fcgi_printf(c, "'>") == -1)
1763 55144267 2022-09-07 thomas return -1;
1764 55144267 2022-09-07 thomas
1765 55144267 2022-09-07 thomas va_start(ap, fmt);
1766 55144267 2022-09-07 thomas r = fcgi_vprintf(c, fmt, ap);
1767 55144267 2022-09-07 thomas va_end(ap);
1768 55144267 2022-09-07 thomas if (r == -1)
1769 55144267 2022-09-07 thomas return -1;
1770 55144267 2022-09-07 thomas
1771 55144267 2022-09-07 thomas if (fcgi_printf(c, "</a>"))
1772 55144267 2022-09-07 thomas return -1;
1773 55144267 2022-09-07 thomas return 0;
1774 55144267 2022-09-07 thomas }
1775 55144267 2022-09-07 thomas
1776 55e6cffd 2022-09-01 thomas static struct got_repository *
1777 55e6cffd 2022-09-01 thomas find_cached_repo(struct server *srv, const char *path)
1778 55e6cffd 2022-09-01 thomas {
1779 55e6cffd 2022-09-01 thomas int i;
1780 55e6cffd 2022-09-01 thomas
1781 55e6cffd 2022-09-01 thomas for (i = 0; i < srv->ncached_repos; i++) {
1782 55e6cffd 2022-09-01 thomas if (strcmp(srv->cached_repos[i].path, path) == 0)
1783 55e6cffd 2022-09-01 thomas return srv->cached_repos[i].repo;
1784 55e6cffd 2022-09-01 thomas }
1785 55e6cffd 2022-09-01 thomas
1786 55e6cffd 2022-09-01 thomas return NULL;
1787 55e6cffd 2022-09-01 thomas }
1788 55e6cffd 2022-09-01 thomas
1789 8a35f56c 2022-07-16 thomas static const struct got_error *
1790 55e6cffd 2022-09-01 thomas cache_repo(struct got_repository **new, struct server *srv,
1791 55e6cffd 2022-09-01 thomas struct repo_dir *repo_dir, struct socket *sock)
1792 55e6cffd 2022-09-01 thomas {
1793 55e6cffd 2022-09-01 thomas const struct got_error *error = NULL;
1794 55e6cffd 2022-09-01 thomas struct got_repository *repo;
1795 55e6cffd 2022-09-01 thomas struct cached_repo *cr;
1796 55e6cffd 2022-09-01 thomas int evicted = 0;
1797 55e6cffd 2022-09-01 thomas
1798 a004b24a 2022-09-06 thomas if (srv->ncached_repos >= GOTWEBD_REPO_CACHESIZE) {
1799 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos - 1];
1800 55e6cffd 2022-09-01 thomas error = got_repo_close(cr->repo);
1801 55e6cffd 2022-09-01 thomas memset(cr, 0, sizeof(*cr));
1802 55e6cffd 2022-09-01 thomas srv->ncached_repos--;
1803 55e6cffd 2022-09-01 thomas if (error)
1804 55e6cffd 2022-09-01 thomas return error;
1805 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[1], &srv->cached_repos[0],
1806 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1807 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[0];
1808 55e6cffd 2022-09-01 thomas evicted = 1;
1809 55e6cffd 2022-09-01 thomas } else {
1810 55e6cffd 2022-09-01 thomas cr = &srv->cached_repos[srv->ncached_repos];
1811 55e6cffd 2022-09-01 thomas }
1812 55e6cffd 2022-09-01 thomas
1813 55e6cffd 2022-09-01 thomas error = got_repo_open(&repo, repo_dir->path, NULL, sock->pack_fds);
1814 55e6cffd 2022-09-01 thomas if (error) {
1815 55e6cffd 2022-09-01 thomas if (evicted) {
1816 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1817 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1818 55e6cffd 2022-09-01 thomas }
1819 55e6cffd 2022-09-01 thomas return error;
1820 55e6cffd 2022-09-01 thomas }
1821 55e6cffd 2022-09-01 thomas
1822 55e6cffd 2022-09-01 thomas if (strlcpy(cr->path, repo_dir->path, sizeof(cr->path))
1823 55e6cffd 2022-09-01 thomas >= sizeof(cr->path)) {
1824 55e6cffd 2022-09-01 thomas if (evicted) {
1825 55e6cffd 2022-09-01 thomas memmove(&srv->cached_repos[0], &srv->cached_repos[1],
1826 55e6cffd 2022-09-01 thomas srv->ncached_repos * sizeof(srv->cached_repos[0]));
1827 55e6cffd 2022-09-01 thomas }
1828 55e6cffd 2022-09-01 thomas return got_error(GOT_ERR_NO_SPACE);
1829 55e6cffd 2022-09-01 thomas }
1830 55e6cffd 2022-09-01 thomas
1831 55e6cffd 2022-09-01 thomas cr->repo = repo;
1832 55e6cffd 2022-09-01 thomas srv->ncached_repos++;
1833 55e6cffd 2022-09-01 thomas *new = repo;
1834 55e6cffd 2022-09-01 thomas return NULL;
1835 55e6cffd 2022-09-01 thomas }
1836 55e6cffd 2022-09-01 thomas
1837 55e6cffd 2022-09-01 thomas static const struct got_error *
1838 8a35f56c 2022-07-16 thomas gotweb_load_got_path(struct request *c, struct repo_dir *repo_dir)
1839 8a35f56c 2022-07-16 thomas {
1840 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1841 8a35f56c 2022-07-16 thomas struct socket *sock = c->sock;
1842 8a35f56c 2022-07-16 thomas struct server *srv = c->srv;
1843 8a35f56c 2022-07-16 thomas struct transport *t = c->t;
1844 55e6cffd 2022-09-01 thomas struct got_repository *repo = NULL;
1845 8a35f56c 2022-07-16 thomas DIR *dt;
1846 8a35f56c 2022-07-16 thomas char *dir_test;
1847 8a35f56c 2022-07-16 thomas
1848 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s/%s", srv->repos_path, repo_dir->name,
1849 8a35f56c 2022-07-16 thomas GOTWEB_GIT_DIR) == -1)
1850 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
1851 8a35f56c 2022-07-16 thomas
1852 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1853 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1854 8a35f56c 2022-07-16 thomas free(dir_test);
1855 8a35f56c 2022-07-16 thomas } else {
1856 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1857 8a35f56c 2022-07-16 thomas dir_test = NULL;
1858 c2d3d9a0 2022-09-01 thomas goto done;
1859 8a35f56c 2022-07-16 thomas }
1860 8a35f56c 2022-07-16 thomas
1861 8a35f56c 2022-07-16 thomas if (asprintf(&dir_test, "%s/%s", srv->repos_path,
1862 c2d3d9a0 2022-09-01 thomas repo_dir->name) == -1)
1863 c2d3d9a0 2022-09-01 thomas return got_error_from_errno("asprintf");
1864 8a35f56c 2022-07-16 thomas
1865 8a35f56c 2022-07-16 thomas dt = opendir(dir_test);
1866 8a35f56c 2022-07-16 thomas if (dt == NULL) {
1867 8a35f56c 2022-07-16 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1868 8a35f56c 2022-07-16 thomas goto err;
1869 c2d3d9a0 2022-09-01 thomas } else {
1870 c2d3d9a0 2022-09-01 thomas repo_dir->path = dir_test;
1871 c2d3d9a0 2022-09-01 thomas dir_test = NULL;
1872 c2d3d9a0 2022-09-01 thomas }
1873 c2d3d9a0 2022-09-01 thomas
1874 8a35f56c 2022-07-16 thomas done:
1875 3991b2a5 2022-10-31 thomas if (srv->respect_exportok &&
1876 3991b2a5 2022-10-31 thomas faccessat(dirfd(dt), "git-daemon-export-ok", F_OK, 0) == -1) {
1877 3991b2a5 2022-10-31 thomas error = got_error_path(repo_dir->name, GOT_ERR_NOT_GIT_REPO);
1878 3991b2a5 2022-10-31 thomas goto err;
1879 3991b2a5 2022-10-31 thomas }
1880 3991b2a5 2022-10-31 thomas
1881 55e6cffd 2022-09-01 thomas repo = find_cached_repo(srv, repo_dir->path);
1882 55e6cffd 2022-09-01 thomas if (repo == NULL) {
1883 55e6cffd 2022-09-01 thomas error = cache_repo(&repo, srv, repo_dir, sock);
1884 55e6cffd 2022-09-01 thomas if (error)
1885 55e6cffd 2022-09-01 thomas goto err;
1886 55e6cffd 2022-09-01 thomas }
1887 55e6cffd 2022-09-01 thomas t->repo = repo;
1888 8a35f56c 2022-07-16 thomas error = gotweb_get_repo_description(&repo_dir->description, srv,
1889 4606e6d4 2022-11-23 thomas repo_dir->path, dirfd(dt));
1890 8a35f56c 2022-07-16 thomas if (error)
1891 8a35f56c 2022-07-16 thomas goto err;
1892 6c7f10f7 2022-11-23 thomas error = got_get_repo_owner(&repo_dir->owner, c);
1893 8a35f56c 2022-07-16 thomas if (error)
1894 8a35f56c 2022-07-16 thomas goto err;
1895 6c7f10f7 2022-11-23 thomas error = got_get_repo_age(&repo_dir->age, c, NULL, TM_DIFF);
1896 8a35f56c 2022-07-16 thomas if (error)
1897 8a35f56c 2022-07-16 thomas goto err;
1898 4606e6d4 2022-11-23 thomas error = gotweb_get_clone_url(&repo_dir->url, srv, repo_dir->path,
1899 4606e6d4 2022-11-23 thomas dirfd(dt));
1900 8a35f56c 2022-07-16 thomas err:
1901 8a35f56c 2022-07-16 thomas free(dir_test);
1902 c2d3d9a0 2022-09-01 thomas if (dt != NULL && closedir(dt) == EOF && error == NULL)
1903 c2d3d9a0 2022-09-01 thomas error = got_error_from_errno("closedir");
1904 8a35f56c 2022-07-16 thomas return error;
1905 8a35f56c 2022-07-16 thomas }
1906 8a35f56c 2022-07-16 thomas
1907 8a35f56c 2022-07-16 thomas static const struct got_error *
1908 8a35f56c 2022-07-16 thomas gotweb_init_repo_dir(struct repo_dir **repo_dir, const char *dir)
1909 8a35f56c 2022-07-16 thomas {
1910 8a35f56c 2022-07-16 thomas const struct got_error *error;
1911 8a35f56c 2022-07-16 thomas
1912 8a35f56c 2022-07-16 thomas *repo_dir = calloc(1, sizeof(**repo_dir));
1913 8a35f56c 2022-07-16 thomas if (*repo_dir == NULL)
1914 8a35f56c 2022-07-16 thomas return got_error_from_errno("calloc");
1915 8a35f56c 2022-07-16 thomas
1916 8a35f56c 2022-07-16 thomas if (asprintf(&(*repo_dir)->name, "%s", dir) == -1) {
1917 8a35f56c 2022-07-16 thomas error = got_error_from_errno("asprintf");
1918 8a35f56c 2022-07-16 thomas free(*repo_dir);
1919 8a35f56c 2022-07-16 thomas *repo_dir = NULL;
1920 8a35f56c 2022-07-16 thomas return error;
1921 8a35f56c 2022-07-16 thomas }
1922 8a35f56c 2022-07-16 thomas (*repo_dir)->owner = NULL;
1923 8a35f56c 2022-07-16 thomas (*repo_dir)->description = NULL;
1924 8a35f56c 2022-07-16 thomas (*repo_dir)->url = NULL;
1925 8a35f56c 2022-07-16 thomas (*repo_dir)->age = NULL;
1926 8a35f56c 2022-07-16 thomas (*repo_dir)->path = NULL;
1927 8a35f56c 2022-07-16 thomas
1928 8a35f56c 2022-07-16 thomas return NULL;
1929 8a35f56c 2022-07-16 thomas }
1930 8a35f56c 2022-07-16 thomas
1931 8a35f56c 2022-07-16 thomas static const struct got_error *
1932 4606e6d4 2022-11-23 thomas gotweb_get_repo_description(char **description, struct server *srv,
1933 4606e6d4 2022-11-23 thomas const char *dirpath, int dir)
1934 8a35f56c 2022-07-16 thomas {
1935 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1936 4606e6d4 2022-11-23 thomas struct stat sb;
1937 4606e6d4 2022-11-23 thomas int fd = -1;
1938 4606e6d4 2022-11-23 thomas off_t len;
1939 8a35f56c 2022-07-16 thomas
1940 8a35f56c 2022-07-16 thomas *description = NULL;
1941 8a35f56c 2022-07-16 thomas if (srv->show_repo_description == 0)
1942 8a35f56c 2022-07-16 thomas return NULL;
1943 8a35f56c 2022-07-16 thomas
1944 4606e6d4 2022-11-23 thomas fd = openat(dir, "description", O_RDONLY);
1945 4606e6d4 2022-11-23 thomas if (fd == -1) {
1946 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1947 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1948 4606e6d4 2022-11-23 thomas dirpath, "description");
1949 4606e6d4 2022-11-23 thomas }
1950 8a35f56c 2022-07-16 thomas goto done;
1951 8a35f56c 2022-07-16 thomas }
1952 8a35f56c 2022-07-16 thomas
1953 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
1954 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
1955 4606e6d4 2022-11-23 thomas dirpath, "description");
1956 8a35f56c 2022-07-16 thomas goto done;
1957 8a35f56c 2022-07-16 thomas }
1958 8a35f56c 2022-07-16 thomas
1959 4606e6d4 2022-11-23 thomas len = sb.st_size;
1960 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXDESCRSZ - 1)
1961 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXDESCRSZ - 1;
1962 8a35f56c 2022-07-16 thomas
1963 8a35f56c 2022-07-16 thomas *description = calloc(len + 1, sizeof(**description));
1964 8a35f56c 2022-07-16 thomas if (*description == NULL) {
1965 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
1966 8a35f56c 2022-07-16 thomas goto done;
1967 8a35f56c 2022-07-16 thomas }
1968 8a35f56c 2022-07-16 thomas
1969 4606e6d4 2022-11-23 thomas if (read(fd, *description, len) == -1)
1970 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
1971 8a35f56c 2022-07-16 thomas done:
1972 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
1973 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
1974 8a35f56c 2022-07-16 thomas return error;
1975 8a35f56c 2022-07-16 thomas }
1976 8a35f56c 2022-07-16 thomas
1977 8a35f56c 2022-07-16 thomas static const struct got_error *
1978 4606e6d4 2022-11-23 thomas gotweb_get_clone_url(char **url, struct server *srv, const char *dirpath,
1979 4606e6d4 2022-11-23 thomas int dir)
1980 8a35f56c 2022-07-16 thomas {
1981 8a35f56c 2022-07-16 thomas const struct got_error *error = NULL;
1982 4606e6d4 2022-11-23 thomas struct stat sb;
1983 4606e6d4 2022-11-23 thomas int fd = -1;
1984 4606e6d4 2022-11-23 thomas off_t len;
1985 8a35f56c 2022-07-16 thomas
1986 8a35f56c 2022-07-16 thomas *url = NULL;
1987 8a35f56c 2022-07-16 thomas if (srv->show_repo_cloneurl == 0)
1988 8a35f56c 2022-07-16 thomas return NULL;
1989 8a35f56c 2022-07-16 thomas
1990 4606e6d4 2022-11-23 thomas fd = openat(dir, "cloneurl", O_RDONLY);
1991 4606e6d4 2022-11-23 thomas if (fd == -1) {
1992 4606e6d4 2022-11-23 thomas if (errno != ENOENT && errno != EACCES) {
1993 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("openat %s/%s",
1994 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
1995 4606e6d4 2022-11-23 thomas }
1996 8a35f56c 2022-07-16 thomas goto done;
1997 8a35f56c 2022-07-16 thomas }
1998 8a35f56c 2022-07-16 thomas
1999 4606e6d4 2022-11-23 thomas if (fstat(fd, &sb) == -1) {
2000 4606e6d4 2022-11-23 thomas error = got_error_from_errno_fmt("fstat %s/%s",
2001 4606e6d4 2022-11-23 thomas dirpath, "cloneurl");
2002 8a35f56c 2022-07-16 thomas goto done;
2003 8a35f56c 2022-07-16 thomas }
2004 8a35f56c 2022-07-16 thomas
2005 4606e6d4 2022-11-23 thomas len = sb.st_size;
2006 3e9a56b5 2022-12-01 thomas if (len > GOTWEBD_MAXCLONEURLSZ - 1)
2007 3e9a56b5 2022-12-01 thomas len = GOTWEBD_MAXCLONEURLSZ - 1;
2008 8a35f56c 2022-07-16 thomas
2009 8a35f56c 2022-07-16 thomas *url = calloc(len + 1, sizeof(**url));
2010 8a35f56c 2022-07-16 thomas if (*url == NULL) {
2011 8a35f56c 2022-07-16 thomas error = got_error_from_errno("calloc");
2012 8a35f56c 2022-07-16 thomas goto done;
2013 8a35f56c 2022-07-16 thomas }
2014 8a35f56c 2022-07-16 thomas
2015 4606e6d4 2022-11-23 thomas if (read(fd, *url, len) == -1)
2016 4606e6d4 2022-11-23 thomas error = got_error_from_errno("read");
2017 8a35f56c 2022-07-16 thomas done:
2018 4606e6d4 2022-11-23 thomas if (fd != -1 && close(fd) == -1 && error == NULL)
2019 4606e6d4 2022-11-23 thomas error = got_error_from_errno("close");
2020 8a35f56c 2022-07-16 thomas return error;
2021 8a35f56c 2022-07-16 thomas }
2022 8a35f56c 2022-07-16 thomas
2023 8a35f56c 2022-07-16 thomas const struct got_error *
2024 8a35f56c 2022-07-16 thomas gotweb_get_time_str(char **repo_age, time_t committer_time, int ref_tm)
2025 8a35f56c 2022-07-16 thomas {
2026 8a35f56c 2022-07-16 thomas struct tm tm;
2027 399ea8e4 2022-07-20 thomas long long diff_time;
2028 8a35f56c 2022-07-16 thomas const char *years = "years ago", *months = "months ago";
2029 8a35f56c 2022-07-16 thomas const char *weeks = "weeks ago", *days = "days ago";
2030 8a35f56c 2022-07-16 thomas const char *hours = "hours ago", *minutes = "minutes ago";
2031 8a35f56c 2022-07-16 thomas const char *seconds = "seconds ago", *now = "right now";
2032 8a35f56c 2022-07-16 thomas char *s;
2033 d6795e9f 2022-12-30 thomas char datebuf[64];
2034 d6795e9f 2022-12-30 thomas size_t r;
2035 8a35f56c 2022-07-16 thomas
2036 8a35f56c 2022-07-16 thomas *repo_age = NULL;
2037 8a35f56c 2022-07-16 thomas
2038 8a35f56c 2022-07-16 thomas switch (ref_tm) {
2039 8a35f56c 2022-07-16 thomas case TM_DIFF:
2040 8a35f56c 2022-07-16 thomas diff_time = time(NULL) - committer_time;
2041 8a35f56c 2022-07-16 thomas if (diff_time > 60 * 60 * 24 * 365 * 2) {
2042 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2043 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 365), years) == -1)
2044 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2045 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
2046 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2047 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / (365 / 12)),
2048 8a35f56c 2022-07-16 thomas months) == -1)
2049 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2050 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
2051 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2052 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24 / 7), weeks) == -1)
2053 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2054 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 24 * 2) {
2055 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2056 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60 / 24), days) == -1)
2057 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2058 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 60 * 2) {
2059 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s",
2060 8a35f56c 2022-07-16 thomas (diff_time / 60 / 60), hours) == -1)
2061 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2062 8a35f56c 2022-07-16 thomas } else if (diff_time > 60 * 2) {
2063 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", (diff_time / 60),
2064 8a35f56c 2022-07-16 thomas minutes) == -1)
2065 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2066 8a35f56c 2022-07-16 thomas } else if (diff_time > 2) {
2067 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%lld %s", diff_time,
2068 8a35f56c 2022-07-16 thomas seconds) == -1)
2069 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2070 8a35f56c 2022-07-16 thomas } else {
2071 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s", now) == -1)
2072 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2073 8a35f56c 2022-07-16 thomas }
2074 8a35f56c 2022-07-16 thomas break;
2075 8a35f56c 2022-07-16 thomas case TM_LONG:
2076 8a35f56c 2022-07-16 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2077 8a35f56c 2022-07-16 thomas return got_error_from_errno("gmtime_r");
2078 8a35f56c 2022-07-16 thomas
2079 8a35f56c 2022-07-16 thomas s = asctime_r(&tm, datebuf);
2080 8a35f56c 2022-07-16 thomas if (s == NULL)
2081 8a35f56c 2022-07-16 thomas return got_error_from_errno("asctime_r");
2082 8a35f56c 2022-07-16 thomas
2083 8a35f56c 2022-07-16 thomas if (asprintf(repo_age, "%s UTC", datebuf) == -1)
2084 8a35f56c 2022-07-16 thomas return got_error_from_errno("asprintf");
2085 8a35f56c 2022-07-16 thomas break;
2086 d6795e9f 2022-12-30 thomas case TM_RFC822:
2087 d6795e9f 2022-12-30 thomas if (gmtime_r(&committer_time, &tm) == NULL)
2088 d6795e9f 2022-12-30 thomas return got_error_from_errno("gmtime_r");
2089 d6795e9f 2022-12-30 thomas
2090 d6795e9f 2022-12-30 thomas r = strftime(datebuf, sizeof(datebuf),
2091 d6795e9f 2022-12-30 thomas "%a, %d %b %Y %H:%M:%S GMT", &tm);
2092 d6795e9f 2022-12-30 thomas if (r == 0)
2093 d6795e9f 2022-12-30 thomas return got_error(GOT_ERR_NO_SPACE);
2094 d6795e9f 2022-12-30 thomas
2095 d6795e9f 2022-12-30 thomas *repo_age = strdup(datebuf);
2096 d6795e9f 2022-12-30 thomas if (*repo_age == NULL)
2097 d6795e9f 2022-12-30 thomas return got_error_from_errno("asprintf");
2098 d6795e9f 2022-12-30 thomas break;
2099 8a35f56c 2022-07-16 thomas }
2100 8a35f56c 2022-07-16 thomas return NULL;
2101 3e12c168 2022-07-16 thomas }